2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-08-31 05:53:55 +00:00
|
|
|
* \file GuiThesaurus.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
2008-11-16 18:48:25 +00:00
|
|
|
* \author Jürgen Spitzmüller
|
2006-03-05 17:24:44 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiThesaurus.h"
|
2008-11-16 18:48:25 +00:00
|
|
|
#include "GuiApplication.h"
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "qt_helpers.h"
|
2008-02-05 12:43:19 +00:00
|
|
|
|
2008-11-16 18:48:25 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "BufferView.h"
|
2008-02-05 12:43:19 +00:00
|
|
|
#include "FuncRequest.h"
|
2008-11-16 18:48:25 +00:00
|
|
|
#include "Language.h"
|
2008-02-05 12:43:19 +00:00
|
|
|
#include "lyxfind.h"
|
2012-03-02 10:20:09 +00:00
|
|
|
#include "WordLangTuple.h"
|
2008-02-05 12:43:19 +00:00
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2008-02-05 12:43:19 +00:00
|
|
|
#include "support/gettext.h"
|
2008-11-16 18:48:25 +00:00
|
|
|
#include "support/lstrings.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-11-16 18:48:25 +00:00
|
|
|
#include <QAbstractItemModel>
|
2007-04-24 14:35:15 +00:00
|
|
|
#include <QHeaderView>
|
2006-05-03 19:51:15 +00:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPushButton>
|
2007-04-24 14:35:15 +00:00
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QTreeWidgetItem>
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
|
2008-11-16 18:48:25 +00:00
|
|
|
using namespace lyx::support;
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2007-11-23 09:44:02 +00:00
|
|
|
GuiThesaurus::GuiThesaurus(GuiView & lv)
|
2008-02-05 12:43:19 +00:00
|
|
|
: GuiDialog(lv, "thesaurus", qt_("Thesaurus"))
|
2007-04-24 14:35:15 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
|
|
|
|
meaningsTV->setColumnCount(1);
|
|
|
|
meaningsTV->header()->hide();
|
|
|
|
|
|
|
|
connect(closePB, SIGNAL(clicked()),
|
2007-09-05 20:33:29 +00:00
|
|
|
this, SLOT(slotClose()));
|
2007-04-24 14:35:15 +00:00
|
|
|
connect(replaceED, SIGNAL(returnPressed()),
|
|
|
|
this, SLOT(replaceClicked()));
|
2007-10-06 19:59:13 +00:00
|
|
|
connect(replaceED, SIGNAL(textChanged(QString)),
|
|
|
|
this, SLOT(change_adaptor()));
|
2008-11-16 18:48:25 +00:00
|
|
|
connect(entryCO, SIGNAL(editTextChanged(const QString &)),
|
|
|
|
this, SLOT(entryChanged()));
|
|
|
|
connect(entryCO, SIGNAL(activated(int)),
|
|
|
|
this, SLOT(entryChanged()));
|
|
|
|
connect(lookupPB, SIGNAL(clicked()),
|
2007-04-24 14:35:15 +00:00
|
|
|
this, SLOT(entryChanged()));
|
|
|
|
connect(replacePB, SIGNAL(clicked()),
|
|
|
|
this, SLOT(replaceClicked()));
|
2008-11-16 18:48:25 +00:00
|
|
|
connect(languageCO, SIGNAL(activated(int)),
|
|
|
|
this, SLOT(entryChanged()));
|
2007-04-24 14:35:15 +00:00
|
|
|
connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
|
|
|
|
this, SLOT(itemClicked(QTreeWidgetItem *, int)));
|
|
|
|
connect(meaningsTV, SIGNAL(itemSelectionChanged()),
|
|
|
|
this, SLOT(selectionChanged()));
|
2008-11-16 18:48:25 +00:00
|
|
|
connect(meaningsTV, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
|
2007-04-24 14:35:15 +00:00
|
|
|
this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2008-11-16 18:48:25 +00:00
|
|
|
// 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);
|
2010-02-10 08:10:31 +00:00
|
|
|
languageCO->setModelColumn(2);
|
2008-11-16 18:48:25 +00:00
|
|
|
|
2007-09-05 20:33:29 +00:00
|
|
|
bc().setCancel(closePB);
|
|
|
|
bc().setApply(replacePB);
|
|
|
|
bc().addReadOnly(replaceED);
|
|
|
|
bc().addReadOnly(replacePB);
|
|
|
|
bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
|
2007-04-24 14:35:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-15 12:09:23 +00:00
|
|
|
void GuiThesaurus::checkStatus()
|
|
|
|
{
|
|
|
|
if (!isBufferAvailable()) {
|
|
|
|
// deactivate the thesaurus if we have no buffer
|
|
|
|
enableView(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::change_adaptor()
|
2007-04-24 14:35:15 +00:00
|
|
|
{
|
2007-09-05 20:33:29 +00:00
|
|
|
changed();
|
2007-04-24 14:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::entryChanged()
|
2007-04-24 14:35:15 +00:00
|
|
|
{
|
|
|
|
updateLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::selectionChanged()
|
2007-04-24 14:35:15 +00:00
|
|
|
{
|
|
|
|
int const col = meaningsTV->currentColumn();
|
2007-10-06 19:59:13 +00:00
|
|
|
if (col < 0 || isBufferReadonly())
|
2007-04-24 14:35:15 +00:00
|
|
|
return;
|
|
|
|
|
2008-11-16 18:48:25 +00:00
|
|
|
QString item = meaningsTV->currentItem()->text(col);
|
2008-11-17 07:15:44 +00:00
|
|
|
// cut out the classification in brackets:
|
|
|
|
// "hominid (generic term)" -> "hominid"
|
2008-11-16 18:48:25 +00:00
|
|
|
QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
|
2008-11-17 07:15:44 +00:00
|
|
|
// This is for items with classifications at the beginning:
|
|
|
|
// "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
|
|
|
|
QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
|
2008-11-16 18:48:25 +00:00
|
|
|
int pos = re.indexIn(item);
|
|
|
|
if (pos > -1)
|
|
|
|
item = re.cap(1).trimmed();
|
2008-11-17 07:15:44 +00:00
|
|
|
pos = rex.indexIn(item);
|
|
|
|
if (pos > -1)
|
|
|
|
item = rex.cap(2).trimmed();
|
2008-11-16 18:48:25 +00:00
|
|
|
replaceED->setText(item);
|
2011-01-15 12:09:23 +00:00
|
|
|
replacePB->setEnabled(!isBufferReadonly());
|
2007-09-05 20:33:29 +00:00
|
|
|
changed();
|
2007-04-24 14:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
|
2007-04-24 14:35:15 +00:00
|
|
|
{
|
|
|
|
selectionChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
|
2007-04-24 14:35:15 +00:00
|
|
|
{
|
2008-11-16 18:48:25 +00:00
|
|
|
QString str = item->text(col);
|
2008-11-17 07:15:44 +00:00
|
|
|
// cut out the classification in brackets:
|
|
|
|
// "hominid (generic term)" -> "hominid"
|
2008-11-16 18:48:25 +00:00
|
|
|
QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
|
2008-11-17 07:15:44 +00:00
|
|
|
// This is for items with classifications at the beginning:
|
|
|
|
// "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
|
|
|
|
QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
|
2008-11-16 18:48:25 +00:00
|
|
|
int pos = re.indexIn(str);
|
|
|
|
if (pos > -1)
|
|
|
|
str = re.cap(1).trimmed();
|
2008-11-17 07:15:44 +00:00
|
|
|
pos = rex.indexIn(str);
|
|
|
|
if (pos > -1)
|
|
|
|
str = rex.cap(2).trimmed();
|
2008-11-16 18:48:25 +00:00
|
|
|
entryCO->insertItem(0, str);
|
|
|
|
entryCO->setCurrentIndex(0);
|
|
|
|
|
2007-04-24 14:35:15 +00:00
|
|
|
selectionChanged();
|
|
|
|
updateLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::updateLists()
|
2007-04-24 14:35:15 +00:00
|
|
|
{
|
|
|
|
meaningsTV->clear();
|
2010-04-20 09:59:16 +00:00
|
|
|
|
|
|
|
if (entryCO->currentText().isEmpty())
|
|
|
|
return;
|
|
|
|
|
2007-04-24 14:35:15 +00:00
|
|
|
meaningsTV->setUpdatesEnabled(false);
|
|
|
|
|
2008-11-16 18:48:25 +00:00
|
|
|
QString const lang = languageCO->itemData(
|
|
|
|
languageCO->currentIndex()).toString();
|
2012-03-02 10:20:09 +00:00
|
|
|
Language * language = const_cast<Language*>(lyx::languages.getLanguage(fromqstr(lang)));
|
|
|
|
docstring const lang_code = from_ascii(language->code());
|
2008-11-16 18:48:25 +00:00
|
|
|
|
|
|
|
Thesaurus::Meanings meanings =
|
2012-03-02 10:20:09 +00:00
|
|
|
getMeanings(WordLangTuple(qstring_to_ucs4(entryCO->currentText()), language));
|
2007-04-24 14:35:15 +00:00
|
|
|
|
|
|
|
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);
|
2007-12-12 19:28:07 +00:00
|
|
|
for (vector<docstring>::const_iterator cit2 = cit->second.begin();
|
2007-04-24 14:35:15 +00:00
|
|
|
cit2 != cit->second.end(); ++cit2) {
|
|
|
|
QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
|
|
|
|
i2->setText(0, toqstr(*cit2));
|
|
|
|
}
|
2008-11-16 18:48:25 +00:00
|
|
|
meaningsTV->setEnabled(true);
|
|
|
|
lookupPB->setEnabled(true);
|
2011-01-15 12:09:23 +00:00
|
|
|
bool const readonly = isBufferReadonly();
|
|
|
|
replaceED->setEnabled(!readonly);
|
|
|
|
replacePB->setEnabled(!readonly);
|
2008-11-16 18:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2007-04-24 14:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
meaningsTV->setUpdatesEnabled(true);
|
|
|
|
meaningsTV->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::updateContents()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-11-16 18:48:25 +00:00
|
|
|
entryCO->clear();
|
|
|
|
entryCO->addItem(toqstr(text_));
|
|
|
|
entryCO->setCurrentIndex(0);
|
2007-09-05 20:33:29 +00:00
|
|
|
replaceED->setText("");
|
2008-11-16 18:48:25 +00:00
|
|
|
int const pos = languageCO->findData(toqstr(lang_));
|
|
|
|
if (pos != -1)
|
|
|
|
languageCO->setCurrentIndex(pos);
|
2007-09-05 20:33:29 +00:00
|
|
|
updateLists();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
void GuiThesaurus::replaceClicked()
|
|
|
|
{
|
|
|
|
replace(qstring_to_ucs4(replaceED->text()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GuiThesaurus::initialiseParams(string const & data)
|
|
|
|
{
|
2008-11-16 18:48:25 +00:00
|
|
|
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());
|
|
|
|
}
|
2007-10-06 19:59:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiThesaurus::clearParams()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-10-06 19:59:13 +00:00
|
|
|
text_.erase();
|
2008-11-16 18:48:25 +00:00
|
|
|
lang_.erase();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 19:59:13 +00:00
|
|
|
|
|
|
|
void GuiThesaurus::replace(docstring const & newstr)
|
|
|
|
{
|
|
|
|
/* FIXME: this is not suitable ! We need to have a "lock"
|
|
|
|
* on a particular charpos in a paragraph that is broken on
|
|
|
|
* deletion/change !
|
|
|
|
*/
|
|
|
|
docstring const data =
|
2009-07-04 23:02:27 +00:00
|
|
|
replace2string(newstr, text_,
|
2007-10-06 19:59:13 +00:00
|
|
|
true, // case sensitive
|
|
|
|
true, // match word
|
|
|
|
false, // all words
|
|
|
|
true); // forward
|
|
|
|
dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-02 10:20:09 +00:00
|
|
|
Thesaurus::Meanings const & GuiThesaurus::getMeanings(WordLangTuple const & wl)
|
2007-10-06 19:59:13 +00:00
|
|
|
{
|
2012-03-02 10:20:09 +00:00
|
|
|
if (wl.word() != laststr_)
|
|
|
|
meanings_ = thesaurus.lookup(wl);
|
2007-10-06 19:59:13 +00:00
|
|
|
return meanings_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-23 09:44:02 +00:00
|
|
|
Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
|
2007-10-06 19:59:13 +00:00
|
|
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2007-04-24 14:35:15 +00:00
|
|
|
|
|
|
|
|
2008-11-14 14:28:50 +00:00
|
|
|
#include "moc_GuiThesaurus.cpp"
|