summaryrefslogtreecommitdiff
path: root/qt/RGBController
diff options
context:
space:
mode:
authordaniel-Jones <daniel@danieljon.es>2016-07-01 22:59:30 +0930
committerdaniel-Jones <daniel@danieljon.es>2016-07-01 22:59:30 +0930
commit3b7cb85d384ca231fb6f25493dbc4e476987beae (patch)
tree6ffe4f6648a0d32644fe2485467990e5c66873db /qt/RGBController
parent963bfd49550f8ec555174ce236f4f0bfa2d370f3 (diff)
downloadRGB-Controller-3b7cb85d384ca231fb6f25493dbc4e476987beae.tar.gz
RGB-Controller-3b7cb85d384ca231fb6f25493dbc4e476987beae.zip
Added the ability to delete presets, removed the hardcoded preset file location - that was dumb on my behalf.
Diffstat (limited to 'qt/RGBController')
-rwxr-xr-xqt/RGBController/RGBController.pro.user2
-rwxr-xr-xqt/RGBController/controllerwindow.cpp113
-rwxr-xr-xqt/RGBController/controllerwindow.h3
-rwxr-xr-xqt/RGBController/controllerwindow.ui25
4 files changed, 102 insertions, 41 deletions
diff --git a/qt/RGBController/RGBController.pro.user b/qt/RGBController/RGBController.pro.user
index cd38fba..fea7287 100755
--- a/qt/RGBController/RGBController.pro.user
+++ b/qt/RGBController/RGBController.pro.user
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
-<!-- Written by QtCreator 4.0.0, 2016-06-15T13:55:50. -->
+<!-- Written by QtCreator 4.0.0, 2016-06-27T00:50:07. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
diff --git a/qt/RGBController/controllerwindow.cpp b/qt/RGBController/controllerwindow.cpp
index ef0ab30..b258ff2 100755
--- a/qt/RGBController/controllerwindow.cpp
+++ b/qt/RGBController/controllerwindow.cpp
@@ -16,8 +16,9 @@ controllerWindow::controllerWindow(QWidget *parent) :
ui->off_button->setEnabled(false);
ui->reload_preset_button->setEnabled(false);
ui->set_preset_button->setEnabled(false);
- ui->preset_save_button->setEnabled(false);
- ui->preset_name_textbox->setEnabled(false);
+ ui->preset_save_button->setEnabled(false);
+ ui->preset_delete_button->setEnabled(false);
+ ui->preset_name_textbox->setEnabled(false);
ui->presets_dropdown->setEnabled(false);
ui->r_slider->setEnabled(false);
ui->g_slider->setEnabled(false);
@@ -83,10 +84,8 @@ void controllerWindow::load_presets()
* -> add [0] to dropdown (name), add [1] to an array that stores all the values
*/
- /* PLEASE READ ME
- * This is a hard coded directory for i3wm's sake, you have to fix this yourself.
- */
- QFile inputFile("/home/daniel_j/documents/school/2016\ research\ project/RGBController/qt/build/degub/presets.txt");
+
+ QFile inputFile("presets.txt");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
@@ -105,28 +104,72 @@ void controllerWindow::load_presets()
{
show_msgbox("Unable to find the presets file.\nThis file needs to be named 'presets.txt' and be located in the same directory as the binary.");
info_log("Presets file not found.");
- }
+ }
}
void controllerWindow::save_preset(QString name)
{
- /* this function will save our preset to file */
- QFile file("presets.txt");
+ /* this function will save our preset to file */
+ QFile file("presets.txt");
+ if(!file.open(QIODevice::Append)) {
+ show_msgbox("Fatal error opening presets for appending text.");
+
+ } else
+ {
+ QTextStream stream(&file);
+ stream << name << "=" << ui->r_slider->value() << "," << ui->g_slider->value() << "," << ui->b_slider->value() << endl;
+ file.close();
+ info_log("Saved preset: " + name);
+ /* things to fix the presets list */
+ ui->presets_dropdown->clear();
+ presets.clear();
+ preset_index = 0;
+ load_presets();
+ }
+}
+
+void controllerWindow::delete_preset(QString name)
+{
+ /*
+ * here we will delete a preset from the presets file
+ * process:
+ * retrieve preset name
+ * go through preset file line by line and write it to a seperate tmp file
+ * if the [0] of split('=') equals the preset name, don't write it to the tmp file
+ * delete the old preset file
+ * rename the tmp file to presets.txt
+ */
+ info_log("deleting preset: " + name);
+ QFile file("tmp.file");
if(!file.open(QIODevice::Append)) {
- show_msgbox("Fatal error opening presets for appending text.");
+ show_msgbox("Fatal error opening temp file for writing");
} else
{
QTextStream stream(&file);
- stream << name << "=" << ui->r_slider->value() << "," << ui->g_slider->value() << "," << ui->b_slider->value() << endl;
+ //stream << "test tmp file" << endl;
+ for (int x = 0; x < ui->presets_dropdown->count(); x++)
+ {
+ if(name != ui->presets_dropdown->itemText(x))
+ {
+ info_log("adding: " + ui->presets_dropdown->itemText(x) + "=" + presets.at(x));
+ stream << ui->presets_dropdown->itemText(x) << "=" << presets.at(x) << endl;
+ } else
+ {
+ info_log("not adding: " + ui->presets_dropdown->itemText(x) + "=" + presets.at(x));
+ }
+ }
file.close();
- info_log("Saved preset: " + name);
- /* things to fix the presets list */
+ /* remove the current presets file then rename the temp file to presets.txt */
+ QFile::remove("presets.txt");
+ QFile::rename("tmp.file", "presets.txt");
+ /* reload presets into memory and clear the drop down box */
ui->presets_dropdown->clear();
presets.clear();
preset_index = 0;
load_presets();
}
+
}
void controllerWindow::serial_rgb_change(int r, int g, int b)
@@ -136,6 +179,7 @@ void controllerWindow::serial_rgb_change(int r, int g, int b)
void controllerWindow::show_msgbox(QString message)
{
+ /* show message box to the user */
QMessageBox msgbox;
msgbox.setText(message);
msgbox.exec();
@@ -168,8 +212,9 @@ void controllerWindow::on_connect_button_clicked()
ui->off_button->setEnabled(true);
ui->reload_preset_button->setEnabled(true);
ui->set_preset_button->setEnabled(true);
- ui->preset_save_button->setEnabled(true);
- ui->preset_name_textbox->setEnabled(true);
+ ui->preset_save_button->setEnabled(true);
+ ui->preset_delete_button->setEnabled(true);
+ ui->preset_name_textbox->setEnabled(true);
ui->presets_dropdown->setEnabled(true);
ui->r_slider->setEnabled(true);
ui->g_slider->setEnabled(true);
@@ -200,8 +245,9 @@ void controllerWindow::on_disconnect_button_clicked()
ui->blue_button->setEnabled(false);
ui->off_button->setEnabled(false);
ui->reload_preset_button->setEnabled(false);
- ui->preset_save_button->setEnabled(false);
- ui->preset_name_textbox->setEnabled(false);
+ ui->preset_save_button->setEnabled(false);
+ ui->preset_delete_button->setEnabled(false);
+ ui->preset_name_textbox->setEnabled(false);
ui->set_preset_button->setEnabled(false);
ui->presets_dropdown->setEnabled(false);
ui->refresh_port_button->setEnabled(true);
@@ -288,8 +334,8 @@ void controllerWindow::on_set_preset_button_clicked()
ui->r_slider->setValue(temparray[0].toInt());
ui->g_slider->setValue(temparray[1].toInt());
ui->b_slider->setValue(temparray[2].toInt());
- } else
- show_msgbox("There are no loaded presets!");
+ } else
+ show_msgbox("There are no loaded presets!");
}
void controllerWindow::on_presets_dropdown_currentIndexChanged(int index)
@@ -299,16 +345,21 @@ void controllerWindow::on_presets_dropdown_currentIndexChanged(int index)
void controllerWindow::on_preset_save_button_clicked()
{
- QString tempname = ui->preset_name_textbox->text();
- if (tempname.contains("="))
- {
- show_msgbox("Cannot save a preset with a name that contains an '='.");
- } else if (tempname.isEmpty())
- {
- show_msgbox("Cannot save a preset without a name.");
- } else
- {
- save_preset(tempname);
- }
- ui->preset_name_textbox->clear();
+ QString tempname = ui->preset_name_textbox->text();
+ if (tempname.contains("="))
+ {
+ show_msgbox("Cannot save a preset with a name that contains an '='.");
+ } else if (tempname.isEmpty())
+ {
+ show_msgbox("Cannot save a preset without a name.");
+ } else
+ {
+ save_preset(tempname);
+ }
+ ui->preset_name_textbox->clear();
+}
+
+void controllerWindow::on_preset_delete_button_clicked()
+{
+ delete_preset(ui->presets_dropdown->currentText());
}
diff --git a/qt/RGBController/controllerwindow.h b/qt/RGBController/controllerwindow.h
index 690b52d..9f73749 100755
--- a/qt/RGBController/controllerwindow.h
+++ b/qt/RGBController/controllerwindow.h
@@ -29,6 +29,7 @@ class controllerWindow : public QMainWindow
void populate_serial_list();
void load_presets();
void save_preset(QString name);
+ void delete_preset(QString name);
void serial_rgb_change(int r, int g, int b);
void show_msgbox(QString message);
/*public variables */
@@ -71,6 +72,8 @@ class controllerWindow : public QMainWindow
void on_preset_save_button_clicked();
+ void on_preset_delete_button_clicked();
+
private:
Ui::controllerWindow *ui;
/* serial communication object */
diff --git a/qt/RGBController/controllerwindow.ui b/qt/RGBController/controllerwindow.ui
index 8ee839e..d582851 100755
--- a/qt/RGBController/controllerwindow.ui
+++ b/qt/RGBController/controllerwindow.ui
@@ -10,7 +10,7 @@
<x>0</x>
<y>0</y>
<width>235</width>
- <height>402</height>
+ <height>428</height>
</rect>
</property>
<property name="windowTitle">
@@ -177,7 +177,7 @@
</property>
</widget>
</item>
- <item row="12" column="0">
+ <item row="14" column="0">
<widget class="QLabel" name="info_log_label">
<property name="text">
<string>Information log</string>
@@ -201,23 +201,30 @@
</property>
</widget>
</item>
- <item row="13" column="0" colspan="2">
+ <item row="15" column="0" colspan="2">
<widget class="QTextEdit" name="info_log_textarea"/>
</item>
- <item row="11" column="0">
+ <item row="12" column="0">
<widget class="QLineEdit" name="preset_name_textbox"/>
</item>
<item row="12" column="1">
- <widget class="QPushButton" name="reload_preset_button">
+ <widget class="QPushButton" name="preset_save_button">
<property name="text">
- <string>Reload</string>
+ <string>Save</string>
</property>
</widget>
</item>
- <item row="11" column="1">
- <widget class="QPushButton" name="preset_save_button">
+ <item row="13" column="1">
+ <widget class="QPushButton" name="preset_delete_button">
<property name="text">
- <string>Save</string>
+ <string>Delete</string>
+ </property>
+ </widget>
+ </item>
+ <item row="11" column="0">
+ <widget class="QPushButton" name="reload_preset_button">
+ <property name="text">
+ <string>Reload</string>
</property>
</widget>
</item>