Skip to content

Commit

Permalink
🔵 commands: added custom static commands (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sclafus authored May 28, 2023
2 parents 256ecf7 + a95a93d commit 3ab69b6
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(SOURCE_UTILS_DIR "${CMAKE_SOURCE_DIR}/src/utils")
set(SOURCE_SETUPWIZARD_DIR "${CMAKE_SOURCE_DIR}/src/setupwizard")
set(SOURCE_PREFERENCES_DIR "${CMAKE_SOURCE_DIR}/src/preferences")
set(SOURCE_SKINSURL_DIR "${CMAKE_SOURCE_DIR}/src/skinsurl")
set(SOURCE_COMMANDS_DIR "${CMAKE_SOURCE_DIR}/src/commands")

include_directories(${FORMS_DIR})
include_directories(${SOURCE_DIR})
Expand All @@ -30,6 +31,7 @@ include_directories(${SOURCE_UTILS_DIR})
include_directories(${SOURCE_SETUPWIZARD_DIR})
include_directories(${SOURCE_PREFERENCES_DIR})
include_directories(${SOURCE_SKINSURL_DIR})
include_directories(${SOURCE_COMMANDS_DIR})

file(GLOB_RECURSE SOURCES
${FORMS_DIR}/*.cpp
Expand All @@ -47,6 +49,8 @@ file(GLOB_RECURSE SOURCES
${SOURCE_PREFERENCES_DIR}/*.cpp
${SOURCE_SKINSURL_DIR}/*.h
${SOURCE_SKINSURL_DIR}/*.cpp
${SOURCE_COMMANDS_DIR}/*.h
${SOURCE_COMMANDS_DIR}/*.cpp
${SOURCE_DIR}/*.h
${SOURCE_DIR}/*.cpp
resources.qrc
Expand Down
165 changes: 165 additions & 0 deletions forms/commandswindow.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CommandsWindow</class>
<widget class="QDialog" name="CommandsWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Commands</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="addCommandBtn">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Add Command</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="removeCommandBtn">
<property name="text">
<string>Remove Command</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="3">
<widget class="QTableWidget" name="commandsTable">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<row>
<property name="text">
<string/>
</property>
</row>
<row>
<property name="text">
<string/>
</property>
</row>
<column>
<property name="text">
<string>Command</string>
</property>
</column>
<column>
<property name="text">
<string>Reply</string>
</property>
</column>
<item row="0" column="0">
<property name="text">
<string>!np</string>
</property>
<property name="flags">
<set>NoItemFlags</set>
</property>
</item>
<item row="0" column="1">
<property name="text">
<string>-</string>
</property>
<property name="flags">
<set>NoItemFlags</set>
</property>
</item>
<item row="1" column="0">
<property name="text">
<string>!skin</string>
</property>
<property name="flags">
<set>NoItemFlags</set>
</property>
</item>
<item row="1" column="1">
<property name="text">
<string>-</string>
</property>
<property name="flags">
<set>NoItemFlags</set>
</property>
</item>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>CommandsWindow</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>CommandsWindow</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
12 changes: 9 additions & 3 deletions forms/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@
<addaction name="actionPreferences"/>
<addaction name="actionStart_Setup"/>
</widget>
<widget class="QMenu" name="menuOsu">
<widget class="QMenu" name="menuResponses">
<property name="title">
<string>Osu</string>
<string>Responses</string>
</property>
<addaction name="actionSkins"/>
<addaction name="actionCommands"/>
</widget>
<addaction name="menuSettings"/>
<addaction name="menuOsu"/>
<addaction name="menuResponses"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QStatusBar" name="statusbar">
Expand Down Expand Up @@ -160,6 +161,11 @@
<string>Skins</string>
</property>
</action>
<action name="actionCommands">
<property name="text">
<string>Commands</string>
</property>
</action>
</widget>
<resources/>
<connections/>
Expand Down
112 changes: 112 additions & 0 deletions src/commands/commandswindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "commandswindow.h"
#include "ui_commandswindow.h"

CommandsWindow::CommandsWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::CommandsWindow)
{
ui->setupUi(this);
loadData();
connect(ui->addCommandBtn, &QPushButton::clicked, this, &CommandsWindow::on_addCommandBtn_clicked);
connect(ui->removeCommandBtn, &QPushButton::clicked, this, &CommandsWindow::on_removeCommandBtn_clicked);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &CommandsWindow::on_saveBtn_clicked);
}

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

void CommandsWindow::loadData()
{
qDebug() << "[Commands] Loading data";
QSettings settings;
settings.beginGroup("command");
QStringList commands = settings.childKeys();

for (QString command: commands) {
addRow(command, settings.value(command).toString());
}
}

void CommandsWindow::addRow(QString command, QString response)
{
// adding new line
ui->commandsTable->insertRow(ui->commandsTable->rowCount());
QTableWidgetItem *commandNameItem = new QTableWidgetItem(command);
QTableWidgetItem *commandResponseItem = new QTableWidgetItem(response);

// Command name column
ui->commandsTable->setItem(
ui->commandsTable->rowCount() - 1,
0,
commandNameItem
);

// Command response column
ui->commandsTable->setItem(
ui->commandsTable->rowCount() - 1,
1,
commandResponseItem
);
}

void CommandsWindow::on_addCommandBtn_clicked()
{
int lastRow = ui->commandsTable->rowCount() - 1;
auto lastCommand = ui->commandsTable->item(lastRow, 0);
auto lastResponse = ui->commandsTable->item(lastRow, 1);

bool isLastCommandEmpty = lastCommand != nullptr && lastCommand->text().isEmpty();
bool isLastResponseEmpty = lastResponse != nullptr && lastResponse->text().isEmpty();

if (!isLastCommandEmpty && !isLastResponseEmpty){
addRow("","");
}
}

void CommandsWindow::on_removeCommandBtn_clicked()
{
QSettings settings;
auto selectionModel = ui->commandsTable->selectionModel();
if (!selectionModel->hasSelection()){
return;
}

auto selected = selectionModel->selectedIndexes();
for (auto selection : selected) {
int row = selection.row();
QString command = ui->commandsTable->item(row, 0)->text();

settings.remove(QString("command/%1").arg(command));
ui->commandsTable->removeRow(row);
}
}

void CommandsWindow::on_saveBtn_clicked()
{
qDebug() << "[Commands] Saving data...";
QSettings settings;

for (int row = 2; row < ui->commandsTable->rowCount(); row++) {
QTableWidgetItem *commandNameItem = ui->commandsTable->item(row, 0);
QTableWidgetItem *commandResponseItem = ui->commandsTable->item(row, 1);

if (commandResponseItem == nullptr || commandNameItem == nullptr) {
return;
}

QString commandName = commandNameItem->text();
QString commandResponse = commandResponseItem->text();

if (!commandName.startsWith("!")) {
commandName.prepend("!");
}

QString key = QString("command/%1").arg(commandName);
settings.setValue(key, commandResponse);
qDebug() << "[Commands] " << commandName << "has been saved";
}
qDebug() << "[Commands] Data saved";
accept();
}
31 changes: 31 additions & 0 deletions src/commands/commandswindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef COMMANDSWINDOW_H
#define COMMANDSWINDOW_H

#include <QDialog>
#include <QMessageBox>
#include <QSettings>

namespace Ui {
class CommandsWindow;
}

class CommandsWindow : public QDialog
{
Q_OBJECT

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

private:
void on_addCommandBtn_clicked();
void on_removeCommandBtn_clicked();
void on_saveBtn_clicked();

void loadData();
void addRow(QString command, QString response);

Ui::CommandsWindow *ui;
};

#endif // COMMANDSWINDOW_H
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main(int argc, char **argv) {
app.setOrganizationName("miraya");
app.setOrganizationDomain("github.com/MirayaProject");
app.setApplicationName("bot");
app.setApplicationVersion("1.1.1");
app.setApplicationVersion("1.2.0-alpha.1");
MainWindow mw;
mw.show();
return app.exec();
Expand Down
6 changes: 6 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ void MainWindow::on_actionSkins_triggered()
}


void MainWindow::on_actionCommands_triggered()
{
CommandsWindow(this).exec();
}


void MainWindow::on_btnStart_clicked()
{
qDebug() << "[MainWindow] init";
Expand Down
Loading

0 comments on commit 3ab69b6

Please sign in to comment.