2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-07-06 10:38:53 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-07-06 10:38:53 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-07-06 10:38:53 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-07-06 10:38:53 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-07-06 10:38:53 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-07-06 10:38:53 +02:00
|
|
|
|
2011-02-03 15:48:14 +01:00
|
|
|
#include "qmljscodestylesettingspage.h"
|
|
|
|
|
#include "ui_qmljscodestylesettingspage.h"
|
|
|
|
|
#include "qmljstoolsconstants.h"
|
2011-05-25 08:50:01 +02:00
|
|
|
#include "qmljstoolssettings.h"
|
2011-02-03 15:48:14 +01:00
|
|
|
#include "qmljsindenter.h"
|
|
|
|
|
#include "qmljsqtstylecodeformatter.h"
|
|
|
|
|
|
|
|
|
|
#include <texteditor/snippets/isnippetprovider.h>
|
|
|
|
|
#include <texteditor/tabsettings.h>
|
2011-08-16 10:45:23 +02:00
|
|
|
#include <texteditor/simplecodestylepreferences.h>
|
2011-02-03 15:48:14 +01:00
|
|
|
#include <texteditor/displaysettings.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
2011-08-16 10:45:23 +02:00
|
|
|
#include <texteditor/codestyleeditor.h>
|
2011-02-03 15:48:14 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <qmljseditor/qmljseditorconstants.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextStream>
|
2011-02-03 15:48:14 +01:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
namespace QmlJSTools {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
// ------------------ CppCodeStyleSettingsWidget
|
|
|
|
|
|
2011-05-25 08:50:01 +02:00
|
|
|
QmlJSCodeStylePreferencesWidget::QmlJSCodeStylePreferencesWidget(QWidget *parent) :
|
2011-02-03 15:48:14 +01:00
|
|
|
QWidget(parent),
|
2011-08-16 10:45:23 +02:00
|
|
|
m_preferences(0),
|
2011-05-25 09:31:03 +02:00
|
|
|
m_ui(new Ui::QmlJSCodeStyleSettingsPage)
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
|
2014-05-08 11:58:23 +02:00
|
|
|
ISnippetProvider *provider = ExtensionSystem::PluginManager::getObject<ISnippetProvider>(
|
|
|
|
|
[](ISnippetProvider *provider) {
|
|
|
|
|
return provider->groupId() == QLatin1String(QmlJSEditor::Constants::QML_SNIPPETS_GROUP_ID);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (provider)
|
|
|
|
|
provider->decorateEditor(m_ui->previewTextEdit);
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2013-09-19 17:59:27 +02:00
|
|
|
decorateEditor(TextEditorSettings::fontSettings());
|
2016-06-27 22:25:11 +03:00
|
|
|
connect(TextEditorSettings::instance(), &TextEditorSettings::fontSettingsChanged,
|
|
|
|
|
this, &QmlJSCodeStylePreferencesWidget::decorateEditor);
|
2011-02-03 15:48:14 +01:00
|
|
|
|
|
|
|
|
setVisualizeWhitespace(true);
|
|
|
|
|
|
|
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 08:50:01 +02:00
|
|
|
QmlJSCodeStylePreferencesWidget::~QmlJSCodeStylePreferencesWidget()
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
delete m_ui;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
void QmlJSCodeStylePreferencesWidget::setPreferences(ICodeStylePreferences *preferences)
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
2011-08-16 10:45:23 +02:00
|
|
|
m_preferences = preferences;
|
|
|
|
|
m_ui->tabPreferencesWidget->setPreferences(preferences);
|
|
|
|
|
if (m_preferences)
|
2015-02-03 23:48:57 +02:00
|
|
|
connect(m_preferences, &ICodeStylePreferences::currentTabSettingsChanged,
|
2015-01-30 11:02:24 +01:00
|
|
|
this, &QmlJSCodeStylePreferencesWidget::slotSettingsChanged);
|
2011-02-03 15:48:14 +01:00
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
void QmlJSCodeStylePreferencesWidget::decorateEditor(const FontSettings &fontSettings)
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
2014-05-08 11:58:23 +02:00
|
|
|
const ISnippetProvider *provider = ExtensionSystem::PluginManager::getObject<ISnippetProvider>(
|
|
|
|
|
[](ISnippetProvider *current) {
|
|
|
|
|
return current->groupId() == QLatin1String(QmlJSEditor::Constants::QML_SNIPPETS_GROUP_ID);
|
|
|
|
|
});
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2014-08-01 23:31:56 +02:00
|
|
|
m_ui->previewTextEdit->textDocument()->setFontSettings(fontSettings);
|
2011-08-16 10:45:23 +02:00
|
|
|
if (provider)
|
|
|
|
|
provider->decorateEditor(m_ui->previewTextEdit);
|
2011-02-03 15:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
2011-05-25 08:50:01 +02:00
|
|
|
void QmlJSCodeStylePreferencesWidget::setVisualizeWhitespace(bool on)
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
DisplaySettings displaySettings = m_ui->previewTextEdit->displaySettings();
|
|
|
|
|
displaySettings.m_visualizeWhitespace = on;
|
|
|
|
|
m_ui->previewTextEdit->setDisplaySettings(displaySettings);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 08:50:01 +02:00
|
|
|
void QmlJSCodeStylePreferencesWidget::slotSettingsChanged()
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 08:50:01 +02:00
|
|
|
void QmlJSCodeStylePreferencesWidget::updatePreview()
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
QTextDocument *doc = m_ui->previewTextEdit->document();
|
|
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
const TabSettings &ts = m_preferences
|
2011-08-16 10:45:23 +02:00
|
|
|
? m_preferences->currentTabSettings()
|
2013-09-19 17:59:27 +02:00
|
|
|
: TextEditorSettings::codeStyle()->tabSettings();
|
2014-08-01 23:31:56 +02:00
|
|
|
m_ui->previewTextEdit->textDocument()->setTabSettings(ts);
|
2011-10-17 13:56:48 +02:00
|
|
|
CreatorCodeFormatter formatter(ts);
|
2011-02-03 15:48:14 +01:00
|
|
|
formatter.invalidateCache(doc);
|
|
|
|
|
|
|
|
|
|
QTextBlock block = doc->firstBlock();
|
|
|
|
|
QTextCursor tc = m_ui->previewTextEdit->textCursor();
|
|
|
|
|
tc.beginEditBlock();
|
|
|
|
|
while (block.isValid()) {
|
2014-08-01 23:31:56 +02:00
|
|
|
m_ui->previewTextEdit->textDocument()->indenter()
|
2014-01-13 16:56:53 +01:00
|
|
|
->indentBlock(doc, block, QChar::Null, ts);
|
2011-02-03 15:48:14 +01:00
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
tc.endEditBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------ CppCodeStyleSettingsPage
|
|
|
|
|
|
|
|
|
|
QmlJSCodeStyleSettingsPage::QmlJSCodeStyleSettingsPage(/*QSharedPointer<CppFileSettings> &settings,*/
|
|
|
|
|
QWidget *parent) :
|
2011-05-25 08:50:01 +02:00
|
|
|
Core::IOptionsPage(parent),
|
|
|
|
|
m_pageTabPreferences(0)
|
|
|
|
|
{
|
2013-01-16 15:22:58 +01:00
|
|
|
setId(Constants::QML_JS_CODE_STYLE_SETTINGS_ID);
|
2012-05-22 11:17:13 +02:00
|
|
|
setDisplayName(QCoreApplication::translate("QmlJSTools", Constants::QML_JS_CODE_STYLE_SETTINGS_NAME));
|
2012-12-29 03:37:27 +01:00
|
|
|
setCategory(QmlJSEditor::Constants::SETTINGS_CATEGORY_QML);
|
2012-05-22 11:17:13 +02:00
|
|
|
setDisplayCategory(QCoreApplication::translate("QmlJSEditor", QmlJSEditor::Constants::SETTINGS_TR_CATEGORY_QML));
|
2016-09-20 10:47:09 +02:00
|
|
|
setCategoryIcon(Utils::Icon(QmlJSTools::Constants::SETTINGS_CATEGORY_QML_ICON));
|
2011-02-03 15:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-03 14:17:03 +01:00
|
|
|
QWidget *QmlJSCodeStyleSettingsPage::widget()
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
2013-12-03 14:17:03 +01:00
|
|
|
if (!m_widget) {
|
2015-02-03 23:48:57 +02:00
|
|
|
SimpleCodeStylePreferences *originalTabPreferences
|
2013-12-03 14:17:03 +01:00
|
|
|
= QmlJSToolsSettings::globalCodeStyle();
|
2015-02-03 23:48:57 +02:00
|
|
|
m_pageTabPreferences = new SimpleCodeStylePreferences(m_widget);
|
2013-12-03 14:17:03 +01:00
|
|
|
m_pageTabPreferences->setDelegatingPool(originalTabPreferences->delegatingPool());
|
|
|
|
|
m_pageTabPreferences->setTabSettings(originalTabPreferences->tabSettings());
|
|
|
|
|
m_pageTabPreferences->setCurrentDelegate(originalTabPreferences->currentDelegate());
|
|
|
|
|
m_pageTabPreferences->setId(originalTabPreferences->id());
|
|
|
|
|
m_widget = new CodeStyleEditor(TextEditorSettings::codeStyleFactory(QmlJSTools::Constants::QML_JS_SETTINGS_ID),
|
|
|
|
|
m_pageTabPreferences);
|
|
|
|
|
}
|
2011-02-03 15:48:14 +01:00
|
|
|
return m_widget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSCodeStyleSettingsPage::apply()
|
|
|
|
|
{
|
2011-05-25 08:50:01 +02:00
|
|
|
if (m_widget) {
|
2012-01-24 15:36:40 +01:00
|
|
|
QSettings *s = Core::ICore::settings();
|
2011-05-25 08:50:01 +02:00
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
SimpleCodeStylePreferences *originalTabPreferences = QmlJSToolsSettings::globalCodeStyle();
|
2011-08-16 10:45:23 +02:00
|
|
|
if (originalTabPreferences->tabSettings() != m_pageTabPreferences->tabSettings()) {
|
|
|
|
|
originalTabPreferences->setTabSettings(m_pageTabPreferences->tabSettings());
|
2013-05-28 15:17:20 +02:00
|
|
|
originalTabPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
|
2011-05-25 08:50:01 +02:00
|
|
|
}
|
2011-08-16 10:45:23 +02:00
|
|
|
if (originalTabPreferences->currentDelegate() != m_pageTabPreferences->currentDelegate()) {
|
|
|
|
|
originalTabPreferences->setCurrentDelegate(m_pageTabPreferences->currentDelegate());
|
2013-05-28 15:17:20 +02:00
|
|
|
originalTabPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
|
2011-05-25 08:50:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
2011-02-03 15:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-03 14:17:03 +01:00
|
|
|
void QmlJSCodeStyleSettingsPage::finish()
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
2013-12-03 14:17:03 +01:00
|
|
|
delete m_widget;
|
2011-02-03 15:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2011-05-25 08:50:01 +02:00
|
|
|
} // namespace QmlJSTools
|