LibreOffice Module cui (master)  1
thesdlg.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <thesdlg.hxx>
21 
22 #include <tools/debug.hxx>
23 #include <svl/lngmisc.hxx>
24 #include <vcl/event.hxx>
25 #include <vcl/svapp.hxx>
26 #include <svtools/langtab.hxx>
28 #include <comphelper/string.hxx>
29 
30 #include <stack>
31 #include <algorithm>
32 
33 #include <com/sun/star/linguistic2/XThesaurus.hpp>
34 #include <com/sun/star/linguistic2/XMeaning.hpp>
35 
36 using namespace ::com::sun::star;
37 
38 IMPL_LINK_NOARG( SvxThesaurusDialog, ModifyTimer_Hdl, Timer *, void )
39 {
40  LookUp(m_xWordCB->get_active_text());
41  m_aModifyIdle.Stop();
42 }
43 
44 IMPL_LINK_NOARG(SvxThesaurusDialog, ReplaceEditHdl_Impl, weld::Entry&, void)
45 {
46  m_xReplaceBtn->set_sensitive(!m_xReplaceEdit->get_text().isEmpty());
47 }
48 
49 IMPL_LINK(SvxThesaurusDialog, KeyInputHdl, const KeyEvent&, rKEvt, bool)
50 {
51  const vcl::KeyCode& rKey = rKEvt.GetKeyCode();
52 
53  if (rKey.GetCode() == KEY_RETURN)
54  {
55  m_xDialog->response(RET_OK);
56  return true;
57  }
58 
59  return false;
60 }
61 
62 uno::Sequence< uno::Reference< linguistic2::XMeaning > > SvxThesaurusDialog::queryMeanings_Impl(
63  OUString& rTerm,
64  const lang::Locale& rLocale,
65  const beans::PropertyValues& rProperties )
66 {
67  uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeanings(
68  xThesaurus->queryMeanings( rTerm, rLocale, rProperties ) );
69 
70  // text with '.' at the end?
71  if ( !aMeanings.hasElements() && rTerm.endsWith(".") )
72  {
73  // try again without trailing '.' chars. It may be a word at the
74  // end of a sentence and not an abbreviation...
75  OUString aTxt(comphelper::string::stripEnd(rTerm, '.'));
76  aMeanings = xThesaurus->queryMeanings( aTxt, rLocale, rProperties );
77  if (aMeanings.hasElements())
78  {
79  rTerm = aTxt;
80  }
81  }
82 
83  return aMeanings;
84 }
85 
87 {
88  lang::Locale aLocale( LanguageTag::convertToLocale( nLookUpLanguage ) );
89  uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeanings = queryMeanings_Impl(
90  aLookUpText, aLocale, uno::Sequence< beans::PropertyValue >() );
91  const sal_Int32 nMeanings = aMeanings.getLength();
92  const uno::Reference< linguistic2::XMeaning > *pMeanings = aMeanings.getConstArray();
93 
94  m_xAlternativesCT->freeze();
95 
96  m_xAlternativesCT->clear();
97  int nRow = 0;
98  for (sal_Int32 i = 0; i < nMeanings; ++i)
99  {
100  OUString rMeaningTxt = pMeanings[i]->getMeaning();
101  uno::Sequence< OUString > aSynonyms( pMeanings[i]->querySynonyms() );
102  const sal_Int32 nSynonyms = aSynonyms.getLength();
103  const OUString *pSynonyms = aSynonyms.getConstArray();
104  DBG_ASSERT( !rMeaningTxt.isEmpty(), "meaning with empty text" );
105  DBG_ASSERT( nSynonyms > 0, "meaning without synonym" );
106 
107  OUString sHeading = OUString::number(i + 1) + ". " + rMeaningTxt;
108  m_xAlternativesCT->append_text(sHeading);
109  m_xAlternativesCT->set_text_emphasis(nRow, true, 0);
110  ++nRow;
111 
112  for (sal_Int32 k = 0; k < nSynonyms; ++k)
113  {
114  // GetThesaurusReplaceText will strip the leading spaces
115  m_xAlternativesCT->append_text(" " + pSynonyms[k]);
116  m_xAlternativesCT->set_text_emphasis(nRow, false, 0);
117  ++nRow;
118  }
119  }
120 
121  m_xAlternativesCT->thaw();
122 
123  return nMeanings > 0;
124 }
125 
126 void SvxThesaurusDialog::LookUp( const OUString &rText )
127 {
128  if (rText != m_xWordCB->get_active_text()) // avoid moving of the cursor if the text is the same
129  m_xWordCB->set_entry_text(rText);
130  LookUp_Impl();
131 }
132 
134 {
135  if (aLookUpHistory.size() >= 2)
136  {
137  aLookUpHistory.pop(); // remove current look up word from stack
138  m_xWordCB->set_entry_text(aLookUpHistory.top()); // retrieve previous look up word
139  aLookUpHistory.pop();
140  LookUp_Impl();
141  }
142 }
143 
144 IMPL_LINK( SvxThesaurusDialog, LanguageHdl_Impl, weld::ComboBox&, rLB, void )
145 {
146  OUString aLangText(rLB.get_active_text());
147  LanguageType nLang = SvtLanguageTable::GetLanguageType( aLangText );
148  DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
149  if (xThesaurus->hasLocale( LanguageTag::convertToLocale( nLang ) ))
150  nLookUpLanguage = nLang;
151  SetWindowTitle( nLang );
152  LookUp_Impl();
153 }
154 
156 {
157  OUString aText(m_xWordCB->get_active_text());
158 
159  aLookUpText = aText;
160  if (!aLookUpText.isEmpty() &&
161  (aLookUpHistory.empty() || aLookUpText != aLookUpHistory.top()))
162  aLookUpHistory.push( aLookUpText );
163 
165  m_xAlternativesCT->set_visible(m_bWordFound);
166  m_xNotFound->set_visible(!m_bWordFound);
167 
168  if (m_bWordFound)
169  Application::PostUserEvent(LINK(this, SvxThesaurusDialog, SelectFirstHdl_Impl));
170 
171  if (m_xWordCB->find_text(aText) == -1)
172  m_xWordCB->append_text(aText);
173 
174  m_xReplaceEdit->set_text( OUString() );
175  ReplaceEditHdl_Impl(*m_xReplaceEdit);
176  m_xLeftBtn->set_sensitive( aLookUpHistory.size() > 1 );
177 }
178 
180 {
181  m_aModifyIdle.Start();
182 }
183 
184 IMPL_LINK( SvxThesaurusDialog, AlternativesSelectHdl_Impl, weld::TreeView&, rBox, void )
185 {
186  int nEntry = rBox.get_selected_index();
187  if (nEntry != -1)
188  {
189  bool bIsHeader = rBox.get_text_emphasis(nEntry, 0);
190  if (bIsHeader)
191  {
192  ++nEntry;
193  rBox.select(nEntry);
194  }
195  OUString aStr = linguistic::GetThesaurusReplaceText(rBox.get_text(nEntry));
196  m_xReplaceEdit->set_text(aStr);
197  ReplaceEditHdl_Impl(*m_xReplaceEdit);
198  }
199 }
200 
201 IMPL_LINK( SvxThesaurusDialog, AlternativesDoubleClickHdl_Impl, weld::TreeView&, rBox, bool )
202 {
203  int nEntry = rBox.get_selected_index();
204  if (nEntry != -1)
205  {
206  bool bIsHeader = rBox.get_text_emphasis(nEntry, 0);
207  if (bIsHeader)
208  {
209  ++nEntry;
210  rBox.select(nEntry);
211  }
212  OUString aStr = linguistic::GetThesaurusReplaceText(rBox.get_text(nEntry));
213  m_xWordCB->set_entry_text(aStr);
214  if (!aStr.isEmpty())
215  LookUp_Impl();
216  }
217 
220  Application::PostUserEvent(LINK(this, SvxThesaurusDialog, SelectFirstHdl_Impl));
221 
222  return true;
223 }
224 
225 IMPL_LINK_NOARG(SvxThesaurusDialog, SelectFirstHdl_Impl, void *, void)
226 {
227  if (m_xAlternativesCT->n_children() >= 2)
228  {
229  m_xAlternativesCT->select(1); // pos 0 is a 'header' that is not selectable
230  AlternativesSelectHdl_Impl(*m_xAlternativesCT);
231  }
232 }
233 
234 // class SvxThesaurusDialog ----------------------------------------------
235 
237  weld::Window* pParent,
238  uno::Reference< linguistic2::XThesaurus > const & xThes,
239  const OUString &rWord,
240  LanguageType nLanguage)
241  : SfxDialogController(pParent, "cui/ui/thesaurus.ui", "ThesaurusDialog")
242  , m_aModifyIdle("cui SvxThesaurusDialog LookUp Modify")
243  , aLookUpText()
244  , nLookUpLanguage(LANGUAGE_NONE)
245  , m_bWordFound(false)
246  , m_xLeftBtn(m_xBuilder->weld_button("left"))
247  , m_xWordCB(m_xBuilder->weld_combo_box("wordcb"))
248  , m_xAlternativesCT(m_xBuilder->weld_tree_view("alternatives"))
249  , m_xNotFound(m_xBuilder->weld_label("notfound"))
250  , m_xReplaceEdit(m_xBuilder->weld_entry("replaceed"))
251  , m_xLangLB(m_xBuilder->weld_combo_box("langcb"))
252  , m_xReplaceBtn(m_xBuilder->weld_button("ok"))
253 {
254  m_aModifyIdle.SetInvokeHandler( LINK( this, SvxThesaurusDialog, ModifyTimer_Hdl ) );
255  m_aModifyIdle.SetPriority( TaskPriority::LOWEST );
256 
257  m_xReplaceEdit->connect_changed( LINK( this, SvxThesaurusDialog, ReplaceEditHdl_Impl ) );
258  m_xReplaceBtn->connect_clicked( LINK( this, SvxThesaurusDialog, ReplaceBtnHdl_Impl ) );
259  m_xLeftBtn->connect_clicked( LINK( this, SvxThesaurusDialog, LeftBtnHdl_Impl ) );
260  m_xWordCB->set_entry_completion(false);
261  m_xWordCB->connect_changed( LINK( this, SvxThesaurusDialog, WordSelectHdl_Impl ) );
262  m_xLangLB->connect_changed( LINK( this, SvxThesaurusDialog, LanguageHdl_Impl ) );
263  m_xAlternativesCT->connect_changed( LINK( this, SvxThesaurusDialog, AlternativesSelectHdl_Impl ));
264  m_xAlternativesCT->connect_row_activated( LINK( this, SvxThesaurusDialog, AlternativesDoubleClickHdl_Impl ));
265  m_xAlternativesCT->connect_key_press(LINK(this, SvxThesaurusDialog, KeyInputHdl));
266 
267  xThesaurus = xThes;
268  aLookUpText = rWord;
269  nLookUpLanguage = nLanguage;
270  if (!rWord.isEmpty())
271  aLookUpHistory.push( rWord );
272 
273  OUString aTmp( rWord );
276  m_xReplaceEdit->set_text( aTmp );
277  ReplaceEditHdl_Impl(*m_xReplaceEdit);
278  m_xWordCB->append_text( aTmp );
279 
280  LookUp( aTmp );
281  m_xAlternativesCT->grab_focus();
282  m_xLeftBtn->set_sensitive(false);
283 
284  // fill language menu button list
285  uno::Sequence< lang::Locale > aLocales;
286  if (xThesaurus.is())
287  aLocales = xThesaurus->getLocales();
288  const sal_Int32 nLocales = aLocales.getLength();
289  const lang::Locale *pLocales = aLocales.getConstArray();
290  m_xLangLB->clear();
291  std::vector< OUString > aLangVec;
292  for (sal_Int32 i = 0; i < nLocales; ++i)
293  {
294  const LanguageType nLang = LanguageTag::convertToLanguageType( pLocales[i] );
295  DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
296  aLangVec.push_back( SvtLanguageTable::GetLanguageString( nLang ) );
297  }
298  std::sort( aLangVec.begin(), aLangVec.end() );
299  m_xLangLB->freeze();
300  for (const OUString & i : aLangVec)
301  m_xLangLB->append_text(i);
302  m_xLangLB->thaw();
303 
304  std::vector< OUString >::iterator aI = std::find(aLangVec.begin(), aLangVec.end(),
306  if (aI != aLangVec.end())
307  {
308  m_xLangLB->set_active_text(*aI);
309  }
310 
311  SetWindowTitle(nLanguage);
312 
313  // disable controls if service is missing
314  if (!xThesaurus.is())
315  m_xDialog->set_sensitive(false);
316 }
317 
319 {
320 }
321 
322 IMPL_LINK_NOARG(SvxThesaurusDialog, ReplaceBtnHdl_Impl, weld::Button&, void)
323 {
324  m_xDialog->response(RET_OK);
325 }
326 
328 {
329  // adjust language
330  OUString aStr(m_xDialog->get_title());
331  sal_Int32 nIndex = aStr.indexOf( '(' );
332  if( nIndex != -1 )
333  aStr = aStr.copy( 0, nIndex - 1 );
334  aStr += " (" + SvtLanguageTable::GetLanguageString( nLanguage ) + ")";
335  m_xDialog->set_title(aStr); // set window title
336 }
337 
339 {
340  return m_xReplaceEdit->get_text();
341 }
342 
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OString stripEnd(const OString &rIn, char c)
#define LANGUAGE_NONE
OUString aLookUpText
Definition: thesdlg.hxx:35
sal_Int32 nIndex
LanguageType nLookUpLanguage
Definition: thesdlg.hxx:36
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
virtual ~SvxThesaurusDialog() override
Definition: thesdlg.cxx:318
bool RemoveHyphens(OUString &rTxt)
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
sal_uInt16 GetCode() const
std::unique_ptr< weld::Label > m_xNotFound
Definition: thesdlg.hxx:43
std::unique_ptr< weld::TreeView > m_xAlternativesCT
Definition: thesdlg.hxx:42
IMPL_LINK(SvxThesaurusDialog, KeyInputHdl, const KeyEvent &, rKEvt, bool)
Definition: thesdlg.cxx:49
OUString GetWord() const
Definition: thesdlg.cxx:338
css::uno::Reference< css::linguistic2::XThesaurus > xThesaurus
Definition: thesdlg.hxx:34
std::unique_ptr< weld::Button > m_xReplaceBtn
Definition: thesdlg.hxx:46
#define DBG_ASSERT(sCon, aError)
bool UpdateAlternativesBox_Impl()
Definition: thesdlg.cxx:86
int i
IMPL_LINK_NOARG(SvxThesaurusDialog, ModifyTimer_Hdl, Timer *, void)
Definition: thesdlg.cxx:38
SvxThesaurusDialog(weld::Window *pParent, css::uno::Reference< css::linguistic2::XThesaurus > const &xThesaurus, const OUString &rWord, LanguageType nLanguage)
Definition: thesdlg.cxx:236
#define LANGUAGE_DONTKNOW
void SetWindowTitle(LanguageType nLanguage)
Definition: thesdlg.cxx:327
constexpr sal_uInt16 KEY_RETURN
std::unique_ptr< weld::Button > m_xLeftBtn
Definition: thesdlg.hxx:40
std::stack< OUString > aLookUpHistory
Definition: thesdlg.hxx:37
bool ReplaceControlChars(OUString &rTxt)
css::uno::Sequence< css::uno::Reference< css::linguistic2::XMeaning > > queryMeanings_Impl(OUString &rTerm, const css::lang::Locale &rLocale, const css::beans::PropertyValues &rProperties)
Definition: thesdlg.cxx:62
std::unique_ptr< weld::ComboBox > m_xWordCB
Definition: thesdlg.hxx:41
RET_OK
Reference< XExecutableDialog > m_xDialog
void SetInvokeHandler(const Link< Timer *, void > &rLink)
static OUString GetLanguageString(const LanguageType eType)
std::unique_ptr< weld::ComboBox > m_xLangLB
Definition: thesdlg.hxx:45
void SetPriority(TaskPriority ePriority)
OUString GetThesaurusReplaceText(const OUString &rText)
void LookUp(const OUString &rText)
Definition: thesdlg.cxx:126
aStr
static LanguageType GetLanguageType(const OUString &rStr)
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
std::unique_ptr< weld::Entry > m_xReplaceEdit
Definition: thesdlg.hxx:44
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo