lolautoaccept/src/mainwindow.cpp

188 lines
5.1 KiB
C++
Raw Normal View History

2022-04-20 00:54:10 +02:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
2022-12-16 23:09:24 +01:00
#include <QTimer>
2022-04-21 00:47:57 +02:00
#include <Log.h>
2023-05-31 22:22:23 +02:00
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), loading(true), ui(new Ui::MainWindow), saveTimer(new QTimer(this)), dd(QLocale().name()),
2023-02-26 13:50:57 +01:00
lolaa(conf.getConfig(), dd) {
2022-04-20 00:54:10 +02:00
ui->setupUi(this);
2023-02-26 13:50:57 +01:00
QObject::connect(&lolaa, &LolAutoAccept::statusChanged, this, &MainWindow::lolaaStatusChanged);
QObject::connect(&lolaa, &LolAutoAccept::positionChanged, this, &MainWindow::onPosChange);
2022-04-23 00:39:45 +02:00
2022-12-16 23:09:24 +01:00
saveTimer->setInterval(std::chrono::minutes(1));
saveTimer->setSingleShot(true);
QObject::connect(saveTimer, &QTimer::timeout, this, &MainWindow::saveConfig);
2022-10-10 21:38:35 +02:00
ui->copyrightlabel->setText(ui->copyrightlabel->text().arg(LOLAA_VERSION));
2022-04-24 01:27:46 +02:00
conf.load();
2022-07-04 00:30:00 +02:00
// for all tabs - set their config and datadragon
Config::RootConfig& rc = conf.getConfig();
for(int32_t tabnr = 0; tabnr < ui->tabWidget->count(); ++tabnr) {
SettingsTab* tab = (SettingsTab*) ui->tabWidget->widget(tabnr);
tab->setup(*rc.getPositionConfig(tab->getPosition()), &dd);
2022-07-04 00:30:00 +02:00
2022-12-16 23:09:24 +01:00
QObject::connect(tab, &SettingsTab::changed, this, &MainWindow::tabchanged);
QObject::connect(tab, &SettingsTab::toggled, this, &MainWindow::tabtoggled);
2022-07-04 00:30:00 +02:00
}
2022-04-24 01:27:46 +02:00
2023-05-01 01:23:08 +02:00
ui->runesPage->setDataDragon(dd);
2023-04-23 23:54:09 +02:00
ui->runesPage->setConfig(conf);
2022-04-24 01:27:46 +02:00
ui->enableAll->setChecked(rc.enabledAutoAccept);
2022-04-24 14:06:27 +02:00
lolaa.setEnabled(rc.enabledAutoAccept, LolAutoAccept::State::LOBBY);
2022-07-03 16:44:07 +02:00
2022-08-26 13:47:21 +02:00
ui->enableSmiteWarning->setChecked(rc.enabledSmiteWarn);
lolaa.setSmiteWarn(rc.enabledSmiteWarn);
2022-09-21 13:47:08 +02:00
ui->enableAutoWrite->setChecked(rc.enabledAutoWrite);
2023-04-23 23:54:09 +02:00
ui->autoWriteText->setText(rc.autoWriteText);
2023-05-31 22:22:23 +02:00
lolaa.setAutoWriteText(rc.enabledAutoWrite, rc.autoWriteText);
2022-09-21 13:47:08 +02:00
2022-07-03 16:44:07 +02:00
resizeEvent(nullptr);
2022-12-16 23:09:24 +01:00
// a timer to delay the loading flag a short time until all other signals are processed
initDoneTimer = new QTimer(this);
initDoneTimer->setSingleShot(true);
initDoneTimer->setInterval(std::chrono::milliseconds(1)); // schedule for first event loop
QObject::connect(initDoneTimer, &QTimer::timeout, this, &MainWindow::initDone, Qt::QueuedConnection);
initDoneTimer->start();
2022-04-20 00:54:10 +02:00
}
MainWindow::~MainWindow() {
2022-04-21 00:47:57 +02:00
lolaa.stop();
2022-04-24 01:27:46 +02:00
conf.save();
2022-04-21 00:47:57 +02:00
2023-02-26 13:50:57 +01:00
delete this->ui;
2022-04-20 00:54:10 +02:00
}
2022-04-24 12:15:58 +02:00
void MainWindow::closeEvent([[maybe_unused]] QCloseEvent* event) {
2023-02-26 13:50:57 +01:00
lolaa.stop();
2022-04-24 01:27:46 +02:00
conf.save();
}
2022-12-16 23:09:24 +01:00
void MainWindow::resetSaveTimer() {
saveTimer->start();
qDebug() << "resetTimer";
}
2022-04-21 19:33:46 +02:00
void MainWindow::toggleMainswitch(bool state) {
2022-12-16 23:09:24 +01:00
qDebug() << "mainswitch toggled: " << state;
2022-07-03 16:44:07 +02:00
2022-04-21 19:33:46 +02:00
if(state) {
2023-02-26 13:50:57 +01:00
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
2022-04-21 19:33:46 +02:00
if(!lolaa.init()) {
2023-05-31 22:22:23 +02:00
qCritical() << "League Client not found!";
2022-04-21 19:33:46 +02:00
ui->statusbar->showMessage(tr("League of Legends Client not found!"));
ui->mainswitch->setCheckState(Qt::CheckState::Unchecked);
2023-02-26 13:50:57 +01:00
ui->mainswitch->setEnabled(true);
2022-04-21 19:33:46 +02:00
return;
}
lolaa.run();
} else {
lolaa.stop();
2023-02-26 13:50:57 +01:00
ui->mainswitch->setCheckState(Qt::CheckState::PartiallyChecked);
ui->mainswitch->setEnabled(false);
2022-04-21 19:33:46 +02:00
}
}
void MainWindow::aatoggled(bool state) {
2022-12-16 23:09:24 +01:00
if( loading ) return;
qDebug() << "enableAll checkbox toggled " << state;
2022-04-21 00:47:57 +02:00
2022-04-24 14:06:27 +02:00
lolaa.setEnabled(state, LolAutoAccept::State::LOBBY);
2022-04-24 01:27:46 +02:00
conf.getConfig().enabledAutoAccept = state;
2022-12-16 23:09:24 +01:00
resetSaveTimer();
2022-04-21 00:47:57 +02:00
}
2022-08-26 13:47:21 +02:00
void MainWindow::smitewarntoggled(bool state) {
2022-12-16 23:09:24 +01:00
if( loading ) return;
qDebug() << "smitewarn checkbox toggled " << state;
2022-08-26 13:47:21 +02:00
lolaa.setSmiteWarn(state);
conf.getConfig().enabledSmiteWarn = state;
2022-12-16 23:09:24 +01:00
resetSaveTimer();
2022-08-26 13:47:21 +02:00
}
2022-07-04 00:30:00 +02:00
void MainWindow::tabtoggled(Position p, LolAutoAccept::State s, bool state) {
2022-12-16 23:09:24 +01:00
if( loading ) return;
qDebug() << "checkbox toggled " << state << " position: " << p << " state: " << (int) s;
2022-04-21 18:35:18 +02:00
2022-10-02 23:46:11 +02:00
lolaa.setEnabled(state, s);
2022-07-04 22:59:48 +02:00
lolaa.reload();
2022-12-16 23:09:24 +01:00
resetSaveTimer();
2022-04-21 18:35:18 +02:00
}
2022-07-04 00:30:00 +02:00
void MainWindow::tabchanged(Position p, LolAutoAccept::State s) {
2022-12-16 23:09:24 +01:00
if( loading ) return;
qDebug() << "edited position: " << p << " state: " << (int) s;
2022-04-21 18:35:18 +02:00
2022-07-04 22:59:48 +02:00
lolaa.reload();
2022-12-16 23:09:24 +01:00
resetSaveTimer();
2022-07-04 22:59:48 +02:00
}
2022-09-21 13:47:08 +02:00
void MainWindow::autoWriteChanged() {
2022-12-16 23:09:24 +01:00
if( loading ) return;
2022-09-21 13:47:08 +02:00
bool enabled = ui->enableAutoWrite->isChecked();
2023-04-23 23:54:09 +02:00
const QString text = ui->autoWriteText->toPlainText();
2022-12-16 23:09:24 +01:00
2023-05-31 22:22:23 +02:00
lolaa.setAutoWriteText(enabled, text);
2022-12-16 23:09:24 +01:00
2022-09-21 13:47:08 +02:00
conf.getConfig().enabledAutoWrite = enabled;
conf.getConfig().autoWriteText = text;
2022-12-16 23:09:24 +01:00
resetSaveTimer();
}
void MainWindow::saveConfig() {
conf.save();
}
void MainWindow::initDone() {
loading = false;
initDoneTimer->deleteLater();
initDoneTimer = nullptr;
qDebug() << "loading done";
2022-09-21 13:47:08 +02:00
}
2022-07-04 22:59:48 +02:00
void MainWindow::onPosChange(Position newpos) {
2022-07-17 00:21:15 +02:00
assert(newpos <= Position::UTILITY);
emit requestTabChange((int) newpos);
2022-04-21 18:35:18 +02:00
}
2022-07-09 01:01:51 +02:00
2023-02-26 13:50:57 +01:00
void MainWindow::lolaaStatusChanged(LolAutoAccept::Status status) {
qDebug() << "new status: " << (int) status;
2022-07-29 00:05:22 +02:00
2023-02-26 13:50:57 +01:00
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);
}