OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / texteditorsettings.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 "texteditorsettings.h"
35 #include "texteditorconstants.h"
36
37 #include "basetexteditor.h"
38 #include "behaviorsettings.h"
39 #include "behaviorsettingspage.h"
40 #include "completionsettings.h"
41 #include "displaysettings.h"
42 #include "displaysettingspage.h"
43 #include "fontsettingspage.h"
44 #include "storagesettings.h"
45 #include "tabsettings.h"
46 #include "texteditorplugin.h"
47 #include "highlightersettingspage.h"
48 #include "snippetssettingspage.h"
49
50 #include <extensionsystem/pluginmanager.h>
51 #include <coreplugin/icore.h>
52 #include <utils/qtcassert.h>
53
54 #include <QtGui/QApplication>
55
56 using namespace TextEditor;
57 using namespace TextEditor::Constants;
58 using namespace TextEditor::Internal;
59
60 namespace TextEditor {
61 namespace Internal {
62
63 class TextEditorSettingsPrivate
64 {
65 public:
66     FontSettingsPage *m_fontSettingsPage;
67     BehaviorSettingsPage *m_behaviorSettingsPage;
68     DisplaySettingsPage *m_displaySettingsPage;
69     HighlighterSettingsPage *m_highlighterSettingsPage;
70     SnippetsSettingsPage *m_snippetsSettingsPage;
71
72     CompletionSettings m_completionSettings;
73
74     void fontZoomRequested(int pointSize);
75     void zoomResetRequested();
76 };
77
78 void TextEditorSettingsPrivate::fontZoomRequested(int zoom)
79 {
80     FontSettings &fs = const_cast<FontSettings&>(m_fontSettingsPage->fontSettings());
81     fs.setFontZoom(qMax(10, fs.fontZoom() + zoom));
82     m_fontSettingsPage->saveSettings();
83 }
84
85 void TextEditorSettingsPrivate::zoomResetRequested()
86 {
87     FontSettings &fs = const_cast<FontSettings&>(m_fontSettingsPage->fontSettings());
88     fs.setFontZoom(100);
89     m_fontSettingsPage->saveSettings();
90 }
91
92 } // namespace Internal
93 } // namespace TextEditor
94
95
96 TextEditorSettings *TextEditorSettings::m_instance = 0;
97
98 TextEditorSettings::TextEditorSettings(QObject *parent)
99     : QObject(parent)
100     , m_d(new Internal::TextEditorSettingsPrivate)
101 {
102     QTC_ASSERT(!m_instance, return);
103     m_instance = this;
104
105     ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
106
107     // Note: default background colors are coming from FormatDescription::background()
108
109     // Add font preference page
110     FormatDescriptions formatDescriptions;
111     formatDescriptions.append(FormatDescription(QLatin1String(C_TEXT), tr("Text")));
112
113     // Special categories
114     const QPalette p = QApplication::palette();
115     formatDescriptions.append(FormatDescription(QLatin1String(C_LINK), tr("Link"), Qt::blue));
116     formatDescriptions.append(FormatDescription(QLatin1String(C_SELECTION), tr("Selection"), p.color(QPalette::HighlightedText)));
117     formatDescriptions.append(FormatDescription(QLatin1String(C_LINE_NUMBER), tr("Line Number")));
118     formatDescriptions.append(FormatDescription(QLatin1String(C_SEARCH_RESULT), tr("Search Result")));
119     formatDescriptions.append(FormatDescription(QLatin1String(C_SEARCH_SCOPE), tr("Search Scope")));
120     formatDescriptions.append(FormatDescription(QLatin1String(C_PARENTHESES), tr("Parentheses")));
121     formatDescriptions.append(FormatDescription(QLatin1String(C_CURRENT_LINE), tr("Current Line")));
122
123     FormatDescription currentLineNumber = FormatDescription(QLatin1String(C_CURRENT_LINE_NUMBER), tr("Current Line Number"), Qt::darkGray);
124     currentLineNumber.format().setBold(true);
125     formatDescriptions.append(currentLineNumber);
126
127     formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES), tr("Occurrences")));
128     formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES_UNUSED), tr("Unused Occurrence")));
129     formatDescriptions.append(FormatDescription(QLatin1String(C_OCCURRENCES_RENAME), tr("Renaming Occurrence")));
130
131     // Standard categories
132     formatDescriptions.append(FormatDescription(QLatin1String(C_NUMBER), tr("Number"), Qt::darkBlue));
133     formatDescriptions.append(FormatDescription(QLatin1String(C_STRING), tr("String"), Qt::darkGreen));
134     formatDescriptions.append(FormatDescription(QLatin1String(C_TYPE), tr("Type"), Qt::darkMagenta));
135     formatDescriptions.append(FormatDescription(QLatin1String(C_LOCAL), tr("Local")));
136     formatDescriptions.append(FormatDescription(QLatin1String(C_FIELD), tr("Field"), Qt::darkRed));
137     formatDescriptions.append(FormatDescription(QLatin1String(C_STATIC), tr("Static"), Qt::darkMagenta));
138
139     FormatDescription virtualMethodFormatDescriptor(QLatin1String(C_VIRTUAL_METHOD), tr("Virtual Method"));
140     virtualMethodFormatDescriptor.format().setItalic(true);
141     formatDescriptions.append(virtualMethodFormatDescriptor);
142
143     formatDescriptions.append(FormatDescription(QLatin1String(C_KEYWORD), tr("Keyword"), Qt::darkYellow));
144     formatDescriptions.append(FormatDescription(QLatin1String(C_OPERATOR), tr("Operator")));
145     formatDescriptions.append(FormatDescription(QLatin1String(C_PREPROCESSOR), tr("Preprocessor"), Qt::darkBlue));
146     formatDescriptions.append(FormatDescription(QLatin1String(C_LABEL), tr("Label"), Qt::darkRed));
147     formatDescriptions.append(FormatDescription(QLatin1String(C_COMMENT), tr("Comment"), Qt::darkGreen));
148     formatDescriptions.append(FormatDescription(QLatin1String(C_DOXYGEN_COMMENT), tr("Doxygen Comment"), Qt::darkBlue));
149     formatDescriptions.append(FormatDescription(QLatin1String(C_DOXYGEN_TAG), tr("Doxygen Tag"), Qt::blue));
150     formatDescriptions.append(FormatDescription(QLatin1String(C_VISUAL_WHITESPACE), tr("Visual Whitespace"), Qt::lightGray));
151     formatDescriptions.append(FormatDescription(QLatin1String(C_DISABLED_CODE), tr("Disabled Code")));
152
153     // Diff categories
154     formatDescriptions.append(FormatDescription(QLatin1String(C_ADDED_LINE), tr("Added Line"), QColor(0, 170, 0)));
155     formatDescriptions.append(FormatDescription(QLatin1String(C_REMOVED_LINE), tr("Removed Line"), Qt::red));
156     formatDescriptions.append(FormatDescription(QLatin1String(C_DIFF_FILE), tr("Diff File"), Qt::darkBlue));
157     formatDescriptions.append(FormatDescription(QLatin1String(C_DIFF_LOCATION), tr("Diff Location"), Qt::blue));
158
159     m_d->m_fontSettingsPage = new FontSettingsPage(formatDescriptions,
160                                                    QLatin1String(Constants::TEXT_EDITOR_FONT_SETTINGS),
161                                                    this);
162     pm->addObject(m_d->m_fontSettingsPage);
163
164     // Add the GUI used to configure the tab, storage and interaction settings
165     TextEditor::BehaviorSettingsPageParameters behaviorSettingsPageParameters;
166     behaviorSettingsPageParameters.id = QLatin1String(Constants::TEXT_EDITOR_BEHAVIOR_SETTINGS);
167     behaviorSettingsPageParameters.displayName = tr("Behavior");
168     behaviorSettingsPageParameters.settingsPrefix = QLatin1String("text");
169     m_d->m_behaviorSettingsPage = new BehaviorSettingsPage(behaviorSettingsPageParameters, this);
170     pm->addObject(m_d->m_behaviorSettingsPage);
171
172     TextEditor::DisplaySettingsPageParameters displaySettingsPageParameters;
173     displaySettingsPageParameters.id = QLatin1String(Constants::TEXT_EDITOR_DISPLAY_SETTINGS),
174     displaySettingsPageParameters.displayName = tr("Display");
175     displaySettingsPageParameters.settingsPrefix = QLatin1String("text");
176     m_d->m_displaySettingsPage = new DisplaySettingsPage(displaySettingsPageParameters, this);
177     pm->addObject(m_d->m_displaySettingsPage);
178
179     m_d->m_highlighterSettingsPage =
180         new HighlighterSettingsPage(QLatin1String(Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS), this);
181     pm->addObject(m_d->m_highlighterSettingsPage);
182
183     m_d->m_snippetsSettingsPage =
184         new SnippetsSettingsPage(QLatin1String(Constants::TEXT_EDITOR_SNIPPETS_SETTINGS), this);
185     pm->addObject(m_d->m_snippetsSettingsPage);
186
187     connect(m_d->m_fontSettingsPage, SIGNAL(changed(TextEditor::FontSettings)),
188             this, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)));
189     connect(m_d->m_behaviorSettingsPage, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
190             this, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)));
191     connect(m_d->m_behaviorSettingsPage, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
192             this, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)));
193     connect(m_d->m_behaviorSettingsPage, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
194             this, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)));
195     connect(m_d->m_displaySettingsPage, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
196             this, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)));
197
198     // TODO: Move these settings to TextEditor category
199     if (QSettings *s = Core::ICore::instance()->settings())
200         m_d->m_completionSettings.fromSettings(QLatin1String("CppTools/"), s);
201 }
202
203 TextEditorSettings::~TextEditorSettings()
204 {
205     ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
206     pm->removeObject(m_d->m_fontSettingsPage);
207     pm->removeObject(m_d->m_behaviorSettingsPage);
208     pm->removeObject(m_d->m_displaySettingsPage);
209     pm->removeObject(m_d->m_highlighterSettingsPage);
210     pm->removeObject(m_d->m_snippetsSettingsPage);
211
212     delete m_d;
213
214     m_instance = 0;
215 }
216
217 TextEditorSettings *TextEditorSettings::instance()
218 {
219     return m_instance;
220 }
221
222 /**
223  * Initializes editor settings. Also connects signals to keep them up to date
224  * when they are changed.
225  */
226 void TextEditorSettings::initializeEditor(BaseTextEditor *editor)
227 {
228     // Connect to settings change signals
229     connect(this, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
230             editor, SLOT(setFontSettingsIfVisible(TextEditor::FontSettings)));
231     connect(this, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
232             editor, SLOT(setTabSettings(TextEditor::TabSettings)));
233     connect(this, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
234             editor, SLOT(setStorageSettings(TextEditor::StorageSettings)));
235     connect(this, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
236             editor, SLOT(setBehaviorSettings(TextEditor::BehaviorSettings)));
237     connect(this, SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
238             editor, SLOT(setDisplaySettings(TextEditor::DisplaySettings)));
239     connect(this, SIGNAL(completionSettingsChanged(TextEditor::CompletionSettings)),
240             editor, SLOT(setCompletionSettings(TextEditor::CompletionSettings)));
241
242     connect(editor, SIGNAL(requestFontZoom(int)),
243             this, SLOT(fontZoomRequested(int)));
244     connect(editor, SIGNAL(requestZoomReset()),
245             this, SLOT(zoomResetRequested()));
246
247     // Apply current settings (tab settings depend on font settings)
248     editor->setFontSettings(fontSettings());
249     editor->setTabSettings(tabSettings());
250     editor->setStorageSettings(storageSettings());
251     editor->setBehaviorSettings(behaviorSettings());
252     editor->setDisplaySettings(displaySettings());
253     editor->setCompletionSettings(completionSettings());
254 }
255
256
257 const FontSettings &TextEditorSettings::fontSettings() const
258 {
259     return m_d->m_fontSettingsPage->fontSettings();
260 }
261
262 const TabSettings &TextEditorSettings::tabSettings() const
263 {
264     return m_d->m_behaviorSettingsPage->tabSettings();
265 }
266
267 const StorageSettings &TextEditorSettings::storageSettings() const
268 {
269     return m_d->m_behaviorSettingsPage->storageSettings();
270 }
271
272 const BehaviorSettings &TextEditorSettings::behaviorSettings() const
273 {
274     return m_d->m_behaviorSettingsPage->behaviorSettings();
275 }
276
277 const DisplaySettings &TextEditorSettings::displaySettings() const
278 {
279     return m_d->m_displaySettingsPage->displaySettings();
280 }
281
282 const CompletionSettings &TextEditorSettings::completionSettings() const
283 {
284     return m_d->m_completionSettings;
285 }
286
287 const HighlighterSettings &TextEditorSettings::highlighterSettings() const
288 {
289     return m_d->m_highlighterSettingsPage->highlighterSettings();
290 }
291
292 void TextEditorSettings::setCompletionSettings(const TextEditor::CompletionSettings &settings)
293 {
294     if (m_d->m_completionSettings == settings)
295         return;
296
297     m_d->m_completionSettings = settings;
298     if (QSettings *s = Core::ICore::instance()->settings())
299         m_d->m_completionSettings.toSettings(QLatin1String("CppTools/"), s);
300
301     emit completionSettingsChanged(m_d->m_completionSettings);
302 }
303
304 #include "moc_texteditorsettings.cpp"