generalsettings.cpp 9.95 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con 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
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con 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.
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
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "generalsettings.h"
31
#include "coreconstants.h"
32
33
34
#include "icore.h"
#include "infobar.h"
#include "editormanager/editormanager.h"
con's avatar
con committed
35

36
37
#include <utils/stylehelper.h>
#include <utils/qtcolorbutton.h>
38
#include <utils/consoleprocess.h>
39
#include <utils/unixutils.h>
40

41
#include <QMessageBox>
con's avatar
con committed
42

43
44
45
46
47
#include <QCoreApplication>
#include <QTextStream>
#include <QDir>
#include <QLibraryInfo>
#include <QSettings>
48

con's avatar
con committed
49
50
#include "ui_generalsettings.h"

51
using namespace Utils;
con's avatar
con committed
52
53
using namespace Core::Internal;

54

55
GeneralSettings::GeneralSettings():
56
    m_page(0), m_dialog(0)
con's avatar
con committed
57
{
hjk's avatar
hjk committed
58
    setId(Core::Constants::SETTINGS_ID_ENVIRONMENT);
59
    setDisplayName(tr("General"));
hjk's avatar
hjk committed
60
    setCategory(Core::Constants::SETTINGS_CATEGORY_CORE);
61
62
    setDisplayCategory(QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE));
    setCategoryIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
63
64
}

65
66
67
68
static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath)
{
    static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);

69
70
    const QString trFile = QLatin1String("/qt_") + locale + QLatin1String(".qm");
    return QFile::exists(qtTrPath + trFile) || QFile::exists(creatorTrPath + trFile);
71
72
73
74
}

void GeneralSettings::fillLanguageBox() const
{
75
76
    const QString currentLocale = language();

77
    m_page->languageBox->addItem(tr("<System Language>"), QString());
78
79
    // need to add this explicitly, since there is no qm file for English
    m_page->languageBox->addItem(QLatin1String("English"), QLatin1String("C"));
80
81
    if (currentLocale == QLatin1String("C"))
        m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
82
83

    const QString creatorTrPath =
hjk's avatar
hjk committed
84
            Core::ICore::resourcePath() + QLatin1String("/translations");
85
    const QStringList languageFiles = QDir(creatorTrPath).entryList(QStringList(QLatin1String("qtcreator*.qm")));
86

87
    Q_FOREACH(const QString &languageFile, languageFiles)
88
    {
89
        int start = languageFile.indexOf(QLatin1Char('_'))+1;
90
91
92
93
        int end = languageFile.lastIndexOf(QLatin1Char('.'));
        const QString locale = languageFile.mid(start, end-start);
        // no need to show a language that creator will not load anyway
        if (hasQmFilesForLocale(locale, creatorTrPath)) {
94
95
96
97
            QLocale tmpLocale(locale);
            QString languageItem = QLocale::languageToString(tmpLocale.language()) + QLatin1String(" (")
                                   + QLocale::countryToString(tmpLocale.country()) + QLatin1Char(')');
            m_page->languageBox->addItem(languageItem, locale);
98
99
100
101
102
103
            if (locale == currentLocale)
                m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
        }
    }
}

104
QWidget *GeneralSettings::createPage(QWidget *parent)
con's avatar
con committed
105
{
106
    m_page = new Ui::GeneralSettings();
107
108
    m_widget = new QWidget(parent);
    m_page->setupUi(m_widget);
con's avatar
con committed
109

hjk's avatar
hjk committed
110
    QSettings *settings = Core::ICore::settings();
111
112
    Q_UNUSED(settings) // Windows

113
    fillLanguageBox();
114

115
    m_page->colorButton->setColor(StyleHelper::requestedBaseColor());
116
    m_page->reloadBehavior->setCurrentIndex(EditorManager::instance()->reloadSetting());
117
#ifdef Q_OS_UNIX
118
119
120
121
    const QStringList availableTerminals = ConsoleProcess::availableTerminalEmulators();
    const QString currentTerminal = ConsoleProcess::terminalEmulator(settings);
    m_page->terminalComboBox->addItems(availableTerminals);
    m_page->terminalComboBox->lineEdit()->setText(currentTerminal);
122
123
#else
    m_page->terminalLabel->hide();
124
    m_page->terminalComboBox->hide();
125
126
    m_page->resetTerminalButton->hide();
#endif
con's avatar
con committed
127

128
129
130
131
132
133
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
    m_page->externalFileBrowserEdit->setText(UnixUtils::fileBrowser(settings));
#else
    m_page->externalFileBrowserLabel->hide();
    m_page->externalFileBrowserEdit->hide();
    m_page->resetFileBrowserButton->hide();
134
    m_page->helpExternalFileBrowserButton->hide();
135
136
#endif

137
138
139
    m_page->autoSaveCheckBox->setChecked(EditorManager::instance()->autoSaveEnabled());
    m_page->autoSaveInterval->setValue(EditorManager::instance()->autoSaveInterval());

140
    connect(m_page->resetColorButton, SIGNAL(clicked()),
con's avatar
con committed
141
            this, SLOT(resetInterfaceColor()));
142
143
    connect(m_page->resetWarningsButton, SIGNAL(clicked()),
            this, SLOT(resetWarnings()));
144
145
146
#ifdef Q_OS_UNIX
    connect(m_page->resetTerminalButton, SIGNAL(clicked()),
            this, SLOT(resetTerminal()));
147
148
149
150
151
152
#ifndef Q_OS_MAC
    connect(m_page->resetFileBrowserButton, SIGNAL(clicked()),
            this, SLOT(resetFileBrowser()));
    connect(m_page->helpExternalFileBrowserButton, SIGNAL(clicked()),
            this, SLOT(showHelpForFileBrowser()));
#endif
153
#endif
154

155
    if (m_searchKeywords.isEmpty()) {
156
157
158
159
160
161
162
163
        QLatin1Char sep(' ');
        QTextStream(&m_searchKeywords)
                << m_page->interfaceBox->title() << sep
                << m_page->colorLabel->text() << sep
                << m_page->languageLabel->text() << sep
                << m_page->systemBox->title() << sep
                << m_page->terminalLabel->text() << sep
                << m_page->modifiedLabel->text();
164
165
        m_searchKeywords.remove(QLatin1Char('&'));
    }
166
    return m_widget;
con's avatar
con committed
167
168
}

169
170
171
172
173
bool GeneralSettings::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

174
void GeneralSettings::apply()
con's avatar
con committed
175
{
176
177
    if (!m_page) // wasn't shown, can't be changed
        return;
178
179
    int currentIndex = m_page->languageBox->currentIndex();
    setLanguage(m_page->languageBox->itemData(currentIndex, Qt::UserRole).toString());
con's avatar
con committed
180
181
    // Apply the new base color if accepted
    StyleHelper::setBaseColor(m_page->colorButton->color());
182
    EditorManager::instance()->setReloadSetting(IDocument::ReloadSetting(m_page->reloadBehavior->currentIndex()));
183
#ifdef Q_OS_UNIX
hjk's avatar
hjk committed
184
    ConsoleProcess::setTerminalEmulator(Core::ICore::settings(),
185
                                        m_page->terminalComboBox->lineEdit()->text());
186
#ifndef Q_OS_MAC
hjk's avatar
hjk committed
187
    Utils::UnixUtils::setFileBrowser(Core::ICore::settings(), m_page->externalFileBrowserEdit->text());
188
#endif
189
#endif
190
191
    EditorManager::instance()->setAutoSaveEnabled(m_page->autoSaveCheckBox->isChecked());
    EditorManager::instance()->setAutoSaveInterval(m_page->autoSaveInterval->value());
con's avatar
con committed
192
193
}

194
195
void GeneralSettings::finish()
{
196
197
    if (!m_page) // page was never shown
        return;
198
    delete m_page;
199
    m_page = 0;
200
201
}

con's avatar
con committed
202
203
void GeneralSettings::resetInterfaceColor()
{
204
    m_page->colorButton->setColor(StyleHelper::DEFAULT_BASE_COLOR);
con's avatar
con committed
205
206
}

207
208
209
210
211
212
void GeneralSettings::resetWarnings()
{
    Core::InfoBar::clearGloballySuppressed();
    QMessageBox::information(0, tr("Reset warnings"), tr("Done"));
}

213
214
void GeneralSettings::resetTerminal()
{
hjk's avatar
hjk committed
215
#if defined(Q_OS_UNIX)
216
    m_page->terminalComboBox->lineEdit()->setText(ConsoleProcess::defaultTerminalEmulator());
hjk's avatar
hjk committed
217
#endif
218
}
219
220
221

void GeneralSettings::resetFileBrowser()
{
hjk's avatar
hjk committed
222
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
223
    m_page->externalFileBrowserEdit->setText(UnixUtils::defaultFileBrowser());
224
#endif
hjk's avatar
hjk committed
225
}
226

227

228
void GeneralSettings::variableHelpDialogCreator(const QString &helpText)
con's avatar
con committed
229
230
{
    if (m_dialog) {
231
232
233
        if (m_dialog->text() != helpText)
            m_dialog->setText(helpText);

con's avatar
con committed
234
235
236
237
238
239
240
        m_dialog->show();
        m_dialog->raise();
        m_dialog->activateWindow();
        return;
    }
    QMessageBox *mb = new QMessageBox(QMessageBox::Information,
                                  tr("Variables"),
241
242
                                  helpText,
                                  QMessageBox::Close,
243
                                  m_widget);
con's avatar
con committed
244
245
246
247
    mb->setWindowModality(Qt::NonModal);
    m_dialog = mb;
    mb->show();
}
248
249
250
251


void GeneralSettings::showHelpForFileBrowser()
{
hjk's avatar
hjk committed
252
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
253
254
    variableHelpDialogCreator(UnixUtils::fileBrowserHelpText());
#endif
hjk's avatar
hjk committed
255
}
256
257
258
259
260
261
262
263
264

void GeneralSettings::resetLanguage()
{
    // system language is default
    m_page->languageBox->setCurrentIndex(0);
}

QString GeneralSettings::language() const
{
hjk's avatar
hjk committed
265
    QSettings* settings = Core::ICore::settings();
266
267
268
269
270
    return settings->value(QLatin1String("General/OverrideLanguage")).toString();
}

void GeneralSettings::setLanguage(const QString &locale)
{
hjk's avatar
hjk committed
271
    QSettings* settings = Core::ICore::settings();
272
273
    if (settings->value(QLatin1String("General/OverrideLanguage")).toString() != locale)
    {
hjk's avatar
hjk committed
274
        QMessageBox::information(Core::ICore::mainWindow(), tr("Restart required"),
275
276
277
278
279
280
281
                                 tr("The language change will take effect after a restart of Qt Creator."));
    }
    if (locale.isEmpty())
        settings->remove(QLatin1String("General/OverrideLanguage"));
    else
        settings->setValue(QLatin1String("General/OverrideLanguage"), locale);
}