From b1aa20b50c2fd2e3c96ceca9cd81e5e3dab2e6a4 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Sun, 27 Mar 2016 15:03:13 +0200 Subject: [PATCH] Add QgsTableWidgetItem for easier sorting of checkbox tables --- python/gui/gui.sip | 1 + python/gui/qgstablewidgetitem.sip | 37 ++++++++++++++++++++++ src/gui/CMakeLists.txt | 2 ++ src/gui/qgstablewidgetitem.cpp | 47 ++++++++++++++++++++++++++++ src/gui/qgstablewidgetitem.h | 52 +++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 python/gui/qgstablewidgetitem.sip create mode 100644 src/gui/qgstablewidgetitem.cpp create mode 100644 src/gui/qgstablewidgetitem.h diff --git a/python/gui/gui.sip b/python/gui/gui.sip index cd9ad21dbf9..fad5130ad6e 100644 --- a/python/gui/gui.sip +++ b/python/gui/gui.sip @@ -143,6 +143,7 @@ %Include qgsslider.sip %Include qgssublayersdialog.sip %Include qgssvgannotationitem.sip +%Include qgstablewidgetitem.sip %Include qgstextannotationitem.sip %Include qgsunitselectionwidget.sip %Include qgsuserinputdockwidget.sip diff --git a/python/gui/qgstablewidgetitem.sip b/python/gui/qgstablewidgetitem.sip new file mode 100644 index 00000000000..17880c0f50c --- /dev/null +++ b/python/gui/qgstablewidgetitem.sip @@ -0,0 +1,37 @@ +/*************************************************************************** + qgstablewidgetitem.sip - QgsTableWidgetItem + + --------------------- + begin : 27.3.2016 + copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch + email : matthias@opengis.ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +class QgsTableWidgetItem : QTableWidgetItem +{ +%TypeHeaderCode +#include "qgstablewidgetitem.h" +%End + + public: + QgsTableWidgetItem(); + QgsTableWidgetItem( const QString& text ); + + /** + * Set the role by which the items should be sorted. + * By default this will be set to Qt::DisplayRole + */ + void setSortRole( int role ); + /** + * Get the role by which the items should be sorted. + * By default this will be Qt::DisplayRole + */ + int sortRole() const; +}; diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 2a3f2836455..4c350ed8bdb 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -267,6 +267,7 @@ SET(QGIS_GUI_SRCS qgsslider.cpp qgssublayersdialog.cpp qgssvgannotationitem.cpp + qgstablewidgetitem.cpp qgstextannotationitem.cpp qgsunitselectionwidget.cpp qgsuserinputdockwidget.cpp @@ -575,6 +576,7 @@ SET(QGIS_GUI_HDRS qgsnumericsortlistviewitem.h qgsrubberband.h qgssvgannotationitem.h + qgstablewidgetitem.h qgstextannotationitem.h qgsuserinputdockwidget.h qgsvectorlayertools.h diff --git a/src/gui/qgstablewidgetitem.cpp b/src/gui/qgstablewidgetitem.cpp new file mode 100644 index 00000000000..d0ef9c30b44 --- /dev/null +++ b/src/gui/qgstablewidgetitem.cpp @@ -0,0 +1,47 @@ +/*************************************************************************** + qgstablewidgetitem.cpp - QgsTableWidgetItem + + --------------------- + begin : 27.3.2016 + copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch + email : matthias@opengis.ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +#include "qgstablewidgetitem.h" + +QgsTableWidgetItem::QgsTableWidgetItem() + : QTableWidgetItem() + , mSortRole( Qt::DisplayRole ) +{ +} + +QgsTableWidgetItem::QgsTableWidgetItem( const QString& text ) + : QTableWidgetItem( text ) + , mSortRole( Qt::DisplayRole ) +{ +} + +void QgsTableWidgetItem::setSortRole( int role ) +{ + mSortRole = role; +} + +int QgsTableWidgetItem::sortRole() const +{ + return mSortRole; +} + +bool QgsTableWidgetItem::operator<( const QTableWidgetItem& other ) const +{ +#if QT_VERSION < 0x050000 + return data( mSortRole ).toString() < other.data( mSortRole ).toString(); +#else + return data( mSortRole ) < other.data( mSortRole ); +#endif +} diff --git a/src/gui/qgstablewidgetitem.h b/src/gui/qgstablewidgetitem.h new file mode 100644 index 00000000000..3ddff914bb7 --- /dev/null +++ b/src/gui/qgstablewidgetitem.h @@ -0,0 +1,52 @@ +/*************************************************************************** + qgstablewidgetitem.h - QgsTableWidgetItem + + --------------------- + begin : 27.3.2016 + copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch + email : matthias@opengis.ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +#ifndef QGSTABLEWIDGETITEM_H +#define QGSTABLEWIDGETITEM_H + +#include + +/** + * This can be used like a regular QTableWidgetItem with the difference that a + * specific role can be set to sort. + */ +class GUI_EXPORT QgsTableWidgetItem : public QTableWidgetItem +{ + public: + QgsTableWidgetItem(); + /** + * Creates a new table widget item with the specified text. + */ + QgsTableWidgetItem( const QString& text ); + + + /** + * Set the role by which the items should be sorted. + * By default this will be set to Qt::DisplayRole + */ + void setSortRole( int role ); + /** + * Get the role by which the items should be sorted. + * By default this will be Qt::DisplayRole + */ + int sortRole() const; + + bool operator <( const QTableWidgetItem& other ) const override; + + private: + int mSortRole; +}; + +#endif // QGSTABLEWIDGETITEM_H