OfficeAssistant_Win10/OfficeAssistant_msvc/navbar.cpp

204 lines
6.2 KiB
C++
Raw Normal View History

2023-08-17 01:10:27 +08:00
//
2023-08-03 16:16:02 +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"
#include "config.h"
#include <QTimer>
#include <QtSvg/QtSvg>
#include <QPixmap>
#include "mainwindowlayout.h"
#include "globalvariables.h"
2023-08-13 12:16:22 +08:00
#include "MyButton.h"
#include "sqlitehelper.h"
2023-08-03 16:16:02 +08:00
#ifdef _DEBUG
#pragma comment(lib, "Qt5Svgd.lib")
#pragma comment(lib, "Qt5Sqld.lib")
2023-08-03 16:16:02 +08:00
#else
#pragma comment(lib, "Qt5Svg.lib")
#pragma comment(lib, "Qt5Sql.lib")
2023-08-03 16:16:02 +08:00
#endif
QString url_param;
void ConvertImageToTransparent(QImage &img)
{
img = img.convertToFormat(QImage::Format_ARGB32);
union myrgb
{
uint rgba;
uchar rgba_bits[4];
};
myrgb* mybits = (myrgb*)img.bits();
int len = img.width()*img.height();
while (len--> 0)
{
mybits->rgba_bits[3] = (mybits->rgba == 0xFFFFFFFF) ? 0 : 255;
mybits++;
}
}
2023-08-15 11:17:09 +08:00
NavBar::NavBar(MainScreen *mainScreen,QWidget *parent) :
2023-08-03 16:16:02 +08:00
QWidget(parent), ui(new Ui::NavBar) {
ui->setupUi(this);
2023-08-18 19:09:55 +08:00
this->parent = parent;
2023-08-03 16:16:02 +08:00
//this->setAttribute(Qt::WA_DeleteOnClose);
buffer = new QByteArray;
/*QString style = "background-color:";
style += configResponse->basic.backgroud_color.toUpper();
style += ";";
setStyleSheet(style);*/
//setStyleSheet("margin:0");
setContentsMargins(0, 0, 0, 0);
ConfigResponse config_response;
ConfigRequest config_request;
config_request.sendRequest(&config_response);
SQLiteHelper sqlite_helper;
sqlite_helper.get_software(&buttonStructs, &config_response);
2023-08-18 19:09:55 +08:00
background_color = config_response.basic.backgroud_color;
text_color = config_response.basic.title_color;
text_cover_color = config_response.basic.title_cover_color;
2023-08-13 12:16:22 +08:00
if(!config_response.succeed)
{
2023-08-15 11:17:09 +08:00
qColor.setNamedColor(buttonStructs[0].background_color);
2023-08-13 12:16:22 +08:00
}else
{
2023-08-13 12:16:22 +08:00
qColor.setNamedColor(config_response.basic.backgroud_color);
2023-08-03 16:16:02 +08:00
}
layout2 = new QHBoxLayout;
layout_right = new QHBoxLayout;
layout_left = new QHBoxLayout;
2023-08-17 01:10:27 +08:00
getLogoFromInternet(&config_response);
2023-08-03 16:16:02 +08:00
logo_label = new QLabel(this);
2023-08-13 12:16:22 +08:00
logo_label->setAttribute(Qt::WA_TranslucentBackground);
2023-08-03 16:16:02 +08:00
//*logo = logo->scaled(this->width() / (5 / scale), this->height());
logo_label->setScaledContents(true);
2023-08-13 12:16:22 +08:00
QPixmap pixmap(QApplication::applicationDirPath() + DEFAULT_LOGO);
pixmap.setMask(pixmap.createMaskFromColor(QColor(Qt::transparent)));
logo_label->setAutoFillBackground(true);
QPalette palette = logo_label->palette();
palette.setColor(QPalette::Window, Qt::transparent);
logo_label->setPalette(palette);
logo_label->setPixmap(pixmap);
2023-08-03 16:16:02 +08:00
logo_label->setMaximumHeight(parent->height() / 8);
layout_left->addWidget(logo_label);
layout_left->setAlignment(Qt::AlignCenter);
for (auto buttonStruct : buttonStructs) {
MyButton *myButton = new MyButton(buttonStruct, height(), height(), &buttons,this);
myButton->setMaximumHeight(parent->height() / 8);
layout_right->addWidget(myButton);
//myButton->show();
connect(myButton, &MyButton::clicked1, mainWindowLayout, &MainWindowLayout::clickButton);
buttons << myButton;
}
layout_right->setAlignment(Qt::AlignLeft);
if (!buttonStructs.empty()) {
connect(this, &NavBar::firstUrl, mainScreen, &MainScreen::firstUrl);
emit firstUrl(buttonStructs[0].url);
}
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();
}
NavBar::~NavBar() {
for (auto buttonStruct : buttonStructs) {
delete buttonStruct.image;
delete buttonStruct.image_cover;
}
for(auto button : buttons){
layout_right->removeWidget(button);
delete button;
}
layout2->removeWidget(logo_label);
delete logo_label;
//delete layout_left;
delete layout_right;
delete layout2;
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());
painter.fillRect(rect, qColor);
}
void NavBar::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
for(auto button:buttons){
button->setMaximumSize(event->size().height(), event->size().height());
button->update();
}
logo_label->setMaximumSize(event->size().width() / 5, event->size().height());
}
2023-08-17 01:10:27 +08:00
void NavBar::getLogoFromInternet(ConfigResponse *configResponse) {
2023-08-03 16:16:02 +08:00
QUrl url_logo(configResponse->basic.logo_url);
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);
eventLoop.exec();
timer.stop();
buffer = new QByteArray;
delete request_logo;
2023-08-21 15:10:19 +08:00
if (downloadSuccess == true&& reply->error() == QNetworkReply::NoError) {
2023-08-03 16:16:02 +08:00
*buffer = reply->readAll();
2023-08-17 01:10:27 +08:00
QFile logo(QApplication::applicationDirPath() + DEFAULT_LOGO);
if (logo.open(QIODevice::WriteOnly)) {
logo.write(*buffer);
logo.close();
}
2023-08-03 16:16:02 +08:00
buffer->clear();
2023-08-21 15:10:19 +08:00
reply->close();
2023-08-03 16:16:02 +08:00
}
delete manager;
manager = nullptr;
2023-08-17 01:10:27 +08:00
}
2023-08-03 16:16:02 +08:00
void NavBar::storeToBuffer() {
buffer = new QByteArray;
}
void NavBar::cancelDownload() {
disconnect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
eventLoop.quit();
reply->abort();
downloadSuccess = false;
}
2023-08-18 19:09:55 +08:00
void NavBar::refresh(int state)
{
SQLiteHelper sqlite_helper;
buttonStructs.clear();
sqlite_helper.get_software(&buttonStructs, background_color,text_color,text_cover_color);
for(auto button:buttons)
{
layout_right->removeWidget(button);
delete button;
buttons.removeOne(button);
2023-08-03 16:16:02 +08:00
}
2023-08-18 19:09:55 +08:00
for (auto buttonStruct : buttonStructs) {
MyButton* myButton = new MyButton(buttonStruct, height(), height(), &buttons, this);
myButton->setMaximumHeight(parent->height() / 8);
layout_right->addWidget(myButton);
//myButton->show();
connect(myButton, &MyButton::clicked1, mainWindowLayout, &MainWindowLayout::clickButton);
buttons << myButton;
2023-08-03 16:16:02 +08:00
}
2023-08-18 19:09:55 +08:00
emit application_manager->refresh();//在这里refresh避免因为refresh导致信号和槽断开
}