1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settings.h"
#include <QMenu>
#include <QCloseEvent>
#include <QMessageBox>
#include "browser.h"
#include "forms/profiledialog.h"
#include <QApplication>
MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
navigationToolBar(new QToolBar(this)),
tabToolBar(new QToolBar(this)),
tabBar(new WebViewTabBar(this)),
urlLineEdit(new QLineEdit(navigationToolBar))
{
browserInstance = instance;
Settings settings;
profile = QWebEngineProfile::defaultProfile();
ui->setupUi(this);
resize(settings.value("window/width", 800).toInt(), settings.value("window/height", 600).toInt());
// Browser menu
browserMenu = new QMenu(qApp->applicationName(), ui->menuBar);
ui->menuBar->addMenu(browserMenu);
browserMenu->addAction(tr("New Window"), this, SLOT(handleNewWindow()), QKeySequence(tr("Ctrl+N")));
browserMenu->addAction(tr("New Tab"), this, SLOT(createNewTab()), QKeySequence(tr("Ctrl+T")));
browserMenu->addSeparator();
browserMenu->addAction(tr("About Qt"), qApp, SLOT(aboutQt()));
browserMenu->addAction(tr("Quit"), qApp, SLOT(quit()), QKeySequence(tr("Ctrl+Q")));
// Profile menu
profileMenu = new QMenu(profile->storageName());
ui->menuBar->addMenu(profileMenu);
profileMenu->addAction(tr("Edit profile"), this, SLOT(createProfileDialog()));
profileMenu->addAction(tr("Load profile"));
this->addToolBar(Qt::TopToolBarArea, navigationToolBar);
this->addToolBarBreak(Qt::TopToolBarArea);
this->addToolBar(Qt::TopToolBarArea, tabToolBar);
navigationToolBar->addWidget(urlLineEdit);
connect(urlLineEdit, SIGNAL(returnPressed()), this, SLOT(handleUrlChanged()));
tabToolBar->addWidget(tabBar);
connect(tabBar, SIGNAL(currentTabChanged(QWebEngineView*)), this, SLOT(handleTabChanged(QWebEngineView*)));
if(!defaultUrl.isEmpty())
createNewTab(defaultUrl);
else
createNewTab(settings.value("defaults/url", QUrl("http://duckduckgo.com")).toUrl());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createNewTab(const QUrl &url)
{
QWebEngineView *view = new QWebEngineView(0);
view->load(url);
tabBar->addTab(view);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(tabBar->count() > 1) {
int ret = QMessageBox::warning(this, tr("Close window?"), tr("Close multiple tabs?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if(ret == QMessageBox::No) {
event->ignore();
return;
}
}
QMainWindow::closeEvent(event);
}
void MainWindow::handleNewWindow(const QUrl &url)
{
browserInstance->addWindow(new MainWindow(browserInstance, url));
}
void MainWindow::handleTabChanged(QWebEngineView *view)
{
centralWidget()->setParent(0);
disconnect(centralWidget());
setCentralWidget(view);
connect(view, SIGNAL(urlChanged(QUrl)), this, SLOT(handleUrlUpdated(QUrl)));
connect(view, SIGNAL(titleChanged(QString)), this, SLOT(handleTitleUpdated(QString)));
this->handleUrlUpdated(view->url());
this->handleTitleUpdated(view->title());
}
void MainWindow::handleUrlChanged()
{
tabBar->currentView()->load(QUrl::fromUserInput(urlLineEdit->text()));
}
void MainWindow::handleUrlUpdated(const QUrl &url)
{
urlLineEdit->setText(url.toString());
}
void MainWindow::handleTitleUpdated(const QString &title)
{
Settings settings;
setWindowTitle(title + settings.value("window/title", "qtwebenginebrowser").toString());
}
void MainWindow::createProfileDialog()
{
ProfileDialog *dialog = new ProfileDialog(profile, this);
dialog->exec();
}
|