diff --git a/plugins/problemreporter/problemreporterplugin.h b/plugins/problemreporter/problemreporterplugin.h --- a/plugins/problemreporter/problemreporterplugin.h +++ b/plugins/problemreporter/problemreporterplugin.h @@ -65,7 +65,7 @@ void updateHighlight(const KDevelop::IndexedString& url); void textDocumentCreated(KDevelop::IDocument* document); void documentActivated(KDevelop::IDocument* document); - void showModel(const QString& name); + void showModel(const QString& id); private: void updateOpenedDocumentsHighlight(); diff --git a/plugins/problemreporter/problemreporterplugin.cpp b/plugins/problemreporter/problemreporterplugin.cpp --- a/plugins/problemreporter/problemreporterplugin.cpp +++ b/plugins/problemreporter/problemreporterplugin.cpp @@ -86,7 +86,7 @@ , m_model(new ProblemReporterModel(this)) { KDevelop::ProblemModelSet* pms = core()->languageController()->problemModelSet(); - pms->addModel(QStringLiteral("Parser"), m_model); + pms->addModel(QStringLiteral("Parser"), i18n("Parser"), m_model); core()->uiController()->addToolView(i18n("Problems"), m_factory); setXMLFile(QStringLiteral("kdevproblemreporter.rc")); @@ -172,11 +172,11 @@ ph->setProblems(documentProblems); } -void ProblemReporterPlugin::showModel(const QString& name) +void ProblemReporterPlugin::showModel(const QString& id) { auto w = dynamic_cast(core()->uiController()->findToolView(i18n("Problems"), m_factory)); if (w) - w->showModel(name); + w->showModel(id); } KDevelop::ContextMenuExtension ProblemReporterPlugin::contextMenuExtension(KDevelop::Context* context) diff --git a/plugins/problemreporter/problemsview.h b/plugins/problemreporter/problemsview.h --- a/plugins/problemreporter/problemsview.h +++ b/plugins/problemreporter/problemsview.h @@ -64,16 +64,16 @@ void onModelAdded(const ModelData& data); /// Triggered when a model is removed from the ModelSet - void onModelRemoved(const QString& name); + void onModelRemoved(const QString& id); /// Triggered when the user (or program) selects a new tab void onCurrentChanged(int idx); /// Triggered when a view changes (happens when the model data changes) void onViewChanged(); /// Open tab for selected model - void showModel(const QString& name); + void showModel(const QString& id); void selectNextItem() override; void selectPreviousItem() override; @@ -111,6 +111,7 @@ QLineEdit* m_filterEdit; int m_prevTabIdx; + QVector m_models; }; } diff --git a/plugins/problemreporter/problemsview.cpp b/plugins/problemreporter/problemsview.cpp --- a/plugins/problemreporter/problemsview.cpp +++ b/plugins/problemreporter/problemsview.cpp @@ -370,45 +370,26 @@ addModel(data); } -/** - * @brief Returns the name part of the label - * - * E.g.: Test (666) => Test - */ -QString nameFromLabel(const QString& label) -{ - QString txt = label; - int i = txt.lastIndexOf('('); - if (i != -1) - txt = txt.left(i - 1); /// ignore whitespace before '(' - return txt; -} - -int tabIndexForName(const QTabWidget* tabWidget, const QString& name) +void ProblemsView::showModel(const QString& id) { - for (int idx = 0; idx < tabWidget->count(); ++idx) { - if (nameFromLabel(tabWidget->tabText(idx)) == name) { - return idx; + for (int i = 0; i < m_models.size(); ++i) { + if (m_models[i].id == id) { + m_tabWidget->setCurrentIndex(i); + return; } } - - return -1; } -void ProblemsView::showModel(const QString& name) +void ProblemsView::onModelRemoved(const QString& id) { - int idx = tabIndexForName(m_tabWidget, name); - if (idx >= 0) - m_tabWidget->setCurrentIndex(idx); -} - -void ProblemsView::onModelRemoved(const QString& name) -{ - int idx = tabIndexForName(m_tabWidget, name); - if (idx >= 0) { - QWidget* w = m_tabWidget->widget(idx); - m_tabWidget->removeTab(idx); - delete w; + for (int i = 0; i < m_models.size(); ++i) { + if (m_models[i].id == id) { + m_models.remove(i); + QWidget* w = m_tabWidget->widget(i); + m_tabWidget->removeTab(i); + delete w; + return; + } } } @@ -433,41 +414,45 @@ updateTab(idx, rows); } -void ProblemsView::addModel(const ModelData& data) +void ProblemsView::addModel(const ModelData& newData) { // We implement follows tabs order: // - // 1) First tab always used by "Parser" model due it's the most important + // 1) First tab always used by "Parser" model due to it's the most important // problem listing, it should be at the front (K.Funk idea at #kdevelop IRC channel). // // 2) Other tabs are alphabetically ordered. - static const QString parserTitle = QStringLiteral("Parser"); + static const QString parserId = QStringLiteral("Parser"); - ProblemTreeView* view = new ProblemTreeView(nullptr, data.model); + ProblemTreeView* view = new ProblemTreeView(nullptr, newData.model); connect(view, &ProblemTreeView::changed, this, &ProblemsView::onViewChanged); int insertIdx = 0; - if (data.name != parserTitle) { - for (insertIdx = 0; insertIdx < m_tabWidget->count(); ++insertIdx) { - QString tabName = nameFromLabel(m_tabWidget->tabText(insertIdx)); + if (newData.id != parserId) { + for (insertIdx = 0; insertIdx < m_models.size(); ++insertIdx) { + const ModelData& currentData = m_models[insertIdx]; // Skip first element if it's already occupied by "Parser" model - if (insertIdx == 0 && tabName == parserTitle) + if (insertIdx == 0 && currentData.id == parserId) continue; - if (tabName.localeAwareCompare(data.name) > 0) + if (currentData.name.localeAwareCompare(newData.name) > 0) break; } } - m_tabWidget->insertTab(insertIdx, view, data.name); + m_tabWidget->insertTab(insertIdx, view, newData.name); + m_models.insert(insertIdx, newData); updateTab(insertIdx, view->model()->rowCount()); } void ProblemsView::updateTab(int idx, int rows) { - const QString name = nameFromLabel(m_tabWidget->tabText(idx)); + if (idx < 0 || idx >= m_models.size()) + return; + + const QString name = m_models[idx].name; const QString tabText = i18nc("%1: tab name, %2: number of problems", "%1 (%2)", name, rows); m_tabWidget->setTabText(idx, tabText); } diff --git a/plugins/problemreporter/tests/test_problemsview.cpp b/plugins/problemreporter/tests/test_problemsview.cpp --- a/plugins/problemreporter/tests/test_problemsview.cpp +++ b/plugins/problemreporter/tests/test_problemsview.cpp @@ -64,7 +64,7 @@ ProblemModel* model = new ProblemModel(pms); IProblem::Ptr p(new DetectedProblem()); model->addProblem(p); - pms->addModel(QStringLiteral("MODEL1"), model); + pms->addModel(QStringLiteral("MODEL1_ID"), QStringLiteral("MODEL1"), model); m_view.reset(new ProblemsView()); } @@ -88,7 +88,7 @@ void TestProblemsView::testAddModel() { ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); - pms->addModel(QStringLiteral("MODEL2"), new ProblemModel(pms)); + pms->addModel(QStringLiteral("MODEL2_ID"), QStringLiteral("MODEL2"), new ProblemModel(pms)); QTabWidget* tab = tabWidget(); QVERIFY(tab); @@ -130,9 +130,9 @@ { // Remove the model ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); - ProblemModel* model = pms->findModel(QStringLiteral("MODEL1")); + ProblemModel* model = pms->findModel(QStringLiteral("MODEL1_ID")); QVERIFY(model); - pms->removeModel(QStringLiteral("MODEL1")); + pms->removeModel(QStringLiteral("MODEL1_ID")); delete model; model = nullptr; @@ -146,7 +146,7 @@ void TestProblemsView::testAddRemoveProblems() { ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); - ProblemModel* model = pms->findModel(QStringLiteral("MODEL2")); + ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID")); QVERIFY(model); QTabWidget* tab = tabWidget(); @@ -178,7 +178,7 @@ void TestProblemsView::testSetProblems() { ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); - ProblemModel* model = pms->findModel(QStringLiteral("MODEL2")); + ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID")); QVERIFY(model); QTabWidget* tab = tabWidget(); diff --git a/shell/problemmodelset.h b/shell/problemmodelset.h --- a/shell/problemmodelset.h +++ b/shell/problemmodelset.h @@ -34,6 +34,7 @@ /// Struct that handles the model and it's name as one unit, stored in ProblemModelSet struct ModelData { + QString id; QString name; ProblemModel *model; }; @@ -50,10 +51,10 @@ * @code * ProblemModelSet *set = new ProblemModelSet(); * ProblemModel *model = new ProblemModel(nullptr); - * set->addModel(QStringLiteral("MODEL"), model); // added() signal is emitted + * set->addModel(QStringLiteral("MODEL_ID"), QStringLiteral("MODEL"), model); // added() signal is emitted * set->models().count(); // returns 1 - * set->findModel(QStringLiteral("MODEL")); // returns the model just added - * set->removeModel(QStringLiteral("MODEL")); // removed() signal is emitted + * set->findModel(QStringLiteral("MODEL_ID")); // returns the model just added + * set->removeModel(QStringLiteral("MODEL_ID")); // removed() signal is emitted * @endcode * */ @@ -65,16 +66,16 @@ ~ProblemModelSet() override; /// Adds a model - void addModel(const QString &name, ProblemModel *model); + void addModel(const QString &id, const QString &name, ProblemModel *model); /// Finds a model - ProblemModel* findModel(const QString &name) const; + ProblemModel* findModel(const QString &id) const; /// Removes a model - void removeModel(const QString &name); + void removeModel(const QString &id); /// Show model in ProblemsView - void showModel(const QString &name); + void showModel(const QString &id); /// Retrieves a list of models stored QVector models() const; @@ -84,10 +85,10 @@ void added(const ModelData &model); /// Emitted when a model is removed - void removed(const QString &name); + void removed(const QString &id); /// Emitted when showModel() is called - void showRequested(const QString &name); + void showRequested(const QString &id); /// Emitted when any model emits problemsChanged() void problemsChanged(); diff --git a/shell/problemmodelset.cpp b/shell/problemmodelset.cpp --- a/shell/problemmodelset.cpp +++ b/shell/problemmodelset.cpp @@ -37,54 +37,53 @@ ProblemModelSet::~ProblemModelSet() = default; -void ProblemModelSet::addModel(const QString &name, ProblemModel *model) +void ProblemModelSet::addModel(const QString &id, const QString &name, ProblemModel *model) { - ModelData m; - m.name = name; - m.model = model; + ModelData m{id, name, model}; + d->data.push_back(m); connect(model, &ProblemModel::problemsChanged, this, &ProblemModelSet::problemsChanged); emit added(m); } -ProblemModel* ProblemModelSet::findModel(const QString &name) const +ProblemModel* ProblemModelSet::findModel(const QString &id) const { ProblemModel *model = nullptr; foreach (const ModelData &data, d->data) { - if (data.name == name) { + if (data.id == id) { model = data.model; break; } } return model; } -void ProblemModelSet::removeModel(const QString &name) +void ProblemModelSet::removeModel(const QString &id) { QVector::iterator itr = d->data.begin(); + while (itr != d->data.end()) { - if(itr->name == name) + if(itr->id == id) break; ++itr; } if(itr != d->data.end()) { (*itr).model->disconnect(this); d->data.erase(itr); + emit removed(id); } - - emit removed(name); } -void ProblemModelSet::showModel(const QString &name) +void ProblemModelSet::showModel(const QString &id) { for (const ModelData &data : d->data) { - if (data.name == name) { - emit showRequested(name); + if (data.id == id) { + emit showRequested(data.id); return; } } diff --git a/shell/tests/test_problemmodelset.cpp b/shell/tests/test_problemmodelset.cpp --- a/shell/tests/test_problemmodelset.cpp +++ b/shell/tests/test_problemmodelset.cpp @@ -32,14 +32,21 @@ const int testModelCount = 3; +const QString testModelIds[] = { + QStringLiteral("MODEL1_ID"), + QStringLiteral("MODEL2_ID"), + QStringLiteral("MODEL3_ID") +}; + const QString testModelNames[] = { QStringLiteral("MODEL1"), QStringLiteral("MODEL2"), QStringLiteral("MODEL3") }; struct TestModelData { + QString id; QString name; ProblemModel* model; }; @@ -72,7 +79,7 @@ m_set.reset(new ProblemModelSet()); for (int i = 0; i < testModelCount; i++) { - m_testData.push_back(TestModelData({testModelNames[i], new ProblemModel(nullptr)})); + m_testData.push_back(TestModelData({testModelIds[i], testModelNames[i], new ProblemModel(nullptr)})); } } @@ -93,7 +100,7 @@ int c = 0; for (int i = 0; i < testModelCount; i++) { - m_set->addModel(m_testData[i].name, m_testData[i].model); + m_set->addModel(m_testData[i].id, m_testData[i].name, m_testData[i].model); c++; QCOMPARE(spy.count(), c); QCOMPARE(m_set->models().count(), c); @@ -105,7 +112,7 @@ { ProblemModel *model = nullptr; for (int i = 0; i < testModelCount; i++) { - model = m_set->findModel(m_testData[i].name); + model = m_set->findModel(m_testData[i].id); QVERIFY(model); QVERIFY(model == m_testData[i].model); @@ -135,7 +142,7 @@ int c = 0; foreach (const TestModelData &data, m_testData) { - m_set->removeModel(data.name); + m_set->removeModel(data.id); c++; QCOMPARE(spy.count(), c);