Skip to content
Permalink
Browse files

Add QgsTableWidgetItem for easier sorting of checkbox tables

  • Loading branch information
m-kuhn committed Mar 29, 2016
1 parent ae7b657 commit b1aa20b50c2fd2e3c96ceca9cd81e5e3dab2e6a4
Showing with 139 additions and 0 deletions.
  1. +1 −0 python/gui/gui.sip
  2. +37 −0 python/gui/qgstablewidgetitem.sip
  3. +2 −0 src/gui/CMakeLists.txt
  4. +47 −0 src/gui/qgstablewidgetitem.cpp
  5. +52 −0 src/gui/qgstablewidgetitem.h
@@ -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
@@ -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;
};
@@ -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
@@ -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
}
@@ -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 <QTableWidget>

/**
* 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

0 comments on commit b1aa20b

Please sign in to comment.
You can’t perform that action at this time.