#include "stdafx.h" #include "WB_QR_full_MSVCQtbased.h" #include <QSettings> #include "shellapi.h" #include "WB_QR.h" #include "OK_Dialog.h" #include "JSON_converters.h" #include <chrono> #include <QtConcurrent/QtConcurrent> #include <ShlObj.h> WB_QR_full_MSVCQtbased::WB_QR_full_MSVCQtbased(QWidget *parent) : QWidget(parent) { ui.setupUi(this); QSettings settings; //Load data and output settings at launch settings.beginGroup("data"); ui.supplier_id_Edit->setText(settings.value("supplier_id").toString()); ui.login_Edit->setText(settings.value("login").toString()); ui.password_Edit->setText(settings.value("password").toString()); ui.QTY_Edit->setText(settings.value("QTY").toString()); settings.endGroup(); settings.beginGroup("output"); if (settings.contains("generate_pdf")) { ui.generate_pdf_check->setChecked(settings.value("generate_pdf").toBool()); } if (settings.contains("generate_xlsx")) { ui.generate_xlsx_check->setChecked(settings.value("generate_xlsx").toBool()); } settings.endGroup(); converters_window->setParent(this); connect(converters_window, &JSON_converters::bar_progress_Signal, ui.progressBar, &QProgressBar::setValue); connect(converters_window, &JSON_converters::bar_maximum_Signal, ui.progressBar, &QProgressBar::setMaximum); converters_window_isshown = false; converters_window->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); } void WB_QR_full_MSVCQtbased::on_create_codes_Button_clicked() { ui.create_codes_Button->setEnabled(false); std::string supplier_id = ui.supplier_id_Edit->text().toStdString(); std::string login = ui.login_Edit->text().toStdString(); std::string password = ui.password_Edit->text().toStdString(); bool qty_ok; int QTY = ui.QTY_Edit->text().toInt(&qty_ok); if (!qty_ok) { OK_Dialog dialog(this); dialog.Dialog_Text->setText("Количество должно быть целым числом"); dialog.exec(); } else { ui.progressBar->setValue(0); ui.progressBar->setMaximum(QTY); int result = 0; long previous_request_time = 0; while ((QTY >= ONE_REQUEST_LIMIT) && (result == 0)) { long new_request_time = std::chrono::time_point_cast<std::chrono::seconds>( std::chrono::current_zone()->to_local( std::chrono::system_clock::now())).time_since_epoch().count(); if ((new_request_time - previous_request_time) < ((60.0 * ONE_REQUEST_LIMIT) / API_MINUTE_LIMIT + 1)) { QTime dieTime = QTime::currentTime().addSecs(((60 * ONE_REQUEST_LIMIT) / API_MINUTE_LIMIT) + ((60 * ONE_REQUEST_LIMIT) % API_MINUTE_LIMIT != 0) + 1 - new_request_time + previous_request_time); while (QTime::currentTime() < dieTime) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); new_request_time += ((60 * ONE_REQUEST_LIMIT) / API_MINUTE_LIMIT) + ((60 * ONE_REQUEST_LIMIT) % API_MINUTE_LIMIT != 0) + 1 - new_request_time + previous_request_time; } QFuture<int> future = QtConcurrent::run(QR_generator, supplier_id, login, password, ONE_REQUEST_LIMIT, ui.generate_pdf_check->isChecked(), ui.generate_json_check->isChecked()); previous_request_time = new_request_time; result = future.result(); QTY -= ONE_REQUEST_LIMIT; if (result == 0) ui.progressBar->setValue(ui.progressBar->maximum() - QTY); } if ((QTY > 0) && (result == 0)) { long new_request_time = std::chrono::time_point_cast<std::chrono::seconds>( std::chrono::current_zone()->to_local( std::chrono::system_clock::now())).time_since_epoch().count(); if ((new_request_time - previous_request_time) < (60 * ONE_REQUEST_LIMIT / API_MINUTE_LIMIT + 2)) { QTime dieTime = QTime::currentTime().addSecs(60 * ONE_REQUEST_LIMIT / API_MINUTE_LIMIT + 2 - new_request_time + previous_request_time); while (QTime::currentTime() < dieTime) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } QFuture<int> future = QtConcurrent::run(QR_generator, supplier_id, login, password, QTY, ui.generate_pdf_check->isChecked(), ui.generate_json_check->isChecked()); result = future.result(); if (result == 0) ui.progressBar->setValue(ui.progressBar->maximum()); } OK_Dialog dialog(this); dialog.Dialog_Text->setText(QString::fromStdString(error_message(result))); dialog.exec(); } ui.create_codes_Button->setEnabled(true); } void WB_QR_full_MSVCQtbased::closeEvent(QCloseEvent* event) //Overrides closeEvent to save input data and output settings when application closes and close the side window { QSettings settings; settings.beginGroup("data"); settings.setValue("supplier_id", ui.supplier_id_Edit->text()); settings.setValue("login", ui.login_Edit->text()); settings.setValue("password", ui.password_Edit->text()); settings.setValue("QTY", ui.QTY_Edit->text()); settings.endGroup(); settings.beginGroup("output"); settings.setValue("generate_pdf", ui.generate_pdf_check->isChecked()); settings.setValue("generate_xlsx", ui.generate_xlsx_check->isChecked()); settings.endGroup(); } void WB_QR_full_MSVCQtbased::showEvent(QShowEvent* event) { converters_window->move(this->pos().x() + 500, ui.show_JSON_converters_Button->mapToGlobal(QPointF(0, 0)).y() - 21); //Overriding show event to set side window position } void WB_QR_full_MSVCQtbased::moveEvent(QMoveEvent* event) { converters_window->move(event->pos().x() + 500, ui.show_JSON_converters_Button->mapToGlobal(QPoint(0, 0)).y() - 21); } void WB_QR_full_MSVCQtbased::on_json_folder_Button_clicked() { PWSTR path; SHGetKnownFolderPath(FOLDERID_PublicDocuments, KF_FLAG_DEFAULT, NULL, &path); std::wstring strpath(path); ShellExecuteW(NULL, L"open", (strpath + L"\\WB_QR\\JSON").c_str(), NULL, NULL, SW_SHOWDEFAULT); CoTaskMemFree(path); } void WB_QR_full_MSVCQtbased::on_pdf_folder_Button_clicked() { PWSTR path; SHGetKnownFolderPath(FOLDERID_PublicDocuments, KF_FLAG_DEFAULT, NULL, &path); std::wstring strpath(path); ShellExecuteW(NULL, L"open", (strpath + L"\\WB_QR\\PDF").c_str(), NULL, NULL, SW_SHOWDEFAULT); CoTaskMemFree(path); } void WB_QR_full_MSVCQtbased::on_xlsx_folder_Button_clicked() { PWSTR path; SHGetKnownFolderPath(FOLDERID_PublicDocuments, KF_FLAG_DEFAULT, NULL, &path); std::wstring strpath(path); ShellExecuteW(NULL, L"open", (strpath + L"\\WB_QR\\XLSX").c_str(), NULL, NULL, SW_SHOWDEFAULT); CoTaskMemFree(path); } void WB_QR_full_MSVCQtbased::on_show_JSON_converters_Button_clicked() { if (converters_window_isshown) { converters_window->hide(); converters_window_isshown = false; } else { converters_window->show(); converters_window_isshown = true; } } WB_QR_full_MSVCQtbased::~WB_QR_full_MSVCQtbased() { }