OfficeAssistant_Win10/OfficeAssistant_msvc/navbar.cpp

228 lines
7.4 KiB
C++
Raw Normal View History

2023-07-26 19:48:14 +08:00
//
// Created by HW on 2023/07/26.
//
// You may need to build the project (run Qt uic code generator) to get "ui_NavBar.h" resolved
#include "navbar.h"
#include "ui_NavBar.h"
2023-07-30 01:06:42 +08:00
#include "config.h"
#include <QTimer>
#include <QtSvg/QtSvg>
2023-08-01 23:51:40 +08:00
#include <QPixmap>
2023-07-30 10:26:41 +08:00
#include "mainwindowlayout.h"
#include "globalvariables.h"
2023-07-30 01:06:42 +08:00
#ifdef _DEBUG
#pragma comment(lib, "Qt5Svgd.lib")
#else
2023-07-31 19:06:37 +08:00
#pragma comment(lib, "Qt5Svg.lib")
2023-07-30 01:06:42 +08:00
#endif
2023-08-01 09:04:53 +08:00
QString url_param;
2023-07-31 19:06:37 +08:00
NavBar::NavBar(ConfigResponse *configResponse, MainScreen *mainScreen,QWidget *parent) :
2023-07-26 19:48:14 +08:00
QWidget(parent), ui(new Ui::NavBar) {
ui->setupUi(this);
2023-08-01 23:51:40 +08:00
//this->setAttribute(Qt::WA_DeleteOnClose);
2023-07-30 13:57:54 +08:00
buffer = new QByteArray;
2023-08-01 23:51:40 +08:00
/*QString style = "background-color:";
style += configResponse->basic.backgroud_color.toUpper();
style += ";";
setStyleSheet(style);*/
//setStyleSheet("margin:0");
setContentsMargins(0, 0, 0, 0);
2023-07-30 13:57:54 +08:00
if (configResponse->succeed&&configResponse->menus.empty()!=true) {
2023-07-30 01:06:42 +08:00
getLogoFromInternet(configResponse);
}
else {
getLogoFromLocal();
2023-07-30 13:57:54 +08:00
}
2023-08-01 23:51:40 +08:00
layout2 = new QHBoxLayout;
layout_right = new QHBoxLayout;
layout_left = new QHBoxLayout;
logo_label = new QLabel(this);
//*logo = logo->scaled(this->width() / (5 / scale), this->height());
logo_label->setScaledContents(true);
logo_label->setPixmap(QPixmap::fromImage(*logo));
logo_label->setMaximumHeight(parent->height() / 8);
layout_left->addWidget(logo_label);
layout_left->setAlignment(Qt::AlignCenter);
2023-07-30 01:06:42 +08:00
for (auto buttonStruct : buttonStructs) {
2023-07-30 18:57:41 +08:00
MyButton *myButton = new MyButton(buttonStruct, height(), height(), &buttons,this);
2023-08-01 23:51:40 +08:00
myButton->setMaximumHeight(parent->height() / 8);
layout_right->addWidget(myButton);
//myButton->show();
2023-07-30 10:26:41 +08:00
connect(myButton, &MyButton::clicked1, mainWindowLayout, &MainWindowLayout::clickButton);
2023-07-30 01:06:42 +08:00
buttons << myButton;
}
2023-08-01 23:51:40 +08:00
layout_right->setAlignment(Qt::AlignLeft);
2023-07-31 19:06:37 +08:00
if (!buttonStructs.empty()) {
connect(this, &NavBar::firstUrl, mainScreen, &MainScreen::firstUrl);
emit firstUrl(buttonStructs[0].url);
}
2023-08-01 23:51:40 +08:00
layout2->addLayout(layout_left, 1);
layout2->addLayout(layout_right, 4);
layout_right->setMargin(0);
layout2->setMargin(0);
//layout2->setAlignment(Qt::AlignJustify);
this->setLayout(layout2);
logo_label->show();
2023-07-26 19:48:14 +08:00
}
NavBar::~NavBar() {
2023-07-30 01:06:42 +08:00
for (auto buttonStruct : buttonStructs) {
delete buttonStruct.image;
delete buttonStruct.image_cover;
}
for(auto button : buttons){
2023-08-01 23:51:40 +08:00
layout_right->removeWidget(button);
2023-07-26 19:48:14 +08:00
delete button;
}
2023-08-01 23:51:40 +08:00
layout2->removeWidget(logo_label);
delete logo_label;
delete logo;
//delete layout_left;
delete layout_right;
delete layout2;
2023-07-26 19:48:14 +08:00
delete ui;
}
void NavBar::paintEvent(QPaintEvent *event) {
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRectF rect(0, 0, this->width(), this->height());
2023-07-30 01:06:42 +08:00
painter.fillRect(rect, qColor);
2023-07-27 13:44:54 +08:00
}
void NavBar::resizeEvent(QResizeEvent *event) {
2023-07-30 18:57:41 +08:00
QWidget::resizeEvent(event);
2023-07-27 13:44:54 +08:00
for(auto button:buttons){
2023-08-01 23:51:40 +08:00
button->setMaximumSize(event->size().height(), event->size().height());
2023-07-27 13:44:54 +08:00
button->update();
}
2023-08-01 23:51:40 +08:00
logo_label->setMaximumSize(event->size().width() / 5, event->size().height());
2023-07-26 19:48:14 +08:00
}
2023-07-30 01:06:42 +08:00
void NavBar::getLogoFromInternet(ConfigResponse *configResponse) {
qColor.setNamedColor(configResponse->basic.backgroud_color);
2023-08-01 09:04:53 +08:00
2023-07-31 19:06:37 +08:00
QUrl url_logo(configResponse->basic.logo_url);
2023-07-30 01:06:42 +08:00
QNetworkRequest *request_logo = new QNetworkRequest(url_logo);
manager = new QNetworkAccessManager;
reply = manager->get(*request_logo);
QTimer timer;
timer.setInterval(5000);
connect(reply, &QNetworkReply::finished, &eventLoop,&QEventLoop::quit);
connect(&timer, &QTimer::timeout,this, &NavBar::cancelDownload);
2023-07-31 19:06:37 +08:00
eventLoop.exec();
2023-07-30 01:06:42 +08:00
timer.stop();
2023-07-30 13:57:54 +08:00
buffer = new QByteArray;
2023-07-30 01:06:42 +08:00
delete request_logo;
if (downloadSuccess == true) {
*buffer = reply->readAll();
2023-08-01 23:51:40 +08:00
logo = new QImage();
logo->loadFromData(*buffer);
2023-07-30 01:06:42 +08:00
buffer->clear();
2023-07-30 13:57:54 +08:00
for (auto button : configResponse->menus) {
2023-07-31 19:06:37 +08:00
QUrl url_image(button.img);
2023-07-30 01:06:42 +08:00
QNetworkRequest *request_image = new QNetworkRequest(url_image);
reply = manager->get(*request_image);
QTimer timer;
timer.setInterval(5000);
connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
connect(&timer, &QTimer::timeout, this, &NavBar::cancelDownload);
2023-07-31 19:06:37 +08:00
eventLoop.exec();
timer.stop();
2023-07-30 01:06:42 +08:00
if (!downloadSuccess) {
break;
}
*buffer = reply->readAll();
QSvgRenderer *render_image = new QSvgRenderer(*buffer);
QImage *image = new QImage(200, 200, QImage::Format_ARGB32);
QPainter painter_image(image);
render_image->render(&painter_image);
2023-07-31 19:06:37 +08:00
QUrl url_image_cover(button.img_cover);
2023-07-30 01:06:42 +08:00
QNetworkRequest *request_image_cover = new QNetworkRequest(url_image_cover);
reply = manager->get(*request_image_cover);
timer.setInterval(5000);
connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
connect(&timer, &QTimer::timeout, this, &NavBar::cancelDownload);
if (!downloadSuccess) {
break;
}
*buffer = reply->readAll();
QSvgRenderer *render_image_cover = new QSvgRenderer(*buffer);
QImage *image_cover = new QImage(200, 200, QImage::Format_ARGB32);
QPainter painter_image_cover(image_cover);
render_image_cover->render(&painter_image_cover);
ButtonStruct buttonStruct;
buttonStruct.image = image;
buttonStruct.image_cover = image_cover;
buttonStruct.text = button.title;
buttonStruct.url = button.url;
2023-08-01 09:04:53 +08:00
buttonStruct.background_color = configResponse->basic.backgroud_color;
buttonStruct.text_color.setNamedColor(configResponse->basic.title_color);
buttonStruct.text_cover_color.setNamedColor(configResponse->basic.title_cover_color);
2023-07-30 01:06:42 +08:00
buttonStructs << buttonStruct;
delete request_image;
delete render_image;
delete request_image_cover;
delete render_image_cover;
}
}
delete manager;
manager = nullptr;
}
void NavBar::storeToBuffer() {
buffer = new QByteArray;
}
void NavBar::cancelDownload() {
disconnect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
eventLoop.quit();
reply->abort();
downloadSuccess = false;
}
void NavBar::getLogoFromLocal() {
QString dir = QApplication::applicationDirPath();
QFile file(dir + DEFAULT_FILE);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(nullptr, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>"));
exit(1);
}
buffer->clear();
*buffer = file.readAll();
file.close();
QJsonDocument *qJsonDocument = new QJsonDocument;
*qJsonDocument = QJsonDocument::fromJson(*buffer);
QJsonObject *obj_root = new QJsonObject;
if (qJsonDocument->isObject()) {
*obj_root = qJsonDocument->object();
delete qJsonDocument;
QString logo_path = obj_root->value("basic").toObject().value("logo").toString();
logo = new QImage(logo_path);
QString color= obj_root->value("basic").toObject().value("backgroud_color").toString();
qColor.setNamedColor(color);
QJsonArray *array = new QJsonArray;
*array = obj_root->value("menu").toArray();
for (auto obj : *array) {
ButtonStruct buttonStruct;
buttonStruct.text = obj.toObject().value("title").toString();
buttonStruct.url = QString(BASE_URL)+obj.toObject().value("url").toString();
buttonStruct.image = new QImage(obj.toObject().value("img").toString());
buttonStruct.image_cover = new QImage(obj.toObject().value("img_cover").toString());
buttonStructs << buttonStruct;
}
delete array;
}
else {
QMessageBox::warning(nullptr, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
delete obj_root;
exit(1);
}
delete obj_root;
}