Commit edcefd69 authored by kh1's avatar kh1
Browse files

Implement tabless help mode, behaves now like edit mode.

parent a8801d15
......@@ -28,6 +28,7 @@
**************************************************************************/
#include "centralwidget.h"
#include "helpmanager.h"
#include "helpviewer.h"
#include "topicchooser.h"
......@@ -35,88 +36,36 @@
#include <QtCore/QEvent>
#include <QtCore/QTimer>
#include <QtGui/QMenu>
#include <QtGui/QLabel>
#include <QtGui/QKeyEvent>
#include <QtGui/QLayout>
#include <QtGui/QPageSetupDialog>
#include <QtGui/QPrinter>
#include <QtGui/QLineEdit>
#include <QtGui/QCheckBox>
#include <QtGui/QTabBar>
#include <QtGui/QTabWidget>
#include <QtGui/QToolButton>
#include <QtGui/QMouseEvent>
#include <QtGui/QFocusEvent>
#include <QtGui/QMainWindow>
#include <QtGui/QSpacerItem>
#include <QtGui/QTextCursor>
#include <QtGui/QPrintDialog>
#include <QtGui/QApplication>
#include <QtGui/QTextDocumentFragment>
#include <QtGui/QPrintPreviewDialog>
#include <QtGui/QPageSetupDialog>
#include <QtGui/QStackedWidget>
#include <QtHelp/QHelpEngine>
#include <QtHelp/QHelpEngineCore>
#include <coreplugin/coreconstants.h>
#include <QtHelp/QHelpSearchEngine>
using namespace Help::Internal;
namespace {
HelpViewer* helpViewerFromTabPosition(const QTabWidget *widget,
const QPoint &point)
{
QTabBar *tabBar = qFindChild<QTabBar*>(widget);
for (int i = 0; i < tabBar->count(); ++i) {
if (tabBar->tabRect(i).contains(point))
return qobject_cast<HelpViewer*>(widget->widget(i));
}
return 0;
}
Help::Internal::CentralWidget *staticCentralWidget = 0;
}
CentralWidget *gStaticCentralWidget = 0;
// -- CentralWidget
CentralWidget::CentralWidget(QWidget *parent)
: QWidget(parent)
, findBar(0)
, tabWidget(0)
, printer(0)
, m_stackedWidget(0)
{
lastTabPage = 0;
globalActionList.clear();
tabWidget = new QTabWidget;
tabWidget->setDocumentMode(true);
tabWidget->setMovable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
connect(tabWidget, SIGNAL(currentChanged(int)), this,
SLOT(currentPageChanged(int)));
QToolButton *newTabButton = new QToolButton(this);
newTabButton->setAutoRaise(true);
newTabButton->setToolTip(tr("Add new page"));
newTabButton->setIcon(QIcon(
#ifdef Q_OS_MAC
QLatin1String(":/trolltech/assistant/images/mac/addtab.png")));
#else
QLatin1String(":/trolltech/assistant/images/win/addtab.png")));
#endif
tabWidget->setCornerWidget(newTabButton, Qt::TopLeftCorner);
connect(newTabButton, SIGNAL(clicked()), this, SLOT(newTab()));
Q_ASSERT(!gStaticCentralWidget);
gStaticCentralWidget = this;
QVBoxLayout *vboxLayout = new QVBoxLayout(this);
vboxLayout->setMargin(0);
vboxLayout->addWidget(tabWidget);
QTabBar *tabBar = qFindChild<QTabBar*>(tabWidget);
if (tabBar) {
tabBar->installEventFilter(this);
tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tabBar, SIGNAL(customContextMenuRequested(QPoint)), this,
SLOT(showTabBarContextMenu(QPoint)));
}
staticCentralWidget = this;
m_stackedWidget = new QStackedWidget(this);
vboxLayout->addWidget(m_stackedWidget);
}
CentralWidget::~CentralWidget()
......@@ -125,597 +74,319 @@ CentralWidget::~CentralWidget()
delete printer;
#endif
QString zoomCount;
QString zoomFactors;
QString currentPages;
for (int i = 0; i < tabWidget->count(); ++i) {
HelpViewer *viewer = qobject_cast<HelpViewer*>(tabWidget->widget(i));
if (viewer && viewer->source().isValid()) {
currentPages += (viewer->source().toString() + QLatin1Char('|'));
zoomCount += QString::number(viewer->zoom()) + QLatin1Char('|');
for (int i = 0; i < m_stackedWidget->count(); ++i) {
const HelpViewer * const viewer = viewerAt(i);
const QUrl &source = viewer->source();
if (source.isValid()) {
currentPages += source.toString() + QLatin1Char('|');
zoomFactors += QString::number(viewer->scale()) + QLatin1Char('|');
}
}
QHelpEngineCore *engine = &HelpManager::helpEngineCore();
engine->setCustomValue(QLatin1String("LastTabPage"), lastTabPage);
engine->setCustomValue(QLatin1String("LastShownPages"), currentPages);
engine->setCustomValue(QLatin1String("LastShownPagesZoom"), zoomCount);
engine->setCustomValue(QLatin1String("LastShownPagesZoom"), zoomFactors);
engine->setCustomValue(QLatin1String("LastTabPage"), currentIndex());
}
CentralWidget *CentralWidget::instance()
{
return staticCentralWidget;
}
void CentralWidget::newTab()
{
HelpViewer* viewer = currentHelpViewer();
#if !defined(QT_NO_WEBKIT)
if (viewer && viewer->hasLoadFinished())
#else
if (viewer)
#endif
setSourceInNewTab(viewer->source());
Q_ASSERT(gStaticCentralWidget);
return gStaticCentralWidget;
}
void CentralWidget::zoomIn()
bool CentralWidget::hasSelection() const
{
HelpViewer* viewer = currentHelpViewer();
if (viewer)
viewer->zoomIn();
if (HelpViewer* viewer = currentHelpViewer())
return !viewer->selectedText().isEmpty();
return false;
}
void CentralWidget::zoomOut()
bool CentralWidget::isForwardAvailable() const
{
HelpViewer* viewer = currentHelpViewer();
const HelpViewer* viewer = currentHelpViewer();
if (viewer)
viewer->zoomOut();
}
return viewer->isForwardAvailable();
void CentralWidget::nextPage()
{
int index = tabWidget->currentIndex() + 1;
if (index >= tabWidget->count())
index = 0;
tabWidget->setCurrentIndex(index);
return false;
}
void CentralWidget::resetZoom()
bool CentralWidget::isBackwardAvailable() const
{
HelpViewer* viewer = currentHelpViewer();
const HelpViewer* viewer = currentHelpViewer();
if (viewer)
viewer->resetZoom();
}
return viewer->isBackwardAvailable();
void CentralWidget::previousPage()
{
int index = tabWidget->currentIndex() -1;
if (index < 0)
index = tabWidget->count() -1;
tabWidget->setCurrentIndex(index);
return false;
}
void CentralWidget::closeTab()
HelpViewer* CentralWidget::viewerAt(int index) const
{
closeTab(tabWidget->currentIndex());
return qobject_cast<HelpViewer*> (m_stackedWidget->widget(index));
}
void CentralWidget::closeTab(int index)
HelpViewer* CentralWidget::currentHelpViewer() const
{
HelpViewer* viewer = helpViewerAtIndex(index);
if (!viewer || tabWidget->count() == 1)
return;
emit viewerAboutToBeRemoved(index);
tabWidget->removeTab(index);
emit viewerRemoved(index);
QTimer::singleShot(0, viewer, SLOT(deleteLater()));
return qobject_cast<HelpViewer*> (m_stackedWidget->currentWidget());
}
void CentralWidget::setSource(const QUrl &url)
void CentralWidget::addPage(HelpViewer *page, bool fromSearch)
{
HelpViewer* viewer = currentHelpViewer();
HelpViewer* lastViewer =
qobject_cast<HelpViewer*>(tabWidget->widget(lastTabPage));
if (!viewer && !lastViewer) {
viewer = new HelpViewer(this, this);
viewer->installEventFilter(this);
lastTabPage = tabWidget->addTab(viewer, QString());
tabWidget->setCurrentIndex(lastTabPage);
connectSignals();
qApp->processEvents();
} else {
viewer = lastViewer;
}
viewer->setSource(url);
currentPageChanged(lastTabPage);
viewer->setFocus(Qt::OtherFocusReason);
tabWidget->setCurrentIndex(lastTabPage);
tabWidget->setTabText(lastTabPage, quoteTabTitle(viewer->documentTitle()));
page->installEventFilter(this);
page->setFocus(Qt::OtherFocusReason);
connectSignals(page);
m_stackedWidget->addWidget(page);
if (fromSearch) {
connect(currentHelpViewer(), SIGNAL(loadFinished(bool)), this,
SLOT(highlightSearchTerms()));
}
}
void CentralWidget::setLastShownPages()
void CentralWidget::removePage(int index)
{
const QHelpEngineCore &engine = HelpManager::helpEngineCore();
QString value = engine.customValue(QLatin1String("LastShownPages"),
QString()).toString();
const QStringList lastShownPageList = value.split(QLatin1Char('|'),
QString::SkipEmptyParts);
const int pageCount = lastShownPageList.count();
QString homePage = engine.customValue(QLatin1String("DefaultHomePage"),
QLatin1String("about:blank")).toString();
int option = engine.customValue(QLatin1String("StartOption"), 2).toInt();
if (option == 0 || option == 1 || pageCount <= 0) {
if (option == 0) {
homePage = engine.customValue(QLatin1String("HomePage"),
homePage).toString();
} else if (option == 1) {
homePage = QLatin1String("about:blank");
}
setSource(homePage);
return;
}
value = engine.customValue(QLatin1String("LastShownPagesZoom"),
QString()).toString();
QVector<QString> zoomVector = value.split(QLatin1Char('|'),
QString::SkipEmptyParts).toVector();
const int zoomCount = zoomVector.count();
zoomVector.insert(zoomCount, pageCount - zoomCount, QLatin1String("0"));
QVector<QString>::const_iterator zIt = zoomVector.constBegin();
QStringList::const_iterator it = lastShownPageList.constBegin();
for (; it != lastShownPageList.constEnd(); ++it, ++zIt)
setSourceInNewTab((*it), (*zIt).toInt());
int tab = engine.customValue(QLatin1String("LastTabPage"), 0).toInt();
tabWidget->setCurrentIndex(tab);
const bool currentChanged = (index == currentIndex());
m_stackedWidget->removeWidget(m_stackedWidget->widget(index));
if (currentChanged)
emit currentViewerChanged();
}
bool CentralWidget::hasSelection() const
int CentralWidget::currentIndex() const
{
const HelpViewer* viewer = currentHelpViewer();
return viewer ? viewer->hasSelection() : false;
return m_stackedWidget->currentIndex();
}
QUrl CentralWidget::currentSource() const
void CentralWidget::setCurrentPage(HelpViewer *page)
{
const HelpViewer* viewer = currentHelpViewer();
if (viewer)
return viewer->source();
return QUrl();
m_stackedWidget->setCurrentWidget(page);
emit currentViewerChanged();
}
QString CentralWidget::currentTitle() const
bool CentralWidget::find(const QString &txt, Find::IFindSupport::FindFlags flags,
bool incremental)
{
const HelpViewer* viewer = currentHelpViewer();
if (viewer)
return viewer->documentTitle();
return QString();
return currentHelpViewer()->findText(txt, flags, incremental, false);
}
void CentralWidget::initPrinter()
{
#ifndef QT_NO_PRINTER
if (!printer)
printer = new QPrinter(QPrinter::HighResolution);
#endif
}
// -- public slots
void CentralWidget::print()
void CentralWidget::copy()
{
#ifndef QT_NO_PRINTER
HelpViewer* viewer = currentHelpViewer();
if (!viewer)
return;
initPrinter();
QPrintDialog *dlg = new QPrintDialog(printer, this);
#if defined(QT_NO_WEBKIT)
if (viewer->textCursor().hasSelection())
dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
#endif
dlg->addEnabledOption(QAbstractPrintDialog::PrintPageRange);
dlg->addEnabledOption(QAbstractPrintDialog::PrintCollateCopies);
dlg->setWindowTitle(tr("Print Document"));
if (dlg->exec() == QDialog::Accepted) {
viewer->print(printer);
}
delete dlg;
#endif
if (HelpViewer* viewer = currentHelpViewer())
viewer->copy();
}
void CentralWidget::printPreview()
void CentralWidget::home()
{
#ifndef QT_NO_PRINTER
initPrinter();
QPrintPreviewDialog preview(printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)),
SLOT(printPreview(QPrinter*)));
preview.exec();
#endif
if (HelpViewer* viewer = currentHelpViewer())
viewer->home();
}
void CentralWidget::printPreview(QPrinter *p)
void CentralWidget::zoomIn()
{
#ifndef QT_NO_PRINTER
HelpViewer *viewer = currentHelpViewer();
HelpViewer* viewer = currentHelpViewer();
if (viewer)
viewer->print(p);
#else
Q_UNUSED(p)
#endif
}
void CentralWidget::pageSetup()
{
#ifndef QT_NO_PRINTER
initPrinter();
QPageSetupDialog dlg(printer);
dlg.exec();
#endif
viewer->scaleUp();
}
void CentralWidget::home()
void CentralWidget::zoomOut()
{
HelpViewer* viewer = currentHelpViewer();
if (viewer)
viewer->home();
viewer->scaleDown();
}
bool CentralWidget::isForwardAvailable() const
void CentralWidget::resetZoom()
{
const HelpViewer* viewer = currentHelpViewer();
HelpViewer* viewer = currentHelpViewer();
if (viewer)
return viewer->isForwardAvailable();
return false;
viewer->resetScale();
}
void CentralWidget::forward()
{
HelpViewer* viewer = currentHelpViewer();
if (viewer)
if (HelpViewer* viewer = currentHelpViewer())
viewer->forward();
}
bool CentralWidget::isBackwardAvailable() const
void CentralWidget::nextPage()
{
const HelpViewer* viewer = currentHelpViewer();
if (viewer)
return viewer->isBackwardAvailable();
return false;
m_stackedWidget->setCurrentIndex((m_stackedWidget->currentIndex() + 1)
% m_stackedWidget->count());
}
void CentralWidget::backward()
{
HelpViewer* viewer = currentHelpViewer();
if (viewer)
if (HelpViewer* viewer = currentHelpViewer())
viewer->backward();
}
QList<QAction*> CentralWidget::globalActions() const
{
return globalActionList;
}
void CentralWidget::setGlobalActions(const QList<QAction*> &actions)
void CentralWidget::previousPage()
{
globalActionList = actions;
m_stackedWidget->setCurrentIndex((m_stackedWidget->currentIndex() - 1)
% m_stackedWidget->count());
}
void CentralWidget::setSourceInNewTab(const QUrl &url, int zoom)
void CentralWidget::print()
{
HelpViewer* viewer = new HelpViewer(this, this);
viewer->installEventFilter(this);
viewer->setZoom(zoom);
viewer->setSource(url);
viewer->setFocus(Qt::OtherFocusReason);
#if defined(QT_NO_WEBKIT)
QFont font = viewer->font();
font.setPointSize(font.pointSize() + int(zoom));
viewer->setFont(font);
#ifndef QT_NO_PRINTER
if (HelpViewer* viewer = currentHelpViewer()) {
initPrinter();
QPrintDialog *dlg = new QPrintDialog(printer, this);
dlg->setWindowTitle(tr("Print Document"));
if (!viewer->selectedText().isEmpty())
dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
dlg->addEnabledOption(QAbstractPrintDialog::PrintPageRange);
dlg->addEnabledOption(QAbstractPrintDialog::PrintCollateCopies);
if (dlg->exec() == QDialog::Accepted)
viewer->print(printer);
delete dlg;
}
#endif
tabWidget->setCurrentIndex(tabWidget->addTab(viewer,
quoteTabTitle(viewer->documentTitle())));
connectSignals();
}
HelpViewer *CentralWidget::newEmptyTab()
void CentralWidget::pageSetup()
{
HelpViewer* viewer = new HelpViewer(this, this);
viewer->installEventFilter(this);
viewer->setFocus(Qt::OtherFocusReason);
#if defined(QT_NO_WEBKIT)
viewer->setDocumentTitle(tr("unknown"));
#ifndef QT_NO_PRINTER
initPrinter();
QPageSetupDialog dlg(printer);
dlg.exec();
#endif
tabWidget->setCurrentIndex(tabWidget->addTab(viewer, tr("unknown")));
connectSignals();
return viewer;
}
void CentralWidget::connectSignals()
{
const HelpViewer* viewer = currentHelpViewer();
if (viewer) {
connect(viewer, SIGNAL(copyAvailable(bool)), this,
SIGNAL(copyAvailable(bool)));
connect(viewer, SIGNAL(forwardAvailable(bool)), this,
SIGNAL(forwardAvailable(bool)));
connect(viewer, SIGNAL(backwardAvailable(bool)), this,
SIGNAL(backwardAvailable(bool)));
connect(viewer, SIGNAL(sourceChanged(QUrl)), this,
SIGNAL(sourceChanged(QUrl)));
connect(viewer, SIGNAL(highlighted(QString)), this,
SIGNAL(highlighted(QString)));
connect(viewer, SIGNAL(sourceChanged(QUrl)), this,
SLOT(setTabTitle(QUrl)));
}
}
HelpViewer *CentralWidget::helpViewerAtIndex(int index) const
{
return qobject_cast<HelpViewer*>(tabWidget->widget(index));
}
int CentralWidget::indexOf(HelpViewer *viewer) const
{
if (!viewer)
return -1;
return tabWidget->indexOf(viewer);
}
HelpViewer *CentralWidget::currentHelpViewer() const
void CentralWidget::printPreview()
{
return qobject_cast<HelpViewer*>(tabWidget->currentWidget());
#ifndef QT_NO_PRINTER
initPrinter();
QPrintPreviewDialog preview(printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)),
SLOT(printPreview(QPrinter*)));
preview.exec();
#endif
}
void CentralWidget::activateTab(bool onlyHelpViewer)
void CentralWidget::setSource(const QUrl &url)
{
if (currentHelpViewer()) {
currentHelpViewer()->setFocus();
} else {
int idx = 0;
if (onlyHelpViewer)
idx = lastTabPage;
tabWidget->setCurrentIndex(idx);
tabWidget->currentWidget()->setFocus();
if (HelpViewer* viewer = currentHelpViewer()) {
viewer->setSource(url);
viewer->setFocus(Qt::OtherFocusReason);
}
}
void CentralWidget::setTabTitle(const QUrl& url)
void CentralWidget::setSourceFromSearch(const QUrl &url)
{
Q_UNUSED(url)
#if !defined(QT_NO_WEBKIT)
QTabBar *tabBar = qFindChild<QTabBar*>(tabWidget);
for (int i = 0; i < tabBar->count(); ++i) {
HelpViewer* view = qobject_cast<HelpViewer*>(tabWidget->widget(i));