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