diff --git a/cbook.cpp b/cbook.cpp index d82d6fc..37de629 100644 --- a/cbook.cpp +++ b/cbook.cpp @@ -74,7 +74,7 @@ bool cBook::save() if(query.value("CNT").toInt() == 1) query.prepare("UPDATE book SET title = :title, subTitle = :subTitle, shortDescription = :shortDescription, description = :description, author = :author, startedAt = :startedAt, finishedAt = :finishedAt, targetDate = :targetDate;"); else - query.prepare("INSERT INTO book (title, subTitle, shortDesctiption, description, author, startedAt, finishedAt, targetDate) VALUES (:title, :subTitle, :shortDesctiption, :description, :author, :startedAt, :finishedAt, :targetDate);"); + query.prepare("INSERT INTO book (title, subTitle, shortDescription, description, author, startedAt, finishedAt, targetDate) VALUES (:title, :subTitle, :shortDescription, :description, :author, :startedAt, :finishedAt, :targetDate);"); query.bindValue(":title", title()); query.bindValue(":subTitle", subTitle()); diff --git a/cmainwindow.cpp b/cmainwindow.cpp index e437504..2dea54e 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -124,6 +124,25 @@ void cMainWindow::closeEvent(QCloseEvent *event) else settings.setValue("main/maximized", QVariant::fromValue(false)); + if(m_bSomethingChanged) + { + switch(QMessageBox::question(this, tr("Save"), m_lpStoryBook->title() + tr(" has been changed.\nDo you want to save?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel)) + { + case QMessageBox::Yes: + onFileSave(); + event->accept(); + return; + case QMessageBox::No: + event->accept(); + return; + case QMessageBox::Cancel: + event->ignore(); + return; + default: + return; + } + } + event->accept(); } @@ -595,6 +614,8 @@ void cMainWindow::updateWindowTitle() if(m_bSomethingChanged) szWindowTitle.append(" *"); + m_lpActionSave->setEnabled(m_bSomethingChanged); + setWindowTitle(szWindowTitle); } diff --git a/cstorybook.cpp b/cstorybook.cpp index 5ec2408..cc4cccc 100644 --- a/cstorybook.cpp +++ b/cstorybook.cpp @@ -23,6 +23,14 @@ #include + +cStoryBook::cStoryBook(QObject *parent) : + QObject(parent), + m_szProject(""), + m_bIsOpen(false) +{ +} + cStoryBook::cStoryBook(const QString &szProject, QObject *parent) : QObject(parent), m_szProject(szProject), @@ -69,6 +77,9 @@ cStoryBook::~cStoryBook() bool cStoryBook::save() { + if(m_szProject.isEmpty()) + return(false); + if(!m_db.isOpen()) return(false); @@ -102,16 +113,49 @@ bool cStoryBook::save() return(true); } -bool cStoryBook::openDatabase() +bool cStoryBook::saveAs(const QString& szProject) { - QFile file(m_szProject); + if(m_szProject == szProject) + return(save()); - if(!file.exists()) - { - myDebug << QObject::tr("project does not exist"); + m_szProject = szProject; + + if(m_db.isOpen()) + m_db.close(); + + if(!openDatabase()) return(false); - } + for(int x = 0;x < m_imageList.count();x++) + m_imageList.at(x)->setID(-1); + + for(int x = 0;x < m_characterList.count();x++) + m_characterList.at(x)->setID(-1); + + for(int x = 0;x < m_placeList.count();x++) + m_placeList.at(x)->setID(-1); + + for(int x = 0;x < m_objectList.count();x++) + m_objectList.at(x)->setID(-1); + + for(int x = 0;x < m_partList.count();x++) + m_partList.at(x)->setID(-1); + + for(int x = 0;x < m_chapterList.count();x++) + m_chapterList.at(x)->setID(-1); + + for(int x = 0;x < m_sceneList.count();x++) + m_sceneList.at(x)->setID(-1); + + for(int x = 0;x < m_rechercheList.count();x++) + m_rechercheList.at(x)->setID(-1); + + + return(save()); +} + +bool cStoryBook::openDatabase() +{ m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setHostName("localhost"); m_db.setDatabaseName(m_szProject); @@ -163,9 +207,9 @@ bool cStoryBook::createDatabase() QSqlQuery query; if(!createTable("CREATE TABLE config ( " - " key STRING, " - " value STRING " - ");")) + " [key] STRING, " + " value STRING " + "); ")) return(false); query.prepare("INSERT INTO config (key, value) VALUES (:key, :value);"); @@ -186,174 +230,174 @@ bool cStoryBook::createDatabase() " startedAt DATETIME, " " finishedAt DATETIME, " " targetDate DATETIME " - ");")) + "); ")) return(false); if(!createTable("CREATE TABLE chapter ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " partID INTEGER REFERENCES part (id), " - " name TEXT, " - " sortOrder INTEGER, " - " description BLOB, " - " text BLOB " - ");")) + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " partID INTEGER REFERENCES part (id), " + " name TEXT, " + " sortOrder INTEGER, " + " description BLOB, " + " text BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE character ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " mainCharacter BOOLEAN, " - " gender INTEGER, " - " title TEXT, " - " firstName TEXT, " - " middleName TEXT, " - " lastName TEXT, " - " nickName TEXT, " - " height DOUBLE, " - " weight DOUBLE, " - " age DOUBLE, " - " dateOfBirth DATE, " - " placeOfBirth TEXT, " - " dateOfDeath DATE, " - " placeOfDeath TEXT, " - " hairColor TEXT, " - " hairCut TEXT, " - " hairLength TEXT, " - " figure TEXT, " - " nature TEXT, " - " spokenLanguages TEXT, " - " skin TEXT, " - " school TEXT, " - " job TEXT, " - " description BLOB " - ");")) - return(false); - - if(!createTable("CREATE TABLE image ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " type TEXT, " - " name TEXT, " - " description BLOB, " - " image BLOB " - ");")) + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " mainCharacter BOOLEAN, " + " creature TEXT, " + " gender INTEGER, " + " title TEXT, " + " firstName TEXT, " + " middleName TEXT, " + " lastName TEXT, " + " nickName TEXT, " + " height DOUBLE, " + " weight DOUBLE, " + " dateOfBirth DATE, " + " placeOfBirth TEXT, " + " dateOfDeath DATE, " + " placeOfDeath TEXT, " + " hairColor TEXT, " + " hairCut TEXT, " + " hairLength TEXT, " + " figure TEXT, " + " nature TEXT, " + " spokenLanguages TEXT, " + " skin TEXT, " + " school TEXT, " + " job TEXT, " + " description BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE characterImage ( " - " characterID INTEGER REFERENCES character (id), " - " imageID INTEGER REFERENCES image (id) " - ");")) + " characterID INTEGER REFERENCES character (id), " + " imageID INTEGER REFERENCES image (id) " + "); ")) + return(false); + + if(!createTable("CREATE TABLE image ( " + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " type TEXT, " + " name TEXT, " + " description BLOB, " + " image BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE object ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " name TEXT, " - " type TEXT, " - " description BLOB " - ");")) + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " name TEXT, " + " type TEXT, " + " description BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE objectImage ( " - " objectID INTEGER REFERENCES object (id), " - " imageID INTEGER REFERENCES image (id) " - ");")) + " objectID INTEGER REFERENCES object (id), " + " imageID INTEGER REFERENCES image (id) " + "); ")) return(false); if(!createTable("CREATE TABLE part ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " name TEXT, " - " [order] INTEGER, " - " description BLOB, " - " text BLOB " - ");")) + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " name TEXT, " + " sortOrder INTEGER, " + " description BLOB, " + " text BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE place ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " name TEXT, " - " location TEXT, " - " type TEXT, " - " description BLOB " - ");")) + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " name TEXT, " + " location TEXT, " + " type TEXT, " + " description BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE placeImage ( " - " placeID INTEGER REFERENCES place (id), " - " imageID INTEGER REFERENCES image (id) " - ");")) + " placeID INTEGER REFERENCES place (id), " + " imageID INTEGER REFERENCES image (id) " + "); ")) return(false); if(!createTable("CREATE TABLE recherche ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " name TEXT, " - " description BLOB, " - " link TEXT " - ");")) + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " name TEXT, " + " description BLOB, " + " link TEXT " + "); ")) return(false); if(!createTable("CREATE TABLE rechercheCharacter ( " - " rechercheID INTEGER REFERENCES recherche (id), " - " characterID INTEGER REFERENCES character (id) " - ");")) + " rechercheID INTEGER REFERENCES recherche (id), " + " characterID INTEGER REFERENCES character (id) " + "); ")) return(false); if(!createTable("CREATE TABLE rechercheImage ( " - " rechercheID INTEGER REFERENCES recherche (id), " - " imageID INTEGER REFERENCES image (id) " - ");")) + " rechercheID INTEGER REFERENCES recherche (id), " + " imageID INTEGER REFERENCES image (id) " + "); ")) return(false); if(!createTable("CREATE TABLE rechercheObject ( " - " rechercheID INTEGER REFERENCES recherche (id), " - " objectID INTEGER REFERENCES object (id) " - ");")) + " rechercheID INTEGER REFERENCES recherche (id), " + " objectID INTEGER REFERENCES object (id) " + "); ")) return(false); if(!createTable("CREATE TABLE recherchePlace ( " - " rechercheID INTEGER REFERENCES recherche (id), " - " placeID INTEGER REFERENCES place (id) " - ");")) + " rechercheID INTEGER REFERENCES recherche (id), " + " placeID INTEGER REFERENCES place (id) " + "); ")) return(false); if(!createTable("CREATE TABLE scene ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT " - " UNIQUE, " - " chapterID INTEGER REFERENCES chapter (id), " - " name TEXT, " - " [order] INTEGER, " - " description TEXT, " - " state INTEGER, " - " startedAt DATETIME, " - " finishedAt DATETIME, " - " targetDate DATETIME, " - " text BLOB " - ");")) + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " chapterID INTEGER REFERENCES chapter (id), " + " name TEXT, " + " sortOrder INTEGER, " + " description BLOB, " + " state INTEGER, " + " startedAt DATETIME, " + " finishedAt DATETIME, " + " targetDate DATETIME, " + " text BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE sceneCharacter ( " - " sceneID INTEGER REFERENCES scene (id), " - " characterID INTEGER REFERENCES character (id), " - " description BLOB " - ");")) + " sceneID INTEGER REFERENCES scene (id), " + " characterID INTEGER REFERENCES character (id), " + " description BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE sceneObject ( " - " sceneID INTEGER REFERENCES scene (id), " - " objectID INTEGER REFERENCES object (id), " - " description BLOB " - ");")) + " sceneID INTEGER REFERENCES scene (id), " + " objectID INTEGER REFERENCES object (id), " + " description BLOB " + "); ")) return(false); if(!createTable("CREATE TABLE scenePlace ( " - " sceneID INTEGER REFERENCES scene (id), " - " placeID INTEGER REFERENCES place (id), " - " description BLOB " - ");")) + " sceneID INTEGER REFERENCES scene (id), " + " placeID INTEGER REFERENCES place (id), " + " description BLOB " + "); ")) return(false); return(true); diff --git a/cstorybook.h b/cstorybook.h index a2cbed3..ec525c9 100644 --- a/cstorybook.h +++ b/cstorybook.h @@ -35,6 +35,13 @@ class cStoryBook : public QObject { Q_OBJECT public: + /*! + \brief + + \fn cStoryBook + \param parent + */ + explicit cStoryBook(QObject *parent = nullptr); /*! \brief @@ -50,7 +57,21 @@ public: */ ~cStoryBook(); + /*! + \brief + + \fn save + \return bool + */ bool save(); + /*! + \brief + + \fn saveAs + \param szProject + \return bool + */ + bool saveAs(const QString& szProject); /*! \brief