diff --git a/ccharacter.cpp b/ccharacter.cpp index 14563e6..942a54e 100644 --- a/ccharacter.cpp +++ b/ccharacter.cpp @@ -339,6 +339,13 @@ cTextDocument* cCharacter::description() return(m_lpDescription); } +void cCharacter::addImage(cImage* lpImage) +{ + if(m_imageList.contains(lpImage)) + return; + m_imageList.append(lpImage); +} + cCharacter* cCharacterList::add(const qint32& iID) { cCharacter* lpCharacter = find(iID); @@ -363,7 +370,7 @@ cCharacter* cCharacterList::find(const qint32& iID) return(0); } -bool cCharacterList::load() +bool cCharacterList::load(cImageList *lpImageList) { QSqlQuery query; @@ -403,6 +410,24 @@ bool cCharacterList::load() lpCharacter->setDescription(blob2TextDocument(query.value("description").toByteArray())); } + query.prepare("SELECT characterID, imageID FROM characterImage;"); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(false); + } + + while(query.next()) + { + cCharacter* lpCharacter = find(query.value("characterID").toInt()); + if(lpCharacter) + { + cImage* lpImage = lpImageList->find(query.value("imageID").toInt()); + if(lpImage) + lpCharacter->addImage(lpImage); + } + } + return(true); } diff --git a/ccharacter.h b/ccharacter.h index 51b8402..2fc9854 100644 --- a/ccharacter.h +++ b/ccharacter.h @@ -3,6 +3,7 @@ #include "ctextdocument.h" +#include "cimage.h" #include #include @@ -106,6 +107,9 @@ public: void setDescription(cTextDocument* lpDescription); cTextDocument* description(); + + void addImage(cImage* lpImage); + private: qint32 m_id; bool m_bMainCharacter; @@ -133,6 +137,7 @@ private: QString m_szSchool; QString m_szJob; cTextDocument* m_lpDescription; + QList m_imageList; signals: public slots: @@ -143,7 +148,7 @@ Q_DECLARE_METATYPE(cCharacter*) class cCharacterList : public QList { public: - bool load(); + bool load(cImageList* lpImageList); bool save(); cCharacter* add(const qint32& iID); diff --git a/cimage.cpp b/cimage.cpp new file mode 100644 index 0000000..af168f2 --- /dev/null +++ b/cimage.cpp @@ -0,0 +1,141 @@ +#include "cimage.h" + +#include "common.h" + +#include +#include + + +cImage::cImage(qint32 iID, QObject *parent) : + QObject(parent), + m_iID(iID), + m_szName(""), + m_szType(""), + m_lpDescription(0) +{ +} + +void cImage::setID(const qint32& iID) +{ + m_iID = iID; +} + +qint32 cImage::id() +{ + return(m_iID); +} + +void cImage::setName(const QString& szName) +{ + m_szName = szName; +} + +QString cImage::name() +{ + return(m_szName); +} + +void cImage::setType(const QString& szType) +{ + m_szType = szType; +} + +QString cImage::type() +{ + return(m_szType); +} + +void cImage::setDescription(cTextDocument* lpDescription) +{ + m_lpDescription = lpDescription; +} + +cTextDocument* cImage::description() +{ + return(m_lpDescription); +} + +QPixmap cImage::load() +{ + QSqlQuery query; + QPixmap image; + + query.prepare("SELECT image FROM image WHERE id = :id;"); + query.bindValue(":id", m_iID); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(image); + } + + if(!query.first()) + { + myDebug << query.lastError().text(); + return(image); + } + + QByteArray ba = query.value("image").toByteArray(); + if(!ba.isEmpty()) + { + if(!image.loadFromData(ba)) + { + myDebug << "image load error."; + return(image); + } + } + + myDebug << "image load error."; + return(image); +} + +cImage* cImageList::add(const qint32& iID) +{ + cImage* lpImage = find(iID); + + if(!lpImage) + { + lpImage = new cImage(iID); + append(lpImage); + } + + return(lpImage); +} + +cImage* cImageList::find(const qint32& iID) +{ + for(int x = 0;x < count();x++) + { + if(at(x)->id() == iID) + return(at(x)); + } + + return(0); +} + +bool cImageList::load() +{ + QSqlQuery query; + + query.prepare("SELECT id, name, type, description FROM image ORDER BY name, type;"); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(false); + } + + while(query.next()) + { + cImage* lpImage = add(query.value("id").toInt()); + + lpImage->setName(query.value("name").toString()); + lpImage->setType(query.value("type").toString()); + lpImage->setDescription(blob2TextDocument(query.value("description").toByteArray())); + } + + return(true); +} + +bool cImageList::save() +{ + return(true); +} diff --git a/cimage.h b/cimage.h new file mode 100644 index 0000000..547d99e --- /dev/null +++ b/cimage.h @@ -0,0 +1,52 @@ +#ifndef CIMAGE_H +#define CIMAGE_H + + +#include "ctextdocument.h" + +#include +#include +#include +#include + + +class cImage : public QObject +{ + Q_OBJECT +public: + cImage(qint32 iID = -1, QObject* parent = nullptr); + + void setID(const qint32& iID); + qint32 id(); + + void setName(const QString& szName); + QString name(); + + void setType(const QString& szType); + QString type(); + + void setDescription(cTextDocument* lpDescription); + cTextDocument* description(); + + QPixmap load(); + +private: + qint32 m_iID; + QString m_szName; + QString m_szType; + cTextDocument* m_lpDescription; +}; + +Q_DECLARE_METATYPE(cImage*) + +class cImageList : public QList +{ +public: + bool load(); + bool save(); + + cImage* add(const qint32& iID); + cImage* find(const qint32& iID); +}; + +#endif // CIMAGE_H diff --git a/cobject.cpp b/cobject.cpp index 1134c08..0708468 100644 --- a/cobject.cpp +++ b/cobject.cpp @@ -55,6 +55,13 @@ cTextDocument* cObject::description() return(m_lpDescription); } +void cObject::addImage(cImage* lpImage) +{ + if(m_imageList.contains(lpImage)) + return; + m_imageList.append(lpImage); +} + cObject* cObjectList::add(const qint32& iID) { cObject* lpObject = find(iID); @@ -79,7 +86,7 @@ cObject* cObjectList::find(const qint32& iID) return(0); } -bool cObjectList::load() +bool cObjectList::load(cImageList *lpImageList) { QSqlQuery query; @@ -99,6 +106,24 @@ bool cObjectList::load() lpObject->setDescription(blob2TextDocument(query.value("description").toByteArray())); } + query.prepare("SELECT objectID, imageID FROM objectImage;"); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(false); + } + + while(query.next()) + { + cObject* lpObject = find(query.value("objectID").toInt()); + if(lpObject) + { + cImage* lpImage = lpImageList->find(query.value("imageID").toInt()); + if(lpImage) + lpObject->addImage(lpImage); + } + } + return(true); } diff --git a/cobject.h b/cobject.h index c69c420..47324fd 100644 --- a/cobject.h +++ b/cobject.h @@ -3,6 +3,7 @@ #include "ctextdocument.h" +#include "cimage.h" #include #include @@ -28,11 +29,14 @@ public: void setDescription(cTextDocument* lpDescription); cTextDocument* description(); + void addImage(cImage* lpImage); + private: qint32 m_iID; QString m_szName; QString m_szType; cTextDocument* m_lpDescription; + QList m_imageList; signals: @@ -44,7 +48,7 @@ Q_DECLARE_METATYPE(cObject*) class cObjectList : public QList { public: - bool load(); + bool load(cImageList* lpImageList); bool save(); cObject* add(const qint32& iID); diff --git a/cplace.cpp b/cplace.cpp index 2f36cef..b7c7897 100644 --- a/cplace.cpp +++ b/cplace.cpp @@ -66,6 +66,13 @@ cTextDocument* cPlace::description() return(m_lpDescription); } +void cPlace::addImage(cImage* lpImage) +{ + if(m_imageList.contains(lpImage)) + return; + m_imageList.append(lpImage); +} + cPlace* cPlaceList::add(const qint32& iID) { cPlace* lpPlace = find(iID); @@ -90,7 +97,7 @@ cPlace* cPlaceList::find(const qint32& iID) return(0); } -bool cPlaceList::load() +bool cPlaceList::load(cImageList *lpImageList) { QSqlQuery query; @@ -111,6 +118,24 @@ bool cPlaceList::load() lpPlace->setDescription(blob2TextDocument(query.value("description").toByteArray())); } + query.prepare("SELECT placeID, imageID FROM placeImage;"); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(false); + } + + while(query.next()) + { + cPlace* lpPlace = find(query.value("placeID").toInt()); + if(lpPlace) + { + cImage* lpImage = lpImageList->find(query.value("imageID").toInt()); + if(lpImage) + lpPlace->addImage(lpImage); + } + } + return(true); } diff --git a/cplace.h b/cplace.h index 5ce5b3d..ff5522b 100644 --- a/cplace.h +++ b/cplace.h @@ -3,6 +3,7 @@ #include "ctextdocument.h" +#include "cimage.h" #include #include @@ -31,12 +32,15 @@ public: void setDescription(cTextDocument* lpDescription); cTextDocument* description(); + void addImage(cImage* lpImage); + private: qint32 m_iID; QString m_szName; QString m_szLocation; QString m_szType; cTextDocument* m_lpDescription; + QList m_imageList; signals: @@ -48,7 +52,7 @@ Q_DECLARE_METATYPE(cPlace*) class cPlaceList : public QList { public: - bool load(); + bool load(cImageList* lpImageList); bool save(); cPlace* add(const qint32& iID); diff --git a/crecherche.cpp b/crecherche.cpp index 8235114..93f5c37 100644 --- a/crecherche.cpp +++ b/crecherche.cpp @@ -55,6 +55,13 @@ cTextDocument* cRecherche::description() return(m_lpDescription); } +void cRecherche::addImage(cImage* lpImage) +{ + if(m_imageList.contains(lpImage)) + return; + m_imageList.append(lpImage); +} + cRecherche* cRechercheList::add(const qint32& iID) { cRecherche* lpRecherche = find(iID); @@ -79,7 +86,7 @@ cRecherche* cRechercheList::find(const qint32& iID) return(0); } -bool cRechercheList::load() +bool cRechercheList::load(cImageList *lpImageList) { QSqlQuery query; @@ -99,6 +106,24 @@ bool cRechercheList::load() lpObject->setDescription(blob2TextDocument(query.value("description").toByteArray())); } + query.prepare("SELECT rechercheID, imageID FROM rechercheImage;"); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(false); + } + + while(query.next()) + { + cRecherche* lpRecherche = find(query.value("rechercheID").toInt()); + if(lpRecherche) + { + cImage* lpImage = lpImageList->find(query.value("imageID").toInt()); + if(lpImage) + lpRecherche->addImage(lpImage); + } + } + return(true); } diff --git a/crecherche.h b/crecherche.h index d7b62b5..d3646f8 100644 --- a/crecherche.h +++ b/crecherche.h @@ -3,6 +3,7 @@ #include "ctextdocument.h" +#include "cimage.h" #include #include @@ -28,11 +29,14 @@ public: void setDescription(cTextDocument* lpDescription); cTextDocument* description(); + void addImage(cImage* lpImage); + private: qint32 m_iID; QString m_szName; QString m_szLink; cTextDocument* m_lpDescription; + QList m_imageList; signals: @@ -44,7 +48,7 @@ Q_DECLARE_METATYPE(cRecherche*) class cRechercheList : public QList { public: - bool load(); + bool load(cImageList* lpImageList); bool save(); cRecherche* add(const qint32& iID); diff --git a/cstorybook.cpp b/cstorybook.cpp index 53f9218..cde12f5 100644 --- a/cstorybook.cpp +++ b/cstorybook.cpp @@ -18,6 +18,14 @@ #include +//rechercheCharacter +//rechercheObject +//recherchePlace +//sceneCharacter +//sceneObject +//scenePlace +//config + cStoryBook::cStoryBook(const QString &szProject, QObject *parent) : QObject(parent), m_szProject(szProject), @@ -26,6 +34,9 @@ cStoryBook::cStoryBook(const QString &szProject, QObject *parent) : if(!openDatabase()) return; + if(!loadImageList()) + return; + if(!loadBook()) return; @@ -187,12 +198,6 @@ bool cStoryBook::createDatabase() ");")) return(false); - if(!createTable("CREATE TABLE characterImage ( " - " characterID INTEGER REFERENCES character (id), " - " imageID INTEGER REFERENCES image (id) " - ");")) - return(false); - if(!createTable("CREATE TABLE image ( " " id INTEGER PRIMARY KEY AUTOINCREMENT " " UNIQUE, " @@ -203,6 +208,12 @@ bool cStoryBook::createDatabase() ");")) return(false); + if(!createTable("CREATE TABLE characterImage ( " + " characterID INTEGER REFERENCES character (id), " + " imageID INTEGER REFERENCES image (id) " + ");")) + return(false); + if(!createTable("CREATE TABLE object ( " " id INTEGER PRIMARY KEY AUTOINCREMENT " " UNIQUE, " @@ -348,22 +359,27 @@ bool cStoryBook::loadSceneList() bool cStoryBook::loadCharacterList() { - return(m_characterList.load()); + return(m_characterList.load(&m_imageList)); } bool cStoryBook::loadPlaceList() { - return(m_placeList.load()); + return(m_placeList.load(&m_imageList)); } bool cStoryBook::loadObjectList() { - return(m_objectList.load()); + return(m_objectList.load(&m_imageList)); } bool cStoryBook::loadRechercheList() { - return(m_rechercheList.load()); + return(m_rechercheList.load(&m_imageList)); +} + +bool cStoryBook::loadImageList() +{ + return(m_imageList.load()); } QString cStoryBook::title() diff --git a/cstorybook.h b/cstorybook.h index 5b7760b..e177278 100644 --- a/cstorybook.h +++ b/cstorybook.h @@ -10,6 +10,7 @@ #include "cplace.h" #include "cobject.h" #include "crecherche.h" +#include "cimage.h" #include #include @@ -51,6 +52,7 @@ private: cPlaceList m_placeList; cObjectList m_objectList; cRechercheList m_rechercheList; + cImageList m_imageList; bool createDatabase(); bool updateDatabase(); @@ -64,6 +66,7 @@ private: bool loadPlaceList(); bool loadObjectList(); bool loadRechercheList(); + bool loadImageList(); }; #endif // CSTORYBOOK_H diff --git a/qtStoryWriter.pro b/qtStoryWriter.pro index dd5c1ba..8b13332 100644 --- a/qtStoryWriter.pro +++ b/qtStoryWriter.pro @@ -57,7 +57,8 @@ SOURCES += \ cpartwindow.cpp \ cplace.cpp \ cobject.cpp \ - crecherche.cpp + crecherche.cpp \ + cimage.cpp HEADERS += \ cmainwindow.h \ @@ -76,7 +77,8 @@ HEADERS += \ cpartwindow.h \ cplace.h \ cobject.h \ - crecherche.h + crecherche.h \ + cimage.h FORMS += \ cmainwindow.ui \