cppcodestylesettingspage.cpp 20 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Christian Kamm's avatar
Christian Kamm committed
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
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 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Christian Kamm's avatar
Christian Kamm committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Christian Kamm's avatar
Christian Kamm committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Christian Kamm's avatar
Christian Kamm committed
29

30 31 32 33 34 35 36 37 38 39 40
#include "cppcodestylesettingspage.h"
#include "cppcodestylepreferences.h"
#include "ui_cppcodestylesettingspage.h"
#include "cpptoolsconstants.h"
#include "cpptoolssettings.h"
#include "cppqtstyleindenter.h"
#include <texteditor/snippets/isnippetprovider.h>
#include <texteditor/fontsettings.h>
#include <texteditor/displaysettings.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
Jarek Kobus's avatar
Jarek Kobus committed
41
#include <texteditor/codestyleeditor.h>
42 43 44
#include <extensionsystem/pluginmanager.h>
#include <cppeditor/cppeditorconstants.h>
#include <coreplugin/icore.h>
45 46
#include <QTextBlock>
#include <QTextStream>
47 48 49 50 51 52 53 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 204 205 206 207 208

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"
};

using namespace TextEditor;

namespace CppTools {

namespace Internal {

// ------------------ CppCodeStyleSettingsWidget

CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent)
    : QWidget(parent),
Jarek Kobus's avatar
Jarek Kobus committed
209
      m_preferences(0),
210 211
      m_ui(new Ui::CppCodeStyleSettingsPage),
      m_blockUpdates(false)
212 213
{
    m_ui->setupUi(this);
214
    m_ui->categoryTab->setProperty("_q_custom_style_disabled", true);
215 216 217 218 219 220 221 222

    m_previews << m_ui->previewTextEditGeneral << m_ui->previewTextEditContent
               << m_ui->previewTextEditBraces << m_ui->previewTextEditSwitch
               << m_ui->previewTextEditPadding;
    for (int i = 0; i < m_previews.size(); ++i) {
        m_previews[i]->setPlainText(defaultCodeStyleSnippets[i]);
    }

Jarek Kobus's avatar
Jarek Kobus committed
223 224 225
    TextEditor::TextEditorSettings *textEditorSettings = TextEditorSettings::instance();
    decorateEditors(textEditorSettings->fontSettings());
    connect(textEditorSettings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
226
       this, SLOT(decorateEditors(TextEditor::FontSettings)));
227 228 229

    setVisualizeWhitespace(true);

Jarek Kobus's avatar
Jarek Kobus committed
230 231
    connect(m_ui->tabSettingsWidget, SIGNAL(settingsChanged(TextEditor::TabSettings)),
       this, SLOT(slotTabSettingsChanged(TextEditor::TabSettings)));
232
    connect(m_ui->indentBlockBraces, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
233
       this, SLOT(slotCodeStyleSettingsChanged()));
234
    connect(m_ui->indentBlockBody, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
235
       this, SLOT(slotCodeStyleSettingsChanged()));
236
    connect(m_ui->indentClassBraces, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
237
       this, SLOT(slotCodeStyleSettingsChanged()));
238
    connect(m_ui->indentNamespaceBraces, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
239
       this, SLOT(slotCodeStyleSettingsChanged()));
240
    connect(m_ui->indentEnumBraces, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
241
       this, SLOT(slotCodeStyleSettingsChanged()));
242
    connect(m_ui->indentNamespaceBody, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
243
       this, SLOT(slotCodeStyleSettingsChanged()));
244
    connect(m_ui->indentSwitchLabels, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
245
       this, SLOT(slotCodeStyleSettingsChanged()));
246
    connect(m_ui->indentCaseStatements, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
247
       this, SLOT(slotCodeStyleSettingsChanged()));
248
    connect(m_ui->indentCaseBlocks, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
249
       this, SLOT(slotCodeStyleSettingsChanged()));
250
    connect(m_ui->indentCaseBreak, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
251
       this, SLOT(slotCodeStyleSettingsChanged()));
252
    connect(m_ui->indentAccessSpecifiers, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
253
       this, SLOT(slotCodeStyleSettingsChanged()));
254
    connect(m_ui->indentDeclarationsRelativeToAccessSpecifiers, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
255
       this, SLOT(slotCodeStyleSettingsChanged()));
256
    connect(m_ui->indentFunctionBody, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
257
       this, SLOT(slotCodeStyleSettingsChanged()));
258
    connect(m_ui->indentFunctionBraces, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
259
       this, SLOT(slotCodeStyleSettingsChanged()));
260
    connect(m_ui->extraPaddingConditions, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
261
       this, SLOT(slotCodeStyleSettingsChanged()));
262
    connect(m_ui->alignAssignments, SIGNAL(toggled(bool)),
Jarek Kobus's avatar
Jarek Kobus committed
263
       this, SLOT(slotCodeStyleSettingsChanged()));
264 265 266

    m_ui->categoryTab->setCurrentIndex(0);

Jarek Kobus's avatar
Jarek Kobus committed
267
    m_ui->tabSettingsWidget->setFlat(true);
268 269 270 271 272 273 274
}

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

Jarek Kobus's avatar
Jarek Kobus committed
275
void CppCodeStylePreferencesWidget::setCodeStyle(CppTools::CppCodeStylePreferences *codeStylePreferences)
276
{
277
    // code preferences
Jarek Kobus's avatar
Jarek Kobus committed
278
    m_preferences = codeStylePreferences;
279

Jarek Kobus's avatar
Jarek Kobus committed
280 281 282 283 284 285
    connect(m_preferences, SIGNAL(currentTabSettingsChanged(TextEditor::TabSettings)),
            this, SLOT(setTabSettings(TextEditor::TabSettings)));
    connect(m_preferences, SIGNAL(currentCodeStyleSettingsChanged(CppTools::CppCodeStyleSettings)),
            this, SLOT(setCodeStyleSettings(CppTools::CppCodeStyleSettings)));
    connect(m_preferences, SIGNAL(currentPreferencesChanged(TextEditor::ICodeStylePreferences*)),
            this, SLOT(slotCurrentPreferencesChanged(TextEditor::ICodeStylePreferences*)));
286

Jarek Kobus's avatar
Jarek Kobus committed
287 288 289
    setTabSettings(m_preferences->tabSettings());
    setCodeStyleSettings(m_preferences->codeStyleSettings(), false);
    slotCurrentPreferencesChanged(m_preferences->currentPreferences(), false);
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
    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();
    set.extraPaddingForConditionsIfConfusingAlign = m_ui->extraPaddingConditions->isChecked();
    set.alignAssignments = m_ui->alignAssignments->isChecked();

    return set;
}

Jarek Kobus's avatar
Jarek Kobus committed
318 319 320 321 322 323
void CppCodeStylePreferencesWidget::setTabSettings(const TextEditor::TabSettings &settings)
{
    m_ui->tabSettingsWidget->setTabSettings(settings);
}

void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSettings &s, bool preview)
324
{
325 326
    const bool wasBlocked = m_blockUpdates;
    m_blockUpdates = true;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    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);
    m_ui->extraPaddingConditions->setChecked(s.extraPaddingForConditionsIfConfusingAlign);
    m_ui->alignAssignments->setChecked(s.alignAssignments);
343 344 345
    m_blockUpdates = wasBlocked;
    if (preview)
        updatePreview();
346 347
}

Jarek Kobus's avatar
Jarek Kobus committed
348
void CppCodeStylePreferencesWidget::slotCurrentPreferencesChanged(TextEditor::ICodeStylePreferences *preferences, bool preview)
349
{
Jarek Kobus's avatar
Jarek Kobus committed
350 351
    const bool enable = !preferences->isReadOnly() && !m_preferences->currentDelegate();
    m_ui->tabSettingsWidget->setEnabled(enable);
352 353 354 355
    m_ui->contentGroupBox->setEnabled(enable);
    m_ui->bracesGroupBox->setEnabled(enable);
    m_ui->switchGroupBox->setEnabled(enable);
    m_ui->alignmentGroupBox->setEnabled(enable);
356 357
    if (preview)
        updatePreview();
358 359 360 361 362 363 364
}

QString CppCodeStylePreferencesWidget::searchKeywords() const
{
    QString rc;
    QLatin1Char sep(' ');
    QTextStream(&rc)
Jarek Kobus's avatar
Jarek Kobus committed
365
       << sep << m_ui->tabSettingsWidget->searchKeywords()
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
       << sep << m_ui->indentBlockBraces->text()
       << sep << m_ui->indentBlockBody->text()
       << sep << m_ui->indentClassBraces->text()
       << sep << m_ui->indentEnumBraces->text()
       << sep << m_ui->indentNamespaceBraces->text()
       << sep << m_ui->indentNamespaceBody->text()
       << sep << m_ui->indentAccessSpecifiers->text()
       << sep << m_ui->indentDeclarationsRelativeToAccessSpecifiers->text()
       << sep << m_ui->indentFunctionBody->text()
       << sep << m_ui->indentFunctionBraces->text()
       << sep << m_ui->indentSwitchLabels->text()
       << sep << m_ui->indentCaseStatements->text()
       << sep << m_ui->indentCaseBlocks->text()
       << sep << m_ui->indentCaseBreak->text()
       << sep << m_ui->contentGroupBox->title()
       << sep << m_ui->bracesGroupBox->title()
       << sep << m_ui->switchGroupBox->title()
       << sep << m_ui->alignmentGroupBox->title()
       << sep << m_ui->extraPaddingConditions->text()
       << sep << m_ui->alignAssignments->text()
          ;
    for (int i = 0; i < m_ui->categoryTab->count(); i++)
        QTextStream(&rc) << sep << m_ui->categoryTab->tabText(i);
    rc.remove(QLatin1Char('&'));
    return rc;
}

Jarek Kobus's avatar
Jarek Kobus committed
393
void CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged()
394
{
395 396
    if (m_blockUpdates)
        return;
397

Jarek Kobus's avatar
Jarek Kobus committed
398 399
    if (m_preferences) {
        CppCodeStylePreferences *current = qobject_cast<CppCodeStylePreferences *>(m_preferences->currentPreferences());
400
        if (current)
Jarek Kobus's avatar
Jarek Kobus committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
            current->setCodeStyleSettings(cppCodeStyleSettings());
    }

    updatePreview();
}

void CppCodeStylePreferencesWidget::slotTabSettingsChanged(const TextEditor::TabSettings &settings)
{
    if (m_blockUpdates)
        return;

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

418 419 420 421 422
    updatePreview();
}

void CppCodeStylePreferencesWidget::updatePreview()
{
Jarek Kobus's avatar
Jarek Kobus committed
423 424 425 426 427 428
    CppCodeStylePreferences *cppCodeStylePreferences = m_preferences
            ? m_preferences
            : CppToolsSettings::instance()->cppCodeStyle();
    const CppCodeStyleSettings ccss = cppCodeStylePreferences->currentCodeStyleSettings();
    const TextEditor::TabSettings ts = cppCodeStylePreferences->currentTabSettings();
    QtStyleCodeFormatter formatter(ts, ccss);
429 430
    foreach (TextEditor::SnippetEditorWidget *preview, m_previews) {
        preview->setTabSettings(ts);
Jarek Kobus's avatar
Jarek Kobus committed
431 432 433
        preview->setCodeStyle(cppCodeStylePreferences);

        QTextDocument *doc = preview->document();
434 435 436 437 438 439
        formatter.invalidateCache(doc);

        QTextBlock block = doc->firstBlock();
        QTextCursor tc = preview->textCursor();
        tc.beginEditBlock();
        while (block.isValid()) {
Jarek Kobus's avatar
Jarek Kobus committed
440
            preview->indenter()->indentBlock(doc, block, QChar::Null, ts);
441 442 443 444 445 446 447

            block = block.next();
        }
        tc.endEditBlock();
    }
}

448
void CppCodeStylePreferencesWidget::decorateEditors(const TextEditor::FontSettings &fontSettings)
449
{
450 451
    const ISnippetProvider *provider = 0;
    const QList<ISnippetProvider *> &providers =
452
        ExtensionSystem::PluginManager::getObjects<ISnippetProvider>();
453 454 455 456 457 458 459 460 461 462 463 464
    foreach (const ISnippetProvider *current, providers) {
        if (current->groupId() == QLatin1String(CppEditor::Constants::CPP_SNIPPETS_GROUP_ID)) {
            provider = current;
            break;
        }
    }

    foreach (TextEditor::SnippetEditorWidget *editor, m_previews) {
        editor->setFontSettings(fontSettings);
        if (provider)
            provider->decorateEditor(editor);
    }
465 466 467 468 469 470 471 472 473 474 475 476 477 478
}

void CppCodeStylePreferencesWidget::setVisualizeWhitespace(bool on)
{
    foreach (TextEditor::SnippetEditorWidget *editor, m_previews) {
        DisplaySettings displaySettings = editor->displaySettings();
        displaySettings.m_visualizeWhitespace = on;
        editor->setDisplaySettings(displaySettings);
    }
}


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

479
CppCodeStyleSettingsPage::CppCodeStyleSettingsPage(QWidget *parent) :
480
    Core::IOptionsPage(parent),
Jarek Kobus's avatar
Jarek Kobus committed
481
    m_pageCppCodeStylePreferences(0)
482
{
483 484 485 486 487
    setId(QLatin1String(Constants::CPP_CODE_STYLE_SETTINGS_ID));
    setDisplayName(QCoreApplication::translate("CppTools", Constants::CPP_CODE_STYLE_SETTINGS_NAME));
    setCategory(QLatin1String(Constants::CPP_SETTINGS_CATEGORY));
    setDisplayCategory(QCoreApplication::translate("CppTools", Constants::CPP_SETTINGS_TR_CATEGORY));
    setCategoryIcon(QLatin1String(Constants::SETTINGS_CATEGORY_CPP_ICON));
488 489 490 491 492
}

QWidget *CppCodeStyleSettingsPage::createPage(QWidget *parent)
{
    CppCodeStylePreferences *originalCodeStylePreferences
Jarek Kobus's avatar
Jarek Kobus committed
493 494 495 496 497 498 499 500 501 502
            = 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());
    TextEditorSettings *settings = TextEditorSettings::instance();
    m_widget = new CodeStyleEditor(settings->codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID),
                                   m_pageCppCodeStylePreferences, parent);
503 504 505 506 507 508 509

    return m_widget;
}

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

Jarek Kobus's avatar
Jarek Kobus committed
512 513 514
        CppCodeStylePreferences *originalCppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
        if (originalCppCodeStylePreferences->codeStyleSettings() != m_pageCppCodeStylePreferences->codeStyleSettings()) {
            originalCppCodeStylePreferences->setCodeStyleSettings(m_pageCppCodeStylePreferences->codeStyleSettings());
515
            if (s)
Jarek Kobus's avatar
Jarek Kobus committed
516
                originalCppCodeStylePreferences->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
517
        }
Jarek Kobus's avatar
Jarek Kobus committed
518 519
        if (originalCppCodeStylePreferences->tabSettings() != m_pageCppCodeStylePreferences->tabSettings()) {
            originalCppCodeStylePreferences->setTabSettings(m_pageCppCodeStylePreferences->tabSettings());
520 521 522
            if (s)
                originalCppCodeStylePreferences->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
        }
Jarek Kobus's avatar
Jarek Kobus committed
523 524
        if (originalCppCodeStylePreferences->currentDelegate() != m_pageCppCodeStylePreferences->currentDelegate()) {
            originalCppCodeStylePreferences->setCurrentDelegate(m_pageCppCodeStylePreferences->currentDelegate());
525 526 527 528 529 530 531 532 533 534 535 536 537
            if (s)
                originalCppCodeStylePreferences->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
        }
    }
}

bool CppCodeStyleSettingsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

} // namespace Internal
} // namespace CppTools