Add QgsTableWidgetItem for easier sorting of checkbox tables

This commit is contained in:
Matthias Kuhn 2016-03-27 15:03:13 +02:00
parent ae7b657da3
commit b1aa20b50c
5 changed files with 139 additions and 0 deletions

View File

@ -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

View File

@ -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;
};

View File

@ -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

View File

@ -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
}

View File

@ -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