diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-01-16 14:01:01 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-01-16 14:01:01 +0100 |
commit | a58c58148db61107497fbdf7fa98ceee96c99c4d (patch) | |
tree | 6b9837916015635be06c87b4e69c63844f8c831e /src/mainwindow.cpp | |
parent | Download manager (diff) | |
download | smolbote-a58c58148db61107497fbdf7fa98ceee96c99c4d.tar.xz |
Added license headers
Diffstat (limited to 'src/mainwindow.cpp')
-rw-r--r-- | src/mainwindow.cpp | 332 |
1 files changed, 176 insertions, 156 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9e028e4..1a52ee2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,156 +1,176 @@ -#include "mainwindow.h"
-#include "ui_mainwindow.h"
-#include "settings.h"
-#include <QMenu>
-#include <QMenuBar>
-#include <QCloseEvent>
-#include <QMessageBox>
-#include "browser.h"
-#include "forms/profiledialog.h"
-#include <QApplication>
-#include <QInputDialog>
-#include <QWebEngineDownloadItem>
-
-MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) :
- QMainWindow(parent),
- downloadManager(new DownloadDialog(this)),
- ui(new Ui::MainWindow),
- navigationToolBar(new QToolBar(this)),
- tabToolBar(new QToolBar(this)),
- tabBar(new WebViewTabBar(this)),
- urlLineEdit(new QLineEdit(navigationToolBar))
-{
- browserInstance = instance;
- Settings settings;
-
- // Load profile and connect its signals
- loadProfile(settings.value("defaults/profile").toString());
-
- 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
- QMenuBar *rightBar = new QMenuBar(ui->menuBar);
- profileMenu = new QMenu(tr("Profile: ") + profileName);
- rightBar->addMenu(profileMenu);
- ui->menuBar->setCornerWidget(rightBar);
- profileMenu->addAction(tr("Edit profile"), this, SLOT(createProfileDialog()));
- profileMenu->addAction(tr("Load profile"), this, SLOT(handleLoadProfile()));
- profileMenu->addAction(tr("Downloads"), downloadManager, SLOT(show()));
- //profileMenu->addAction(tr("Settings"));
- //profileMenu->addAction(tr("Cookies"));
-
- 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)
-{
- tabBar->addTab(profile, url);
-}
-
-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::loadProfile(const QString &name)
-{
- if(profile) {
- profile->deleteLater();
- }
-
- if(name.isEmpty()) {
- qDebug("Creating off-the-record profile");
- profileName = tr("Off the record");
- profile = new WebEngineProfile(this);
- } else {
- profileName = name;
- qDebug("Using profile: %s", qUtf8Printable(profileName));
- profile = new WebEngineProfile(profileName, this);
- }
-
- connect(profile, SIGNAL(downloadRequested(QWebEngineDownloadItem*)), downloadManager, SLOT(addDownload(QWebEngineDownloadItem*)));
-}
-
-void MainWindow::handleLoadProfile()
-{
- bool ok;
- QString name = QInputDialog::getText(this, tr("Load Profile"), tr("Enter Profile name"), QLineEdit::Normal, QString(""), &ok);
- if(ok) {
- loadProfile(name);
- profileMenu->setTitle(tr("Profile: ") + profileName);
- tabBar->setProfile(profile);
- }
-}
-
-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").toString());
-}
-
-void MainWindow::createProfileDialog()
-{
- ProfileDialog *dialog = new ProfileDialog(profile, this);
- dialog->exec();
-}
+/** LICENSE ******************************************************************** + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "settings.h" +#include <QMenu> +#include <QMenuBar> +#include <QCloseEvent> +#include <QMessageBox> +#include "browser.h" +#include "forms/profiledialog.h" +#include <QApplication> +#include <QInputDialog> +#include <QWebEngineDownloadItem> + +MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : + QMainWindow(parent), + downloadManager(new DownloadDialog(this)), + ui(new Ui::MainWindow), + navigationToolBar(new QToolBar(this)), + tabToolBar(new QToolBar(this)), + tabBar(new WebViewTabBar(this)), + urlLineEdit(new QLineEdit(navigationToolBar)) +{ + browserInstance = instance; + Settings settings; + + // Load profile and connect its signals + loadProfile(settings.value("defaults/profile").toString()); + + 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 + QMenuBar *rightBar = new QMenuBar(ui->menuBar); + profileMenu = new QMenu(tr("Profile: ") + profileName); + rightBar->addMenu(profileMenu); + ui->menuBar->setCornerWidget(rightBar); + profileMenu->addAction(tr("Edit profile"), this, SLOT(createProfileDialog())); + profileMenu->addAction(tr("Load profile"), this, SLOT(handleLoadProfile())); + profileMenu->addAction(tr("Downloads"), downloadManager, SLOT(show())); + //profileMenu->addAction(tr("Settings")); + //profileMenu->addAction(tr("Cookies")); + + 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) +{ + tabBar->addTab(profile, url); +} + +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::loadProfile(const QString &name) +{ + if(profile) { + profile->deleteLater(); + } + + if(name.isEmpty()) { + qDebug("Creating off-the-record profile"); + profileName = tr("Off the record"); + profile = new WebEngineProfile(this); + } else { + profileName = name; + qDebug("Using profile: %s", qUtf8Printable(profileName)); + profile = new WebEngineProfile(profileName, this); + } + + connect(profile, SIGNAL(downloadRequested(QWebEngineDownloadItem*)), downloadManager, SLOT(addDownload(QWebEngineDownloadItem*))); +} + +void MainWindow::handleLoadProfile() +{ + bool ok; + QString name = QInputDialog::getText(this, tr("Load Profile"), tr("Enter Profile name"), QLineEdit::Normal, QString(""), &ok); + if(ok) { + loadProfile(name); + profileMenu->setTitle(tr("Profile: ") + profileName); + tabBar->setProfile(profile); + } +} + +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").toString()); +} + +void MainWindow::createProfileDialog() +{ + ProfileDialog *dialog = new ProfileDialog(profile, this); + dialog->exec(); +} |