Initial Testa commit

This commit is contained in:
David Kaya
2017-02-03 00:49:10 +01:00
parent 26e441c1d7
commit 7ec51f7da7
13 changed files with 378 additions and 0 deletions

59
applicationcontroller.cpp Normal file
View File

@@ -0,0 +1,59 @@
#include <QJsonDocument>
#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<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&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();
}

28
applicationcontroller.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef APPLICATIONCONTROLLER_H
#define APPLICATIONCONTROLLER_H
#include <QObject>
#include <QJsonObject>
#include <QAbstractSocket>
#include <QtWebSockets/QWebSocket>
#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

17
main.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include <QApplication>
#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();
}

11
mainwindow.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}

15
mainwindow.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H

87
nativeprogressbar_mac.mm Normal file
View File

@@ -0,0 +1,87 @@
// BSD License: https://github.com/hokein/DockProgressBar
#import <Cocoa/Cocoa.h>
@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

19
notification.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include <QSystemTrayIcon>
#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;
}

15
notification.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef NOTIFICATION_H
#define NOTIFICATION_H
#include <string>
class Notification
{
public:
static void show(const std::string& title, const std::string& message);
static void hide();
private:
Notification() { }
};
#endif // NOTIFICATION_H

13
progressbar.h Normal file
View File

@@ -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

15
progressbar_mac.mm Normal file
View File

@@ -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];
}

43
testa.pro Normal file
View File

@@ -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

36
webengineview.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include <QtGlobal>
#include <QtWidgets/QMenu>
#include <QtGui/QContextMenuEvent>
#include <QSystemTrayIcon>
#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();
}

20
webengineview.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef WEBENGINEVIEW_H
#define WEBENGINEVIEW_H
#include <QtCore/QtGlobal>
#include <QtWebEngineWidgets/QWebEngineView>
#include <QtWebEngineWidgets/QWebEnginePage>
#include <QtWebEngineWidgets/QWebEngineSettings>
class WebEngineView : public QWebEngineView
{
Q_OBJECT
public:
WebEngineView();
public Q_SLOTS:
void showCustomContextMenu(const QPoint&);
void loadUrl(QUrl url);
};
#endif // WEBENGINEVIEW_H