QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgstreewidgetitem.cpp"
20#include "qgsvariantutils.h"
21#include "qgis.h"
22
23QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidget *parent, int type )
24 : QTreeWidgetItem( parent, type )
25{}
26
28 : QTreeWidgetItem( type )
29{}
30
31QgsTreeWidgetItem::QgsTreeWidgetItem( const QStringList &strings, int type )
32 : QTreeWidgetItem( strings, type )
33{}
34
35QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidget *view, const QStringList &strings, int type )
36 : QTreeWidgetItem( view, strings, type )
37{}
38
39QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidget *view, QTreeWidgetItem *after, int type )
40 : QTreeWidgetItem( view, after, type )
41{}
42
43QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidgetItem *parent, int type )
44 : QTreeWidgetItem( parent, type )
45{}
46
47QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidgetItem *parent, const QStringList &strings, int type )
48 : QTreeWidgetItem( parent, strings, type )
49{}
50
51QgsTreeWidgetItem::QgsTreeWidgetItem( QTreeWidgetItem *parent, QTreeWidgetItem *after, int type )
52 : QTreeWidgetItem( parent, after, type )
53{}
54
55void QgsTreeWidgetItem::setSortData( int column, const QVariant &value )
56{
57 setData( column, CustomSortRole, value );
58}
59
60QVariant QgsTreeWidgetItem::sortData( int column ) const
61{
62 return data( column, CustomSortRole );
63}
64
66{
67 setData( 0, AlwaysOnTopPriorityRole, priority );
68}
69
71{
72 const QVariant val = data( 0, AlwaysOnTopPriorityRole );
73 return val.isValid() ? val.toInt() : -1;
74}
75
76bool QgsTreeWidgetItem::operator<( const QTreeWidgetItem &other ) const
77{
78 const int column = treeWidget()->sortColumn();
79
80 // check always on top priority - note - no way of determining sort order from tree widget, so
81 // these will sometimes be incorrectly placed at the bottom
82 const QVariant priority1 = data( 0, AlwaysOnTopPriorityRole );
83 const QVariant priority2 = other.data( 0, AlwaysOnTopPriorityRole );
84 if ( priority1.isValid() && priority2.isValid() )
85 {
86 const int i1 = priority1.toInt();
87 const int i2 = priority2.toInt();
88 if ( i1 != i2 )
89 return priority1.toInt() < priority2.toInt();
90 }
91 else if ( priority1.isValid() )
92 {
93 return true;
94 }
95 else if ( priority2.isValid() )
96 {
97 return false;
98 }
99
100 // no always on top priorities, check data
101 bool ok1, ok2, val;
102
103 // prefer the custom sort role, but fall back to display text
104 QVariant val1 = data( column, CustomSortRole );
105 if ( !val1.isValid() )
106 val1 = text( column );
107 QVariant val2 = other.data( column, CustomSortRole );
108 if ( !val2.isValid() )
109 val2 = other.text( column );
110
111 if ( !QgsVariantUtils::isNull( val1 ) && !QgsVariantUtils::isNull( val2 ) )
112 {
113 val = val1.toDouble( &ok1 ) < val2.toDouble( &ok2 );
114 if ( ok1 && ok2 )
115 {
116 return val;
117 }
118 else if ( ok1 || ok2 )
119 {
120 // sort numbers before strings
121 return ok1;
122 }
123 }
124
125 return qgsVariantLessThan( val1, val2 );
126}
127
128//
129// QgsTreeWidgetItemObject
130//
131
135
137 : QgsTreeWidgetItem( parent, type )
138{}
139
140// override setData to emit signal when edited. By default the itemChanged signal fires way too often
141void QgsTreeWidgetItemObject::setData( int column, int role, const QVariant &value )
142{
143 QgsTreeWidgetItem::setData( column, role, value );
144 if ( role == Qt::EditRole )
145 {
146 emit itemEdited( this, column );
147 }
148}
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.
QTreeWidgetItem subclass with custom handling for item sorting.
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:121