-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
autochecktreewidget.cpp
162 lines (135 loc) · 4.19 KB
/
autochecktreewidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright (c) 2007 Mathias Soeken <msoeken@tzi.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "autochecktreewidget.h"
using namespace IncidenceEditorNG;
//@cond PRIVATE
class AutoCheckTreeWidget::Private
{
public:
Private()
: mAutoCheckChildren( false ),
mAutoCheck( true ) {}
bool mAutoCheckChildren;
bool mAutoCheck;
};
//@endcond
AutoCheckTreeWidget::AutoCheckTreeWidget( QWidget *parent )
: QTreeWidget( parent ), d( new Private() )
{
connect( model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(slotRowsInserted(QModelIndex,int,int)) );
connect( model(),
SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this,
SLOT(slotDataChanged(QModelIndex,QModelIndex)) );
setColumnCount( 2 );
}
AutoCheckTreeWidget::~AutoCheckTreeWidget()
{
delete d;
}
QTreeWidgetItem *AutoCheckTreeWidget::itemByPath( const QStringList &path ) const
{
QStringList newPath = path;
QTreeWidgetItem *item = 0;
while ( newPath.count() ) {
item = findItem( item, newPath.takeFirst() );
if ( !item ) {
return 0;
}
}
return item;
}
QStringList AutoCheckTreeWidget::pathByItem( QTreeWidgetItem *item ) const
{
QStringList path;
QTreeWidgetItem *current = item;
while ( current ) {
path.prepend( current->text( 0 ) );
current = current->parent();
}
return path;
}
bool AutoCheckTreeWidget::autoCheckChildren() const
{
return d->mAutoCheckChildren;
}
void AutoCheckTreeWidget::setAutoCheckChildren( bool autoCheckChildren )
{
d->mAutoCheckChildren = autoCheckChildren;
}
bool AutoCheckTreeWidget::autoCheck() const
{
return d->mAutoCheck;
}
void AutoCheckTreeWidget::setAutoCheck( bool autoCheck )
{
d->mAutoCheck = autoCheck;
}
QTreeWidgetItem *AutoCheckTreeWidget::findItem( QTreeWidgetItem *parent, const QString &text ) const
{
if ( parent ) {
for ( int i = 0; i < parent->childCount(); ++i ) {
if ( parent->child( i )->text( 0 ) == text ) {
return parent->child( i );
}
}
} else {
for ( int i = 0; i < topLevelItemCount(); ++i ) {
if ( topLevelItem( i )->text( 0 ) == text ) {
return topLevelItem( i );
}
}
}
return 0;
}
void AutoCheckTreeWidget::slotRowsInserted( const QModelIndex &parent,
int start, int end )
{
if ( d->mAutoCheck ) {
QTreeWidgetItem *item = itemFromIndex( parent );
QTreeWidgetItem *child;
if ( item ) {
QBrush b( Qt::yellow );
item->setBackground( 0, b );
for ( int i = start; i < qMax( end, item->childCount() ); ++i ) {
child = item->child( i );
child->setFlags( child->flags() | Qt::ItemIsUserCheckable );
child->setCheckState( 0, Qt::Unchecked );
}
} else { /* top level item has been inserted */
for ( int i = start; i < qMax( end, topLevelItemCount() ); ++i ) {
child = topLevelItem( i );
child->setFlags( child->flags() | Qt::ItemIsUserCheckable );
child->setCheckState( 0, Qt::Unchecked );
}
}
}
}
void AutoCheckTreeWidget::slotDataChanged( const QModelIndex &topLeft,
const QModelIndex &bottomRight )
{
if ( !d->mAutoCheckChildren ) {
return;
}
QTreeWidgetItem *item1 = itemFromIndex( topLeft );
QTreeWidgetItem *item2 = itemFromIndex( bottomRight );
if ( item1 == item2 ) {
for ( int i = 0; i < item1->childCount(); ++i ) {
item1->child( i )->setCheckState( 0, item1->checkState( 0 ) );
}
}
}