From 35f5a0c5e13df3a0a41ef990b886a98ce2374a5c Mon Sep 17 00:00:00 2001 From: Arno Date: Mon, 3 Jun 2013 09:32:41 +0200 Subject: Remove nodes Implement remove nodes for ArchiveView. Only empty nodes without children can be removed. Everything else is too dangerous :) --- archivemodel.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++----- archivemodel.h | 2 ++ archiveview.cpp | 12 ++++++++++++ archiveview.h | 1 + shemov.cpp | 5 +++++ shemov.h | 1 + 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/archivemodel.cpp b/archivemodel.cpp index 21ee015..9ef2eac 100644 --- a/archivemodel.cpp +++ b/archivemodel.cpp @@ -94,11 +94,42 @@ bool ArchiveModel::setData(const QModelIndex &idx, const QVariant &value, int ro return true; } }else{ - QSqlError e = updateQuery.lastError(); - QString databaseText = e.databaseText().isEmpty() ? tr("(none)") : e.databaseText(); - QString driverText = e.driverText().isEmpty() ? tr("(none)") : e.driverText(); - QString errormsg = QString(tr("Database error:
")).arg(driverText).arg(databaseText); - emit databaseError(errormsg); + emitDatabaseError(updateQuery.lastError()); + } + return false; +} + +bool ArchiveModel::removeNode(const QModelIndex &idx){ + if(!idx.isValid()){ + return false; + } + SmTreeItem *item = itemAt(idx); + if(item->childCount()){ + return false; + } + int nodeType = item->data(Type).toInt(); + QSqlQuery removeQuery(mDb); + if(nodeType == GenreNode){ + removeQuery.prepare("DELETE FROM genres WHERE igenres_id = :id"); + }else if(nodeType == ActorNode){ + removeQuery.prepare("DELETE FROM actors WHERE iactors_id = :id"); + }else if(nodeType == SeriesNode){ + removeQuery.prepare("DELETE FROM series WHERE iseries_id = :id"); + }else{ + return false; + } + QVariant id = item->data(GenericId); + removeQuery.bindValue(":id", id); + if(removeQuery.exec()){ + if(nodeType == GenreNode || nodeType == ActorNode){ + removeRows(idx.row(), 1, idx.parent()); + return true; + }else if(nodeType == SeriesNode){ + emit needRefresh(); + return true; + } + }else{ + emitDatabaseError(removeQuery.lastError()); } return false; } @@ -151,6 +182,13 @@ void ArchiveModel::collectorFinished(){ setRoot(item); } +void ArchiveModel::emitDatabaseError(const QSqlError &e){ + QString databaseText = e.databaseText().isEmpty() ? tr("(none)") : e.databaseText(); + QString driverText = e.driverText().isEmpty() ? tr("(none)") : e.driverText(); + QString errormsg = QString(tr("Database error:
")).arg(driverText).arg(databaseText); + emit databaseError(errormsg); +} + ArchiveCollector::ArchiveCollector(int numFields, QObject *parent) : QThread(parent), mNumFields(numFields) { mDb = QSqlDatabase::cloneDatabase(QSqlDatabase::database("treedb"), "archivedb"); mDb.open(); diff --git a/archivemodel.h b/archivemodel.h index 946bb3c..8055c31 100644 --- a/archivemodel.h +++ b/archivemodel.h @@ -30,6 +30,7 @@ class ArchiveModel : public SmTreeModel { ArchiveCollector *collector() { return mCollector; } virtual QVariant data(const QModelIndex &index, int role) const; virtual bool setData(const QModelIndex &idx, const QVariant &value, int role); + virtual bool removeNode(const QModelIndex &idx); QStringList indexToPath(const QModelIndex &idx) const; QModelIndexList pathToIndex(const QStringList &path) const; @@ -46,6 +47,7 @@ class ArchiveModel : public SmTreeModel { void collectorFinished(); private: + void emitDatabaseError(const QSqlError &e); QSqlDatabase mDb; QHash mAvailableOrders; ArchiveCollector *mCollector; diff --git a/archiveview.cpp b/archiveview.cpp index 4d9e370..96bf01b 100644 --- a/archiveview.cpp +++ b/archiveview.cpp @@ -185,6 +185,18 @@ void ArchiveTree::rename(){ } } +void ArchiveTree::remove(){ + QModelIndex idx = currentIndex(); + QModelIndex realIdx = mProxy->mapToSource(idx); + QString warningMsg = QString("Really remove %1?").arg(idx.data().toString()); + int retval = QMessageBox::warning(this, tr("Question"), warningMsg, QMessageBox::Yes | QMessageBox::No); + if(retval == QMessageBox::Yes){ + if(!mModel->removeNode(realIdx)){ + impossible(); + } + } +} + void ArchiveTree::impossible(const QString msg){ QMessageBox::critical(this, tr("Error"), msg); } diff --git a/archiveview.h b/archiveview.h index 540db29..9e3e666 100644 --- a/archiveview.h +++ b/archiveview.h @@ -72,6 +72,7 @@ class ArchiveTree : public SmTreeView { public slots: void rename(); + void remove(); private: void impossible(const QString msg = tr("Unable to perform function!")); diff --git a/shemov.cpp b/shemov.cpp index e0425fb..8912295 100644 --- a/shemov.cpp +++ b/shemov.cpp @@ -738,9 +738,14 @@ void SheMov::createActions(){ /* picView(er) END Actions! */ // ArchiveView actions + // rename mArchiveViewRenameA = new QAction(tr("Rename..."), this); connect(mArchiveViewRenameA, SIGNAL(triggered()), mArchive->archiveTree(), SLOT(rename())); mArchive->archiveTree()->addAction(mArchiveViewRenameA); + // remove + mArchiveViewRemoveA = new QAction(tr("Remove..."), this); + connect(mArchiveViewRemoveA, SIGNAL(triggered()), mArchive->archiveTree(), SLOT(remove())); + mArchive->archiveTree()->addAction(mArchiveViewRemoveA); //don't add actions with checkable(true) unless you know what you're doing! mPicActionGroup = new QActionGroup(this); diff --git a/shemov.h b/shemov.h index a18e0b1..d7c6095 100644 --- a/shemov.h +++ b/shemov.h @@ -187,6 +187,7 @@ class SheMov : public QMainWindow { //ArchiveView actions QAction *mArchiveViewRenameA; + QAction *mArchiveViewRemoveA; QSignalMapper *mOpenWithMapperFS; QSignalMapper *mOpenWithMapperAV; -- cgit v1.2.3-70-g09d2