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
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
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 Application::PostUserEvent(LINK(this, SvxThesaurusDialog, SelectFirstHdl_Impl));
221
222 return true;
223}
224
225IMPL_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::Widget* pParent,
239 const OUString &rWord,
240 LanguageType nLanguage)
241 : SfxDialogController(pParent, "cui/ui/thesaurus.ui", "ThesaurusDialog")
242 , m_aModifyIdle("cui SvxThesaurusDialog LookUp Modify")
243 , nLookUpLanguage(LANGUAGE_NONE)
244 , m_bWordFound(false)
245 , m_xLeftBtn(m_xBuilder->weld_button("left"))
246 , m_xWordCB(m_xBuilder->weld_combo_box("wordcb"))
247 , m_xAlternativesCT(m_xBuilder->weld_tree_view("alternatives"))
248 , m_xNotFound(m_xBuilder->weld_label("notfound"))
249 , m_xReplaceEdit(m_xBuilder->weld_entry("replaceed"))
250 , m_xLangLB(m_xBuilder->weld_combo_box("langcb"))
251 , m_xReplaceBtn(m_xBuilder->weld_button("ok"))
252{
253 m_aModifyIdle.SetInvokeHandler( LINK( this, SvxThesaurusDialog, ModifyTimer_Hdl ) );
254 m_aModifyIdle.SetPriority( TaskPriority::LOWEST );
255
256 m_xReplaceEdit->connect_changed( LINK( this, SvxThesaurusDialog, ReplaceEditHdl_Impl ) );
257 m_xReplaceBtn->connect_clicked( LINK( this, SvxThesaurusDialog, ReplaceBtnHdl_Impl ) );
258 m_xLeftBtn->connect_clicked( LINK( this, SvxThesaurusDialog, LeftBtnHdl_Impl ) );
259 m_xWordCB->set_entry_completion(false);
260 m_xWordCB->connect_changed( LINK( this, SvxThesaurusDialog, WordSelectHdl_Impl ) );
261 m_xLangLB->connect_changed( LINK( this, SvxThesaurusDialog, LanguageHdl_Impl ) );
262 m_xAlternativesCT->connect_changed( LINK( this, SvxThesaurusDialog, AlternativesSelectHdl_Impl ));
263 m_xAlternativesCT->connect_row_activated( LINK( this, SvxThesaurusDialog, AlternativesDoubleClickHdl_Impl ));
264 m_xAlternativesCT->connect_key_press(LINK(this, SvxThesaurusDialog, KeyInputHdl));
265
266 xThesaurus = xThes;
267 aLookUpText = rWord;
268 nLookUpLanguage = nLanguage;
269 if (!rWord.isEmpty())
270 aLookUpHistory.push( rWord );
271
272 OUString aTmp( rWord );
273 (void)linguistic::RemoveHyphens( aTmp );
275 m_xReplaceEdit->set_text( aTmp );
276 ReplaceEditHdl_Impl(*m_xReplaceEdit);
277 m_xWordCB->append_text( aTmp );
278
279 LookUp( aTmp );
280 m_xAlternativesCT->grab_focus();
281 m_xLeftBtn->set_sensitive(false);
282
283 // fill language menu button list
285 if (xThesaurus.is())
286 aLocales = xThesaurus->getLocales();
287 const sal_Int32 nLocales = aLocales.getLength();
288 const lang::Locale *pLocales = aLocales.getConstArray();
289 m_xLangLB->clear();
290 std::vector< OUString > aLangVec;
291 for (sal_Int32 i = 0; i < nLocales; ++i)
292 {
293 const LanguageType nLang = LanguageTag::convertToLanguageType( pLocales[i] );
294 DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
295 aLangVec.push_back( SvtLanguageTable::GetLanguageString( nLang ) );
296 }
297 std::sort( aLangVec.begin(), aLangVec.end() );
298 m_xLangLB->freeze();
299 for (const OUString & i : aLangVec)
300 m_xLangLB->append_text(i);
301 m_xLangLB->thaw();
302
303 std::vector< OUString >::iterator aI = std::find(aLangVec.begin(), aLangVec.end(),
305 if (aI != aLangVec.end())
306 {
307 m_xLangLB->set_active_text(*aI);
308 }
309
310 SetWindowTitle(nLanguage);
311
312 // disable controls if service is missing
313 if (!xThesaurus.is())
314 m_xDialog->set_sensitive(false);
315}
316
318{
319}
320
322{
323 m_xDialog->response(RET_OK);
324}
325
327{
328 // adjust language
329 OUString aStr(m_xDialog->get_title());
330 sal_Int32 nIndex = aStr.indexOf( '(' );
331 if( nIndex != -1 )
332 aStr = aStr.copy( 0, nIndex - 1 );
333 aStr += " (" + SvtLanguageTable::GetLanguageString( nLanguage ) + ")";
334 m_xDialog->set_title(aStr); // set window title
335}
336
338{
339 return m_xReplaceEdit->get_text();
340}
341
342/* 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 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:45
void LookUp(const OUString &rText)
Definition: thesdlg.cxx:126
std::unique_ptr< weld::TreeView > m_xAlternativesCT
Definition: thesdlg.hxx:41
std::unique_ptr< weld::ComboBox > m_xLangLB
Definition: thesdlg.hxx:44
std::unique_ptr< weld::Entry > m_xReplaceEdit
Definition: thesdlg.hxx:43
LanguageType nLookUpLanguage
Definition: thesdlg.hxx:35
css::uno::Reference< css::linguistic2::XThesaurus > xThesaurus
Definition: thesdlg.hxx:33
virtual ~SvxThesaurusDialog() override
Definition: thesdlg.cxx:317
void SetWindowTitle(LanguageType nLanguage)
Definition: thesdlg.cxx:326
std::stack< OUString > aLookUpHistory
Definition: thesdlg.hxx:36
OUString GetWord() const
Definition: thesdlg.cxx:337
SvxThesaurusDialog(weld::Widget *pParent, css::uno::Reference< css::linguistic2::XThesaurus > const &xThesaurus, const OUString &rWord, LanguageType nLanguage)
Definition: thesdlg.cxx:236
OUString aLookUpText
Definition: thesdlg.hxx:34
std::unique_ptr< weld::ComboBox > m_xWordCB
Definition: thesdlg.hxx:40
bool UpdateAlternativesBox_Impl()
Definition: thesdlg.cxx:86
std::unique_ptr< weld::Button > m_xLeftBtn
Definition: thesdlg.hxx:39
std::unique_ptr< weld::Label > m_xNotFound
Definition: thesdlg.hxx:42
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