]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiThesaurus.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiThesaurus.cpp
index 083ed2eac0d7d07102a1475d2bf1bd3ea7a41add..c3e7c95829532b0f5994f89ef652434cac7aa624 100644 (file)
 #include "FuncRequest.h"
 #include "Language.h"
 #include "lyxfind.h"
+#include "WordLangTuple.h"
 
 #include "support/debug.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
 #include <QAbstractItemModel>
+#include <QCompleter>
+#include <QDialogButtonBox>
 #include <QHeaderView>
 #include <QLineEdit>
 #include <QPushButton>
@@ -49,8 +52,8 @@ GuiThesaurus::GuiThesaurus(GuiView & lv)
        meaningsTV->setColumnCount(1);
        meaningsTV->header()->hide();
 
-       connect(closePB, SIGNAL(clicked()),
-               this, SLOT(slotClose()));
+       connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
+               this, SLOT(slotButtonBox(QAbstractButton *)));
        connect(replaceED, SIGNAL(returnPressed()),
                this, SLOT(replaceClicked()));
        connect(replaceED, SIGNAL(textChanged(QString)),
@@ -77,14 +80,28 @@ GuiThesaurus::GuiThesaurus(GuiView & lv)
        // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
        language_model->sort(0);
        languageCO->setModel(language_model);
+       languageCO->setModelColumn(2);
 
-       bc().setCancel(closePB);
-       bc().setApply(replacePB);
+       //bug #8138
+       if (entryCO->completer())
+               entryCO->completer()->setCompletionMode(QCompleter::PopupCompletion);
+
+       bc().setCancel(buttonBox->button(QDialogButtonBox::Close));
+       bc().setApply(replacePB, true);
        bc().addReadOnly(replaceED);
        bc().addReadOnly(replacePB);
        bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
 }
 
+void GuiThesaurus::checkStatus()
+{
+       if (!isBufferAvailable()) {
+               // deactivate the thesaurus if we have no buffer
+               enableView(false);
+               return;
+       }
+       updateView();
+}
 
 void GuiThesaurus::change_adaptor()
 {
@@ -105,17 +122,20 @@ void GuiThesaurus::selectionChanged()
                return;
 
        QString item = meaningsTV->currentItem()->text(col);
-       // cut out the classification in brackets
-       // (as in "hominid (generic term)")
-       // FIXME: not ideal yet. We need to cut off classifications
-       // at the beginning as well 
-       // (as in "(noun) man" and "(noun) male (generic term)")
+       // cut out the classification in brackets:
+       // "hominid (generic term)" -> "hominid"
        QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
+       // This is for items with classifications at the beginning:
+       // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
+       QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
        int pos = re.indexIn(item);
        if (pos > -1)
                item = re.cap(1).trimmed();
+       pos = rex.indexIn(item);
+       if (pos > -1)
+               item = rex.cap(2).trimmed();
        replaceED->setText(item);
-       replacePB->setEnabled(true);
+       replacePB->setEnabled(!isBufferReadonly());
        changed();
 }
 
@@ -129,15 +149,18 @@ void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
 {
        QString str = item->text(col);
-       // cut out the classification in brackets
-       // (as in "hominid (generic term)")
-       // FIXME: not ideal yet. We need to cut off classifications
-       // at the beginning as well 
-       // (as in "(noun) man" and "(noun) male (generic term)")
+       // cut out the classification in brackets:
+       // "hominid (generic term)" -> "hominid"
        QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
+       // This is for items with classifications at the beginning:
+       // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
+       QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
        int pos = re.indexIn(str);
        if (pos > -1)
                str = re.cap(1).trimmed();
+       pos = rex.indexIn(str);
+       if (pos > -1)
+               str = rex.cap(2).trimmed();
        entryCO->insertItem(0, str);
        entryCO->setCurrentIndex(0);
 
@@ -149,15 +172,19 @@ void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
 void GuiThesaurus::updateLists()
 {
        meaningsTV->clear();
+
+       if (entryCO->currentText().isEmpty())
+               return;
+
        meaningsTV->setUpdatesEnabled(false);
 
        QString const lang = languageCO->itemData(
                languageCO->currentIndex()).toString();
-       docstring const lang_code =
-               from_ascii(lyx::languages.getLanguage(fromqstr(lang))->code());
+       Language * language = const_cast<Language*>(lyx::languages.getLanguage(fromqstr(lang)));
+       docstring const lang_code = from_ascii(language->code());
 
        Thesaurus::Meanings meanings =
-               getMeanings(qstring_to_ucs4(entryCO->currentText()), lang_code);
+               getMeanings(WordLangTuple(qstring_to_ucs4(entryCO->currentText()), language));
 
        for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
                cit != meanings.end(); ++cit) {
@@ -171,8 +198,9 @@ void GuiThesaurus::updateLists()
                        }
                meaningsTV->setEnabled(true);
                lookupPB->setEnabled(true);
-               replaceED->setEnabled(true);
-               replacePB->setEnabled(true);
+               bool const readonly = isBufferReadonly();
+               replaceED->setEnabled(!readonly);
+               replacePB->setEnabled(!readonly);
        }
 
        if (meanings.empty()) {
@@ -210,15 +238,15 @@ void GuiThesaurus::replaceClicked()
 }
 
 
-bool GuiThesaurus::initialiseParams(string const & data)
+bool GuiThesaurus::initialiseParams(string const & sdata)
 {
        string arg;
-       string const lang = rsplit(data, arg, ' ');
+       string const lang = rsplit(sdata, arg, ' ');
        if (prefixIs(lang, "lang=")) {
                lang_ = from_utf8(split(lang, '='));
                text_ = from_utf8(arg);
        } else {
-               text_ = from_utf8(data);
+               text_ = from_utf8(sdata);
                if (bufferview())
                        lang_ = from_ascii(
                                bufferview()->buffer().params().language->lang());
@@ -240,21 +268,20 @@ void GuiThesaurus::replace(docstring const & newstr)
         * on a particular charpos in a paragraph that is broken on
         * deletion/change !
         */
-       docstring const data =
-               replace2string(text_, newstr,
+       docstring const sdata =
+               replace2string(newstr, text_,
                                     true,  // case sensitive
                                     true,  // match word
                                     false, // all words
                                     true); // forward
-       dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
+       dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
 }
 
 
-Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str,
-       docstring const & lang)
+Thesaurus::Meanings const & GuiThesaurus::getMeanings(WordLangTuple const & wl)
 {
-       if (str != laststr_)
-               meanings_ = thesaurus.lookup(str, lang);
+       if (wl.word() != laststr_)
+               meanings_ = thesaurus.lookup(wl);
        return meanings_;
 }