Files
qt-creator/src/plugins/help/docsettingspage.cpp

200 lines
5.8 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2010-03-05 11:25:49 +01:00
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** 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
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#include "docsettingspage.h"
#include "helpconstants.h"
#include "helpmanager.h"
2008-12-02 12:01:29 +01:00
#include <QtCore/QCoreApplication>
2008-12-02 12:01:29 +01:00
#include <QtGui/QFileDialog>
#include <QtGui/QKeyEvent>
2008-12-02 12:01:29 +01:00
#include <QtGui/QMessageBox>
#include <QtHelp/QHelpEngineCore>
2008-12-02 12:01:29 +01:00
using namespace Help::Internal;
DocSettingsPage::DocSettingsPage()
2008-12-02 12:01:29 +01:00
{
}
QString DocSettingsPage::id() const
2008-12-02 12:01:29 +01:00
{
return QLatin1String("B.Documentation");
2008-12-02 12:01:29 +01:00
}
QString DocSettingsPage::displayName() const
{
return tr("Documentation");
}
2008-12-02 12:01:29 +01:00
QString DocSettingsPage::category() const
{
return QLatin1String(Help::Constants::HELP_CATEGORY);
2008-12-02 12:01:29 +01:00
}
QString DocSettingsPage::displayCategory() const
2008-12-02 12:01:29 +01:00
{
return QCoreApplication::translate("Help", Help::Constants::HELP_TR_CATEGORY);
2008-12-02 12:01:29 +01:00
}
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;
2008-12-02 12:01:29 +01:00
}
void DocSettingsPage::addDocumentation()
{
const QStringList &files =
QFileDialog::getOpenFileNames(m_ui.addButton->parentWidget(),
tr("Add Documentation"), m_recentDialogPath, tr("Qt Help Files (*.qch)"));
2008-12-02 12:01:29 +01:00
if (files.isEmpty())
return;
m_recentDialogPath = QFileInfo(files.first()).canonicalPath();
const QHelpEngineCore &engine = HelpManager::helpEngineCore();
const QStringList &nameSpaces = engine.registeredDocumentations();
2008-12-02 12:01:29 +01:00
foreach (const QString &file, files) {
const QString &nameSpace = engine.namespaceName(file);
if (nameSpace.isEmpty())
2008-12-02 12:01:29 +01:00
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);
2008-12-02 12:01:29 +01:00
}
}
}
void DocSettingsPage::removeDocumentation()
{
removeDocumentation(m_ui.docsListWidget->selectedItems());
2008-12-02 12:01:29 +01:00
}
void DocSettingsPage::apply()
2008-12-02 12:01:29 +01:00
{
emit dialogAccepted();
emit documentationChanged();
m_filesToRegister.clear();
m_filesToUnregister.clear();
2008-12-02 12:01:29 +01:00
}
bool DocSettingsPage::matches(const QString &s) const
{
return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}
QStringList DocSettingsPage::docsToRegister() const
2008-12-02 12:01:29 +01:00
{
return m_filesToRegister.values();
}
QStringList DocSettingsPage::docsToUnregister() const
{
return m_filesToUnregister.keys();
2008-12-02 12:01:29 +01:00
}
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);
}