q.exec();
}
endInsertRows();
+ refresh();
+ return true;
+}
+
+bool EmuFrontFileObjectModel::removeRows(int row, int count, const QModelIndex &parent)
+{
+ if (parent.isValid()) {
+ return false; // This is a flat model
+ }
+ if (rowCount() < row + count - 1)
+ return false;
+
+ QSqlQuery q;
+ q.prepare(QString("DELETE FROM %1 WHERE id=:id").arg(tableName));
+ QModelIndex primaryIndex;
+ int id = -1;
+ beginRemoveRows(QModelIndex(), row, row + count - 1);
+ for(int i = 0; i < count; ++i) {
+ primaryIndex = QSqlQueryModel::index(row + i, EmuFrontFileObject_Id);
+ id = data(primaryIndex).toInt();
+ qDebug() << "Removing data item with id " << id;
+ q.bindValue(":id", id);
+ q.exec();
+ }
+ endRemoveRows();
+ refresh();
return true;
}
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
bool insertRows(int row, int count, const QModelIndex &parent);
+ bool removeRows(int row, int count, const QModelIndex &parent);
enum {
EmuFrontFileObject_Id,
EmuFrontFileObject_Name,
void EmuFrontDataDialog::connectSignals()
{
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
- /*connect(objectList, SIGNAL(clicked(const QModelIndex &)),
- this, SLOT(listObjectClicked(const QModelIndex &)));*/
+ connect(objectList, SIGNAL(clicked(const QModelIndex &)),
+ this, SLOT(listObjectClicked(const QModelIndex &)));
connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
void EmuFrontDataDialog::editButtonClicked()
{
+ setButtonsEnabled(false);
qDebug() << "Edit button clicked";
}
void EmuFrontDataDialog::deleteButtonClicked()
{
- qDebug() << "Delete button clicked";
+ setButtonsEnabled(false);
+ if (!objectList->currentIndex().isValid())
+ return;
+ int row = objectList->currentIndex().row();
+ if ( !model->removeRows(row, 1) ) {
+ errorMessage->showMessage(tr("Failed removing selected item."));
+ }
}
+
void EmuFrontDataDialog::addButtonClicked()
{
int row = objectList->currentIndex().isValid() ?
objectList->setCurrentIndex(ind);
objectList->edit(ind);
}
+
+void EmuFrontDataDialog::listObjectClicked(const QModelIndex &index)
+{
+ setButtonsEnabled(index.isValid());
+}
+
+void EmuFrontDataDialog::setButtonsEnabled(bool b)
+{
+ editButton->setEnabled(b);
+ deleteButton->setEnabled(b);
+}
void editButtonClicked();
void addButtonClicked();
void deleteButtonClicked();
+ void listObjectClicked(const QModelIndex &);
protected:
EmuFrontQueryModel *model;
virtual void setHiddenColumns() = 0;
void hideColumns();
virtual void connectSignals();
+ void setButtonsEnabled(bool);
};