diff options
author | Carlos Bohórquez <carlos@kernelmap.com> | 2015-09-09 22:42:09 +0200 |
---|---|---|
committer | Carlos Bohórquez <carlos@kernelmap.com> | 2015-09-09 22:42:09 +0200 |
commit | b30859996093f7dac37b9a077aff78493305a270 (patch) | |
tree | 1d1349c2fe82ce7f155698722ba2d1525d4f834c /lumina-fm/widgets/DirWidget.h | |
parent | Update the dependencies/project to include the Qt5-Concurrent build module (n... (diff) | |
download | lumina-b30859996093f7dac37b9a077aff78493305a270.tar.gz lumina-b30859996093f7dac37b9a077aff78493305a270.tar.bz2 lumina-b30859996093f7dac37b9a077aff78493305a270.zip |
Solves issue 11233
A new class has been created for manage the files displayed in
QTreeWidget.
This class inherits from QTreeWidgetItem and only redefines the
operator< function. Now it's possible order by size and dates.
Diffstat (limited to 'lumina-fm/widgets/DirWidget.h')
-rw-r--r-- | lumina-fm/widgets/DirWidget.h | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/lumina-fm/widgets/DirWidget.h b/lumina-fm/widgets/DirWidget.h index 2c971fd8..514b3e7f 100644 --- a/lumina-fm/widgets/DirWidget.h +++ b/lumina-fm/widgets/DirWidget.h @@ -22,6 +22,10 @@ #define ZSNAPDIR QString("/.zfs/snapshot/") +#ifndef DEBUG +#define DEBUG 0 +#endif + namespace Ui{ class DirWidget; }; @@ -154,4 +158,64 @@ protected: void mouseReleaseEvent(QMouseEvent *); }; -#endif
\ No newline at end of file + +/* + * Virtual class for managing the sort of folders/files items. The problem with base class is that it only manages texts fields and + * we have dates and sizes. + * + * On this class, we overwrite the function operator<. + */ + +class CQTreeWidgetItem : public QTreeWidgetItem { +public: + CQTreeWidgetItem(int type = Type) : QTreeWidgetItem(type) {} + CQTreeWidgetItem(const QStringList & strings, int type = Type) : QTreeWidgetItem(strings, type) {} + CQTreeWidgetItem(QTreeWidget * parent, int type = Type) : QTreeWidgetItem(parent, type) {} + CQTreeWidgetItem(QTreeWidget * parent, const QStringList & strings, int type = Type) : QTreeWidgetItem(parent, strings, type) {} + CQTreeWidgetItem(QTreeWidget * parent, QTreeWidgetItem * preceding, int type = Type) : QTreeWidgetItem(parent, preceding, type) {} + CQTreeWidgetItem(QTreeWidgetItem * parent, int type = Type) : QTreeWidgetItem(parent, type) {} + CQTreeWidgetItem(QTreeWidgetItem * parent, const QStringList & strings, int type = Type) : QTreeWidgetItem(parent, strings, type) {} + CQTreeWidgetItem(QTreeWidgetItem * parent, QTreeWidgetItem * preceding, int type = Type) : QTreeWidgetItem(parent, preceding, type) {} + virtual ~CQTreeWidgetItem() {} + inline virtual bool operator<(const QTreeWidgetItem &tmp) const { + int column = this->treeWidget()->sortColumn(); + // We are in date text + if(column == DirWidget::DATEMOD || column == DirWidget::DATECREATE) { + // Get the stored text and try to convert to QDateTime + QString text = this->text(column); + QString text_tmp = tmp.text(column); + QDateTime date_time = QDateTime::fromString(text, Qt::DefaultLocaleShortDate); + QDateTime date_time_tmp = QDateTime::fromString(text_tmp, Qt::DefaultLocaleShortDate); + // If the conversion are ok in both objects, compare them + if(date_time.isValid() && date_time_tmp.isValid()) + return date_time < date_time_tmp; + // If some of the dates are invalid, use the base class implementation (order by string) + else { + if(DEBUG) + qDebug() << "Cannot convert the date. Texts arrived are " << text << " and " << text_tmp; + return QTreeWidgetItem::operator <(tmp); + } + } + // We are in size text + else if(column == DirWidget::SIZE) { + QString text = this->text(column); + QString text_tmp = tmp.text(column); + double filesize, filesize_tmp; + // On folders, text is empty so we check for that + // In case we are in folders, we put -1 for differentiate of regular files with 0 bytes. + // Doing so, all folders we'll be together instead of mixing with files with 0 bytes. + if(text.isEmpty()) + filesize = -1; + else + filesize = LUtils::DisplaySizeToBytes(text); + if(text_tmp.isEmpty()) + filesize_tmp = -1; + else + filesize_tmp = LUtils::DisplaySizeToBytes(text_tmp); + return filesize < filesize_tmp; + } + // In other cases, we trust base class implementation + return QTreeWidgetItem::operator<(tmp); + } +}; +#endif |