Skip to content

Commit ba10967

Browse files
author
Jim Hamill
committed
Add entry now works (though maybe buggy)
1 parent 2ec776c commit ba10967

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

BrowseWidget.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ BrowseWidget::BrowseWidget(QWidget *parent) :
2121
BrowseWidget::~BrowseWidget()
2222
{
2323
// Delete the model
24-
if (m_model) {
25-
delete m_model;
26-
}
24+
if (m_model) delete m_model;
2725

2826
// Delete the user interface
2927
delete ui;
@@ -59,9 +57,6 @@ bool BrowseWidget::setTable(QString table)
5957
ui->filterEdit->setEnabled(true);
6058
ui->filterButton->setEnabled(true);
6159

62-
// Make sure we save changes when moving to a new row (this is necessary so "Add" works)
63-
connect(ui->tableView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(commitChanges()));
64-
6560
return true;
6661
}
6762

@@ -94,15 +89,27 @@ void BrowseWidget::on_addButton_clicked()
9489

9590
// Insert the row
9691
m_model->insertRecord(-1, newRecord);
92+
93+
// Select the newly inserted row
94+
ui->tableView->selectRow(m_model->rowCount()-1);
95+
96+
// Make sure we save changes when moving to a new row (this is necessary so "Add" works)
97+
connect(ui->tableView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex,QModelIndex)));
9798
}
9899

99100
void BrowseWidget::on_clearButton_clicked()
100101
{
101-
// Remove all rows
102-
m_model->removeRows(1, m_model->rowCount()-1);
102+
// Display messagebox for confirmation
103+
if (QMessageBox::Yes == QMessageBox::question(this, "Clear Table", "Are you sure you want to clear this table?\n\nThis action cannot be undone.")) {
103104

104-
// Refresh browse widget
105-
m_model->select();
105+
// Remove all rows TODO make more efficient
106+
for (int i = 0; i < m_model->rowCount(); i++) {
107+
m_model->removeRow(i);
108+
}
109+
110+
// Refresh browse widget
111+
m_model->select();
112+
}
106113
}
107114

108115
void BrowseWidget::commitChanges()
@@ -116,7 +123,20 @@ void BrowseWidget::commitChanges()
116123
QMessageBox::critical(this, "Operation failed", m_model->lastError().text());
117124
}
118125
}
126+
}
127+
128+
void BrowseWidget::currentChanged(QModelIndex previous, QModelIndex current)
129+
{
130+
// If the row has changed, commit changes
131+
if (previous.row() != current.row()) {
119132

120-
// Revert back to OnFieldChange
121-
//m_model->setEditStrategy(QSqlTableModel::OnFieldChange);
133+
// Commit changes
134+
commitChanges();
135+
136+
// There's no reason to call this anymore, we don't need to know if the row has changed
137+
disconnect(ui->tableView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex,QModelIndex)));
138+
139+
// Revert back to OnFieldChange
140+
m_model->setEditStrategy(QSqlTableModel::OnFieldChange);
141+
}
122142
}

BrowseWidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ private slots:
3030

3131
void on_clearButton_clicked();
3232

33+
void currentChanged(QModelIndex previous, QModelIndex current);
34+
3335
void commitChanges();
3436

3537
private:

Extension/MySQLExtension.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ MySQLExtension::MySQLExtension(QObject *parent, QSqlDatabase *database) :
1212
Extension(parent, database)
1313
{
1414
// Backup actions
15-
m_backupAction = new QAction(QIcon::fromTheme("filesave"), QT_TR_NOOP("&Backup..."), &m_toolsToolBar);
16-
m_backupAction->setStatusTip(QT_TR_NOOP("Backup database to file"));
15+
m_backupAction = new QAction(QIcon::fromTheme("document-export"), QT_TR_NOOP("&Export..."), &m_toolsToolBar);
16+
m_backupAction->setStatusTip(QT_TR_NOOP("Export database"));
1717
connect(m_backupAction, SIGNAL(triggered()), &m_toolsWidget, SLOT(on_backupDatabaseButton_clicked()));
1818

1919
// Restore actions
20-
m_restoreAction = new QAction(QIcon::fromTheme("fileopen"), QT_TR_NOOP("&Restore..."), &m_toolsToolBar);
21-
m_restoreAction->setStatusTip(QT_TR_NOOP("Restore database to file"));
20+
m_restoreAction = new QAction(QIcon::fromTheme("document-import"), QT_TR_NOOP("&Import..."), &m_toolsToolBar);
21+
m_restoreAction->setStatusTip(QT_TR_NOOP("Import Database"));
2222
connect(m_restoreAction, SIGNAL(triggered()), &m_toolsWidget, SLOT(on_restoreDatabaseButton_clicked()));
2323

2424
// Clear actions

0 commit comments

Comments
 (0)