diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 0d99742..82bb886 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -1805,6 +1805,43 @@ void cMainWindow::onEditPlace() void cMainWindow::onDeletePlace() { + QStandardItem* lpItem = m_lpPlaceModel->itemFromIndex(ui->m_lpPlaceList->currentIndex()); + if(!lpItem) + return; + + cPlace* lpPlace = qvariant_cast(lpItem->data()); + + if(!lpPlace) + return; + + if(m_lpStoryBook->placeInUse(lpPlace)) + { + QMessageBox::critical(this, "Delete Place", tr("This place is still in use.\nPlease delete the usage before deleting the place.")); + return; + + } + + if(QMessageBox::question(this, "Delete Place", tr("Are you sure you want to delete this place:
") + "" + lpPlace->name() + "?") != QMessageBox::Yes) + return; + + lpPlace->setDeleted(true); + m_lpStoryBook->fillPlaceList(ui->m_lpPlaceList); + m_bSomethingChanged = true; + updateWindowTitle(); + + for(int x = 0;x < ui->m_lpMainTab->count();x++) + { + cWidget* lpWidget = (cWidget*)ui->m_lpMainTab->widget(x); + if(lpWidget->type() == cWidget::TYPE_place) + { + cPlaceWindow* lpPlaceWindow = (cPlaceWindow*)lpWidget->widget(); + if(lpPlaceWindow->place() == lpPlace) + { + onMainTabTabCloseRequested(x); + return; + } + } + } } void cMainWindow::onAddObject() @@ -1857,6 +1894,43 @@ void cMainWindow::onEditObject() void cMainWindow::onDeleteObject() { + QStandardItem* lpItem = m_lpObjectModel->itemFromIndex(ui->m_lpObjectList->currentIndex()); + if(!lpItem) + return; + + cObject* lpObject = qvariant_cast(lpItem->data()); + + if(!lpObject) + return; + + if(m_lpStoryBook->objectInUse(lpObject)) + { + QMessageBox::critical(this, "Delete Object", tr("This object is still in use.\nPlease delete the usage before deleting the object.")); + return; + + } + + if(QMessageBox::question(this, "Delete Object", tr("Are you sure you want to delete this object:
") + "" + lpObject->name() + "?") != QMessageBox::Yes) + return; + + lpObject->setDeleted(true); + m_lpStoryBook->fillObjectList(ui->m_lpObjectList); + m_bSomethingChanged = true; + updateWindowTitle(); + + for(int x = 0;x < ui->m_lpMainTab->count();x++) + { + cWidget* lpWidget = (cWidget*)ui->m_lpMainTab->widget(x); + if(lpWidget->type() == cWidget::TYPE_object) + { + cObjectWindow* lpObjectWindow = (cObjectWindow*)lpWidget->widget(); + if(lpObjectWindow->object() == lpObject) + { + onMainTabTabCloseRequested(x); + return; + } + } + } } void cMainWindow::onAddRecherche() @@ -1909,6 +1983,43 @@ void cMainWindow::onEditRecherche() void cMainWindow::onDeleteRecherche() { + QStandardItem* lpItem = m_lpRechercheModel->itemFromIndex(ui->m_lpRechercheList->currentIndex()); + if(!lpItem) + return; + + cRecherche* lpRecherche = qvariant_cast(lpItem->data()); + + if(!lpRecherche) + return; + + if(m_lpStoryBook->rechercheInUse(lpRecherche)) + { + QMessageBox::critical(this, "Delete Recherche", tr("This recherche is still in use.\nPlease delete the usage before deleting the recherche.")); + return; + + } + + if(QMessageBox::question(this, "Delete Recherche", tr("Are you sure you want to delete this recherche:
") + "" + lpRecherche->name() + "?") != QMessageBox::Yes) + return; + + lpRecherche->setDeleted(true); + m_lpStoryBook->fillRechercheList(ui->m_lpRechercheList); + m_bSomethingChanged = true; + updateWindowTitle(); + + for(int x = 0;x < ui->m_lpMainTab->count();x++) + { + cWidget* lpWidget = (cWidget*)ui->m_lpMainTab->widget(x); + if(lpWidget->type() == cWidget::TYPE_recherche) + { + cRechercheWindow* lpRechercheWindow = (cRechercheWindow*)lpWidget->widget(); + if(lpRechercheWindow->recherche() == lpRecherche) + { + onMainTabTabCloseRequested(x); + return; + } + } + } } QString cMainWindow::getProjectLoadName() diff --git a/crecherche.cpp b/crecherche.cpp index 07431c3..75d0be2 100644 --- a/crecherche.cpp +++ b/crecherche.cpp @@ -16,7 +16,8 @@ cRecherche::cRecherche(qint32 iID, QObject *parent) : m_iID(iID), m_szName(""), m_szLink(""), - m_lpDescription(0) + m_lpDescription(0), + m_bDeleted(false) { } @@ -60,6 +61,16 @@ cTextDocument* cRecherche::description() return(m_lpDescription); } +void cRecherche::setDeleted(bool bDeleted) +{ + m_bDeleted = bDeleted; +} + +bool cRecherche::deleted() +{ + return(m_bDeleted); +} + void cRecherche::addImage(cImage* lpImage, cTextDocument* lpDescription) { m_imageList.append(new cImageDescription(lpImage, lpDescription)); @@ -147,6 +158,46 @@ QList cRechercheList::find(cCharacter* lpCharacter) return(rechercheList); } +QList cRechercheList::find(cPlace* lpPlace) +{ + QList rechercheList; + + for(int x = 0;x < count();x++) + { + QList list = at(x)->placeList(); + + for(int y = 0;y < list.count();y++) + { + if(list.at(y)->place() == lpPlace) + { + rechercheList.append(at(x)); + break; + } + } + } + return(rechercheList); +} + +QList cRechercheList::find(cObject* lpObject) +{ + QList rechercheList; + + for(int x = 0;x < count();x++) + { + QList list = at(x)->objectList(); + + for(int y = 0;y < list.count();y++) + { + if(list.at(y)->object() == lpObject) + { + rechercheList.append(at(x)); + break; + } + } + } + return(rechercheList); +} + bool cRechercheList::load(cImageList *lpImageList, cCharacterList *lpCharacterList, cObjectList *lpObjectList, cPlaceList *lpPlaceList) { QSqlQuery query; @@ -247,13 +298,16 @@ bool cRechercheList::save() QSqlQuery queryUpdate; QSqlQuery queryInsert; QSqlQuery querySelect; + QSqlQuery queryDelete; queryUpdate.prepare("UPDATE recherche SET name=:name, description=:description, link=:link WHERE id=:id;"); queryInsert.prepare("INSERT INTO recherche (name, description, link) VALUES (:name, :description, :link);"); querySelect.prepare("SELECT id FROM recherche WHERE _rowid_=(SELECT MAX(_rowid_) FROM recherche);"); + queryDelete.prepare("DELETE FROM recherche WHERE id=:id;"); QSqlQuery imageDelete; QSqlQuery imageAdd; + imageDelete.prepare("DELETE FROM rechercheImage WHERE rechercheID=:rechercheID;"); imageAdd.prepare("INSERT INTO rechercheImage (rechercheID, imageID, description) VALUES (:rechercheID, :imageID, :description);"); @@ -276,7 +330,18 @@ bool cRechercheList::save() { cRecherche* lpRecherche = at(x); - if(lpRecherche->id() != -1) + if(lpRecherche->deleted()) + { + queryDelete.bindValue(":id", lpRecherche->id()); + + if(!queryDelete.exec()) + { + myDebug << queryDelete.lastError().text(); + return(false); + } + this->removeOne(lpRecherche); + } + else if(lpRecherche->id() != -1) { queryUpdate.bindValue(":id", lpRecherche->id()); queryUpdate.bindValue(":name", lpRecherche->name()); diff --git a/crecherche.h b/crecherche.h index b40aa33..a6a8008 100644 --- a/crecherche.h +++ b/crecherche.h @@ -159,6 +159,21 @@ public: */ QList placeList(); + /*! + \brief + + \fn setDeleted + \param bDeleted + */ + void setDeleted(bool bDeleted); + /*! + \brief + + \fn deleted + \return bool + */ + bool deleted(); + private: qint32 m_iID; /*!< TODO: describe */ QString m_szName; /*!< TODO: describe */ @@ -168,6 +183,7 @@ private: QList m_characterList; /*!< TODO: describe */ QList m_objectList; /*!< TODO: describe */ QList m_placeList; /*!< TODO: describe */ + bool m_bDeleted; /*!< TODO: describe */ signals: @@ -228,6 +244,24 @@ public: \return QList */ QList find(cCharacter* lpCharacter); + + /*! + \brief + + \fn find + \param lpPlace + \return QList + */ + QList find(cPlace* lpPlace); + + /*! + \brief + + \fn find + \param lpObject + \return QList + */ + QList find(cObject* lpObject); }; #endif // CRECHERCHE_H diff --git a/cstorybook.cpp b/cstorybook.cpp index f534e00..5c571f5 100644 --- a/cstorybook.cpp +++ b/cstorybook.cpp @@ -677,6 +677,33 @@ bool cStoryBook::characterInUse(cCharacter* lpCharacter) return(false); } +bool cStoryBook::placeInUse(cPlace* lpPlace) +{ + if(m_sceneList.find(lpPlace).count() > 0) + return(true); + + if(m_rechercheList.find(lpPlace).count() > 0) + return(true); + + return(false); +} + +bool cStoryBook::objectInUse(cObject* lpObject) +{ + if(m_sceneList.find(lpObject).count() > 0) + return(true); + + if(m_rechercheList.find(lpObject).count() > 0) + return(true); + + return(false); +} + +bool cStoryBook::rechercheInUse(cRecherche* /*lpRecherche*/) +{ + return(false); +} + bool cStoryBook::fillOutlineList(QTreeView* lpView) { QMap part; @@ -854,6 +881,9 @@ bool cStoryBook::fillPlaceList(QTreeView* lpView) cPlace* lpPlace = m_placeList.at(x); QList lpItems; + if(lpPlace->deleted()) + continue; + lpItems.append(new QStandardItem(lpPlace->name())); lpItems.append(new QStandardItem(lpPlace->location())); lpItems.append(new QStandardItem(lpPlace->type())); @@ -892,6 +922,9 @@ bool cStoryBook::fillObjectList(QTreeView* lpView) cObject* lpObject = m_objectList.at(x); QList lpItems; + if(lpObject->deleted()) + continue; + lpItems.append(new QStandardItem(lpObject->name())); lpItems.append(new QStandardItem(lpObject->type())); @@ -929,6 +962,9 @@ bool cStoryBook::fillRechercheList(QTreeView* lpView) cRecherche* lpRecherche = m_rechercheList.at(x); QList lpItems; + if(lpRecherche->deleted()) + continue; + lpItems.append(new QStandardItem(lpRecherche->name())); lpItems.append(new QStandardItem(lpRecherche->link())); diff --git a/cstorybook.h b/cstorybook.h index ea13ad1..06110f7 100644 --- a/cstorybook.h +++ b/cstorybook.h @@ -286,6 +286,33 @@ public: */ bool characterInUse(cCharacter* lpCharacter); + /*! + \brief + + \fn placeInUse + \param lpPlace + \return bool + */ + bool placeInUse(cPlace* lpPlace); + + /*! + \brief + + \fn objectInUse + \param lpObject + \return bool + */ + bool objectInUse(cObject* lpObject); + + /*! + \brief + + \fn rechercheInUse + \param lpRecherche + \return bool + */ + bool rechercheInUse(cRecherche* lpRecherche); + private: QString m_szProject; /*!< TODO: describe */ bool m_bIsOpen; /*!< TODO: describe */