QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgstreewidgetitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstreewidgetitem.cpp
3 ---------------------
4 begin : 06 Nov, 2005
5 copyright : (C) 2005 by Brendan Morley
6 email : morb at ozemail dot com dot au
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgstreewidgetitem.h"
19
20#include "qgis.h"
21#include "qgsvariantutils.h"
22
23#include "moc_qgstreewidgetitem.cpp"
24
25QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidget *parent, int type )
26 : QTreeWidgetItem( parent, type )
27{}
28
30 : QTreeWidgetItem( type )
31{}
32
33QgsTreeWidgetItem::QgsTreeWidgetItem( const QStringList &strings, int type )
34 : QTreeWidgetItem( strings, type )
35{}
36
37QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidget *view, const QStringList &strings, int type )
38 : QTreeWidgetItem( view, strings, type )
39{}
40
41QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidget *view, QTreeWidgetItem *after, int type )
42 : QTreeWidgetItem( view, after, type )
43{}
44
45QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidgetItem *parent, int type )
46 : QTreeWidgetItem( parent, type )
47{}
48
49QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidgetItem *parent, const QStringList &strings, int type )
50 : QTreeWidgetItem( parent, strings, type )
51{}
52
53QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidgetItem *parent, QTreeWidgetItem *after, int type )
54 : QTreeWidgetItem( parent, after, type )
55{}
56
57void QgsTreeWidgetItem::setSortData( int column, const QVariant &value )
58{
59 setData( column, CustomSortRole, value );
60}
61
62QVariant QgsTreeWidgetItem::sortData( int column ) const
63{
64 return data( column, CustomSortRole );
65}
66
68{
69 setData( 0, AlwaysOnTopPriorityRole, priority );
70}
71
73{
74 const QVariant val = data( 0, AlwaysOnTopPriorityRole );
75 return val.isValid() ? val.toInt() : -1;
76}
77
78bool QgsTreeWidgetItem::operator<( const QTreeWidgetItem &other ) const
79{
80 const int column = treeWidget()->sortColumn();
81
82 // check always on top priority - note - no way of determining sort order from tree widget, so
83 // these will sometimes be incorrectly placed at the bottom
84 const QVariant priority1 = data( 0, AlwaysOnTopPriorityRole );
85 const QVariant priority2 = other.data( 0, AlwaysOnTopPriorityRole );
86 if ( priority1.isValid() && priority2.isValid() )
87 {
88 const int i1 = priority1.toInt();
89 const int i2 = priority2.toInt();
90 if ( i1 != i2 )
91 return priority1.toInt() < priority2.toInt();
92 }
93 else if ( priority1.isValid() )
94 {
95 return true;
96 }
97 else if ( priority2.isValid() )
98 {
99 return false;
100 }
101
102 // no always on top priorities, check data
103 bool ok1, ok2, val;
104
105 // prefer the custom sort role, but fall back to display text
106 QVariant val1 = data( column, CustomSortRole );
107 if ( !val1.isValid() )
108 val1 = text( column );
109 QVariant val2 = other.data( column, CustomSortRole );
110 if ( !val2.isValid() )
111 val2 = other.text( column );
112
113 if ( !QgsVariantUtils::isNull( val1 ) && !QgsVariantUtils::isNull( val2 ) )
114 {
115 val = val1.toDouble( &ok1 ) < val2.toDouble( &ok2 );
116 if ( ok1 && ok2 )
117 {
118 return val;
119 }
120 else if ( ok1 || ok2 )
121 {
122 // sort numbers before strings
123 return ok1;
124 }
125 }
126
127 return qgsVariantLessThan( val1, val2 );
128}
129
130//
131// QgsTreeWidgetItemObject
132//
133
137
139 : QgsTreeWidgetItem( parent, type )
140{}
141
142// override setData to emit signal when edited. By default the itemChanged signal fires way too often
143void QgsTreeWidgetItemObject::setData( int column, int role, const QVariant &value )
144{
145 QgsTreeWidgetItem::setData( column, role, value );
146 if ( role == Qt::EditRole )
147 {
148 emit itemEdited( this, column );
149 }
150}
void itemEdited(QTreeWidgetItem *item, int column)
Emitted when the contents of the column in the specified item has been edited by the user.
QgsTreeWidgetItemObject(int type=Type)
Constructor for QgsTreeWidgetItemObject.
void setData(int column, int role, const QVariant &value) override
Sets the value for the item's column and role to the given value.
void setAlwaysOnTopPriority(int priority)
Sets a the item to display always on top of other items in the widget, regardless of the sort column ...
int alwaysOnTopPriority() const
Returns the item's priority when it is set to show always on top.
void setSortData(int column, const QVariant &value)
Sets the custom sort data for a specified column.
bool operator<(const QTreeWidgetItem &other) const override
Returns true if this item should appear before another item when sorting a list of items.
QVariant sortData(int column) const
Returns the custom sort data for a specified column.
QgsTreeWidgetItem(QTreeWidget *view, int type=Type)
Constructor for QgsTreeWidgetItem.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
Definition qgis.cpp:588