forked from qt-creator/qt-creator
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 <hjk@theqtcompany.com>
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
#include "plugincollection.h"
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/categorysortfiltermodel.h>
|
||||
#include <utils/itemviews.h>
|
||||
#include <utils/treemodel.h>
|
||||
|
||||
@@ -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);
|
||||
|
||||
65
src/libs/utils/categorysortfiltermodel.cpp
Normal file
65
src/libs/utils/categorysortfiltermodel.cpp
Normal file
@@ -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
|
||||
|
||||
51
src/libs/utils/categorysortfiltermodel.h
Normal file
51
src/libs/utils/categorysortfiltermodel.h
Normal file
@@ -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 <QSortFilterProxyModel>
|
||||
|
||||
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
|
||||
@@ -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 \
|
||||
|
||||
@@ -41,6 +41,8 @@ QtcLibrary {
|
||||
"bracematcher.h",
|
||||
"buildablehelperlibrary.cpp",
|
||||
"buildablehelperlibrary.h",
|
||||
"categorysortfiltermodel.cpp",
|
||||
"categorysortfiltermodel.h",
|
||||
"changeset.cpp",
|
||||
"changeset.h",
|
||||
"checkablemessagebox.cpp",
|
||||
|
||||
Reference in New Issue
Block a user