texteditorsettings.cpp 12.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "texteditorsettings.h"
31
#include "texteditorconstants.h"
con's avatar
con committed
32

33
#include "basetexteditor.h"
34
#include "behaviorsettings.h"
35
#include "behaviorsettingspage.h"
36
#include "completionsettings.h"
con's avatar
con committed
37
#include "displaysettings.h"
38
#include "displaysettingspage.h"
con's avatar
con committed
39
40
41
42
#include "fontsettingspage.h"
#include "storagesettings.h"
#include "tabsettings.h"
#include "texteditorplugin.h"
43
#include "highlightersettingspage.h"
con's avatar
con committed
44

45
#include <extensionsystem/pluginmanager.h>
46
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
47
48
#include <utils/qtcassert.h>

49
#include <QtGui/QApplication>
con's avatar
con committed
50
51
52

using namespace TextEditor;
using namespace TextEditor::Constants;
53
54
55
56
57
58
59
60
61
62
63
using namespace TextEditor::Internal;

namespace TextEditor {
namespace Internal {

class TextEditorSettingsPrivate
{
public:
    FontSettingsPage *m_fontSettingsPage;
    BehaviorSettingsPage *m_behaviorSettingsPage;
    DisplaySettingsPage *m_displaySettingsPage;
64
    HighlighterSettingsPage *m_highlighterSettingsPage;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    CompletionSettings m_completionSettings;

    void fontZoomRequested(int pointSize);
    void zoomResetRequested();
};

void TextEditorSettingsPrivate::fontZoomRequested(int zoom)
{
    FontSettings &fs = const_cast<FontSettings&>(m_fontSettingsPage->fontSettings());
    fs.setFontZoom(qMax(10, fs.fontZoom() + zoom));
    m_fontSettingsPage->saveSettings();
}

void TextEditorSettingsPrivate::zoomResetRequested()
{
    FontSettings &fs = const_cast<FontSettings&>(m_fontSettingsPage->fontSettings());
    fs.setFontZoom(100);
    m_fontSettingsPage->saveSettings();
}

} // namespace Internal
} // namespace TextEditor

con's avatar
con committed
89
90
91

TextEditorSettings *TextEditorSettings::m_instance = 0;

92
TextEditorSettings::TextEditorSettings(QObject *parent)
con's avatar
con committed
93
    : QObject(parent)
94
    , m_d(new Internal::TextEditorSettingsPrivate)
con's avatar
con committed
95
{
hjk's avatar
hjk committed
96
    QTC_ASSERT(!m_instance, return);
con's avatar
con committed
97
98
99
100
101
102
103
104
    m_instance = this;

    ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();

    // Note: default background colors are coming from FormatDescription::background()

    // Add font preference page
    FormatDescriptions formatDescriptions;
105
    formatDescriptions.append(FormatDescription(QLatin1String(C_TEXT), tr("Text")));
con's avatar
con committed
106
107
108

    // Special categories
    const QPalette p = QApplication::palette();
109
110
111
112
113
114
115
    formatDescriptions.append(FormatDescription(QLatin1String(C_LINK), tr("Link"), Qt::blue));
    formatDescriptions.append(FormatDescription(QLatin1String(C_SELECTION), tr("Selection"), p.color(QPalette::HighlightedText)));
    formatDescriptions.append(FormatDescription(QLatin1String(C_LINE_NUMBER), tr("Line Number")));
    formatDescriptions.append(FormatDescription(QLatin1String(C_SEARCH_RESULT), tr("Search Result")));
    formatDescriptions.append(FormatDescription(QLatin1String(C_SEARCH_SCOPE), tr("Search Scope")));
    formatDescriptions.append(FormatDescription(QLatin1String(C_PARENTHESES), tr("Parentheses")));
    formatDescriptions.append(FormatDescription(QLatin1String(C_CURRENT_LINE), tr("Current Line")));
116
117
118

    FormatDescription currentLineNumber = FormatDescription(QLatin1String(C_CURRENT_LINE_NUMBER), tr("Current Line Number"), Qt::darkGray);
    currentLineNumber.format().setBold(true);
119
    formatDescriptions.append(currentLineNumber);
con's avatar
con committed
120

121
    formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES), tr("Occurrences")));
Roberto Raggi's avatar
Roberto Raggi committed
122
    formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES_UNUSED), tr("Unused Occurrence")));
123
124
    formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES_RENAME), tr("Renaming Occurrence")));

con's avatar
con committed
125
    // Standard categories
126
127
128
    formatDescriptions.append(FormatDescription(QLatin1String(C_NUMBER), tr("Number"), Qt::darkBlue));
    formatDescriptions.append(FormatDescription(QLatin1String(C_STRING), tr("String"), Qt::darkGreen));
    formatDescriptions.append(FormatDescription(QLatin1String(C_TYPE), tr("Type"), Qt::darkMagenta));
129
    formatDescriptions.append(FormatDescription(QLatin1String(C_LOCAL), tr("Local")));
130
    formatDescriptions.append(FormatDescription(QLatin1String(C_FIELD), tr("Field"), Qt::darkRed));
131
132
133
134
135

    FormatDescription virtualMethodFormatDescriptor(QLatin1String(C_VIRTUAL_METHOD), tr("Virtual Method"));
    virtualMethodFormatDescriptor.format().setItalic(true);
    formatDescriptions.append(virtualMethodFormatDescriptor);

136
137
138
139
140
141
142
    formatDescriptions.append(FormatDescription(QLatin1String(C_KEYWORD), tr("Keyword"), Qt::darkYellow));
    formatDescriptions.append(FormatDescription(QLatin1String(C_OPERATOR), tr("Operator")));
    formatDescriptions.append(FormatDescription(QLatin1String(C_PREPROCESSOR), tr("Preprocessor"), Qt::darkBlue));
    formatDescriptions.append(FormatDescription(QLatin1String(C_LABEL), tr("Label"), Qt::darkRed));
    formatDescriptions.append(FormatDescription(QLatin1String(C_COMMENT), tr("Comment"), Qt::darkGreen));
    formatDescriptions.append(FormatDescription(QLatin1String(C_DOXYGEN_COMMENT), tr("Doxygen Comment"), Qt::darkBlue));
    formatDescriptions.append(FormatDescription(QLatin1String(C_DOXYGEN_TAG), tr("Doxygen Tag"), Qt::blue));
mae's avatar
mae committed
143
    formatDescriptions.append(FormatDescription(QLatin1String(C_VISUAL_WHITESPACE), tr("Visual Whitespace"), Qt::lightGray));
144
    formatDescriptions.append(FormatDescription(QLatin1String(C_DISABLED_CODE), tr("Disabled Code")));
con's avatar
con committed
145
146

    // Diff categories
147
148
149
150
    formatDescriptions.append(FormatDescription(QLatin1String(C_ADDED_LINE), tr("Added Line"), QColor(0, 170, 0)));
    formatDescriptions.append(FormatDescription(QLatin1String(C_REMOVED_LINE), tr("Removed Line"), Qt::red));
    formatDescriptions.append(FormatDescription(QLatin1String(C_DIFF_FILE), tr("Diff File"), Qt::darkBlue));
    formatDescriptions.append(FormatDescription(QLatin1String(C_DIFF_LOCATION), tr("Diff Location"), Qt::blue));
con's avatar
con committed
151

152
    m_d->m_fontSettingsPage = new FontSettingsPage(formatDescriptions,
153
                                                   QLatin1String(Constants::TEXT_EDITOR_FONT_SETTINGS),
154
155
                                                   this);
    pm->addObject(m_d->m_fontSettingsPage);
con's avatar
con committed
156

157
158
    // Add the GUI used to configure the tab, storage and interaction settings
    TextEditor::BehaviorSettingsPageParameters behaviorSettingsPageParameters;
159
    behaviorSettingsPageParameters.id = QLatin1String(Constants::TEXT_EDITOR_BEHAVIOR_SETTINGS);
160
    behaviorSettingsPageParameters.displayName = tr("Behavior");
161
    behaviorSettingsPageParameters.settingsPrefix = QLatin1String("text");
162
163
    m_d->m_behaviorSettingsPage = new BehaviorSettingsPage(behaviorSettingsPageParameters, this);
    pm->addObject(m_d->m_behaviorSettingsPage);
164
165

    TextEditor::DisplaySettingsPageParameters displaySettingsPageParameters;
166
    displaySettingsPageParameters.id = QLatin1String(Constants::TEXT_EDITOR_DISPLAY_SETTINGS),
167
    displaySettingsPageParameters.displayName = tr("Display");
168
    displaySettingsPageParameters.settingsPrefix = QLatin1String("text");
169
170
    m_d->m_displaySettingsPage = new DisplaySettingsPage(displaySettingsPageParameters, this);
    pm->addObject(m_d->m_displaySettingsPage);
con's avatar
con committed
171

172
    m_d->m_highlighterSettingsPage =
173
        new HighlighterSettingsPage(QLatin1String(Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS), this);
174
175
    pm->addObject(m_d->m_highlighterSettingsPage);

176
    connect(m_d->m_fontSettingsPage, SIGNAL(changed(TextEditor::FontSettings)),
con's avatar
con committed
177
            this, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)));
178
    connect(m_d->m_behaviorSettingsPage, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
con's avatar
con committed
179
            this, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)));
180
    connect(m_d->m_behaviorSettingsPage, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
con's avatar
con committed
181
            this, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)));
182
    connect(m_d->m_behaviorSettingsPage, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
183
            this, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)));
184
    connect(m_d->m_displaySettingsPage, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
con's avatar
con committed
185
            this, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)));
186
187
188
189

    // TODO: Move these settings to TextEditor category
    if (QSettings *s = Core::ICore::instance()->settings())
        m_d->m_completionSettings.fromSettings(QLatin1String("CppTools/"), s);
con's avatar
con committed
190
191
192
193
194
}

TextEditorSettings::~TextEditorSettings()
{
    ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
195
196
197
    pm->removeObject(m_d->m_fontSettingsPage);
    pm->removeObject(m_d->m_behaviorSettingsPage);
    pm->removeObject(m_d->m_displaySettingsPage);
198
    pm->removeObject(m_d->m_highlighterSettingsPage);
199
200

    delete m_d;
con's avatar
con committed
201
202
203
204
205
206
207
208
209

    m_instance = 0;
}

TextEditorSettings *TextEditorSettings::instance()
{
    return m_instance;
}

210
211
212
213
214
215
216
217
/**
 * Initializes editor settings. Also connects signals to keep them up to date
 * when they are changed.
 */
void TextEditorSettings::initializeEditor(BaseTextEditor *editor)
{
    // Connect to settings change signals
    connect(this, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
Roberto Raggi's avatar
Roberto Raggi committed
218
            editor, SLOT(setFontSettingsIfVisible(TextEditor::FontSettings)));
219
220
221
222
    connect(this, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
            editor, SLOT(setTabSettings(TextEditor::TabSettings)));
    connect(this, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
            editor, SLOT(setStorageSettings(TextEditor::StorageSettings)));
223
224
    connect(this, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
            editor, SLOT(setBehaviorSettings(TextEditor::BehaviorSettings)));
225
226
    connect(this, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
            editor, SLOT(setDisplaySettings(TextEditor::DisplaySettings)));
227
228
    connect(this, SIGNAL(completionSettingsChanged(TextEditor::CompletionSettings)),
            editor, SLOT(setCompletionSettings(TextEditor::CompletionSettings)));
229

mae's avatar
mae committed
230
231
232
233
    connect(editor, SIGNAL(requestFontZoom(int)),
            this, SLOT(fontZoomRequested(int)));
    connect(editor, SIGNAL(requestZoomReset()),
            this, SLOT(zoomResetRequested()));
mae's avatar
mae committed
234

235
236
237
238
    // Apply current settings (tab settings depend on font settings)
    editor->setFontSettings(fontSettings());
    editor->setTabSettings(tabSettings());
    editor->setStorageSettings(storageSettings());
239
    editor->setBehaviorSettings(behaviorSettings());
240
    editor->setDisplaySettings(displaySettings());
241
    editor->setCompletionSettings(completionSettings());
242
243
}

mae's avatar
mae committed
244

245
const FontSettings &TextEditorSettings::fontSettings() const
mae's avatar
mae committed
246
{
247
    return m_d->m_fontSettingsPage->fontSettings();
mae's avatar
mae committed
248
249
}

250
const TabSettings &TextEditorSettings::tabSettings() const
mae's avatar
mae committed
251
{
252
    return m_d->m_behaviorSettingsPage->tabSettings();
mae's avatar
mae committed
253
254
}

255
const StorageSettings &TextEditorSettings::storageSettings() const
con's avatar
con committed
256
{
257
    return m_d->m_behaviorSettingsPage->storageSettings();
con's avatar
con committed
258
259
}

260
const BehaviorSettings &TextEditorSettings::behaviorSettings() const
con's avatar
con committed
261
{
262
    return m_d->m_behaviorSettingsPage->behaviorSettings();
con's avatar
con committed
263
264
}

265
const DisplaySettings &TextEditorSettings::displaySettings() const
con's avatar
con committed
266
{
267
    return m_d->m_displaySettingsPage->displaySettings();
con's avatar
con committed
268
269
}

270
const CompletionSettings &TextEditorSettings::completionSettings() const
271
{
272
    return m_d->m_completionSettings;
273
274
}

275
276
277
278
279
const HighlighterSettings &TextEditorSettings::highlighterSettings() const
{
    return m_d->m_highlighterSettingsPage->highlighterSettings();
}

280
void TextEditorSettings::setCompletionSettings(const TextEditor::CompletionSettings &settings)
con's avatar
con committed
281
{
282
283
284
285
286
287
288
289
    if (m_d->m_completionSettings == settings)
        return;

    m_d->m_completionSettings = settings;
    if (QSettings *s = Core::ICore::instance()->settings())
        m_d->m_completionSettings.toSettings(QLatin1String("CppTools/"), s);

    emit completionSettingsChanged(m_d->m_completionSettings);
con's avatar
con committed
290
}
291
292

#include "moc_texteditorsettings.cpp"