LibreOffice Module sc (master)  1
namemgrtable.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 //ScRangeManagerTable
11 #include <memory>
12 #include <global.hxx>
13 #include <globstr.hrc>
14 #include <scresid.hxx>
15 #include <globalnames.hxx>
16 #include <namemgrtable.hxx>
17 #include <rangenam.hxx>
18 
19 #include <unotools/charclass.hxx>
20 #include <vcl/weld.hxx>
21 #include <tools/link.hxx>
22 
24 {
25  std::unique_ptr<weld::TreeIter> xCurrentEntry(m_xTreeView->make_iterator());
26  if (m_xTreeView->get_cursor(xCurrentEntry.get()))
27  GetLine(rLine, *xCurrentEntry);
28 }
29 
31 {
32  std::vector<int> aRows = m_xTreeView->get_selected_rows();
33  std::sort(aRows.begin(), aRows.end());
34  for (auto it = aRows.rbegin(); it != aRows.rend(); ++it)
35  m_xTreeView->remove(*it);
36 }
37 
39 {
40  return m_xTreeView->count_selected_rows() > 1;
41 }
42 
44 {
45  for (int i = 0, nEntryCount = m_xTreeView->n_children(); i < nEntryCount; ++i)
46  {
47  if (rLine.aName == m_xTreeView->get_text(i, 0)
48  && rLine.aScope == m_xTreeView->get_text(i, 2))
49  {
50  m_xTreeView->set_cursor(i);
51  }
52  }
53 }
54 
56  std::unique_ptr<weld::TreeView> xTreeView,
57  const std::map<OUString, std::unique_ptr<ScRangeName>>& rRangeMap, const ScAddress& rPos)
58  : m_xTreeView(std::move(xTreeView))
59  , maGlobalString(ScResId(STR_GLOBAL_SCOPE))
60  , m_RangeMap(rRangeMap)
61  , maPos(rPos)
62  , m_nId(0)
63  , mbNeedUpdate(true)
64 {
65  auto nColWidth = m_xTreeView->get_size_request().Width() / 7;
66  std::vector<int> aWidths;
67  aWidths.push_back(nColWidth * 2);
68  aWidths.push_back(nColWidth * 3);
69  m_xTreeView->set_column_fixed_widths(aWidths);
70 
71  Init();
72  m_xTreeView->set_selection_mode(SelectionMode::Multiple);
73  m_xTreeView->connect_size_allocate(LINK(this, ScRangeManagerTable, SizeAllocHdl));
74  m_xTreeView->connect_visible_range_changed(LINK(this, ScRangeManagerTable, VisRowsScrolledHdl));
75 }
76 
78 {
79  CheckForFormulaString();
80 }
81 
83 {
84  const ScRangeName* pRangeName;
85  if (rLine.aScope == maGlobalString)
86  pRangeName = m_RangeMap.find(OUString(STR_GLOBAL_RANGE_NAME))->second.get();
87  else
88  pRangeName = m_RangeMap.find(rLine.aScope)->second.get();
89 
90  return pRangeName->findByUpperName(ScGlobal::getCharClassPtr()->uppercase(rLine.aName));
91 }
92 
94 {
95  if (UpdatesBlocked())
96  return;
97 
98  auto lambda = [this](weld::TreeIter& rEntry) {
99  OUString sId(m_xTreeView->get_id(rEntry));
100  std::map<OUString, bool>::const_iterator itr = maCalculatedFormulaEntries.find(sId);
101  if (itr == maCalculatedFormulaEntries.end() || !itr->second)
102  {
103  ScRangeNameLine aLine;
104  GetLine(aLine, rEntry);
105  const ScRangeData* pData = findRangeData(aLine);
106  OUString aFormulaString;
107  pData->GetSymbol(aFormulaString, maPos);
108  m_xTreeView->set_text(rEntry, aFormulaString, 1);
109  maCalculatedFormulaEntries.insert(std::pair<OUString, bool>(sId, true));
110  }
111  return false;
112  };
113 
114  // ensure all visible entries are up to date
115  m_xTreeView->visible_foreach(lambda);
116  // and ensure all selected entries are up to date
117  m_xTreeView->selected_foreach(lambda);
118 }
119 
120 IMPL_LINK_NOARG(ScRangeManagerTable, SizeAllocHdl, const Size&, void) { CheckForFormulaString(); }
121 
122 void ScRangeManagerTable::addEntry(const ScRangeNameLine& rLine, bool bSetCurEntry)
123 {
124  int nRow = m_xTreeView->n_children();
125  m_xTreeView->append();
126  m_xTreeView->set_text(nRow, rLine.aName, 0);
127  m_xTreeView->set_text(nRow, rLine.aExpression, 1);
128  m_xTreeView->set_text(nRow, rLine.aScope, 2);
129  // just unique to track which one has been cached by maCalculatedFormulaEntries
130  m_xTreeView->set_id(nRow, OUString::number(m_nId++));
131  if (bSetCurEntry)
132  m_xTreeView->set_cursor(nRow);
133 }
134 
136 {
137  rLine.aName = m_xTreeView->get_text(rEntry, 0);
138  rLine.aExpression = m_xTreeView->get_text(rEntry, 1);
139  rLine.aScope = m_xTreeView->get_text(rEntry, 2);
140 }
141 
143 {
144  m_xTreeView->freeze();
145  m_xTreeView->clear();
146  for (auto const& itr : m_RangeMap)
147  {
148  const ScRangeName* const pLocalRangeName = itr.second.get();
149  ScRangeNameLine aLine;
150  if (itr.first == STR_GLOBAL_RANGE_NAME)
151  aLine.aScope = maGlobalString;
152  else
153  aLine.aScope = itr.first;
154  for (const auto& rEntry : *pLocalRangeName)
155  {
156  if (!rEntry.second->HasType(ScRangeData::Type::Database))
157  {
158  aLine.aName = rEntry.second->GetName();
159  addEntry(aLine, false);
160  }
161  }
162  }
163  m_xTreeView->thaw();
164 }
165 
166 std::vector<ScRangeNameLine> ScRangeManagerTable::GetSelectedEntries()
167 {
168  std::vector<ScRangeNameLine> aSelectedEntries;
169  m_xTreeView->selected_foreach([this, &aSelectedEntries](weld::TreeIter& rEntry) {
170  ScRangeNameLine aLine;
171  GetLine(aLine, rEntry);
172  aSelectedEntries.push_back(aLine);
173  return false;
174  });
175  return aSelectedEntries;
176 }
177 
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::map< OUString, bool > maCalculatedFormulaEntries
void GetCurrentLine(ScRangeNameLine &rLine)
std::unique_ptr< ContentProperties > pData
void SetEntry(const ScRangeNameLine &rLine)
const ScRangeData * findRangeData(const ScRangeNameLine &rLine)
OUString aExpression
SC_DLLPUBLIC ScRangeData * findByUpperName(const OUString &rName)
Definition: rangenam.cxx:681
#define STR_GLOBAL_RANGE_NAME
Definition: globalnames.hxx:15
SC_DLLPUBLIC void GetSymbol(OUString &rSymbol, const formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT) const
Definition: rangenam.cxx:243
int i
const ScAddress maPos
void GetLine(ScRangeNameLine &aLine, const weld::TreeIter &rEntry)
ScRangeManagerTable(std::unique_ptr< weld::TreeView >, const std::map< OUString, std::unique_ptr< ScRangeName >> &rTabRangeNames, const ScAddress &rPos)
bool IsMultiSelection() const
const std::map< OUString, std::unique_ptr< ScRangeName > > & m_RangeMap
OUString ScResId(std::string_view aId)
Definition: scdll.cxx:89
static SC_DLLPUBLIC const CharClass * getCharClassPtr()
Definition: global.cxx:1009
OUString sId
IMPL_LINK_NOARG(ScRangeManagerTable, VisRowsScrolledHdl, weld::TreeView &, void)
void addEntry(const ScRangeNameLine &rLine, bool bSetCurEntry)
std::unique_ptr< weld::TreeView > m_xTreeView
std::vector< ScRangeNameLine > GetSelectedEntries()
bool UpdatesBlocked() const