1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "qmljscodestylesettingspage.h"
27 #include "ui_qmljscodestylesettingspage.h"
28 #include "qmljstoolsconstants.h"
29 #include "qmljstoolssettings.h"
30 #include "qmljsindenter.h"
31 #include "qmljsqtstylecodeformatter.h"
32 
33 #include <texteditor/fontsettings.h>
34 #include <texteditor/snippets/snippetprovider.h>
35 #include <texteditor/tabsettings.h>
36 #include <texteditor/simplecodestylepreferences.h>
37 #include <texteditor/displaysettings.h>
38 #include <texteditor/texteditorsettings.h>
39 #include <texteditor/codestyleeditor.h>
40 #include <extensionsystem/pluginmanager.h>
41 #include <qmljseditor/qmljseditorconstants.h>
42 #include <coreplugin/icore.h>
43 
44 #include <QTextStream>
45 
46 using namespace TextEditor;
47 
48 namespace QmlJSTools {
49 namespace Internal {
50 
51 // ------------------ CppCodeStyleSettingsWidget
52 
QmlJSCodeStylePreferencesWidget(QWidget * parent)53 QmlJSCodeStylePreferencesWidget::QmlJSCodeStylePreferencesWidget(QWidget *parent) :
54     QWidget(parent),
55     m_ui(new Ui::QmlJSCodeStyleSettingsPage)
56 {
57     m_ui->setupUi(this);
58 
59     decorateEditor(TextEditorSettings::fontSettings());
60     connect(TextEditorSettings::instance(), &TextEditorSettings::fontSettingsChanged,
61        this, &QmlJSCodeStylePreferencesWidget::decorateEditor);
62 
63     setVisualizeWhitespace(true);
64 
65     updatePreview();
66 }
67 
~QmlJSCodeStylePreferencesWidget()68 QmlJSCodeStylePreferencesWidget::~QmlJSCodeStylePreferencesWidget()
69 {
70     delete m_ui;
71 }
72 
setPreferences(ICodeStylePreferences * preferences)73 void QmlJSCodeStylePreferencesWidget::setPreferences(ICodeStylePreferences *preferences)
74 {
75     m_preferences = preferences;
76     m_ui->tabPreferencesWidget->setPreferences(preferences);
77     if (m_preferences)
78         connect(m_preferences, &ICodeStylePreferences::currentTabSettingsChanged,
79                 this, &QmlJSCodeStylePreferencesWidget::slotSettingsChanged);
80     updatePreview();
81 }
82 
decorateEditor(const FontSettings & fontSettings)83 void QmlJSCodeStylePreferencesWidget::decorateEditor(const FontSettings &fontSettings)
84 {
85     m_ui->previewTextEdit->textDocument()->setFontSettings(fontSettings);
86     SnippetProvider::decorateEditor(m_ui->previewTextEdit,
87                                     QmlJSEditor::Constants::QML_SNIPPETS_GROUP_ID);
88 }
89 
setVisualizeWhitespace(bool on)90 void QmlJSCodeStylePreferencesWidget::setVisualizeWhitespace(bool on)
91 {
92     DisplaySettings displaySettings = m_ui->previewTextEdit->displaySettings();
93     displaySettings.m_visualizeWhitespace = on;
94     m_ui->previewTextEdit->setDisplaySettings(displaySettings);
95 }
96 
slotSettingsChanged()97 void QmlJSCodeStylePreferencesWidget::slotSettingsChanged()
98 {
99     updatePreview();
100 }
101 
updatePreview()102 void QmlJSCodeStylePreferencesWidget::updatePreview()
103 {
104     QTextDocument *doc = m_ui->previewTextEdit->document();
105 
106     const TabSettings &ts = m_preferences
107             ? m_preferences->currentTabSettings()
108             : TextEditorSettings::codeStyle()->tabSettings();
109     m_ui->previewTextEdit->textDocument()->setTabSettings(ts);
110     CreatorCodeFormatter formatter(ts);
111     formatter.invalidateCache(doc);
112 
113     QTextBlock block = doc->firstBlock();
114     QTextCursor tc = m_ui->previewTextEdit->textCursor();
115     tc.beginEditBlock();
116     while (block.isValid()) {
117         m_ui->previewTextEdit->textDocument()->indenter()->indentBlock(block, QChar::Null, ts);
118         block = block.next();
119     }
120     tc.endEditBlock();
121 }
122 
123 // ------------------ CppCodeStyleSettingsPage
124 
QmlJSCodeStyleSettingsPage()125 QmlJSCodeStyleSettingsPage::QmlJSCodeStyleSettingsPage()
126 {
127     setId(Constants::QML_JS_CODE_STYLE_SETTINGS_ID);
128     setDisplayName(QCoreApplication::translate("QmlJSTools", Constants::QML_JS_CODE_STYLE_SETTINGS_NAME));
129     setCategory(QmlJSEditor::Constants::SETTINGS_CATEGORY_QML);
130     setDisplayCategory(QCoreApplication::translate("QmlJSEditor", "Qt Quick"));
131     setCategoryIconPath(":/qmljstools/images/settingscategory_qml.png");
132 }
133 
widget()134 QWidget *QmlJSCodeStyleSettingsPage::widget()
135 {
136     if (!m_widget) {
137         SimpleCodeStylePreferences *originalTabPreferences
138                 = QmlJSToolsSettings::globalCodeStyle();
139         m_pageTabPreferences = new SimpleCodeStylePreferences(m_widget);
140         m_pageTabPreferences->setDelegatingPool(originalTabPreferences->delegatingPool());
141         m_pageTabPreferences->setTabSettings(originalTabPreferences->tabSettings());
142         m_pageTabPreferences->setCurrentDelegate(originalTabPreferences->currentDelegate());
143         m_pageTabPreferences->setId(originalTabPreferences->id());
144         m_widget = new CodeStyleEditor(TextEditorSettings::codeStyleFactory(QmlJSTools::Constants::QML_JS_SETTINGS_ID),
145                                        m_pageTabPreferences);
146     }
147     return m_widget;
148 }
149 
apply()150 void QmlJSCodeStyleSettingsPage::apply()
151 {
152     if (m_widget) {
153         QSettings *s = Core::ICore::settings();
154 
155         SimpleCodeStylePreferences *originalTabPreferences = QmlJSToolsSettings::globalCodeStyle();
156         if (originalTabPreferences->tabSettings() != m_pageTabPreferences->tabSettings()) {
157             originalTabPreferences->setTabSettings(m_pageTabPreferences->tabSettings());
158             originalTabPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
159         }
160         if (originalTabPreferences->currentDelegate() != m_pageTabPreferences->currentDelegate()) {
161             originalTabPreferences->setCurrentDelegate(m_pageTabPreferences->currentDelegate());
162             originalTabPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
163         }
164     }
165 }
166 
finish()167 void QmlJSCodeStyleSettingsPage::finish()
168 {
169     delete m_widget;
170 }
171 
172 } // namespace Internal
173 } // namespace QmlJSTools
174