tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / cui / source / dialogs / thesdlg.cxx
blob07eef25be0dd47e9798a8216fc0644b433777d57
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <thesdlg.hxx>
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>
27 #include <i18nlangtag/languagetag.hxx>
28 #include <comphelper/string.hxx>
30 #include <stack>
31 #include <algorithm>
33 #include <com/sun/star/linguistic2/XThesaurus.hpp>
34 #include <com/sun/star/linguistic2/XMeaning.hpp>
36 using namespace ::com::sun::star;
38 IMPL_LINK_NOARG( SvxThesaurusDialog, ModifyTimer_Hdl, Timer *, void )
40 LookUp(m_xWordCB->get_active_text());
41 m_aModifyIdle.Stop();
44 IMPL_LINK_NOARG(SvxThesaurusDialog, ReplaceEditHdl_Impl, weld::Entry&, void)
46 m_xReplaceBtn->set_sensitive(!m_xReplaceEdit->get_text().isEmpty());
49 IMPL_LINK(SvxThesaurusDialog, KeyInputHdl, const KeyEvent&, rKEvt, bool)
51 const vcl::KeyCode& rKey = rKEvt.GetKeyCode();
53 if (rKey.GetCode() == KEY_RETURN)
55 m_xDialog->response(RET_OK);
56 return true;
59 return false;
62 uno::Sequence< uno::Reference< linguistic2::XMeaning > > SvxThesaurusDialog::queryMeanings_Impl(
63 OUString& rTerm,
64 const lang::Locale& rLocale,
65 const beans::PropertyValues& rProperties )
67 uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeanings(
68 xThesaurus->queryMeanings( rTerm, rLocale, rProperties ) );
70 // text with '.' at the end?
71 if ( !aMeanings.hasElements() && rTerm.endsWith(".") )
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())
79 rTerm = aTxt;
83 return aMeanings;
86 bool SvxThesaurusDialog::UpdateAlternativesBox_Impl()
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();
93 m_xAlternativesCT->freeze();
95 m_xAlternativesCT->clear();
96 int nRow = 0;
97 for (sal_Int32 i = 0; i < nMeanings; ++i)
99 OUString rMeaningTxt = aMeanings[i]->getMeaning();
100 uno::Sequence<OUString> aSynonyms(aMeanings[i]->querySynonyms());
101 DBG_ASSERT( !rMeaningTxt.isEmpty(), "meaning with empty text" );
102 DBG_ASSERT(aSynonyms.hasElements(), "meaning without synonym");
104 OUString sHeading = OUString::number(i + 1) + ". " + rMeaningTxt;
105 m_xAlternativesCT->append_text(sHeading);
106 m_xAlternativesCT->set_text_emphasis(nRow, true, 0);
107 ++nRow;
109 for (auto& synonym : aSynonyms)
111 // GetThesaurusReplaceText will strip the leading spaces
112 m_xAlternativesCT->append_text(" " + synonym);
113 m_xAlternativesCT->set_text_emphasis(nRow, false, 0);
114 ++nRow;
118 m_xAlternativesCT->thaw();
120 return nMeanings > 0;
123 void SvxThesaurusDialog::LookUp( const OUString &rText )
125 if (rText != m_xWordCB->get_active_text()) // avoid moving of the cursor if the text is the same
126 m_xWordCB->set_entry_text(rText);
127 LookUp_Impl();
130 IMPL_LINK_NOARG(SvxThesaurusDialog, LeftBtnHdl_Impl, weld::Button&, void)
132 if (aLookUpHistory.size() >= 2)
134 aLookUpHistory.pop(); // remove current look up word from stack
135 m_xWordCB->set_entry_text(aLookUpHistory.top()); // retrieve previous look up word
136 aLookUpHistory.pop();
137 LookUp_Impl();
141 IMPL_LINK( SvxThesaurusDialog, LanguageHdl_Impl, weld::ComboBox&, rLB, void )
143 OUString aLangText(rLB.get_active_text());
144 LanguageType nLang = SvtLanguageTable::GetLanguageType( aLangText );
145 DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
146 if (xThesaurus->hasLocale( LanguageTag::convertToLocale( nLang ) ))
147 nLookUpLanguage = nLang;
148 SetWindowTitle( nLang );
149 LookUp_Impl();
152 void SvxThesaurusDialog::LookUp_Impl()
154 OUString aText(m_xWordCB->get_active_text());
156 aLookUpText = aText;
157 if (!aLookUpText.isEmpty() &&
158 (aLookUpHistory.empty() || aLookUpText != aLookUpHistory.top()))
159 aLookUpHistory.push( aLookUpText );
161 m_bWordFound = UpdateAlternativesBox_Impl();
162 m_xAlternativesCT->set_visible(m_bWordFound);
163 m_xNotFound->set_visible(!m_bWordFound);
165 if (m_bWordFound && !m_nSelectFirstEvent)
166 m_nSelectFirstEvent = Application::PostUserEvent(LINK(this, SvxThesaurusDialog, SelectFirstHdl_Impl));
168 if (m_xWordCB->find_text(aText) == -1)
169 m_xWordCB->append_text(aText);
171 m_xReplaceEdit->set_text( OUString() );
172 ReplaceEditHdl_Impl(*m_xReplaceEdit);
173 m_xLeftBtn->set_sensitive( aLookUpHistory.size() > 1 );
176 IMPL_LINK_NOARG(SvxThesaurusDialog, WordSelectHdl_Impl, weld::ComboBox&, void)
178 m_aModifyIdle.Start();
181 IMPL_LINK( SvxThesaurusDialog, AlternativesSelectHdl_Impl, weld::TreeView&, rBox, void )
183 int nEntry = rBox.get_selected_index();
184 if (nEntry != -1)
186 bool bIsHeader = rBox.get_text_emphasis(nEntry, 0);
187 if (bIsHeader)
189 ++nEntry;
190 rBox.select(nEntry);
192 OUString aStr = linguistic::GetThesaurusReplaceText(rBox.get_text(nEntry));
193 m_xReplaceEdit->set_text(aStr);
194 ReplaceEditHdl_Impl(*m_xReplaceEdit);
198 IMPL_LINK( SvxThesaurusDialog, AlternativesDoubleClickHdl_Impl, weld::TreeView&, rBox, bool )
200 int nEntry = rBox.get_selected_index();
201 if (nEntry != -1)
203 bool bIsHeader = rBox.get_text_emphasis(nEntry, 0);
204 if (bIsHeader)
206 ++nEntry;
207 rBox.select(nEntry);
209 OUString aStr = linguistic::GetThesaurusReplaceText(rBox.get_text(nEntry));
210 m_xWordCB->set_entry_text(aStr);
211 if (!aStr.isEmpty())
212 LookUp_Impl();
215 //! workaround to set the selection since calling SelectEntryPos within
216 //! the double click handler does not work
217 if (!m_nSelectFirstEvent)
218 m_nSelectFirstEvent = Application::PostUserEvent(LINK(this, SvxThesaurusDialog, SelectFirstHdl_Impl));
220 return true;
223 IMPL_LINK_NOARG(SvxThesaurusDialog, SelectFirstHdl_Impl, void *, void)
225 m_nSelectFirstEvent = nullptr;
226 if (m_xAlternativesCT->n_children() >= 2)
228 m_xAlternativesCT->select(1); // pos 0 is a 'header' that is not selectable
229 AlternativesSelectHdl_Impl(*m_xAlternativesCT);
233 // class SvxThesaurusDialog ----------------------------------------------
235 SvxThesaurusDialog::SvxThesaurusDialog(
236 weld::Widget* pParent,
237 uno::Reference< linguistic2::XThesaurus > const & xThes,
238 const OUString &rWord,
239 LanguageType nLanguage)
240 : SfxDialogController(pParent, u"cui/ui/thesaurus.ui"_ustr, u"ThesaurusDialog"_ustr)
241 , m_aModifyIdle("cui SvxThesaurusDialog LookUp Modify")
242 , nLookUpLanguage(LANGUAGE_NONE)
243 , m_bWordFound(false)
244 , m_xLeftBtn(m_xBuilder->weld_button(u"left"_ustr))
245 , m_xWordCB(m_xBuilder->weld_combo_box(u"wordcb"_ustr))
246 , m_xAlternativesCT(m_xBuilder->weld_tree_view(u"alternatives"_ustr))
247 , m_xNotFound(m_xBuilder->weld_label(u"notfound"_ustr))
248 , m_xReplaceEdit(m_xBuilder->weld_entry(u"replaceed"_ustr))
249 , m_xLangLB(m_xBuilder->weld_combo_box(u"langcb"_ustr))
250 , m_xReplaceBtn(m_xBuilder->weld_button(u"ok"_ustr))
251 , m_nSelectFirstEvent(nullptr)
253 m_aModifyIdle.SetInvokeHandler( LINK( this, SvxThesaurusDialog, ModifyTimer_Hdl ) );
254 m_aModifyIdle.SetPriority( TaskPriority::LOWEST );
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_selection_changed(
263 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));
267 xThesaurus = xThes;
268 aLookUpText = rWord;
269 nLookUpLanguage = nLanguage;
270 if (!rWord.isEmpty())
271 aLookUpHistory.push( rWord );
273 OUString aTmp( rWord );
274 (void)linguistic::RemoveHyphens( aTmp );
275 (void)linguistic::ReplaceControlChars( aTmp );
276 m_xReplaceEdit->set_text( aTmp );
277 ReplaceEditHdl_Impl(*m_xReplaceEdit);
278 m_xWordCB->append_text( aTmp );
280 LookUp( aTmp );
281 m_xAlternativesCT->grab_focus();
282 m_xLeftBtn->set_sensitive(false);
284 // fill language menu button list
285 uno::Sequence< lang::Locale > aLocales;
286 if (xThesaurus.is())
287 aLocales = xThesaurus->getLocales();
288 m_xLangLB->clear();
289 std::vector< OUString > aLangVec;
290 for (auto& locale : aLocales)
292 const LanguageType nLang = LanguageTag::convertToLanguageType(locale);
293 DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
294 aLangVec.push_back( SvtLanguageTable::GetLanguageString( nLang ) );
296 std::sort( aLangVec.begin(), aLangVec.end() );
297 m_xLangLB->freeze();
298 for (const OUString & i : aLangVec)
299 m_xLangLB->append_text(i);
300 m_xLangLB->thaw();
302 std::vector< OUString >::iterator aI = std::find(aLangVec.begin(), aLangVec.end(),
303 SvtLanguageTable::GetLanguageString(nLanguage));
304 if (aI != aLangVec.end())
306 m_xLangLB->set_active_text(*aI);
309 SetWindowTitle(nLanguage);
311 // disable controls if service is missing
312 if (!xThesaurus.is())
313 m_xDialog->set_sensitive(false);
314 else
315 m_xWordCB->grab_focus();
318 SvxThesaurusDialog::~SvxThesaurusDialog()
320 if (m_nSelectFirstEvent)
322 Application::RemoveUserEvent(m_nSelectFirstEvent);
323 m_nSelectFirstEvent = nullptr;
327 IMPL_LINK_NOARG(SvxThesaurusDialog, ReplaceBtnHdl_Impl, weld::Button&, void)
329 m_xDialog->response(RET_OK);
332 void SvxThesaurusDialog::SetWindowTitle( LanguageType nLanguage )
334 // adjust language
335 OUString aStr(m_xDialog->get_title());
336 sal_Int32 nIndex = aStr.indexOf( '(' );
337 if( nIndex != -1 )
338 aStr = aStr.copy( 0, nIndex - 1 );
339 aStr += " (" + SvtLanguageTable::GetLanguageString( nLanguage ) + ")";
340 m_xDialog->set_title(aStr); // set window title
343 OUString SvxThesaurusDialog::GetWord() const
345 return m_xReplaceEdit->get_text();
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */