Commit 1a45aaf9 authored by Eike Hein's avatar Eike Hein

Implement QQmlParserStatus in FolderModel

Summary:
This saves four calls to `invalidateFilter()` during startup with
default config. With non-default config it will save various
calls to `invalidate()` and `invalidateFilter()` more.

Test Plan: Tests pass with adjustments.

Reviewers: #plasma, broulik

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D9122
parent fbbdafa1
......@@ -50,7 +50,9 @@ void FolderModelTest::init()
m_folderDir = new QTemporaryDir();
createTestFolder(desktop);
m_folderModel = new FolderModel(this);
m_folderModel->classBegin();
m_folderModel->setUrl(m_folderDir->path() + QDir::separator() + desktop );
m_folderModel->componentComplete();
QSignalSpy s(m_folderModel, &FolderModel::listingCompleted);
s.wait(1000);
}
......@@ -265,10 +267,18 @@ void FolderModelTest::tst_lockedChanged()
void FolderModelTest::tst_multiScreen()
{
delete m_folderModel;
// Custom instance for this test to set screen mapper before marking component
// as complete.
m_folderModel = new FolderModel(this);
m_folderModel->classBegin();
m_folderModel->setUrl(m_folderDir->path() + QDir::separator() + desktop );
auto *screenMapper = ScreenMapper::instance();
m_folderModel->setUsedByContainment(true);
m_folderModel->setScreenMapper(screenMapper);
m_folderModel->setScreen(0);
m_folderModel->componentComplete();
QSignalSpy s(m_folderModel, &FolderModel::listingCompleted);
s.wait(1000);
const auto count = m_folderModel->rowCount();
......@@ -282,10 +292,12 @@ void FolderModelTest::tst_multiScreen()
// move one file to a new screen
const auto movedItem = m_folderModel->index(0, 0).data(FolderModel::UrlRole).toString();
FolderModel secondFolderModel;
secondFolderModel.classBegin();
secondFolderModel.setUrl(m_folderDir->path() + QDir::separator() + desktop );
secondFolderModel.setUsedByContainment(true);
secondFolderModel.setScreenMapper(screenMapper);
secondFolderModel.setScreen(1);
secondFolderModel.componentComplete();
QSignalSpy s2(&secondFolderModel, &FolderModel::listingCompleted);
s2.wait(1000);
const auto count2 = secondFolderModel.rowCount();
......
......@@ -59,9 +59,11 @@ void PositionerTest::cleanupTestCase()
void PositionerTest::init()
{
m_folderModel = new FolderModel(this);
m_folderModel->classBegin();
m_folderModel->setScreen(0);
m_folderModel->setScreenMapper(ScreenMapper::instance());
m_folderModel->setUsedByContainment(true);
m_folderModel->componentComplete();
m_positioner = new Positioner(this);
m_positioner->setEnabled(true);
m_positioner->setFolderModel(m_folderModel);
......@@ -217,10 +219,12 @@ void PositionerTest::tst_proxyMapping()
{
auto *screenMapper = ScreenMapper::instance();
FolderModel secondFolderModel;
secondFolderModel.classBegin();
secondFolderModel.setUrl(m_folderDir->path() + QDir::separator() + desktop );
secondFolderModel.setUsedByContainment(true);
secondFolderModel.setScreenMapper(screenMapper);
secondFolderModel.setScreen(1);
secondFolderModel.componentComplete();
Positioner secondPositioner;
secondPositioner.setEnabled(true);
secondPositioner.setFolderModel(&secondFolderModel);
......
......@@ -119,7 +119,8 @@ FolderModel::FolderModel(QObject *parent) : QSortFilterProxyModel(parent),
m_parseDesktopFiles(false),
m_previews(false),
m_filterMode(NoFilter),
m_filterPatternMatchAll(true)
m_filterPatternMatchAll(true),
m_complete(false)
{
//needed to pass the job around with qml
qmlRegisterType<KIO::DropJob>();
......@@ -234,6 +235,34 @@ QHash< int, QByteArray > FolderModel::staticRoleNames()
return roleNames;
}
void FolderModel::classBegin()
{
}
void FolderModel::componentComplete()
{
m_complete = true;
invalidate();
}
void FolderModel::invalidateIfComplete()
{
if (!m_complete) {
return;
}
invalidate();
}
void FolderModel::invalidateFilterIfComplete()
{
if (!m_complete) {
return;
}
invalidateFilter();
}
QString FolderModel::url() const
{
return m_url;
......@@ -394,7 +423,7 @@ void FolderModel::setSortMode(int mode)
if (mode == -1 /* Unsorted */) {
setDynamicSortFilter(false);
} else {
invalidate();
invalidateIfComplete();
sort(m_sortMode, m_sortDesc ? Qt::DescendingOrder : Qt::AscendingOrder);
setDynamicSortFilter(true);
}
......@@ -414,7 +443,7 @@ void FolderModel::setSortDesc(bool desc)
m_sortDesc = desc;
if (m_sortMode != -1 /* Unsorted */) {
invalidate();
invalidateIfComplete();
sort(m_sortMode, m_sortDesc ? Qt::DescendingOrder : Qt::AscendingOrder);
}
......@@ -433,7 +462,7 @@ void FolderModel::setSortDirsFirst(bool enable)
m_sortDirsFirst = enable;
if (m_sortMode != -1 /* Unsorted */) {
invalidate();
invalidateIfComplete();
sort(m_sortMode, m_sortDesc ? Qt::DescendingOrder : Qt::AscendingOrder);
}
......@@ -524,7 +553,7 @@ void FolderModel::setFilterMode(int filterMode)
if (m_filterMode != (FilterMode)filterMode) {
m_filterMode = (FilterMode)filterMode;
invalidateFilter();
invalidateFilterIfComplete();
emit filterModeChanged();
}
......@@ -555,7 +584,7 @@ void FolderModel::setFilterPattern(const QString &pattern)
m_regExps.append(rx);
}
invalidateFilter();
invalidateFilterIfComplete();
emit filterPatternChanged();
}
......@@ -573,7 +602,7 @@ void FolderModel::setFilterMimeTypes(const QStringList &mimeList)
m_mimeSet = set;
invalidateFilter();
invalidateFilterIfComplete();
emit filterMimeTypesChanged();
}
......@@ -1820,11 +1849,11 @@ void FolderModel::setScreenMapper(ScreenMapper *screenMapper)
m_screenMapper = screenMapper;
if (m_screenMapper) {
connect(m_screenMapper, &ScreenMapper::screensChanged, this, &FolderModel::invalidateFilter);
connect(m_screenMapper, &ScreenMapper::screenMappingChanged, this, &FolderModel::invalidateFilter);
connect(m_screenMapper, &ScreenMapper::screensChanged, this, &FolderModel::invalidateFilterIfComplete);
connect(m_screenMapper, &ScreenMapper::screenMappingChanged, this, &FolderModel::invalidateFilterIfComplete);
}
invalidateFilter();
invalidateFilterIfComplete();
emit screenMapperChanged();
}
......
......@@ -24,6 +24,7 @@
#include <QImage>
#include <QItemSelection>
#include <QQmlParserStatus>
#include <QPointer>
#include <QSortFilterProxyModel>
#include <QStringList>
......@@ -73,9 +74,10 @@ class DirLister : public KDirLister
void handleError(KIO::Job *job) override;
};
class FOLDERPLUGIN_TESTS_EXPORT FolderModel : public QSortFilterProxyModel
class FOLDERPLUGIN_TESTS_EXPORT FolderModel : public QSortFilterProxyModel, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
Q_PROPERTY(QString iconName READ iconName NOTIFY iconNameChanged)
......@@ -134,6 +136,9 @@ class FOLDERPLUGIN_TESTS_EXPORT FolderModel : public QSortFilterProxyModel
QHash<int, QByteArray> roleNames() const override;
static QHash<int, QByteArray> staticRoleNames();
void classBegin() override;
void componentComplete() override;
QString url() const;
void setUrl(const QString &url);
......@@ -289,6 +294,8 @@ class FOLDERPLUGIN_TESTS_EXPORT FolderModel : public QSortFilterProxyModel
void emptyTrashBin();
void restoreSelectedFromTrash();
void undoTextChanged(const QString &text);
void invalidateIfComplete();
void invalidateFilterIfComplete();
private:
struct DragImage {
......@@ -343,6 +350,7 @@ class FOLDERPLUGIN_TESTS_EXPORT FolderModel : public QSortFilterProxyModel
int m_screen = -1;
ScreenMapper *m_screenMapper = nullptr;
QObject *m_appletInterface = nullptr;
bool m_complete;
};
#endif
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