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
36using namespace ::com::sun::star;
37
38IMPL_LINK_NOARG( SvxThesaurusDialog, ModifyTimer_Hdl, Timer *, void )
39{
40 LookUp(m_xWordCB->get_active_text());
41 m_aModifyIdle.Stop();
42}
43
44IMPL_LINK_NOARG(SvxThesaurusDialog, ReplaceEditHdl_Impl, weld::Entry&, void)
45{
46 m_xReplaceBtn->set_sensitive(!m_xReplaceEdit->get_text().isEmpty());
47}
48
49IMPL_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
63 OUString& rTerm,
64 const lang::Locale& rLocale,
65 const beans::PropertyValues& rProperties )
66{
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 ) );
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
126void 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
144IMPL_LINK( SvxThesaurusDialog, LanguageHdl_Impl, weld::ComboBox&, rLB, void )
145{
146 OUString aLangText(rLB.get_active_text());
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()))
163
165 m_xAlternativesCT->set_visible(m_bWordFound);
166 m_xNotFound->set_visible(!m_bWordFound);
167
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
184IMPL_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
201IMPL_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 if (!m_nSelectFirstEvent)
221 m_nSelectFirstEvent = Application::PostUserEvent(LINK(this, SvxThesaurusDialog, SelectFirstHdl_Impl));
222
223 return true;
224}
225
226IMPL_LINK_NOARG(SvxThesaurusDialog, SelectFirstHdl_Impl, void *, void)
227{
228 m_nSelectFirstEvent = nullptr;
229 if (m_xAlternativesCT->n_children() >= 2)
230 {
231 m_xAlternativesCT->select(1); // pos 0 is a 'header' that is not selectable
232 AlternativesSelectHdl_Impl(*m_xAlternativesCT);
233 }
234}
235
236// class SvxThesaurusDialog ----------------------------------------------
237
239 weld::Widget* pParent,
241 const OUString &rWord,
242 LanguageType nLanguage)
243 : SfxDialogController(pParent, "cui/ui/thesaurus.ui", "ThesaurusDialog")
244 , m_aModifyIdle("cui SvxThesaurusDialog LookUp Modify")
245 , nLookUpLanguage(LANGUAGE_NONE)
246 , m_bWordFound(false)
247 , m_xLeftBtn(m_xBuilder->weld_button("left"))
248 , m_xWordCB(m_xBuilder->weld_combo_box("wordcb"))
249 , m_xAlternativesCT(m_xBuilder->weld_tree_view("alternatives"))
250 , m_xNotFound(m_xBuilder->weld_label("notfound"))
251 , m_xReplaceEdit(m_xBuilder->weld_entry("replaceed"))
252 , m_xLangLB(m_xBuilder->weld_combo_box("langcb"))
253 , m_xReplaceBtn(m_xBuilder->weld_button("ok"))
254 , m_nSelectFirstEvent(nullptr)
255{
256 m_aModifyIdle.SetInvokeHandler( LINK( this, SvxThesaurusDialog, ModifyTimer_Hdl ) );
257 m_aModifyIdle.SetPriority( TaskPriority::LOWEST );
258
259 m_xReplaceEdit->connect_changed( LINK( this, SvxThesaurusDialog, ReplaceEditHdl_Impl ) );
260 m_xReplaceBtn->connect_clicked( LINK( this, SvxThesaurusDialog, ReplaceBtnHdl_Impl ) );
261 m_xLeftBtn->connect_clicked( LINK( this, SvxThesaurusDialog, LeftBtnHdl_Impl ) );
262 m_xWordCB->set_entry_completion(false);
263 m_xWordCB->connect_changed( LINK( this, SvxThesaurusDialog, WordSelectHdl_Impl ) );
264 m_xLangLB->connect_changed( LINK( this, SvxThesaurusDialog, LanguageHdl_Impl ) );
265 m_xAlternativesCT->connect_changed( LINK( this, SvxThesaurusDialog, AlternativesSelectHdl_Impl ));
266 m_xAlternativesCT->connect_row_activated( LINK( this, SvxThesaurusDialog, AlternativesDoubleClickHdl_Impl ));
267 m_xAlternativesCT->connect_key_press(LINK(this, SvxThesaurusDialog, KeyInputHdl));
268
269 xThesaurus = xThes;
270 aLookUpText = rWord;
271 nLookUpLanguage = nLanguage;
272 if (!rWord.isEmpty())
273 aLookUpHistory.push( rWord );
274
275 OUString aTmp( rWord );
276 (void)linguistic::RemoveHyphens( aTmp );
278 m_xReplaceEdit->set_text( aTmp );
279 ReplaceEditHdl_Impl(*m_xReplaceEdit);
280 m_xWordCB->append_text( aTmp );
281
282 LookUp( aTmp );
283 m_xAlternativesCT->grab_focus();
284 m_xLeftBtn->set_sensitive(false);
285
286 // fill language menu button list
288 if (xThesaurus.is())
289 aLocales = xThesaurus->getLocales();
290 const sal_Int32 nLocales = aLocales.getLength();
291 const lang::Locale *pLocales = aLocales.getConstArray();
292 m_xLangLB->clear();
293 std::vector< OUString > aLangVec;
294 for (sal_Int32 i = 0; i < nLocales; ++i)
295 {
296 const LanguageType nLang = LanguageTag::convertToLanguageType( pLocales[i] );
297 DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
298 aLangVec.push_back( SvtLanguageTable::GetLanguageString( nLang ) );
299 }
300 std::sort( aLangVec.begin(), aLangVec.end() );
301 m_xLangLB->freeze();
302 for (const OUString & i : aLangVec)
303 m_xLangLB->append_text(i);
304 m_xLangLB->thaw();
305
306 std::vector< OUString >::iterator aI = std::find(aLangVec.begin(), aLangVec.end(),
308 if (aI != aLangVec.end())
309 {
310 m_xLangLB->set_active_text(*aI);
311 }
312
313 SetWindowTitle(nLanguage);
314
315 // disable controls if service is missing
316 if (!xThesaurus.is())
317 m_xDialog->set_sensitive(false);
318 else
319 m_xWordCB->grab_focus();
320}
321
323{
325 {
327 m_nSelectFirstEvent = nullptr;
328 }
329}
330
332{
333 m_xDialog->response(RET_OK);
334}
335
337{
338 // adjust language
339 OUString aStr(m_xDialog->get_title());
340 sal_Int32 nIndex = aStr.indexOf( '(' );
341 if( nIndex != -1 )
342 aStr = aStr.copy( 0, nIndex - 1 );
343 aStr += " (" + SvtLanguageTable::GetLanguageString( nLanguage ) + ")";
344 m_xDialog->set_title(aStr); // set window title
345}
346
348{
349 return m_xReplaceEdit->get_text();
350}
351
352/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
static OUString GetLanguageString(const LanguageType eType)
static LanguageType GetLanguageType(std::u16string_view rStr)
std::unique_ptr< weld::Button > m_xReplaceBtn
Definition: thesdlg.hxx:47
void LookUp(const OUString &rText)
Definition: thesdlg.cxx:126
std::unique_ptr< weld::TreeView > m_xAlternativesCT
Definition: thesdlg.hxx:43
std::unique_ptr< weld::ComboBox > m_xLangLB
Definition: thesdlg.hxx:46
std::unique_ptr< weld::Entry > m_xReplaceEdit
Definition: thesdlg.hxx:45
LanguageType nLookUpLanguage
Definition: thesdlg.hxx:37
css::uno::Reference< css::linguistic2::XThesaurus > xThesaurus
Definition: thesdlg.hxx:35
virtual ~SvxThesaurusDialog() override
Definition: thesdlg.cxx:322
void SetWindowTitle(LanguageType nLanguage)
Definition: thesdlg.cxx:336
std::stack< OUString > aLookUpHistory
Definition: thesdlg.hxx:38
OUString GetWord() const
Definition: thesdlg.cxx:347
SvxThesaurusDialog(weld::Widget *pParent, css::uno::Reference< css::linguistic2::XThesaurus > const &xThesaurus, const OUString &rWord, LanguageType nLanguage)
Definition: thesdlg.cxx:238
ImplSVEvent * m_nSelectFirstEvent
Definition: thesdlg.hxx:48
OUString aLookUpText
Definition: thesdlg.hxx:36
std::unique_ptr< weld::ComboBox > m_xWordCB
Definition: thesdlg.hxx:42
bool UpdateAlternativesBox_Impl()
Definition: thesdlg.cxx:86
std::unique_ptr< weld::Button > m_xLeftBtn
Definition: thesdlg.hxx:41
std::unique_ptr< weld::Label > m_xNotFound
Definition: thesdlg.hxx:44
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
void SetPriority(TaskPriority ePriority)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
sal_uInt16 GetCode() const
#define DBG_ASSERT(sCon, aError)
sal_Int32 nIndex
constexpr sal_uInt16 KEY_RETURN
#define LANGUAGE_NONE
#define LANGUAGE_DONTKNOW
aStr
OString stripEnd(const OString &rIn, char c)
int i
bool RemoveHyphens(OUString &rTxt)
OUString GetThesaurusReplaceText(const OUString &rText)
bool ReplaceControlChars(OUString &rTxt)
IMPL_LINK_NOARG(SvxThesaurusDialog, ModifyTimer_Hdl, Timer *, void)
Definition: thesdlg.cxx:38
IMPL_LINK(SvxThesaurusDialog, KeyInputHdl, const KeyEvent &, rKEvt, bool)
Definition: thesdlg.cxx:49
RET_OK