aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/help/docsettingspage.cpp
blob: 5f1938bc802ca29657f423cc7a068ca4422cc166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** 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
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "docsettingspage.h"
#include "helpconstants.h"
#include "helpmanager.h"

#include <QtCore/QCoreApplication>

#include <QtGui/QFileDialog>
#include <QtGui/QKeyEvent>
#include <QtGui/QMessageBox>

#include <QtHelp/QHelpEngineCore>

using namespace Help::Internal;

DocSettingsPage::DocSettingsPage()
{
}

QString DocSettingsPage::id() const
{
    return QLatin1String("B.Documentation");
}

QString DocSettingsPage::displayName() const
{
    return tr("Documentation");
}

QString DocSettingsPage::category() const
{
    return QLatin1String(Help::Constants::HELP_CATEGORY);
}

QString DocSettingsPage::displayCategory() const
{
    return QCoreApplication::translate("Help", Help::Constants::HELP_TR_CATEGORY);
}

QWidget *DocSettingsPage::createPage(QWidget *parent)
{
    QWidget *widget = new QWidget(parent);
    m_ui.setupUi(widget);

    connect(m_ui.addButton, SIGNAL(clicked()), this, SLOT(addDocumentation()));
    connect(m_ui.removeButton, SIGNAL(clicked()), this, SLOT(removeDocumentation()));

    m_ui.docsListWidget->installEventFilter(this);

    QHelpEngineCore *engine = &HelpManager::helpEngineCore();
    const QStringList &nameSpaces = engine->registeredDocumentations();
    foreach (const QString &nameSpace, nameSpaces)
        addItem(nameSpace, engine->documentationFileName(nameSpace));

    m_filesToRegister.clear();
    m_filesToUnregister.clear();

    if (m_searchKeywords.isEmpty())
        m_searchKeywords = m_ui.groupBox->title();
    return widget;
}

void DocSettingsPage::addDocumentation()
{
    const QStringList &files =
        QFileDialog::getOpenFileNames(m_ui.addButton->parentWidget(),
        tr("Add Documentation"), m_recentDialogPath, tr("Qt Help Files (*.qch)"));

    if (files.isEmpty())
        return;
    m_recentDialogPath = QFileInfo(files.first()).canonicalPath();

    const QHelpEngineCore &engine = HelpManager::helpEngineCore();
    const QStringList &nameSpaces = engine.registeredDocumentations();

    foreach (const QString &file, files) {
        const QString &nameSpace = engine.namespaceName(file);
        if (nameSpace.isEmpty())
            continue;

        if (m_filesToUnregister.value(nameSpace) != QDir::cleanPath(file)) {
            if (!m_filesToRegister.contains(nameSpace) && !nameSpaces.contains(nameSpace)) {
                addItem(nameSpace, file);
                m_filesToRegister.insert(nameSpace, QDir::cleanPath(file));
            }
        } else {
            addItem(nameSpace, file);
            m_filesToUnregister.remove(nameSpace);
        }
    }
}

void DocSettingsPage::removeDocumentation()
{
    removeDocumentation(m_ui.docsListWidget->selectedItems());
}

void DocSettingsPage::apply()
{
    HelpManager* manager = &HelpManager::instance();
    manager->unregisterDocumentation(m_filesToUnregister.keys());
    manager->registerDocumentation(m_filesToRegister.values());
    if (manager->guiEngineNeedsUpdate()) {
        // emit this signal to the help plugin, since we don't want
        // to force gui help engine setup if we are not in help mode
        emit documentationChanged();
    }

    m_filesToRegister.clear();
    m_filesToUnregister.clear();
}

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

bool DocSettingsPage::eventFilter(QObject *object, QEvent *event)
{
    if (object != m_ui.docsListWidget)
        return Core::IOptionsPage::eventFilter(object, event);

    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *ke = static_cast<QKeyEvent*>(event);
        switch (ke->key()) {
            case Qt::Key_Delete:
                removeDocumentation(m_ui.docsListWidget->selectedItems());
            break;
            default: break;
        }
    }

    return Core::IOptionsPage::eventFilter(object, event);
}

void DocSettingsPage::removeDocumentation(const QList<QListWidgetItem*> items)
{
    if (items.isEmpty())
        return;

    int row = 0;
    QHelpEngineCore *engine = &HelpManager::helpEngineCore();
    foreach (QListWidgetItem* item, items) {
        const QString &nameSpace = item->text();
        const QString &docPath = engine->documentationFileName(nameSpace);

        if (m_filesToRegister.value(nameSpace) != docPath) {
            if (!m_filesToUnregister.contains(nameSpace))
                m_filesToUnregister.insert(nameSpace, docPath);
        } else {
            m_filesToRegister.remove(nameSpace);
        }

        row = m_ui.docsListWidget->row(item);
        delete m_ui.docsListWidget->takeItem(row);
    }

    m_ui.docsListWidget->setCurrentRow(qMax(row - 1, 0),
        QItemSelectionModel::ClearAndSelect);
}

void DocSettingsPage::addItem(const QString &nameSpace, const QString &fileName)
{
    QListWidgetItem* item = new QListWidgetItem(nameSpace);
    item->setToolTip(fileName);
    m_ui.docsListWidget->addItem(item);
}