Bug 92249 - khexedit statistics columns not sorted correctly
Summary: khexedit statistics columns not sorted correctly
Status: RESOLVED FIXED
Alias: None
Product: khexedit
Classification: Applications
Component: general (show other bugs)
Version: 0.8.5
Platform: unspecified Linux
: NOR normal
Target Milestone: ---
Assignee: Espen Sand
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-28 11:40 UTC by p92
Modified: 2004-11-14 03:14 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments
sort order should be numeric on 'occurence' (56.27 KB, image/jpeg)
2004-11-11 11:47 UTC, p92
Details

Note You need to log in before you can comment on or make changes to this bug.
Description p92 2004-10-28 11:40:11 UTC
Version:           0.8.5 (using KDE 3.2.3, Mandrake Linux Cooker i586 - Cooker)
Compiler:          gcc version 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)
OS:                Linux (i686) release 2.6.9pc

Step to reproduce:
1. khexedit a text file
2. go to tools -> statistics
3. Sort on occurence descending (click the column header)

the sort is not correctly done (it should be a numeric sort not an ascii one)
Comment 1 p92 2004-11-11 11:44:37 UTC
still valid in 0.8.5 using KDE 3.3.1 CVS >= 20041110

'occurence' column is the fith one from left.

see screenshot
Comment 2 p92 2004-11-11 11:47:38 UTC
Created attachment 8247 [details]
sort order should be numeric on 'occurence'
Comment 3 Friedrich W. H. Kossebau 2004-11-14 03:14:26 UTC
CVS commit by kossebau: 

Backport from HEAD:
QListViewItems are sorted by comparing the text without leading whitespaces
(since when?). 
This fails for aligned numbers. Fixed q'n'd by reimplementing the comparison
for the items.

BUG:92249


  M +51 -31    fileinfodialog.cc   1.15.4.1


--- kdeutils/khexedit/fileinfodialog.cc  #1.15:1.15.4.1
@@ -30,4 +30,45 @@
 #include "listview.h"
 
+// quick'n'dirty hack to have the occurrence column sorted correctly
+class CStatisticListViewItem : public QListViewItem
+{
+  public:
+    CStatisticListViewItem( QListView * parent, QListViewItem * after,
+                            QString label1, QString label2, QString label3, QString label4,
+                            QString label5, QString label6, QString label7, int i, int o)
+  : QListViewItem( parent, after, label1, label2, label3, label4, label5, label6, label7),
+    item( i ),
+    occurrence( o )
+    {}
+
+    virtual int compare( QListViewItem *i, int col, bool ascending/*Qt doc says: ignore this one*/ ) const
+    {
+      // occurrence column (or the percent one)?
+      if( col == 5 || col == 6 )
+      {
+        const int otherOccurrence = ((CStatisticListViewItem*)i)->occurrence;
+        return occurrence < otherOccurrence ? -1 : occurrence == otherOccurrence ? 0 : 1;
+      }
+      // char column?
+      else if( col == 4 )
+      {
+        const int otherItem = ((CStatisticListViewItem*)i)->item;
+        return item < otherItem ? -1 : item == otherItem ? 0 : 1;
+      }
+      // default
+      else
+        return QListViewItem::compare(i,col,ascending);
+    }
+
+  protected:
+    // no of byte
+    int item;
+    // number of the byte's occurrences
+    int occurrence;
+};
+
+
+
+
 CFileInfoDialog::CFileInfoDialog( QWidget *parent,const char *name,bool modal)
   :KDialogBase( Plain, i18n("Statistics"), Help|User1|Cancel, User1,
@@ -172,19 +213,11 @@ void CFileInfoDialog::setStatistics( voi
     b.sprintf("%s", printBin(i) );
 
-    if( QChar((char)i).isPrint() == true )
-    {
-      c = QChar((char)i);
-    }
-    else
-    {
-      c = QChar('.');
-    }
+    const QChar _i((char)i);
+    c = _i.isPrint() ? _i : QChar('.'); 
 
-    item = new QListViewItem( mFrequencyList, item, h, d, o, b, c, u, u );
+    item = new CStatisticListViewItem( mFrequencyList, item, h, d, o, b, c, u, u, i, -1 );
     if( i == 0 )
-    {
       mFrequencyList->setSelected( item, true );
     }
-  }
 }
 
@@ -202,13 +235,10 @@ void CFileInfoDialog::setStatistics( SSt
 
   uint size, pre, i;
-
+  // find width of occurrences
   for( i=size=0; i<256; i++ )
-  {
-    if( sc.occurrence[i] > size ) { size = sc.occurrence[i]; }
-  }
+    if( sc.occurrence[i] > size ) 
+      size = sc.occurrence[i];
   for( pre = 1; size > 0 ; pre++ )
-  {
     size /= 10;
-  }
 
   for( i=0; i<256; i++ )
@@ -221,7 +251,5 @@ void CFileInfoDialog::setStatistics( SSt
     n = QString("%1").arg( sc.occurrence[i], pre );
     if( sc.documentSize == 0 )
-    {
-      p.sprintf("0.00" );
-    }
+      p = "0.00";
     else
     {
@@ -230,19 +258,11 @@ void CFileInfoDialog::setStatistics( SSt
     }
 
-    if( QChar((char)i).isPrint() == true )
-    {
-      c = QChar((char)i);
-    }
-    else
-    {
-      c = QChar('.');
-    }
+    const QChar _i((char)i);
+    c = _i.isPrint() ? _i : QChar('.'); 
 
-    item = new QListViewItem( mFrequencyList, item, h, d, o, b, c, n, p );
+    item = new CStatisticListViewItem( mFrequencyList, item, h, d, o, b, c, n, p, i, sc.occurrence[i] );
     if( i == 0 )
-    {
       mFrequencyList->setSelected( item, true );
     }
-  }
 }