Commit b1411d54 authored by kh1's avatar kh1
Browse files

Implement open pages classes to handle doc files like edit mode.

Based on the implementation done by ck, slightly modified and thus put
under the private help internal namespace.
parent a3a531c5
/**************************************************************************
**
** 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 "openpagesmanager.h"
#include "centralwidget.h"
#include "helpconstants.h"
#include "helpmanager.h"
#include "helpviewer.h"
#include "openpagesmodel.h"
#include "openpageswidget.h"
#include <QtGui/QComboBox>
#include <QtGui/QTreeView>
#include <QtHelp/QHelpEngineCore>
using namespace Help::Internal;
OpenPagesManager *OpenPagesManager::m_instance = 0;
// -- OpenPagesManager
OpenPagesManager::OpenPagesManager(QObject *parent)
: QObject(parent)
, m_comboBox(0)
, m_model(0)
, m_openPagesWidget(0)
{
Q_ASSERT(!m_instance);
m_instance = this;
m_model = new OpenPagesModel(this);
m_openPagesWidget = new OpenPagesWidget(m_model);
connect(m_openPagesWidget, SIGNAL(setCurrentPage(QModelIndex)), this,
SLOT(setCurrentPage(QModelIndex)));
connect(m_openPagesWidget, SIGNAL(closePage(QModelIndex)), this,
SLOT(closePage(QModelIndex)));
connect(m_openPagesWidget, SIGNAL(closePagesExcept(QModelIndex)), this,
SLOT(closePagesExcept(QModelIndex)));
m_comboBox = new QComboBox;
m_comboBox->setModel(m_model);
m_comboBox->setMinimumContentsLength(40);
connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(setCurrentPage(int)));
}
OpenPagesManager &OpenPagesManager::instance()
{
Q_ASSERT(m_instance);
return *m_instance;
}
QWidget* OpenPagesManager::openPagesWidget() const
{
return m_openPagesWidget;
}
QComboBox* OpenPagesManager::openPagesComboBox() const
{
if (!m_comboBox) {
}
return m_comboBox;
}
int OpenPagesManager::pageCount() const
{
return m_model->rowCount();
}
QStringList splitString(const QVariant &value)
{
using namespace Help::Constants;
return value.toString().split(ListSeparator, QString::SkipEmptyParts);
}
void OpenPagesManager::setupInitialPages()
{
const QHelpEngineCore &engine = HelpManager::helpEngineCore();
const int option = engine.customValue(QLatin1String("StartOption"),
Help::Constants::ShowLastPages).toInt();
QString homePage = engine.customValue(QLatin1String("DefaultHomePage"),
Help::Constants::AboutBlank).toString();
int initialPage = 0;
switch (option) {
case Help::Constants::ShowHomePage: {
m_model->addPage(engine.customValue(QLatin1String("HomePage"),
homePage).toString());
} break;
case Help::Constants::ShowBlankPage: {
m_model->addPage(QUrl(Help::Constants::AboutBlank));
} break;
case Help::Constants::ShowLastPages: {
const QStringList &lastShownPageList = splitString(engine
.customValue(QLatin1String("LastShownPages")));
const int pageCount = lastShownPageList.count();
if (pageCount > 0) {
QStringList zoomFactors = splitString(engine
.customValue(QLatin1String("LastShownPagesZoom")));
while (zoomFactors.count() < pageCount)
zoomFactors.append(Help::Constants::DefaultZoomFactor);
initialPage = engine.customValue(QLatin1String("LastTabPage"), 0).toInt();
for (int curPage = 0; curPage < pageCount; ++curPage) {
const QString &curFile = lastShownPageList.at(curPage);
if (engine.findFile(curFile).isValid()
|| curFile == Help::Constants::AboutBlank) {
m_model->addPage(curFile, zoomFactors.at(curPage).toFloat());
} else if (curPage <= initialPage && initialPage > 0) {
--initialPage;
}
}
}
} break;
default: break;
}
if (m_model->rowCount() == 0)
m_model->addPage(homePage);
for (int i = 0; i < m_model->rowCount(); ++i)
CentralWidget::instance()->addPage(m_model->pageAt(i));
setCurrentPage(initialPage);
}
// -- public slots
HelpViewer *OpenPagesManager::createPage()
{
return createPage(QUrl(Help::Constants::AboutBlank));
}
HelpViewer *OpenPagesManager::createPageFromSearch(const QUrl &url)
{
return createPage(url, true);
}
HelpViewer *OpenPagesManager::createPage(const QUrl &url, bool fromSearch)
{
if (HelpViewer::launchWithExternalApp(url))
return 0;
m_model->addPage(url);
const int index = m_model->rowCount() - 1;
HelpViewer * const page = m_model->pageAt(index);
CentralWidget::instance()->addPage(page, fromSearch);
emit pagesChanged();
setCurrentPage(index);
return page;
}
void OpenPagesManager::setCurrentPage(int index)
{
m_comboBox->setCurrentIndex(index);
CentralWidget::instance()->setCurrentPage(m_model->pageAt(index));
selectCurrentPage(); // update the selction inside the tree view
}
void OpenPagesManager::setCurrentPage(const QModelIndex &index)
{
if (index.isValid()) {
m_comboBox->setCurrentIndex(index.row());
CentralWidget::instance()->setCurrentPage(m_model->pageAt(index.row()));
}
}
void OpenPagesManager::closeCurrentPage()
{
QModelIndexList indexes = m_openPagesWidget->selectionModel()->selectedRows();
if (indexes.isEmpty())
return;
Q_ASSERT(indexes.count() == 1);
removePage(indexes.first().row());
}
void OpenPagesManager::closePage(const QModelIndex &index)
{
if (index.isValid())
removePage(index.row());
}
void OpenPagesManager::closePagesExcept(const QModelIndex &index)
{
if (index.isValid()) {
int i = 0;
HelpViewer *viewer = m_model->pageAt(index.row());
while (m_model->rowCount() > 1) {
if (m_model->pageAt(i) != viewer) {
removePage(i);
} else {
i++;
}
}
}
}
// -- private
void OpenPagesManager::selectCurrentPage()
{
QItemSelectionModel * selModel = m_openPagesWidget->selectionModel();
selModel->clearSelection();
selModel->select(m_model->index(CentralWidget::instance()->currentIndex(), 0),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
m_openPagesWidget->scrollTo(m_openPagesWidget->currentIndex());
}
void OpenPagesManager::removePage(int index)
{
Q_ASSERT(m_model->rowCount() > 1);
m_model->removePage(index);
CentralWidget::instance()->removePage(index);
emit pagesChanged();
selectCurrentPage(); // update the selction inside the tree view
}
/**************************************************************************
**
** 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.
**
**************************************************************************/
#ifndef OPENPAGESMANAGER_H
#define OPENPAGESMANAGER_H
#include <QtCore/QObject>
QT_FORWARD_DECLARE_CLASS(QComboBox)
QT_FORWARD_DECLARE_CLASS(QListView)
QT_FORWARD_DECLARE_CLASS(QModelIndex)
QT_FORWARD_DECLARE_CLASS(QTreeView)
QT_FORWARD_DECLARE_CLASS(QUrl)
QT_FORWARD_DECLARE_CLASS(QWidget)
class HelpViewer;
namespace Help {
namespace Internal {
class OpenPagesModel;
class OpenPagesManager : public QObject
{
Q_OBJECT
public:
OpenPagesManager(QObject *parent = 0);
static OpenPagesManager &instance();
QWidget* openPagesWidget() const;
QComboBox* openPagesComboBox() const;
int pageCount() const;
void setupInitialPages();
public slots:
HelpViewer *createPage();
HelpViewer *createPageFromSearch(const QUrl &url);
HelpViewer *createPage(const QUrl &url, bool fromSearch = false);
void setCurrentPage(int index);
void setCurrentPage(const QModelIndex &index);
void closeCurrentPage();
void closePage(const QModelIndex &index);
void closePagesExcept(const QModelIndex &index);
signals:
void pagesChanged();
private:
void selectCurrentPage();
void removePage(int index);
private:
QComboBox *m_comboBox;
OpenPagesModel *m_model;
QTreeView *m_openPagesWidget;
static OpenPagesManager *m_instance;
};
} // namespace Internal
} // namespace Help
#endif // OPENPAGESMANAGER_H
/**************************************************************************
**
** 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 "openpagesmodel.h"
#include "helpviewer.h"
#include <QtCore/QUrl>
using namespace Help::Internal;
OpenPagesModel::OpenPagesModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
int OpenPagesModel::columnCount(const QModelIndex &/*parent*/) const
{
return 2;
}
int OpenPagesModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_pages.count();
}
QVariant OpenPagesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= rowCount() || role != Qt::DisplayRole)
return QVariant();
QString title = m_pages.at(index.row())->title();
title.replace(QLatin1Char('&'), QLatin1String("&&"));
return title.isEmpty() ? tr("(Untitled)") : title;
}
void OpenPagesModel::addPage(const QUrl &url, qreal zoom)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
HelpViewer *page = new HelpViewer(zoom);
connect(page, SIGNAL(titleChanged()), this, SLOT(handleTitleChanged()));
m_pages << page;
endInsertRows();
page->setSource(url);
}
void OpenPagesModel::removePage(int index)
{
Q_ASSERT(index >= 0 && index < rowCount());
beginRemoveRows(QModelIndex(), index, index);
HelpViewer *page = m_pages.at(index);
m_pages.removeAt(index);
endRemoveRows();
page->deleteLater();
}
HelpViewer *OpenPagesModel::pageAt(int index) const
{
Q_ASSERT(index >= 0 && index < rowCount());
return m_pages.at(index);
}
void OpenPagesModel::handleTitleChanged()
{
HelpViewer *page = static_cast<HelpViewer *>(sender());
const int row = m_pages.indexOf(page);
Q_ASSERT(row != -1 );
const QModelIndex &item = index(row, 0);
emit dataChanged(item, item);
}
/**************************************************************************
**
** 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.
**
**************************************************************************/
#ifndef OPENPAGESMODEL_H
#define OPENPAGESMODEL_H
#include <QtCore/QList>
#include <QtCore/QAbstractTableModel>
QT_FORWARD_DECLARE_CLASS(QUrl)
class HelpViewer;
namespace Help {
namespace Internal {
class OpenPagesModel : public QAbstractTableModel
{
Q_OBJECT
public:
OpenPagesModel(QObject *parent);
int columnCount(const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void addPage(const QUrl &url, qreal zoom = 0);
void removePage(int index);
HelpViewer *pageAt(int index) const;
private slots:
void handleTitleChanged();
private:
QList<HelpViewer *> m_pages;
};
} // namespace Internal
} // namespace Help
#endif // OPENPAGESMODEL_H
/**************************************************************************
**
** 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 "openpageswidget.h"
#include "openpagesmodel.h"
#include <QtGui/QApplication>
#include <QtGui/QPainter>
#include <QtGui/QHeaderView>
#include <QtGui/QKeyEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QMenu>
#ifdef Q_WS_MAC
#include <qmacstyle_mac.h>
#endif
using namespace Help::Internal;
// -- OpenPagesDelegate
OpenPagesDelegate::OpenPagesDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void OpenPagesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (option.state & QStyle::State_MouseOver) {
if ((QApplication::mouseButtons() & Qt::LeftButton) == 0)
pressedIndex = QModelIndex();
QBrush brush = option.palette.alternateBase();
if (index == pressedIndex)
brush = option.palette.dark();
painter->fillRect(option.rect, brush);
}
QStyledItemDelegate::paint(painter, option, index);
if (index.column() == 1 && option.state & QStyle::State_MouseOver) {
QIcon icon((option.state & QStyle::State_Selected)
? ":/core/images/closebutton.png" : ":/core/images/darkclosebutton.png");
const QRect iconRect(option.rect.right() - option.rect.height(),
option.rect.top(), option.rect.height(), option.rect.height());
icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
}
}
// -- OpenPagesWidget
OpenPagesWidget::OpenPagesWidget(OpenPagesModel *model)
{
setModel(model);
setIndentation(0);
setItemDelegate((m_delegate = new OpenPagesDelegate(this)));
setFrameStyle(QFrame::NoFrame);
setTextElideMode(Qt::ElideMiddle);
setAttribute(Qt::WA_MacShowFocusRect, false);
viewport()->setAttribute(Qt::WA_Hover);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection);
header()->hide();
header()->setStretchLastSection(false);
header()->setResizeMode(0, QHeaderView::Stretch);
header()->setResizeMode(1, QHeaderView::Fixed);