OSDN Git Service

56f1dd9b79fe53e21659ad72ffc668c28167a36b
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / generalsettingspage.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 "generalsettingspage.h"
35
36 #include "bookmarkmanager.h"
37 #include "centralwidget.h"
38 #include "helpconstants.h"
39 #include "helpviewer.h"
40 #include "localhelpmanager.h"
41 #include "xbelsupport.h"
42
43 #include <coreplugin/coreconstants.h>
44 #include <coreplugin/helpmanager.h>
45 #include <coreplugin/icore.h>
46
47 #include <QtCore/QCoreApplication>
48 #include <QtCore/QSettings>
49 #include <QtCore/QTextStream>
50
51 #include <QtGui/QApplication>
52 #include <QtGui/QFileDialog>
53
54 #if !defined(QT_NO_WEBKIT)
55 #include <QtWebKit/QWebSettings>
56 #endif
57
58 using namespace Help::Internal;
59
60 GeneralSettingsPage::GeneralSettingsPage()
61     : m_ui(0)
62 {
63     m_font = qApp->font();
64 #if !defined(QT_NO_WEBKIT)
65     QWebSettings* webSettings = QWebSettings::globalSettings();
66     m_font.setPointSize(webSettings->fontSize(QWebSettings::DefaultFontSize));
67 #endif
68 }
69
70 QString GeneralSettingsPage::id() const
71 {
72     return QLatin1String("A.General settings");
73 }
74
75 QString GeneralSettingsPage::displayName() const
76 {
77     return tr("General Settings");
78 }
79
80 QString GeneralSettingsPage::category() const
81 {
82     return QLatin1String(Help::Constants::HELP_CATEGORY);
83 }
84
85 QString GeneralSettingsPage::displayCategory() const
86 {
87     return QCoreApplication::translate("Help", Help::Constants::HELP_TR_CATEGORY);
88 }
89
90 QIcon GeneralSettingsPage::categoryIcon() const
91 {
92     return QIcon(QLatin1String(Help::Constants::HELP_CATEGORY_ICON));
93 }
94
95 QWidget *GeneralSettingsPage::createPage(QWidget *parent)
96 {
97     QWidget *widget = new QWidget(parent);
98     m_ui = new Ui::GeneralSettingsPage;
99     m_ui->setupUi(widget);
100     m_ui->sizeComboBox->setEditable(false);
101     m_ui->styleComboBox->setEditable(false);
102
103     Core::HelpManager *manager = Core::HelpManager::instance();
104     m_font = qVariantValue<QFont>(manager->customValue(QLatin1String("font"),
105         m_font));
106
107     updateFontSize();
108     updateFontStyle();
109     updateFontFamily();
110
111     m_homePage = manager->customValue(QLatin1String("HomePage"), QString())
112         .toString();
113     if (m_homePage.isEmpty()) {
114         m_homePage = manager->customValue(QLatin1String("DefaultHomePage"),
115             Help::Constants::AboutBlank).toString();
116     }
117     m_ui->homePageLineEdit->setText(m_homePage);
118
119     const int startOption = manager->customValue(QLatin1String("StartOption"),
120         Help::Constants::ShowLastPages).toInt();
121     m_ui->helpStartComboBox->setCurrentIndex(startOption);
122
123     m_contextOption = manager->customValue(QLatin1String("ContextHelpOption"),
124         Help::Constants::SideBySideIfPossible).toInt();
125     m_ui->contextHelpComboBox->setCurrentIndex(m_contextOption);
126
127     connect(m_ui->currentPageButton, SIGNAL(clicked()), this, SLOT(setCurrentPage()));
128     connect(m_ui->blankPageButton, SIGNAL(clicked()), this, SLOT(setBlankPage()));
129     connect(m_ui->defaultPageButton, SIGNAL(clicked()), this, SLOT(setDefaultPage()));
130
131     HelpViewer *viewer = CentralWidget::instance()->currentHelpViewer();
132     if (!viewer)
133         m_ui->currentPageButton->setEnabled(false);
134
135     m_ui->errorLabel->setVisible(false);
136     connect(m_ui->importButton, SIGNAL(clicked()), this, SLOT(importBookmarks()));
137     connect(m_ui->exportButton, SIGNAL(clicked()), this, SLOT(exportBookmarks()));
138
139     if (m_searchKeywords.isEmpty()) {
140         QTextStream(&m_searchKeywords) << ' ' << m_ui->contextHelpLabel->text()
141            << ' ' << m_ui->startPageLabel->text() << ' ' << m_ui->homePageLabel->text()
142            << ' ' << m_ui->bookmarkGroupBox->title();
143         m_searchKeywords.remove(QLatin1Char('&'));
144     }
145
146     m_returnOnClose = manager->customValue(QLatin1String("ReturnOnClose"),
147         false).toBool();
148     m_ui->m_returnOnClose->setChecked(m_returnOnClose);
149
150     return widget;
151 }
152
153 void GeneralSettingsPage::apply()
154 {
155     if (!m_ui) // page was never shown
156         return;
157     QFont newFont;
158     const QString &family = m_ui->familyComboBox->currentFont().family();
159     newFont.setFamily(family);
160
161     int fontSize = 14;
162     int currentIndex = m_ui->sizeComboBox->currentIndex();
163     if (currentIndex != -1)
164         fontSize = m_ui->sizeComboBox->itemData(currentIndex).toInt();
165     newFont.setPointSize(fontSize);
166
167     QString fontStyle = QLatin1String("Normal");
168     currentIndex = m_ui->styleComboBox->currentIndex();
169     if (currentIndex != -1)
170         fontStyle = m_ui->styleComboBox->itemText(currentIndex);
171     newFont.setBold(m_fontDatabase.bold(family, fontStyle));
172     if (fontStyle.contains(QLatin1String("Italic")))
173         newFont.setStyle(QFont::StyleItalic);
174     else if (fontStyle.contains(QLatin1String("Oblique")))
175         newFont.setStyle(QFont::StyleOblique);
176     else
177         newFont.setStyle(QFont::StyleNormal);
178
179     const int weight = m_fontDatabase.weight(family, fontStyle);
180     if (weight >= 0)    // Weight < 0 asserts...
181         newFont.setWeight(weight);
182
183     Core::HelpManager *manager = Core::HelpManager::instance();
184     if (newFont != m_font) {
185         m_font = newFont;
186         manager->setCustomValue(QLatin1String("font"), newFont);
187         emit fontChanged();
188     }
189
190     QString homePage = m_ui->homePageLineEdit->text();
191     if (homePage.isEmpty())
192         homePage = Help::Constants::AboutBlank;
193     manager->setCustomValue(QLatin1String("HomePage"), homePage);
194
195     const int startOption = m_ui->helpStartComboBox->currentIndex();
196     manager->setCustomValue(QLatin1String("StartOption"), startOption);
197
198     const int helpOption = m_ui->contextHelpComboBox->currentIndex();
199     if (m_contextOption != helpOption) {
200         m_contextOption = helpOption;
201         manager->setCustomValue(QLatin1String("ContextHelpOption"), helpOption);
202
203         QSettings *settings = Core::ICore::instance()->settings();
204         settings->beginGroup(Help::Constants::ID_MODE_HELP);
205         settings->setValue(QLatin1String("ContextHelpOption"), helpOption);
206         settings->endGroup();
207
208         emit contextHelpOptionChanged();
209     }
210
211     const bool close = m_ui->m_returnOnClose->isChecked();
212     if (m_returnOnClose != close) {
213         m_returnOnClose = close;
214         manager->setCustomValue(QLatin1String("ReturnOnClose"), close);
215         emit returnOnCloseChanged();
216     }
217 }
218
219 void GeneralSettingsPage::setCurrentPage()
220 {
221     HelpViewer *viewer = CentralWidget::instance()->currentHelpViewer();
222     if (viewer)
223         m_ui->homePageLineEdit->setText(viewer->source().toString());
224 }
225
226 void GeneralSettingsPage::setBlankPage()
227 {
228     m_ui->homePageLineEdit->setText(Help::Constants::AboutBlank);
229 }
230
231 void GeneralSettingsPage::setDefaultPage()
232 {
233     const QString &defaultHomePage = Core::HelpManager::instance()
234         ->customValue(QLatin1String("DefaultHomePage"), QString()).toString();
235     m_ui->homePageLineEdit->setText(defaultHomePage);
236 }
237
238 void GeneralSettingsPage::importBookmarks()
239 {
240     m_ui->errorLabel->setVisible(false);
241
242     QString fileName = QFileDialog::getOpenFileName(0, tr("Import Bookmarks"),
243         QDir::currentPath(), tr("Files (*.xbel)"));
244
245     if (fileName.isEmpty())
246         return;
247
248     QFile file(fileName);
249     if (file.open(QIODevice::ReadOnly)) {
250         const BookmarkManager &manager = LocalHelpManager::bookmarkManager();
251         XbelReader reader(manager.treeBookmarkModel(), manager.listBookmarkModel());
252         if (reader.readFromFile(&file))
253             return;
254     }
255
256     m_ui->errorLabel->setVisible(true);
257     m_ui->errorLabel->setText(tr("There was an error while importing bookmarks!"));
258 }
259
260 void GeneralSettingsPage::exportBookmarks()
261 {
262     m_ui->errorLabel->setVisible(false);
263
264     QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"),
265         "untitled.xbel", tr("Files (*.xbel)"));
266
267     QLatin1String suffix(".xbel");
268     if (!fileName.endsWith(suffix))
269         fileName.append(suffix);
270
271     QFile file(fileName);
272     if (file.open(QIODevice::WriteOnly)) {
273         XbelWriter writer(LocalHelpManager::bookmarkManager().treeBookmarkModel());
274         writer.writeToFile(&file);
275     }
276 }
277
278 void GeneralSettingsPage::updateFontSize()
279 {
280     const QString &family = m_font.family();
281     const QString &fontStyle = m_fontDatabase.styleString(m_font);
282
283     QList<int> pointSizes = m_fontDatabase.pointSizes(family, fontStyle);
284     if (pointSizes.empty())
285         pointSizes = QFontDatabase::standardSizes();
286
287     m_ui->sizeComboBox->clear();
288     m_ui->sizeComboBox->setCurrentIndex(-1);
289     m_ui->sizeComboBox->setEnabled(!pointSizes.empty());
290
291     //  try to maintain selection or select closest.
292     if (!pointSizes.empty()) {
293         QString n;
294         foreach (int pointSize, pointSizes)
295             m_ui->sizeComboBox->addItem(n.setNum(pointSize), QVariant(pointSize));
296         const int closestIndex = closestPointSizeIndex(m_font.pointSize());
297         if (closestIndex != -1)
298             m_ui->sizeComboBox->setCurrentIndex(closestIndex);
299     }
300 }
301
302 void GeneralSettingsPage::updateFontStyle()
303 {
304     const QString &fontStyle = m_fontDatabase.styleString(m_font);
305     const QStringList &styles = m_fontDatabase.styles(m_font.family());
306
307     m_ui->styleComboBox->clear();
308     m_ui->styleComboBox->setCurrentIndex(-1);
309     m_ui->styleComboBox->setEnabled(!styles.empty());
310
311     if (!styles.empty()) {
312         int normalIndex = -1;
313         const QString normalStyle = QLatin1String("Normal");
314         foreach (const QString &style, styles) {
315             // try to maintain selection or select 'normal' preferably
316             const int newIndex = m_ui->styleComboBox->count();
317             m_ui->styleComboBox->addItem(style);
318             if (fontStyle == style) {
319                 m_ui->styleComboBox->setCurrentIndex(newIndex);
320             } else {
321                 if (fontStyle ==  normalStyle)
322                     normalIndex = newIndex;
323             }
324         }
325         if (m_ui->styleComboBox->currentIndex() == -1 && normalIndex != -1)
326             m_ui->styleComboBox->setCurrentIndex(normalIndex);
327     }
328 }
329
330 void GeneralSettingsPage::updateFontFamily()
331 {
332     m_ui->familyComboBox->setCurrentFont(m_font);
333 }
334
335 int GeneralSettingsPage::closestPointSizeIndex(int desiredPointSize) const
336 {
337     //  try to maintain selection or select closest.
338     int closestIndex = -1;
339     int closestAbsError = 0xFFFF;
340
341     const int pointSizeCount = m_ui->sizeComboBox->count();
342     for (int i = 0; i < pointSizeCount; i++) {
343         const int itemPointSize = m_ui->sizeComboBox->itemData(i).toInt();
344         const int absError = qAbs(desiredPointSize - itemPointSize);
345         if (absError < closestAbsError) {
346             closestIndex  = i;
347             closestAbsError = absError;
348             if (closestAbsError == 0)
349                 break;
350         } else {    // past optimum
351             if (absError > closestAbsError) {
352                 break;
353             }
354         }
355     }
356     return closestIndex;
357 }
358
359 bool GeneralSettingsPage::matches(const QString &s) const
360 {
361     return m_searchKeywords.contains(s, Qt::CaseInsensitive);
362 }
363
364 void GeneralSettingsPage::finish()
365 {
366     if (!m_ui) // page was never shown
367         return;
368     delete m_ui;
369     m_ui = 0;
370 }