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 "generalsettingspage.h"
36 #include "bookmarkmanager.h"
37 #include "centralwidget.h"
38 #include "helpconstants.h"
39 #include "helpviewer.h"
40 #include "localhelpmanager.h"
41 #include "xbelsupport.h"
43 #include <coreplugin/coreconstants.h>
44 #include <coreplugin/helpmanager.h>
45 #include <coreplugin/icore.h>
47 #include <QtCore/QCoreApplication>
48 #include <QtCore/QSettings>
49 #include <QtCore/QTextStream>
51 #include <QtGui/QApplication>
52 #include <QtGui/QFileDialog>
54 #if !defined(QT_NO_WEBKIT)
55 #include <QtWebKit/QWebSettings>
58 using namespace Help::Internal;
60 GeneralSettingsPage::GeneralSettingsPage()
63 m_font = qApp->font();
64 #if !defined(QT_NO_WEBKIT)
65 QWebSettings* webSettings = QWebSettings::globalSettings();
66 m_font.setPointSize(webSettings->fontSize(QWebSettings::DefaultFontSize));
70 QString GeneralSettingsPage::id() const
72 return QLatin1String("A.General settings");
75 QString GeneralSettingsPage::displayName() const
77 return tr("General Settings");
80 QString GeneralSettingsPage::category() const
82 return QLatin1String(Help::Constants::HELP_CATEGORY);
85 QString GeneralSettingsPage::displayCategory() const
87 return QCoreApplication::translate("Help", Help::Constants::HELP_TR_CATEGORY);
90 QIcon GeneralSettingsPage::categoryIcon() const
92 return QIcon(QLatin1String(Help::Constants::HELP_CATEGORY_ICON));
95 QWidget *GeneralSettingsPage::createPage(QWidget *parent)
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);
103 Core::HelpManager *manager = Core::HelpManager::instance();
104 m_font = qVariantValue<QFont>(manager->customValue(QLatin1String("font"),
111 m_homePage = manager->customValue(QLatin1String("HomePage"), QString())
113 if (m_homePage.isEmpty()) {
114 m_homePage = manager->customValue(QLatin1String("DefaultHomePage"),
115 Help::Constants::AboutBlank).toString();
117 m_ui->homePageLineEdit->setText(m_homePage);
119 const int startOption = manager->customValue(QLatin1String("StartOption"),
120 Help::Constants::ShowLastPages).toInt();
121 m_ui->helpStartComboBox->setCurrentIndex(startOption);
123 m_contextOption = manager->customValue(QLatin1String("ContextHelpOption"),
124 Help::Constants::SideBySideIfPossible).toInt();
125 m_ui->contextHelpComboBox->setCurrentIndex(m_contextOption);
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()));
131 HelpViewer *viewer = CentralWidget::instance()->currentHelpViewer();
133 m_ui->currentPageButton->setEnabled(false);
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()));
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('&'));
146 m_returnOnClose = manager->customValue(QLatin1String("ReturnOnClose"),
148 m_ui->m_returnOnClose->setChecked(m_returnOnClose);
153 void GeneralSettingsPage::apply()
155 if (!m_ui) // page was never shown
158 const QString &family = m_ui->familyComboBox->currentFont().family();
159 newFont.setFamily(family);
162 int currentIndex = m_ui->sizeComboBox->currentIndex();
163 if (currentIndex != -1)
164 fontSize = m_ui->sizeComboBox->itemData(currentIndex).toInt();
165 newFont.setPointSize(fontSize);
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);
177 newFont.setStyle(QFont::StyleNormal);
179 const int weight = m_fontDatabase.weight(family, fontStyle);
180 if (weight >= 0) // Weight < 0 asserts...
181 newFont.setWeight(weight);
183 Core::HelpManager *manager = Core::HelpManager::instance();
184 if (newFont != m_font) {
186 manager->setCustomValue(QLatin1String("font"), newFont);
190 QString homePage = m_ui->homePageLineEdit->text();
191 if (homePage.isEmpty())
192 homePage = Help::Constants::AboutBlank;
193 manager->setCustomValue(QLatin1String("HomePage"), homePage);
195 const int startOption = m_ui->helpStartComboBox->currentIndex();
196 manager->setCustomValue(QLatin1String("StartOption"), startOption);
198 const int helpOption = m_ui->contextHelpComboBox->currentIndex();
199 if (m_contextOption != helpOption) {
200 m_contextOption = helpOption;
201 manager->setCustomValue(QLatin1String("ContextHelpOption"), helpOption);
203 QSettings *settings = Core::ICore::instance()->settings();
204 settings->beginGroup(Help::Constants::ID_MODE_HELP);
205 settings->setValue(QLatin1String("ContextHelpOption"), helpOption);
206 settings->endGroup();
208 emit contextHelpOptionChanged();
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();
219 void GeneralSettingsPage::setCurrentPage()
221 HelpViewer *viewer = CentralWidget::instance()->currentHelpViewer();
223 m_ui->homePageLineEdit->setText(viewer->source().toString());
226 void GeneralSettingsPage::setBlankPage()
228 m_ui->homePageLineEdit->setText(Help::Constants::AboutBlank);
231 void GeneralSettingsPage::setDefaultPage()
233 const QString &defaultHomePage = Core::HelpManager::instance()
234 ->customValue(QLatin1String("DefaultHomePage"), QString()).toString();
235 m_ui->homePageLineEdit->setText(defaultHomePage);
238 void GeneralSettingsPage::importBookmarks()
240 m_ui->errorLabel->setVisible(false);
242 QString fileName = QFileDialog::getOpenFileName(0, tr("Import Bookmarks"),
243 QDir::currentPath(), tr("Files (*.xbel)"));
245 if (fileName.isEmpty())
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))
256 m_ui->errorLabel->setVisible(true);
257 m_ui->errorLabel->setText(tr("There was an error while importing bookmarks!"));
260 void GeneralSettingsPage::exportBookmarks()
262 m_ui->errorLabel->setVisible(false);
264 QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"),
265 "untitled.xbel", tr("Files (*.xbel)"));
267 QLatin1String suffix(".xbel");
268 if (!fileName.endsWith(suffix))
269 fileName.append(suffix);
271 QFile file(fileName);
272 if (file.open(QIODevice::WriteOnly)) {
273 XbelWriter writer(LocalHelpManager::bookmarkManager().treeBookmarkModel());
274 writer.writeToFile(&file);
278 void GeneralSettingsPage::updateFontSize()
280 const QString &family = m_font.family();
281 const QString &fontStyle = m_fontDatabase.styleString(m_font);
283 QList<int> pointSizes = m_fontDatabase.pointSizes(family, fontStyle);
284 if (pointSizes.empty())
285 pointSizes = QFontDatabase::standardSizes();
287 m_ui->sizeComboBox->clear();
288 m_ui->sizeComboBox->setCurrentIndex(-1);
289 m_ui->sizeComboBox->setEnabled(!pointSizes.empty());
291 // try to maintain selection or select closest.
292 if (!pointSizes.empty()) {
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);
302 void GeneralSettingsPage::updateFontStyle()
304 const QString &fontStyle = m_fontDatabase.styleString(m_font);
305 const QStringList &styles = m_fontDatabase.styles(m_font.family());
307 m_ui->styleComboBox->clear();
308 m_ui->styleComboBox->setCurrentIndex(-1);
309 m_ui->styleComboBox->setEnabled(!styles.empty());
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);
321 if (fontStyle == normalStyle)
322 normalIndex = newIndex;
325 if (m_ui->styleComboBox->currentIndex() == -1 && normalIndex != -1)
326 m_ui->styleComboBox->setCurrentIndex(normalIndex);
330 void GeneralSettingsPage::updateFontFamily()
332 m_ui->familyComboBox->setCurrentFont(m_font);
335 int GeneralSettingsPage::closestPointSizeIndex(int desiredPointSize) const
337 // try to maintain selection or select closest.
338 int closestIndex = -1;
339 int closestAbsError = 0xFFFF;
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) {
347 closestAbsError = absError;
348 if (closestAbsError == 0)
350 } else { // past optimum
351 if (absError > closestAbsError) {
359 bool GeneralSettingsPage::matches(const QString &s) const
361 return m_searchKeywords.contains(s, Qt::CaseInsensitive);
364 void GeneralSettingsPage::finish()
366 if (!m_ui) // page was never shown