summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor/behaviorsettingspage.cpp
blob: 9595415fb09268efb1ebcc0ea007f5318e0074f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
209
210
211
212
213
214
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "behaviorsettingspage.h"

#include "behaviorsettings.h"
#include "storagesettings.h"
#include "tabsettings.h"
#include "ui_behaviorsettingspage.h"

#include <coreplugin/icore.h>

#include <QtCore/QSettings>
#include <QtCore/QTextStream>

using namespace TextEditor;

struct BehaviorSettingsPage::BehaviorSettingsPagePrivate
{
    explicit BehaviorSettingsPagePrivate(const BehaviorSettingsPageParameters &p);

    const BehaviorSettingsPageParameters m_parameters;
    Ui::BehaviorSettingsPage m_page;

    TabSettings m_tabSettings;
    StorageSettings m_storageSettings;
    BehaviorSettings m_behaviorSettings;

    QString m_searchKeywords;
};

BehaviorSettingsPage::BehaviorSettingsPagePrivate::BehaviorSettingsPagePrivate
    (const BehaviorSettingsPageParameters &p)
  : m_parameters(p)
{
    if (const QSettings *s = Core::ICore::instance()->settings()) {
        m_tabSettings.fromSettings(m_parameters.settingsPrefix, s);
        m_storageSettings.fromSettings(m_parameters.settingsPrefix, s);
        m_behaviorSettings.fromSettings(m_parameters.settingsPrefix, s);
    }
}

BehaviorSettingsPage::BehaviorSettingsPage(const BehaviorSettingsPageParameters &p,
                                           QObject *parent)
  : TextEditorOptionsPage(parent),
    m_d(new BehaviorSettingsPagePrivate(p))
{
}

BehaviorSettingsPage::~BehaviorSettingsPage()
{
    delete m_d;
}

QString BehaviorSettingsPage::id() const
{
    return m_d->m_parameters.id;
}

QString BehaviorSettingsPage::displayName() const
{
    return m_d->m_parameters.displayName;
}

QWidget *BehaviorSettingsPage::createPage(QWidget *parent)
{
    QWidget *w = new QWidget(parent);
    m_d->m_page.setupUi(w);
    settingsToUI();
    if (m_d->m_searchKeywords.isEmpty()) {
        QTextStream(&m_d->m_searchKeywords) << m_d->m_page.insertSpaces->text()
          << ' ' << m_d->m_page.smartBackspace->text()
          << ' ' << m_d->m_page.cleanWhitespace->text()
          << ' ' << m_d->m_page.addFinalNewLine->text()
          << ' ' << m_d->m_page.mouseNavigation->text()
          << ' ' << m_d->m_page.scrollWheelZooming->text()
          << ' ' << m_d->m_page.groupBoxTabAndIndentSettings->title()
          << ' ' << m_d->m_page.groupBoxStorageSettings->title()
          << ' ' << m_d->m_page.groupBoxMouse->title();
        m_d->m_searchKeywords.remove(QLatin1Char('&'));
    }
    return w;
}

void BehaviorSettingsPage::apply()
{
    TabSettings newTabSettings;
    StorageSettings newStorageSettings;
    BehaviorSettings newBehaviorSettings;

    settingsFromUI(newTabSettings, newStorageSettings, newBehaviorSettings);

    Core::ICore *core = Core::ICore::instance();
    QSettings *s = core->settings();

    if (newTabSettings != m_d->m_tabSettings) {
        m_d->m_tabSettings = newTabSettings;
        if (s)
            m_d->m_tabSettings.toSettings(m_d->m_parameters.settingsPrefix, s);

        emit tabSettingsChanged(newTabSettings);
    }

    if (newStorageSettings != m_d->m_storageSettings) {
        m_d->m_storageSettings = newStorageSettings;
        if (s)
            m_d->m_storageSettings.toSettings(m_d->m_parameters.settingsPrefix, s);

        emit storageSettingsChanged(newStorageSettings);
    }

    if (newBehaviorSettings != m_d->m_behaviorSettings) {
        m_d->m_behaviorSettings = newBehaviorSettings;
        if (s)
            m_d->m_behaviorSettings.toSettings(m_d->m_parameters.settingsPrefix, s);

        emit behaviorSettingsChanged(newBehaviorSettings);
    }
}

void BehaviorSettingsPage::settingsFromUI(TabSettings &tabSettings,
                                          StorageSettings &storageSettings,
                                          BehaviorSettings &behaviorSettings) const
{
    tabSettings.m_spacesForTabs = m_d->m_page.insertSpaces->isChecked();
    tabSettings.m_autoSpacesForTabs = m_d->m_page.autoInsertSpaces->isChecked();
    tabSettings.m_autoIndent = m_d->m_page.autoIndent->isChecked();
    tabSettings.m_smartBackspace = m_d->m_page.smartBackspace->isChecked();
    tabSettings.m_tabSize = m_d->m_page.tabSize->value();
    tabSettings.m_indentSize = m_d->m_page.indentSize->value();
    tabSettings.m_indentBraces = m_d->m_page.indentBlocksBehavior->currentIndex() >= 1;
    tabSettings.m_doubleIndentBlocks = m_d->m_page.indentBlocksBehavior->currentIndex() >= 2;

    tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)m_d->m_page.tabKeyBehavior->currentIndex();

    storageSettings.m_cleanWhitespace = m_d->m_page.cleanWhitespace->isChecked();
    storageSettings.m_inEntireDocument = m_d->m_page.inEntireDocument->isChecked();
    storageSettings.m_cleanIndentation = m_d->m_page.cleanIndentation->isChecked();
    storageSettings.m_addFinalNewLine = m_d->m_page.addFinalNewLine->isChecked();

    behaviorSettings.m_mouseNavigation = m_d->m_page.mouseNavigation->isChecked();
    behaviorSettings.m_scrollWheelZooming = m_d->m_page.scrollWheelZooming->isChecked();
}

void BehaviorSettingsPage::settingsToUI()
{
    const TabSettings &tabSettings = m_d->m_tabSettings;
    m_d->m_page.insertSpaces->setChecked(tabSettings.m_spacesForTabs);
    m_d->m_page.autoInsertSpaces->setChecked(tabSettings.m_autoSpacesForTabs);
    m_d->m_page.autoIndent->setChecked(tabSettings.m_autoIndent);
    m_d->m_page.smartBackspace->setChecked(tabSettings.m_smartBackspace);
    m_d->m_page.tabSize->setValue(tabSettings.m_tabSize);
    m_d->m_page.indentSize->setValue(tabSettings.m_indentSize);
    m_d->m_page.indentBlocksBehavior->setCurrentIndex(tabSettings.m_indentBraces ?
                                                      (tabSettings.m_doubleIndentBlocks ? 2 : 1)
                                                        : 0);
    m_d->m_page.tabKeyBehavior->setCurrentIndex(tabSettings.m_tabKeyBehavior);

    const StorageSettings &storageSettings = m_d->m_storageSettings;
    m_d->m_page.cleanWhitespace->setChecked(storageSettings.m_cleanWhitespace);
    m_d->m_page.inEntireDocument->setChecked(storageSettings.m_inEntireDocument);
    m_d->m_page.cleanIndentation->setChecked(storageSettings.m_cleanIndentation);
    m_d->m_page.addFinalNewLine->setChecked(storageSettings.m_addFinalNewLine);

    const BehaviorSettings &behaviorSettings = m_d->m_behaviorSettings;
    m_d->m_page.mouseNavigation->setChecked(behaviorSettings.m_mouseNavigation);
    m_d->m_page.scrollWheelZooming->setChecked(behaviorSettings.m_scrollWheelZooming);
}

const TabSettings &BehaviorSettingsPage::tabSettings() const
{
    return m_d->m_tabSettings;
}

const StorageSettings &BehaviorSettingsPage::storageSettings() const
{
    return m_d->m_storageSettings;
}

const BehaviorSettings &BehaviorSettingsPage::behaviorSettings() const
{
    return m_d->m_behaviorSettings;
}

bool BehaviorSettingsPage::matches(const QString &s) const
{
   return m_d->m_searchKeywords.contains(s, Qt::CaseInsensitive);
}