source: trunk/examples/tools/settingseditor/settingstree.cpp@ 315

Last change on this file since 315 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "settingstree.h"
45#include "variantdelegate.h"
46
47SettingsTree::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
71void 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
87QSize SettingsTree::sizeHint() const
88{
89 return QSize(800, 600);
90}
91
92void 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
105void SettingsTree::setFallbacksEnabled(bool enabled)
106{
107 if (settings) {
108 settings->setFallbacksEnabled(enabled);
109 refresh();
110 }
111}
112
113void SettingsTree::maybeRefresh()
114{
115 if (state() != EditingState)
116 refresh();
117}
118
119void SettingsTree::refresh()
120{
121 if (!settings)
122 return;
123
124 disconnect(this, SIGNAL(itemChanged(QTreeWidgetItem *, int)),
125 this, SLOT(updateSetting(QTreeWidgetItem *)));
126
127 settings->sync();
128 updateChildItems(0);
129
130 connect(this, SIGNAL(itemChanged(QTreeWidgetItem *, int)),
131 this, SLOT(updateSetting(QTreeWidgetItem *)));
132}
133
134bool 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
143void 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
157void 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
214QTreeWidgetItem *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
232QTreeWidgetItem *SettingsTree::childAt(QTreeWidgetItem *parent, int index)
233{
234 if (parent)
235 return parent->child(index);
236 else
237 return topLevelItem(index);
238}
239
240int SettingsTree::childCount(QTreeWidgetItem *parent)
241{
242 if (parent)
243 return parent->childCount();
244 else
245 return topLevelItemCount();
246}
247
248int 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
258void 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}
Note: See TracBrowser for help on using the repository browser.