Commit 2bae103f authored by David Redondo's avatar David Redondo 🏎

[Cuttleffish] Use proper QSortFilterProxyModel

Summary:
Don't reload everything when filter or category is changed. Fixes selected icon
changing when filtering and the first icon not being selectd when the current
icon is selected.

Test Plan: select and filter

Reviewers: #plasma, apol

Reviewed By: apol

Subscribers: apol, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D27421
parent a8576410
......@@ -5,7 +5,7 @@ find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED Test)
include_directories(../src)
ecm_add_test(iconmodeltest.cpp ../src/iconmodel.cpp
ecm_add_test(iconmodeltest.cpp ../src/iconmodel.cpp ../src/sortfiltermodel.cpp
TEST_NAME iconmodeltest
LINK_LIBRARIES
Qt5::Gui
......
......@@ -26,6 +26,7 @@
#include <QSignalSpy>
#include "iconmodel.h"
#include "sortfiltermodel.h"
using namespace CuttleFish;
......@@ -40,11 +41,14 @@ private Q_SLOTS:
void init()
{
m_iconModel = new IconModel(this);
m_proxyModel = new SortFilterModel(this);
m_proxyModel->setSourceModel(m_iconModel);
}
void cleanup()
{
delete m_iconModel;
delete m_proxyModel;
}
void initTestCase()
......@@ -55,14 +59,14 @@ private Q_SLOTS:
{
const int _all = m_iconModel->rowCount(QModelIndex());
m_iconModel->setFilter("edit");
const int _edit = m_iconModel->rowCount(QModelIndex());
m_proxyModel->setFilter("edit");
const int _edit = m_proxyModel->rowCount(QModelIndex());
m_iconModel->setCategory("actions");
const int _editactions = m_iconModel->rowCount(QModelIndex());
m_proxyModel->setCategory("actions");
const int _editactions = m_proxyModel->rowCount(QModelIndex());
m_iconModel->setCategory("all");
const int _alledit = m_iconModel->rowCount(QModelIndex());
m_proxyModel->setCategory("all");
const int _alledit = m_proxyModel->rowCount(QModelIndex());
QVERIFY(_all > _edit);
QVERIFY(_all > _editactions);
......@@ -80,6 +84,7 @@ private:
QJsonArray m_empty;
IconModel* m_iconModel;
SortFilterModel* m_proxyModel;
};
......
......@@ -35,7 +35,8 @@ GridView {
cacheBuffer: 20
highlightMoveDuration: 0
boundsBehavior: Flickable.StopAtBounds
model: iconModel
model: proxyModel
currentIndex: proxyModel.currentIndex
highlight: Item {}
......@@ -48,4 +49,7 @@ GridView {
width: Kirigami.Units.gridUnit * 8
height: width
}
Component.onCompleted: {
currentItem.setAsPreview()
}
}
......@@ -91,9 +91,9 @@ MouseArea {
}
}
onClicked: (mouse) => {
iconGrid.currentIndex = index;
proxyModel.currentIndex = index
iconGrid.forceActiveFocus();
if (mouse.button == Qt.RightButton) {
if (mouse.button == Qt.RightButton) {
cuttlefish.itemRightClicked()
}
}
......
......@@ -56,7 +56,7 @@ Rectangle {
repeat: false
interval: 100
onTriggered: {
iconModel.filter = filterInput.text
proxyModel.filter = filterInput.text
}
}
Component.onCompleted: {
......@@ -70,11 +70,10 @@ Rectangle {
model: iconModel.categories
onActivated: {
if (currentText == "all") {
iconModel.category = "";
proxyModel.category = "";
} else if (currentText != "") {
iconModel.category = currentText
proxyModel.category = currentText
}
iconModel.sort()
}
popup.modal: false
}
......
......@@ -3,6 +3,7 @@ set(cuttlefish_SRCS
main.cpp
iconmodel.cpp
colorschemes.cpp
sortfiltermodel.cpp
)
add_executable(cuttlefish ${cuttlefish_SRCS})
......
......@@ -60,8 +60,6 @@ IconModel::IconModel(QObject *parent) :
m_roleNames.insert(Theme, "iconTheme");
m_roleNames.insert(Type, "type");
connect(this, &IconModel::categoryChanged, this, &IconModel::load);
m_categories = QStringList() << "all" \
<< "actions"
<< "animations"
......@@ -169,39 +167,11 @@ void IconModel::add(const QFileInfo &info, const QString &cat)
}
}
QString IconModel::category() const
{
return m_category;
}
QStringList IconModel::categories() const
{
return m_categories;
}
void IconModel::setCategory(const QString& cat)
{
if (cat != m_category) {
m_category = cat;
emit categoryChanged();
}
}
QString IconModel::filter() const
{
return m_filter;
}
void IconModel::setFilter(const QString &filter)
{
//qDebug() << "Filter: " << filter;
if (m_filter != filter) {
m_filter = filter;
load();
emit filterChanged();
}
}
void IconModel::load()
{
//qDebug() << "\n -- Loading (category / filter) : " << m_category << m_filter;
......@@ -247,47 +217,18 @@ void IconModel::load()
while (it.hasNext()) {
it.next();
const QFileInfo &info = it.fileInfo();
if (matchIcons(info)) {
add(info, categoryFromPath(info.absoluteFilePath()));
}
add(info, categoryFromPath(info.absoluteFilePath()));
}
}
}
}
sort();
endResetModel();
m_loading = false;
emit loadingChanged();
}
void IconModel::sort()
{
std::sort(m_icons.begin(), m_icons.end());
}
bool IconModel::matchIcons(const QFileInfo& info)
{
bool ok = false;
// Category is empty or all? Skip further matching.
bool catmatch = m_category.isEmpty() || m_category == QStringLiteral("all");
// category match?
if (!catmatch && m_category == categoryFromPath(info.absoluteFilePath())) {
catmatch = true;
}
// name filter
if (m_filter.isEmpty() || info.fileName().indexOf(m_filter) != -1) {
if (catmatch) {
ok = true;
}
}
return ok;
}
QString IconModel::categoryFromPath(const QString& path)
{
QStringList cats;
......
......@@ -36,8 +36,6 @@ class IconModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
Q_PROPERTY(QString category READ category WRITE setCategory NOTIFY categoryChanged)
Q_PROPERTY(QStringList categories READ categories NOTIFY categoriesChanged)
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged);
......@@ -64,16 +62,9 @@ public:
QString key(int role) const;
bool matchIcons(const QFileInfo &info);
void add(const QFileInfo &info, const QString &cat);
void remove(const QString &iconFile);
void setCategory(const QString &cat);
QString category() const;
void setFilter(const QString &filter);
QString filter() const;
QStringList categories() const;
bool loading();
......@@ -81,12 +72,9 @@ public:
void load();
Q_INVOKABLE void output(const QString &text);
Q_INVOKABLE void sort();
Q_SIGNALS:
void filterChanged();
void categoryChanged();
void categoriesChanged();
void loadingChanged();
......@@ -94,9 +82,7 @@ private:
QHash<int, QByteArray> m_roleNames;
QStringList m_icons;
QString m_category;
QStringList m_categories;
QString m_filter;
QHash<QString, QVariantMap> m_data;
QHash<QString, QString> m_categoryTranslations;
......
......@@ -37,6 +37,7 @@
// Own
#include "colorschemes.h"
#include "iconmodel.h"
#include "sortfiltermodel.h"
void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
......@@ -121,8 +122,13 @@ int main(int argc, char **argv)
}
auto iconModel = new CuttleFish::IconModel(engine.rootContext());
auto proxyModel = new CuttleFish::SortFilterModel(engine.rootContext());
proxyModel->setSourceModel(iconModel);
proxyModel->sort(0);
auto colorSchemes = new CuttleFish::ColorSchemes(engine.rootContext());
engine.rootContext()->setContextProperty("iconModel", iconModel);
engine.rootContext()->setContextProperty("proxyModel", proxyModel);
engine.rootContext()->setContextProperty("pickerMode", parser.isSet("picker"));
engine.rootContext()->setContextProperty("colorSchemes", colorSchemes);
......
/***************************************************************************
* *
* Copyright 2020 David Redondo <kde@david-redondo.de> *
* *
* 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 . *
* *
***************************************************************************/
#include "iconmodel.h"
#include "sortfiltermodel.h"
using namespace CuttleFish;
SortFilterModel::SortFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
setSortRole(IconModel::IconName);
setSortCaseSensitivity(Qt::CaseInsensitive);
}
bool SortFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent);
if (!(m_category.isEmpty() || m_category == QLatin1String("all"))
&& (m_category != sourceIndex.data(IconModel::Category))) {
return false;
}
if (!m_filter.isEmpty()) {
return sourceIndex.data(IconModel::IconName).toString().contains(m_filter, Qt::CaseInsensitive);
}
return true;
}
int SortFilterModel::currentIndex()
{
const QModelIndex index = mapFromSource(m_currentSourceIndex);
if (index.isValid()) {
return index.row();
}
m_currentSourceIndex = mapFromSource(this->index(0, 0));
return 0;
}
void SortFilterModel::setCurrentIndex(int index)
{
if (mapFromSource(m_currentSourceIndex).row() != index) {
m_currentSourceIndex = mapToSource(this->index(index, 0));
emit currentIndexChanged();
}
}
QString SortFilterModel::category() const
{
return m_category;
}
void SortFilterModel::setCategory(const QString &category)
{
if (category == m_category) {
return;
}
int oldIndex = currentIndex();
m_category = category;
invalidateFilter();
emit categoryChanged();
if (currentIndex() != oldIndex) {
emit currentIndexChanged();
}
}
QString SortFilterModel::filter() const
{
return m_filter;
}
void SortFilterModel::setFilter(const QString& filter)
{
if (filter == m_filter) {
return;
}
int oldIndex = currentIndex();
m_filter = filter;
invalidateFilter();
emit filterChanged();
if (currentIndex() != oldIndex) {
emit currentIndexChanged();
}
}
/***************************************************************************
* *
* Copyright 2020 David Redondo <kde@david-redondo.de> *
* *
* 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 . *
* *
***************************************************************************/
#include <QSortFilterProxyModel>
namespace CuttleFish {
class SortFilterModel : public QSortFilterProxyModel {
Q_OBJECT
Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
Q_PROPERTY(QString category READ category WRITE setCategory NOTIFY categoryChanged)
Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
public:
SortFilterModel(QObject *parent);
void setCategory(const QString &category);
QString category() const;
void setFilter(const QString &filter);
QString filter() const;
void setCurrentIndex(int index);
int currentIndex();
Q_SIGNALS:
void filterChanged();
void categoryChanged();
void currentIndexChanged();
private:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
QString m_category;
QString m_filter;
QModelIndex m_currentSourceIndex;
};
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment