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