diff --git a/include/lolautoaccept.h b/include/lolautoaccept.h index 2613328..4a4dcb6 100644 --- a/include/lolautoaccept.h +++ b/include/lolautoaccept.h @@ -4,6 +4,8 @@ #include #include +#include + #include "blitzapi.h" #include "clientapi.h" #include "config.h" @@ -11,11 +13,11 @@ #include "runepage.h" #include "runestyle.h" -class LolAutoAccept { +class LolAutoAccept : public QObject { + Q_OBJECT + public: - using onposchange_func = std::function; using onruneschange_func = std::function; - using onfailed_func = std::function; protected: struct Stage { Stage(); @@ -34,9 +36,7 @@ protected: Config::RootConfig& config; DataDragon& dd; - onposchange_func onPoschange; onruneschange_func onRuneschange; - onfailed_func onFailed; bool shouldrun = false; std::thread lolaathread; @@ -63,8 +63,15 @@ public: PICK = 2, }; - LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onfailed_func = {}, onposchange_func = {}, onruneschange_func = {}); - ~LolAutoAccept(); + enum class Status { + Off, + Running, + Failed + }; + Q_ENUM(Status) + + LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onruneschange_func = {}, QObject* parent = nullptr); + virtual ~LolAutoAccept(); void setChamps(const std::vector& champs, State s); void setEnabled(bool b, State s); @@ -73,6 +80,7 @@ public: bool init(); // returns true on success void run(); void stop(); + Status getStatus(); void reload(); // reload the config, when something was changed @@ -82,6 +90,10 @@ public: void setOnRuneChangeFunc(onruneschange_func on); void setAutoWriteText(bool enabled, const std::string& text = {}); +signals: + void statusChanged(LolAutoAccept::Status); // new status: 0 = off, 1 = on, 2 = failed + void positionChanged(Position); + private: void stopJoinThread(); void innerRun(); @@ -109,3 +121,5 @@ private: const std::string& getChatid(); void applyRunes_internal(uint32_t champid, Position pos); }; + +Q_DECLARE_METATYPE(LolAutoAccept::Status) diff --git a/include/mainwindow.h b/include/mainwindow.h index da5b081..d712a50 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -24,7 +24,6 @@ public: protected: virtual void closeEvent(QCloseEvent* event) override; - virtual void resizeEvent(QResizeEvent *event) override; public slots: void resetSaveTimer(); @@ -48,7 +47,7 @@ signals: private: // returns empty string on no match void onPosChange(Position newpos); // to trigger the signal from a QObject - void onFail(); // get triggerd, when the autoacceptor fails (lost connection) + void lolaaStatusChanged(LolAutoAccept::Status); // get triggerd, when the autoacceptor fails (lost connection) bool loading; Ui::MainWindow *ui; diff --git a/src/lolautoaccept.cpp b/src/lolautoaccept.cpp index 804a60c..97630f9 100644 --- a/src/lolautoaccept.cpp +++ b/src/lolautoaccept.cpp @@ -9,7 +9,9 @@ LolAutoAccept::Stage::Stage() {} LolAutoAccept::Stage::~Stage() {} -LolAutoAccept::LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onfailed_func fail, onposchange_func onposch, onruneschange_func onrunch) : config(config), dd(dd), onPoschange(onposch), onRuneschange(onrunch), onFailed(fail) { +LolAutoAccept::LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onruneschange_func onrunch, QObject* parent) : QObject(parent), config(config), dd(dd), onRuneschange(onrunch) { + qRegisterMetaType(); + std::lock_guard lock(stagesMutex); stages.resize(3); // accept, ban, pick } @@ -73,6 +75,10 @@ void LolAutoAccept::stop() { shouldrun = false; } +LolAutoAccept::Status LolAutoAccept::getStatus() { + return shouldrun ? Status::Running : Status::Off; +} + void LolAutoAccept::reload() { Log::note << "reload LolAutoAccept"; if(currentPositionSet) @@ -134,6 +140,7 @@ void LolAutoAccept::innerRun() { auto convs = clientapi->getAllConversations(); Log::info << "got " << convs.size() << " conversations"; + emit statusChanged(Status::Running); while(shouldrun) { uint32_t extrasleep = 800; auto start = std::chrono::high_resolution_clock::now(); @@ -193,6 +200,7 @@ void LolAutoAccept::innerRun() { std::this_thread::sleep_for(std::chrono::milliseconds(400 + extrasleep)); } + emit statusChanged(Status::Off); } catch(RestClient::WebException& e) { Log::error << "WebException catched: " << e.curlresponse; if(e.curlresponse == CURLE_COULDNT_CONNECT) { @@ -202,11 +210,9 @@ void LolAutoAccept::innerRun() { // disable this thread shouldrun = false; - // notify the ui - if(onFailed) { - onFailed(); - } } + // notify the ui + emit statusChanged(Status::Failed); } } @@ -428,9 +434,8 @@ void LolAutoAccept::champSelect() { if(pos != currentPosition || !currentPositionSet) { Log::note << "LolAutoAccept reloading config for position: " << pos << " because it was: " << currentPosition; loadPosition(pos); - if(onPoschange) { - onPoschange(pos); - } + + emit positionChanged(pos); } Log::debug << "cellid: " << cellid << " position: " << pos << " counter: " << session.counter << " timer: timeleftip: " << session.timer.adjustedTimeLeftInPhase << " totaltimephase: " << session.timer.totalTimeInPhase; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0926372..82b1085 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -6,8 +6,12 @@ #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), loading(true), ui(new Ui::MainWindow), saveTimer(new QTimer(this)), dd(QLocale().name().toStdString()), - lolaa(conf.getConfig(), dd, std::bind(&MainWindow::onFail, this), std::bind(&MainWindow::onPosChange, this, std::placeholders::_1)) { + lolaa(conf.getConfig(), dd) { ui->setupUi(this); + this->ui->runesPage->setLolAA(&lolaa); + + QObject::connect(&lolaa, &LolAutoAccept::statusChanged, this, &MainWindow::lolaaStatusChanged); + QObject::connect(&lolaa, &LolAutoAccept::positionChanged, this, &MainWindow::onPosChange); saveTimer->setInterval(std::chrono::minutes(1)); saveTimer->setSingleShot(true); @@ -53,19 +57,14 @@ MainWindow::~MainWindow() { lolaa.stop(); conf.save(); - delete ui; + delete this->ui; } void MainWindow::closeEvent([[maybe_unused]] QCloseEvent* event) { + lolaa.stop(); conf.save(); } -void MainWindow::resizeEvent([[maybe_unused]] QResizeEvent *event) { - ui->verticalLayoutWidget->setMinimumSize(ui->centralwidget->size()); - ui->verticalLayoutWidget->setMaximumSize(ui->centralwidget->size()); - ui->verticalLayoutWidget->setMinimumSize(ui->centralwidget->size()); -} - void MainWindow::resetSaveTimer() { saveTimer->start(); qDebug() << "resetTimer"; @@ -75,10 +74,18 @@ void MainWindow::toggleMainswitch(bool state) { qDebug() << "mainswitch toggled: " << state; if(state) { + ui->mainswitch->setCheckState(Qt::CheckState::PartiallyChecked); + ui->mainswitch->setEnabled(false); + + // make sure the changes to the mainswitch are rendered, before going into the blocking call + QCoreApplication::processEvents(); + + // TODO: make this non blocking if(!lolaa.init()) { Log::error << "League Client not found!"; ui->statusbar->showMessage(tr("League of Legends Client not found!")); ui->mainswitch->setCheckState(Qt::CheckState::Unchecked); + ui->mainswitch->setEnabled(true); return; } @@ -86,12 +93,10 @@ void MainWindow::toggleMainswitch(bool state) { ui->runedisplay->setStyles(lolaa.getRuneStyles()); lolaa.run(); - - ui->statusbar->showMessage(tr("Auto-Acceptor started!")); - } else { lolaa.stop(); - ui->statusbar->showMessage(tr("Auto-Acceptor stoped!")); + ui->mainswitch->setCheckState(Qt::CheckState::PartiallyChecked); + ui->mainswitch->setEnabled(false); } } @@ -171,8 +176,21 @@ void MainWindow::onPosChange(Position newpos) { emit requestTabChange((int) newpos); } -void MainWindow::onFail() { - ui->mainswitch->setChecked(false); - ui->statusbar->showMessage(tr("Auto-Acceptor failed!")); -} +void MainWindow::lolaaStatusChanged(LolAutoAccept::Status status) { + qDebug() << "new status: " << (int) status; + switch(status) { + case LolAutoAccept::Status::Off: + this->ui->statusbar->showMessage(tr("Auto-Acceptor stoped!")); + break; + case LolAutoAccept::Status::Running: + this->ui->statusbar->showMessage(tr("Auto-Acceptor started!")); + break; + case LolAutoAccept::Status::Failed: + this->ui->statusbar->showMessage(tr("Auto-Acceptor failed!")); + break; + } + + this->ui->mainswitch->setEnabled(true); + this->ui->mainswitch->setChecked(status == LolAutoAccept::Status::Running); +}