author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
** All rights reserved. |
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "settingstree.h" |
|
45 |
#include "variantdelegate.h" |
|
46 |
||
47 |
SettingsTree::SettingsTree(QWidget *parent) |
|
48 |
: QTreeWidget(parent) |
|
49 |
{ |
|
50 |
setItemDelegate(new VariantDelegate(this)); |
|
51 |
||
52 |
QStringList labels; |
|
53 |
labels << tr("Setting") << tr("Type") << tr("Value"); |
|
54 |
setHeaderLabels(labels); |
|
55 |
header()->setResizeMode(0, QHeaderView::Stretch); |
|
56 |
header()->setResizeMode(2, QHeaderView::Stretch); |
|
57 |
||
58 |
settings = 0; |
|
59 |
refreshTimer.setInterval(2000); |
|
60 |
autoRefresh = false; |
|
61 |
||
62 |
groupIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon), |
|
63 |
QIcon::Normal, QIcon::Off); |
|
64 |
groupIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon), |
|
65 |
QIcon::Normal, QIcon::On); |
|
66 |
keyIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon)); |
|
67 |
||
68 |
connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(maybeRefresh())); |
|
69 |
} |
|
70 |
||
71 |
void SettingsTree::setSettingsObject(QSettings *settings) |
|
72 |
{ |
|
73 |
delete this->settings; |
|
74 |
this->settings = settings; |
|
75 |
clear(); |
|
76 |
||
77 |
if (settings) { |
|
78 |
settings->setParent(this); |
|
79 |
refresh(); |
|
80 |
if (autoRefresh) |
|
81 |
refreshTimer.start(); |
|
82 |
} else { |
|
83 |
refreshTimer.stop(); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
QSize SettingsTree::sizeHint() const |
|
88 |
{ |
|
89 |
return QSize(800, 600); |
|
90 |
} |
|
91 |
||
92 |
void SettingsTree::setAutoRefresh(bool autoRefresh) |
|
93 |
{ |
|
94 |
this->autoRefresh = autoRefresh; |
|
95 |
if (settings) { |
|
96 |
if (autoRefresh) { |
|
97 |
maybeRefresh(); |
|
98 |
refreshTimer.start(); |
|
99 |
} else { |
|
100 |
refreshTimer.stop(); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
void SettingsTree::setFallbacksEnabled(bool enabled) |
|
106 |
{ |
|
107 |
if (settings) { |
|
108 |
settings->setFallbacksEnabled(enabled); |
|
109 |
refresh(); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
void SettingsTree::maybeRefresh() |
|
114 |
{ |
|
115 |
if (state() != EditingState) |
|
116 |
refresh(); |
|
117 |
} |
|
118 |
||
119 |
void SettingsTree::refresh() |
|
120 |
{ |
|
121 |
if (!settings) |
|
122 |
return; |
|
123 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
124 |
disconnect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
125 |
this, SLOT(updateSetting(QTreeWidgetItem*))); |
0 | 126 |
|
127 |
settings->sync(); |
|
128 |
updateChildItems(0); |
|
129 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
this, SLOT(updateSetting(QTreeWidgetItem*))); |
0 | 132 |
} |
133 |
||
134 |
bool SettingsTree::event(QEvent *event) |
|
135 |
{ |
|
136 |
if (event->type() == QEvent::WindowActivate) { |
|
137 |
if (isActiveWindow() && autoRefresh) |
|
138 |
maybeRefresh(); |
|
139 |
} |
|
140 |
return QTreeWidget::event(event); |
|
141 |
} |
|
142 |
||
143 |
void SettingsTree::updateSetting(QTreeWidgetItem *item) |
|
144 |
{ |
|
145 |
QString key = item->text(0); |
|
146 |
QTreeWidgetItem *ancestor = item->parent(); |
|
147 |
while (ancestor) { |
|
148 |
key.prepend(ancestor->text(0) + "/"); |
|
149 |
ancestor = ancestor->parent(); |
|
150 |
} |
|
151 |
||
152 |
settings->setValue(key, item->data(2, Qt::UserRole)); |
|
153 |
if (autoRefresh) |
|
154 |
refresh(); |
|
155 |
} |
|
156 |
||
157 |
void SettingsTree::updateChildItems(QTreeWidgetItem *parent) |
|
158 |
{ |
|
159 |
int dividerIndex = 0; |
|
160 |
||
161 |
foreach (QString group, settings->childGroups()) { |
|
162 |
QTreeWidgetItem *child; |
|
163 |
int childIndex = findChild(parent, group, dividerIndex); |
|
164 |
if (childIndex != -1) { |
|
165 |
child = childAt(parent, childIndex); |
|
166 |
child->setText(1, ""); |
|
167 |
child->setText(2, ""); |
|
168 |
child->setData(2, Qt::UserRole, QVariant()); |
|
169 |
moveItemForward(parent, childIndex, dividerIndex); |
|
170 |
} else { |
|
171 |
child = createItem(group, parent, dividerIndex); |
|
172 |
} |
|
173 |
child->setIcon(0, groupIcon); |
|
174 |
++dividerIndex; |
|
175 |
||
176 |
settings->beginGroup(group); |
|
177 |
updateChildItems(child); |
|
178 |
settings->endGroup(); |
|
179 |
} |
|
180 |
||
181 |
foreach (QString key, settings->childKeys()) { |
|
182 |
QTreeWidgetItem *child; |
|
183 |
int childIndex = findChild(parent, key, 0); |
|
184 |
||
185 |
if (childIndex == -1 || childIndex >= dividerIndex) { |
|
186 |
if (childIndex != -1) { |
|
187 |
child = childAt(parent, childIndex); |
|
188 |
for (int i = 0; i < child->childCount(); ++i) |
|
189 |
delete childAt(child, i); |
|
190 |
moveItemForward(parent, childIndex, dividerIndex); |
|
191 |
} else { |
|
192 |
child = createItem(key, parent, dividerIndex); |
|
193 |
} |
|
194 |
child->setIcon(0, keyIcon); |
|
195 |
++dividerIndex; |
|
196 |
} else { |
|
197 |
child = childAt(parent, childIndex); |
|
198 |
} |
|
199 |
||
200 |
QVariant value = settings->value(key); |
|
201 |
if (value.type() == QVariant::Invalid) { |
|
202 |
child->setText(1, "Invalid"); |
|
203 |
} else { |
|
204 |
child->setText(1, value.typeName()); |
|
205 |
} |
|
206 |
child->setText(2, VariantDelegate::displayText(value)); |
|
207 |
child->setData(2, Qt::UserRole, value); |
|
208 |
} |
|
209 |
||
210 |
while (dividerIndex < childCount(parent)) |
|
211 |
delete childAt(parent, dividerIndex); |
|
212 |
} |
|
213 |
||
214 |
QTreeWidgetItem *SettingsTree::createItem(const QString &text, |
|
215 |
QTreeWidgetItem *parent, int index) |
|
216 |
{ |
|
217 |
QTreeWidgetItem *after = 0; |
|
218 |
if (index != 0) |
|
219 |
after = childAt(parent, index - 1); |
|
220 |
||
221 |
QTreeWidgetItem *item; |
|
222 |
if (parent) |
|
223 |
item = new QTreeWidgetItem(parent, after); |
|
224 |
else |
|
225 |
item = new QTreeWidgetItem(this, after); |
|
226 |
||
227 |
item->setText(0, text); |
|
228 |
item->setFlags(item->flags() | Qt::ItemIsEditable); |
|
229 |
return item; |
|
230 |
} |
|
231 |
||
232 |
QTreeWidgetItem *SettingsTree::childAt(QTreeWidgetItem *parent, int index) |
|
233 |
{ |
|
234 |
if (parent) |
|
235 |
return parent->child(index); |
|
236 |
else |
|
237 |
return topLevelItem(index); |
|
238 |
} |
|
239 |
||
240 |
int SettingsTree::childCount(QTreeWidgetItem *parent) |
|
241 |
{ |
|
242 |
if (parent) |
|
243 |
return parent->childCount(); |
|
244 |
else |
|
245 |
return topLevelItemCount(); |
|
246 |
} |
|
247 |
||
248 |
int SettingsTree::findChild(QTreeWidgetItem *parent, const QString &text, |
|
249 |
int startIndex) |
|
250 |
{ |
|
251 |
for (int i = startIndex; i < childCount(parent); ++i) { |
|
252 |
if (childAt(parent, i)->text(0) == text) |
|
253 |
return i; |
|
254 |
} |
|
255 |
return -1; |
|
256 |
} |
|
257 |
||
258 |
void SettingsTree::moveItemForward(QTreeWidgetItem *parent, int oldIndex, |
|
259 |
int newIndex) |
|
260 |
{ |
|
261 |
for (int i = 0; i < oldIndex - newIndex; ++i) |
|
262 |
delete childAt(parent, newIndex); |
|
263 |
} |