cppcodestylesettingspage.cpp 21.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Christian Kamm's avatar
Christian Kamm committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
Christian Kamm's avatar
Christian Kamm committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Christian Kamm's avatar
Christian Kamm committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Christian Kamm's avatar
Christian Kamm committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
Christian Kamm's avatar
Christian Kamm committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Christian Kamm's avatar
Christian Kamm committed
30

31
#include "cppcodestylesettingspage.h"
32

33
#include "cppcodestylepreferences.h"
34 35
#include "cpppointerdeclarationformatter.h"
#include "cppqtstyleindenter.h"
36 37
#include "cpptoolsconstants.h"
#include "cpptoolssettings.h"
38
#include <ui_cppcodestylesettingspage.h>
39 40 41 42

#include <coreplugin/icore.h>
#include <cppeditor/cppeditorconstants.h>
#include <texteditor/codestyleeditor.h>
43
#include <texteditor/textdocument.h>
44
#include <texteditor/displaysettings.h>
45 46 47
#include <texteditor/snippets/isnippetprovider.h>
#include <texteditor/texteditorsettings.h>

48 49 50 51
#include <cplusplus/pp.h>

#include <extensionsystem/pluginmanager.h>

52 53
#include <QTextBlock>
#include <QTextStream>
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

static const char *defaultCodeStyleSnippets[] = {
    "#include <math.h>\n"
    "\n"
    "class Complex\n"
    "    {\n"
    "public:\n"
    "    Complex(double re, double im)\n"
    "        : _re(re), _im(im)\n"
    "        {}\n"
    "    double modulus() const\n"
    "        {\n"
    "        return sqrt(_re * _re + _im * _im);\n"
    "        }\n"
    "private:\n"
    "    double _re;\n"
    "    double _im;\n"
    "    };\n"
    "\n"
    "void bar(int i)\n"
    "    {\n"
    "    static int counter = 0;\n"
    "    counter += i;\n"
    "    }\n"
    "\n"
    "namespace Foo\n"
    "    {\n"
    "    namespace Bar\n"
    "        {\n"
    "        void foo(int a, int b)\n"
    "            {\n"
    "            for (int i = 0; i < a; i++)\n"
    "                {\n"
    "                if (i < b)\n"
    "                    bar(i);\n"
    "                else\n"
    "                    {\n"
    "                    bar(i);\n"
    "                    bar(b);\n"
    "                    }\n"
    "                }\n"
    "            }\n"
    "        } // namespace Bar\n"
    "    } // namespace Foo\n"
    ,
    "#include <math.h>\n"
    "\n"
    "class Complex\n"
    "    {\n"
    "public:\n"
    "    Complex(double re, double im)\n"
    "        : _re(re), _im(im)\n"
    "        {}\n"
    "    double modulus() const\n"
    "        {\n"
    "        return sqrt(_re * _re + _im * _im);\n"
    "        }\n"
    "private:\n"
    "    double _re;\n"
    "    double _im;\n"
    "    };\n"
    "\n"
    "void bar(int i)\n"
    "    {\n"
    "    static int counter = 0;\n"
    "    counter += i;\n"
    "    }\n"
    "\n"
    "namespace Foo\n"
    "    {\n"
    "    namespace Bar\n"
    "        {\n"
    "        void foo(int a, int b)\n"
    "            {\n"
    "            for (int i = 0; i < a; i++)\n"
    "                {\n"
    "                if (i < b)\n"
    "                    bar(i);\n"
    "                else\n"
    "                    {\n"
    "                    bar(i);\n"
    "                    bar(b);\n"
    "                    }\n"
    "                }\n"
    "            }\n"
    "        } // namespace Bar\n"
    "    } // namespace Foo\n"
    ,
    "namespace Foo\n"
    "{\n"
    "namespace Bar\n"
    "{\n"
    "class FooBar\n"
    "    {\n"
    "public:\n"
    "    FooBar(int a)\n"
    "        : _a(a)\n"
    "        {}\n"
    "    int calculate() const\n"
    "        {\n"
    "        if (a > 10)\n"
    "            {\n"
    "            int b = 2 * a;\n"
    "            return a * b;\n"
    "            }\n"
    "        return -a;\n"
    "        }\n"
    "private:\n"
    "    int _a;\n"
    "    };\n"
    "}\n"
    "}\n"
    ,
    "#include \"bar.h\"\n"
    "\n"
    "int foo(int a)\n"
    "    {\n"
    "    switch (a)\n"
    "        {\n"
    "        case 1:\n"
    "            bar(1);\n"
    "            break;\n"
    "        case 2:\n"
    "            {\n"
    "            bar(2);\n"
    "            break;\n"
    "            }\n"
    "        case 3:\n"
    "        default:\n"
    "            bar(3);\n"
    "            break;\n"
    "        }\n"
    "    return 0;\n"
    "    }\n"
    ,
    "void foo() {\n"
    "    if (a &&\n"
    "        b)\n"
    "        c;\n"
    "\n"
    "    while (a ||\n"
    "           b)\n"
    "        break;\n"
    "    a = b +\n"
    "        c;\n"
    "    myInstance.longMemberName +=\n"
    "            foo;\n"
    "    myInstance.longMemberName += bar +\n"
    "                                 foo;\n"
    "}\n"
204 205 206 207 208 209 210 211 212 213 214 215 216
    ,
    "int *foo(const Bar &b1, Bar &&b2, int*, int *&rpi)\n"
    "{\n"
    "    int*pi = 0;\n"
    "    int*const*const cpcpi = &pi;\n"
    "    int*const*pcpi = &pi;\n"
    "    int**const cppi = &pi;\n"
    "\n"
    "    void (*foo)(char *s) = 0;\n"
    "    int (*bar)[] = 0;\n"
    "\n"
    "    return pi;\n"
    "}\n"
217 218 219 220 221 222 223 224
};

using namespace TextEditor;

namespace CppTools {

namespace Internal {

225
static void applyRefactorings(QTextDocument *textDocument, TextEditorWidget *editor,
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                              const CppCodeStyleSettings &settings)
{
    // Preprocess source
    Environment env;
    Preprocessor preprocess(0, &env);
    const QByteArray preprocessedSource
        = preprocess.run(QLatin1String("<no-file>"), textDocument->toPlainText());

    Document::Ptr cppDocument = Document::create(QLatin1String("<no-file>"));
    cppDocument->setUtf8Source(preprocessedSource);
    cppDocument->parse(Document::ParseTranlationUnit);
    cppDocument->check();

    CppRefactoringFilePtr cppRefactoringFile = CppRefactoringChanges::file(editor, cppDocument);

    // Run the formatter
    Overview overview;
    overview.showReturnTypes = true;
    overview.starBindFlags = Overview::StarBindFlags(0);

    if (settings.bindStarToIdentifier)
        overview.starBindFlags |= Overview::BindToIdentifier;
    if (settings.bindStarToTypeName)
        overview.starBindFlags |= Overview::BindToTypeName;
    if (settings.bindStarToLeftSpecifier)
        overview.starBindFlags |= Overview::BindToLeftSpecifier;
    if (settings.bindStarToRightSpecifier)
        overview.starBindFlags |= Overview::BindToRightSpecifier;

    PointerDeclarationFormatter formatter(cppRefactoringFile, overview);
    Utils::ChangeSet change = formatter.format(cppDocument->translationUnit()->ast());

    // Apply change
    QTextCursor cursor(textDocument);
    change.apply(&cursor);
}

263 264 265 266
// ------------------ CppCodeStyleSettingsWidget

CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent)
    : QWidget(parent),
Jarek Kobus's avatar
Jarek Kobus committed
267
      m_preferences(0),
268 269
      m_ui(new Ui::CppCodeStyleSettingsPage),
      m_blockUpdates(false)
270 271
{
    m_ui->setupUi(this);
272
    m_ui->categoryTab->setProperty("_q_custom_style_disabled", true);
273 274 275

    m_previews << m_ui->previewTextEditGeneral << m_ui->previewTextEditContent
               << m_ui->previewTextEditBraces << m_ui->previewTextEditSwitch
276
               << m_ui->previewTextEditPadding << m_ui->previewTextEditPointerReferences;
277 278
    for (int i = 0; i < m_previews.size(); ++i)
        m_previews[i]->setPlainText(QLatin1String(defaultCodeStyleSnippets[i]));
279

280 281
    decorateEditors(TextEditorSettings::fontSettings());
    connect(TextEditorSettings::instance(), SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
282
       this, SLOT(decorateEditors(TextEditor::FontSettings)));
283 284 285

    setVisualizeWhitespace(true);

286
    connect(m_ui->tabSettingsWidget, &TabSettingsWidget::settingsChanged,
Montel Laurent's avatar
Montel Laurent committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
            this, &CppCodeStylePreferencesWidget::slotTabSettingsChanged);
    connect(m_ui->indentBlockBraces, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentBlockBody, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentClassBraces, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentNamespaceBraces, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentEnumBraces, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentNamespaceBody, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentSwitchLabels, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentCaseStatements, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentCaseBlocks, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentCaseBreak, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentAccessSpecifiers, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentDeclarationsRelativeToAccessSpecifiers, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentFunctionBody, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->indentFunctionBraces, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->extraPaddingConditions, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->alignAssignments, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->bindStarToIdentifier, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->bindStarToTypeName, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->bindStarToLeftSpecifier, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
    connect(m_ui->bindStarToRightSpecifier, &QCheckBox::toggled,
            this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
328 329 330

    m_ui->categoryTab->setCurrentIndex(0);

Jarek Kobus's avatar
Jarek Kobus committed
331
    m_ui->tabSettingsWidget->setFlat(true);
332 333 334 335 336 337 338
}

CppCodeStylePreferencesWidget::~CppCodeStylePreferencesWidget()
{
    delete m_ui;
}

Jarek Kobus's avatar
Jarek Kobus committed
339
void CppCodeStylePreferencesWidget::setCodeStyle(CppTools::CppCodeStylePreferences *codeStylePreferences)
340
{
341
    // code preferences
Jarek Kobus's avatar
Jarek Kobus committed
342
    m_preferences = codeStylePreferences;
343

Montel Laurent's avatar
Montel Laurent committed
344 345
    connect(m_preferences, &CppCodeStylePreferences::currentTabSettingsChanged,
            this, &CppCodeStylePreferencesWidget::setTabSettings);
Jarek Kobus's avatar
Jarek Kobus committed
346 347 348 349
    connect(m_preferences, SIGNAL(currentCodeStyleSettingsChanged(CppTools::CppCodeStyleSettings)),
            this, SLOT(setCodeStyleSettings(CppTools::CppCodeStyleSettings)));
    connect(m_preferences, SIGNAL(currentPreferencesChanged(TextEditor::ICodeStylePreferences*)),
            this, SLOT(slotCurrentPreferencesChanged(TextEditor::ICodeStylePreferences*)));
350

Jarek Kobus's avatar
Jarek Kobus committed
351 352 353
    setTabSettings(m_preferences->tabSettings());
    setCodeStyleSettings(m_preferences->codeStyleSettings(), false);
    slotCurrentPreferencesChanged(m_preferences->currentPreferences(), false);
354

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    updatePreview();
}

CppCodeStyleSettings CppCodeStylePreferencesWidget::cppCodeStyleSettings() const
{
    CppCodeStyleSettings set;

    set.indentBlockBraces = m_ui->indentBlockBraces->isChecked();
    set.indentBlockBody = m_ui->indentBlockBody->isChecked();
    set.indentClassBraces = m_ui->indentClassBraces->isChecked();
    set.indentEnumBraces = m_ui->indentEnumBraces->isChecked();
    set.indentNamespaceBraces = m_ui->indentNamespaceBraces->isChecked();
    set.indentNamespaceBody = m_ui->indentNamespaceBody->isChecked();
    set.indentAccessSpecifiers = m_ui->indentAccessSpecifiers->isChecked();
    set.indentDeclarationsRelativeToAccessSpecifiers = m_ui->indentDeclarationsRelativeToAccessSpecifiers->isChecked();
    set.indentFunctionBody = m_ui->indentFunctionBody->isChecked();
    set.indentFunctionBraces = m_ui->indentFunctionBraces->isChecked();
    set.indentSwitchLabels = m_ui->indentSwitchLabels->isChecked();
    set.indentStatementsRelativeToSwitchLabels = m_ui->indentCaseStatements->isChecked();
    set.indentBlocksRelativeToSwitchLabels = m_ui->indentCaseBlocks->isChecked();
    set.indentControlFlowRelativeToSwitchLabels = m_ui->indentCaseBreak->isChecked();
376 377 378 379
    set.bindStarToIdentifier = m_ui->bindStarToIdentifier->isChecked();
    set.bindStarToTypeName = m_ui->bindStarToTypeName->isChecked();
    set.bindStarToLeftSpecifier = m_ui->bindStarToLeftSpecifier->isChecked();
    set.bindStarToRightSpecifier = m_ui->bindStarToRightSpecifier->isChecked();
380 381 382 383 384 385
    set.extraPaddingForConditionsIfConfusingAlign = m_ui->extraPaddingConditions->isChecked();
    set.alignAssignments = m_ui->alignAssignments->isChecked();

    return set;
}

386
void CppCodeStylePreferencesWidget::setTabSettings(const TabSettings &settings)
Jarek Kobus's avatar
Jarek Kobus committed
387 388 389 390 391
{
    m_ui->tabSettingsWidget->setTabSettings(settings);
}

void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSettings &s, bool preview)
392
{
393 394
    const bool wasBlocked = m_blockUpdates;
    m_blockUpdates = true;
395 396 397 398 399 400 401 402 403 404 405 406 407 408
    m_ui->indentBlockBraces->setChecked(s.indentBlockBraces);
    m_ui->indentBlockBody->setChecked(s.indentBlockBody);
    m_ui->indentClassBraces->setChecked(s.indentClassBraces);
    m_ui->indentEnumBraces->setChecked(s.indentEnumBraces);
    m_ui->indentNamespaceBraces->setChecked(s.indentNamespaceBraces);
    m_ui->indentNamespaceBody->setChecked(s.indentNamespaceBody);
    m_ui->indentAccessSpecifiers->setChecked(s.indentAccessSpecifiers);
    m_ui->indentDeclarationsRelativeToAccessSpecifiers->setChecked(s.indentDeclarationsRelativeToAccessSpecifiers);
    m_ui->indentFunctionBody->setChecked(s.indentFunctionBody);
    m_ui->indentFunctionBraces->setChecked(s.indentFunctionBraces);
    m_ui->indentSwitchLabels->setChecked(s.indentSwitchLabels);
    m_ui->indentCaseStatements->setChecked(s.indentStatementsRelativeToSwitchLabels);
    m_ui->indentCaseBlocks->setChecked(s.indentBlocksRelativeToSwitchLabels);
    m_ui->indentCaseBreak->setChecked(s.indentControlFlowRelativeToSwitchLabels);
409 410 411 412
    m_ui->bindStarToIdentifier->setChecked(s.bindStarToIdentifier);
    m_ui->bindStarToTypeName->setChecked(s.bindStarToTypeName);
    m_ui->bindStarToLeftSpecifier->setChecked(s.bindStarToLeftSpecifier);
    m_ui->bindStarToRightSpecifier->setChecked(s.bindStarToRightSpecifier);
413 414
    m_ui->extraPaddingConditions->setChecked(s.extraPaddingForConditionsIfConfusingAlign);
    m_ui->alignAssignments->setChecked(s.alignAssignments);
415 416 417
    m_blockUpdates = wasBlocked;
    if (preview)
        updatePreview();
418 419
}

420
void CppCodeStylePreferencesWidget::slotCurrentPreferencesChanged(ICodeStylePreferences *preferences, bool preview)
421
{
Jarek Kobus's avatar
Jarek Kobus committed
422 423
    const bool enable = !preferences->isReadOnly() && !m_preferences->currentDelegate();
    m_ui->tabSettingsWidget->setEnabled(enable);
424 425 426 427
    m_ui->contentGroupBox->setEnabled(enable);
    m_ui->bracesGroupBox->setEnabled(enable);
    m_ui->switchGroupBox->setEnabled(enable);
    m_ui->alignmentGroupBox->setEnabled(enable);
428
    m_ui->pointerReferencesGroupBox->setEnabled(enable);
429 430
    if (preview)
        updatePreview();
431 432
}

Jarek Kobus's avatar
Jarek Kobus committed
433
void CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged()
434
{
435 436
    if (m_blockUpdates)
        return;
437

Jarek Kobus's avatar
Jarek Kobus committed
438 439
    if (m_preferences) {
        CppCodeStylePreferences *current = qobject_cast<CppCodeStylePreferences *>(m_preferences->currentPreferences());
440
        if (current)
Jarek Kobus's avatar
Jarek Kobus committed
441 442 443 444 445 446
            current->setCodeStyleSettings(cppCodeStyleSettings());
    }

    updatePreview();
}

447
void CppCodeStylePreferencesWidget::slotTabSettingsChanged(const TabSettings &settings)
Jarek Kobus's avatar
Jarek Kobus committed
448 449 450 451 452 453 454 455
{
    if (m_blockUpdates)
        return;

    if (m_preferences) {
        CppCodeStylePreferences *current = qobject_cast<CppCodeStylePreferences *>(m_preferences->currentPreferences());
        if (current)
            current->setTabSettings(settings);
456 457
    }

458 459 460 461 462
    updatePreview();
}

void CppCodeStylePreferencesWidget::updatePreview()
{
Jarek Kobus's avatar
Jarek Kobus committed
463 464 465 466
    CppCodeStylePreferences *cppCodeStylePreferences = m_preferences
            ? m_preferences
            : CppToolsSettings::instance()->cppCodeStyle();
    const CppCodeStyleSettings ccss = cppCodeStylePreferences->currentCodeStyleSettings();
467
    const TabSettings ts = cppCodeStylePreferences->currentTabSettings();
Jarek Kobus's avatar
Jarek Kobus committed
468
    QtStyleCodeFormatter formatter(ts, ccss);
469
    foreach (SnippetEditorWidget *preview, m_previews) {
470
        preview->textDocument()->setTabSettings(ts);
Jarek Kobus's avatar
Jarek Kobus committed
471 472 473
        preview->setCodeStyle(cppCodeStylePreferences);

        QTextDocument *doc = preview->document();
474 475 476 477 478 479
        formatter.invalidateCache(doc);

        QTextBlock block = doc->firstBlock();
        QTextCursor tc = preview->textCursor();
        tc.beginEditBlock();
        while (block.isValid()) {
480
            preview->textDocument()->indenter()->indentBlock(doc, block, QChar::Null, ts);
481 482 483

            block = block.next();
        }
484
        applyRefactorings(doc, preview, ccss);
485 486 487 488
        tc.endEditBlock();
    }
}

489
void CppCodeStylePreferencesWidget::decorateEditors(const FontSettings &fontSettings)
490
{
491 492 493 494
    const ISnippetProvider *provider = ExtensionSystem::PluginManager::getObject<ISnippetProvider>(
        [](ISnippetProvider *current) {
            return current->groupId() == QLatin1String(CppEditor::Constants::CPP_SNIPPETS_GROUP_ID);
        });
495

496
    foreach (SnippetEditorWidget *editor, m_previews) {
497
        editor->textDocument()->setFontSettings(fontSettings);
498 499 500
        if (provider)
            provider->decorateEditor(editor);
    }
501 502 503 504
}

void CppCodeStylePreferencesWidget::setVisualizeWhitespace(bool on)
{
505
    foreach (SnippetEditorWidget *editor, m_previews) {
506 507 508 509 510 511 512 513 514
        DisplaySettings displaySettings = editor->displaySettings();
        displaySettings.m_visualizeWhitespace = on;
        editor->setDisplaySettings(displaySettings);
    }
}


// ------------------ CppCodeStyleSettingsPage

515
CppCodeStyleSettingsPage::CppCodeStyleSettingsPage(QWidget *parent) :
516
    Core::IOptionsPage(parent),
Jarek Kobus's avatar
Jarek Kobus committed
517
    m_pageCppCodeStylePreferences(0)
518
{
hjk's avatar
hjk committed
519
    setId(Constants::CPP_CODE_STYLE_SETTINGS_ID);
520
    setDisplayName(QCoreApplication::translate("CppTools", Constants::CPP_CODE_STYLE_SETTINGS_NAME));
hjk's avatar
hjk committed
521
    setCategory(Constants::CPP_SETTINGS_CATEGORY);
522 523
    setDisplayCategory(QCoreApplication::translate("CppTools", Constants::CPP_SETTINGS_TR_CATEGORY));
    setCategoryIcon(QLatin1String(Constants::SETTINGS_CATEGORY_CPP_ICON));
524 525
}

526
QWidget *CppCodeStyleSettingsPage::widget()
527
{
528 529 530 531 532 533 534 535 536 537 538 539
    if (!m_widget) {
        CppCodeStylePreferences *originalCodeStylePreferences
                = CppToolsSettings::instance()->cppCodeStyle();
        m_pageCppCodeStylePreferences = new CppCodeStylePreferences(m_widget);
        m_pageCppCodeStylePreferences->setDelegatingPool(originalCodeStylePreferences->delegatingPool());
        m_pageCppCodeStylePreferences->setCodeStyleSettings(originalCodeStylePreferences->codeStyleSettings());
        m_pageCppCodeStylePreferences->setCurrentDelegate(originalCodeStylePreferences->currentDelegate());
        // we set id so that it won't be possible to set delegate to the original prefs
        m_pageCppCodeStylePreferences->setId(originalCodeStylePreferences->id());
        m_widget = new CodeStyleEditor(TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID),
                                       m_pageCppCodeStylePreferences);
    }
540 541 542 543 544 545
    return m_widget;
}

void CppCodeStyleSettingsPage::apply()
{
    if (m_widget) {
hjk's avatar
hjk committed
546
        QSettings *s = Core::ICore::settings();
547

Jarek Kobus's avatar
Jarek Kobus committed
548 549 550
        CppCodeStylePreferences *originalCppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
        if (originalCppCodeStylePreferences->codeStyleSettings() != m_pageCppCodeStylePreferences->codeStyleSettings()) {
            originalCppCodeStylePreferences->setCodeStyleSettings(m_pageCppCodeStylePreferences->codeStyleSettings());
hjk's avatar
hjk committed
551
            originalCppCodeStylePreferences->toSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s);
552
        }
Jarek Kobus's avatar
Jarek Kobus committed
553 554
        if (originalCppCodeStylePreferences->tabSettings() != m_pageCppCodeStylePreferences->tabSettings()) {
            originalCppCodeStylePreferences->setTabSettings(m_pageCppCodeStylePreferences->tabSettings());
hjk's avatar
hjk committed
555
            originalCppCodeStylePreferences->toSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s);
556
        }
Jarek Kobus's avatar
Jarek Kobus committed
557 558
        if (originalCppCodeStylePreferences->currentDelegate() != m_pageCppCodeStylePreferences->currentDelegate()) {
            originalCppCodeStylePreferences->setCurrentDelegate(m_pageCppCodeStylePreferences->currentDelegate());
hjk's avatar
hjk committed
559
            originalCppCodeStylePreferences->toSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s);
560 561 562 563
        }
    }
}

564
void CppCodeStyleSettingsPage::finish()
565
{
566
    delete m_widget;
567 568 569 570
}

} // namespace Internal
} // namespace CppTools