aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
blob: 7987a8774549921963d5ecec8966cd83a3619c9a (plain)
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
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMenu>

MainWindow::MainWindow(QSettings *settings, 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))
{
    settings->beginGroup("defaults");

    ui->setupUi(this);
    resize(settings->value("width", 800).toInt(), settings->value("height", 600).toInt());

    // Populate the menu bar
    // Browser menu - with new window, new tab, open and quit
    QMenu *browserMenu = new QMenu(tr("Browser"), ui->menuBar);
    browserMenu->addAction(tr("Quit"), qApp, SLOT(quit()), QKeySequence(tr("Ctrl+Q")));
    browserMenu->addAction(tr("New Tab"), this, SLOT(createNewTab()), QKeySequence(tr("Ctrl+T")));
    ui->menuBar->addMenu(browserMenu);
    // View menu - fullscreen
    // Page menu - what page actions?
    // Help menu - help contents, about, about qt

    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("url", QUrl("http://duckduckgo.com")).toUrl());

    settings->endGroup(); // "defaults"
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createNewTab(const QUrl &url)
{
    QWebEngineView *view = new QWebEngineView(0);
    view->load(url);
    tabBar->addTab(view);
}

void MainWindow::handleTabChanged(QWebEngineView *view)
{
    centralWidget()->setParent(0);
    disconnect(centralWidget());
    setCentralWidget(view);
    connect(view, SIGNAL(urlChanged(QUrl)), this, SLOT(handleUrlUpdated(QUrl)));
    this->handleUrlUpdated(view->url());
}

void MainWindow::handleUrlChanged()
{
    tabBar->currentView()->load(QUrl::fromUserInput(urlLineEdit->text()));
}

void MainWindow::handleUrlUpdated(const QUrl &url)
{
    urlLineEdit->setText(url.toString());
}