texteditorsettings.cpp 10.1 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"
con's avatar
con committed
36
#include "displaysettings.h"
37
#include "displaysettingspage.h"
con's avatar
con committed
38
39
40
41
42
#include "fontsettingspage.h"
#include "storagesettings.h"
#include "tabsettings.h"
#include "texteditorplugin.h"

43
#include <extensionsystem/pluginmanager.h>
hjk's avatar
hjk committed
44
45
#include <utils/qtcassert.h>

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

using namespace TextEditor;
using namespace TextEditor::Constants;

TextEditorSettings *TextEditorSettings::m_instance = 0;

53
TextEditorSettings::TextEditorSettings(QObject *parent)
con's avatar
con committed
54
55
    : QObject(parent)
{
hjk's avatar
hjk committed
56
    QTC_ASSERT(!m_instance, return);
con's avatar
con committed
57
58
59
60
61
62
63
64
    m_instance = this;

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

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

    // Add font preference page
    FormatDescriptions formatDescriptions;
65
    formatDescriptions.append(FormatDescription(QLatin1String(C_TEXT), tr("Text")));
con's avatar
con committed
66
67
68

    // Special categories
    const QPalette p = QApplication::palette();
69
70
71
72
73
74
75
    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")));
76
77
78

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

81
    formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES), tr("Occurrences")));
Roberto Raggi's avatar
Roberto Raggi committed
82
    formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES_UNUSED), tr("Unused Occurrence")));
83
84
    formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES_RENAME), tr("Renaming Occurrence")));

con's avatar
con committed
85
    // Standard categories
86
87
88
89
90
91
92
93
94
95
    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));
    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
96
    formatDescriptions.append(FormatDescription(QLatin1String(C_VISUAL_WHITESPACE), tr("Visual Whitespace"), Qt::lightGray));
97
    formatDescriptions.append(FormatDescription(QLatin1String(C_DISABLED_CODE), tr("Disabled Code")));
con's avatar
con committed
98
99

    // Diff categories
100
101
102
103
    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
104
105

    m_fontSettingsPage = new FontSettingsPage(formatDescriptions,
106
                                              QLatin1String("A.FontSettings"),
107
                                              this);
con's avatar
con committed
108
109
    pm->addObject(m_fontSettingsPage);

110
111
    // Add the GUI used to configure the tab, storage and interaction settings
    TextEditor::BehaviorSettingsPageParameters behaviorSettingsPageParameters;
112
    behaviorSettingsPageParameters.id = QLatin1String("B.BehaviourSettings");
113
    behaviorSettingsPageParameters.displayName = tr("Behavior");
114
115
116
117
118
    behaviorSettingsPageParameters.settingsPrefix = QLatin1String("text");
    m_behaviorSettingsPage = new BehaviorSettingsPage(behaviorSettingsPageParameters, this);
    pm->addObject(m_behaviorSettingsPage);

    TextEditor::DisplaySettingsPageParameters displaySettingsPageParameters;
119
    displaySettingsPageParameters.id = QLatin1String("D.DisplaySettings"),
120
    displaySettingsPageParameters.displayName = tr("Display");
121
122
123
    displaySettingsPageParameters.settingsPrefix = QLatin1String("text");
    m_displaySettingsPage = new DisplaySettingsPage(displaySettingsPageParameters, this);
    pm->addObject(m_displaySettingsPage);
con's avatar
con committed
124
125
126

    connect(m_fontSettingsPage, SIGNAL(changed(TextEditor::FontSettings)),
            this, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)));
127
    connect(m_behaviorSettingsPage, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
con's avatar
con committed
128
            this, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)));
129
    connect(m_behaviorSettingsPage, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
con's avatar
con committed
130
            this, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)));
131
132
    connect(m_behaviorSettingsPage, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
            this, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)));
133
    connect(m_displaySettingsPage, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
con's avatar
con committed
134
135
136
137
138
139
140
            this, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)));
}

TextEditorSettings::~TextEditorSettings()
{
    ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
    pm->removeObject(m_fontSettingsPage);
141
142
    pm->removeObject(m_behaviorSettingsPage);
    pm->removeObject(m_displaySettingsPage);
con's avatar
con committed
143
144
145
146
147
148
149
150
151

    m_instance = 0;
}

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

152
153
154
155
156
157
158
159
/**
 * 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
160
            editor, SLOT(setFontSettingsIfVisible(TextEditor::FontSettings)));
161
162
163
164
    connect(this, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
            editor, SLOT(setTabSettings(TextEditor::TabSettings)));
    connect(this, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
            editor, SLOT(setStorageSettings(TextEditor::StorageSettings)));
165
166
    connect(this, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
            editor, SLOT(setBehaviorSettings(TextEditor::BehaviorSettings)));
167
168
169
    connect(this, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
            editor, SLOT(setDisplaySettings(TextEditor::DisplaySettings)));

mae's avatar
mae committed
170
171
172
173
    connect(editor, SIGNAL(requestFontZoom(int)),
            this, SLOT(fontZoomRequested(int)));
    connect(editor, SIGNAL(requestZoomReset()),
            this, SLOT(zoomResetRequested()));
mae's avatar
mae committed
174

175
176
177
178
    // Apply current settings (tab settings depend on font settings)
    editor->setFontSettings(fontSettings());
    editor->setTabSettings(tabSettings());
    editor->setStorageSettings(storageSettings());
179
    editor->setBehaviorSettings(behaviorSettings());
180
181
182
    editor->setDisplaySettings(displaySettings());
}

mae's avatar
mae committed
183

mae's avatar
mae committed
184
void TextEditorSettings::fontZoomRequested(int zoom)
mae's avatar
mae committed
185
{
186
    FontSettings &fs = const_cast<FontSettings&>(fontSettings());
mae's avatar
mae committed
187
188
189
190
191
192
    fs.setFontZoom(qMax(10, fs.fontZoom() + zoom));
    m_fontSettingsPage->saveSettings();
}

void TextEditorSettings::zoomResetRequested()
{
193
    FontSettings &fs = const_cast<FontSettings&>(fontSettings());
mae's avatar
mae committed
194
    fs.setFontZoom(100);
195
    m_fontSettingsPage->saveSettings();
mae's avatar
mae committed
196
197
}

198
const FontSettings &TextEditorSettings::fontSettings() const
con's avatar
con committed
199
200
201
202
{
    return m_fontSettingsPage->fontSettings();
}

203
const TabSettings &TextEditorSettings::tabSettings() const
con's avatar
con committed
204
{
205
    return m_behaviorSettingsPage->tabSettings();
con's avatar
con committed
206
207
}

208
const StorageSettings &TextEditorSettings::storageSettings() const
con's avatar
con committed
209
{
210
    return m_behaviorSettingsPage->storageSettings();
con's avatar
con committed
211
212
}

213
const BehaviorSettings &TextEditorSettings::behaviorSettings() const
214
215
216
217
{
    return m_behaviorSettingsPage->behaviorSettings();
}

218
const DisplaySettings &TextEditorSettings::displaySettings() const
con's avatar
con committed
219
{
220
    return m_displaySettingsPage->displaySettings();
con's avatar
con committed
221
}