Skip to content

Commit b91e868

Browse files
committed
ViewWindow: create the contextMenuRequest signal
1 parent 549f561 commit b91e868

File tree

8 files changed

+49
-156
lines changed

8 files changed

+49
-156
lines changed

src/mainwindow.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ MainWindow::MainWindow( const QStringList& arguments )
139139
this, &MainWindow::onUrlChanged);
140140
connect(m_viewWindowMgr, &ViewWindowMgr::linkClicked,
141141
this, &MainWindow::onLinkClicked);
142+
connect(m_viewWindowMgr, &ViewWindowMgr::contextMenuRequested,
143+
this, &MainWindow::showBrowserContextMenu);
142144

143145
// Add navigation dock
144146
m_navPanel->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
@@ -418,6 +420,33 @@ void MainWindow::refreshCurrentBrowser( )
418420
m_navPanel->refresh();
419421
}
420422

423+
void MainWindow::showBrowserContextMenu(ViewWindow* browser,
424+
const QPoint& globalPos,
425+
const QUrl& link)
426+
{
427+
Q_UNUSED(browser)
428+
QMenu* m = new QMenu(this);
429+
430+
if ( !link.isEmpty() )
431+
{
432+
m->addAction( i18n("Open Link in a new tab\tShift+LMB"),
433+
[this, link] ()
434+
{
435+
openPage( link, UBrowser::OPEN_IN_NEW );
436+
});
437+
m->addAction( i18n("Open Link in a new background tab\tCtrl+LMB"),
438+
[this, link] ()
439+
{
440+
openPage( link, UBrowser::OPEN_IN_BACKGROUND );
441+
});
442+
m->addSeparator();
443+
}
444+
445+
setupPopupMenu( m );
446+
m->exec( globalPos );
447+
m->deleteLater();
448+
}
449+
421450
void MainWindow::activateUrl( const QUrl& link )
422451
{
423452
if ( link.isEmpty() )

src/mainwindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class QAction;
3333
class QActionGroup;
3434
class QCloseEvent;
3535
class QMenu;
36+
class QPoint;
3637
class QSharedMemory;
3738
class QTemporaryFile;
3839

@@ -154,6 +155,9 @@ class MainWindow : public QMainWindow, public Ui::MainWindow
154155
// Link activation
155156
void activateUrl( const QUrl& link );
156157
bool onLinkClicked( ViewWindow* browser, const QUrl& url, UBrowser::OpenMode mode );
158+
void showBrowserContextMenu( ViewWindow* browser,
159+
const QPoint& globalPos,
160+
const QUrl& link );
157161

158162
void updateToolbars();
159163
void updateActions();

src/qtwebengine/viewwindow.cpp

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
#include <QApplication>
2020
#include <QContextMenuEvent>
21-
#include <QKeySequence>
22-
#include <QMenu>
2321
#include <QPalette>
2422
#include <QString>
2523
#include <QUrl>
@@ -45,7 +43,6 @@ class QPrinter;
4543
#include <browser-types.hpp>
4644
#include <ebook.h>
4745

48-
#include "../i18n.h"
4946
#include "../mainwindow.h"
5047
#include "../viewwindowmgr.h"
5148
#include "webenginepage.h"
@@ -63,8 +60,6 @@ ViewWindow::ViewWindow( QWidget* parent )
6360
: QWebEngineView ( parent )
6461
{
6562
invalidate();
66-
m_contextMenu = 0;
67-
m_contextMenuLink = 0;
6863
m_storedScrollbarPosition = 0;
6964

7065
WebEnginePage* page = new WebEnginePage( this );
@@ -86,7 +81,6 @@ ViewWindow::~ViewWindow()
8681

8782
void ViewWindow::invalidate( )
8883
{
89-
m_newTabLinkKeeper = QString();
9084
m_storedScrollbarPosition = 0;
9185
reload();
9286
}
@@ -96,47 +90,9 @@ void ViewWindow::load ( const QUrl& url )
9690
// Do not use setContent() here, it resets QWebHistory
9791
QWebEngineView::load( url );
9892

99-
m_newTabLinkKeeper.clear();
10093
mainWindow->viewWindowMgr()->setTabName( this );
10194
}
10295

103-
QMenu* ViewWindow::createStandardContextMenu( QWidget* parent )
104-
{
105-
QMenu* contextMenu = new QMenu( parent );
106-
107-
contextMenu->addAction( "&Copy", ::mainWindow, SLOT(slotBrowserCopy()) );
108-
contextMenu->addAction( "&Select all", ::mainWindow, SLOT(slotBrowserSelectAll()) );
109-
110-
return contextMenu;
111-
}
112-
113-
QMenu* ViewWindow::getContextMenu( const QUrl& link, QWidget* parent )
114-
{
115-
if ( link.isEmpty() )
116-
{
117-
// standard context menu
118-
if ( !m_contextMenu )
119-
m_contextMenu = createStandardContextMenu( parent );
120-
121-
return m_contextMenu;
122-
}
123-
else
124-
{
125-
// Open in New Tab context menu
126-
// standard context menu
127-
if ( !m_contextMenuLink )
128-
{
129-
m_contextMenuLink = createStandardContextMenu( parent );
130-
m_contextMenuLink->addSeparator();
131-
m_contextMenuLink->addAction( i18n("&Open this link in a new tab"), ::mainWindow, SLOT(onOpenPageInNewTab()), QKeySequence("Shift+Enter") );
132-
m_contextMenuLink->addAction( i18n("&Open this link in a new background tab"), ::mainWindow, SLOT(onOpenPageInNewBackgroundTab()), QKeySequence("Ctrl+Enter") );
133-
}
134-
135-
setTabKeeper( link );
136-
return m_contextMenuLink;
137-
}
138-
}
139-
14096
QString ViewWindow::title() const
14197
{
14298
QString title = ::mainWindow->chmFile()->getTopicByUrl( url() );
@@ -158,11 +114,6 @@ bool ViewWindow::canGoForward() const
158114
return history()->canGoForward();
159115
}
160116

161-
void ViewWindow::setTabKeeper( const QUrl& link )
162-
{
163-
m_newTabLinkKeeper = link;
164-
}
165-
166117
void ViewWindow::print( QPrinter* printer, std::function<void (bool success)> result )
167118
{
168119
#if QT_VERSION < QT_VERSION_CHECK(6, 2, 0)
@@ -266,25 +217,13 @@ void ViewWindow::selectedCopy()
266217

267218
void ViewWindow::contextMenuEvent(QContextMenuEvent* e)
268219
{
269-
QMenu* m = new QMenu(0);
270-
271220
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
272221
QUrl link = lastContextMenuRequest()->linkUrl();
273222
#else
274223
QUrl link = page()->contextMenuData().linkUrl();
275224
#endif
276225

277-
if ( !link.isEmpty() )
278-
{
279-
m->addAction( i18n("Open Link in a new tab\tShift+LMB"), ::mainWindow, SLOT( onOpenPageInNewTab() ) );
280-
m->addAction( i18n("Open Link in a new background tab\tCtrl+LMB"), ::mainWindow, SLOT( onOpenPageInNewBackgroundTab() ) );
281-
m->addSeparator();
282-
setTabKeeper( link );
283-
}
284-
285-
::mainWindow->setupPopupMenu( m );
286-
m->exec( e->globalPos() );
287-
delete m;
226+
emit contextMenuRequested(e->globalPos(), link);
288227
}
289228

290229
void ViewWindow::onLoadFinished ( bool )

src/qtwebengine/viewwindow.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <QtGlobal>
2929

3030
class QContextMenuEvent;
31-
class QMenu;
31+
class QPoint;
3232
class QPrinter;
3333
class QWidget;
3434

@@ -49,13 +49,13 @@ class ViewWindow : public QWebEngineView
4949
void load (const QUrl& url );
5050

5151
QUrl url() const { return QWebEngineView::url(); }
52-
QUrl getNewTabLink() const { return m_newTabLinkKeeper; }
5352

5453
signals:
5554
void dataLoaded( ViewWindow* window );
5655

5756
// This signal is emitted whenever the user clicks on a link.
5857
void linkClicked(const QUrl& url, UBrowser::OpenMode mode);
58+
void contextMenuRequested(const QPoint& globalPos, const QUrl& url);
5959

6060
public:
6161
// Apply the configuration settings (JS enabled etc) to the web renderer
@@ -103,9 +103,6 @@ class ViewWindow : public QWebEngineView
103103

104104
bool canGoForward() const;
105105

106-
//! Keeps the tab URL between link following
107-
void setTabKeeper ( const QUrl& link );
108-
109106
public slots:
110107
void zoomIncrease();
111108
void zoomDecrease();
@@ -114,9 +111,6 @@ class ViewWindow : public QWebEngineView
114111
bool openPage ( const QUrl& url );
115112
void handleStartPageAsImage( QUrl& link );
116113

117-
QMenu* getContextMenu( const QUrl& link, QWidget* parent );
118-
QMenu* createStandardContextMenu( QWidget* parent );
119-
120114
// Overriden to change the source
121115
void setSource ( const QUrl& name );
122116

@@ -130,13 +124,6 @@ class ViewWindow : public QWebEngineView
130124
void onLinkClicked(const QUrl& url, UBrowser::OpenMode mode = UBrowser::OPEN_IN_CURRENT);
131125

132126
private:
133-
QMenu* m_contextMenu;
134-
QMenu* m_contextMenuLink;
135-
136-
// This member keeps a "open new tab" link between getContextMenu()
137-
// call and appropriate slot call
138-
QUrl m_newTabLinkKeeper;
139-
140127
// Keeps the scrollbar position to move after the page is loaded
141128
int m_storedScrollbarPosition;
142129
};

src/qtwebkit/viewwindow.cpp

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
#include <QApplication>
2020
#include <QContextMenuEvent>
21-
#include <QKeySequence>
22-
#include <QMenu>
2321
#include <QMouseEvent>
2422
#include <QPalette>
2523
#include <QString>
@@ -37,7 +35,6 @@ class QPrinter;
3735
#include <browser-types.hpp>
3836
#include <ebook.h>
3937

40-
#include "../i18n.h"
4138
#include "../mainwindow.h"
4239
#include "../viewwindowmgr.h"
4340
#include "dataprovider.h"
@@ -51,8 +48,6 @@ ViewWindow::ViewWindow( QWidget* parent )
5148
: QWebView ( parent )
5249
{
5350
invalidate();
54-
m_contextMenu = 0;
55-
m_contextMenuLink = 0;
5651
m_storedScrollbarPosition = 0;
5752

5853
// Use our network emulation layer
@@ -86,7 +81,6 @@ ViewWindow::~ViewWindow()
8681

8782
void ViewWindow::invalidate( )
8883
{
89-
m_newTabLinkKeeper = QString();
9084
m_storedScrollbarPosition = 0;
9185
reload();
9286
}
@@ -98,7 +92,6 @@ void ViewWindow::load( const QUrl& url )
9892
// Do not use setContent() here, it resets QWebHistory
9993
QWebView::load( url );
10094

101-
m_newTabLinkKeeper.clear();
10295
mainWindow->viewWindowMgr()->setTabName( this );
10396
}
10497

@@ -115,45 +108,6 @@ void ViewWindow::applySettings(BrowserSettings& settings)
115108
setup->setAttribute( QWebSettings::LocalStorageEnabled, settings.enableLocalStorage );
116109
}
117110

118-
QMenu* ViewWindow::createStandardContextMenu( QWidget* parent )
119-
{
120-
QMenu* contextMenu = new QMenu( parent );
121-
122-
contextMenu->addAction( "&Copy", ::mainWindow, SLOT(slotBrowserCopy()) );
123-
contextMenu->addAction( "&Select all", ::mainWindow, SLOT(slotBrowserSelectAll()) );
124-
125-
return contextMenu;
126-
}
127-
128-
QMenu* ViewWindow::getContextMenu( const QUrl& link, QWidget* parent )
129-
{
130-
if ( link.isEmpty() )
131-
{
132-
// standard context menu
133-
if ( !m_contextMenu )
134-
m_contextMenu = createStandardContextMenu( parent );
135-
136-
return m_contextMenu;
137-
}
138-
else
139-
{
140-
// Open in New Tab context menu
141-
// standard context menu
142-
if ( !m_contextMenuLink )
143-
{
144-
m_contextMenuLink = createStandardContextMenu( parent );
145-
m_contextMenuLink->addSeparator();
146-
147-
m_contextMenuLink->addAction( i18n("&Open this link in a new tab"), ::mainWindow, SLOT(onOpenPageInNewTab()), QKeySequence("Shift+Enter") );
148-
149-
m_contextMenuLink->addAction( i18n("&Open this link in a new background tab"), ::mainWindow, SLOT(onOpenPageInNewBackgroundTab()), QKeySequence("Ctrl+Enter") );
150-
}
151-
152-
setTabKeeper( link );
153-
return m_contextMenuLink;
154-
}
155-
}
156-
157111
QString ViewWindow::title() const
158112
{
159113
QString title = ::mainWindow->chmFile()->getTopicByUrl( url() );
@@ -175,11 +129,6 @@ bool ViewWindow::canGoForward() const
175129
return history()->canGoForward();
176130
}
177131

178-
void ViewWindow::setTabKeeper( const QUrl& link )
179-
{
180-
m_newTabLinkKeeper = link;
181-
}
182-
183132
void ViewWindow::print( QPrinter* printer, std::function<void (bool success)> result )
184133
{
185134
QWebView::print( printer );
@@ -308,21 +257,8 @@ void ViewWindow::mouseReleaseEvent ( QMouseEvent* event )
308257

309258
void ViewWindow::contextMenuEvent(QContextMenuEvent* e)
310259
{
311-
// From Qt Assistant
312-
QMenu* m = new QMenu(0);
313260
QUrl link = anchorAt( e->pos() );
314-
315-
if ( !link.isEmpty() )
316-
{
317-
m->addAction( i18n("Open Link in a new tab\tShift+LMB"), ::mainWindow, SLOT( onOpenPageInNewTab() ) );
318-
m->addAction( i18n("Open Link in a new background tab\tCtrl+LMB"), ::mainWindow, SLOT( onOpenPageInNewBackgroundTab() ) );
319-
m->addSeparator();
320-
setTabKeeper( link );
321-
}
322-
323-
::mainWindow->setupPopupMenu( m );
324-
m->exec( e->globalPos() );
325-
delete m;
261+
emit contextMenuRequested(e->globalPos(), link);
326262
}
327263

328264
void ViewWindow::onLoadFinished ( bool )

0 commit comments

Comments
 (0)