generalsettingspage.cpp 10.7 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27 28 29 30 31
**
**************************************************************************/

#include "generalsettingspage.h"

32
#include "bookmarkmanager.h"
33
#include "centralwidget.h"
34 35
#include "helpconstants.h"
#include "helpmanager.h"
36
#include "helpviewer.h"
37
#include "xbelsupport.h"
38

39 40 41 42 43
#include <coreplugin/coreconstants.h>

#include <QtCore/QCoreApplication>
#include <QtCore/QTextStream>

44
#include <QtGui/QApplication>
45
#include <QtGui/QFileDialog>
46

47
#include <QtHelp/QHelpEngineCore>
48

49 50 51 52
#if !defined(QT_NO_WEBKIT)
#include <QtWebKit/QWebSettings>
#endif

53 54
using namespace Help::Internal;

55
GeneralSettingsPage::GeneralSettingsPage()
56
{
57
    m_font = qApp->font();
58 59
#if !defined(QT_NO_WEBKIT)
    QWebSettings* webSettings = QWebSettings::globalSettings();
60
    m_font.setPointSize(webSettings->fontSize(QWebSettings::DefaultFontSize));
61 62 63 64 65
#endif
}

QString GeneralSettingsPage::id() const
{
66
    return QLatin1String("A.General settings");
67 68
}

69
QString GeneralSettingsPage::displayName() const
70
{
71
    return tr("General Settings");
72 73 74 75
}

QString GeneralSettingsPage::category() const
{
76
    return QLatin1String(Help::Constants::HELP_CATEGORY);
77 78
}

79
QString GeneralSettingsPage::displayCategory() const
80
{
81
    return QCoreApplication::translate("Help", Help::Constants::HELP_TR_CATEGORY);
82 83
}

84 85 86 87 88
QIcon GeneralSettingsPage::categoryIcon() const
{
    return QIcon(QLatin1String(Help::Constants::HELP_CATEGORY_ICON));
}

89 90
QWidget *GeneralSettingsPage::createPage(QWidget *parent)
{
91
    QWidget *widget = new QWidget(parent);
92

93
    m_ui.setupUi(widget);
94 95 96
    m_ui.sizeComboBox->setEditable(false);
    m_ui.styleComboBox->setEditable(false);

97 98
    const QHelpEngineCore &engine = HelpManager::helpEngineCore();
    m_font = qVariantValue<QFont>(engine.customValue(QLatin1String("font"), m_font));
99 100 101 102 103

    updateFontSize();
    updateFontStyle();
    updateFontFamily();

104 105 106
    m_homePage = engine.customValue(QLatin1String("HomePage"), QString()).toString();
    if (m_homePage.isEmpty()) {
        m_homePage = engine.customValue(QLatin1String("DefaultHomePage"),
107
            Help::Constants::AboutBlank).toString();
108
    }
109
    m_ui.homePageLineEdit->setText(m_homePage);
110

111 112
    int startOption = engine.customValue(QLatin1String("StartOption"), 2).toInt();
    m_ui.helpStartComboBox->setCurrentIndex(startOption);
113

114 115
    int helpOption = engine.customValue(QLatin1String("ContextHelpOption"), 0).toInt();
    m_ui.contextHelpComboBox->setCurrentIndex(helpOption);
116

117 118 119 120
    connect(m_ui.currentPageButton, SIGNAL(clicked()), this, SLOT(setCurrentPage()));
    connect(m_ui.blankPageButton, SIGNAL(clicked()), this, SLOT(setBlankPage()));
    connect(m_ui.defaultPageButton, SIGNAL(clicked()), this, SLOT(setDefaultPage()));

121
    HelpViewer *viewer = CentralWidget::instance()->currentHelpViewer();
122
    if (!viewer)
123 124
        m_ui.currentPageButton->setEnabled(false);

125 126 127 128
    m_ui.errorLabel->setVisible(false);
    connect(m_ui.importButton, SIGNAL(clicked()), this, SLOT(importBookmarks()));
    connect(m_ui.exportButton, SIGNAL(clicked()), this, SLOT(exportBookmarks()));

129 130 131 132 133 134
    if (m_searchKeywords.isEmpty()) {
        QTextStream(&m_searchKeywords) << ' ' << m_ui.contextHelpLabel->text()
           << ' ' << m_ui.startPageLabel->text() << ' ' << m_ui.homePageLabel->text()
           << ' ' << m_ui.bookmarkGroupBox->title();
        m_searchKeywords.remove(QLatin1Char('&'));
    }
135
    return widget;
136 137 138
}

void GeneralSettingsPage::apply()
139 140
{
    QFont newFont;
141
    const QString &family = m_ui.familyComboBox->currentFont().family();
142
    newFont.setFamily(family);
143 144 145 146 147

    int fontSize = 14;
    int currentIndex = m_ui.sizeComboBox->currentIndex();
    if (currentIndex != -1)
        fontSize = m_ui.sizeComboBox->itemData(currentIndex).toInt();
148
    newFont.setPointSize(fontSize);
149 150 151 152 153

    QString fontStyle = QLatin1String("Normal");
    currentIndex = m_ui.styleComboBox->currentIndex();
    if (currentIndex != -1)
        fontStyle = m_ui.styleComboBox->itemText(currentIndex);
154
    newFont.setBold(m_fontDatabase.bold(family, fontStyle));
155
    if (fontStyle.contains(QLatin1String("Italic")))
156
        newFont.setStyle(QFont::StyleItalic);
157
    else if (fontStyle.contains(QLatin1String("Oblique")))
158
        newFont.setStyle(QFont::StyleOblique);
159
    else
160
        newFont.setStyle(QFont::StyleNormal);
161

162
    const int weight = m_fontDatabase.weight(family, fontStyle);
163
    if (weight >= 0)    // Weight < 0 asserts...
164
        newFont.setWeight(weight);
165

166 167
    QHelpEngineCore *engine = &HelpManager::helpEngineCore();
    engine->setCustomValue(QLatin1String("font"), newFont);
168

169 170
    if (newFont != m_font)
        emit fontChanged();
171 172 173

    QString homePage = m_ui.homePageLineEdit->text();
    if (homePage.isEmpty())
174
        homePage = Help::Constants::AboutBlank;
175
    engine->setCustomValue(QLatin1String("HomePage"), homePage);
176

177 178
    const int startOption = m_ui.helpStartComboBox->currentIndex();
    engine->setCustomValue(QLatin1String("StartOption"), startOption);
179

180 181
    const int helpOption = m_ui.contextHelpComboBox->currentIndex();
    engine->setCustomValue(QLatin1String("ContextHelpOption"), helpOption);
182

183
    // no need to call setup on the gui engine since we use only core engine
184 185
}

186 187
void GeneralSettingsPage::setCurrentPage()
{
188
    HelpViewer *viewer = CentralWidget::instance()->currentHelpViewer();
189 190 191 192 193 194
    if (viewer)
        m_ui.homePageLineEdit->setText(viewer->source().toString());
}

void GeneralSettingsPage::setBlankPage()
{
195
    m_ui.homePageLineEdit->setText(Help::Constants::AboutBlank);
196 197 198 199
}

void GeneralSettingsPage::setDefaultPage()
{
200 201 202
    const QString &defaultHomePage = HelpManager::helpEngineCore()
        .customValue(QLatin1String("DefaultHomePage"), QString()).toString();
    m_ui.homePageLineEdit->setText(defaultHomePage);
203 204
}

205 206 207 208 209 210 211 212 213 214 215 216
void GeneralSettingsPage::importBookmarks()
{
    m_ui.errorLabel->setVisible(false);

    QString fileName = QFileDialog::getOpenFileName(0, tr("Open Image"),
        QDir::currentPath(), tr("Files (*.xbel)"));

    if (fileName.isEmpty())
        return;

    QFile file(fileName);
    if (file.open(QIODevice::ReadOnly)) {
217 218
        const BookmarkManager &manager = HelpManager::bookmarkManager();
        XbelReader reader(manager.treeBookmarkModel(), manager.listBookmarkModel());
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        if (reader.readFromFile(&file))
            return;
    }

    m_ui.errorLabel->setVisible(true);
    m_ui.errorLabel->setText(tr("There was an error while importing bookmarks!"));
}

void GeneralSettingsPage::exportBookmarks()
{
    m_ui.errorLabel->setVisible(false);

    QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"),
        "untitled.xbel", tr("Files (*.xbel)"));

    QLatin1String suffix(".xbel");
    if (!fileName.endsWith(suffix))
        fileName.append(suffix);

    QFile file(fileName);
    if (file.open(QIODevice::WriteOnly)) {
240
        XbelWriter writer(HelpManager::bookmarkManager().treeBookmarkModel());
241 242 243 244
        writer.writeToFile(&file);
    }
}

245 246
void GeneralSettingsPage::updateFontSize()
{
247 248
    const QString &family = m_font.family();
    const QString &fontStyle = m_fontDatabase.styleString(m_font);
249

250
    QList<int> pointSizes = m_fontDatabase.pointSizes(family, fontStyle);
251 252 253 254 255 256 257 258 259 260 261 262
    if (pointSizes.empty())
        pointSizes = QFontDatabase::standardSizes();

    m_ui.sizeComboBox->clear();
    m_ui.sizeComboBox->setCurrentIndex(-1);
    m_ui.sizeComboBox->setEnabled(!pointSizes.empty());

    //  try to maintain selection or select closest.
    if (!pointSizes.empty()) {
        QString n;
        foreach (int pointSize, pointSizes)
            m_ui.sizeComboBox->addItem(n.setNum(pointSize), QVariant(pointSize));
263
        const int closestIndex = closestPointSizeIndex(m_font.pointSize());
264 265 266 267 268 269 270
        if (closestIndex != -1)
            m_ui.sizeComboBox->setCurrentIndex(closestIndex);
    }
}

void GeneralSettingsPage::updateFontStyle()
{
271 272
    const QString &fontStyle = m_fontDatabase.styleString(m_font);
    const QStringList &styles = m_fontDatabase.styles(m_font.family());
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

    m_ui.styleComboBox->clear();
    m_ui.styleComboBox->setCurrentIndex(-1);
    m_ui.styleComboBox->setEnabled(!styles.empty());

    if (!styles.empty()) {
        int normalIndex = -1;
        const QString normalStyle = QLatin1String("Normal");
        foreach (const QString &style, styles) {
            // try to maintain selection or select 'normal' preferably
            const int newIndex = m_ui.styleComboBox->count();
            m_ui.styleComboBox->addItem(style);
            if (fontStyle == style) {
                m_ui.styleComboBox->setCurrentIndex(newIndex);
            } else {
                if (fontStyle ==  normalStyle)
                    normalIndex = newIndex;
            }
        }
        if (m_ui.styleComboBox->currentIndex() == -1 && normalIndex != -1)
            m_ui.styleComboBox->setCurrentIndex(normalIndex);
    }
}

void GeneralSettingsPage::updateFontFamily()
{
299
    m_ui.familyComboBox->setCurrentFont(m_font);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
}

int GeneralSettingsPage::closestPointSizeIndex(int desiredPointSize) const
{
    //  try to maintain selection or select closest.
    int closestIndex = -1;
    int closestAbsError = 0xFFFF;

    const int pointSizeCount = m_ui.sizeComboBox->count();
    for (int i = 0; i < pointSizeCount; i++) {
        const int itemPointSize = m_ui.sizeComboBox->itemData(i).toInt();
        const int absError = qAbs(desiredPointSize - itemPointSize);
        if (absError < closestAbsError) {
            closestIndex  = i;
            closestAbsError = absError;
            if (closestAbsError == 0)
                break;
        } else {    // past optimum
            if (absError > closestAbsError) {
                break;
            }
        }
    }
    return closestIndex;
}
325 326 327 328 329

bool GeneralSettingsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}