From 742c7be190f2d580e89474b88a5120ac797a8c4e Mon Sep 17 00:00:00 2001 From: SergoZar Date: Fri, 1 Sep 2023 14:13:41 +0000 Subject: [PATCH] Upload files to "/" --- autostart.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 35 +++++++++++++++++++ modethread.h | 32 ++++++++++++++++++ numlockmanager.h | 69 +++++++++++++++++++++++++++++++++++++ statusshow.h | 29 ++++++++++++++++ 5 files changed, 253 insertions(+) create mode 100644 autostart.h create mode 100644 config.h create mode 100644 modethread.h create mode 100644 numlockmanager.h create mode 100644 statusshow.h diff --git a/autostart.h b/autostart.h new file mode 100644 index 0000000..8a8d908 --- /dev/null +++ b/autostart.h @@ -0,0 +1,88 @@ +#ifndef AUTOSTART_H +#define AUTOSTART_H + +#include +#include +#include + +#define MY_APP_NAME "NumLockManager" +#define MY_APP_PATH QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + + +#ifdef Q_OS_WIN +#include + +class Autostart{ +public: + Autostart (){} + ~Autostart(){delete settings;} + + bool status(){ + return !settings->value(MY_APP_NAME).toString().isEmpty(); + } + + void set(bool s){ + s ? settings->setValue(MY_APP_NAME, MY_APP_PATH) : settings->remove(MY_APP_NAME); + settings->sync(); + } + + void switch_(){ + status() ? settings->remove(MY_APP_NAME) : settings->setValue(MY_APP_NAME, MY_APP_PATH); + settings->sync(); + } +private: + QSettings * settings = new QSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); +}; + +#endif + +#ifdef Q_OS_LINUX +#include +#include + +class Autostart{ +public: + Autostart (){ + file_data = file_data.replace("$", MY_APP_PATH); + } + + bool status(){ + return QFile::exists(file_path); + } + + void set(bool s){ + s ? make_file() : (void)QFile::remove(file_path); + } + + void switch_(){ + status() ? (void)QFile::remove(file_path) : make_file(); + } + +private: + QString file_path = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0] + "/autostart/NumLockManager.desktop"; + QString file_data = QString("[Desktop Entry]\n" + "Encoding=UTF-8\n" + "Version=1.2\n" + "Type=Application\n" + "Name=NumLockManager\n" + "Comment=\n" + "Exec=$\n" + "RunHook=0\n" + "StartupNotify=false\n" + "Terminal=false\n" + "Hidden=false\n"); + + + + void make_file(){ + QFile file(file_path); + file.open(QIODevice::WriteOnly | QIODevice::Text); + file.write(file_data.toUtf8()); + file.close(); + } +}; + + +#endif + +#endif // AUTOSTART_H diff --git a/config.h b/config.h new file mode 100644 index 0000000..f8e7c3b --- /dev/null +++ b/config.h @@ -0,0 +1,35 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include +#include + +class Config{ +public: + Config(){ file->exists() ? load() : setMode(0);} + ~Config(){ + file->close(); + delete file; + } + void setMode(int m) { mode = m; save(); } + int getMode() {return mode;} + +private: + int mode = 0; + QFile * file = new QFile(QCoreApplication::applicationDirPath() + "/src/config.ini"); + + void load(){ + file->open(QIODevice::ReadOnly | QIODevice::Text); + mode = file->readAll().toInt(); + file->close(); + } + + void save(){ + file->open(QIODevice::WriteOnly | QIODevice::Text); + QString a; + a.setNum(mode); + file->write(a.toUtf8()); + file->close(); + } +}; +#endif // CONFIG_H diff --git a/modethread.h b/modethread.h new file mode 100644 index 0000000..1c04eb7 --- /dev/null +++ b/modethread.h @@ -0,0 +1,32 @@ +#ifndef MODETHREAD_H +#define MODETHREAD_H + +#include + +#include "wl_numlock.h" + +class ModeThread : public QThread +{ + Q_OBJECT +public: + ModeThread(){} + ~ModeThread(){ delete key_manager; } + + void setMode(int m){ mode = m; } + +private: + int mode = 0; // 0 - on/off, 1 - on, 2 - off + WLKeyManager * key_manager = new WLKeyManager(); + +protected: + void run() override{ + forever{ + if (mode == 0) return; + msleep(50); + if ((mode == 1 && !key_manager->getNumLockState()) || (mode == 2 && key_manager->getNumLockState())) + key_manager->pressNumLock(); + } + } +}; + +#endif // MODETHREAD_H diff --git a/numlockmanager.h b/numlockmanager.h new file mode 100644 index 0000000..7205c4a --- /dev/null +++ b/numlockmanager.h @@ -0,0 +1,69 @@ +#ifndef NUMLOCKMANAGER_H +#define NUMLOCKMANAGER_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "statusshow.h" +#include "modethread.h" +#include "config.h" +#include "ui_numlockmanager.h" +#include "autostart.h" + + + +QT_BEGIN_NAMESPACE +namespace Ui { class NumLockManager; } +QT_END_NAMESPACE + +class NumLockManager : public QMainWindow +{ + Q_OBJECT + +public: + NumLockManager(QWidget *parent = nullptr); + ~NumLockManager() { delete ui; } + +private: + Ui::NumLockManager *ui; + + StatusShow * ss = new StatusShow(); + ModeThread * mode = new ModeThread(); + Config config; + Autostart * autostart = new Autostart(); + + QSystemTrayIcon * tray_icon = new QSystemTrayIcon(this); + QIcon tray_icon_off = QIcon(QCoreApplication::applicationDirPath() + "/src/icons/num_lock_icon_off.png"); + QIcon tray_icon_on = QIcon(QCoreApplication::applicationDirPath() + "/src/icons/num_lock_icon_on.png"); + QIcon icon_check_false = QIcon(QCoreApplication::applicationDirPath() + "/src/icons/check_radio_c_0.png"); + QIcon icon_check_true = QIcon(QCoreApplication::applicationDirPath() + "/src/icons/check_radio_c_1.png"); + QIcon icon_radio_false = QIcon(QCoreApplication::applicationDirPath() + "/src/icons/check_radio_r_0.png"); + QIcon icon_radio_true = QIcon(QCoreApplication::applicationDirPath() + "/src/icons/check_radio_r_1.png"); + + QMenu * menu = new QMenu(); + QAction * action_autostart = new QAction(); + QAction * action_mode_0 = new QAction(); + QAction * action_mode_1 = new QAction(); + QAction * action_mode_2 = new QAction(); + QAction * action_about = new QAction(); + QAction * action_exit = new QAction(); + QAction * actions[6] = {action_autostart, action_mode_0, action_mode_1, action_mode_2, action_about ,action_exit}; + QAction * actions_mode[3] = {action_mode_0, action_mode_1, action_mode_2}; + + void set_mode(int index); + +public slots: + void icon_activated(QSystemTrayIcon::ActivationReason reason) {Q_UNUSED(reason); menu->exec(QCursor::pos()); } + void getStatus(bool status); + void set_menu_hz(QAction * action); + +protected: + void closeEvent(QCloseEvent *event) override{ event->ignore(); hide(); } +}; +#endif // NUMLOCKMANAGER_H diff --git a/statusshow.h b/statusshow.h new file mode 100644 index 0000000..25a0f59 --- /dev/null +++ b/statusshow.h @@ -0,0 +1,29 @@ +#ifndef STATUSSHOW_H +#define STATUSSHOW_H + +#include + +#include "wl_numlock.h" + +class StatusShow : public QThread +{ + Q_OBJECT +public: + StatusShow(){} + ~StatusShow(){ delete key_manager; } + +private: + WLKeyManager * key_manager; + +signals: + void status(bool); +protected: + void run() override { + forever{ + emit status(key_manager->getNumLockState()); + msleep(500); + } + } +}; + +#endif // STATUSSHOW_H