From bc9a5e33f0e624fbb792e3a40136e90945760512 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Wed, 23 May 2018 11:19:26 +0200 Subject: [PATCH] Implementation of character deletion. --- ccharacter.cpp | 29 +++++++- ccharacter.h | 184 ++++++++++++++++++++++++++---------------------- cmainwindow.cpp | 43 ++++++++++- crecherche.cpp | 20 ++++++ crecherche.h | 9 +++ cscene.cpp | 20 ++++++ cscene.h | 9 +++ cstorybook.cpp | 14 ++++ cstorybook.h | 9 +++ 9 files changed, 248 insertions(+), 89 deletions(-) diff --git a/ccharacter.cpp b/ccharacter.cpp index 69fcfe1..4260fd9 100644 --- a/ccharacter.cpp +++ b/ccharacter.cpp @@ -38,7 +38,8 @@ cCharacter::cCharacter(qint32 iID, QObject *parent) : m_szSkin(QString("")), m_szSchool(QString("")), m_szJob(QString("")), - m_lpDescription(0) + m_lpDescription(0), + m_bDeleted(false) { } @@ -344,6 +345,17 @@ cTextDocument* cCharacter::description() return(m_lpDescription); } + +void cCharacter::setDeleted(bool bDeleted) +{ + m_bDeleted = bDeleted; +} + +bool cCharacter::deleted() +{ + return(m_bDeleted); +} + void cCharacter::addImage(cImage* lpImage, cTextDocument* lpDescription) { m_imageList.append(new cImageDescription(lpImage, lpDescription)); @@ -475,10 +487,12 @@ bool cCharacterList::save() QSqlQuery queryUpdate; QSqlQuery queryInsert; QSqlQuery querySelect; + QSqlQuery queryDelete; queryUpdate.prepare("UPDATE character SET mainCharacter=:mainCharacter, creature=:creature, gender=:gender, title=:title, firstName=:firstName, middleName=:middleName, lastName=:lastName, nickName=:nickName, height=:height, weight=:weight, dateOfBirth=:dateOfBirth, dateOfDeath=:dateOfDeath, placeOfBirth=:placeOfBirth, placeOfDeath=:placeOfDeath, hairColor=:hairColor, hairCut=:hairCut, hairLength=:hairLength, figure=:figure, nature=:nature, spokenLanguages=:spokenLanguages, skin=:skin, school=:school, job=:job, description=:description WHERE id=:id;"); queryInsert.prepare("INSERT INTO character (mainCharacter, creature, gender, title, firstName, middleName, lastName, nickName, height, weight, dateOfBirth, dateOfDeath, placeOfBirth, placeOfDeath, hairColor, hairCut, hairLength, figure, nature, spokenLanguages, skin, school, job, description) VALUES (:mainCharacter, :creature, :gender, :title, :firstName, :middleName, :lastName, :nickName, :height, :weight,:dateOfBirth, :dateOfDeath, :placeOfBirth, :placeOfDeath, :hairColor, :hairCut, :hairLength, :figure, :nature, :spokenLanguages, :skin, :school, :job, :description);"); querySelect.prepare("SELECT id FROM character WHERE _rowid_=(SELECT MAX(_rowid_) FROM character);"); + queryDelete.prepare("DELETE FROM character WHERE id=:id;"); QSqlQuery imageDelete; QSqlQuery imageAdd; @@ -490,7 +504,18 @@ bool cCharacterList::save() { cCharacter* lpCharacter = at(x); - if(lpCharacter->id() != -1) + if(lpCharacter->deleted()) + { + queryDelete.bindValue(":id", lpCharacter->id()); + + if(!queryDelete.exec()) + { + myDebug << queryDelete.lastError().text(); + return(false); + } + this->removeOne(lpCharacter); + } + else if(lpCharacter->id() != -1) { queryUpdate.bindValue(":id", lpCharacter->id()); queryUpdate.bindValue(":mainCharacter", lpCharacter->mainCharacter()); diff --git a/ccharacter.h b/ccharacter.h index 17f7af4..0efc360 100644 --- a/ccharacter.h +++ b/ccharacter.h @@ -54,14 +54,14 @@ public: \fn setID \param iID */ - void setID(const qint32& iID); + void setID(const qint32& iID); /*! \brief \fn id \return qint32 */ - qint32 id(); + qint32 id(); /*! \brief @@ -69,14 +69,14 @@ public: \fn setMainCharacter \param bMainCharacter */ - void setMainCharacter(bool bMainCharacter); + void setMainCharacter(bool bMainCharacter); /*! \brief \fn mainCharacter \return bool */ - bool mainCharacter(); + bool mainCharacter(); /*! \brief @@ -84,14 +84,14 @@ public: \fn setCreature \param szCreature */ - void setCreature(const QString& szCreature); + void setCreature(const QString& szCreature); /*! \brief \fn creature \return QString */ - QString creature(); + QString creature(); /*! \brief @@ -99,21 +99,21 @@ public: \fn setGender \param gender */ - void setGender(GENDER gender); + void setGender(GENDER gender); /*! \brief \fn gender \return GENDER */ - GENDER gender(); + GENDER gender(); /*! \brief \fn genderText \return QString */ - QString genderText(); + QString genderText(); /*! \brief @@ -121,7 +121,7 @@ public: \param gender \return QString */ - QString genderText(GENDER gender) const; + QString genderText(GENDER gender) const; /*! \brief @@ -129,14 +129,14 @@ public: \fn setTitle \param szTitle */ - void setTitle(const QString& szTitle); + void setTitle(const QString& szTitle); /*! \brief \fn title \return QString */ - QString title(); + QString title(); /*! \brief @@ -144,14 +144,14 @@ public: \fn setFirstName \param szFirstName */ - void setFirstName(const QString& szFirstName); + void setFirstName(const QString& szFirstName); /*! \brief \fn firstName \return QString */ - QString firstName(); + QString firstName(); /*! \brief @@ -159,14 +159,14 @@ public: \fn setMiddleName \param szMiddleName */ - void setMiddleName(const QString& szMiddleName); + void setMiddleName(const QString& szMiddleName); /*! \brief \fn middleName \return QString */ - QString middleName(); + QString middleName(); /*! \brief @@ -174,14 +174,14 @@ public: \fn setLastName \param szLastName */ - void setLastName(const QString& szLastName); + void setLastName(const QString& szLastName); /*! \brief \fn lastName \return QString */ - QString lastName(); + QString lastName(); /*! \brief @@ -189,7 +189,7 @@ public: \fn name \return QString */ - QString name(); + QString name(); /*! \brief @@ -197,14 +197,14 @@ public: \fn setNickName \param szNickName */ - void setNickName(const QString& szNickName); + void setNickName(const QString& szNickName); /*! \brief \fn nickName \return QString */ - QString nickName(); + QString nickName(); /*! \brief @@ -212,14 +212,14 @@ public: \fn setHeight \param dHeight */ - void setHeight(qreal dHeight); + void setHeight(qreal dHeight); /*! \brief \fn height \return qreal */ - qreal height(); + qreal height(); /*! \brief @@ -227,14 +227,14 @@ public: \fn setWeight \param dWeight */ - void setWeight(qreal dWeight); + void setWeight(qreal dWeight); /*! \brief \fn weight \return qreal */ - qreal weight(); + qreal weight(); /*! \brief @@ -242,14 +242,14 @@ public: \fn setAge \param dAge */ - void setAge(qreal dAge); + void setAge(qreal dAge); /*! \brief \fn age \return qreal */ - qreal age(); + qreal age(); /*! \brief @@ -257,14 +257,14 @@ public: \fn setDateOfBirth \param dateOfBirth */ - void setDateOfBirth(const QDate& dateOfBirth); + void setDateOfBirth(const QDate& dateOfBirth); /*! \brief \fn dateOfBirth \return QDate */ - QDate dateOfBirth(); + QDate dateOfBirth(); /*! \brief @@ -272,14 +272,14 @@ public: \fn setPlaceOfBirth \param szPlaceOfBirth */ - void setPlaceOfBirth(const QString& szPlaceOfBirth); + void setPlaceOfBirth(const QString& szPlaceOfBirth); /*! \brief \fn placeOfBirth \return QString */ - QString placeOfBirth(); + QString placeOfBirth(); /*! \brief @@ -287,14 +287,14 @@ public: \fn setDateOfDeath \param dateOfDeath */ - void setDateOfDeath(const QDate& dateOfDeath); + void setDateOfDeath(const QDate& dateOfDeath); /*! \brief \fn dateOfDeath \return QDate */ - QDate dateOfDeath(); + QDate dateOfDeath(); /*! \brief @@ -302,14 +302,14 @@ public: \fn setPlaceOfDeath \param szPlaceOfDeath */ - void setPlaceOfDeath(const QString& szPlaceOfDeath); + void setPlaceOfDeath(const QString& szPlaceOfDeath); /*! \brief \fn placeOfDeath \return QString */ - QString placeOfDeath(); + QString placeOfDeath(); /*! \brief @@ -317,14 +317,14 @@ public: \fn setHairColor \param szHairColor */ - void setHairColor(const QString& szHairColor); + void setHairColor(const QString& szHairColor); /*! \brief \fn hairColor \return QString */ - QString hairColor(); + QString hairColor(); /*! \brief @@ -332,14 +332,14 @@ public: \fn setHairCut \param szHairCut */ - void setHairCut(const QString& szHairCut); + void setHairCut(const QString& szHairCut); /*! \brief \fn hairCut \return QString */ - QString hairCut(); + QString hairCut(); /*! \brief @@ -347,14 +347,14 @@ public: \fn setHairLength \param szHairLength */ - void setHairLength(const QString& szHairLength); + void setHairLength(const QString& szHairLength); /*! \brief \fn hairLength \return QString */ - QString hairLength(); + QString hairLength(); /*! \brief @@ -362,14 +362,14 @@ public: \fn setFigure \param szFigure */ - void setFigure(const QString& szFigure); + void setFigure(const QString& szFigure); /*! \brief \fn figure \return QString */ - QString figure(); + QString figure(); /*! \brief @@ -377,14 +377,14 @@ public: \fn setNature \param szNature */ - void setNature(const QString& szNature); + void setNature(const QString& szNature); /*! \brief \fn nature \return QString */ - QString nature(); + QString nature(); /*! \brief @@ -392,14 +392,14 @@ public: \fn setSpokenLanguages \param szSpokenLanguages */ - void setSpokenLanguages(const QString& szSpokenLanguages); + void setSpokenLanguages(const QString& szSpokenLanguages); /*! \brief \fn spokenLanguages \return QString */ - QString spokenLanguages(); + QString spokenLanguages(); /*! \brief @@ -407,14 +407,14 @@ public: \fn setSkin \param szSkin */ - void setSkin(const QString& szSkin); + void setSkin(const QString& szSkin); /*! \brief \fn skin \return QString */ - QString skin(); + QString skin(); /*! \brief @@ -422,14 +422,14 @@ public: \fn setSchool \param szSchool */ - void setSchool(const QString& szSchool); + void setSchool(const QString& szSchool); /*! \brief \fn school \return QString */ - QString school(); + QString school(); /*! \brief @@ -437,14 +437,14 @@ public: \fn setJob \param szJob */ - void setJob(const QString& szJob); + void setJob(const QString& szJob); /*! \brief \fn job \return QString */ - QString job(); + QString job(); /*! \brief @@ -452,14 +452,14 @@ public: \fn setDescription \param lpDescription */ - void setDescription(cTextDocument* lpDescription); + void setDescription(cTextDocument* lpDescription); /*! \brief \fn description \return cTextDocument */ - cTextDocument* description(); + cTextDocument* description(); /*! \brief @@ -467,42 +467,58 @@ public: \fn addImage \param lpImage */ - void addImage(cImage* lpImage, cTextDocument* lpDescription); + void addImage(cImage* lpImage, cTextDocument* lpDescription); /*! \brief \fn images \return QList */ - QList images(); + QList images(); + + /*! + \brief + + \fn setDeleted + \param bDeleted + */ + void setDeleted(bool bDeleted); + /*! + \brief + + \fn deleted + \return bool + */ + bool deleted(); private: - qint32 m_id; /*!< TODO: describe */ - bool m_bMainCharacter; /*!< TODO: describe */ - QString m_szCreature; /*!< TODO: describe */ - GENDER m_gender; /*!< TODO: describe */ - QString m_szTitle; /*!< TODO: describe */ - QString m_szFirstName; /*!< TODO: describe */ - QString m_szMiddleName; /*!< TODO: describe */ - QString m_szLastName; /*!< TODO: describe */ - QString m_szNickName; /*!< TODO: describe */ - qreal m_dHeight; /*!< TODO: describe */ - qreal m_dWeight; /*!< TODO: describe */ - qreal m_dAge; /*!< TODO: describe */ - QDate m_dateOfBirth; /*!< TODO: describe */ - QString m_szPlaceOfBirth; /*!< TODO: describe */ - QDate m_dateOfDeath; /*!< TODO: describe */ - QString m_szPlaceOfDeath; /*!< TODO: describe */ - QString m_szHairColor; /*!< TODO: describe */ - QString m_szHairCut; /*!< TODO: describe */ - QString m_szHairLength; /*!< TODO: describe */ - QString m_szFigure; /*!< TODO: describe */ - QString m_szNature; /*!< TODO: describe */ - QString m_szSpokenLanguages; /*!< TODO: describe */ - QString m_szSkin; /*!< TODO: describe */ - QString m_szSchool; /*!< TODO: describe */ - QString m_szJob; /*!< TODO: describe */ - cTextDocument* m_lpDescription; /*!< TODO: describe */ - QList m_imageList; /*!< TODO: describe */ + qint32 m_id; /*!< TODO: describe */ + bool m_bMainCharacter; /*!< TODO: describe */ + QString m_szCreature; /*!< TODO: describe */ + GENDER m_gender; /*!< TODO: describe */ + QString m_szTitle; /*!< TODO: describe */ + QString m_szFirstName; /*!< TODO: describe */ + QString m_szMiddleName; /*!< TODO: describe */ + QString m_szLastName; /*!< TODO: describe */ + QString m_szNickName; /*!< TODO: describe */ + qreal m_dHeight; /*!< TODO: describe */ + qreal m_dWeight; /*!< TODO: describe */ + qreal m_dAge; /*!< TODO: describe */ + QDate m_dateOfBirth; /*!< TODO: describe */ + QString m_szPlaceOfBirth; /*!< TODO: describe */ + QDate m_dateOfDeath; /*!< TODO: describe */ + QString m_szPlaceOfDeath; /*!< TODO: describe */ + QString m_szHairColor; /*!< TODO: describe */ + QString m_szHairCut; /*!< TODO: describe */ + QString m_szHairLength; /*!< TODO: describe */ + QString m_szFigure; /*!< TODO: describe */ + QString m_szNature; /*!< TODO: describe */ + QString m_szSpokenLanguages; /*!< TODO: describe */ + QString m_szSkin; /*!< TODO: describe */ + QString m_szSchool; /*!< TODO: describe */ + QString m_szJob; /*!< TODO: describe */ + cTextDocument* m_lpDescription; /*!< TODO: describe */ + QList m_imageList; /*!< TODO: describe */ + bool m_bDeleted; /*!< TODO: describe */ signals: public slots: diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 3120f8e..0d99742 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -1440,7 +1440,7 @@ void cMainWindow::onDeletePart() } - if(QMessageBox::question(this, "Delete Part", tr("Are you sure you want to delete this part:
") + "
" + lpPart->name() + "
") != QMessageBox::Yes) + if(QMessageBox::question(this, "Delete Part", tr("Are you sure you want to delete this part:
") + "" + lpPart->name() + "") != QMessageBox::Yes) return; lpPart->setDeleted(true); @@ -1542,7 +1542,7 @@ void cMainWindow::onDeleteChapter() } - if(QMessageBox::question(this, "Delete Chapter", tr("Are you sure you want to delete this chapter:
") + "
" + lpChapter->name() + "
" + tr("
from part
") + "
" + lpChapter->part()->name() + "
") != QMessageBox::Yes) + if(QMessageBox::question(this, "Delete Chapter", tr("Are you sure you want to delete this chapter:
") + "" + lpChapter->name() + "" + tr("
from part
") + "" + lpChapter->part()->name() + "") != QMessageBox::Yes) return; lpChapter->setDeleted(true); @@ -1643,7 +1643,7 @@ void cMainWindow::onDeleteScene() if(!lpScene) return; - if(QMessageBox::question(this, "Delete Scene", tr("Are you sure you want to delete this scene:
") + "
" + lpScene->name() + "
" + tr("
from chapter
") + "
" + lpScene->chapter()->name() + "
") != QMessageBox::Yes) + if(QMessageBox::question(this, "Delete Scene", tr("Are you sure you want to delete this scene:
") + "" + lpScene->name() + "" + tr("
from chapter
") + "" + lpScene->chapter()->name() + "") != QMessageBox::Yes) return; lpScene->setDeleted(true); @@ -1716,6 +1716,43 @@ void cMainWindow::onEditCharacter() void cMainWindow::onDeleteCharacter() { + QStandardItem* lpItem = m_lpCharacterModel->itemFromIndex(ui->m_lpCharacterList->currentIndex()); + if(!lpItem) + return; + + cCharacter* lpCharacter = qvariant_cast(lpItem->data()); + + if(!lpCharacter) + return; + + if(m_lpStoryBook->characterInUse(lpCharacter)) + { + QMessageBox::critical(this, "Delete Character", tr("This character is still in use.\nPlease delete the usage before deleting the character.")); + return; + + } + + if(QMessageBox::question(this, "Delete Character", tr("Are you sure you want to delete this character:
") + "" + lpCharacter->name() + "?") != QMessageBox::Yes) + return; + + lpCharacter->setDeleted(true); + m_lpStoryBook->fillCharacterList(ui->m_lpCharacterList); + 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_character) + { + cCharacterWindow* lpCharacterWindow = (cCharacterWindow*)lpWidget->widget(); + if(lpCharacterWindow->character() == lpCharacter) + { + onMainTabTabCloseRequested(x); + return; + } + } + } } void cMainWindow::onAddPlace() diff --git a/crecherche.cpp b/crecherche.cpp index 768a14a..07431c3 100644 --- a/crecherche.cpp +++ b/crecherche.cpp @@ -127,6 +127,26 @@ cRecherche* cRechercheList::find(const qint32& iID) return(0); } +QList cRechercheList::find(cCharacter* lpCharacter) +{ + QList rechercheList; + + for(int x = 0;x < count();x++) + { + QList list = at(x)->characterList(); + + for(int y = 0;y < list.count();y++) + { + if(list.at(y)->character() == lpCharacter) + { + rechercheList.append(at(x)); + break; + } + } + } + return(rechercheList); +} + bool cRechercheList::load(cImageList *lpImageList, cCharacterList *lpCharacterList, cObjectList *lpObjectList, cPlaceList *lpPlaceList) { QSqlQuery query; diff --git a/crecherche.h b/crecherche.h index 0a43ba0..b40aa33 100644 --- a/crecherche.h +++ b/crecherche.h @@ -219,6 +219,15 @@ public: \return cRecherche */ cRecherche* find(const qint32& iID); + + /*! + \brief + + \fn find + \param lpCharacter + \return QList + */ + QList find(cCharacter* lpCharacter); }; #endif // CRECHERCHE_H diff --git a/cscene.cpp b/cscene.cpp index 9e9bf43..c9b1614 100644 --- a/cscene.cpp +++ b/cscene.cpp @@ -276,6 +276,26 @@ QList cSceneList::find(cChapter* lpChapter) return(sceneList); } +QList cSceneList::find(cCharacter* lpCharacter) +{ + QList sceneList; + + for(int x = 0;x < count();x++) + { + QList list = at(x)->characterList(); + + for(int y = 0;y < list.count();y++) + { + if(list.at(y)->character() == lpCharacter) + { + sceneList.append(at(x)); + break; + } + } + } + return(sceneList); +} + qint32 cSceneList::nextSort(cChapter* lpChapter) { qint32 iSort = -1; diff --git a/cscene.h b/cscene.h index 4bc0212..26aeee8 100644 --- a/cscene.h +++ b/cscene.h @@ -401,6 +401,15 @@ public: */ cScene* find(const qint32& iID); + /*! + \brief + + \fn find + \param lpCharacter + \return QList + */ + QList find(cCharacter* lpCharacter); + /*! \brief diff --git a/cstorybook.cpp b/cstorybook.cpp index 11e994e..f534e00 100644 --- a/cstorybook.cpp +++ b/cstorybook.cpp @@ -666,6 +666,17 @@ bool cStoryBook::hasScene(cChapter* lpChapter) return(m_sceneList.find(lpChapter).count() > 0); } +bool cStoryBook::characterInUse(cCharacter* lpCharacter) +{ + if(m_sceneList.find(lpCharacter).count() > 0) + return(true); + + if(m_rechercheList.find(lpCharacter).count() > 0) + return(true); + + return(false); +} + bool cStoryBook::fillOutlineList(QTreeView* lpView) { QMap part; @@ -797,6 +808,9 @@ bool cStoryBook::fillCharacterList(QTreeView* lpView) cCharacter* lpCharacter = m_characterList.at(x); QList lpItems; + if(lpCharacter->deleted()) + continue; + lpItems.append(new QStandardItem(lpCharacter->name())); lpItems.append(new QStandardItem(lpCharacter->creature())); lpItems.append(new QStandardItem(lpCharacter->genderText())); diff --git a/cstorybook.h b/cstorybook.h index 6eb50e6..ea13ad1 100644 --- a/cstorybook.h +++ b/cstorybook.h @@ -277,6 +277,15 @@ public: */ bool hasScene(cChapter* lpChapter); + /*! + \brief + + \fn characterInUse + \param lpCharacter + \return bool + */ + bool characterInUse(cCharacter* lpCharacter); + private: QString m_szProject; /*!< TODO: describe */ bool m_bIsOpen; /*!< TODO: describe */