From 7ec51f7da796a106288f37330f6a484cf773dff1 Mon Sep 17 00:00:00 2001 From: David Kaya Date: Fri, 3 Feb 2017 00:49:10 +0100 Subject: [PATCH] Initial Testa commit --- applicationcontroller.cpp | 59 ++++++++++++++++++++++++++ applicationcontroller.h | 28 +++++++++++++ main.cpp | 17 ++++++++ mainwindow.cpp | 11 +++++ mainwindow.h | 15 +++++++ nativeprogressbar_mac.mm | 87 +++++++++++++++++++++++++++++++++++++++ notification.cpp | 19 +++++++++ notification.h | 15 +++++++ progressbar.h | 13 ++++++ progressbar_mac.mm | 15 +++++++ testa.pro | 43 +++++++++++++++++++ webengineview.cpp | 36 ++++++++++++++++ webengineview.h | 20 +++++++++ 13 files changed, 378 insertions(+) create mode 100644 applicationcontroller.cpp create mode 100644 applicationcontroller.h create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 nativeprogressbar_mac.mm create mode 100644 notification.cpp create mode 100644 notification.h create mode 100644 progressbar.h create mode 100644 progressbar_mac.mm create mode 100644 testa.pro create mode 100644 webengineview.cpp create mode 100644 webengineview.h diff --git a/applicationcontroller.cpp b/applicationcontroller.cpp new file mode 100644 index 0000000..e1270f6 --- /dev/null +++ b/applicationcontroller.cpp @@ -0,0 +1,59 @@ +#include +#include "applicationcontroller.h" +#include "notification.h" +#include "progressbar.h" + +ApplicationController::ApplicationController(WebEngineView* webEngineView, QObject *parent) : + QObject(parent), + webEngineView(webEngineView), + webSocket(new QWebSocket()) +{ + connect(webSocket, &QWebSocket::connected, this, &ApplicationController::connected); + connect(webSocket, &QWebSocket::textMessageReceived, this, &ApplicationController::messageReceived); + connect(webSocket, static_cast(&QWebSocket::error), this, &ApplicationController::error); + + webSocket->open(QUrl("ws://localhost:12345")); // TODO: extract to configuration +} + +void ApplicationController::connected() +{ + qDebug() << "Connected"; +} + +void ApplicationController::messageReceived(const QString &message) +{ + QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8()); + QJsonObject jsonObject = jsonDocument.object(); + + QString messageType = getMessageType(jsonObject); + QJsonObject parameters = getParameters(jsonObject); + + if (messageType == "loadUrl") { + QUrl url(parameters["url"].toString()); + + webEngineView->loadUrl(url); + } else if (messageType == "showNotification") { + const QString title = parameters["title"].toString(); + const QString message = parameters["message"].toString(); + + Notification::show(title.toStdString(), message.toStdString()); + } else if (messageType == "setProgress") { + float value = parameters["value"].toDouble(); + + ProgressBar::show(value); + } +} +void ApplicationController::error(QAbstractSocket::SocketError error) +{ + qDebug() << webSocket->errorString(); +} + +QString ApplicationController::getMessageType(const QJsonObject &jsonObject) +{ + return jsonObject["action"].toString(); +} + +QJsonObject ApplicationController::getParameters(const QJsonObject &jsonObject) +{ + return jsonObject["parameters"].toObject(); +} diff --git a/applicationcontroller.h b/applicationcontroller.h new file mode 100644 index 0000000..50fec3f --- /dev/null +++ b/applicationcontroller.h @@ -0,0 +1,28 @@ +#ifndef APPLICATIONCONTROLLER_H +#define APPLICATIONCONTROLLER_H + +#include +#include +#include +#include +#include "webengineview.h" + +class ApplicationController : public QObject +{ + Q_OBJECT +public: + explicit ApplicationController(WebEngineView *webEngineView, QObject *parent = 0); + void test(); +private slots: + void connected(); + void messageReceived(const QString &message); + void error(QAbstractSocket::SocketError error); +private: + QWebSocket* webSocket; + WebEngineView* webEngineView; + + QString getMessageType(const QJsonObject &jsonObject); + QJsonObject getParameters(const QJsonObject &jsonObject); +}; + +#endif // APPLICATIONCONTROLLER_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..23a00cd --- /dev/null +++ b/main.cpp @@ -0,0 +1,17 @@ +#include + +#include "mainwindow.h" +#include "webengineview.h" +#include "applicationcontroller.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication::setAttribute(Qt::ApplicationAttribute::AA_EnableHighDpiScaling); + QApplication a(argc, argv); + + WebEngineView view; + + ApplicationController applicationController(&view); + + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..9716148 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ +} + +MainWindow::~MainWindow() +{ + +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..ca7ddb5 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,15 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + ~MainWindow(); +}; + +#endif // MAINWINDOW_H diff --git a/nativeprogressbar_mac.mm b/nativeprogressbar_mac.mm new file mode 100644 index 0000000..55cb84d --- /dev/null +++ b/nativeprogressbar_mac.mm @@ -0,0 +1,87 @@ +// BSD License: https://github.com/hokein/DockProgressBar + +#import + +@interface DockDownloadProgressBar : NSProgressIndicator + ++ (DockDownloadProgressBar*)sharedDockDownloadProgressBar; + +- (void)setProgress:(float)progress; + +- (void)updateProgressBar; + +- (void)hideProgressBar; + +- (void)clear; + +@end + +@implementation DockDownloadProgressBar + ++ (DockDownloadProgressBar*)sharedDockDownloadProgressBar { + static DockDownloadProgressBar* progress_bar; + NSDockTile* dock_tile = [NSApp dockTile]; + if (!progress_bar) { + progress_bar = [[DockDownloadProgressBar alloc] initWithFrame: + NSMakeRect(0.0f, 0.0f, dock_tile.size.width, 15.0f)]; + [progress_bar setStyle:NSProgressIndicatorBarStyle]; + [progress_bar setIndeterminate:NO]; + [progress_bar setBezeled:YES]; + [progress_bar setMinValue:0]; + [progress_bar setMaxValue:1]; + [progress_bar setHidden:NO]; + } + if ([dock_tile contentView] == NULL) { + NSImageView* content_view = [[NSImageView alloc] init]; + [content_view setImage:[NSApp applicationIconImage]]; + [dock_tile setContentView:content_view]; + [content_view addSubview:progress_bar]; + } + return progress_bar; +} + +- (void)drawRect:(NSRect)dirtyRect { + // Draw edges of rounded rect. + NSRect rect = NSInsetRect([self bounds], 1.0, 1.0); + CGFloat radius = rect.size.height / 2; + NSBezierPath* bezier_path = [NSBezierPath bezierPathWithRoundedRect:rect + xRadius:radius + yRadius:radius]; + [bezier_path setLineWidth:2.0]; + [[NSColor grayColor] set]; + [bezier_path stroke]; + + // Fill the rounded rect. + rect = NSInsetRect(rect, 2.0, 2.0); + radius = rect.size.height / 2; + bezier_path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius]; + [bezier_path setLineWidth:1.0]; + [bezier_path addClip]; + + // Calculate the progress width. + rect.size.width = floor(rect.size.width * ([self doubleValue] / [self maxValue])); + + // Fill the progress bar with color blue. + [[NSColor colorWithSRGBRed:0.2 green:0.6 blue:1 alpha:1] set]; + NSRectFill(rect); +} + +- (void)updateProgressBar { + [self setHidden:NO]; + [[NSApp dockTile] display]; +} + +- (void)hideProgressBar { + [self setHidden:YES]; + [[NSApp dockTile] display]; +} + +- (void)setProgress:(float)progress { + [self setDoubleValue:progress]; +} + +- (void)clear { + [[NSApp dockTile] setContentView:NULL]; +} + +@end diff --git a/notification.cpp b/notification.cpp new file mode 100644 index 0000000..5d54fec --- /dev/null +++ b/notification.cpp @@ -0,0 +1,19 @@ +#include + +#include "notification.h" + +static QSystemTrayIcon* sharedSystemTrayIcon; + +void Notification::show(const std::string& title, const std::string& message) +{ + if (sharedSystemTrayIcon == nullptr) + sharedSystemTrayIcon = new QSystemTrayIcon(); + sharedSystemTrayIcon->show(); + sharedSystemTrayIcon->showMessage(QString::fromStdString(title), QString::fromStdString(message)); +} + +void Notification::hide() +{ + sharedSystemTrayIcon->hide(); + delete sharedSystemTrayIcon; +} diff --git a/notification.h b/notification.h new file mode 100644 index 0000000..7184014 --- /dev/null +++ b/notification.h @@ -0,0 +1,15 @@ +#ifndef NOTIFICATION_H +#define NOTIFICATION_H + +#include + +class Notification +{ +public: + static void show(const std::string& title, const std::string& message); + static void hide(); +private: + Notification() { } +}; + +#endif // NOTIFICATION_H diff --git a/progressbar.h b/progressbar.h new file mode 100644 index 0000000..ddc46a4 --- /dev/null +++ b/progressbar.h @@ -0,0 +1,13 @@ +#ifndef PROGRESSBAR_H +#define PROGRESSBAR_H + +class ProgressBar +{ +public: + static void show(float progres); + static void hide(); +private: + ProgressBar() {} +}; + +#endif // PROGRESSBAR_H diff --git a/progressbar_mac.mm b/progressbar_mac.mm new file mode 100644 index 0000000..3e57ea8 --- /dev/null +++ b/progressbar_mac.mm @@ -0,0 +1,15 @@ +#include "progressbar.h" +#include "nativeprogressbar_mac.mm" + +void ProgressBar::show(float progress) +{ + DockDownloadProgressBar* progressBar = [DockDownloadProgressBar sharedDockDownloadProgressBar]; + [progressBar setProgress:progress]; + [progressBar updateProgressBar]; +} + +void ProgressBar::hide() +{ + DockDownloadProgressBar* progressBar = [DockDownloadProgressBar sharedDockDownloadProgressBar]; + [progressBar hideProgressBar]; +} diff --git a/testa.pro b/testa.pro new file mode 100644 index 0000000..9316820 --- /dev/null +++ b/testa.pro @@ -0,0 +1,43 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-01-27T18:13:16 +# +#------------------------------------------------- + +QT += core gui webenginewidgets websockets + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = desktop-shell +TEMPLATE = app + +LIBS += -framework CoreData -framework Foundation -framework AppKit + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + + +SOURCES += main.cpp \ + mainwindow.cpp \ + webengineview.cpp \ + notification.cpp \ + applicationcontroller.cpp + +macx { + OBJECTIVE_SOURCES += nativeprogressbar_mac.mm \ + progressbar_mac.mm +} + +HEADERS += mainwindow.h \ + webengineview.h \ + progressbar.h \ + notification.h \ + applicationcontroller.h diff --git a/webengineview.cpp b/webengineview.cpp new file mode 100644 index 0000000..71d5663 --- /dev/null +++ b/webengineview.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +#include "webengineview.h" + +WebEngineView::WebEngineView() + : QWebEngineView() +{ + connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showCustomContextMenu(const QPoint&))); + + setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); + resize(1024, 750); // TODO: from config +} + +void WebEngineView::showCustomContextMenu(const QPoint& a_pos) +{ + QAction printPageAction("YSoft!", NULL); + connect(&printPageAction, SIGNAL(triggered()), this, SLOT(printPage())); + + QAction openPageInBrowserAction("Custom context menu?", NULL); + + QMenu* contextMenu = new QMenu(this); + contextMenu->addAction(&printPageAction); + contextMenu->addAction(&openPageInBrowserAction); + contextMenu->exec(mapToGlobal(a_pos)); + + delete contextMenu; +} + +void WebEngineView::loadUrl(QUrl url) +{ + setUrl(url); + show(); +} diff --git a/webengineview.h b/webengineview.h new file mode 100644 index 0000000..31f6796 --- /dev/null +++ b/webengineview.h @@ -0,0 +1,20 @@ +#ifndef WEBENGINEVIEW_H +#define WEBENGINEVIEW_H + +#include +#include +#include +#include + +class WebEngineView : public QWebEngineView +{ + Q_OBJECT +public: + WebEngineView(); + +public Q_SLOTS: + void showCustomContextMenu(const QPoint&); + void loadUrl(QUrl url); +}; + +#endif // WEBENGINEVIEW_H