Commit c798f54f authored by kh's avatar kh
Browse files

Clenaup, sync with Assistant source.

parent b95926c8
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include "helpviewer.h" #include "helpviewer.h"
#include "topicchooser.h" #include "topicchooser.h"
#include <QtCore/QDir>
#include <QtCore/QEvent> #include <QtCore/QEvent>
#include <QtCore/QTimer> #include <QtCore/QTimer>
...@@ -62,7 +61,8 @@ ...@@ -62,7 +61,8 @@
using namespace Help::Internal; using namespace Help::Internal;
namespace { namespace {
HelpViewer* helpViewerFromTabPosition(const QTabWidget *widget, const QPoint &point) HelpViewer* helpViewerFromTabPosition(const QTabWidget *widget,
const QPoint &point)
{ {
QTabBar *tabBar = qFindChild<QTabBar*>(widget); QTabBar *tabBar = qFindChild<QTabBar*>(widget);
for (int i = 0; i < tabBar->count(); ++i) { for (int i = 0; i < tabBar->count(); ++i) {
...@@ -85,22 +85,22 @@ CentralWidget::CentralWidget(QHelpEngine *engine, QWidget *parent) ...@@ -85,22 +85,22 @@ CentralWidget::CentralWidget(QHelpEngine *engine, QWidget *parent)
globalActionList.clear(); globalActionList.clear();
collectionFile = helpEngine->collectionFile(); collectionFile = helpEngine->collectionFile();
#ifdef Q_OS_MAC
# define SYSTEM "mac"
#else
# define SYSTEM "win"
#endif
tabWidget = new QTabWidget; tabWidget = new QTabWidget;
tabWidget->setDocumentMode(true); tabWidget->setDocumentMode(true);
tabWidget->setMovable(true); tabWidget->setMovable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentPageChanged(int))); connect(tabWidget, SIGNAL(currentChanged(int)), this,
SLOT(currentPageChanged(int)));
QToolButton *newTabButton = new QToolButton(this); QToolButton *newTabButton = new QToolButton(this);
newTabButton->setAutoRaise(true); newTabButton->setAutoRaise(true);
newTabButton->setToolTip(tr("Add new page")); newTabButton->setToolTip(tr("Add new page"));
newTabButton->setIcon(QIcon(QString::fromLatin1(":/trolltech/assistant/images/" SYSTEM "/addtab.png"))); 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); tabWidget->setCornerWidget(newTabButton, Qt::TopLeftCorner);
connect(newTabButton, SIGNAL(clicked()), this, SLOT(newTab())); connect(newTabButton, SIGNAL(clicked()), this, SLOT(newTab()));
...@@ -122,19 +122,23 @@ CentralWidget::CentralWidget(QHelpEngine *engine, QWidget *parent) ...@@ -122,19 +122,23 @@ CentralWidget::CentralWidget(QHelpEngine *engine, QWidget *parent)
CentralWidget::~CentralWidget() CentralWidget::~CentralWidget()
{ {
QDir dir; #ifndef QT_NO_PRINTER
QString currentPages; delete printer;
#endif
QHelpEngineCore engine(collectionFile, 0); QHelpEngineCore engine(collectionFile, 0);
if (!engine.setupData())
return;
if (engine.setupData()) { QString currentPages;
for (int i = 0; i < tabWidget->count(); ++i) { for (int i = 0; i < tabWidget->count(); ++i) {
HelpViewer *viewer = qobject_cast<HelpViewer*>(tabWidget->widget(i)); HelpViewer *viewer = qobject_cast<HelpViewer*>(tabWidget->widget(i));
if (viewer && viewer->source().isValid()) if (viewer && viewer->source().isValid())
currentPages.append(viewer->source().toString()).append(QLatin1Char('|')); currentPages += (viewer->source().toString() + QLatin1Char('|'));
}
engine.setCustomValue(QLatin1String("LastTabPage"), lastTabPage);
engine.setCustomValue(QLatin1String("LastShownPages"), currentPages);
} }
engine.setCustomValue(QLatin1String("LastTabPage"), lastTabPage);
engine.setCustomValue(QLatin1String("LastShownPages"), currentPages);
} }
CentralWidget *CentralWidget::instance() CentralWidget *CentralWidget::instance()
...@@ -165,10 +169,10 @@ void CentralWidget::zoomOut() ...@@ -165,10 +169,10 @@ void CentralWidget::zoomOut()
void CentralWidget::nextPage() void CentralWidget::nextPage()
{ {
if (tabWidget->currentIndex() < tabWidget->count() -1) int index = tabWidget->currentIndex() + 1;
tabWidget->setCurrentIndex(tabWidget->currentIndex() +1); if (index >= tabWidget->count())
else index = 0;
tabWidget->setCurrentIndex(0); tabWidget->setCurrentIndex(index);
} }
void CentralWidget::resetZoom() void CentralWidget::resetZoom()
...@@ -181,10 +185,9 @@ void CentralWidget::resetZoom() ...@@ -181,10 +185,9 @@ void CentralWidget::resetZoom()
void CentralWidget::previousPage() void CentralWidget::previousPage()
{ {
int index = tabWidget->currentIndex() -1; int index = tabWidget->currentIndex() -1;
if (index >= 0) if (index < 0)
tabWidget->setCurrentIndex(index); index = tabWidget->count() -1;
else tabWidget->setCurrentIndex(index);
tabWidget->setCurrentIndex(tabWidget->count() -1);
} }
void CentralWidget::closeTab() void CentralWidget::closeTab()
...@@ -205,7 +208,8 @@ void CentralWidget::closeTab(int index) ...@@ -205,7 +208,8 @@ void CentralWidget::closeTab(int index)
void CentralWidget::setSource(const QUrl &url) void CentralWidget::setSource(const QUrl &url)
{ {
HelpViewer* viewer = currentHelpViewer(); HelpViewer* viewer = currentHelpViewer();
HelpViewer* lastViewer = qobject_cast<HelpViewer*>(tabWidget->widget(lastTabPage)); HelpViewer* lastViewer =
qobject_cast<HelpViewer*>(tabWidget->widget(lastTabPage));
if (!viewer && !lastViewer) { if (!viewer && !lastViewer) {
viewer = new HelpViewer(helpEngine, this); viewer = new HelpViewer(helpEngine, this);
...@@ -214,8 +218,9 @@ void CentralWidget::setSource(const QUrl &url) ...@@ -214,8 +218,9 @@ void CentralWidget::setSource(const QUrl &url)
tabWidget->setCurrentIndex(lastTabPage); tabWidget->setCurrentIndex(lastTabPage);
connectSignals(); connectSignals();
qApp->processEvents(); qApp->processEvents();
} else } else {
viewer = lastViewer; viewer = lastViewer;
}
viewer->setSource(url); viewer->setSource(url);
currentPageChanged(lastTabPage); currentPageChanged(lastTabPage);
...@@ -226,24 +231,28 @@ void CentralWidget::setSource(const QUrl &url) ...@@ -226,24 +231,28 @@ void CentralWidget::setSource(const QUrl &url)
void CentralWidget::setLastShownPages() void CentralWidget::setLastShownPages()
{ {
const QStringList lastShownPageList = QString value = helpEngine->customValue(QLatin1String("LastShownPages"),
helpEngine->customValue(QLatin1String("LastShownPages")). toString(). QString()).toString();
split(QLatin1Char('|'), QString::SkipEmptyParts); const QStringList lastShownPageList = value.split(QLatin1Char('|'),
QString::SkipEmptyParts);
if (!lastShownPageList.isEmpty()) { const int pageCount = lastShownPageList.count();
foreach (const QString& page, lastShownPageList) if (pageCount <= 0) {
setSourceInNewTab(page); QUrl url = helpEngine->findFile(QString::fromLatin1("qthelp://com."
"trolltech.qt.440/qdoc/index.html"));
tabWidget->setCurrentIndex(helpEngine->
customValue(QLatin1String("LastTabPage"), 0).toInt());
} else {
QUrl url(helpEngine->findFile(QUrl("qthelp://com.trolltech.qt.440/qdoc/index.html")));
if (!url.isValid()) { if (!url.isValid()) {
url.setUrl(QString("qthelp://com.nokia.qtcreator.%1%2/doc/index.html"). url.setUrl(QString("qthelp://com.nokia.qtcreator.%1%2/doc/index.html").
arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR)); arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR));
url.setUrl(QString::fromLatin1("qthelp://com.nokia.qtcreator.%1%2/"
"doc/index.html").arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR));
} }
setSource(url); setSource(url);
return;
} }
QStringList::const_iterator it = lastShownPageList.constBegin();
for (; it != lastShownPageList.constEnd(); ++it)
setSourceInNewTab((*it));
} }
bool CentralWidget::hasSelection() const bool CentralWidget::hasSelection() const
...@@ -326,6 +335,8 @@ void CentralWidget::printPreview(QPrinter *p) ...@@ -326,6 +335,8 @@ void CentralWidget::printPreview(QPrinter *p)
HelpViewer *viewer = currentHelpViewer(); HelpViewer *viewer = currentHelpViewer();
if (viewer) if (viewer)
viewer->print(p); viewer->print(p);
#else
Q_UNUSED(p);
#endif #endif
} }
...@@ -475,6 +486,8 @@ void CentralWidget::setTabTitle(const QUrl& url) ...@@ -475,6 +486,8 @@ void CentralWidget::setTabTitle(const QUrl& url)
break; break;
} }
} }
#else
Q_UNUSED(url);
#endif #endif
if (viewer) { if (viewer) {
...@@ -485,16 +498,9 @@ void CentralWidget::setTabTitle(const QUrl& url) ...@@ -485,16 +498,9 @@ void CentralWidget::setTabTitle(const QUrl& url)
void CentralWidget::currentPageChanged(int index) void CentralWidget::currentPageChanged(int index)
{ {
const HelpViewer *viewer = currentHelpViewer(); lastTabPage = index;
if (viewer || tabWidget->count() == 1)
lastTabPage = index;
bool enabled = false; tabWidget->setTabsClosable(tabWidget->count() > 1);
if (viewer)
enabled = tabWidget->count() > 1;
tabWidget->setTabsClosable(enabled);
tabWidget->cornerWidget(Qt::TopLeftCorner)->setEnabled(true); tabWidget->cornerWidget(Qt::TopLeftCorner)->setEnabled(true);
emit currentViewerChanged(); emit currentViewerChanged();
...@@ -509,34 +515,32 @@ void CentralWidget::showTabBarContextMenu(const QPoint &point) ...@@ -509,34 +515,32 @@ void CentralWidget::showTabBarContextMenu(const QPoint &point)
QTabBar *tabBar = qFindChild<QTabBar*>(tabWidget); QTabBar *tabBar = qFindChild<QTabBar*>(tabWidget);
QMenu menu(QLatin1String(""), tabBar); QMenu menu(QLatin1String(""), tabBar);
QAction *new_page = menu.addAction(tr("Add New Page")); QAction *newPage = menu.addAction(tr("Add New Page"));
QAction *close_page = menu.addAction(tr("Close This Page"));
QAction *close_pages = menu.addAction(tr("Close Other Pages"));
menu.addSeparator();
QAction *newBookmark = menu.addAction(tr("Add Bookmark for this Page..."));
if (tabBar->count() == 1) { bool enableAction = tabBar->count() > 1;
close_page->setEnabled(false); QAction *closePage = menu.addAction(tr("Close This Page"));
close_pages->setEnabled(false); closePage->setEnabled(enableAction);
}
QAction *closePages = menu.addAction(tr("Close Other Pages"));
closePages->setEnabled(enableAction);
menu.addSeparator();
QAction *newBookmark = menu.addAction(tr("Add Bookmark for this Page..."));
const QString &url = viewer->source().toString(); const QString &url = viewer->source().toString();
if (url.isEmpty() || url == QLatin1String("about:blank")) if (url.isEmpty() || url == QLatin1String("about:blank"))
newBookmark->setEnabled(false); newBookmark->setEnabled(false);
QAction *picked_action = menu.exec(tabBar->mapToGlobal(point)); QAction *pickedAction = menu.exec(tabBar->mapToGlobal(point));
if (!picked_action) if (pickedAction == newPage)
return;
if (picked_action == new_page)
setSourceInNewTab(viewer->source()); setSourceInNewTab(viewer->source());
if (picked_action == close_page) { if (pickedAction == closePage) {
tabWidget->removeTab(tabWidget->indexOf(viewer)); tabWidget->removeTab(tabWidget->indexOf(viewer));
QTimer::singleShot(0, viewer, SLOT(deleteLater())); QTimer::singleShot(0, viewer, SLOT(deleteLater()));
} }
if (picked_action == close_pages) { if (pickedAction == closePages) {
int currentPage = tabWidget->indexOf(viewer); int currentPage = tabWidget->indexOf(viewer);
for (int i = tabBar->count() -1; i >= 0; --i) { for (int i = tabBar->count() -1; i >= 0; --i) {
viewer = qobject_cast<HelpViewer*>(tabWidget->widget(i)); viewer = qobject_cast<HelpViewer*>(tabWidget->widget(i));
...@@ -550,7 +554,7 @@ void CentralWidget::showTabBarContextMenu(const QPoint &point) ...@@ -550,7 +554,7 @@ void CentralWidget::showTabBarContextMenu(const QPoint &point)
} }
} }
if (picked_action == newBookmark) if (pickedAction == newBookmark)
emit addNewBookmark(viewer->documentTitle(), url); emit addNewBookmark(viewer->documentTitle(), url);
} }
...@@ -568,31 +572,40 @@ void CentralWidget::focusInEvent(QFocusEvent * /* event */) ...@@ -568,31 +572,40 @@ void CentralWidget::focusInEvent(QFocusEvent * /* event */)
bool CentralWidget::eventFilter(QObject *object, QEvent *e) bool CentralWidget::eventFilter(QObject *object, QEvent *e)
{ {
if (currentHelpViewer() == object && e->type() == QEvent::KeyPress){ if (e->type() == QEvent::KeyPress){
QKeyEvent *ke = static_cast<QKeyEvent*>(e); if ((static_cast<QKeyEvent*>(e))->key() == Qt::Key_Backspace) {
if (ke->key() == Qt::Key_Backspace) {
HelpViewer *viewer = currentHelpViewer(); HelpViewer *viewer = currentHelpViewer();
if (viewer && viewer->isBackwardAvailable()) if (viewer == object) {
viewer->backward(); if (viewer->isBackwardAvailable()) {
return true; #if !defined(QT_NO_WEBKIT)
// this helps in case there is an html <input> field
if (!viewer->hasFocus())
#endif
viewer->backward();
}
return true;
}
} }
} }
QTabBar *tabBar = qobject_cast<QTabBar*>(object); if (qobject_cast<QTabBar*>(object)) {
bool mousRel = e->type() == QEvent::MouseButtonRelease; bool dblClick = e->type() == QEvent::MouseButtonDblClick;
bool dblClick = e->type() == QEvent::MouseButtonDblClick; if((e->type() == QEvent::MouseButtonRelease) || dblClick) {
if (tabWidget->count() <= 1)
if (tabBar && (mousRel || dblClick)) { return QWidget::eventFilter(object, e);
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
HelpViewer *viewer = helpViewerFromTabPosition(tabWidget, mouseEvent->pos()); QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
if (tabWidget->count() <= 1) HelpViewer *viewer = helpViewerFromTabPosition(tabWidget,
return QWidget::eventFilter(object, e); mouseEvent->pos());
if (viewer && (mouseEvent->button() == Qt::MidButton || dblClick)) { if (viewer) {
tabWidget->removeTab(tabWidget->indexOf(viewer)); if ((mouseEvent->button() == Qt::MidButton) || dblClick) {
QTimer::singleShot(0, viewer, SLOT(deleteLater())); tabWidget->removeTab(tabWidget->indexOf(viewer));
currentPageChanged(tabWidget->currentIndex()); QTimer::singleShot(0, viewer, SLOT(deleteLater()));
return true; currentPageChanged(tabWidget->currentIndex());
return true;
}
}
} }
} }
return QWidget::eventFilter(object, e); return QWidget::eventFilter(object, e);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment