aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-01-11 10:18:08 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-01-11 10:18:08 +0100
commit120a5f7c6d31a3bc5acdb54b19326516f8399767 (patch)
tree882e3a096a2ef9bd3138633fb564024e45ba6524
downloadsmolbote-120a5f7c6d31a3bc5acdb54b19326516f8399767.tar.xz
Initial commit0.1.0
-rw-r--r--.gitignore5
-rw-r--r--src/browser.cpp55
-rw-r--r--src/browser.h32
-rw-r--r--src/main.cpp26
-rw-r--r--src/mainwindow.cpp76
-rw-r--r--src/mainwindow.h39
-rw-r--r--src/mainwindow.ui52
-rw-r--r--src/qt-simplebrowser.pro23
-rw-r--r--src/widgets/webviewtabbar.cpp50
-rw-r--r--src/widgets/webviewtabbar.h31
-rw-r--r--test/config.ini5
11 files changed, 394 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..09aa797
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+# build folders
+build*
+
+# QtCreator .pro.user settings
+*.user
diff --git a/src/browser.cpp b/src/browser.cpp
new file mode 100644
index 0000000..7ccd13a
--- /dev/null
+++ b/src/browser.cpp
@@ -0,0 +1,55 @@
+#include "browser.h"
+#include "mainwindow.h"
+#include <QtWebEngine>
+
+Browser::Browser(QString configPath, QObject *parent) : QObject(parent)
+{
+ // TODO Read Profile
+ m_settings = new QSettings(configPath, QSettings::IniFormat, this);
+
+ // TODO Restore previous session
+
+ QtWebEngine::initialize();
+}
+
+Browser::~Browser()
+{
+ // TODO Save Profile
+ m_settings->sync();
+
+ // TODO Save session
+
+ // cleanup
+ qDeleteAll(m_windows);
+ m_windows.clear();
+}
+
+
+QSettings* Browser::settings()
+{
+ return m_settings;
+}
+
+QVector<MainWindow*> Browser::windows()
+{
+ return m_windows;
+}
+
+void Browser::addWindow(MainWindow *window)
+{
+ if(m_windows.contains(window)) {
+ return;
+ }
+
+ m_windows.append(window);
+ connect(window, &QObject::destroyed, [this, window]() {
+ this->removeWindow(window);
+ });
+
+ window->show();
+}
+
+void Browser::removeWindow(MainWindow *window)
+{
+ m_windows.removeOne(window);
+}
diff --git a/src/browser.h b/src/browser.h
new file mode 100644
index 0000000..c63d56f
--- /dev/null
+++ b/src/browser.h
@@ -0,0 +1,32 @@
+#ifndef BROWSER_H
+#define BROWSER_H
+
+#include <QObject>
+#include <QVector>
+#include <QSettings>
+
+class MainWindow;
+class Browser : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit Browser(QString configPath, QObject *parent = 0);
+ ~Browser();
+
+ QSettings* settings();
+
+ // what is windows() useful for?
+ QVector<MainWindow*> windows();
+ void addWindow(MainWindow* window);
+
+public slots:
+ void removeWindow(MainWindow* window);
+
+private:
+ QSettings *m_settings;
+ QVector<MainWindow*> m_windows;
+
+};
+
+#endif // BROWSER_H
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..94ad981
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,26 @@
+#include "browser.h"
+#include "mainwindow.h"
+#include <QApplication>
+#include <QCommandLineParser>
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+ app.setApplicationName("qtwebenginebrowser");
+ app.setApplicationVersion("0.0.0");
+
+ QCommandLineParser parser;
+ parser.setApplicationDescription("Test browser using QtWebEngine");
+ parser.addHelpOption();
+ parser.addVersionOption();
+
+ QCommandLineOption configOption(QStringList() << "c" << "config", "Set configuration file.", "PATH", "config.ini");
+ parser.addOption(configOption);
+
+ parser.process(app);
+
+ Browser instance(parser.value(configOption));
+ instance.addWindow(new MainWindow(instance.settings()));
+
+ return app.exec();
+}
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
new file mode 100644
index 0000000..7987a87
--- /dev/null
+++ b/src/mainwindow.cpp
@@ -0,0 +1,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());
+}
diff --git a/src/mainwindow.h b/src/mainwindow.h
new file mode 100644
index 0000000..a130087
--- /dev/null
+++ b/src/mainwindow.h
@@ -0,0 +1,39 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+#include <QToolBar>
+#include <QLineEdit>
+#include <QWebEngineView>
+#include <QUrl>
+#include "widgets/webviewtabbar.h"
+#include <QSettings>
+
+namespace Ui {
+class MainWindow;
+}
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ explicit MainWindow(QSettings *settings, QUrl defaultUrl = QUrl(""), QWidget *parent = 0);
+ ~MainWindow();
+
+public slots:
+ void createNewTab(const QUrl &url = QUrl(""));
+
+private slots:
+ void handleTabChanged(QWebEngineView *view);
+ void handleUrlChanged();
+ void handleUrlUpdated(const QUrl &url);
+
+private:
+ Ui::MainWindow *ui;
+ QToolBar *navigationToolBar, *tabToolBar;
+ WebViewTabBar *tabBar;
+ QLineEdit *urlLineEdit;
+};
+
+#endif // MAINWINDOW_H
diff --git a/src/mainwindow.ui b/src/mainwindow.ui
new file mode 100644
index 0000000..5a9c403
--- /dev/null
+++ b/src/mainwindow.ui
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>400</width>
+ <height>300</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>MainWindow</string>
+ </property>
+ <widget class="QWidget" name="centralWidget"/>
+ <widget class="QMenuBar" name="menuBar">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>400</width>
+ <height>26</height>
+ </rect>
+ </property>
+ </widget>
+ <widget class="QStatusBar" name="statusBar"/>
+ <action name="actionNew_Window">
+ <property name="text">
+ <string>New Window</string>
+ </property>
+ </action>
+ <action name="actionNew_Tab">
+ <property name="text">
+ <string>New Tab</string>
+ </property>
+ </action>
+ <action name="actionOpen">
+ <property name="text">
+ <string>Open</string>
+ </property>
+ </action>
+ <action name="actionQuit">
+ <property name="text">
+ <string>Quit</string>
+ </property>
+ </action>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/src/qt-simplebrowser.pro b/src/qt-simplebrowser.pro
new file mode 100644
index 0000000..65dcca6
--- /dev/null
+++ b/src/qt-simplebrowser.pro
@@ -0,0 +1,23 @@
+#-------------------------------------------------
+#
+#
+#
+#-------------------------------------------------
+
+QT += core gui widgets \
+ webengine webenginewidgets
+
+TARGET = qtbrowser
+TEMPLATE = app
+
+
+SOURCES += main.cpp \
+ mainwindow.cpp \
+ browser.cpp \
+ widgets/webviewtabbar.cpp
+
+HEADERS += mainwindow.h \
+ browser.h \
+ widgets/webviewtabbar.h
+
+FORMS += mainwindow.ui
diff --git a/src/widgets/webviewtabbar.cpp b/src/widgets/webviewtabbar.cpp
new file mode 100644
index 0000000..59e5c69
--- /dev/null
+++ b/src/widgets/webviewtabbar.cpp
@@ -0,0 +1,50 @@
+#include "webviewtabbar.h"
+
+WebViewTabBar::WebViewTabBar(QWidget *parent) :
+ QTabBar(parent)
+{
+ setTabsClosable(true);
+ connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(handleTabClose(int)));
+ connect(this, SIGNAL(currentChanged(int)), this, SLOT(handleCurrentChanged(int)));
+}
+
+WebViewTabBar::~WebViewTabBar()
+{
+ // cleanup
+ qDeleteAll(m_views);
+ m_views.clear();
+}
+
+int WebViewTabBar::addTab(QWebEngineView *view)
+{
+ m_views.append(view);
+ //connect(view, SIGNAL(titleChanged()), this, SLOT(updateTabText()));
+ connect(view, &QWebEngineView::titleChanged, [this, view](const QString &title) {
+ int index = m_views.indexOf(view);
+ setTabText(index, title);
+ });
+ return QTabBar::addTab("New Tab");
+}
+
+QWebEngineView *WebViewTabBar::currentView()
+{
+ return m_views.at(currentIndex());
+}
+
+void WebViewTabBar::handleCurrentChanged(int index)
+{
+ emit(currentTabChanged(m_views.at(index)));
+}
+
+void WebViewTabBar::handleTabClose(int index)
+{
+ removeTab(index);
+ m_views.at(index)->deleteLater();
+ m_views.remove(index);
+}
+
+void WebViewTabBar::updateTabText(QWebEngineView *view, const QString &text)
+{
+ int index = m_views.indexOf(view);
+ setTabText(index, text);
+}
diff --git a/src/widgets/webviewtabbar.h b/src/widgets/webviewtabbar.h
new file mode 100644
index 0000000..28a9a18
--- /dev/null
+++ b/src/widgets/webviewtabbar.h
@@ -0,0 +1,31 @@
+#ifndef WEBVIEWTABBAR_H
+#define WEBVIEWTABBAR_H
+
+#include <QTabBar>
+#include <QWebEngineView>
+
+class WebViewTabBar : public QTabBar
+{
+ Q_OBJECT
+
+public:
+ WebViewTabBar(QWidget *parent = 0);
+ ~WebViewTabBar();
+
+ int addTab(QWebEngineView *view);
+ QWebEngineView *currentView();
+
+signals:
+ void currentTabChanged(QWebEngineView *view);
+
+private slots:
+ void handleCurrentChanged(int index);
+ void handleTabClose(int index);
+
+ void updateTabText(QWebEngineView *view, const QString &text);
+
+private:
+ QVector<QWebEngineView*> m_views;
+};
+
+#endif // WEBVIEWTABBAR_H
diff --git a/test/config.ini b/test/config.ini
new file mode 100644
index 0000000..e65e15c
--- /dev/null
+++ b/test/config.ini
@@ -0,0 +1,5 @@
+[defaults]
+title=quill
+url=https://duckduckgo.com
+width=1280
+height=720