diff --git a/src/glossary/glossaryview.cpp b/src/glossary/glossaryview.cpp index 19bdf14..0aab0b1 100644 --- a/src/glossary/glossaryview.cpp +++ b/src/glossary/glossaryview.cpp @@ -1,209 +1,209 @@ /* **************************************************************************** This file is part of Lokalize Copyright (C) 2007-2011 by Nick Shaforostoff 2018-2019 by Simon Depiets This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. **************************************************************************** */ #include "glossaryview.h" #include "lokalize_debug.h" #include "glossary.h" #include "project.h" #include "catalog.h" #include "flowlayout.h" #include "glossarywindow.h" #include "stemming.h" #include #include #include #include #include #include #include #include using namespace GlossaryNS; GlossaryView::GlossaryView(QWidget* parent, Catalog* catalog, const QVector& actions) : QDockWidget(i18nc("@title:window", "Glossary"), parent) , m_browser(new QScrollArea(this)) , m_catalog(catalog) , m_flowLayout(new FlowLayout(FlowLayout::glossary,/*who gets signals*/this, actions, 0, 10)) , m_glossary(Project::instance()->glossary()) , m_rxClean(Project::instance()->markup() + '|' + Project::instance()->accel()) //cleaning regexp; NOTE isEmpty()? , m_rxSplit(QStringLiteral("\\W|\\d"))//splitting regexp , m_currentIndex(-1) , m_normTitle(i18nc("@title:window", "Glossary")) , m_hasInfoTitle(m_normTitle + QStringLiteral(" [*]")) , m_hasInfo(false) { setObjectName(QStringLiteral("glossaryView")); QWidget* w = new QWidget(m_browser); m_browser->setWidget(w); m_browser->setWidgetResizable(true); w->setLayout(m_flowLayout); w->show(); setToolTip(i18nc("@info:tooltip", "

Translations for common terms appear here.

" "

Press shortcut displayed near the term to insert its translation.

" "

Use context menu to add new entry (tip: select words in original and translation fields before calling Define new term).

")); setWidget(m_browser); m_browser->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); m_browser->setAutoFillBackground(true); m_browser->setBackgroundRole(QPalette::Window); m_rxClean.setMinimal(true); connect(m_glossary, &Glossary::changed, this, QOverload<>::of(&GlossaryView::slotNewEntryDisplayed), Qt::QueuedConnection); } GlossaryView::~GlossaryView() { } //TODO define new term by dragging some text. // void GlossaryView::dragEnterEvent(QDragEnterEvent* event) // { // /* if(event->mimeData()->hasUrls() && event->mimeData()->urls().first().path().endsWith(".po")) // { // event->acceptProposedAction(); // };*/ // } // // void GlossaryView::dropEvent(QDropEvent *event) // { // event->acceptProposedAction();*/ // } void GlossaryView::slotNewEntryDisplayed() { slotNewEntryDisplayed(DocPosition()); } void GlossaryView::slotNewEntryDisplayed(DocPosition pos) { //qCWarning(LOKALIZE_LOG)<<"\n\n\n\nstart"<numberOfEntries() <= pos.entry) return;//because of Qt::QueuedConnection //if (!toggleViewAction()->isChecked()) // return; Glossary& glossary = *m_glossary; QString source = m_catalog->source(pos); QString sourceLowered = source.toLower(); QString msg = sourceLowered; msg.remove(m_rxClean); QString msgStemmed; // QRegExp accel(Project::instance()->accel()); // qCWarning(LOKALIZE_LOG)<accel()<sourceLangCode(); QList termIds; foreach (const QString& w, msg.split(m_rxSplit, QString::SkipEmptyParts)) { QString word = stem(sourceLangCode, w); QList indexes = glossary.idsForLangWord(sourceLangCode, word); //if (indexes.size()) //qCWarning(LOKALIZE_LOG)<<"found entry for:" <clearTerms(); bool found = false; //m_flowLayout->setEnabled(false); - foreach (const QByteArray& termId, termIds.toSet()) { + foreach (const QByteArray& termId, QSet(termIds.begin(), termIds.end())) { // now check which of them are really hits... foreach (const QString& enTerm, glossary.terms(termId, sourceLangCode)) { // ...and if so, which part of termEn list we must thank for match ... bool ok = msg.contains(enTerm); //,//Qt::CaseInsensitive //we lowered terms on load if (!ok) { QString enTermStemmed; foreach (const QString& word, enTerm.split(m_rxSplit, QString::SkipEmptyParts)) enTermStemmed += stem(sourceLangCode, word) + ' '; ok = msgStemmed.contains(enTermStemmed); } if (ok) { //insert it into label found = true; int pos = sourceLowered.indexOf(enTerm); m_flowLayout->addTerm(enTerm, termId,/*uppercase*/pos != -1 && source.at(pos).isUpper()); break; } } } //m_flowLayout->setEnabled(true); if (!found) clear(); else if (!m_hasInfo) { m_hasInfo = true; setWindowTitle(m_hasInfoTitle); } setUpdatesEnabled(true); } void GlossaryView::clear() { if (m_hasInfo) { m_flowLayout->clearTerms(); m_hasInfo = false; setWindowTitle(m_normTitle); } } diff --git a/src/tests/projectmodeltest.cpp b/src/tests/projectmodeltest.cpp index f1513b1..f56ca84 100644 --- a/src/tests/projectmodeltest.cpp +++ b/src/tests/projectmodeltest.cpp @@ -1,109 +1,109 @@ /* * This file is part of Lokalize * * Copyright (c) 2019 Alexander Potashev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include "project/projectmodel.h" class ProjectModelTest : public QObject { Q_OBJECT private Q_SLOTS: void testInvalid(); void testHalfTranslated(); }; void ProjectModelTest::testInvalid() { QAtomicInt loaded; auto *model = new ProjectModel(this); connect(model, &ProjectModel::loadingFinished, [&loaded]() { loaded.fetchAndAddRelaxed(1); }); connect(model, &ProjectModel::totalsChanged, [ = ](int fuzzy, int translated, int untranslated, bool done) { QCOMPARE(fuzzy, 0); QCOMPARE(translated, 0); QCOMPARE(untranslated, 0); QCOMPARE(done, true); }); model->setUrl(QUrl::fromLocalFile(QFINDTESTDATA("data/dir-invalid")), {}); // Wait for signal while (!loaded.load()) { QCoreApplication::processEvents(); } QCOMPARE(model->rowCount(QModelIndex()), 1); QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole), QStringLiteral("invalid.po")); QCOMPARE(model->data(model->index(0, 1), Qt::DisplayRole), QRect(0, 0, 0, 0)); QCOMPARE(model->data(model->index(0, 2), Qt::DisplayRole), 0); QCOMPARE(model->data(model->index(0, 3), Qt::DisplayRole), 0); QCOMPARE(model->data(model->index(0, 4), Qt::DisplayRole), 0); QCOMPARE(model->data(model->index(0, 5), Qt::DisplayRole), 0); QCOMPARE(model->data(model->index(0, 6), Qt::DisplayRole), 0); QCOMPARE(model->data(model->index(0, 7), Qt::DisplayRole), QString()); QCOMPARE(model->data(model->index(0, 8), Qt::DisplayRole), QString()); QCOMPARE(model->data(model->index(0, 9), Qt::DisplayRole), QString()); } void ProjectModelTest::testHalfTranslated() { QAtomicInt loaded; auto *model = new ProjectModel(this); connect(model, &ProjectModel::loadingFinished, [&loaded]() { loaded.fetchAndAddRelaxed(1); }); connect(model, &ProjectModel::totalsChanged, [ = ](int fuzzy, int translated, int untranslated, bool done) { QCOMPARE(fuzzy, 1); QCOMPARE(translated, 3); QCOMPARE(untranslated, 2); QCOMPARE(done, true); }); model->setUrl(QUrl::fromLocalFile(QFINDTESTDATA("data/dir-halftranslated")), {}); // Wait for signal - while (!loaded.load()) { + while (!loaded.loadRelaxed()) { QCoreApplication::processEvents(); } QCOMPARE(model->rowCount(QModelIndex()), 1); QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole), QStringLiteral("halftranslated.po")); QCOMPARE(model->data(model->index(0, 1), Qt::DisplayRole), QRect(3, 2, 1, 0)); QCOMPARE(model->data(model->index(0, 2), Qt::DisplayRole), 6); QCOMPARE(model->data(model->index(0, 3), Qt::DisplayRole), 3); QCOMPARE(model->data(model->index(0, 4), Qt::DisplayRole), 1); QCOMPARE(model->data(model->index(0, 5), Qt::DisplayRole), 2); QCOMPARE(model->data(model->index(0, 6), Qt::DisplayRole), 3); QCOMPARE(model->data(model->index(0, 7), Qt::DisplayRole), QStringLiteral("2019-05-20 03:26+0200")); QCOMPARE(model->data(model->index(0, 8), Qt::DisplayRole), QStringLiteral("2019-06-13 08:53+0300")); QCOMPARE(model->data(model->index(0, 9), Qt::DisplayRole), QStringLiteral("Alexander Potashev ")); } QTEST_GUILESS_MAIN(ProjectModelTest) #include "projectmodeltest.moc"