Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsTableWidgetItem for easier sorting of checkbox tables
  • Loading branch information
m-kuhn committed Mar 29, 2016
1 parent ae7b657 commit b1aa20b
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions 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;
};
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -267,6 +267,7 @@ SET(QGIS_GUI_SRCS
qgsslider.cpp
qgssublayersdialog.cpp
qgssvgannotationitem.cpp
qgstablewidgetitem.cpp
qgstextannotationitem.cpp
qgsunitselectionwidget.cpp
qgsuserinputdockwidget.cpp
Expand Down Expand Up @@ -575,6 +576,7 @@ SET(QGIS_GUI_HDRS
qgsnumericsortlistviewitem.h
qgsrubberband.h
qgssvgannotationitem.h
qgstablewidgetitem.h
qgstextannotationitem.h
qgsuserinputdockwidget.h
qgsvectorlayertools.h
Expand Down
47 changes: 47 additions & 0 deletions 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
}
52 changes: 52 additions & 0 deletions 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 <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.