OSDN Git Service

62f2760d018b08a9689be3646183d5b710f0b9a5
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / generalsettings.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "generalsettings.h"
35 #include "coreconstants.h"
36
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>
43
44 #include <QtGui/QMainWindow>
45 #include <QtGui/QMessageBox>
46
47 #include <QtCore/QCoreApplication>
48 #include <QtCore/QTextStream>
49 #include <QtCore/QDir>
50 #include <QtCore/QLibraryInfo>
51 #include <QtCore/QSettings>
52
53 #include "ui_generalsettings.h"
54
55 using namespace Utils;
56 using namespace Core::Internal;
57
58
59 GeneralSettings::GeneralSettings():
60     m_page(0), m_dialog(0)
61 {
62 }
63
64 QString GeneralSettings::id() const
65 {
66     return QLatin1String(Core::Constants::SETTINGS_ID_ENVIRONMENT);
67 }
68
69 QString GeneralSettings::displayName() const
70 {
71     return tr("General");
72 }
73
74 QString GeneralSettings::category() const
75 {
76     return QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE);
77 }
78
79 QString GeneralSettings::displayCategory() const
80 {
81     return QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE);
82 }
83
84 QIcon GeneralSettings::categoryIcon() const
85 {
86     return QIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
87 }
88
89 static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath)
90 {
91     static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
92
93     const QString trFile = QLatin1String("qt_") + locale + QLatin1String(".qm");
94     return QFile::exists(qtTrPath+'/'+trFile) || QFile::exists(creatorTrPath+'/'+trFile);
95 }
96
97 void GeneralSettings::fillLanguageBox() const
98 {
99     const QString currentLocale = language();
100
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);
106
107     const QString creatorTrPath =
108             Core::ICore::instance()->resourcePath() + QLatin1String("/translations");
109     const QStringList languageFiles = QDir(creatorTrPath).entryList(QStringList(QLatin1String("qtcreator*.qm")));
110
111     Q_FOREACH(const QString &languageFile, languageFiles)
112     {
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);
121         }
122     }
123 }
124
125 QWidget *GeneralSettings::createPage(QWidget *parent)
126 {
127     m_page = new Ui::GeneralSettings();
128     m_widget = new QWidget(parent);
129     m_page->setupUi(m_widget);
130
131     QSettings* settings = Core::ICore::instance()->settings();
132     Q_UNUSED(settings) // Windows
133
134     fillLanguageBox();
135
136     m_page->colorButton->setColor(StyleHelper::requestedBaseColor());
137     m_page->reloadBehavior->setCurrentIndex(EditorManager::instance()->reloadSetting());
138 #ifdef Q_OS_UNIX
139     m_page->terminalEdit->setText(ConsoleProcess::terminalEmulator(settings));
140 #else
141     m_page->terminalLabel->hide();
142     m_page->terminalEdit->hide();
143     m_page->resetTerminalButton->hide();
144 #endif
145
146 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
147     m_page->externalFileBrowserEdit->setText(UnixUtils::fileBrowser(settings));
148 #else
149     m_page->externalFileBrowserLabel->hide();
150     m_page->externalFileBrowserEdit->hide();
151     m_page->resetFileBrowserButton->hide();
152     m_page->helpExternalFileBrowserButton->hide();
153 #endif
154
155     connect(m_page->resetButton, SIGNAL(clicked()),
156             this, SLOT(resetInterfaceColor()));
157 #ifdef Q_OS_UNIX
158     connect(m_page->resetTerminalButton, SIGNAL(clicked()),
159             this, SLOT(resetTerminal()));
160 #ifndef Q_OS_MAC
161     connect(m_page->resetFileBrowserButton, SIGNAL(clicked()),
162             this, SLOT(resetFileBrowser()));
163     connect(m_page->helpExternalFileBrowserButton, SIGNAL(clicked()),
164             this, SLOT(showHelpForFileBrowser()));
165 #endif
166 #endif
167
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('&'));
178     }
179     return m_widget;
180 }
181
182 bool GeneralSettings::matches(const QString &s) const
183 {
184     return m_searchKeywords.contains(s, Qt::CaseInsensitive);
185 }
186
187 void GeneralSettings::apply()
188 {
189     if (!m_page) // wasn't shown, can't be changed
190         return;
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()));
196 #ifdef Q_OS_UNIX
197     ConsoleProcess::setTerminalEmulator(Core::ICore::instance()->settings(),
198                                         m_page->terminalEdit->text());
199 #ifndef Q_OS_MAC
200     Utils::UnixUtils::setFileBrowser(Core::ICore::instance()->settings(), m_page->externalFileBrowserEdit->text());
201 #endif
202 #endif
203 }
204
205 void GeneralSettings::finish()
206 {
207     if (!m_page) // page was never shown
208         return;
209     delete m_page;
210     m_page = 0;
211 }
212
213 void GeneralSettings::resetInterfaceColor()
214 {
215     m_page->colorButton->setColor(StyleHelper::DEFAULT_BASE_COLOR);
216 }
217
218 #ifdef Q_OS_UNIX
219 void GeneralSettings::resetTerminal()
220 {
221     m_page->terminalEdit->setText(ConsoleProcess::defaultTerminalEmulator() + QLatin1String(" -e"));
222 }
223
224 #ifndef Q_OS_MAC
225 void GeneralSettings::resetFileBrowser()
226 {
227     m_page->externalFileBrowserEdit->setText(UnixUtils::defaultFileBrowser());
228 }
229 #endif
230 #endif
231
232
233 void GeneralSettings::variableHelpDialogCreator(const QString &helpText)
234 {
235     if (m_dialog) {
236         if (m_dialog->text() != helpText)
237             m_dialog->setText(helpText);
238
239         m_dialog->show();
240         m_dialog->raise();
241         m_dialog->activateWindow();
242         return;
243     }
244     QMessageBox *mb = new QMessageBox(QMessageBox::Information,
245                                   tr("Variables"),
246                                   helpText,
247                                   QMessageBox::Close,
248                                   m_widget);
249     mb->setWindowModality(Qt::NonModal);
250     m_dialog = mb;
251     mb->show();
252 }
253
254
255 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
256 void GeneralSettings::showHelpForFileBrowser()
257 {
258     variableHelpDialogCreator(UnixUtils::fileBrowserHelpText());
259 }
260 #endif
261
262 void GeneralSettings::resetLanguage()
263 {
264     // system language is default
265     m_page->languageBox->setCurrentIndex(0);
266 }
267
268 QString GeneralSettings::language() const
269 {
270     QSettings* settings = Core::ICore::instance()->settings();
271     return settings->value(QLatin1String("General/OverrideLanguage")).toString();
272 }
273
274 void GeneralSettings::setLanguage(const QString &locale)
275 {
276     QSettings* settings = Core::ICore::instance()->settings();
277     if (settings->value(QLatin1String("General/OverrideLanguage")).toString() != locale)
278     {
279         QMessageBox::information(Core::ICore::instance()->mainWindow(), tr("Restart required"),
280                                  tr("The language change will take effect after a restart of Qt Creator."));
281     }
282     if (locale.isEmpty())
283         settings->remove(QLatin1String("General/OverrideLanguage"));
284     else
285         settings->setValue(QLatin1String("General/OverrideLanguage"), locale);
286 }