allow custom sorting of roomslist (by clicking on header sections)
authorsheepluva
Wed, 02 May 2012 10:47:21 +0200
changeset 6987 e34415c77342
parent 6986 409dd3851309
child 6988 5d78966a79d2
allow custom sorting of roomslist (by clicking on header sections)
QTfrontend/model/roomslistmodel.cpp
QTfrontend/ui/page/pageroomslist.cpp
QTfrontend/ui/page/pageroomslist.h
--- a/QTfrontend/model/roomslistmodel.cpp	Wed May 02 08:07:17 2012 +0100
+++ b/QTfrontend/model/roomslistmodel.cpp	Wed May 02 10:47:21 2012 +0200
@@ -92,11 +92,13 @@
 
     // not a role we have data for
     if (role != Qt::DisplayRole)
-        // only decorate name column
-        if ((role != Qt::DecorationRole) || (column != 1))
-            // only dye map column
-            if ((role != Qt::ForegroundRole) || (column != 5))
-                return QVariant();
+        // only custom-align counters
+        if ((role != Qt::TextAlignmentRole) || (column < 2) || (column > 3))
+            // only decorate name column
+            if ((role != Qt::DecorationRole) || (column != 1))
+                // only dye map column
+                if ((role != Qt::ForegroundRole) || (column != 5))
+                    return QVariant();
 
     // decorate room name based on room state
     if (role == Qt::DecorationRole)
@@ -146,6 +148,11 @@
             return QVariant();
     }
 
+    if (role == Qt::TextAlignmentRole)
+    {
+        return (int)(Qt::AlignHCenter | Qt::AlignVCenter);
+    }
+
     Q_ASSERT(false);
     return QVariant();
 }
--- a/QTfrontend/ui/page/pageroomslist.cpp	Wed May 02 08:07:17 2012 +0100
+++ b/QTfrontend/ui/page/pageroomslist.cpp	Wed May 02 10:47:21 2012 +0200
@@ -26,6 +26,8 @@
 #include <QHeaderView>
 #include <QTableView>
 
+#include <QSortFilterProxyModel>
+
 #include "ammoSchemeModel.h"
 #include "pageroomslist.h"
 #include "hwconsts.h"
@@ -474,17 +476,31 @@
 
 void PageRoomsList::setModel(QAbstractTableModel *model)
 {
-    roomsList->setModel(model);
+    roomsModel = new QSortFilterProxyModel(this);
+    roomsModel->setSourceModel(model);
+    roomsModel->setDynamicSortFilter(true);
+    roomsModel->setSortCaseSensitivity(Qt::CaseInsensitive);
+    roomsList->setModel(roomsModel);
 
     roomsList->hideColumn(0);
 
     QHeaderView * h = roomsList->horizontalHeader();
+
     h->setResizeMode(1, QHeaderView::Stretch);
-    h->resizeSection(2, 16);
-    h->resizeSection(3, 16);
+    h->resizeSection(2, 32);
+    h->resizeSection(3, 32);
     h->resizeSection(4, 100);
     h->resizeSection(5, 100);
     h->resizeSection(6, 100);
     h->resizeSection(7, 100);
 
+    connect(h, SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)),
+            this, SLOT(onSortIndicatorChanged(int, Qt::SortOrder)));
+
 }
+
+void PageRoomsList::onSortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
+{
+    roomsList->horizontalHeader()->setSortIndicatorShown(true);
+    roomsModel->sort(logicalIndex, order);
+}
--- a/QTfrontend/ui/page/pageroomslist.h	Wed May 02 08:07:17 2012 +0100
+++ b/QTfrontend/ui/page/pageroomslist.h	Wed May 02 10:47:21 2012 +0200
@@ -25,6 +25,8 @@
 class AmmoSchemeModel;
 class QTableView;
 
+class QSortFilterProxyModel;
+
 class PageRoomsList : public AbstractPage
 {
         Q_OBJECT
@@ -73,9 +75,11 @@
         void onRefreshClick();
         void onClearClick();
         void onJoinConfirmation(const QString &);
+        void onSortIndicatorChanged(int logicalIndex, Qt::SortOrder order);
 
     private:
         QSettings * m_gameSettings;
+        QSortFilterProxyModel * roomsModel;
 
         AmmoSchemeModel * ammoSchemeModel;