From 49d098c2d7d73d912687e7c675965444bfd3d6a5 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Wed, 20 Feb 2019 17:43:25 +0100 Subject: [PATCH] folder view sorting --- cfoldersortfilterproxymodel.cpp | 20 +++++ cfoldersortfilterproxymodel.h | 29 +++++++ cimportdialog.cpp | 2 +- cimportdialog.h | 4 +- cmainwindow.cpp | 34 ++++++--- cmainwindow.h | 76 ++++++++++--------- common.cpp | 17 +++-- common.h | 4 +- ....cpp => cthumbnailsortfilterproxymodel.cpp | 10 +-- ...odel.h => cthumbnailsortfilterproxymodel.h | 4 +- pictureLibrary.pro | 6 +- 11 files changed, 137 insertions(+), 69 deletions(-) create mode 100644 cfoldersortfilterproxymodel.cpp create mode 100644 cfoldersortfilterproxymodel.h rename cthumbnailfilterproxymodel.cpp => cthumbnailsortfilterproxymodel.cpp (53%) rename cthumbnailfilterproxymodel.h => cthumbnailsortfilterproxymodel.h (85%) diff --git a/cfoldersortfilterproxymodel.cpp b/cfoldersortfilterproxymodel.cpp new file mode 100644 index 0000000..a65d1ef --- /dev/null +++ b/cfoldersortfilterproxymodel.cpp @@ -0,0 +1,20 @@ +/*! + \file cfoldersortfilterproxymodel.cpp + +*/ + +#include "cfoldersortfilterproxymodel.h" + + +cFolderSortFilterProxyModel::cFolderSortFilterProxyModel(QObject* parent) : + QSortFilterProxyModel (parent) +{ +} + +bool cFolderSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const +{ + QVariant leftData = sourceModel()->data(left); + QVariant rightData = sourceModel()->data(right); + + return(leftData.toString() < rightData.toString()); +} diff --git a/cfoldersortfilterproxymodel.h b/cfoldersortfilterproxymodel.h new file mode 100644 index 0000000..6f53f4d --- /dev/null +++ b/cfoldersortfilterproxymodel.h @@ -0,0 +1,29 @@ +/*! + \file cfoldersortfilterproxymodel.h + +*/ + +#ifndef CFOLDERSORTFILTERPROXYMODEL_H +#define CFOLDERSORTFILTERPROXYMODEL_H + + +#include + + +/*! + \brief + + \class cFolderSortFilterProxyModel cfoldersortfilterproxymodel.h "cfoldersortfilterproxymodel.h" +*/ +class cFolderSortFilterProxyModel : public QSortFilterProxyModel +{ + Q_OBJECT + +public: + cFolderSortFilterProxyModel(QObject* parent = nullptr); + +protected: + bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; +}; + +#endif // CFOLDERSORTFILTERPROXYMODEL_H diff --git a/cimportdialog.cpp b/cimportdialog.cpp index 2a829c3..0324e06 100644 --- a/cimportdialog.cpp +++ b/cimportdialog.cpp @@ -62,7 +62,7 @@ void cImportDialog::initUI() ui->m_lpFolderView->setModel(m_lpFolderViewModel); m_lpThumbnailViewModel = new QStandardItemModel; - m_lpThumbnailFilterProxyModel = new cThumbnailFilterProxyModel(this); + m_lpThumbnailFilterProxyModel = new cThumbnailSortFilterProxyModel(this); ui->m_lpThumbnailView->setModel(m_lpThumbnailFilterProxyModel); m_lpThumbnailFilterProxyModel->setSourceModel(m_lpThumbnailViewModel); diff --git a/cimportdialog.h b/cimportdialog.h index 5694028..604b43a 100644 --- a/cimportdialog.h +++ b/cimportdialog.h @@ -12,7 +12,7 @@ #include #include -#include "cthumbnailfilterproxymodel.h" +#include "cthumbnailsortfilterproxymodel.h" namespace Ui { @@ -93,7 +93,7 @@ private: QString m_szRootPath; /*!< TODO: describe */ QStandardItemModel* m_lpFolderViewModel; /*!< TODO: describe */ QStandardItemModel* m_lpThumbnailViewModel; /*!< TODO: describe */ - cThumbnailFilterProxyModel* m_lpThumbnailFilterProxyModel; /*!< TODO: describe */ + cThumbnailSortFilterProxyModel* m_lpThumbnailFilterProxyModel; /*!< TODO: describe */ bool m_bLoading; /*!< TODO: describe */ bool m_bHasImported; /*!< TODO: describe */ QStandardItem* m_lpRootItem; /*!< TODO: describe */ diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 6e09193..5871b6c 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -30,8 +30,9 @@ cMainWindow::cMainWindow(cSplashScreen* lpSplashScreen, QWidget *parent) : ui(new Ui::cMainWindow), m_lpProgressBar(nullptr), m_lpFolderViewModel(nullptr), + m_lpFolderSortFilterProxyModel(nullptr), m_lpThumbnailViewModel(nullptr), - m_lpThumbnailFilterProxyModel(nullptr), + m_lpThumbnailSortFilterProxyModel(nullptr), m_lpRootItem(nullptr), m_bLoading(false), m_lpSplashScreen(lpSplashScreen), @@ -50,7 +51,8 @@ cMainWindow::~cMainWindow() { delete m_lpThumbnailViewModel; delete m_lpFolderViewModel; - delete m_lpThumbnailFilterProxyModel; + delete m_lpThumbnailSortFilterProxyModel; + delete m_lpFolderSortFilterProxyModel; delete ui; } @@ -60,12 +62,14 @@ void cMainWindow::initUI() ui->setupUi(this); m_lpFolderViewModel = new QStandardItemModel; - ui->m_lpFolderView->setModel(m_lpFolderViewModel); + m_lpFolderSortFilterProxyModel = new cFolderSortFilterProxyModel(this); + ui->m_lpFolderView->setModel(m_lpFolderSortFilterProxyModel); + m_lpFolderSortFilterProxyModel->setSourceModel(m_lpFolderViewModel); m_lpThumbnailViewModel = new QStandardItemModel; - m_lpThumbnailFilterProxyModel = new cThumbnailFilterProxyModel(this); - ui->m_lpThumbnailView->setModel(m_lpThumbnailFilterProxyModel); - m_lpThumbnailFilterProxyModel->setSourceModel(m_lpThumbnailViewModel); + m_lpThumbnailSortFilterProxyModel = new cThumbnailSortFilterProxyModel(this); + ui->m_lpThumbnailView->setModel(m_lpThumbnailSortFilterProxyModel); + m_lpThumbnailSortFilterProxyModel->setSourceModel(m_lpThumbnailViewModel); m_lpProgressBar = new QProgressBar(this); m_lpProgressBar->setVisible(false); @@ -232,7 +236,7 @@ void cMainWindow::onThumbnailSelected(const QItemSelection& /*selection*/, const if(!index.isValid()) return; - cPicture* lpPicture = m_lpThumbnailFilterProxyModel->data(index, Qt::UserRole+1).value(); + cPicture* lpPicture = m_lpThumbnailSortFilterProxyModel->data(index, Qt::UserRole+1).value(); lpToolBoxInfo->setPicture(lpPicture); } } @@ -242,11 +246,11 @@ void cMainWindow::onFolderSelected(const QItemSelection& /*selection*/, const QI if(m_bLoading) return; - QStandardItem* lpItem = m_lpFolderViewModel->itemFromIndex(ui->m_lpFolderView->currentIndex()); - if(!lpItem) + QModelIndex index = ui->m_lpFolderView->selectionModel()->selectedIndexes()[0]; + if(!index.isValid()) return; - m_lpThumbnailFilterProxyModel->setFilterPath(lpItem->data(Qt::UserRole+2).toString()); + m_lpThumbnailSortFilterProxyModel->setFilterPath(m_lpFolderSortFilterProxyModel->data(index, Qt::UserRole+2).toString()); } void cMainWindow::setCurrentFile(const QString& fileName) @@ -439,7 +443,7 @@ void cMainWindow::onChangeDate() if(!index.isValid()) return; - cPicture* lpPicture = m_lpThumbnailFilterProxyModel->data(index, Qt::UserRole+1).value(); + cPicture* lpPicture = m_lpThumbnailSortFilterProxyModel->data(index, Qt::UserRole+1).value(); if(lpPicture->dateTime().isValid()) { @@ -453,7 +457,13 @@ void cMainWindow::onChangeDate() return; lpPicture->setDateTime(dateTimePicker.dateTime()); + QString szPath = QString::number(lpPicture->dateTime().date().year()) + "/" + lpPicture->dateTime().date().toString("yyyy-MM-dd"); + lpPicture->setFilePath(szPath); + m_lpThumbnailSortFilterProxyModel->setData(index, QVariant::fromValue(szPath), Qt::UserRole+2); - m_lpThumbnailFilterProxyModel->submit(); + insertPath(szPath, m_lpRootItem); + m_lpFolderSortFilterProxyModel->sort(0); + + m_lpThumbnailSortFilterProxyModel->invalidate(); } } diff --git a/cmainwindow.h b/cmainwindow.h index 5479180..c4c31ef 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -11,7 +11,8 @@ #include "csplashscreen.h" #include "cpicture.h" -#include "cthumbnailfilterproxymodel.h" +#include "cfoldersortfilterproxymodel.h" +#include "cthumbnailsortfilterproxymodel.h" #include "ctoolboxinfo.h" @@ -59,43 +60,44 @@ public: ~cMainWindow(); private: - Ui::cMainWindow* ui; /*!< TODO: describe */ - QProgressBar* m_lpProgressBar; /*!< TODO: describe */ - QStandardItemModel* m_lpFolderViewModel; /*!< TODO: describe */ - QStandardItemModel* m_lpThumbnailViewModel; /*!< TODO: describe */ - cThumbnailFilterProxyModel* m_lpThumbnailFilterProxyModel; /*!< TODO: describe */ - QStandardItem* m_lpRootItem; /*!< TODO: describe */ + Ui::cMainWindow* ui; /*!< TODO: describe */ + QProgressBar* m_lpProgressBar; /*!< TODO: describe */ + QStandardItemModel* m_lpFolderViewModel; /*!< TODO: describe */ + cFolderSortFilterProxyModel* m_lpFolderSortFilterProxyModel; /*!< TODO: describe */ + QStandardItemModel* m_lpThumbnailViewModel; /*!< TODO: describe */ + cThumbnailSortFilterProxyModel* m_lpThumbnailSortFilterProxyModel; /*!< TODO: describe */ + QStandardItem* m_lpRootItem; /*!< TODO: describe */ - bool m_bLoading; /*!< TODO: describe */ + bool m_bLoading; /*!< TODO: describe */ - cSplashScreen* m_lpSplashScreen; /*!< TODO: describe */ - cPictureLibrary m_pictureLibrary; /*!< TODO: describe */ - cPictureList m_pictureList; /*!< TODO: describe */ + cSplashScreen* m_lpSplashScreen; /*!< TODO: describe */ + cPictureLibrary m_pictureLibrary; /*!< TODO: describe */ + cPictureList m_pictureList; /*!< TODO: describe */ - QMenu* m_lpFileMenu; /*!< TODO: describe */ + QMenu* m_lpFileMenu; /*!< TODO: describe */ - QToolBar* m_lpFileToolBar; /*!< TODO: describe */ + QToolBar* m_lpFileToolBar; /*!< TODO: describe */ - QAction* m_lpFileNewAction; /*!< TODO: describe */ - QAction* m_lpFileOpenAction; /*!< TODO: describe */ - QAction* m_lpFileImportAction; /*!< TODO: describe */ - QAction* m_lpFileQuitAction; /*!< TODO: describe */ + QAction* m_lpFileNewAction; /*!< TODO: describe */ + QAction* m_lpFileOpenAction; /*!< TODO: describe */ + QAction* m_lpFileImportAction; /*!< TODO: describe */ + QAction* m_lpFileQuitAction; /*!< TODO: describe */ - QAction* m_lpSeparatorRecent; /*!< TODO: describe */ - enum { MaxRecentFiles = 5 }; /*!< TODO: describe */ - QAction* m_lpRecentFileAction[MaxRecentFiles]; /*!< TODO: describe */ + QAction* m_lpSeparatorRecent; /*!< TODO: describe */ + enum { MaxRecentFiles = 5 }; /*!< TODO: describe */ + QAction* m_lpRecentFileAction[MaxRecentFiles]; /*!< TODO: describe */ - QAction* m_lpChangeDateAction; /*!< TODO: describe */ + QAction* m_lpChangeDateAction; /*!< TODO: describe */ /*! \brief \fn initUI */ - void initUI(); - void createActions(); /*!< TODO: describe */ - void createFileActions(); - void createContextActions(); + void initUI(); + void createActions(); /*!< TODO: describe */ + void createFileActions(); + void createContextActions(); /*! \brief @@ -103,19 +105,19 @@ private: \fn setCurrentFile \param szFileName */ - void setCurrentFile(const QString& szFileName); + void setCurrentFile(const QString& szFileName); /*! \brief \fn updateRecentFileActions */ - void updateRecentFileActions(); + void updateRecentFileActions(); /*! \brief \fn openRecentFile */ - void openRecentFile(); + void openRecentFile(); /*! \brief @@ -123,7 +125,7 @@ private: \fn loadData \param bProgressBar */ - void loadData(bool bProgressBar = false); + void loadData(bool bProgressBar = false); protected: /*! @@ -132,7 +134,7 @@ protected: \fn closeEvent \param event */ - void closeEvent(QCloseEvent* event); + void closeEvent(QCloseEvent* event); private slots: /*! @@ -142,7 +144,7 @@ private slots: \param selection \param previous */ - void onThumbnailSelected(const QItemSelection& selection, const QItemSelection& previous); + void onThumbnailSelected(const QItemSelection& selection, const QItemSelection& previous); /*! \brief @@ -150,25 +152,25 @@ private slots: \param selection \param previous */ - void onFolderSelected(const QItemSelection& selection, const QItemSelection& previous); + void onFolderSelected(const QItemSelection& selection, const QItemSelection& previous); /*! \brief \fn onFileNew */ - void onFileNew(); + void onFileNew(); /*! \brief \fn onFileOpen */ - void onFileOpen(); + void onFileOpen(); /*! \brief \fn onFileImport */ - void onFileImport(); + void onFileImport(); /*! \brief @@ -176,14 +178,14 @@ private slots: \fn onThumbnailViewContextMenu \param pos */ - void onThumbnailViewContextMenu(const QPoint& pos); + void onThumbnailViewContextMenu(const QPoint& pos); /*! \brief \fn onChangeDate */ - void onChangeDate(); + void onChangeDate(); }; #endif // CMAINWINDOW_H diff --git a/common.cpp b/common.cpp index 7897de9..a130283 100644 --- a/common.cpp +++ b/common.cpp @@ -38,16 +38,16 @@ QByteArray image2Blob(const QImage &image) return(ba); } -void insertPath(QString szPath, QStandardItem* lpRootItem) +QStandardItem* insertPath(QString szPath, QStandardItem* lpRootItem) { if(!lpRootItem) - return; + return(0); - QString szPath1 = szPath.replace("\\", "/"); - QStringList szPathList = szPath1.split("/"); - QStandardItem* lpCurRoot = lpRootItem; - int path; - bool bFound; + QString szPath1 = szPath.replace("\\", "/"); + QStringList szPathList = szPath1.split("/"); + QStandardItem* lpCurRoot = lpRootItem; + int path; + bool bFound; szPath1 = ""; @@ -79,6 +79,9 @@ void insertPath(QString szPath, QStandardItem* lpRootItem) lpCurRoot->appendRow(lpNewItem); lpNewItem->setData(QVariant::fromValue(szPath1), Qt::UserRole+2); szPath1.append(QDir::separator()); + lpCurRoot = lpNewItem; } + + return(lpCurRoot); } diff --git a/common.h b/common.h index 29bbba1..8ba20eb 100644 --- a/common.h +++ b/common.h @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -49,8 +50,9 @@ QByteArray image2Blob(const QImage& image); \fn insertPath \param szPath \param lpRootItem + \return QStandardItem */ -void insertPath(QString szPath, QStandardItem* lpRootItem); +QStandardItem* insertPath(QString szPath, QStandardItem* lpRootItem); #endif // COMMON_H diff --git a/cthumbnailfilterproxymodel.cpp b/cthumbnailsortfilterproxymodel.cpp similarity index 53% rename from cthumbnailfilterproxymodel.cpp rename to cthumbnailsortfilterproxymodel.cpp index 7215b60..0a3b512 100644 --- a/cthumbnailfilterproxymodel.cpp +++ b/cthumbnailsortfilterproxymodel.cpp @@ -3,15 +3,15 @@ */ -#include "cthumbnailfilterproxymodel.h" +#include "cthumbnailsortfilterproxymodel.h" -cThumbnailFilterProxyModel::cThumbnailFilterProxyModel(QObject* parent) : +cThumbnailSortFilterProxyModel::cThumbnailSortFilterProxyModel(QObject* parent) : QSortFilterProxyModel (parent) { } -void cThumbnailFilterProxyModel::setFilterPath(const QString &szPath) +void cThumbnailSortFilterProxyModel::setFilterPath(const QString &szPath) { m_szPath = szPath; m_szPath.replace("\\", "/"); @@ -19,12 +19,12 @@ void cThumbnailFilterProxyModel::setFilterPath(const QString &szPath) invalidateFilter(); } -QString cThumbnailFilterProxyModel::filterPath() +QString cThumbnailSortFilterProxyModel::filterPath() { return(m_szPath); } -bool cThumbnailFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +bool cThumbnailSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent); QString tmp = sourceModel()->data(index0, Qt::UserRole+2).toString(); diff --git a/cthumbnailfilterproxymodel.h b/cthumbnailsortfilterproxymodel.h similarity index 85% rename from cthumbnailfilterproxymodel.h rename to cthumbnailsortfilterproxymodel.h index d7cd0e0..58e7416 100644 --- a/cthumbnailfilterproxymodel.h +++ b/cthumbnailsortfilterproxymodel.h @@ -15,12 +15,12 @@ \class cThumbnailFilterProxyModel cthumbnailfilterproxymodel.h "cthumbnailfilterproxymodel.h" */ -class cThumbnailFilterProxyModel : public QSortFilterProxyModel +class cThumbnailSortFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT public: - cThumbnailFilterProxyModel(QObject* parent = nullptr); + cThumbnailSortFilterProxyModel(QObject* parent = nullptr); /*! \brief diff --git a/pictureLibrary.pro b/pictureLibrary.pro index 139cbc2..4606c57 100644 --- a/pictureLibrary.pro +++ b/pictureLibrary.pro @@ -64,7 +64,8 @@ SOURCES += \ ctoolboxinfo.cpp \ cimportdialog.cpp \ cdatetimepicker.cpp \ - cthumbnailfilterproxymodel.cpp + cthumbnailsortfilterproxymodel.cpp \ + cfoldersortfilterproxymodel.cpp HEADERS += \ cmainwindow.h \ @@ -77,7 +78,8 @@ HEADERS += \ ctoolboxinfo.h \ cimportdialog.h \ cdatetimepicker.h \ - cthumbnailfilterproxymodel.h + cthumbnailsortfilterproxymodel.h \ + cfoldersortfilterproxymodel.h FORMS += \ cmainwindow.ui \