X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiThesaurus.cpp;h=6d6269e6f5e34093c865b47d6f87086e9a81cb11;hb=425d092204118ea6c24c28e85fdf03fcf2bb51a4;hp=3cc0de64cc445052b1b485bc43403cdabac793dd;hpb=6f3cdf8da80a6b3889f2a3b8b53f44590152d600;p=lyx.git diff --git a/src/frontends/qt4/GuiThesaurus.cpp b/src/frontends/qt4/GuiThesaurus.cpp index 3cc0de64cc..6d6269e6f5 100644 --- a/src/frontends/qt4/GuiThesaurus.cpp +++ b/src/frontends/qt4/GuiThesaurus.cpp @@ -4,6 +4,7 @@ * Licence details can be found in the file COPYING. * * \author John Levon + * \author Jürgen Spitzmüller * * Full author contact details are available in file CREDITS. */ @@ -11,31 +12,39 @@ #include #include "GuiThesaurus.h" +#include "GuiApplication.h" #include "qt_helpers.h" -#include "debug.h" -#include +#include "Buffer.h" +#include "BufferParams.h" +#include "BufferView.h" +#include "FuncRequest.h" +#include "Language.h" +#include "lyxfind.h" + +#include "support/debug.h" +#include "support/gettext.h" +#include "support/lstrings.h" + +#include #include #include #include #include #include -#include "lyxfind.h" -#include "FuncRequest.h" - -using std::string; +using namespace lyx::support; +using namespace std; namespace lyx { namespace frontend { -GuiThesaurus::GuiThesaurus(LyXView & lv) - : GuiDialog(lv, "thesaurus") +GuiThesaurus::GuiThesaurus(GuiView & lv) + : GuiDialog(lv, "thesaurus", qt_("Thesaurus")) { setupUi(this); - setViewTitle(_("Thesaurus")); meaningsTV->setColumnCount(1); meaningsTV->header()->hide(); @@ -46,17 +55,29 @@ GuiThesaurus::GuiThesaurus(LyXView & lv) this, SLOT(replaceClicked())); connect(replaceED, SIGNAL(textChanged(QString)), this, SLOT(change_adaptor())); - connect(entryED, SIGNAL(returnPressed()), + connect(entryCO, SIGNAL(editTextChanged(const QString &)), + this, SLOT(entryChanged())); + connect(entryCO, SIGNAL(activated(int)), + this, SLOT(entryChanged())); + connect(lookupPB, SIGNAL(clicked()), this, SLOT(entryChanged())); connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked())); + connect(languageCO, SIGNAL(activated(int)), + this, SLOT(entryChanged())); connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(itemClicked(QTreeWidgetItem *, int))); connect(meaningsTV, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged())); - connect(meaningsTV, SIGNAL(itemActivated(QTreeWidgetItem *, int)), + connect(meaningsTV, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(selectionClicked(QTreeWidgetItem *, int))); + // language + QAbstractItemModel * language_model = guiApp->languageModel(); + // FIXME: it would be nice if sorting was enabled/disabled via a checkbox. + language_model->sort(0); + languageCO->setModel(language_model); + bc().setCancel(closePB); bc().setApply(replacePB); bc().addReadOnly(replaceED); @@ -71,13 +92,6 @@ void GuiThesaurus::change_adaptor() } -void GuiThesaurus::closeEvent(QCloseEvent * e) -{ - slotClose(); - GuiDialog::closeEvent(e); -} - - void GuiThesaurus::entryChanged() { updateLists(); @@ -90,7 +104,20 @@ void GuiThesaurus::selectionChanged() if (col < 0 || isBufferReadonly()) return; - replaceED->setText(meaningsTV->currentItem()->text(col)); + QString item = meaningsTV->currentItem()->text(col); + // 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); changed(); } @@ -104,7 +131,22 @@ void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/) void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col) { - entryED->setText(item->text(col)); + QString str = item->text(col); + // 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); + selectionChanged(); updateLists(); } @@ -115,18 +157,39 @@ void GuiThesaurus::updateLists() meaningsTV->clear(); meaningsTV->setUpdatesEnabled(false); - Thesaurus::Meanings meanings = getMeanings(qstring_to_ucs4(entryED->text())); + QString const lang = languageCO->itemData( + languageCO->currentIndex()).toString(); + docstring const lang_code = + from_ascii(lyx::languages.getLanguage(fromqstr(lang))->code()); + + Thesaurus::Meanings meanings = + getMeanings(qstring_to_ucs4(entryCO->currentText()), lang_code); for (Thesaurus::Meanings::const_iterator cit = meanings.begin(); cit != meanings.end(); ++cit) { QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV); i->setText(0, toqstr(cit->first)); meaningsTV->expandItem(i); - for (std::vector::const_iterator cit2 = cit->second.begin(); + for (vector::const_iterator cit2 = cit->second.begin(); cit2 != cit->second.end(); ++cit2) { QTreeWidgetItem * i2 = new QTreeWidgetItem(i); i2->setText(0, toqstr(*cit2)); } + meaningsTV->setEnabled(true); + lookupPB->setEnabled(true); + replaceED->setEnabled(true); + replacePB->setEnabled(true); + } + + if (meanings.empty()) { + if (!thesaurus.thesaurusAvailable(lang_code)) { + QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV); + i->setText(0, qt_("No thesaurus available for this language!")); + meaningsTV->setEnabled(false); + lookupPB->setEnabled(false); + replaceED->setEnabled(false); + replacePB->setEnabled(false); + } } meaningsTV->setUpdatesEnabled(true); @@ -136,8 +199,13 @@ void GuiThesaurus::updateLists() void GuiThesaurus::updateContents() { - entryED->setText(toqstr(text_)); + entryCO->clear(); + entryCO->addItem(toqstr(text_)); + entryCO->setCurrentIndex(0); replaceED->setText(""); + int const pos = languageCO->findData(toqstr(lang_)); + if (pos != -1) + languageCO->setCurrentIndex(pos); updateLists(); } @@ -150,7 +218,17 @@ void GuiThesaurus::replaceClicked() bool GuiThesaurus::initialiseParams(string const & data) { - text_ = from_utf8(data); + string arg; + string const lang = rsplit(data, arg, ' '); + if (prefixIs(lang, "lang=")) { + lang_ = from_utf8(split(lang, '=')); + text_ = from_utf8(arg); + } else { + text_ = from_utf8(data); + if (bufferview()) + lang_ = from_ascii( + bufferview()->buffer().params().language->lang()); + } return true; } @@ -158,6 +236,7 @@ bool GuiThesaurus::initialiseParams(string const & data) void GuiThesaurus::clearParams() { text_.erase(); + lang_.erase(); } @@ -168,7 +247,7 @@ void GuiThesaurus::replace(docstring const & newstr) * deletion/change ! */ docstring const data = - replace2string(text_, newstr, + replace2string(newstr, text_, true, // case sensitive true, // match word false, // all words @@ -177,19 +256,20 @@ void GuiThesaurus::replace(docstring const & newstr) } -Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str) +Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str, + docstring const & lang) { if (str != laststr_) - meanings_ = thesaurus.lookup(str); + meanings_ = thesaurus.lookup(str, lang); return meanings_; } -Dialog * createGuiThesaurus(LyXView & lv) { return new GuiThesaurus(lv); } +Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); } } // namespace frontend } // namespace lyx -#include "GuiThesaurus_moc.cpp" +#include "moc_GuiThesaurus.cpp"