1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "generalsettings.h"
35 #include "coreconstants.h"
37 #include <utils/stylehelper.h>
38 #include <utils/qtcolorbutton.h>
39 #include <utils/consoleprocess.h>
40 #include <utils/unixutils.h>
41 #include <coreplugin/editormanager/editormanager.h>
42 #include <coreplugin/icore.h>
44 #include <QtGui/QMainWindow>
45 #include <QtGui/QMessageBox>
47 #include <QtCore/QCoreApplication>
48 #include <QtCore/QTextStream>
49 #include <QtCore/QDir>
50 #include <QtCore/QLibraryInfo>
51 #include <QtCore/QSettings>
53 #include "ui_generalsettings.h"
55 using namespace Utils;
56 using namespace Core::Internal;
59 GeneralSettings::GeneralSettings():
60 m_page(0), m_dialog(0)
64 QString GeneralSettings::id() const
66 return QLatin1String(Core::Constants::SETTINGS_ID_ENVIRONMENT);
69 QString GeneralSettings::displayName() const
74 QString GeneralSettings::category() const
76 return QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE);
79 QString GeneralSettings::displayCategory() const
81 return QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE);
84 QIcon GeneralSettings::categoryIcon() const
86 return QIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
89 static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath)
91 static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
93 const QString trFile = QLatin1String("qt_") + locale + QLatin1String(".qm");
94 return QFile::exists(qtTrPath+'/'+trFile) || QFile::exists(creatorTrPath+'/'+trFile);
97 void GeneralSettings::fillLanguageBox() const
99 const QString currentLocale = language();
101 m_page->languageBox->addItem(tr("<System Language>"), QString());
102 // need to add this explicitly, since there is no qm file for English
103 m_page->languageBox->addItem(QLatin1String("English"), QLatin1String("C"));
104 if (currentLocale == QLatin1String("C"))
105 m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
107 const QString creatorTrPath =
108 Core::ICore::instance()->resourcePath() + QLatin1String("/translations");
109 const QStringList languageFiles = QDir(creatorTrPath).entryList(QStringList(QLatin1String("qtcreator*.qm")));
111 Q_FOREACH(const QString &languageFile, languageFiles)
113 int start = languageFile.indexOf(QLatin1Char('_'))+1;
114 int end = languageFile.lastIndexOf(QLatin1Char('.'));
115 const QString locale = languageFile.mid(start, end-start);
116 // no need to show a language that creator will not load anyway
117 if (hasQmFilesForLocale(locale, creatorTrPath)) {
118 m_page->languageBox->addItem(QLocale::languageToString(QLocale(locale).language()), locale);
119 if (locale == currentLocale)
120 m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1);
125 QWidget *GeneralSettings::createPage(QWidget *parent)
127 m_page = new Ui::GeneralSettings();
128 m_widget = new QWidget(parent);
129 m_page->setupUi(m_widget);
131 QSettings* settings = Core::ICore::instance()->settings();
132 Q_UNUSED(settings) // Windows
136 m_page->colorButton->setColor(StyleHelper::requestedBaseColor());
137 m_page->reloadBehavior->setCurrentIndex(EditorManager::instance()->reloadSetting());
139 m_page->terminalEdit->setText(ConsoleProcess::terminalEmulator(settings));
141 m_page->terminalLabel->hide();
142 m_page->terminalEdit->hide();
143 m_page->resetTerminalButton->hide();
146 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
147 m_page->externalFileBrowserEdit->setText(UnixUtils::fileBrowser(settings));
149 m_page->externalFileBrowserLabel->hide();
150 m_page->externalFileBrowserEdit->hide();
151 m_page->resetFileBrowserButton->hide();
152 m_page->helpExternalFileBrowserButton->hide();
155 connect(m_page->resetButton, SIGNAL(clicked()),
156 this, SLOT(resetInterfaceColor()));
158 connect(m_page->resetTerminalButton, SIGNAL(clicked()),
159 this, SLOT(resetTerminal()));
161 connect(m_page->resetFileBrowserButton, SIGNAL(clicked()),
162 this, SLOT(resetFileBrowser()));
163 connect(m_page->helpExternalFileBrowserButton, SIGNAL(clicked()),
164 this, SLOT(showHelpForFileBrowser()));
168 if (m_searchKeywords.isEmpty()) {
169 QLatin1Char sep(' ');
170 QTextStream(&m_searchKeywords)
171 << m_page->interfaceBox->title() << sep
172 << m_page->colorLabel->text() << sep
173 << m_page->languageLabel->text() << sep
174 << m_page->systemBox->title() << sep
175 << m_page->terminalLabel->text() << sep
176 << m_page->modifiedLabel->text();
177 m_searchKeywords.remove(QLatin1Char('&'));
182 bool GeneralSettings::matches(const QString &s) const
184 return m_searchKeywords.contains(s, Qt::CaseInsensitive);
187 void GeneralSettings::apply()
189 if (!m_page) // wasn't shown, can't be changed
191 int currentIndex = m_page->languageBox->currentIndex();
192 setLanguage(m_page->languageBox->itemData(currentIndex, Qt::UserRole).toString());
193 // Apply the new base color if accepted
194 StyleHelper::setBaseColor(m_page->colorButton->color());
195 EditorManager::instance()->setReloadSetting(IFile::ReloadSetting(m_page->reloadBehavior->currentIndex()));
197 ConsoleProcess::setTerminalEmulator(Core::ICore::instance()->settings(),
198 m_page->terminalEdit->text());
200 Utils::UnixUtils::setFileBrowser(Core::ICore::instance()->settings(), m_page->externalFileBrowserEdit->text());
205 void GeneralSettings::finish()
207 if (!m_page) // page was never shown
213 void GeneralSettings::resetInterfaceColor()
215 m_page->colorButton->setColor(StyleHelper::DEFAULT_BASE_COLOR);
219 void GeneralSettings::resetTerminal()
221 m_page->terminalEdit->setText(ConsoleProcess::defaultTerminalEmulator() + QLatin1String(" -e"));
225 void GeneralSettings::resetFileBrowser()
227 m_page->externalFileBrowserEdit->setText(UnixUtils::defaultFileBrowser());
233 void GeneralSettings::variableHelpDialogCreator(const QString &helpText)
236 if (m_dialog->text() != helpText)
237 m_dialog->setText(helpText);
241 m_dialog->activateWindow();
244 QMessageBox *mb = new QMessageBox(QMessageBox::Information,
249 mb->setWindowModality(Qt::NonModal);
255 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
256 void GeneralSettings::showHelpForFileBrowser()
258 variableHelpDialogCreator(UnixUtils::fileBrowserHelpText());
262 void GeneralSettings::resetLanguage()
264 // system language is default
265 m_page->languageBox->setCurrentIndex(0);
268 QString GeneralSettings::language() const
270 QSettings* settings = Core::ICore::instance()->settings();
271 return settings->value(QLatin1String("General/OverrideLanguage")).toString();
274 void GeneralSettings::setLanguage(const QString &locale)
276 QSettings* settings = Core::ICore::instance()->settings();
277 if (settings->value(QLatin1String("General/OverrideLanguage")).toString() != locale)
279 QMessageBox::information(Core::ICore::instance()->mainWindow(), tr("Restart required"),
280 tr("The language change will take effect after a restart of Qt Creator."));
282 if (locale.isEmpty())
283 settings->remove(QLatin1String("General/OverrideLanguage"));
285 settings->setValue(QLatin1String("General/OverrideLanguage"), locale);