summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-09-18 16:15:28 +0200
committerYorhel <git@yorhel.nl>2009-09-18 16:15:28 +0200
commit395b4044c7febda9f07e4ff1ec7723b29760195c (patch)
tree44079378705f3485774bc9eb43c1b2eb5e9bb0ce
parentb36adf6d49cde8135843e1b357dd5e016a69aa7f (diff)
Added delete option and context menu to transaction table
And cleaned some things up and fixed some typos while I was at it.
-rw-r--r--geld.cpp65
-rw-r--r--geld.h8
-rw-r--r--geld.ui31
3 files changed, 80 insertions, 24 deletions
diff --git a/geld.cpp b/geld.cpp
index 4308814..b020fe7 100644
--- a/geld.cpp
+++ b/geld.cpp
@@ -27,8 +27,20 @@ Geld::Geld(QWidget *parent)
TransactionModel *transmod = new TransactionModel(ui->tableTransactions);
ui->tableTransactions->setModel(transmod);
ui->tableTransactions->setColumnHidden(0, true);
- ui->tableTransactions->resizeColumnsToContents();
- ui->tableTransactions->horizontalHeader()->setStretchLastSection(true);
+ refreshTransactions();
+
+ // the Qt Designer interface lacks functionality for connecting actions to widgets
+ ui->tableTransactions->addAction(ui->action_trans_Add);
+ ui->tableTransactions->addAction(ui->action_trans_Edit);
+ ui->tableTransactions->addAction(ui->action_trans_Delete);
+
+ // the Qt Designer interface also lacks some functionality when it comes to connecting signals to functions
+ connect(ui->btnAddTransaction, SIGNAL(clicked()), this, SLOT(addTransaction()));
+ connect(ui->action_trans_Add, SIGNAL(triggered()), this, SLOT(addTransaction()));
+ connect(ui->btnEditTransaction, SIGNAL(clicked()), this, SLOT(editTransaction()));
+ connect(ui->action_trans_Edit, SIGNAL(triggered()), this, SLOT(editTransaction()));
+ connect(ui->tableTransactions, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editTransaction()));
+ connect(ui->action_trans_Delete, SIGNAL(triggered()), this, SLOT(deleteTransaction()));
ui->statusBar->showMessage(QString("Opened database file %1").arg(f), 0);
}
@@ -80,39 +92,56 @@ void Geld::closeDB()
QSqlDatabase::removeDatabase(conn);
}
-void Geld::on_btnEditTransaction_clicked()
-{
+int Geld::selectedTransaction() {
// this. is. soooooooo. ugly.
TransactionModel *mod = (TransactionModel *) ui->tableTransactions->model();
QModelIndexList indexes = ui->tableTransactions->selectionModel()->selection().indexes();
- if(indexes.count() != mod->columnCount())
- return ui->statusBar->showMessage("Nothing selected.", 3000);
- int id = mod->data(mod->index(indexes.at(0).row(), 0), Qt::DisplayRole).toInt();
+ if(indexes.count() != mod->columnCount()) {
+ ui->statusBar->showMessage("Nothing selected.", 3000);
+ return 0;
+ }
+ return mod->data(mod->index(indexes.at(0).row(), 0), Qt::DisplayRole).toInt();
+}
+
+void Geld::refreshTransactions() {
+ ((TransactionModel *) ui->tableTransactions->model())->refresh();
+ ui->tableTransactions->resizeColumnsToContents();
+ ui->tableTransactions->horizontalHeader()->setStretchLastSection(true);
+}
+
+void Geld::editTransaction()
+{
+ int id = selectedTransaction();
+ if(!id)
+ return;
Transaction t(this);
t.load(id);
if(t.exec() == QDialog::Accepted) {
t.save(id);
- mod->refresh();
+ refreshTransactions();
}
}
-void Geld::on_btnAddTramsactopm_clicked()
+void Geld::addTransaction()
{
Transaction t(this);
if(t.exec() == QDialog::Accepted) {
t.save();
- ((TransactionModel *) ui->tableTransactions->model())->refresh();
+ refreshTransactions();
}
}
-void Geld::on_tableTransactions_doubleClicked(QModelIndex index)
+void Geld::deleteTransaction()
{
- TransactionModel *mod = (TransactionModel *) ui->tableTransactions->model();
- int id = mod->record(index.row()).value("id").toInt();
- Transaction t(this);
- t.load(id);
- if(t.exec() == QDialog::Accepted) {
- t.save(id);
- mod->refresh();
+ int id = selectedTransaction();
+ if(!id)
+ return;
+ if(QMessageBox::question(this, "You sure?", "Are you sure you want to remove the selected transaction?", QMessageBox::No|QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
+ QSqlQuery q;
+ q.prepare("DELETE FROM transactions WHERE id = ?");
+ q.addBindValue(id);
+ q.exec();
+ refreshTransactions();
}
}
+
diff --git a/geld.h b/geld.h
index c4fe4f2..b807cd0 100644
--- a/geld.h
+++ b/geld.h
@@ -20,12 +20,14 @@ public:
private:
void initDB(QString f);
void closeDB();
+ int selectedTransaction();
+ void refreshTransactions();
Ui::Geld *ui;
private slots:
- void on_tableTransactions_doubleClicked(QModelIndex index);
- void on_btnAddTramsactopm_clicked();
- void on_btnEditTransaction_clicked();
+ void addTransaction();
+ void editTransaction();
+ void deleteTransaction();
};
#endif // GELD_H
diff --git a/geld.ui b/geld.ui
index f487673..b73598d 100644
--- a/geld.ui
+++ b/geld.ui
@@ -30,6 +30,9 @@
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="3">
<widget class="QTableView" name="tableTransactions">
+ <property name="contextMenuPolicy">
+ <enum>Qt::ActionsContextMenu</enum>
+ </property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
@@ -68,7 +71,7 @@
</widget>
</item>
<item row="1" column="2">
- <widget class="QPushButton" name="btnAddTramsactopm">
+ <widget class="QPushButton" name="btnAddTransaction">
<property name="text">
<string>&amp;Add new transaction</string>
</property>
@@ -102,13 +105,35 @@
<property name="text">
<string>&amp;Close</string>
</property>
+ <property name="statusTip">
+ <string/>
+ </property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
- <action name="action_About">
+ <action name="action_trans_Delete">
+ <property name="text">
+ <string>&amp;Delete</string>
+ </property>
+ <property name="toolTip">
+ <string>Remove transaction</string>
+ </property>
+ </action>
+ <action name="action_trans_Add">
<property name="text">
- <string>&amp;About</string>
+ <string>&amp;New transaction</string>
+ </property>
+ <property name="toolTip">
+ <string>Add new transaction</string>
+ </property>
+ </action>
+ <action name="action_trans_Edit">
+ <property name="text">
+ <string>&amp;Edit</string>
+ </property>
+ <property name="toolTip">
+ <string>Edit transaction</string>
</property>
</action>
</widget>