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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "highlightersettingspage.h"
#include "highlightersettings.h"
#include "manager.h"
#include "managedefinitionsdialog.h"
#include "ui_highlightersettingspage.h"
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <QtGui/QMessageBox>
using namespace TextEditor;
using namespace Internal;
struct HighlighterSettingsPage::HighlighterSettingsPagePrivate
{
explicit HighlighterSettingsPagePrivate(const QString &id);
const QString m_id;
const QString m_displayName;
const QString m_settingsPrefix;
QString m_searchKeywords;
HighlighterSettings m_settings;
Ui::HighlighterSettingsPage *m_page;
};
HighlighterSettingsPage::HighlighterSettingsPagePrivate::
HighlighterSettingsPagePrivate(const QString &id) :
m_id(id),
m_displayName(tr("Generic Highlighter")),
m_settingsPrefix(QLatin1String("Text")),
m_page(0)
{}
HighlighterSettingsPage::HighlighterSettingsPage(const QString &id, QObject *parent) :
TextEditorOptionsPage(parent),
m_requestMimeTypeRegistration(false),
m_d(new HighlighterSettingsPagePrivate(id))
{
if (QSettings *s = Core::ICore::instance()->settings())
m_d->m_settings.fromSettings(m_d->m_settingsPrefix, s);
}
HighlighterSettingsPage::~HighlighterSettingsPage()
{
delete m_d;
}
QString HighlighterSettingsPage::id() const
{
return m_d->m_id;
}
QString HighlighterSettingsPage::displayName() const
{
return m_d->m_displayName;
}
QWidget *HighlighterSettingsPage::createPage(QWidget *parent)
{
QWidget *w = new QWidget(parent);
m_d->m_page = new Ui::HighlighterSettingsPage;
m_d->m_page->setupUi(w);
m_d->m_page->definitionFilesPath->setExpectedKind(Utils::PathChooser::ExistingDirectory);
m_d->m_page->definitionFilesPath->addButton(tr("Download Definitions"), this,
SLOT(requestAvailableDefinitionsMetaData()));
m_d->m_page->fallbackDefinitionFilesPath->setExpectedKind(Utils::PathChooser::ExistingDirectory);
m_d->m_page->fallbackDefinitionFilesPath->addButton(tr("Autodetect"), this,
SLOT(resetDefinitionsLocation()));
settingsToUI();
if (m_d->m_searchKeywords.isEmpty()) {
QTextStream(&m_d->m_searchKeywords) << m_d->m_page->definitionFilesGroupBox->title()
<< m_d->m_page->locationLabel->text()
<< m_d->m_page->alertWhenNoDefinition->text()
<< m_d->m_page->useFallbackLocation->text()
<< m_d->m_page->ignoreLabel->text();
}
connect(m_d->m_page->useFallbackLocation, SIGNAL(clicked(bool)),
this, SLOT(setFallbackLocationState(bool)));
connect(m_d->m_page->definitionFilesPath, SIGNAL(validChanged(bool)),
this, SLOT(setDownloadDefinitionsState(bool)));
connect(w, SIGNAL(destroyed()), this, SLOT(ignoreDownloadReply()));
return w;
}
void HighlighterSettingsPage::apply()
{
if (!m_d->m_page) // page was not shown
return;
if (settingsChanged())
settingsFromUI();
if (m_requestMimeTypeRegistration) {
Manager::instance()->registerMimeTypes();
m_requestMimeTypeRegistration = false;
}
}
void HighlighterSettingsPage::finish()
{
if (!m_d->m_page) // page was not shown
return;
delete m_d->m_page;
m_d->m_page = 0;
}
bool HighlighterSettingsPage::matches(const QString &s) const
{
return m_d->m_searchKeywords.contains(s, Qt::CaseInsensitive);
}
const HighlighterSettings &HighlighterSettingsPage::highlighterSettings() const
{
return m_d->m_settings;
}
void HighlighterSettingsPage::settingsFromUI()
{
if (!m_requestMimeTypeRegistration && (
m_d->m_settings.definitionFilesPath() != m_d->m_page->definitionFilesPath->path() ||
m_d->m_settings.fallbackDefinitionFilesPath() !=
m_d->m_page->fallbackDefinitionFilesPath->path() ||
m_d->m_settings.useFallbackLocation() != m_d->m_page->useFallbackLocation->isChecked())) {
m_requestMimeTypeRegistration = true;
}
m_d->m_settings.setDefinitionFilesPath(m_d->m_page->definitionFilesPath->path());
m_d->m_settings.setFallbackDefinitionFilesPath(m_d->m_page->fallbackDefinitionFilesPath->path());
m_d->m_settings.setAlertWhenNoDefinition(m_d->m_page->alertWhenNoDefinition->isChecked());
m_d->m_settings.setUseFallbackLocation(m_d->m_page->useFallbackLocation->isChecked());
m_d->m_settings.setIgnoredFilesPatterns(m_d->m_page->ignoreEdit->text());
if (QSettings *s = Core::ICore::instance()->settings())
m_d->m_settings.toSettings(m_d->m_settingsPrefix, s);
}
void HighlighterSettingsPage::settingsToUI()
{
m_d->m_page->definitionFilesPath->setPath(m_d->m_settings.definitionFilesPath());
m_d->m_page->fallbackDefinitionFilesPath->setPath(m_d->m_settings.fallbackDefinitionFilesPath());
m_d->m_page->alertWhenNoDefinition->setChecked(m_d->m_settings.alertWhenNoDefinition());
m_d->m_page->useFallbackLocation->setChecked(m_d->m_settings.useFallbackLocation());
m_d->m_page->ignoreEdit->setText(m_d->m_settings.ignoredFilesPatterns());
setFallbackLocationState(m_d->m_page->useFallbackLocation->isChecked());
setDownloadDefinitionsState(m_d->m_page->definitionFilesPath->isValid());
}
void HighlighterSettingsPage::resetDefinitionsLocation()
{
const QString &location = findFallbackDefinitionsLocation();
if (location.isEmpty())
QMessageBox::information(0, tr("Autodetect Definitions"),
tr("No pre-installed definitions could be found."));
else
m_d->m_page->fallbackDefinitionFilesPath->setPath(location);
}
void HighlighterSettingsPage::requestAvailableDefinitionsMetaData()
{
setDownloadDefinitionsState(false);
connect(Manager::instance(),
SIGNAL(definitionsMetaDataReady(QList<Internal::HighlightDefinitionMetaData>)),
this,
SLOT(manageDefinitions(QList<Internal::HighlightDefinitionMetaData>)),
Qt::UniqueConnection);
connect(Manager::instance(), SIGNAL(errorDownloadingDefinitionsMetaData()),
this, SLOT(showError()), Qt::UniqueConnection);
Manager::instance()->downloadAvailableDefinitionsMetaData();
}
void HighlighterSettingsPage::ignoreDownloadReply()
{
disconnect(Manager::instance(),
SIGNAL(definitionsMetaDataReady(QList<Internal::HighlightDefinitionMetaData>)),
this,
SLOT(manageDefinitions(QList<Internal::HighlightDefinitionMetaData>)));
disconnect(Manager::instance(), SIGNAL(errorDownloadingDefinitionsMetaData()),
this, SLOT(showError()));
}
void HighlighterSettingsPage::manageDefinitions(const QList<HighlightDefinitionMetaData> &metaData)
{
ManageDefinitionsDialog dialog(metaData,
m_d->m_page->definitionFilesPath->path() + QLatin1Char('/'),
m_d->m_page->definitionFilesPath->buttonAtIndex(1)->window());
if (dialog.exec() && !m_requestMimeTypeRegistration)
m_requestMimeTypeRegistration = true;
setDownloadDefinitionsState(m_d->m_page->definitionFilesPath->isValid());
}
void HighlighterSettingsPage::showError()
{
QMessageBox::critical(m_d->m_page->definitionFilesPath->buttonAtIndex(1)->window(),
tr("Error connecting to server."),
tr("Not possible to retrieve data."));
setDownloadDefinitionsState(m_d->m_page->definitionFilesPath->isValid());
}
void HighlighterSettingsPage::setFallbackLocationState(bool checked)
{
m_d->m_page->fallbackDefinitionFilesPath->setEnabled(checked);
}
void HighlighterSettingsPage::setDownloadDefinitionsState(bool valid)
{
m_d->m_page->definitionFilesPath->buttonAtIndex(1)->setEnabled(valid);
}
bool HighlighterSettingsPage::settingsChanged() const
{
if (m_d->m_settings.definitionFilesPath() != m_d->m_page->definitionFilesPath->path())
return true;
if (m_d->m_settings.fallbackDefinitionFilesPath() !=
m_d->m_page->fallbackDefinitionFilesPath->path())
return true;
if (m_d->m_settings.alertWhenNoDefinition() != m_d->m_page->alertWhenNoDefinition->isChecked())
return true;
if (m_d->m_settings.useFallbackLocation() != m_d->m_page->useFallbackLocation->isChecked())
return true;
if (m_d->m_settings.ignoredFilesPatterns() != m_d->m_page->ignoreEdit->text())
return true;
return false;
}
|