[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bacula-devel] patch: sorting on bat joblist


Ok, this new patch should fix the sorting.
Dirk, please discard the "nosort.patch" I've sent yesterday and apply this instead.

I apologize again for the inconvenience.
Ciao,
Riccardo


rghetta wrote:
Hello,
I made the most stupid mistake with the sorting patch.
QT sorts only alphabetically, so all numeric fields are messed up, including obviously the jobid. I suppose there is a way to have QT sort properly, but at this time I haven't found one, so perhaps the best solution is to disable column sorting.
Dirk, please apply the attached patch until a better solution comes up.

I'm terribly sorry for all the trouble.
Riccardo
Index: bacula/src/qt-console/util/fmtwidgetitem.h
===================================================================
--- bacula/src/qt-console/util/fmtwidgetitem.h	(revision 6983)
+++ bacula/src/qt-console/util/fmtwidgetitem.h	(working copy)
@@ -46,11 +46,11 @@
 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 @@
    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 @@
    /* 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 @@
 
    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 @@
  */
 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_ */
Index: bacula/src/qt-console/util/fmtwidgetitem.cpp
===================================================================
--- bacula/src/qt-console/util/fmtwidgetitem.cpp	(revision 6983)
+++ bacula/src/qt-console/util/fmtwidgetitem.cpp	(working copy)
@@ -156,28 +156,48 @@
 {
 }
 
+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)
 {
    qint64 qfld = fld.trimmed().toLongLong();
@@ -193,7 +213,8 @@
       msg = convertBytesSI(qfld);
       break;
    }
-   setNumericFld(index, msg);
+
+   setNumericFld(index, msg, qfld);
 }
 
 void ItemFormatterBase::setDurationFld(int index, const QString &fld)
@@ -234,36 +255,9 @@
       }
       if (dfld)
  	 msg += QString(" %1s").arg(dfld);
-           
-/*
-     double net = 0;
-     QList<qlonglong> 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 @@
 {
 }
 
-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,9 +350,47 @@
    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 */
+   }
+}
+
+/***********************************************
+ *
  * tableitem formatting routines
  *
  ***********************************************/
@@ -373,15 +402,11 @@
 {
 }
 
-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 @@
    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);
 }
+
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bacula-devel mailing list
Bacula-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/bacula-devel


This mailing list archive is a service of Copilotco.