diff options
-rw-r--r-- | lumina-fm/widgets/DirWidget.cpp | 9 | ||||
-rw-r--r-- | lumina-fm/widgets/DirWidget.h | 66 |
2 files changed, 70 insertions, 5 deletions
diff --git a/lumina-fm/widgets/DirWidget.cpp b/lumina-fm/widgets/DirWidget.cpp index ed1f4544..22879c1b 100644 --- a/lumina-fm/widgets/DirWidget.cpp +++ b/lumina-fm/widgets/DirWidget.cpp @@ -111,7 +111,7 @@ void DirWidget::setShowThumbnails(bool show){ void DirWidget::setDetails(QList<DETAILTYPES> list){ listDetails = list; //Need to re-create the header item as well - QTreeWidgetItem *it = new QTreeWidgetItem(); + CQTreeWidgetItem *it = new CQTreeWidgetItem(); int nmcol = -1; int typecol = -1; for(int t=0; t<listDetails.length(); t++){ switch(listDetails[t]){ @@ -283,15 +283,16 @@ void DirWidget::LoadDir(QString dir, QList<LFileInfo> list){ watcher->addPath(list[i].absoluteFilePath()); if(showDetails){ //Now create all the individual items for the details tree - QTreeWidgetItem *it; + CQTreeWidgetItem *it; bool addnew = false; //See if an item already exists for this file QList<QTreeWidgetItem*> items = treeWidget->findItems(list[i].fileName(),Qt::MatchExactly,0); //NOTE: This requires column 0 to be the name if(items.isEmpty()){ - it = new QTreeWidgetItem(); + it = new CQTreeWidgetItem(); addnew = true; }else{ - it = items.first(); + // Safe downcasting because CQTreeWidgetItem only redefines the virtual function bool opearot<. Not new methos added. + it = static_cast<CQTreeWidgetItem *> (items.first()); } //Now update the entry contents it->setWhatsThis(0, QString(canmodify ? "cut": "copy")+"::::"+list[i].absoluteFilePath()); 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 |