-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
97 lines (86 loc) · 3.31 KB
/
mainwindow.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "olop.hpp"
#include <QDesktopServices>
MainWindow::MainWindow(QWidget *parent, QString url) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CustomWebEnginePage* customPage = new CustomWebEnginePage();
ui->Web->setPage(customPage);
connect(customPage, &CustomWebEnginePage::customJavaScriptConsoleMessage,
[](QWebEnginePage::JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID)
{
QString formattedSourceInfo;
if(sourceID.isEmpty()) {
formattedSourceInfo = QString::number(lineNumber) + ":";
} else {
formattedSourceInfo = QFileInfo(sourceID).fileName() + ":" + QString::number(lineNumber);
}
switch (level) {
case QWebEnginePage::InfoMessageLevel:
qDebug().noquote() << "[CONSOLE]" << formattedSourceInfo << message;
break;
case QWebEnginePage::WarningMessageLevel:
case QWebEnginePage::ErrorMessageLevel:
qWarning().noquote() << "[CONSOLE]" << formattedSourceInfo << message;
break;
default:
qDebug().noquote() << "[CONSOLE]" << message;
break;
}
});
ui->Web->setUrl(QUrl("http://localhost:"+url+"/Loading.html"));
QFile file(":/fluent_dark.qss");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
QString qss = stream.readAll();
file.close();
// Appliquer le style QSS à la fenêtre principale
this->setStyleSheet(qss);
} else {
qWarning() << "Impossible d'ouvrir le fichier QSS.";
}
connect(this, &MainWindow::resizeEvent, this, &MainWindow::resizeEvent);
connect(ui->Web, &QWebEngineView::loadFinished, this, [=](bool ok) {
//onLoadingHtmlFinished(ok, "http://localhost:"+url+"/");
});
connect(static_cast<CustomWebEnginePage*>(ui->Web->page()), &CustomWebEnginePage::urlRequested, this,
[=](const QUrl& url) {
QDesktopServices::openUrl(url);
});
// Initialisation de UiInstance
uiInstance = new Ui::UiInstance(ui->Web->page(), url);
qDebug() << "Olop a été initialisé";
}
MainWindow::~MainWindow()
{
delete uiInstance; // Ajoutez cette ligne
delete ui;
}
void MainWindow::onLoadingHtmlFinished(bool ok, const QString &url)
{
if (!ok)
return;
QWebEngineView *view = qobject_cast<QWebEngineView *>(sender());
if (!view)
return;
QUrl currentUrl = view->url();
if (currentUrl.path() == "/Loading.html" && currentUrl.host() == "localhost")
{
QString jsCommand = QString("loadOlop('%1');").arg(url);
ui->Web->page()->runJavaScript(jsCommand);
}
}
void MainWindow::resizeEvent(QResizeEvent* event)
{
QMainWindow::resizeEvent(event);
// Redimensionner le widget QWebEngine avec la fenêtre principale
ui->Web->setGeometry(0, 0, ui->centralwidget->width(), ui->centralwidget->height());
ui->Web->page()->runJavaScript("MAIN.ResizeEvent()");
}
void MainWindow::on_actionQuitter_triggered()
{
exit(0);
}