Skip to content

Commit 549f561

Browse files
committed
ViewWindow: Add the mode argument to the linkClicked signal
1 parent f8462ff commit 549f561

15 files changed

+147
-59
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
55

66
# Project files
77
set(CPP_SOURCES
8+
browser-types.hpp
89
config.cpp
910
config.h
1011
dialog_chooseurlfromlist.cpp

src/browser-types.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* uChmViewer - a CHM and EPUB file viewer with broad language support
3+
* Copyright (C) 2022 Nick Egorrov, [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef BROWSER_TYPES_HPP
20+
#define BROWSER_TYPES_HPP
21+
22+
23+
namespace UBrowser
24+
{
25+
26+
//------------------------------------------------------------------------------
27+
// Start BrowserAPI group.
28+
/// @addtogroup BrowserAPI
29+
/// @{
30+
31+
enum OpenMode {
32+
/// The link should open in the current tab.
33+
OPEN_IN_CURRENT,
34+
/// The link should open in the new foregraund tab.
35+
OPEN_IN_NEW,
36+
/// The link should open in the new backgraund tab.
37+
OPEN_IN_BACKGROUND
38+
};
39+
40+
/// @}
41+
// End BrowserAPI group.
42+
//------------------------------------------------------------------------------
43+
44+
} // namespace UBrowser
45+
46+
#endif // BROWSER_TYPES_HPP

src/mainwindow.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include <functional>
1920
#include <cstdio>
2021
#include <cstdlib>
2122
#include <cstring>
@@ -71,6 +72,7 @@
7172

7273
class QCloseEvent;
7374

75+
#include <browser-types.hpp>
7476
#include <ebook.h>
7577

7678
#include "config.h"
@@ -136,7 +138,7 @@ MainWindow::MainWindow( const QStringList& arguments )
136138
connect(m_viewWindowMgr, &ViewWindowMgr::urlChanged,
137139
this, &MainWindow::onUrlChanged);
138140
connect(m_viewWindowMgr, &ViewWindowMgr::linkClicked,
139-
this, &MainWindow::activateUrl);
141+
this, &MainWindow::onLinkClicked);
140142

141143
// Add navigation dock
142144
m_navPanel->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
@@ -424,19 +426,19 @@ void MainWindow::activateUrl( const QUrl& link )
424426
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
425427

426428
if ( mods & Qt::ShiftModifier )
427-
openPage( link, OPF_NEW_TAB | OPF_CONTENT_TREE );
429+
openPage( link, UBrowser::OPEN_IN_NEW );
428430
else if ( mods & Qt::ControlModifier )
429-
openPage( link, OPF_NEW_TAB | OPF_BACKGROUND );
431+
openPage( link, UBrowser::OPEN_IN_BACKGROUND );
430432
else
431-
openPage( link, OPF_CONTENT_TREE );
433+
openPage( link, UBrowser::OPEN_IN_CURRENT );
432434
}
433435

434-
bool MainWindow::openPage( const QUrl& url, unsigned int flags )
436+
bool MainWindow::openPage(const QUrl& url, UBrowser::OpenMode mode )
435437
{
436-
return onLinkClicked( currentBrowser(), url, flags );
438+
return onLinkClicked( currentBrowser(), url, mode );
437439
}
438440

439-
bool MainWindow::onLinkClicked( ViewWindow* browser, const QUrl& url, unsigned int flags )
441+
bool MainWindow::onLinkClicked( ViewWindow* browser, const QUrl& url, UBrowser::OpenMode mode )
440442
{
441443
QString otherlink;
442444

@@ -470,22 +472,23 @@ bool MainWindow::onLinkClicked( ViewWindow* browser, const QUrl& url, unsigned i
470472
return false; // do not change the current page.
471473
}
472474

473-
if ( flags & OPF_NEW_TAB )
475+
476+
if ( mode == UBrowser::OPEN_IN_NEW || mode == UBrowser::OPEN_IN_BACKGROUND )
474477
{
475478
qreal zoom = currentBrowser()->zoomFactor();
476-
browser = m_viewWindowMgr->addNewTab( !(flags & OPF_BACKGROUND) );
479+
browser = m_viewWindowMgr->addNewTab( mode != UBrowser::OPEN_IN_BACKGROUND );
477480
browser->setZoomFactor( zoom );
478481
}
479482

480483
browser->load( url );
481484

482-
// Open all the tree items to show current item (if needed)
483-
if ( (flags & OPF_CONTENT_TREE) != 0 )
485+
if ( mode != UBrowser::OPEN_IN_BACKGROUND )
486+
{
487+
// Open all the tree items to show current item (if needed)
484488
m_navPanel->findUrlInContents( url );
485-
486-
// Focus on the view window so keyboard scroll works; do not do it for the background tabs
487-
if ( (flags & OPF_BACKGROUND) == 0 )
489+
// Focus on the view window so keyboard scroll works; do not do it for the background tabs
488490
browser->setFocus( Qt::OtherFocusReason );
491+
}
489492

490493
return true;
491494
}
@@ -729,12 +732,12 @@ QUrl MainWindow::getNewTabLink() const
729732

730733
void MainWindow::onOpenPageInNewTab( )
731734
{
732-
openPage( getNewTabLink(), OPF_NEW_TAB | OPF_CONTENT_TREE );
735+
openPage( getNewTabLink(), UBrowser::OPEN_IN_NEW );
733736
}
734737

735738
void MainWindow::onOpenPageInNewBackgroundTab( )
736739
{
737-
openPage( getNewTabLink(), OPF_NEW_TAB | OPF_BACKGROUND );
740+
openPage( getNewTabLink(), UBrowser::OPEN_IN_BACKGROUND );
738741
}
739742

740743
void MainWindow::browserChanged(ViewWindow* browser)

src/mainwindow.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class QMenu;
3636
class QSharedMemory;
3737
class QTemporaryFile;
3838

39+
#include <browser-types.hpp>
3940
#include <ebook.h>
4041

4142
class NavigationPanel;
@@ -66,20 +67,11 @@ class MainWindow : public QMainWindow, public Ui::MainWindow
6667
{
6768
Q_OBJECT
6869

69-
public:
70-
// "Open page" parameter flags
71-
enum
72-
{
73-
OPF_CONTENT_TREE = 1 << 0, //! Locate this page in the content tree
74-
OPF_NEW_TAB = 1 << 2, //! Open the page in a new tab
75-
OPF_BACKGROUND = 1 << 3 //! Open the page in a new tab in background
76-
};
77-
7870
public:
7971
MainWindow( const QStringList& arguments );
8072
~MainWindow();
8173

82-
bool openPage (const QUrl& url, unsigned int flags = OPF_CONTENT_TREE );
74+
bool openPage (const QUrl& url, UBrowser::OpenMode mode = UBrowser::OPEN_IN_CURRENT );
8375

8476
EBook* chmFile() const { return m_ebookFile; }
8577
const QString& getOpenedFileName () { return m_ebookFilename; }
@@ -161,7 +153,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindow
161153

162154
// Link activation
163155
void activateUrl( const QUrl& link );
164-
bool onLinkClicked( ViewWindow* browser, const QUrl& url, unsigned int flags );
156+
bool onLinkClicked( ViewWindow* browser, const QUrl& url, UBrowser::OpenMode mode );
165157

166158
void updateToolbars();
167159
void updateActions();

src/navigationpanel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void NavigationPanel::showPrevInToc()
160160
lit--;
161161

162162
if ( *lit )
163-
::mainWindow->openPage( ((TreeItem_TOC*) (*lit) )->getUrl(), MainWindow::OPF_CONTENT_TREE );
163+
::mainWindow->openPage( ((TreeItem_TOC*) (*lit) )->getUrl() );
164164
}
165165

166166
void NavigationPanel::showNextInToc()
@@ -178,7 +178,7 @@ void NavigationPanel::showNextInToc()
178178
lit++;
179179

180180
if ( *lit )
181-
::mainWindow->openPage( ((TreeItem_TOC*) (*lit) )->getUrl(), MainWindow::OPF_CONTENT_TREE );
181+
::mainWindow->openPage( ((TreeItem_TOC*) (*lit) )->getUrl() );
182182
}
183183

184184
int NavigationPanel::active() const

src/qtwebengine/viewwindow.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include <QApplication>
2020
#include <QContextMenuEvent>
21-
#include <QDialog>
2221
#include <QKeySequence>
2322
#include <QMenu>
2423
#include <QPalette>
@@ -42,8 +41,15 @@
4241

4342
class QPrinter;
4443

44+
#include <browser-settings.hpp>
45+
#include <browser-types.hpp>
4546
#include <ebook.h>
4647

48+
#include "../i18n.h"
49+
#include "../mainwindow.h"
50+
#include "../viewwindowmgr.h"
51+
#include "webenginepage.h"
52+
4753
#include "viewwindow.h"
4854

4955

@@ -62,7 +68,7 @@ ViewWindow::ViewWindow( QWidget* parent )
6268
m_storedScrollbarPosition = 0;
6369

6470
WebEnginePage* page = new WebEnginePage( this );
65-
connect( page, SIGNAL( linkClicked ( const QUrl& ) ), this, SLOT( onLinkClicked( const QUrl& ) ) );
71+
connect( page, SIGNAL( linkClicked ( const QUrl&, UBrowser::OpenMode ) ), this, SLOT( onLinkClicked( const QUrl&, UBrowser::OpenMode ) ) );
6672
setPage( page );
6773

6874
connect( this, SIGNAL( loadFinished(bool)), this, SLOT( onLoadFinished(bool)) );
@@ -294,9 +300,9 @@ void ViewWindow::onLoadFinished ( bool )
294300
emit dataLoaded( this );
295301
}
296302

297-
void ViewWindow::onLinkClicked(const QUrl& url)
303+
void ViewWindow::onLinkClicked(const QUrl& url, UBrowser::OpenMode mode)
298304
{
299-
emit linkClicked( url );
305+
emit linkClicked( url, mode );
300306
}
301307

302308
void ViewWindow::applySettings(BrowserSettings& settings)

src/qtwebengine/viewwindow.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class QMenu;
3232
class QPrinter;
3333
class QWidget;
3434

35+
#include <browser-types.hpp>
36+
3537
struct BrowserSettings;
3638

3739

@@ -53,7 +55,7 @@ class ViewWindow : public QWebEngineView
5355
void dataLoaded( ViewWindow* window );
5456

5557
// This signal is emitted whenever the user clicks on a link.
56-
void linkClicked(const QUrl& url);
58+
void linkClicked(const QUrl& url, UBrowser::OpenMode mode);
5759

5860
public:
5961
// Apply the configuration settings (JS enabled etc) to the web renderer
@@ -125,7 +127,7 @@ class ViewWindow : public QWebEngineView
125127
private slots:
126128
// Used to restore the scrollbar position and the navigation button status
127129
void onLoadFinished ( bool ok );
128-
void onLinkClicked(const QUrl& url);
130+
void onLinkClicked(const QUrl& url, UBrowser::OpenMode mode = UBrowser::OPEN_IN_CURRENT);
129131

130132
private:
131133
QMenu* m_contextMenu;

src/qtwebengine/webenginepage.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QWebEnginePage>
2727
#include <QWebEngineProfile>
2828

29+
#include <browser-types.hpp>
2930
#include <ebook_chm.h>
3031
#include <ebook_epub.h>
3132

@@ -41,7 +42,7 @@ class WebEnginePage : public QWebEnginePage
4142

4243
signals:
4344
// This signal is emitted whenever the user clicks on a link.
44-
void linkClicked( const QUrl& url );
45+
void linkClicked( const QUrl& url, UBrowser::OpenMode mode );
4546

4647
public:
4748
WebEnginePage(QObject* parent)
@@ -63,7 +64,13 @@ class WebEnginePage : public QWebEnginePage
6364
{
6465
}
6566

66-
// Link click capture. This does not work for the right mouse button.
67+
/* Link click capture. This does not work for the right mouse button.
68+
* Important! In Qt5, this function is called both when clicking with
69+
* the Ctrl and/or Shift keys pressed and when clicking without keyboard
70+
* modifiers, whereas in Qt6 it is called only when clicking without
71+
* keyboard modifiers. In any Qt, the createWindow function is called
72+
* when clicking with keyboard modifier.
73+
*/
6774
virtual bool acceptNavigationRequest( const QUrl& url, NavigationType type, bool isMainFrame ) override
6875
{
6976
#if PRINT_DEBUG
@@ -77,7 +84,14 @@ class WebEnginePage : public QWebEnginePage
7784

7885
if ( type == QWebEnginePage::NavigationTypeLinkClicked )
7986
{
80-
emit linkClicked( url );
87+
/*
88+
* Here m_url is used as a flag. If linkClicked was emitted in the createWindow
89+
* function, the m_url variable has been cleared and we don't need to re-emit
90+
* linkClicked. This is specifically for QwebEngine < 6.2.
91+
*/
92+
if ( !m_url.isEmpty() )
93+
emit linkClicked( url, UBrowser::OPEN_IN_CURRENT );
94+
8195
return false;
8296
}
8397

@@ -104,11 +118,16 @@ class WebEnginePage : public QWebEnginePage
104118
if ( !m_url.isEmpty() )
105119
{
106120
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
107-
if ( !( mods & (Qt::ShiftModifier | Qt::ControlModifier ) ) )
108-
linkClicked( ( m_url ) );
121+
122+
if ( ( mods & Qt::ShiftModifier ) != 0 || type == QWebEnginePage::WebBrowserTab)
123+
emit linkClicked( m_url, UBrowser::OPEN_IN_NEW );
124+
else
125+
emit linkClicked( m_url, UBrowser::OPEN_IN_BACKGROUND );
126+
127+
m_url.clear();
109128
}
110129

111-
return 0;
130+
return nullptr;
112131
}
113132

114133
protected slots:

0 commit comments

Comments
 (0)