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 {
97  ListWrapper aList(*mxList);
98  std::vector<OUString> aTabNames = rDoc.GetAllTableNames();
99  SCTAB nTabCount = aTabNames.size();
100 
101  // tdf#92160 - too many results blow the widget's mind
102  size_t nMatchMax = rMatchedRanges.size();
103  if (nMatchMax > ListWrapper::mnMaximum)
104  nMatchMax = ListWrapper::mnMaximum;
105 
106  if (bCellNotes)
107  {
108  for (size_t i = 0, n = nMatchMax; i < n; ++i)
109  {
110  /* TODO: a CellNotes iterator would come handy and might speed
111  * things up a little, though we only loop through the
112  * search/replace result positions here. */
113  ScRange const & rRange( rMatchedRanges[i] );
114  // Bear in mind that mostly the range is one address position
115  // or a column or a row joined.
116  ScAddress aPos( rRange.aStart );
117  for ( ; aPos.Tab() <= rRange.aEnd.Tab(); aPos.IncTab())
118  {
119  if (aPos.Tab() >= nTabCount)
120  break; // can this even happen? we just searched on existing sheets ...
121  for (aPos.SetCol( rRange.aStart.Col()); aPos.Col() <= rRange.aEnd.Col(); aPos.IncCol())
122  {
123  for (aPos.SetRow( rRange.aStart.Row()); aPos.Row() <= rRange.aEnd.Row(); aPos.IncRow())
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  }
132  }
133  }
134  }
135  else
136  {
137  for (size_t i = 0, n = nMatchMax; i < n; ++i)
138  {
139  ScCellIterator aIter(rDoc, rMatchedRanges[i]);
140  for (bool bHas = aIter.first(); bHas; bHas = aIter.next())
141  {
142  const ScAddress& aPos = aIter.GetPos();
143  if (aPos.Tab() >= nTabCount)
144  // Out-of-bound sheet index.
145  continue;
146 
147  aList.Insert(aTabNames[aPos.Tab()], aPos,
148  rDoc.GetAddressConvention(),
149  rDoc.GetString(aPos));
150  }
151  }
152  }
153 
154  OUString aTotal(ScResId(SCSTR_TOTAL, aList.mnCount));
155  OUString aSearchResults = aTotal.replaceFirst("%1", OUString::number(aList.mnCount));
156  if (aList.mnCount > ListWrapper::mnMaximum)
157  aSearchResults += " " + ScGlobal::ReplaceOrAppend( aSkipped, "%1", OUString::number( ListWrapper::mnMaximum ) );
158  mxSearchResults->set_label(aSearchResults);
159 
160  mpDoc = &rDoc;
161 }
162 
164 {
165  if (mpBindings)
166  {
167  // Remove this dialog from the view frame after the dialog gets
168  // dismissed, else it would keep popping up endlessly!
169  SfxDispatcher* pDispacher = mpBindings ->GetDispatcher();
170  SfxBoolItem aItem(SID_SEARCH_RESULTS_DIALOG, false);
171  if (pDispacher)
172  {
173  pDispacher->ExecuteList(SID_SEARCH_RESULTS_DIALOG,
174  SfxCallMode::SYNCHRON | SfxCallMode::RECORD, { &aItem });
175  }
176  }
177 
179 }
180 
181 IMPL_LINK(SearchResultsDlg, HeaderBarClick, int, nColumn, void)
182 {
183  if (!mbSorted)
184  {
185  mxList->make_sorted();
186  mbSorted = true;
187  }
188 
189  bool bSortAtoZ = mxList->get_sort_order();
190 
191  //set new arrow positions in headerbar
192  if (nColumn == mxList->get_sort_column())
193  {
194  bSortAtoZ = !bSortAtoZ;
195  mxList->set_sort_order(bSortAtoZ);
196  }
197  else
198  {
199  int nOldSortColumn = mxList->get_sort_column();
200  if (nOldSortColumn != -1)
201  mxList->set_sort_indicator(TRISTATE_INDET, nOldSortColumn);
202  mxList->set_sort_column(nColumn);
203  }
204 
205  if (nColumn != -1)
206  {
207  //sort lists
208  mxList->set_sort_indicator(bSortAtoZ ? TRISTATE_TRUE : TRISTATE_FALSE, nColumn);
209  }
210 }
211 
213 {
214  if (!mpDoc)
215  return;
216 
217  int nEntry = mxList->get_selected_index();
218  OUString aTabStr = mxList->get_text(nEntry, 0);
219  OUString aPosStr = mxList->get_text(nEntry, 1);
220 
221  SCTAB nTab = -1;
222  if (!mpDoc->GetTable(aTabStr, nTab))
223  // No sheet with specified name.
224  return;
225 
226  ScAddress aPos;
227  ScRefFlags nRes = aPos.Parse(aPosStr, *mpDoc, mpDoc->GetAddressConvention());
228  if (!(nRes & ScRefFlags::VALID))
229  // Invalid address string.
230  return;
231 
232  // Jump to the cell.
234  pScViewShell->SetTabNo(nTab);
235  pScViewShell->SetCursor(aPos.Col(), aPos.Row());
236  pScViewShell->AlignToCursor(aPos.Col(), aPos.Row(), SC_FOLLOW_JUMP);
237 }
238 
239 IMPL_STATIC_LINK( SearchResultsDlg, OnShowToggled, weld::ToggleButton&, rButton, void )
240 {
242  ScViewOptions aViewOpt( pScViewShell->GetViewData().GetOptions() );
243  aViewOpt.SetOption( VOPT_SUMMARY, rButton.get_active() );
244  pScViewShell->GetViewData().SetOptions( aViewOpt );
245 }
246 
248  vcl::Window* _pParent, sal_uInt16 nId, SfxBindings* pBindings, SfxChildWinInfo* /*pInfo*/)
249  : SfxChildWindow(_pParent, nId)
250  , m_xDialog(std::make_shared<SearchResultsDlg>(pBindings, _pParent->GetFrameWeld()))
251 {
253 }
254 
256 
258 {
260  aInfo.bVisible = false;
261  return aInfo;
262 }
263 
264 SFX_IMPL_CHILDWINDOW_WITHID(SearchResultsDlgWrapper, SID_SEARCH_RESULTS_DIALOG);
265 
266 }
267 
268 /* 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:2107
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 * >())
std::unique_ptr< weld::Label > mxSearchResults
ScAddress aStart
Definition: address.hxx:500
SfxChildWindow * GetChildWindow(sal_uInt16)
void IncTab(SCTAB nDelta=1)
Definition: address.hxx:308
SCROW Row() const
Definition: address.hxx:262
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:501
std::shared_ptr< T > make_shared(Args &&...args)
static SC_DLLPUBLIC OUString ReplaceOrAppend(const OUString &rString, const OUString &rPlaceholder, const OUString &rReplacement)
Replaces the first occurrence of rPlaceholder in rString with rReplacement, or if rPlaceholder is not...
Definition: global.cxx:1069
SC_DLLPUBLIC void SetCursor(SCCOL nPosX, SCROW nPosY, bool bNew=false)
Definition: tabview3.cxx:358
TRISTATE_TRUE
SC_DLLPUBLIC formula::FormulaGrammar::AddressConvention GetAddressConvention() const
Definition: documen3.cxx:489
virtual void thaw()=0
SC_DLLPUBLIC ScPostIt * GetNote(const ScAddress &rPos)
Notes.
Definition: document.cxx:6508
void append_text(const OUString &rStr)
IMPL_LINK(SearchResultsDlg, HeaderBarClick, int, nColumn, void)
Additional class containing cell annotation data.
Definition: postit.hxx:160
virtual int n_children() const =0
SC_DLLPUBLIC OUString GetString(SCCOL nCol, SCROW nRow, SCTAB nTab, const ScInterpreterContext *pContext=nullptr) const
Definition: document.cxx:3486
void SetOptions(const ScViewOptions &rOpt)
Definition: viewdata.cxx:3870
Walk through all cells in an area.
Definition: dociter.hxx:208
SCTAB Tab() const
Definition: address.hxx:271
void SetRow(SCROW nRowP)
Definition: address.hxx:275
void SetCol(SCCOL nColP)
Definition: address.hxx:279
TRISTATE_INDET
ScViewData & GetViewData()
Definition: tabview.hxx:332
IMPL_STATIC_LINK(SearchResultsDlg, OnShowToggled, weld::ToggleButton &, rButton, void)
std::unique_ptr< weld::TreeView > mxList
IMPL_LINK_NOARG(SearchResultsDlg, ListSelectHdl, weld::TreeView &, void)
int i
void FillResults(ScDocument &rDoc, const ScRangeList &rMatchedRanges, bool bCellNotes)
void IncCol(SCCOL nDelta=1)
Definition: address.hxx:304
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:1793
size_t size() const
Definition: rangelst.hxx:90
void IncRow(SCROW nDelta=1)
Definition: address.hxx:300
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
OUString GetText() const
Returns the caption text of this note.
Definition: postit.cxx:913
SFX_IMPL_CHILDWINDOW_WITHID(SearchResultsDlgWrapper, SID_SEARCH_RESULTS_DIALOG)
SfxBindings * mpBindings
std::size_t mnCount
SCCOL Col() const
Definition: address.hxx:267
const ScAddress & GetPos() const
Definition: dociter.hxx:234
static ScTabViewShell * GetActiveViewShell()
Definition: tabvwsh4.cxx:1023
SfxViewFrame * GetFrame() const
void SetController(std::shared_ptr< SfxDialogController > controller)
Reference< XExecutableDialog > m_xDialog
const ScViewOptions & GetOptions() const
Definition: viewdata.hxx:537
std::shared_ptr< SearchResultsDlg > m_xDialog
void AlignToCursor(SCCOL nCurX, SCROW nCurY, ScFollowMode eMode, const ScSplitPos *pWhich=nullptr)
Definition: tabview3.cxx:911
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:1539
ScRefFlags
Definition: address.hxx:145
virtual ~SearchResultsDlg() override
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
virtual SfxChildWinInfo GetInfo() const
sal_uInt16 nPos
sal_Int16 SCTAB
Definition: types.hxx:23
SearchResultsDlgWrapper(vcl::Window *_pParent, sal_uInt16 nId, SfxBindings *pBindings, SfxChildWinInfo *pInfo)