OSDN Git Service

fc8892ae22e9473c2e0e84bbbc6af72f4506a0ea
[qt-creator-jp/qt-creator-jp.git] / src / plugins / locator / settingspage.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "settingspage.h"
35 #include "locatorconstants.h"
36
37 #include "locatorplugin.h"
38 #include "ilocatorfilter.h"
39 #include "directoryfilter.h"
40
41 #include <qtconcurrent/QtConcurrentTools>
42 #include <utils/qtcassert.h>
43
44 #include <QtCore/QCoreApplication>
45
46 Q_DECLARE_METATYPE(Locator::ILocatorFilter*)
47
48 using namespace Locator;
49 using namespace Locator::Internal;
50
51 SettingsPage::SettingsPage(LocatorPlugin *plugin)
52     : m_plugin(plugin), m_page(0)
53 {
54 }
55
56 QString SettingsPage::id() const
57 {
58     return QLatin1String(Constants::FILTER_OPTIONS_PAGE);
59 }
60
61 QString SettingsPage::displayName() const
62 {
63     return QCoreApplication::translate("Locator", Locator::Constants::FILTER_OPTIONS_PAGE);
64 }
65
66 QString SettingsPage::category() const
67 {
68     return QLatin1String(Constants::LOCATOR_CATEGORY);
69 }
70
71 QString SettingsPage::displayCategory() const
72 {
73     return QCoreApplication::translate("Locator", Locator::Constants::LOCATOR_TR_CATEGORY);
74 }
75
76 QIcon SettingsPage::categoryIcon() const
77 {
78     return QIcon(QLatin1String(Locator::Constants::SETTINGS_CATEGORY_LOCATOR_ICON));
79 }
80
81 QWidget *SettingsPage::createPage(QWidget *parent)
82 {
83
84     m_page = new QWidget(parent);
85     m_ui.setupUi(m_page);
86     connect(m_ui.filterList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
87             this, SLOT(updateButtonStates()));
88     connect(m_ui.filterList, SIGNAL(itemActivated(QListWidgetItem *)),
89             this, SLOT(configureFilter(QListWidgetItem *)));
90     connect(m_ui.editButton, SIGNAL(clicked()),
91             this, SLOT(configureFilter()));
92     connect(m_ui.addButton, SIGNAL(clicked()),
93             this, SLOT(addCustomFilter()));
94     connect(m_ui.removeButton, SIGNAL(clicked()),
95             this, SLOT(removeCustomFilter()));
96
97     m_ui.refreshInterval->setValue(m_plugin->refreshInterval());
98     m_filters = m_plugin->filters();
99     m_customFilters = m_plugin->customFilters();
100     saveFilterStates();
101     updateFilterList();
102     if (m_searchKeywords.isEmpty()) {
103         m_searchKeywords = m_ui.refreshIntervalLabel->text();
104         m_searchKeywords.remove(QLatin1Char('&'));
105     }
106     return m_page;
107 }
108
109 void SettingsPage::apply()
110 {
111     // Delete removed filters and clear added filters
112     qDeleteAll(m_removedFilters);
113     m_removedFilters.clear();
114     m_addedFilters.clear();
115
116     // Pass the new configuration on to the plugin
117     m_plugin->setFilters(m_filters);
118     m_plugin->setCustomFilters(m_customFilters);
119     m_plugin->setRefreshInterval(m_ui.refreshInterval->value());
120     requestRefresh();
121     m_plugin->saveSettings();
122     saveFilterStates();
123 }
124
125 void SettingsPage::finish()
126 {
127     // If settings were applied, this shouldn't change anything. Otherwise it
128     // makes sure the filter states aren't changed permanently.
129     restoreFilterStates();
130
131     // Delete added filters and clear removed filters
132     qDeleteAll(m_addedFilters);
133     m_addedFilters.clear();
134     m_removedFilters.clear();
135
136     // Further cleanup
137     m_filters.clear();
138     m_customFilters.clear();
139     m_refreshFilters.clear();
140 }
141
142 void SettingsPage::requestRefresh()
143 {
144     if (!m_refreshFilters.isEmpty())
145         m_plugin->refresh(m_refreshFilters);
146 }
147
148 void SettingsPage::saveFilterStates()
149 {
150     m_filterStates.clear();
151     foreach (ILocatorFilter *filter, m_filters)
152         m_filterStates.insert(filter, filter->saveState());
153 }
154
155 void SettingsPage::restoreFilterStates()
156 {
157     foreach (ILocatorFilter *filter, m_filterStates.keys())
158         filter->restoreState(m_filterStates.value(filter));
159 }
160
161 void SettingsPage::updateFilterList()
162 {
163     m_ui.filterList->clear();
164     foreach (ILocatorFilter *filter, m_filters) {
165         if (filter->isHidden())
166             continue;
167
168         QString title;
169         if (filter->isIncludedByDefault())
170             title = filter->displayName();
171         else
172             title = tr("%1 (prefix: %2)").arg(filter->displayName()).arg(filter->shortcutString());
173         QListWidgetItem *item = new QListWidgetItem(title);
174         item->setData(Qt::UserRole, qVariantFromValue(filter));
175         m_ui.filterList->addItem(item);
176     }
177     if (m_ui.filterList->count() > 0)
178         m_ui.filterList->setCurrentRow(0);
179 }
180
181 void SettingsPage::updateButtonStates()
182 {
183     QListWidgetItem *item = m_ui.filterList->currentItem();
184     ILocatorFilter *filter = (item ? item->data(Qt::UserRole).value<ILocatorFilter *>() : 0);
185     m_ui.editButton->setEnabled(filter && filter->isConfigurable());
186     m_ui.removeButton->setEnabled(filter && m_customFilters.contains(filter));
187 }
188
189 void SettingsPage::configureFilter(QListWidgetItem *item)
190 {
191     if (!item)
192         item = m_ui.filterList->currentItem();
193     QTC_ASSERT(item, return);
194     ILocatorFilter *filter = item->data(Qt::UserRole).value<ILocatorFilter *>();
195     QTC_ASSERT(filter, return);
196
197     if (!filter->isConfigurable())
198         return;
199     bool needsRefresh = false;
200     filter->openConfigDialog(m_page, needsRefresh);
201     if (needsRefresh && !m_refreshFilters.contains(filter))
202         m_refreshFilters.append(filter);
203     updateFilterList();
204 }
205
206 void SettingsPage::addCustomFilter()
207 {
208     ILocatorFilter *filter = new DirectoryFilter;
209     bool needsRefresh = false;
210     if (filter->openConfigDialog(m_page, needsRefresh)) {
211         m_filters.append(filter);
212         m_addedFilters.append(filter);
213         m_customFilters.append(filter);
214         m_refreshFilters.append(filter);
215         updateFilterList();
216     }
217 }
218
219 void SettingsPage::removeCustomFilter()
220 {
221     QListWidgetItem *item = m_ui.filterList->currentItem();
222     QTC_ASSERT(item, return);
223     ILocatorFilter *filter = item->data(Qt::UserRole).value<ILocatorFilter *>();
224     QTC_ASSERT(m_customFilters.contains(filter), return);
225     m_filters.removeAll(filter);
226     m_customFilters.removeAll(filter);
227     m_refreshFilters.removeAll(filter);
228     if (m_addedFilters.contains(filter)) {
229         m_addedFilters.removeAll(filter);
230         delete filter;
231     } else {
232         m_removedFilters.append(filter);
233     }
234     updateFilterList();
235 }
236
237 bool SettingsPage::matches(const QString &s) const
238 {
239     return m_searchKeywords.contains(s, Qt::CaseInsensitive);
240 }