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 <o3tl/safeint.hxx>
15#include <scresid.hxx>
16#include <globalnames.hxx>
17#include <namemgrtable.hxx>
18#include <rangenam.hxx>
19
21#include <vcl/weld.hxx>
22#include <tools/link.hxx>
23
25{
26 std::unique_ptr<weld::TreeIter> xCurrentEntry(m_xTreeView->make_iterator());
27 if (m_xTreeView->get_cursor(xCurrentEntry.get()))
28 GetLine(rLine, *xCurrentEntry);
29}
30
32{
33 std::vector<int> aRows = m_xTreeView->get_selected_rows();
34 std::sort(aRows.begin(), aRows.end());
35 for (auto it = aRows.rbegin(); it != aRows.rend(); ++it)
36 m_xTreeView->remove(*it);
37}
38
40{
41 return m_xTreeView->count_selected_rows() > 1;
42}
43
45{
46 for (int i = 0, nEntryCount = m_xTreeView->n_children(); i < nEntryCount; ++i)
47 {
48 if (rLine.aName == m_xTreeView->get_text(i, 0)
49 && rLine.aScope == m_xTreeView->get_text(i, 2))
50 {
51 m_xTreeView->set_cursor(i);
52 }
53 }
54}
55
57 std::unique_ptr<weld::TreeView> xTreeView,
58 const std::map<OUString, std::unique_ptr<ScRangeName>>& rRangeMap, const ScAddress& rPos)
59 : m_xTreeView(std::move(xTreeView))
60 , maGlobalString(ScResId(STR_GLOBAL_SCOPE))
61 , m_RangeMap(rRangeMap)
62 , maPos(rPos)
63 , m_nId(0)
64 , mbNeedUpdate(true)
65{
66 auto nColWidth = m_xTreeView->get_size_request().Width() / 7;
67 std::vector<int> aWidths{ o3tl::narrowing<int>(nColWidth * 2),
68 o3tl::narrowing<int>(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::getCharClass().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 = pData->GetSymbol(maPos);
107 m_xTreeView->set_text(rEntry, aFormulaString, 1);
108 maCalculatedFormulaEntries.insert(std::pair<OUString, bool>(sId, true));
109 }
110 return false;
111 };
112
113 // ensure all visible entries are up to date
114 m_xTreeView->visible_foreach(lambda);
115 // and ensure all selected entries are up to date
116 m_xTreeView->selected_foreach(lambda);
117}
118
119IMPL_LINK_NOARG(ScRangeManagerTable, SizeAllocHdl, const Size&, void) { CheckForFormulaString(); }
120
121void ScRangeManagerTable::addEntry(const ScRangeNameLine& rLine, bool bSetCurEntry)
122{
123 int nRow = m_xTreeView->n_children();
124 m_xTreeView->append();
125 m_xTreeView->set_text(nRow, rLine.aName, 0);
126 m_xTreeView->set_text(nRow, rLine.aExpression, 1);
127 m_xTreeView->set_text(nRow, rLine.aScope, 2);
128 // just unique to track which one has been cached by maCalculatedFormulaEntries
129 m_xTreeView->set_id(nRow, OUString::number(m_nId++));
130 if (bSetCurEntry)
131 m_xTreeView->set_cursor(nRow);
132}
133
135{
136 rLine.aName = m_xTreeView->get_text(rEntry, 0);
137 rLine.aExpression = m_xTreeView->get_text(rEntry, 1);
138 rLine.aScope = m_xTreeView->get_text(rEntry, 2);
139}
140
142{
143 m_xTreeView->freeze();
144 m_xTreeView->clear();
145 for (auto const& itr : m_RangeMap)
146 {
147 const ScRangeName* const pLocalRangeName = itr.second.get();
148 ScRangeNameLine aLine;
149 if (itr.first == STR_GLOBAL_RANGE_NAME)
150 aLine.aScope = maGlobalString;
151 else
152 aLine.aScope = itr.first;
153 for (const auto& rEntry : *pLocalRangeName)
154 {
155 if (!rEntry.second->HasType(ScRangeData::Type::Database))
156 {
157 aLine.aName = rEntry.second->GetName();
158 addEntry(aLine, false);
159 }
160 }
161 }
162 m_xTreeView->thaw();
163}
164
165std::vector<ScRangeNameLine> ScRangeManagerTable::GetSelectedEntries()
166{
167 std::vector<ScRangeNameLine> aSelectedEntries;
168 m_xTreeView->selected_foreach([this, &aSelectedEntries](weld::TreeIter& rEntry) {
169 ScRangeNameLine aLine;
170 GetLine(aLine, rEntry);
171 aSelectedEntries.push_back(aLine);
172 return false;
173 });
174 return aSelectedEntries;
175}
176
177/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::TreeView > m_xTreeView
static SC_DLLPUBLIC const CharClass & getCharClass()
Definition: global.cxx:1024
void addEntry(const ScRangeNameLine &rLine, bool bSetCurEntry)
const ScAddress maPos
void GetCurrentLine(ScRangeNameLine &rLine)
const std::map< OUString, std::unique_ptr< ScRangeName > > & m_RangeMap
void SetEntry(const ScRangeNameLine &rLine)
void GetLine(ScRangeNameLine &aLine, const weld::TreeIter &rEntry)
bool IsMultiSelection() const
ScRangeManagerTable(std::unique_ptr< weld::TreeView >, const std::map< OUString, std::unique_ptr< ScRangeName > > &rTabRangeNames, const ScAddress &rPos)
std::unique_ptr< weld::TreeView > m_xTreeView
std::map< OUString, bool > maCalculatedFormulaEntries
const ScRangeData * findRangeData(const ScRangeNameLine &rLine)
std::vector< ScRangeNameLine > GetSelectedEntries()
bool UpdatesBlocked() const
SC_DLLPUBLIC ScRangeData * findByUpperName(const OUString &rName)
Definition: rangenam.cxx:704
constexpr OUStringLiteral STR_GLOBAL_RANGE_NAME
Definition: globalnames.hxx:17
std::unique_ptr< sal_Int32[]> pData
IMPL_LINK_NOARG(ScRangeManagerTable, VisRowsScrolledHdl, weld::TreeView &, void)
int i
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
OUString aExpression
OUString sId