qt basics

This commit is contained in:
mrbesen 2022-04-20 00:54:10 +02:00
parent 8779d5a800
commit 7d15768295
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
11 changed files with 254 additions and 86 deletions

5
.gitignore vendored
View File

@ -16,3 +16,8 @@ lolautoaccept_strip
log.txt
*.png
ui/*.h
.qmake.stash
*.pro.user
*.qm

View File

@ -1,74 +0,0 @@
# Author Yannis Gerlach
# Hochschule Osnabrück
# 13.11.2020
# `make clean all` nicht mit -j verwenden! -> race condition im make file
# statdessen: `make clean; make all -j` verwenden
NAME = lolautoaccept
NAMETEST = test
CFLAGS = -std=c++17 -O2 -g -pipe -Wall -Wextra -Wno-unused-parameter -Wpedantic -rdynamic -mfpmath=both -march=native -m64 -funroll-loops -mavx2 `pkg-config opencv4 --cflags`
CXX = g++
SRCF = src/
BUILDDIR = build/
TESTF = tests/
DEPF = $(BUILDDIR)deps/
INCF = ./include/
INCFS = $(shell find $(INCF) -type d)
LOGF = ./thirdparty/Log/
LOGO = $(LOGF)Log.o
INCLUDES = -I$(LOGF) $(addprefix -I, $(INCFS))
LDFLAGS = -lX11 -lXext `pkg-config opencv4 --libs` -lXInputSimulator
SRCFILES = $(shell find $(SRCF) -name "*.cpp")
OBJFILES = $(patsubst $(SRCF)%, $(BUILDDIR)%, $(patsubst %.cpp, %.o, $(SRCFILES))) $(LOGO)
DEPFILES = $(wildcard $(DEPF)*.d)
SOURCEDIRS = $(shell find $(SRCF) -type d -printf "%p/\n")
BUILDDIRS = $(patsubst $(SRCF)%, $(BUILDDIR)%, $(SOURCEDIRS))
OBJFILESTEST = $(filter-out $(BUILDDIR)main.o, $(OBJFILES))
INCLUDES += $(addprefix -I, $(SOURCEDIRS))
all: $(NAME) runtest
$(NAME): $(BUILDDIRS) $(DEPF) $(OBJFILES)
@echo "Linking $@"
@$(CXX) $(CFLAGS) -o $@ $(filter %.o, $^) $(LDFLAGS)
$(BUILDDIR)%.o: $(SRCF)%.cpp
@echo "Compiling: $@"
@$(CXX) $(CFLAGS) $(INCLUDES) $< -MM -MT $@ > $(DEPF)$(subst /,_,$*).d
@$(CXX) -c -o $@ $(CFLAGS) $(INCLUDES) $<
$(NAME)_strip: $(NAME)
@echo "Strip $<"
@strip -o $@ $<
%/:
mkdir -p $@
clean-depends:
$(RM) -r $(DEPF)
$(LOGO):
$(MAKE) -C $(LOGF) all
clean:
$(RM) -r $(NAME) $(BUILDDIR) $(NAMETEST) $(NAME)_strip
$(MAKE) -C $(LOGF) $@
$(NAMETEST): $(BUILDDIRS) $(DEPF) $(TESTF)*.cpp $(OBJFILESTEST)
@echo "Compiling tests"
@$(CXX) -o $@ $(filter %.o, $^) $(filter %.cpp, $^) $(CFLAGS) -I$(SRCF) $(INCLUDES) $(LDFLAGS)
runtest: $(NAMETEST)
@echo "Running tests"
./$<
.PHONY: clean all $(NAMETEST) clean-depends runtest
include $(DEPFILES)

View File

@ -8,7 +8,7 @@
class LolAutoAccept {
private:
ScreenShot* screen;
ScreenShot* screen = nullptr;
Matcher acceptmatcher;
Matcher arrowmatcher;
Matcher banmatcher;
@ -21,6 +21,8 @@ private:
std::string ban;
std::string pick;
bool shouldrun = false;
enum class State {
LOBBY,
PREPICK,
@ -43,5 +45,7 @@ public:
void setBan(const std::string& ban);
void setPick(const std::string& pick);
bool init(); // returns true on success
void run();
void stop();
};

21
include/mainwindow.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

64
lolautoaccept.pro Normal file
View File

@ -0,0 +1,64 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
unix:LIBS += -lX11 -lXext `pkg-config opencv4 --libs` -lXInputSimulator
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
src/fakescreen.cpp \
src/lolautoaccept.cpp \
src/main.cpp \
src/mainwindow.cpp \
src/matcher.cpp \
src/scaleableinputs.cpp \
src/screen.cpp \
thirdparty/Log/Log.cpp
# mainwindow.cpp
HEADERS += \
include/fakescreen.h \
include/lolautoaccept.h \
include/mainwindow.h \
include/matcher.h \
include/scaleableinputs.h \
include/screen.h \
thirdparty/Log/Log.h
# mainwindow.h
MOC_DIR = build/generated/
UI_DIR = ui/
OBJECTS_DIR = build/
FORMS += \
ui/mainwindow.ui
TRANSLATIONS += \
ts/de_DE.ts \
ts/en.ts
INCLUDEPATH += $$PWD/include/ \
$$PWD/thirdparty/Log/ \
/usr/include/opencv4/opencv \
/usr/include/opencv4
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# https://wiki.qt.io/Automating_generation_of_qm_files

View File

@ -144,12 +144,18 @@ void LolAutoAccept::setPick(const std::string& pick) {
this->pick = pick;
}
void LolAutoAccept::run() {
bool LolAutoAccept::init() {
if(screen)
return true;
// get window
auto wins = ScreenShot::getWindows("League of Legends");
if(wins.size() != 1) {
Log::fatal << "invalid count of windows found: " << wins.size();
return;
for(auto w : wins) {
delete w;
}
return false;
}
screen = (wins.at(0)); // just take the first
@ -159,11 +165,15 @@ void LolAutoAccept::run() {
// testing: whole screen:
// screen = new ScreenShot();
return true;
}
void LolAutoAccept::run() {
lastacceptmatch = {false};
shouldrun = true;
while(true) {
while(shouldrun) {
auto start = std::chrono::high_resolution_clock::now();
switch(state) {
case State::LOBBY:
@ -171,7 +181,7 @@ void LolAutoAccept::run() {
break;
case State::PREPICK:
checkForBan();
default: break;
default: break;
}
auto end = std::chrono::high_resolution_clock::now();
@ -182,4 +192,8 @@ void LolAutoAccept::run() {
}
delete screen;
}
}
void LolAutoAccept::stop() {
shouldrun = false;
}

View File

@ -1,8 +1,15 @@
#include "lolautoaccept.h"
#include <thread>
#include <QApplication>
#include <QMessageBox>
#include <QTranslator>
#include <Log.h>
int main(int argc, const char** argv) {
#include "mainwindow.h"
#include "lolautoaccept.h"
int main(int argc, char** argv) {
Log::init();
Log::setConsoleLogLevel(Log::Level::TRACE);
Log::addLogfile("log.txt", Log::Level::TRACE);
@ -11,14 +18,45 @@ int main(int argc, const char** argv) {
#endif
Log::info << "Hello, World!";
Log::note << "Using Locale: " << QLocale().name().toStdString();
LolAutoAccept lolaa;
QApplication app(argc, argv);
QTranslator translator;
if(translator.load(QLocale().name(), QLatin1String("ts"))) {
app.installTranslator(&translator);
} else {
Log::warn << "translation not found";
}
MainWindow win;
lolaa.setPrePick("morgana");
lolaa.setBan("swain");
lolaa.setBan("akshan");
lolaa.run();
while(!lolaa.init()) {
QMessageBox warn;
warn.setWindowTitle(MainWindow::tr("LoL-Auto-Accept"));
warn.setText(QMessageBox::tr("League of Legends Client not found!"));
warn.setInformativeText(QMessageBox::tr("Open the client and retry or close."));
warn.setStandardButtons(QMessageBox::Retry | QMessageBox::Close);
warn.setDefaultButton(QMessageBox::Retry);
warn.setIcon(QMessageBox::Warning);
if(warn.exec() == QMessageBox::Close) {
Log::stop();
return 0;
}
}
std::thread lolaathread(&LolAutoAccept::run, &lolaa);
win.show();
int ret = app.exec();
lolaa.stop();
if(lolaathread.joinable()) {
lolaathread.join();
}
Log::stop();
return 0;
return ret;
}

11
src/mainwindow.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
}
MainWindow::~MainWindow() {
delete ui;
}

29
ts/de_DE.ts Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE">
<context>
<name>MainWindow</name>
<message>
<location filename="../ui/mainwindow.ui" line="14"/>
<source>LoL-Auto-Accept</source>
<translation>LoL-Auto-Accept</translation>
</message>
</context>
<context>
<name>QMessageBox</name>
<message>
<source>Client not found</source>
<translation type="vanished">Leage of Legends Client wurde nicht gefunden!</translation>
</message>
<message>
<location filename="../src/main.cpp" line="38"/>
<source>League of Legends Client not found!</source>
<translation type="unfinished">League of Legends Client nicht gefunden!</translation>
</message>
<message>
<location filename="../src/main.cpp" line="39"/>
<source>Open the client and retry or close.</source>
<translation>Starte den Client und wiederhole oder Fenster schließen.</translation>
</message>
</context>
</TS>

25
ts/en.ts Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
<name>MainWindow</name>
<message>
<location filename="../ui/mainwindow.ui" line="14"/>
<source>LoL-Auto-Accept</source>
<translation>LoL-Auto-Accept</translation>
</message>
</context>
<context>
<name>QMessageBox</name>
<message>
<location filename="../src/main.cpp" line="38"/>
<source>League of Legends Client not found!</source>
<translation>League of Legends Client not found!</translation>
</message>
<message>
<location filename="../src/main.cpp" line="39"/>
<source>Open the client and retry or close.</source>
<translation>Open the client and retry or close.</translation>
</message>
</context>
</TS>

31
ui/mainwindow.ui Normal file
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>LoL-Auto-Accept</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>