2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "docsettingspage.h"
|
2009-11-27 16:12:12 +01:00
|
|
|
#include "helpconstants.h"
|
2010-06-11 13:11:37 +02:00
|
|
|
|
|
|
|
|
#include <coreplugin/helpmanager.h>
|
2016-08-18 14:33:52 +02:00
|
|
|
#include <utils/algorithm.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
#include <QMessageBox>
|
2010-03-02 16:48:49 +01:00
|
|
|
|
2016-02-12 16:18:14 +01:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
#include <QVariant>
|
|
|
|
|
#include <QVector>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2013-08-29 19:00:34 +02:00
|
|
|
using namespace Core;
|
2016-02-12 16:18:14 +01:00
|
|
|
|
|
|
|
|
namespace Help {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class DocEntry
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QString name;
|
|
|
|
|
QString fileName;
|
|
|
|
|
QString nameSpace;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool operator<(const DocEntry &d1, const DocEntry &d2)
|
|
|
|
|
{ return d1.name < d2.name; }
|
|
|
|
|
|
|
|
|
|
static DocEntry createEntry(const QString &nameSpace, const QString &fileName, bool userManaged)
|
|
|
|
|
{
|
|
|
|
|
DocEntry result;
|
|
|
|
|
result.name = userManaged ? nameSpace : DocSettingsPage::tr("%1 (auto-detected)").arg(nameSpace);
|
|
|
|
|
result.fileName = fileName;
|
|
|
|
|
result.nameSpace = nameSpace;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DocModel : public QAbstractListModel {
|
|
|
|
|
public:
|
|
|
|
|
typedef QVector<DocEntry> DocEntries;
|
|
|
|
|
|
|
|
|
|
explicit DocModel(const DocEntries &e = DocEntries(), QObject *parent = nullptr)
|
|
|
|
|
: QAbstractListModel(parent), m_docEntries(e) {}
|
|
|
|
|
|
|
|
|
|
int rowCount(const QModelIndex & = QModelIndex()) const override { return m_docEntries.size(); }
|
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override;
|
|
|
|
|
|
|
|
|
|
void insertEntry(const DocEntry &e);
|
|
|
|
|
void removeAt(int row);
|
|
|
|
|
|
|
|
|
|
const DocEntry &entryAt(int row) const { return m_docEntries.at(row); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
DocEntries m_docEntries;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QVariant DocModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
QVariant result;
|
|
|
|
|
const int row = index.row();
|
|
|
|
|
if (index.isValid() && row < m_docEntries.size()) {
|
|
|
|
|
switch (role) {
|
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
|
result = QVariant(m_docEntries.at(row).name);
|
|
|
|
|
break;
|
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
|
result = QVariant(QDir::toNativeSeparators(m_docEntries.at(row).fileName));
|
|
|
|
|
break;
|
|
|
|
|
case Qt::UserRole:
|
|
|
|
|
result = QVariant(m_docEntries.at(row).nameSpace);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DocModel::insertEntry(const DocEntry &e)
|
|
|
|
|
{
|
|
|
|
|
const auto it = std::lower_bound(m_docEntries.begin(), m_docEntries.end(), e);
|
|
|
|
|
const int index = int(it - m_docEntries.begin());
|
|
|
|
|
beginInsertRows(QModelIndex(), index, index);
|
|
|
|
|
m_docEntries.insert(it, e);
|
|
|
|
|
endInsertRows();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DocModel::removeAt(int row)
|
|
|
|
|
{
|
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
|
m_docEntries.removeAt(row);
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Help
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace Help::Internal;
|
2009-01-13 15:41:33 +01:00
|
|
|
|
2010-03-16 15:48:09 +01:00
|
|
|
DocSettingsPage::DocSettingsPage()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-01-16 15:22:58 +01:00
|
|
|
setId("B.Documentation");
|
2012-05-22 11:17:13 +02:00
|
|
|
setDisplayName(tr("Documentation"));
|
2012-12-29 03:37:27 +01:00
|
|
|
setCategory(Help::Constants::HELP_CATEGORY);
|
2010-03-26 17:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-03 14:17:03 +01:00
|
|
|
QWidget *DocSettingsPage::widget()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-12-03 14:17:03 +01:00
|
|
|
if (!m_widget) {
|
|
|
|
|
const QStringList nameSpaces = HelpManager::registeredNamespaces();
|
2014-12-02 17:51:14 +01:00
|
|
|
const QSet<QString> userDocumentationPaths = HelpManager::userDocumentationPaths();
|
2016-02-12 16:18:14 +01:00
|
|
|
DocModel::DocEntries entries;
|
|
|
|
|
entries.reserve(nameSpaces.size());
|
2013-12-03 14:17:03 +01:00
|
|
|
foreach (const QString &nameSpace, nameSpaces) {
|
2014-12-02 17:51:14 +01:00
|
|
|
const QString filePath = HelpManager::fileFromNamespace(nameSpace);
|
|
|
|
|
bool user = userDocumentationPaths.contains(filePath);
|
2016-02-12 16:18:14 +01:00
|
|
|
entries.append(createEntry(nameSpace, filePath, user));
|
2014-12-02 17:51:14 +01:00
|
|
|
m_filesToRegister.insert(nameSpace, filePath);
|
|
|
|
|
m_filesToRegisterUserManaged.insert(nameSpace, user);
|
2013-12-03 14:17:03 +01:00
|
|
|
}
|
2016-02-12 16:18:14 +01:00
|
|
|
std::stable_sort(entries.begin(), entries.end());
|
2010-03-16 15:48:09 +01:00
|
|
|
|
2013-12-03 14:17:03 +01:00
|
|
|
m_filesToUnregister.clear();
|
2016-02-12 16:18:14 +01:00
|
|
|
|
|
|
|
|
m_widget = new QWidget;
|
|
|
|
|
m_ui.setupUi(m_widget);
|
|
|
|
|
m_model = new DocModel(entries, m_ui.docsListView);
|
|
|
|
|
m_proxyModel = new QSortFilterProxyModel(m_ui.docsListView);
|
|
|
|
|
m_proxyModel->setSourceModel(m_model);
|
|
|
|
|
m_ui.docsListView->setModel(m_proxyModel);
|
|
|
|
|
m_ui.filterLineEdit->setFiltering(true);
|
|
|
|
|
connect(m_ui.filterLineEdit, &QLineEdit::textChanged,
|
|
|
|
|
m_proxyModel, &QSortFilterProxyModel::setFilterFixedString);
|
|
|
|
|
|
|
|
|
|
connect(m_ui.addButton, &QAbstractButton::clicked, this, &DocSettingsPage::addDocumentation);
|
|
|
|
|
connect(m_ui.removeButton, &QAbstractButton::clicked, this,
|
|
|
|
|
[this] () { removeDocumentation(currentSelection()); });
|
|
|
|
|
|
|
|
|
|
m_ui.docsListView->installEventFilter(this);
|
2013-12-03 14:17:03 +01:00
|
|
|
}
|
|
|
|
|
return m_widget;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DocSettingsPage::addDocumentation()
|
|
|
|
|
{
|
2010-03-16 15:48:09 +01:00
|
|
|
const QStringList &files =
|
|
|
|
|
QFileDialog::getOpenFileNames(m_ui.addButton->parentWidget(),
|
|
|
|
|
tr("Add Documentation"), m_recentDialogPath, tr("Qt Help Files (*.qch)"));
|
2009-01-13 15:41:33 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (files.isEmpty())
|
|
|
|
|
return;
|
2010-03-16 15:48:09 +01:00
|
|
|
m_recentDialogPath = QFileInfo(files.first()).canonicalPath();
|
|
|
|
|
|
2012-05-25 16:32:37 +02:00
|
|
|
NameSpaceToPathHash docsUnableToRegister;
|
2008-12-02 12:01:29 +01:00
|
|
|
foreach (const QString &file, files) {
|
2012-05-25 16:32:37 +02:00
|
|
|
const QString filePath = QDir::cleanPath(file);
|
2013-08-29 19:00:34 +02:00
|
|
|
const QString &nameSpace = HelpManager::namespaceFromFile(filePath);
|
2012-05-25 16:32:37 +02:00
|
|
|
if (nameSpace.isEmpty()) {
|
2016-07-20 10:11:35 +02:00
|
|
|
docsUnableToRegister.insertMulti("UnknownNamespace", QDir::toNativeSeparators(filePath));
|
2012-05-25 16:32:37 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_filesToRegister.contains(nameSpace)) {
|
|
|
|
|
docsUnableToRegister.insert(nameSpace, QDir::toNativeSeparators(filePath));
|
2008-12-02 12:01:29 +01:00
|
|
|
continue;
|
2012-05-25 16:32:37 +02:00
|
|
|
}
|
2010-03-16 15:48:09 +01:00
|
|
|
|
2016-02-12 16:18:14 +01:00
|
|
|
m_model->insertEntry(createEntry(nameSpace, file, true /* user managed */));
|
|
|
|
|
|
2015-05-28 10:25:20 +02:00
|
|
|
m_filesToRegister.insert(nameSpace, filePath);
|
2014-12-02 17:51:14 +01:00
|
|
|
m_filesToRegisterUserManaged.insert(nameSpace, true/*user managed*/);
|
2012-05-25 16:32:37 +02:00
|
|
|
|
|
|
|
|
// If the files to unregister contains the namespace, grab a copy of all paths added and try to
|
|
|
|
|
// remove the current file path. Afterwards remove the whole entry and add the clean list back.
|
|
|
|
|
// Possible outcome:
|
|
|
|
|
// - might not add the entry back at all if we register the same file again
|
|
|
|
|
// - might add the entry back with paths to other files with the same namespace
|
|
|
|
|
// The reason to do this is, if we remove a file with a given namespace/ path and re-add another
|
|
|
|
|
// file with the same namespace but a different path, we need to unregister the namespace before
|
|
|
|
|
// we can register the new one. Help engine allows just one registered namespace.
|
|
|
|
|
if (m_filesToUnregister.contains(nameSpace)) {
|
|
|
|
|
QSet<QString> values = m_filesToUnregister.values(nameSpace).toSet();
|
|
|
|
|
values.remove(filePath);
|
2010-03-16 15:48:09 +01:00
|
|
|
m_filesToUnregister.remove(nameSpace);
|
2012-05-25 16:32:37 +02:00
|
|
|
foreach (const QString &value, values)
|
|
|
|
|
m_filesToUnregister.insertMulti(nameSpace, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString formatedFail;
|
2016-07-20 10:11:35 +02:00
|
|
|
if (docsUnableToRegister.contains("UnknownNamespace")) {
|
2012-05-25 16:32:37 +02:00
|
|
|
formatedFail += QString::fromLatin1("<ul><li><b>%1</b>").arg(tr("Invalid documentation file:"));
|
2016-07-20 10:11:35 +02:00
|
|
|
foreach (const QString &value, docsUnableToRegister.values("UnknownNamespace"))
|
2012-05-25 16:32:37 +02:00
|
|
|
formatedFail += QString::fromLatin1("<ul><li>%2</li></ul>").arg(value);
|
2016-07-20 10:11:35 +02:00
|
|
|
formatedFail += "</li></ul>";
|
|
|
|
|
docsUnableToRegister.remove("UnknownNamespace");
|
2012-05-25 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!docsUnableToRegister.isEmpty()) {
|
|
|
|
|
formatedFail += QString::fromLatin1("<ul><li><b>%1</b>").arg(tr("Namespace already registered:"));
|
2013-03-14 14:19:23 +01:00
|
|
|
const NameSpaceToPathHash::ConstIterator cend = docsUnableToRegister.constEnd();
|
|
|
|
|
for (NameSpaceToPathHash::ConstIterator it = docsUnableToRegister.constBegin(); it != cend; ++it) {
|
|
|
|
|
formatedFail += QString::fromLatin1("<ul><li>%1 - %2</li></ul>").arg(it.key(), it.value());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2016-07-20 10:11:35 +02:00
|
|
|
formatedFail += "</li></ul>";
|
2012-05-25 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!formatedFail.isEmpty()) {
|
|
|
|
|
QMessageBox::information(m_ui.addButton->parentWidget(), tr("Registration failed"),
|
|
|
|
|
tr("Unable to register documentation.") + formatedFail, QMessageBox::Ok);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-13 15:41:33 +01:00
|
|
|
void DocSettingsPage::apply()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-08-29 19:00:34 +02:00
|
|
|
HelpManager::unregisterDocumentation(m_filesToUnregister.keys());
|
2014-12-02 17:51:14 +01:00
|
|
|
QStringList files;
|
|
|
|
|
auto it = m_filesToRegisterUserManaged.constBegin();
|
|
|
|
|
while (it != m_filesToRegisterUserManaged.constEnd()) {
|
|
|
|
|
if (it.value()/*userManaged*/)
|
|
|
|
|
files << m_filesToRegister.value(it.key());
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
HelpManager::registerUserDocumentation(files);
|
2010-03-16 15:48:09 +01:00
|
|
|
|
|
|
|
|
m_filesToUnregister.clear();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-03 14:17:03 +01:00
|
|
|
void DocSettingsPage::finish()
|
2009-11-24 15:05:02 +01:00
|
|
|
{
|
2013-12-03 14:17:03 +01:00
|
|
|
delete m_widget;
|
2009-11-24 15:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-02 16:48:49 +01:00
|
|
|
bool DocSettingsPage::eventFilter(QObject *object, QEvent *event)
|
|
|
|
|
{
|
2016-02-12 16:18:14 +01:00
|
|
|
if (object != m_ui.docsListView)
|
2013-08-29 19:00:34 +02:00
|
|
|
return IOptionsPage::eventFilter(object, event);
|
2010-03-02 16:48:49 +01:00
|
|
|
|
|
|
|
|
if (event->type() == QEvent::KeyPress) {
|
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent*>(event);
|
|
|
|
|
switch (ke->key()) {
|
|
|
|
|
case Qt::Key_Delete:
|
2016-02-12 16:18:14 +01:00
|
|
|
removeDocumentation(currentSelection());
|
2010-03-02 16:48:49 +01:00
|
|
|
break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-29 19:00:34 +02:00
|
|
|
return IOptionsPage::eventFilter(object, event);
|
2010-03-02 16:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-12 16:18:14 +01:00
|
|
|
void DocSettingsPage::removeDocumentation(const QList<QModelIndex> &items)
|
2010-03-02 16:48:49 +01:00
|
|
|
{
|
|
|
|
|
if (items.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2016-08-18 14:33:52 +02:00
|
|
|
QList<QModelIndex> itemsByDecreasingRow = items;
|
|
|
|
|
Utils::sort(itemsByDecreasingRow, [](const QModelIndex &i1, const QModelIndex &i2) {
|
|
|
|
|
return i1.row() > i2.row();
|
|
|
|
|
});
|
|
|
|
|
foreach (const QModelIndex &item, itemsByDecreasingRow) {
|
|
|
|
|
const int row = item.row();
|
2016-02-12 16:18:14 +01:00
|
|
|
const QString nameSpace = m_model->entryAt(row).nameSpace;
|
2012-05-25 16:32:37 +02:00
|
|
|
|
|
|
|
|
m_filesToRegister.remove(nameSpace);
|
2014-12-02 17:51:14 +01:00
|
|
|
m_filesToRegisterUserManaged.remove(nameSpace);
|
2013-08-29 19:00:34 +02:00
|
|
|
m_filesToUnregister.insertMulti(nameSpace, QDir::cleanPath(HelpManager::fileFromNamespace(nameSpace)));
|
2010-03-16 15:48:09 +01:00
|
|
|
|
2016-02-12 16:18:14 +01:00
|
|
|
m_model->removeAt(row);
|
2010-03-02 16:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-18 14:33:52 +02:00
|
|
|
const int newlySelectedRow = qMax(itemsByDecreasingRow.last().row() - 1, 0);
|
2016-02-12 16:18:14 +01:00
|
|
|
const QModelIndex index = m_proxyModel->mapFromSource(m_model->index(newlySelectedRow));
|
|
|
|
|
m_ui.docsListView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
|
2010-03-02 16:48:49 +01:00
|
|
|
}
|
2010-03-16 15:48:09 +01:00
|
|
|
|
2016-02-12 16:18:14 +01:00
|
|
|
QList<QModelIndex> DocSettingsPage::currentSelection() const
|
2010-03-16 15:48:09 +01:00
|
|
|
{
|
2016-02-12 16:18:14 +01:00
|
|
|
QModelIndexList result;
|
|
|
|
|
Q_ASSERT(!m_widget.isNull());
|
|
|
|
|
foreach (const QModelIndex &index, m_ui.docsListView->selectionModel()->selectedRows())
|
|
|
|
|
result.append(m_proxyModel->mapToSource(index));
|
|
|
|
|
return result;
|
2010-03-16 15:48:09 +01:00
|
|
|
}
|