generalsettings.cpp 9.45 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "generalsettings.h"
34
#include "coreconstants.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>
con's avatar
con committed
40
#include <coreplugin/editormanager/editormanager.h>
41
#include <coreplugin/icore.h>
42

43
#include <QtGui/QMainWindow>
44
#include <QtGui/QMessageBox>
con's avatar
con committed
45

46
#include <QtCore/QCoreApplication>
47
#include <QtCore/QTextStream>
48 49 50
#include <QtCore/QDir>
#include <QtCore/QLibraryInfo>
#include <QtCore/QSettings>
51

con's avatar
con committed
52 53
#include "ui_generalsettings.h"

54
using namespace Utils;
con's avatar
con committed
55 56
using namespace Core::Internal;

57

58
GeneralSettings::GeneralSettings():
59
    m_page(0), m_dialog(0)
con's avatar
con committed
60 61 62
{
}

63 64
QString GeneralSettings::id() const
{
65
    return QLatin1String(Core::Constants::SETTINGS_ID_ENVIRONMENT);
66 67
}

68
QString GeneralSettings::displayName() const
con's avatar
con committed
69 70 71 72 73 74
{
    return tr("General");
}

QString GeneralSettings::category() const
{
75
    return QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE);
con's avatar
con committed
76 77
}

78
QString GeneralSettings::displayCategory() const
con's avatar
con committed
79
{
80
    return QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE);
con's avatar
con committed
81 82
}

83 84 85 86 87
QIcon GeneralSettings::categoryIcon() const
{
    return QIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
}

88 89 90 91 92 93 94 95 96 97
static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath)
{
    static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);

    const QString trFile = QLatin1String("qt_") + locale + QLatin1String(".qm");
    return QFile::exists(qtTrPath+'/'+trFile) || QFile::exists(creatorTrPath+'/'+trFile);
}

void GeneralSettings::fillLanguageBox() const
{
98 99
    const QString currentLocale = language();

100
    m_page->languageBox->addItem(tr("<System Language>"), QString());
101 102
    // need to add this explicitly, since there is no qm file for English
    m_page->languageBox->addItem(QLatin1String("English"), QLatin1String("C"));
103 104
    if (currentLocale == QLatin1String("C"))
        m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
105 106 107

    const QString creatorTrPath =
            Core::ICore::instance()->resourcePath() + QLatin1String("/translations");
108
    const QStringList languageFiles = QDir(creatorTrPath).entryList(QStringList(QLatin1String("qtcreator*.qm")));
109

110
    Q_FOREACH(const QString &languageFile, languageFiles)
111
    {
112
        int start = languageFile.indexOf(QLatin1Char('_'))+1;
113 114 115 116 117 118 119 120 121 122 123
        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)) {
            m_page->languageBox->addItem(QLocale::languageToString(QLocale(locale).language()), locale);
            if (locale == currentLocale)
                m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
        }
    }
}

124
QWidget *GeneralSettings::createPage(QWidget *parent)
con's avatar
con committed
125
{
126
    m_page = new Ui::GeneralSettings();
127 128
    m_widget = new QWidget(parent);
    m_page->setupUi(m_widget);
con's avatar
con committed
129

130
    QSettings* settings = Core::ICore::instance()->settings();
131 132
    Q_UNUSED(settings) // Windows

133
    fillLanguageBox();
134

135
    m_page->colorButton->setColor(StyleHelper::requestedBaseColor());
136
    m_page->reloadBehavior->setCurrentIndex(EditorManager::instance()->reloadSetting());
137
#ifdef Q_OS_UNIX
138
    m_page->terminalEdit->setText(ConsoleProcess::terminalEmulator(settings));
139 140 141 142 143
#else
    m_page->terminalLabel->hide();
    m_page->terminalEdit->hide();
    m_page->resetTerminalButton->hide();
#endif
con's avatar
con committed
144

145 146 147 148 149 150
#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();
151
    m_page->helpExternalFileBrowserButton->hide();
152 153
#endif

154 155 156
    m_page->autoSaveCheckBox->setChecked(EditorManager::instance()->autoSaveEnabled());
    m_page->autoSaveInterval->setValue(EditorManager::instance()->autoSaveInterval());

con's avatar
con committed
157 158
    connect(m_page->resetButton, SIGNAL(clicked()),
            this, SLOT(resetInterfaceColor()));
159 160 161
#ifdef Q_OS_UNIX
    connect(m_page->resetTerminalButton, SIGNAL(clicked()),
            this, SLOT(resetTerminal()));
162 163 164 165 166 167
#ifndef Q_OS_MAC
    connect(m_page->resetFileBrowserButton, SIGNAL(clicked()),
            this, SLOT(resetFileBrowser()));
    connect(m_page->helpExternalFileBrowserButton, SIGNAL(clicked()),
            this, SLOT(showHelpForFileBrowser()));
#endif
168
#endif
169

170
    if (m_searchKeywords.isEmpty()) {
171 172 173 174 175 176 177 178
        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();
179 180
        m_searchKeywords.remove(QLatin1Char('&'));
    }
181
    return m_widget;
con's avatar
con committed
182 183
}

184 185 186 187 188
bool GeneralSettings::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

189
void GeneralSettings::apply()
con's avatar
con committed
190
{
191 192
    if (!m_page) // wasn't shown, can't be changed
        return;
193 194
    int currentIndex = m_page->languageBox->currentIndex();
    setLanguage(m_page->languageBox->itemData(currentIndex, Qt::UserRole).toString());
con's avatar
con committed
195 196
    // Apply the new base color if accepted
    StyleHelper::setBaseColor(m_page->colorButton->color());
197
    EditorManager::instance()->setReloadSetting(IFile::ReloadSetting(m_page->reloadBehavior->currentIndex()));
198
#ifdef Q_OS_UNIX
199
    ConsoleProcess::setTerminalEmulator(Core::ICore::instance()->settings(),
200
                                        m_page->terminalEdit->text());
201
#ifndef Q_OS_MAC
202
    Utils::UnixUtils::setFileBrowser(Core::ICore::instance()->settings(), m_page->externalFileBrowserEdit->text());
203
#endif
204
#endif
205 206
    EditorManager::instance()->setAutoSaveEnabled(m_page->autoSaveCheckBox->isChecked());
    EditorManager::instance()->setAutoSaveInterval(m_page->autoSaveInterval->value());
con's avatar
con committed
207 208
}

209 210
void GeneralSettings::finish()
{
211 212
    if (!m_page) // page was never shown
        return;
213
    delete m_page;
214
    m_page = 0;
215 216
}

con's avatar
con committed
217 218
void GeneralSettings::resetInterfaceColor()
{
219
    m_page->colorButton->setColor(StyleHelper::DEFAULT_BASE_COLOR);
con's avatar
con committed
220 221
}

222 223 224 225 226
#ifdef Q_OS_UNIX
void GeneralSettings::resetTerminal()
{
    m_page->terminalEdit->setText(ConsoleProcess::defaultTerminalEmulator() + QLatin1String(" -e"));
}
227 228 229 230 231 232 233

#ifndef Q_OS_MAC
void GeneralSettings::resetFileBrowser()
{
    m_page->externalFileBrowserEdit->setText(UnixUtils::defaultFileBrowser());
}
#endif
234 235
#endif

236

237
void GeneralSettings::variableHelpDialogCreator(const QString &helpText)
con's avatar
con committed
238 239
{
    if (m_dialog) {
240 241 242
        if (m_dialog->text() != helpText)
            m_dialog->setText(helpText);

con's avatar
con committed
243 244 245 246 247 248 249
        m_dialog->show();
        m_dialog->raise();
        m_dialog->activateWindow();
        return;
    }
    QMessageBox *mb = new QMessageBox(QMessageBox::Information,
                                  tr("Variables"),
250 251
                                  helpText,
                                  QMessageBox::Close,
252
                                  m_widget);
con's avatar
con committed
253 254 255 256
    mb->setWindowModality(Qt::NonModal);
    m_dialog = mb;
    mb->show();
}
257 258 259 260 261 262 263 264


#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
void GeneralSettings::showHelpForFileBrowser()
{
    variableHelpDialogCreator(UnixUtils::fileBrowserHelpText());
}
#endif
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

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

QString GeneralSettings::language() const
{
    QSettings* settings = Core::ICore::instance()->settings();
    return settings->value(QLatin1String("General/OverrideLanguage")).toString();
}

void GeneralSettings::setLanguage(const QString &locale)
{
    QSettings* settings = Core::ICore::instance()->settings();
    if (settings->value(QLatin1String("General/OverrideLanguage")).toString() != locale)
    {
        QMessageBox::information(Core::ICore::instance()->mainWindow(), tr("Restart required"),
                                 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);
}