Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Cleanup and Quality Improvements #6

Merged
merged 7 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(THIRD_PARTY_LIB_SOURCES
add_library(QR_Code_Generator SHARED ${THIRD_PARTY_LIB_SOURCES})

set(PROJECT_SOURCES
res/icons.qrc
src/main.cpp
src/mainwindow.cpp
src/mainwindow.hpp
Expand All @@ -37,7 +38,6 @@ set(PROJECT_SOURCES
src/networking/server.ui
src/networking/executor.cpp
src/networking/executor.hpp
res/icons.qrc
src/preferences.h
src/preferences.cpp
src/preferences.ui
Expand Down
5 changes: 3 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
load_key_maps();
this->p = new Preferences(this);
ui->setupUi(this);
ui->settingsButton->connect(ui->settingsButton, &QPushButton::pressed, this, [=] { this->p->show(); });
QPushButton::connect(ui->settingsButton, &QPushButton::pressed, this, [=] { this->p->show(); });
QPushButton::connect(ui->startButton, &QPushButton::pressed, this, &MainWindow::launch_server);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_startButton_clicked()
void MainWindow::launch_server()
{
Server server(this);
server.exec();
Expand Down
8 changes: 4 additions & 4 deletions src/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
class MainWindow final : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;

private slots:
void on_startButton_clicked();
void launch_server();

private:
Ui::MainWindow *ui;
Expand Down
8 changes: 4 additions & 4 deletions src/networking/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ bool inject_gamepad_state(vgp_data_exchange_gamepad_reading reading)

// Use the right thumbstick to move the mouse
// if (abs(reading.right_thumbstick_x) > THRESHOLD || abs(reading.right_thumbstick_y) > THRESHOLD)
int offsetX = reading.right_thumbstick_x * mouse_sensivity;
int offsetY = reading.right_thumbstick_y * mouse_sensivity;
int scaleX = abs(offsetX) < (THRESHOLD * mouse_sensivity) ? 0 : 1;
int scaleY = abs(offsetY) < (THRESHOLD * mouse_sensivity) ? 0 : 1;
int offsetX = reading.right_thumbstick_x * mouse_sensitivity;
int offsetY = reading.right_thumbstick_y * mouse_sensitivity;
int scaleX = abs(offsetX) < (THRESHOLD * mouse_sensitivity) ? 0 : 1;
int scaleY = abs(offsetY) < (THRESHOLD * mouse_sensitivity) ? 0 : 1;

qDebug() << "Moving mouse by" << offsetX << ", " << offsetY;
for (int count = 1; count <= std::max(abs(offsetX), abs(offsetY)); ++count)
Expand Down
1 change: 0 additions & 1 deletion src/networking/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "ui_server.h"

#include "../../VGP_Data_Exchange/C/Colfer.h"
#include "../../third-party-libs/QR-Code-generator/cpp/qrcodegen.hpp"
#include "../settings_key_variables.h"

Expand Down
2 changes: 1 addition & 1 deletion src/networking/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Server : public QDialog

public:
explicit Server(QWidget *parent = nullptr);
~Server();
~Server() override;
QTcpServer *tcpServer = nullptr;

private slots:
Expand Down
35 changes: 12 additions & 23 deletions src/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
#include "settings_key_variables.h"
#include "ui_preferences.h"
#include "winuser.h"
#include <QDebug>
#include <QKeyEvent>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>
#include <QMouseEvent>
#include <QPixmap>
#include <QProcess>
#include <QSlider>

Expand All @@ -21,32 +17,30 @@ Preferences::Preferences(QWidget *parent) : QDialog(parent), ui(new Ui::Preferen
ui->buttonBox->connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
[=] { // running the functions to change and save the new settings if the user presses ok
this->change_mouse_sensitivity(ui->horizontalSlider->value() * 100);
qDebug() << mouse_sensivity;
save_setting(setting_keys::Mouse_sensivity,
mouse_sensivity / 100); // saving the new mouse sensivity
change_key_inputs(); // changing and saving key maps
qDebug() << mouse_sensitivity;
save_setting(setting_keys::Mouse_sensitivity,
mouse_sensitivity / 100); // saving the new mouse sensitivity
change_key_inputs(); // changing and saving key maps
});
ui->buttonBox->connect(ui->buttonBox, &QDialogButtonBox::rejected, this, [=] { load_keys(); });
ui->formLayout->setSizeConstraint(QLayout::SetMinimumSize);
ui->formLayout->setHorizontalSpacing(50);
ui->formLayout->setVerticalSpacing(10);
ui->horizontalSlider->setValue(mouse_sensivity / 100);
ui->horizontalSlider->setValue(mouse_sensitivity / 100);
Preferences::load_keys();
}

/**
* @brief Preferences::change_mouse_sensitivity
* Changes the mouse sensivity or the cursor speed
* Changes the mouse sensitivity or the cursor speed
* @param value
* The amount of mouse sensivity you want to set.
* The amount of mouse sensitivity you want to set.
*/
void Preferences::change_mouse_sensitivity(int value)
{
mouse_sensivity = value;
mouse_sensitivity = value;
}

/**
* @brief Preferences::change_key_inputs
* This changes the keyboard maps and saves those changes in the config file.
* This function is executed if the user presses ok button.
*/
Expand Down Expand Up @@ -116,8 +110,7 @@ void Preferences::change_key_inputs()
}

/**
* @brief Preferences::get_scan_code
* Copies the name of the of corrosponding key or virutal key code to the provided buffer.
* Copies the name of the of corresponding key or virtual key code to the provided buffer.
* @param vk
* The virtual key code of the key you want to get.
* @param a
Expand All @@ -140,7 +133,6 @@ void Preferences::get_scan_code(WORD vk, char *a, int size)
}

/**
* @brief Preferences::load_keys
* Displays the key to which each button is mapped to.
* Saves the initial key maps in variables that can be changed later if user wants to.
*/
Expand Down Expand Up @@ -197,11 +189,10 @@ void Preferences::load_keys()
}

/**
* @brief Preferences::eventFilter
* The event filter virtual function is redefined to to filter for mouse and keyboard inputs when user tries to change
* the button-key maps. Checks which object is sending the event and type of event. If event is a keyboard or
* mousebutton press than map and the object is buttonmap than get the virtual key code of the key pressed and store the
* change in a temporary variable.
* mouse button press then map and the object is button map then get the virtual key code of the key pressed
* and store the change in a temporary variable.
* @param sender
* To get the address of the object that is triggering the event.
* @param event
Expand Down Expand Up @@ -245,7 +236,7 @@ bool Preferences::eventFilter(QObject *sender, QEvent *event)
valid = true;
break;
default:
qDebug() << "[!] Error Occured No Legal Mouse Button Found";
qDebug() << "[!] Error Occurred. No Legal Mouse Button Found";
}
if (valid)
ptr->setText(QString(buffer));
Expand All @@ -261,7 +252,6 @@ bool Preferences::eventFilter(QObject *sender, QEvent *event)
}

/**
* @brief Preferences::keyPressEvent
* Make the Qdialog box ignores the enter key and escape key presses when the focus is on button map
* @param e
* Capture the key_press event
Expand All @@ -274,7 +264,6 @@ void Preferences::keyPressEvent(QKeyEvent *e)
}

/**
* @brief Preferences::install_event_filter
* Install the above event filter on all the button maps to capture the key presses when they have the focus.
*/
void Preferences::install_event_filter()
Expand Down
8 changes: 4 additions & 4 deletions src/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ namespace Ui
class Preferences;
}

class Preferences : public QDialog
class Preferences final : public QDialog
{
Q_OBJECT

public:
explicit Preferences(QWidget *parent = nullptr);
void load_keys();
~Preferences();
~Preferences() override;

protected:
bool eventFilter(QObject *sender, QEvent *event);
void keyPressEvent(QKeyEvent *e);
bool eventFilter(QObject *sender, QEvent *event) override;
void keyPressEvent(QKeyEvent *e) override;

private:
Ui::Preferences *ui;
Expand Down
48 changes: 20 additions & 28 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ const QString SETTINGS_FILE = QDir::toNativeSeparators(
QDir::homePath() + "//VirtualGamePad.ini"); // the path of the settigs file. C:\Users\<username>\VirtualGamePad.ini
QSettings *settings;

QString setting_keys::Mouse_sensivity = "mouse_setting/mouse_sensivity";
QString setting_keys::Mouse_sensitivity = "mouse_setting/mouse_sensitivity";

void (*load_functions[3])(void) = {
void (*load_functions[])(void) = {
load_mouse_setting, load_port_number,
load_key_maps}; // an array of pointer to functions that needs to run on startup to load settings.

/**
* @brief save_setting
* Save a setting to the settings file
* @param key
* The name of the setting in string format if you want to group settings seperate the head group using </>
* @param value
* The value of the settings can be of any data type string, int, char, float, e.t.c.
*/
void save_setting(QString key, QVariant value)
void save_setting(const QString &key, const QVariant &value)
{
settings->setValue(key, value);
settings->sync();
Expand All @@ -31,29 +30,26 @@ void save_setting(QString key, QVariant value)
}

/**
* @brief load_setting
* Get the value of a setting.
* @param key
* The fullname including the groups of the setting in string format
* @return The setting as QVariant can be converted into almost all data types.
*/
QVariant load_setting(QString key)
QVariant load_setting(const QString &key)
{
QVariant value = settings->value(key);
return value;
}

/**
* @brief load_mouse_setting
* Loads and sets the mouse sensivity.
* Loads and sets the mouse sensitivity.
*/
void load_mouse_setting()
{
mouse_sensivity = settings->value(setting_keys::Mouse_sensivity).toInt() * 100;
mouse_sensitivity = settings->value(setting_keys::Mouse_sensitivity).toInt() * 100;
}

/**
* @brief load_settings_file
* Loads the settings file for the current application.
* @param parent
* The parent object either a widget or Qt Application e.t.c
Expand All @@ -64,7 +60,6 @@ void load_settings_file(QObject *parent = nullptr)
}

/**
* @brief load_port_number
* Load and set the port number
*/
void load_port_number() // set the port number as user_defined
Expand All @@ -73,66 +68,63 @@ void load_port_number() // set the port number as user_defined
}

/**
* @brief load_key_maps
* Load the key maps from the settings file.
*/
void load_key_maps() // set the key mappings to the stored values
{
GAMEPAD_BUTTONS[GamepadButtons_A] =
TRIAL{(WORD)settings->value(keymaps[A], GAMEPAD_BUTTONS[GamepadButtons_A].vk).toULongLong(),
Input{(WORD)settings->value(keymaps[A], GAMEPAD_BUTTONS[GamepadButtons_A].vk).toULongLong(),
is_mouse_button(settings->value(keymaps[A], GAMEPAD_BUTTONS[GamepadButtons_A].vk).toULongLong())};
GAMEPAD_BUTTONS[GamepadButtons_B] =
TRIAL{(WORD)settings->value(keymaps[B], GAMEPAD_BUTTONS[GamepadButtons_B].vk).toULongLong(),
Input{(WORD)settings->value(keymaps[B], GAMEPAD_BUTTONS[GamepadButtons_B].vk).toULongLong(),
is_mouse_button(settings->value(keymaps[B], GAMEPAD_BUTTONS[GamepadButtons_B].vk).toULongLong())};
GAMEPAD_BUTTONS[GamepadButtons_X] =
TRIAL{(WORD)settings->value(keymaps[X], GAMEPAD_BUTTONS[GamepadButtons_X].vk).toULongLong(),
Input{(WORD)settings->value(keymaps[X], GAMEPAD_BUTTONS[GamepadButtons_X].vk).toULongLong(),
is_mouse_button(settings->value(keymaps[X], GAMEPAD_BUTTONS[GamepadButtons_X].vk).toULongLong())};
GAMEPAD_BUTTONS[GamepadButtons_Y] =
TRIAL{(WORD)settings->value(keymaps[Y], GAMEPAD_BUTTONS[GamepadButtons_Y].vk).toULongLong(),
Input{(WORD)settings->value(keymaps[Y], GAMEPAD_BUTTONS[GamepadButtons_Y].vk).toULongLong(),
is_mouse_button(settings->value(keymaps[Y], GAMEPAD_BUTTONS[GamepadButtons_Y].vk).toULongLong())};
GAMEPAD_BUTTONS[GamepadButtons_LeftShoulder] =
TRIAL{(WORD)settings->value(keymaps[LSHDR], GAMEPAD_BUTTONS[GamepadButtons_LeftShoulder].vk).toULongLong(),
Input{(WORD)settings->value(keymaps[LSHDR], GAMEPAD_BUTTONS[GamepadButtons_LeftShoulder].vk).toULongLong(),
is_mouse_button(
settings->value(keymaps[LSHDR], GAMEPAD_BUTTONS[GamepadButtons_LeftShoulder].vk).toULongLong())};
GAMEPAD_BUTTONS[GamepadButtons_RightShoulder] =
TRIAL{(WORD)settings->value(keymaps[RSHDR], GAMEPAD_BUTTONS[GamepadButtons_RightShoulder].vk).toULongLong(),
Input{(WORD)settings->value(keymaps[RSHDR], GAMEPAD_BUTTONS[GamepadButtons_RightShoulder].vk).toULongLong(),
is_mouse_button(
settings->value(keymaps[RSHDR], GAMEPAD_BUTTONS[GamepadButtons_RightShoulder].vk).toULongLong())};
GAMEPAD_BUTTONS[GamepadButtons_DPadDown] = TRIAL{
GAMEPAD_BUTTONS[GamepadButtons_DPadDown] = Input{
(WORD)settings->value(keymaps[DPADDOWN], GAMEPAD_BUTTONS[GamepadButtons_DPadDown].vk).toULongLong(),
is_mouse_button(settings->value(keymaps[DPADDOWN], GAMEPAD_BUTTONS[GamepadButtons_DPadDown].vk).toULongLong())};
GAMEPAD_BUTTONS[GamepadButtons_DPadUp] = TRIAL{
GAMEPAD_BUTTONS[GamepadButtons_DPadUp] = Input{
(WORD)settings->value(keymaps[DPADUP], GAMEPAD_BUTTONS[GamepadButtons_DPadUp].vk).toLongLong(),
is_mouse_button(settings->value(keymaps[DPADUP], GAMEPAD_BUTTONS[GamepadButtons_DPadUp].vk).toLongLong())};
GAMEPAD_BUTTONS[GamepadButtons_DPadRight] =
TRIAL{(WORD)settings->value(keymaps[DPADRIGHT], GAMEPAD_BUTTONS[GamepadButtons_DPadRight].vk).toLongLong(),
Input{(WORD)settings->value(keymaps[DPADRIGHT], GAMEPAD_BUTTONS[GamepadButtons_DPadRight].vk).toLongLong(),
is_mouse_button(
settings->value(keymaps[DPADRIGHT], GAMEPAD_BUTTONS[GamepadButtons_DPadRight].vk).toLongLong())};
GAMEPAD_BUTTONS[GamepadButtons_DPadLeft] = TRIAL{
GAMEPAD_BUTTONS[GamepadButtons_DPadLeft] = Input{
(WORD)settings->value(keymaps[DPADLEFT], GAMEPAD_BUTTONS[GamepadButtons_DPadLeft].vk).toLongLong(),
is_mouse_button(settings->value(keymaps[DPADLEFT], GAMEPAD_BUTTONS[GamepadButtons_DPadLeft].vk).toLongLong())};
GAMEPAD_BUTTONS[GamepadButtons_View] =
TRIAL{(WORD)settings->value(keymaps[VIEW], GAMEPAD_BUTTONS[GamepadButtons_View].vk).toLongLong(),
Input{(WORD)settings->value(keymaps[VIEW], GAMEPAD_BUTTONS[GamepadButtons_View].vk).toLongLong(),
is_mouse_button(settings->value(keymaps[VIEW], GAMEPAD_BUTTONS[GamepadButtons_View].vk).toLongLong())};
GAMEPAD_BUTTONS[GamepadButtons_Menu] =
TRIAL{(WORD)settings->value(keymaps[MENU], GAMEPAD_BUTTONS[GamepadButtons_Menu].vk).toLongLong(),
Input{(WORD)settings->value(keymaps[MENU], GAMEPAD_BUTTONS[GamepadButtons_Menu].vk).toLongLong(),
is_mouse_button(settings->value(keymaps[MENU], GAMEPAD_BUTTONS[GamepadButtons_Menu].vk).toLongLong())};
}

/**
* @brief load_all_settings
* Run the functions using the function array to load all the necessary settings.
*/
void load_all_settings()
{
for (int i = 0; i < 3; i++)
for (auto &load_function : load_functions)
{
(*load_functions[i])();
(*load_function)();
}
}

/**
* @brief is_mouse_button
* Function to find out if the user mapped a keyboard key or mouse button
* @param vk
* Virtual key code of the key or mouse button pressed
Expand Down
Loading
Loading