From 72c83463f18f09e390818844bc183b9368b253c9 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 9 Jun 2015 14:04:34 +0200 Subject: [PATCH] Move sort filter proxy model with category logic to utils It is useful for other tree models as well. Change-Id: I48262c266229a91d61579ec1dc068fc18bc33ee0 Reviewed-by: hjk --- src/libs/extensionsystem/pluginview.cpp | 33 +---------- src/libs/utils/categorysortfiltermodel.cpp | 65 ++++++++++++++++++++++ src/libs/utils/categorysortfiltermodel.h | 51 +++++++++++++++++ src/libs/utils/utils-lib.pri | 6 +- src/libs/utils/utils.qbs | 2 + 5 files changed, 124 insertions(+), 33 deletions(-) create mode 100644 src/libs/utils/categorysortfiltermodel.cpp create mode 100644 src/libs/utils/categorysortfiltermodel.h diff --git a/src/libs/extensionsystem/pluginview.cpp b/src/libs/extensionsystem/pluginview.cpp index 53f3b71524d..817bfd36c5e 100644 --- a/src/libs/extensionsystem/pluginview.cpp +++ b/src/libs/extensionsystem/pluginview.cpp @@ -35,6 +35,7 @@ #include "plugincollection.h" #include +#include #include #include @@ -277,36 +278,6 @@ public: PluginView *m_view; // Not owned. }; -class SortFilterModel : public QSortFilterProxyModel -{ -public: - SortFilterModel(QObject *parent) : QSortFilterProxyModel(parent) {} - -protected: - bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override - { - if (!source_parent.isValid()) { - // category items should be visible if any of its children match - const QRegExp ®exp = filterRegExp(); - const QModelIndex &categoryIndex = sourceModel()->index(source_row, 0, source_parent); - if (regexp.indexIn(sourceModel()->data(categoryIndex, filterRole()).toString()) != -1) - return true; - const int rowCount = sourceModel()->rowCount(categoryIndex); - const int columnCount = sourceModel()->columnCount(categoryIndex); - for (int row = 0; row < rowCount; ++row) { - for (int column = 0; column < columnCount; ++column) { - if (regexp.indexIn(sourceModel()->data( - sourceModel()->index(row, column, categoryIndex), - filterRole()).toString()) != -1) - return true; - } - } - return false; - } - return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); - } -}; - } // Internal using namespace ExtensionSystem::Internal; @@ -334,7 +305,7 @@ PluginView::PluginView(QWidget *parent) m_model = new TreeModel(this); m_model->setHeader(QStringList() << tr("Name") << tr("Load") << tr("Version") << tr("Vendor")); - m_sortModel = new SortFilterModel(this); + m_sortModel = new CategorySortFilterModel(this); m_sortModel->setSourceModel(m_model); m_sortModel->setSortRole(SortRole); m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive); diff --git a/src/libs/utils/categorysortfiltermodel.cpp b/src/libs/utils/categorysortfiltermodel.cpp new file mode 100644 index 00000000000..5e86e6d18e9 --- /dev/null +++ b/src/libs/utils/categorysortfiltermodel.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include "categorysortfiltermodel.h" + +namespace Utils { + +CategorySortFilterModel::CategorySortFilterModel(QObject *parent) + : QSortFilterProxyModel(parent) +{ +} + +bool CategorySortFilterModel::filterAcceptsRow(int source_row, + const QModelIndex &source_parent) const +{ + if (!source_parent.isValid()) { + // category items should be visible if any of its children match + const QRegExp ®exp = filterRegExp(); + const QModelIndex &categoryIndex = sourceModel()->index(source_row, 0, source_parent); + if (regexp.indexIn(sourceModel()->data(categoryIndex, filterRole()).toString()) != -1) + return true; + const int rowCount = sourceModel()->rowCount(categoryIndex); + const int columnCount = sourceModel()->columnCount(categoryIndex); + for (int row = 0; row < rowCount; ++row) { + for (int column = 0; column < columnCount; ++column) { + if (regexp.indexIn(sourceModel()->data( + sourceModel()->index(row, column, categoryIndex), + filterRole()).toString()) != -1) + return true; + } + } + return false; + } + return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); +} + +} // Utils + diff --git a/src/libs/utils/categorysortfiltermodel.h b/src/libs/utils/categorysortfiltermodel.h new file mode 100644 index 00000000000..11d40de5540 --- /dev/null +++ b/src/libs/utils/categorysortfiltermodel.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#ifndef CATEGORYSORTFILTERMODEL_H +#define CATEGORYSORTFILTERMODEL_H + +#include "utils_global.h" + +#include + +namespace Utils { + +class QTCREATOR_UTILS_EXPORT CategorySortFilterModel : public QSortFilterProxyModel +{ +public: + CategorySortFilterModel(QObject *parent = 0); + +protected: + bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; +}; + +} // Utils + +#endif // CATEGORYSORTFILTERMODEL_H diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 4016eca551d..1b456b873ba 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -90,7 +90,8 @@ SOURCES += $$PWD/environment.cpp \ $$PWD/theme/theme.cpp \ $$PWD/progressindicator.cpp \ $$PWD/fadingindicator.cpp \ - $$PWD/overridecursor.cpp + $$PWD/overridecursor.cpp \ + $$PWD/categorysortfiltermodel.cpp win32:SOURCES += $$PWD/consoleprocess_win.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp @@ -192,7 +193,8 @@ HEADERS += \ $$PWD/progressindicator.h \ $$PWD/fadingindicator.h \ $$PWD/executeondestruction.h \ - $$PWD/overridecursor.h + $$PWD/overridecursor.h \ + $$PWD/categorysortfiltermodel.h FORMS += $$PWD/filewizardpage.ui \ $$PWD/projectintropage.ui \ diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs index d4de660ae31..c92516ba6cf 100644 --- a/src/libs/utils/utils.qbs +++ b/src/libs/utils/utils.qbs @@ -41,6 +41,8 @@ QtcLibrary { "bracematcher.h", "buildablehelperlibrary.cpp", "buildablehelperlibrary.h", + "categorysortfiltermodel.cpp", + "categorysortfiltermodel.h", "changeset.cpp", "changeset.h", "checkablemessagebox.cpp",