From: Dirk H Bartley Date: Sun, 18 May 2008 13:04:07 +0000 (+0000) Subject: Riccardo's patch to correct sorting on numeric fields. X-Git-Tag: Release-7.0.0~4574 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=2a9a6b04a4a9fec56719ae681f9cdca7e3a47005;p=bacula%2Fbacula Riccardo's patch to correct sorting on numeric fields. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@6984 91ce42f0-d328-0410-95d8-f526ca767f89 --- diff --git a/bacula/src/qt-console/util/fmtwidgetitem.cpp b/bacula/src/qt-console/util/fmtwidgetitem.cpp index f9314edf92..bfca07fac4 100644 --- a/bacula/src/qt-console/util/fmtwidgetitem.cpp +++ b/bacula/src/qt-console/util/fmtwidgetitem.cpp @@ -156,26 +156,46 @@ ItemFormatterBase::~ItemFormatterBase() { } +void ItemFormatterBase::setTextFld(int index, const QString &fld, bool center) +{ + setText(index, fld.trimmed()); + if (center) { + setTextAlignment(index, Qt::AlignCenter); + } +} + +void ItemFormatterBase::setRightFld(int index, const QString &fld) +{ + setText(index, fld.trimmed()); + setTextAlignment(index, Qt::AlignRight | Qt::AlignVCenter); +} + void ItemFormatterBase::setBoolFld(int index, const QString &fld, bool center) { if (fld.trimmed().toInt()) - setTextFld(index, "Yes", center); + setTextFld(index, QObject::tr("Yes"), center); else - setTextFld(index, "No", center); + setTextFld(index, QObject::tr("No"), center); } void ItemFormatterBase::setBoolFld(int index, int fld, bool center) { if (fld) - setTextFld(index, "Yes", center); + setTextFld(index, QObject::tr("Yes"), center); else - setTextFld(index, "No", center); + setTextFld(index, QObject::tr("No"), center); } void ItemFormatterBase::setNumericFld(int index, const QString &fld) { - setTextFld(index, fld); - setTextAlignment(index, Qt::AlignRight | Qt::AlignVCenter); + setRightFld(index, fld.trimmed()); + setSortValue(index, fld.toDouble() ); +} + +void ItemFormatterBase::setNumericFld(int index, const QString &fld, const QVariant &sortval) +{ + setRightFld(index, fld.trimmed()); + setSortValue(index, sortval ); } void ItemFormatterBase::setBytesFld(int index, const QString &fld) @@ -193,7 +213,8 @@ void ItemFormatterBase::setBytesFld(int index, const QString &fld) msg = convertBytesSI(qfld); break; } - setNumericFld(index, msg); + + setNumericFld(index, msg, qfld); } void ItemFormatterBase::setDurationFld(int index, const QString &fld) @@ -234,36 +255,9 @@ void ItemFormatterBase::setDurationFld(int index, const QString &fld) } if (dfld) msg += QString(" %1s").arg(dfld); - -/* - double net = 0; - QList durations; - durations.append(1); - durations.append(60); - durations.append(HOUR); - durations.append(DAY); - durations.append(MONTH); - durations.append(YEAR); - QStringList abbrlist = (QStringList() << "s" << "min" << "h" << "d" << "m" << "y"); - bool done = false; - int count = 1; - while (done == false) { - if ((dfld < durations[count]) || (count >= abbrlist.count() - 1)) { - done = true; - net = (double)dfld / (double)(durations[count - 1]); - if (net != 0) { - msg = QString("%1%2") - .arg(net, 0, 'f', 2, QLatin1Char(' ')) - .arg(abbrlist[count - 1]); - } else { - msg = "0s"; - } - } - count += 1; - } -*/ } - - setNumericFld(index, msg); + } + + setNumericFld(index, msg, fld.trimmed().toLongLong()); } void ItemFormatterBase::setVolStatusFld(int index, const QString &fld, bool center) @@ -340,13 +334,10 @@ level(indent_level) { } -void TreeItemFormatter::setTextFld(int index, const QString &fld, bool center) +void TreeItemFormatter::setText(int index, const QString &fld) { wdg->setData(index, Qt::UserRole, level); - if (center) { - setTextAlignment(index, Qt::AlignCenter); - } - wdg->setText(index, fld.trimmed()); + wdg->setText(index, fld); } void TreeItemFormatter::setTextAlignment(int index, int align) @@ -359,6 +350,44 @@ void TreeItemFormatter::setBackground(int index, const QBrush &qb) wdg->setBackground(index, qb); } +/* at this time we don't sort trees, so this method does nothing */ +void TreeItemFormatter::setSortValue(int /* index */, const QVariant & /* value */) +{ +} + +/*********************************************** + * + * Specialized table widget used for sorting + * + ***********************************************/ +TableItemFormatter::BatSortingTableItem::BatSortingTableItem(): +QTableWidgetItem(1) +{ +} + +void TableItemFormatter::BatSortingTableItem::setSortData(const QVariant &d) +{ + setData(SORTDATA_ROLE, d); +} + +bool TableItemFormatter::BatSortingTableItem::operator< ( const QTableWidgetItem & o ) const +{ + QVariant my = data(SORTDATA_ROLE); + QVariant other = o.data(SORTDATA_ROLE); + if (!my.isValid() || !other.isValid() || my.type() != other.type()) + return QTableWidgetItem::operator< (o); /* invalid combination, revert to default sorting */ + + /* 64bit integers must be handled separately, others can be converted to double */ + if (QVariant::ULongLong == my.type()) { + return my.toULongLong() < other.toULongLong(); + } else if (QVariant::LongLong == my.type()) { + return my.toLongLong() < other.toLongLong(); + } else if (my.canConvert(QVariant::Double)) { + return my.toDouble() < other.toDouble(); + } else { + return QTableWidgetItem::operator< (o); /* invalid combination, revert to default sorting */ + } +} /*********************************************** * @@ -373,15 +402,11 @@ last(NULL) { } -void TableItemFormatter::setTextFld(int col, const QString &fld, bool center) +void TableItemFormatter::setText(int col, const QString &fld) { - last = new QTableWidgetItem(1); -/* last->setForeground(blackBrush); */ + last = new BatSortingTableItem; parent->setItem(row, col, last); - if (center) { - setTextAlignment(col, Qt::AlignCenter); - } - last->setText(fld.trimmed()); + last->setText(fld); } void TableItemFormatter::setTextAlignment(int /*index*/, int align) @@ -394,12 +419,18 @@ void TableItemFormatter::setBackground(int /*index*/, const QBrush &qb) last->setBackground(qb); } +void TableItemFormatter::setSortValue(int /* index */, const QVariant &value ) +{ + last->setSortData(value); +} + QTableWidgetItem *TableItemFormatter::widget(int col) { - return parent->item(row, col); + return parent->item(row, col); } const QTableWidgetItem *TableItemFormatter::widget(int col) const { - return parent->item(row, col); + return parent->item(row, col); } + diff --git a/bacula/src/qt-console/util/fmtwidgetitem.h b/bacula/src/qt-console/util/fmtwidgetitem.h index f0761888d6..324a8c64c5 100644 --- a/bacula/src/qt-console/util/fmtwidgetitem.h +++ b/bacula/src/qt-console/util/fmtwidgetitem.h @@ -46,11 +46,11 @@ class QBrush; class ItemFormatterBase { public: - enum BYTES_CONVERSION { - BYTES_CONVERSION_NONE, - BYTES_CONVERSION_IEC, - BYTES_CONVERSION_SI, - }; + enum BYTES_CONVERSION { + BYTES_CONVERSION_NONE, + BYTES_CONVERSION_IEC, + BYTES_CONVERSION_SI, + }; public: virtual ~ItemFormatterBase(); @@ -60,10 +60,14 @@ public: void setBoolFld(int index, int fld, bool center = true); /* Normal text field. Centers field if center true*/ - virtual void setTextFld(int index, const QString &fld, bool center = false) = 0; + void setTextFld(int index, const QString &fld, bool center = false); - /* Right-aligned text field.*/ + /* Right-aligned text field. */ + void setRightFld(int index, const QString &fld); + + /* Numeric field - sorted as numeric type */ void setNumericFld(int index, const QString &fld); + void setNumericFld(int index, const QString &fld, const QVariant &sortVal); /* fld value interpreted as bytes and formatted with size suffixes */ void setBytesFld(int index, const QString &fld); @@ -95,10 +99,15 @@ protected: /* only derived classes can create one of these */ ItemFormatterBase(); + virtual void setText(int index, const QString &fld) = 0; virtual void setTextAlignment(int index, int align) = 0; virtual void setBackground(int index, const QBrush &) = 0; + /* sets the *optional* value used for sorting */ + virtual void setSortValue(int index, const QVariant &value) = 0; + private: + /* bytes formatted as power-of-two with IEC suffixes (KiB, MiB, and so on) */ static QString convertBytesIEC(qint64 fld); @@ -120,15 +129,15 @@ public: TreeItemFormatter(QTreeWidgetItem &parent, int indent_level); - virtual void setTextFld(int index, const QString &fld, bool center = false); - /* access internal widget */ QTreeWidgetItem *widget() { return wdg; } const QTreeWidgetItem *widget() const { return wdg; } protected: + virtual void setText(int index, const QString &fld); virtual void setTextAlignment(int index, int align); virtual void setBackground(int index, const QBrush &); + virtual void setSortValue(int index, const QVariant &value); private: QTreeWidgetItem *wdg; @@ -142,24 +151,41 @@ private: */ class TableItemFormatter : public ItemFormatterBase { +private: + + /* specialized widget item - allows an optional data property for sorting */ + class BatSortingTableItem : public QTableWidgetItem + { + private: + static const int SORTDATA_ROLE = Qt::UserRole + 100; + public: + BatSortingTableItem(); + + /* uses the sort data if available, reverts to default behavior othervise */ + virtual bool operator< ( const QTableWidgetItem & o ) const; + + /* set the value used for sorting - MUST BE A NUMERIC TYPE */ + void setSortData(const QVariant &d); + }; + public: TableItemFormatter(QTableWidget &parent, int row); - virtual void setTextFld(int col, const QString &fld, bool center = false); - /* access internal widget at column col*/ QTableWidgetItem *widget(int col); const QTableWidgetItem *widget(int col) const; protected: + virtual void setText(int index, const QString &fld); virtual void setTextAlignment(int index, int align); virtual void setBackground(int index, const QBrush &); + virtual void setSortValue(int index, const QVariant &value); private: QTableWidget *parent; int row; - QTableWidgetItem *last; + BatSortingTableItem *last; }; #endif /* _FMTWIDGETITEM_H_ */