Commit 4cb93339 authored by kh's avatar kh

make ctrl+lmb and mmb work in a same way everywhere (like in browsers)

parent a36dd9d2
......@@ -62,11 +62,16 @@ SearchWidget::SearchWidget(QHelpSearchEngine *engine, QWidget *parent)
setFocusProxy(queryWidget);
connect(queryWidget, SIGNAL(search()), this, SLOT(search()));
connect(resultWidget, SIGNAL(requestShowLink(const QUrl&)),
this, SIGNAL(requestShowLink(const QUrl&)));
connect(resultWidget, SIGNAL(requestShowLink(QUrl)), this,
SIGNAL(requestShowLink(QUrl)));
connect(searchEngine, SIGNAL(searchingStarted()), this, SLOT(searchingStarted()));
connect(searchEngine, SIGNAL(searchingFinished(int)), this, SLOT(searchingFinished(int)));
connect(searchEngine, SIGNAL(searchingStarted()), this,
SLOT(searchingStarted()));
connect(searchEngine, SIGNAL(searchingFinished(int)), this,
SLOT(searchingFinished(int)));
QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
browser->viewport()->installEventFilter(this);
}
SearchWidget::~SearchWidget()
......@@ -76,10 +81,6 @@ SearchWidget::~SearchWidget()
void SearchWidget::zoomIn()
{
#ifndef QT_CLUCENE_SUPPORT
return;
#endif
QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
if (browser && zoomCount != 10) {
zoomCount++;
......@@ -89,10 +90,6 @@ void SearchWidget::zoomIn()
void SearchWidget::zoomOut()
{
#ifndef QT_CLUCENE_SUPPORT
return;
#endif
QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
if (browser && zoomCount != -5) {
zoomCount--;
......@@ -102,10 +99,6 @@ void SearchWidget::zoomOut()
void SearchWidget::resetZoom()
{
#ifndef QT_CLUCENE_SUPPORT
return;
#endif
if (zoomCount == 0)
return;
......@@ -133,6 +126,24 @@ void SearchWidget::searchingFinished(int hits)
qApp->restoreOverrideCursor();
}
bool SearchWidget::eventFilter(QObject* o, QEvent *e)
{
QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
if (browser && o == browser->viewport()
&& e->type() == QEvent::MouseButtonRelease){
QMouseEvent *me = static_cast<QMouseEvent*>(e);
QUrl link = resultWidget->linkAt(me->pos());
if (!link.isEmpty() || link.isValid()) {
bool controlPressed = me->modifiers() & Qt::ControlModifier;
if((me->button() == Qt::LeftButton && controlPressed)
|| (me->button() == Qt::MidButton)) {
emit requestShowLinkInNewTab(link);
}
}
}
return QWidget::eventFilter(o,e);
}
void SearchWidget::keyPressEvent(QKeyEvent *keyEvent)
{
if (keyEvent->key() == Qt::Key_Escape)
......@@ -144,7 +155,6 @@ void SearchWidget::contextMenuEvent(QContextMenuEvent *contextMenuEvent)
QMenu menu;
QPoint point = contextMenuEvent->globalPos();
#ifdef QT_CLUCENE_SUPPORT
QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
if (!browser)
return;
......@@ -155,22 +165,25 @@ void SearchWidget::contextMenuEvent(QContextMenuEvent *contextMenuEvent)
QUrl link = browser->anchorAt(point);
QAction *copyAction = menu.addAction(tr("&Copy") +
QString(QLatin1String("\t") + QString(QKeySequence(Qt::CTRL | Qt::Key_C))));
QKeySequence keySeq(QKeySequence::Copy);
QAction *copyAction = menu.addAction(tr("&Copy") + QLatin1String("\t") +
keySeq.toString(QKeySequence::NativeText));
copyAction->setEnabled(QTextCursor(browser->textCursor()).hasSelection());
QAction *copyAnchorAction = menu.addAction(tr("Copy &Link Location"));
copyAnchorAction->setEnabled(!link.isEmpty() && link.isValid());
keySeq = QKeySequence(Qt::CTRL);
QAction *newTabAction = menu.addAction(tr("Open Link in New Tab") +
QString(QLatin1String("\t") + QString(QKeySequence(Qt::CTRL))) +
QLatin1String("\t") + keySeq.toString(QKeySequence::NativeText) +
QLatin1String("LMB"));
newTabAction->setEnabled(!link.isEmpty() && link.isValid());
menu.addSeparator();
keySeq = QKeySequence::SelectAll;
QAction *selectAllAction = menu.addAction(tr("Select All") +
QString(QLatin1String("\t") + QString(QKeySequence(Qt::CTRL | Qt::Key_A))));
QLatin1String("\t") + keySeq.toString(QKeySequence::NativeText));
QAction *usedAction = menu.exec(mapToGlobal(contextMenuEvent->pos()));
if (usedAction == copyAction) {
......@@ -191,19 +204,4 @@ void SearchWidget::contextMenuEvent(QContextMenuEvent *contextMenuEvent)
else if (usedAction == selectAllAction) {
browser->selectAll();
}
#else
point = resultWidget->mapFromGlobal(point);
QUrl link = resultWidget->linkAt(point);
if (link.isEmpty() || !link.isValid())
return;
QAction *curTab = menu.addAction(tr("Open Link"));
QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
QAction *action = menu.exec(mapToGlobal(contextMenuEvent->pos()));
if (curTab == action)
emit requestShowLink(link);
else if (newTab == action)
emit requestShowLinkInNewTab(link);
#endif
}
......@@ -69,6 +69,7 @@ private slots:
void searchingFinished(int hits);
private:
bool eventFilter(QObject* o, QEvent *e);
void keyPressEvent(QKeyEvent *keyEvent);
void contextMenuEvent(QContextMenuEvent *contextMenuEvent);
......
......@@ -523,58 +523,57 @@ void BookmarkWidget::focusInEvent(QFocusEvent *e)
bool BookmarkWidget::eventFilter(QObject *object, QEvent *e)
{
if (object == this && e->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent*>(e);
if ((object == this) || (object == treeView->viewport())) {
QModelIndex index = treeView->currentIndex();
switch (ke->key()) {
if (e->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent*>(e);
if (index.isValid() && searchField->text().isEmpty()) {
case Qt::Key_F2: {
const QModelIndex& source = filterBookmarkModel->mapToSource(index);
QStandardItem *item =
bookmarkManager->treeBookmarkModel()->itemFromIndex(source);
if (ke->key() == Qt::Key_F2) {
QStandardItem *item = bookmarkManager->treeBookmarkModel()
->itemFromIndex(filterBookmarkModel->mapToSource(index));
if (item) {
item->setEditable(true);
treeView->edit(index);
item->setEditable(false);
}
} break;
case Qt::Key_Delete: {
} else if (ke->key() == Qt::Key_Delete) {
bookmarkManager->removeBookmarkItem(treeView,
filterBookmarkModel->mapToSource(index));
} break;
}
}
case Qt::Key_Up:
case Qt::Key_Down:
treeView->subclassKeyPressEvent(ke);
break;
case Qt::Key_Enter: {
case Qt::Key_Return:
index = treeView->selectionModel()->currentIndex();
if (index.isValid()) {
QString data = index.data(Qt::UserRole + 10).toString();
if (!data.isEmpty() && data != QLatin1String("Folder"))
emit requestShowLink(data);
}
} break;
switch (ke->key()) {
default: break;
case Qt::Key_Up: {
case Qt::Key_Down:
treeView->subclassKeyPressEvent(ke);
} break;
case Qt::Key_Escape:
emit escapePressed();
break;
case Qt::Key_Enter: {
case Qt::Key_Return:
index = treeView->selectionModel()->currentIndex();
if (index.isValid()) {
QString data = index.data(Qt::UserRole + 10).toString();
if (!data.isEmpty() && data != QLatin1String("Folder"))
emit requestShowLink(data);
}
} break;
default:
break;
}
}
else if (object == treeView->viewport() && e->type() == QEvent::MouseButtonRelease) {
const QModelIndex& index = treeView->currentIndex();
QMouseEvent *me = static_cast<QMouseEvent*>(e);
if (index.isValid() && (me->button() == Qt::MidButton)) {
QString data = index.data(Qt::UserRole + 10).toString();
if (!data.isEmpty() && data != QLatin1String("Folder"))
Help::Internal::CentralWidget::instance()->setSourceInNewTab(data);
case Qt::Key_Escape: {
emit escapePressed();
} break;
}
} else if (e->type() == QEvent::MouseButtonRelease) {
if (index.isValid()) {
QMouseEvent *me = static_cast<QMouseEvent*>(e);
bool controlPressed = me->modifiers() & Qt::ControlModifier;
if(((me->button() == Qt::LeftButton) && controlPressed)
|| (me->button() == Qt::MidButton)) {
QString data = index.data(Qt::UserRole + 10).toString();
if (!data.isEmpty() && data != QLatin1String("Folder"))
Help::Internal::CentralWidget::instance()->setSourceInNewTab(data);
}
}
}
}
return QWidget::eventFilter(object, e);
......
......@@ -104,19 +104,26 @@ void ContentWindow::keyPressEvent(QKeyEvent *e)
bool ContentWindow::eventFilter(QObject *o, QEvent *e)
{
if (m_contentWidget && o == m_contentWidget->viewport() && e->type()
== QEvent::MouseButtonRelease) {
if (m_contentWidget && o == m_contentWidget->viewport()
&& e->type() == QEvent::MouseButtonRelease) {
QMouseEvent *me = static_cast<QMouseEvent*>(e);
if (m_contentWidget->indexAt(me->pos()).isValid()
&& me->button() == Qt::LeftButton) {
itemClicked(m_contentWidget->currentIndex());
} else if (m_contentWidget->indexAt(me->pos()).isValid()
&& me->button() == Qt::MidButton) {
QHelpContentModel *contentModel =
qobject_cast<QHelpContentModel*>(m_contentWidget->model());
QHelpContentItem *itm =
contentModel->contentItemAt(m_contentWidget->currentIndex());
Help::Internal::CentralWidget::instance()->setSourceInNewTab(itm->url());
QModelIndex index = m_contentWidget->indexAt(me->pos());
QItemSelectionModel *sm = m_contentWidget->selectionModel();
Qt::MouseButtons button = me->button();
if (index.isValid() && (sm && sm->isSelected(index))) {
if ((button == Qt::LeftButton && (me->modifiers() & Qt::ControlModifier))
|| (button == Qt::MidButton)) {
QHelpContentModel *contentModel =
qobject_cast<QHelpContentModel*>(m_contentWidget->model());
if (contentModel) {
QHelpContentItem *itm = contentModel->contentItemAt(index);
if (itm && !isPdfFile(itm))
Help::Internal::CentralWidget::instance()->setSourceInNewTab(itm->url());
}
} else if (button == Qt::LeftButton) {
itemClicked(index);
}
}
}
return QWidget::eventFilter(o, e);
......@@ -127,16 +134,19 @@ void ContentWindow::showContextMenu(const QPoint &pos)
if (!m_contentWidget->indexAt(pos).isValid())
return;
QMenu menu;
QAction *curTab = menu.addAction(tr("Open Link"));
QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
menu.move(m_contentWidget->mapToGlobal(pos));
QHelpContentModel *contentModel =
qobject_cast<QHelpContentModel*>(m_contentWidget->model());
QHelpContentItem *itm =
contentModel->contentItemAt(m_contentWidget->currentIndex());
QMenu menu;
QAction *curTab = menu.addAction(tr("Open Link"));
QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
if (isPdfFile(itm))
newTab->setEnabled(false);
menu.move(m_contentWidget->mapToGlobal(pos));
QAction *action = menu.exec();
if (curTab == action)
emit linkActivated(itm->url());
......@@ -146,12 +156,18 @@ void ContentWindow::showContextMenu(const QPoint &pos)
void ContentWindow::itemClicked(const QModelIndex &index)
{
if (!index.isValid())
return;
QHelpContentModel *contentModel =
qobject_cast<QHelpContentModel*>(m_contentWidget->model());
QHelpContentItem *itm =
contentModel->contentItemAt(index);
if (itm)
emit linkActivated(itm->url());
if (contentModel) {
QHelpContentItem *itm = contentModel->contentItemAt(index);
if (itm)
emit linkActivated(itm->url());
}
}
bool ContentWindow::isPdfFile(QHelpContentItem *item) const
{
const QString &path = item->url().path();
return path.endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive);
}
......@@ -37,6 +37,7 @@
QT_BEGIN_NAMESPACE
class QHelpEngine;
class QHelpContentItem;
class QHelpContentWidget;
QT_END_NAMESPACE
......@@ -65,6 +66,7 @@ private:
void focusInEvent(QFocusEvent *e);
void keyPressEvent(QKeyEvent *e);
bool eventFilter(QObject *o, QEvent *e);
bool isPdfFile(QHelpContentItem *item) const;
QHelpEngine *m_helpEngine;
QHelpContentWidget *m_contentWidget;
......
......@@ -146,6 +146,7 @@ QNetworkReply *HelpNetworkAccessManager::createRequest(Operation op,
class HelpPage : public QWebPage
{
friend class HelpViewer;
public:
HelpPage(Help::Internal::CentralWidget *central, QHelpEngine *engine, QObject *parent);
......@@ -158,11 +159,17 @@ protected:
private:
Help::Internal::CentralWidget *centralWidget;
QHelpEngine *helpEngine;
Qt::MouseButtons m_pressedButtons;
Qt::KeyboardModifiers m_keyboardModifiers;
};
HelpPage::HelpPage(Help::Internal::CentralWidget *central, QHelpEngine *engine, QObject *parent)
: QWebPage(parent), centralWidget(central), helpEngine(engine)
{
: QWebPage(parent)
, centralWidget(central)
, helpEngine(engine)
, m_pressedButtons(Qt::NoButton)
, m_keyboardModifiers(Qt::NoModifier)
{
}
QWebPage *HelpPage::createWindow(QWebPage::WebWindowType)
......@@ -184,14 +191,18 @@ static bool isLocalUrl(const QUrl &url)
}
bool HelpPage::acceptNavigationRequest(QWebFrame *,
const QNetworkRequest &request, QWebPage::NavigationType)
const QNetworkRequest &request, QWebPage::NavigationType type)
{
const QUrl &url = request.url();
if (isLocalUrl(url)) {
if (url.path().endsWith(QLatin1String("pdf"))) {
QString fileName = url.toString();
fileName = QDir::tempPath() + QDir::separator() + fileName.right
(fileName.length() - fileName.lastIndexOf(QChar('/')));
const QString& path = url.path();
if (path.endsWith(QLatin1String(".pdf"))) {
const int lastDash = path.lastIndexOf(QChar('/'));
QString fileName = QDir::tempPath() + QDir::separator();
if (lastDash < 0)
fileName += path;
else
fileName += path.mid(lastDash + 1, path.length());
QFile tmpFile(QDir::cleanPath(fileName));
if (tmpFile.open(QIODevice::ReadWrite)) {
......@@ -201,6 +212,17 @@ bool HelpPage::acceptNavigationRequest(QWebFrame *,
QDesktopServices::openUrl(QUrl(tmpFile.fileName()));
return false;
}
if (type == QWebPage::NavigationTypeLinkClicked
&& (m_keyboardModifiers & Qt::ControlModifier
|| m_pressedButtons == Qt::MidButton)) {
HelpViewer* viewer = centralWidget->newEmptyTab();
if (viewer)
centralWidget->setSource(url);
m_pressedButtons = Qt::NoButton;
m_keyboardModifiers = Qt::NoModifier;
return false;
}
return true;
}
......@@ -209,7 +231,10 @@ bool HelpPage::acceptNavigationRequest(QWebFrame *,
}
HelpViewer::HelpViewer(QHelpEngine *engine, Help::Internal::CentralWidget *parent)
: QWebView(parent), helpEngine(engine), parentWidget(parent)
: QWebView(parent)
, helpEngine(engine)
, parentWidget(parent)
, multiTabsAllowed(true)
{
setPage(new HelpPage(parent, helpEngine, this));
settings()->setAttribute(QWebSettings::PluginsEnabled, false);
......@@ -219,8 +244,10 @@ HelpViewer::HelpViewer(QHelpEngine *engine, Help::Internal::CentralWidget *paren
QAction* action = pageAction(QWebPage::OpenLinkInNewWindow);
action->setText(tr("Open Link in New Tab"));
if (!parent)
if (!parent) {
multiTabsAllowed = false;
action->setVisible(false);
}
pageAction(QWebPage::DownloadLinkToDisk)->setVisible(false);
pageAction(QWebPage::DownloadImageToDisk)->setVisible(false);
......@@ -306,6 +333,16 @@ void HelpViewer::actionChanged()
emit forwardAvailable(a->isEnabled());
}
void HelpViewer::mousePressEvent(QMouseEvent *event)
{
HelpPage *currentPage = static_cast<HelpPage*>(page());
if ((currentPage != 0) && multiTabsAllowed) {
currentPage->m_pressedButtons = event->buttons();
currentPage->m_keyboardModifiers = event->modifiers();
}
QWebView::mousePressEvent(event);
}
#else // !defined(QT_NO_WEBKIT)
HelpViewer::HelpViewer(QHelpEngine *engine, Help::Internal::CentralWidget *parent)
......
......@@ -102,6 +102,7 @@ Q_SIGNALS:
protected:
virtual void wheelEvent(QWheelEvent *);
void mouseReleaseEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *event);
private Q_SLOTS:
void actionChanged();
......@@ -110,6 +111,7 @@ private:
QHelpEngine *helpEngine;
Help::Internal::CentralWidget* parentWidget;
QUrl homeUrl;
bool multiTabsAllowed;
};
#else
......
......@@ -108,10 +108,9 @@ bool IndexWindow::eventFilter(QObject *obj, QEvent *e)
m_indexWidget->setCurrentIndex(idx);
break;
case Qt::Key_Escape:
emit escapePressed();
emit escapePressed();
break;
default:
;
default: ; // stop complaining
}
} else if (obj == m_indexWidget && e->type() == QEvent::ContextMenu) {
QContextMenuEvent *ctxtEvent = static_cast<QContextMenuEvent*>(e);
......@@ -126,42 +125,18 @@ bool IndexWindow::eventFilter(QObject *obj, QEvent *e)
if (curTab == action)
m_indexWidget->activateCurrentItem();
else if (newTab == action) {
QHelpIndexModel *model =
qobject_cast<QHelpIndexModel*>(m_indexWidget->model());
QString keyword = model->data(idx, Qt::DisplayRole).toString();
if (model) {
QMap<QString, QUrl> links = model->linksForKeyword(keyword);
if (links.count() == 1) {
Help::Internal::CentralWidget::instance()->
setSourceInNewTab(links.constBegin().value());
} else {
TopicChooser tc(this, keyword, links);
if (tc.exec() == QDialog::Accepted) {
Help::Internal::CentralWidget::instance()->setSourceInNewTab(tc.link());
}
}
}
open(m_indexWidget, idx);
}
}
} else if (m_indexWidget && obj == m_indexWidget->viewport()
&& e->type() == QEvent::MouseButtonRelease) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
QModelIndex idx = m_indexWidget->indexAt(mouseEvent->pos());
if (idx.isValid() && mouseEvent->button()==Qt::MidButton) {
QHelpIndexModel *model =
qobject_cast<QHelpIndexModel*>(m_indexWidget->model());
QString keyword = model->data(idx, Qt::DisplayRole).toString();
if (model) {
QMap<QString, QUrl> links = model->linksForKeyword(keyword);
if (links.count() > 1) {
TopicChooser tc(this, keyword, links);
if (tc.exec() == QDialog::Accepted) {
Help::Internal::CentralWidget::instance()->setSourceInNewTab(tc.link());
}
} else if (links.count() == 1) {
Help::Internal::CentralWidget::instance()->
setSourceInNewTab(links.constBegin().value());
}
if (idx.isValid()) {
Qt::MouseButtons button = mouseEvent->button();
if (((button == Qt::LeftButton) && (mouseEvent->modifiers() & Qt::ControlModifier))
|| (button == Qt::MidButton)) {
open(m_indexWidget, idx);
}
}
}
......@@ -198,3 +173,28 @@ void IndexWindow::focusInEvent(QFocusEvent *e)
m_searchLineEdit->setFocus();
}
}
void IndexWindow::open(QHelpIndexWidget* indexWidget, const QModelIndex &index)
{
QHelpIndexModel *model = qobject_cast<QHelpIndexModel*>(indexWidget->model());
if (model) {
QString keyword = model->data(index, Qt::DisplayRole).toString();
QMap<QString, QUrl> links = model->linksForKeyword(keyword);
QUrl url;
if (links.count() > 1) {
TopicChooser tc(this, keyword, links);
if (tc.exec() == QDialog::Accepted)
url = tc.link();
} else if (links.count() == 1) {
url = links.constBegin().value();
} else {
return;
}
if (url.path().endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive))
Help::Internal::CentralWidget::instance()->setSource(url);
else
Help::Internal::CentralWidget::instance()->setSourceInNewTab(url);
}
}
......@@ -38,6 +38,7 @@ QT_BEGIN_NAMESPACE
class QHelpIndexWidget;
class QHelpEngine;
class QModelIndex;
QT_END_NAMESPACE
......@@ -69,6 +70,7 @@ private slots:
private:
bool eventFilter(QObject *obj, QEvent *e);
void focusInEvent(QFocusEvent *e);
void open(QHelpIndexWidget* indexWidget, const QModelIndex &index);
QLineEdit *m_searchLineEdit;
QHelpIndexWidget *m_indexWidget;
......
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