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));