LibreOffice Module sc (master)  1
searchresults.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 #include <searchresults.hxx>
11 #include <sfx2/bindings.hxx>
12 #include <sfx2/dispatch.hxx>
13 #include <sfx2/viewfrm.hxx>
14 #include <svx/srchdlg.hxx>
15 #include <dociter.hxx>
16 #include <document.hxx>
17 #include <tabvwsh.hxx>
18 #include <strings.hrc>
19 #include <sc.hrc>
20 #include <scresid.hxx>
21 
22 namespace sc {
23 
25  : SfxDialogController(pParent, "modules/scalc/ui/searchresults.ui", "SearchResultsDialog")
26  , aSkipped(ScResId(SCSTR_SKIPPED))
27  , mpBindings(_pBindings)
28  , mpDoc(nullptr)
29  , mbSorted(false)
30  , mxList(m_xBuilder->weld_tree_view("results"))
31  , mxSearchResults(m_xBuilder->weld_label("lbSearchResults"))
32  , mxShowDialog(m_xBuilder->weld_check_button("cbShow"))
33 {
34  mxList->set_size_request(mxList->get_approximate_digit_width() * 50, mxList->get_height_rows(15));
35  mxShowDialog->connect_toggled(LINK(this, SearchResultsDlg, OnShowToggled));
36  std::vector<int> aWidths;
37  aWidths.push_back(mxList->get_approximate_digit_width() * 10);
38  aWidths.push_back(mxList->get_approximate_digit_width() * 10);
39  mxList->set_column_fixed_widths(aWidths);
40  mxList->connect_changed(LINK(this, SearchResultsDlg, ListSelectHdl));
41  mxList->connect_column_clicked(LINK(this, SearchResultsDlg, HeaderBarClick));
42 }
43 
45 {
46  // tdf#133807 if the search dialog is shown then re-present that dialog
47  // when this results dialog is dismissed
48  SfxViewFrame* pViewFrame = mpBindings->GetDispatcher()->GetFrame();
49  if (!pViewFrame)
50  return;
51  SfxChildWindow* pChildWindow = pViewFrame->GetChildWindow(
52  SvxSearchDialogWrapper::GetChildWindowId());
53  if (!pChildWindow)
54  return;
55  SvxSearchDialog* pSearchDlg = static_cast<SvxSearchDialog*>(pChildWindow->GetController().get());
56  if (!pSearchDlg)
57  return;
58  pSearchDlg->Present();
59 }
60 
61 namespace
62 {
63  class ListWrapper {
64  weld::TreeView& mrList;
65  public:
66  size_t mnCount = 0;
67  static const size_t mnMaximum = 1000;
68  ListWrapper(weld::TreeView& rList)
69  : mrList(rList)
70  {
71  mrList.clear();
72  mrList.freeze();
73  }
74  ~ListWrapper()
75  {
76  mrList.thaw();
77  }
78  void Insert(const OUString &rTabName,
79  const ScAddress &rPos,
81  const OUString &rText)
82  {
83  if (mnCount++ < mnMaximum)
84  {
85  mrList.append_text(rTabName);
86  int nPos = mrList.n_children() - 1;
87  mrList.set_text(nPos, rPos.Format(ScRefFlags::ADDR_ABS,
88  nullptr, eConvention), 1);
89  mrList.set_text(nPos, rText, 2);
90  }
91  }
92  };
93 }
94 
95 void SearchResultsDlg::FillResults( ScDocument& rDoc, const ScRangeList &rMatchedRanges, bool bCellNotes,
96  bool bEmptyCells )
97 {
98  ListWrapper aList(*mxList);
99  std::vector<OUString> aTabNames = rDoc.GetAllTableNames();
100  SCTAB nTabCount = aTabNames.size();
101 
102  // tdf#92160 - too many results blow the widget's mind
103  size_t nMatchMax = rMatchedRanges.size();
104  if (nMatchMax > ListWrapper::mnMaximum)
105  nMatchMax = ListWrapper::mnMaximum;
106 
107  if (bCellNotes || bEmptyCells)
108  {
109  for (size_t i = 0, n = nMatchMax; i < n; ++i)
110  {
111  ScRange const & rRange( rMatchedRanges[i] );
112  // Bear in mind that mostly the range is one address position
113  // or a column or a row joined.
114  ScAddress aPos( rRange.aStart );
115  for ( ; aPos.Tab() <= rRange.aEnd.Tab(); aPos.IncTab())
116  {
117  if (aPos.Tab() >= nTabCount)
118  break; // can this even happen? we just searched on existing sheets ...
119  for (aPos.SetCol( rRange.aStart.Col()); aPos.Col() <= rRange.aEnd.Col(); aPos.IncCol())
120  {
121  for (aPos.SetRow( rRange.aStart.Row()); aPos.Row() <= rRange.aEnd.Row(); aPos.IncRow())
122  {
123  if (bCellNotes)
124  {
125  const ScPostIt* pNote = rDoc.GetNote( aPos);
126  if (pNote)
127  aList.Insert(aTabNames[aPos.Tab()], aPos,
128  rDoc.GetAddressConvention(),
129  pNote->GetText());
130  }
131  else // bEmptyCells
132  {
133  aList.Insert(aTabNames[aPos.Tab()], aPos,
134  rDoc.GetAddressConvention(),
135  rDoc.GetString(aPos));
136  }
137  }
138  }
139  }
140  }
141  }
142  else
143  {
144  for (size_t i = 0, n = nMatchMax; i < n; ++i)
145  {
146  ScCellIterator aIter(rDoc, rMatchedRanges[i]);
147  for (bool bHas = aIter.first(); bHas; bHas = aIter.next())
148  {
149  const ScAddress& aPos = aIter.GetPos();
150  if (aPos.Tab() >= nTabCount)
151  // Out-of-bound sheet index.
152  continue;
153 
154  aList.Insert(aTabNames[aPos.Tab()], aPos,
155  rDoc.GetAddressConvention(),
156  rDoc.GetString(aPos));
157  }
158  }
159  }
160 
161  OUString aTotal(ScResId(SCSTR_TOTAL, aList.mnCount));
162  OUString aSearchResults = aTotal.replaceFirst("%1", OUString::number(aList.mnCount));
163  if (aList.mnCount > ListWrapper::mnMaximum)
164  aSearchResults += " " + ScGlobal::ReplaceOrAppend( aSkipped, u"%1", OUString::number( ListWrapper::mnMaximum ) );
165  mxSearchResults->set_label(aSearchResults);
166 
167  mpDoc = &rDoc;
168 }
169 
171 {
172  if (mpBindings)
173  {
174  // Remove this dialog from the view frame after the dialog gets
175  // dismissed, else it would keep popping up endlessly!
176  SfxDispatcher* pDispacher = mpBindings ->GetDispatcher();
177  SfxBoolItem aItem(SID_SEARCH_RESULTS_DIALOG, false);
178  if (pDispacher)
179  {
180  pDispacher->ExecuteList(SID_SEARCH_RESULTS_DIALOG,
181  SfxCallMode::SYNCHRON | SfxCallMode::RECORD, { &aItem });
182  }
183  }
184 
186 }
187 
188 IMPL_LINK(SearchResultsDlg, HeaderBarClick, int, nColumn, void)
189 {
190  if (!mbSorted)
191  {
192  mxList->make_sorted();
193  mbSorted = true;
194  }
195 
196  bool bSortAtoZ = mxList->get_sort_order();
197 
198  //set new arrow positions in headerbar
199  if (nColumn == mxList->get_sort_column())
200  {
201  bSortAtoZ = !bSortAtoZ;
202  mxList->set_sort_order(bSortAtoZ);
203  }
204  else
205  {
206  int nOldSortColumn = mxList->get_sort_column();
207  if (nOldSortColumn != -1)
208  mxList->set_sort_indicator(TRISTATE_INDET, nOldSortColumn);
209  mxList->set_sort_column(nColumn);
210  }
211 
212  if (nColumn != -1)
213  {
214  //sort lists
215  mxList->set_sort_indicator(bSortAtoZ ? TRISTATE_TRUE : TRISTATE_FALSE, nColumn);
216  }
217 }
218 
220 {
221  if (!mpDoc)
222  return;
223 
224  int nEntry = mxList->get_selected_index();
225  OUString aTabStr = mxList->get_text(nEntry, 0);
226  OUString aPosStr = mxList->get_text(nEntry, 1);
227 
228  SCTAB nTab = -1;
229  if (!mpDoc->GetTable(aTabStr, nTab))
230  // No sheet with specified name.
231  return;
232 
233  ScAddress aPos;
234  ScRefFlags nRes = aPos.Parse(aPosStr, *mpDoc, mpDoc->GetAddressConvention());
235  if (!(nRes & ScRefFlags::VALID))
236  // Invalid address string.
237  return;
238 
239  // Jump to the cell.
241  pScViewShell->SetTabNo(nTab);
242  pScViewShell->SetCursor(aPos.Col(), aPos.Row());
243  pScViewShell->AlignToCursor(aPos.Col(), aPos.Row(), SC_FOLLOW_JUMP);
244 }
245 
246 IMPL_STATIC_LINK( SearchResultsDlg, OnShowToggled, weld::Toggleable&, rButton, void )
247 {
249  ScViewOptions aViewOpt( pScViewShell->GetViewData().GetOptions() );
250  aViewOpt.SetOption( VOPT_SUMMARY, rButton.get_active() );
251  pScViewShell->GetViewData().SetOptions( aViewOpt );
252 }
253 
255  vcl::Window* _pParent, sal_uInt16 nId, SfxBindings* pBindings, SfxChildWinInfo* /*pInfo*/)
256  : SfxChildWindow(_pParent, nId)
257  , m_xDialog(std::make_shared<SearchResultsDlg>(pBindings, _pParent->GetFrameWeld()))
258 {
260 }
261 
263 
265 {
267  aInfo.bVisible = false;
268  return aInfo;
269 }
270 
271 SFX_IMPL_CHILDWINDOW_WITHID(SearchResultsDlgWrapper, SID_SEARCH_RESULTS_DIALOG);
272 
273 }
274 
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void set_text(int row, const OUString &rText, int col=-1)=0
std::shared_ptr< SfxDialogController > & GetController()
void SetOption(ScViewOption eOpt, bool bNew)
Definition: viewopti.hxx:87
virtual void Close()
SC_DLLPUBLIC void Format(OStringBuffer &r, ScRefFlags nFlags, const ScDocument *pDocument=nullptr, const Details &rDetails=detailsOOOa1) const
Definition: address.cxx:2116
const SfxPoolItem * ExecuteList(sal_uInt16 nSlot, SfxCallMode nCall, std::initializer_list< SfxPoolItem const * > args, std::initializer_list< SfxPoolItem const * > internalargs=std::initializer_list< SfxPoolItem const * >())
static SC_DLLPUBLIC OUString ReplaceOrAppend(const OUString &rString, std::u16string_view rPlaceholder, const OUString &rReplacement)
Replaces the first occurrence of rPlaceholder in rString with rReplacement, or if rPlaceholder is not...
Definition: global.cxx:1093
void FillResults(ScDocument &rDoc, const ScRangeList &rMatchedRanges, bool bCellNotes, bool bEmptyCells)
std::unique_ptr< weld::Label > mxSearchResults
ScAddress aStart
Definition: address.hxx:499
SfxChildWindow * GetChildWindow(sal_uInt16)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:89
void IncTab(SCTAB nDelta=1)
Definition: address.hxx:307
SCROW Row() const
Definition: address.hxx:261
SC_DLLPUBLIC std::vector< OUString > GetAllTableNames() const
Definition: document.cxx:287
sal_Int64 n
virtual ~SearchResultsDlgWrapper() override
virtual void clear()=0
ScAddress aEnd
Definition: address.hxx:500
std::shared_ptr< T > make_shared(Args &&...args)
SC_DLLPUBLIC void SetCursor(SCCOL nPosX, SCROW nPosY, bool bNew=false)
Definition: tabview3.cxx:363
TRISTATE_TRUE
SC_DLLPUBLIC formula::FormulaGrammar::AddressConvention GetAddressConvention() const
Definition: documen3.cxx:494
virtual void thaw()=0
SC_DLLPUBLIC ScPostIt * GetNote(const ScAddress &rPos)
Notes.
Definition: document.cxx:6534
void append_text(const OUString &rStr)
IMPL_LINK(SearchResultsDlg, HeaderBarClick, int, nColumn, void)
Additional class containing cell annotation data.
Definition: postit.hxx:159
virtual int n_children() const =0
SC_DLLPUBLIC OUString GetString(SCCOL nCol, SCROW nRow, SCTAB nTab, const ScInterpreterContext *pContext=nullptr) const
Definition: document.cxx:3517
void SetOptions(const ScViewOptions &rOpt)
Definition: viewdata.cxx:3939
Walk through all cells in an area.
Definition: dociter.hxx:207
SCTAB Tab() const
Definition: address.hxx:270
void SetRow(SCROW nRowP)
Definition: address.hxx:274
void SetCol(SCCOL nColP)
Definition: address.hxx:278
TRISTATE_INDET
ScViewData & GetViewData()
Definition: tabview.hxx:333
std::unique_ptr< weld::TreeView > mxList
IMPL_LINK_NOARG(SearchResultsDlg, ListSelectHdl, weld::TreeView &, void)
int i
void IncCol(SCCOL nDelta=1)
Definition: address.hxx:303
std::unique_ptr< weld::CheckButton > mxShowDialog
virtual void freeze()=0
TRISTATE_FALSE
SC_DLLPUBLIC void SetTabNo(SCTAB nTab, bool bNew=false, bool bExtendSelection=false, bool bSameTabButMoved=false)
Definition: tabview3.cxx:1798
size_t size() const
Definition: rangelst.hxx:89
void IncRow(SCROW nDelta=1)
Definition: address.hxx:299
float u
OUString GetText() const
Returns the caption text of this note.
Definition: postit.cxx:914
SFX_IMPL_CHILDWINDOW_WITHID(SearchResultsDlgWrapper, SID_SEARCH_RESULTS_DIALOG)
SfxBindings * mpBindings
std::size_t mnCount
SCCOL Col() const
Definition: address.hxx:266
const ScAddress & GetPos() const
Definition: dociter.hxx:233
static ScTabViewShell * GetActiveViewShell()
Definition: tabvwsh4.cxx:1038
SfxViewFrame * GetFrame() const
void SetController(std::shared_ptr< SfxDialogController > controller)
Reference< XExecutableDialog > m_xDialog
const ScViewOptions & GetOptions() const
Definition: viewdata.hxx:544
std::shared_ptr< SearchResultsDlg > m_xDialog
void AlignToCursor(SCCOL nCurX, SCROW nCurY, ScFollowMode eMode, const ScSplitPos *pWhich=nullptr)
Definition: tabview3.cxx:916
virtual void Close() override
SfxDispatcher * GetDispatcher() const
SearchResultsDlg(SfxBindings *_pBindings, weld::Window *pParent)
SC_DLLPUBLIC ScRefFlags Parse(const OUString &, const ScDocument &, const Details &rDetails=detailsOOOa1, ExternalInfo *pExtInfo=nullptr, const css::uno::Sequence< css::sheet::ExternalLinkInfo > *pExternalLinks=nullptr, sal_Int32 *pSheetEndPos=nullptr, const OUString *pErrRef=nullptr)
Definition: address.cxx:1548
ScRefFlags
Definition: address.hxx:144
virtual ~SearchResultsDlg() override
IMPL_STATIC_LINK(SearchResultsDlg, OnShowToggled, weld::Toggleable &, rButton, void)
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
virtual SfxChildWinInfo GetInfo() const
sal_uInt16 nPos
sal_Int16 SCTAB
Definition: types.hxx:22
SearchResultsDlgWrapper(vcl::Window *_pParent, sal_uInt16 nId, SfxBindings *pBindings, SfxChildWinInfo *pInfo)