diff --git a/cbook.cpp b/cbook.cpp new file mode 100644 index 0000000..5495b08 --- /dev/null +++ b/cbook.cpp @@ -0,0 +1,94 @@ +#include "cbook.h" + + +cBook::cBook(const QString &szTitle) : + m_szTitle(szTitle), + m_szSubtitle(""), + m_szShortDescription(""), + m_szDescription(""), + m_szAuthor(""), + m_startedAt(QDate(1, 1, 1)), + m_finishedAt(QDate(1, 1, 1)), + m_targetDate(QDate(1, 1, 1)) +{ +} + +void cBook::setTitle(const QString& szTitle) +{ + m_szTitle = szTitle; +} + +QString cBook::title() +{ + return(m_szTitle); +} + +void cBook::setSubTitle(const QString& szSubTitle) +{ + m_szSubtitle = szSubTitle; +} + +QString cBook::subTitle() +{ + return(m_szSubtitle); +} + +void cBook::setShortDescription(const QString& szShortDescription) +{ + m_szShortDescription = szShortDescription; +} + +QString cBook::shortDescription() +{ + return(m_szShortDescription); +} + +void cBook::setDescription(const QString& szDescription) +{ + m_szDescription = szDescription; +} + +QString cBook::description() +{ + return(m_szDescription); +} + +void cBook::setAuthor(const QString& szAuthor) +{ + m_szAuthor = szAuthor; +} + +QString cBook::author() +{ + return(m_szAuthor); +} + +void cBook::setStartedAt(const QDate& startedAt) +{ + m_startedAt = startedAt; +} + +QDate cBook::startedAt() +{ + return(m_startedAt); +} + +void cBook::setFinishedAt(const QDate& finishedAt) +{ + m_finishedAt = finishedAt; + +} +QDate cBook::finishedAt() +{ + return(m_finishedAt); +} + +void cBook::setTargetDate(const QDate& targetDate) +{ + m_targetDate = targetDate; +} + +QDate cBook::targetDate() +{ + return(m_targetDate); +} diff --git a/cbook.h b/cbook.h new file mode 100644 index 0000000..161bfe6 --- /dev/null +++ b/cbook.h @@ -0,0 +1,48 @@ +#ifndef CBOOK_H +#define CBOOK_H + + +#include +#include + + +class cBook +{ +public: + cBook(const QString& szTitle = ""); + + void setTitle(const QString& szTitle); + QString title(); + + void setSubTitle(const QString& szSubTitle); + QString subTitle(); + + void setShortDescription(const QString& szShortDescription); + QString shortDescription(); + + void setDescription(const QString& szDescription); + QString description(); + + void setAuthor(const QString& szAuthor); + QString author(); + + void setStartedAt(const QDate& startedAt); + QDate startedAt(); + + void setFinishedAt(const QDate& finishedAt); + QDate finishedAt(); + + void setTargetDate(const QDate& targetDate); + QDate targetDate(); +private: + QString m_szTitle; + QString m_szSubtitle; + QString m_szShortDescription; + QString m_szDescription; + QString m_szAuthor; + QDate m_startedAt; + QDate m_finishedAt; + QDate m_targetDate; +}; + +#endif // CBOOK_H diff --git a/cmainwindow.cpp b/cmainwindow.cpp index b36f8c4..2f3089a 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -15,9 +15,13 @@ cMainWindow::cMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::cMainWindow), - m_bUpdatingTab(false) + m_bUpdatingTab(false), + m_lpStoryBook(0) { - ui->setupUi(this); + initUI(); + createActions(); + + m_lpStoryBook = new cStoryBook("c:/temp/qtStoryWriter/DerAdler"); cStructureWindow* lpStructureWindow = new cStructureWindow(this); cWidget* lpWidget1 = new cWidget(lpStructureWindow); @@ -30,42 +34,31 @@ cMainWindow::cMainWindow(QWidget *parent) : ui->m_lpMainTab->addTab((QWidget*)lpWidget1, "Structure"); ui->m_lpMainTab->addTab((QWidget*)lpWidget2, "Bla"); - -// g_lpInputDocument = new cTextDocument(ui->m_lpInput); -// g_lpOutputDocument = new cTextDocument(ui->m_lpOutput); - -// ui->m_lpInput->setDocument(g_lpInputDocument); } cMainWindow::~cMainWindow() { + if(m_lpStoryBook) + delete m_lpStoryBook; + delete ui; } -//void cMainWindow::on_actionSave_triggered() -//{ -//#ifdef __linux__ -// #ifdef WITH_ZIP -// g_lpInputDocument->saveAs("/tmp/qtStoryWriter/documentText.zip", false); -// cDocumentReader reader("/tmp/qtStoryWriter/documentText.zip", false); -// #else -// g_lpInputDocument->saveAs("/tmp/qtStoryWriter/documentText.xml", false); -// cDocumentReader reader("/tmp/qtStoryWriter/documentText.xml", false); -// #endif -//#elif _WIN32 -// #ifdef WITH_ZIP -// g_lpInputDocument->saveAs("C:/Temp/qtStoryWriter/documentText.zip", false); -// cDocumentReader reader("C:/Temp/qtStoryWriter/documentText.zip", false); -// #else -// g_lpInputDocument->saveAs("C:/Temp/qtStoryWriter/documentText.xml", false); -// cDocumentReader reader("C:/Temp/qtStoryWriter/documentText.xml", false); -// #endif -// g_lpOutputDocument = reader.readDocument(); -// ui->m_lpOutput->setDocument(g_lpOutputDocument); -//#endif -//} +void cMainWindow::initUI() +{ + ui->setupUi(this); -void cMainWindow::on_m_lpMainTab_currentChanged(int index) + ui->m_lpMainToolBox->setCurrentIndex(0); +} + +void cMainWindow::createActions() +{ + connect(ui->m_lpMainTab, SIGNAL(currentChanged(int)), this, SLOT(onMainTabCurrentChanged(int))); + connect(ui->m_lpMainTab, SIGNAL(tabCloseRequested(int)), this,SLOT(onMainTabTabCloseRequested(int))); + connect(ui->m_lpMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(onMdiAreaSubWindowActivated(QMdiSubWindow*))); +} + +void cMainWindow::onMainTabCurrentChanged(int index) { if(m_bUpdatingTab) return; @@ -77,7 +70,21 @@ void cMainWindow::on_m_lpMainTab_currentChanged(int index) m_bUpdatingTab = false; } -void cMainWindow::on_m_lpMdiArea_subWindowActivated(QMdiSubWindow *arg1) +void cMainWindow::onMainTabTabCloseRequested(int index) +{ + if(m_bUpdatingTab) + return; + + m_bUpdatingTab = true; + cWidget* lpWidget = (cWidget*)ui->m_lpMainTab->currentWidget(); + QMdiSubWindow* lpWindow = lpWidget->window(); + ui->m_lpMainTab->removeTab(index); + ui->m_lpMdiArea->removeSubWindow(lpWindow); + delete(lpWidget); + m_bUpdatingTab = false; +} + +void cMainWindow::onMdiAreaSubWindowActivated(QMdiSubWindow *arg1) { if(m_bUpdatingTab) return; diff --git a/cmainwindow.h b/cmainwindow.h index 2ca6433..2ee6c20 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -1,6 +1,9 @@ #ifndef CMAINWINDOW_H #define CMAINWINDOW_H + +#include "cstorybook.h" + #include #include @@ -18,14 +21,17 @@ public: ~cMainWindow(); private slots: - - void on_m_lpMainTab_currentChanged(int index); - void on_m_lpMdiArea_subWindowActivated(QMdiSubWindow *arg1); + void onMainTabCurrentChanged(int index); + void onMainTabTabCloseRequested(int index); + void onMdiAreaSubWindowActivated(QMdiSubWindow *arg1); private: Ui::cMainWindow* ui; - bool m_bUpdatingTab; + cStoryBook* m_lpStoryBook; + + void initUI(); + void createActions(); }; #endif // CMAINWINDOW_H diff --git a/cmainwindow.ui b/cmainwindow.ui index 2513963..e119b24 100644 --- a/cmainwindow.ui +++ b/cmainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 569 - 460 + 869 + 660 @@ -15,50 +15,115 @@ - - - - - - -1 - - - true - - - true - - - true + + + + Qt::Horizontal + + + + 3 + + + + + 0 + 0 + 69 + 481 + + + Outline + - - - - - Qt::ScrollBarAsNeeded - - - Qt::ScrollBarAsNeeded - - - QMdiArea::SubWindowView - - - true - - - true - - - true - - - QTabWidget::South + + + + 0 + 0 + 69 + 481 + + + Character + - - + + + + 0 + 0 + 69 + 481 + + + + Place + + + + + + 0 + 0 + 69 + 481 + + + + Object + + + + + + + + + + -1 + + + true + + + true + + + true + + + + + + + Qt::ScrollBarAsNeeded + + + Qt::ScrollBarAsNeeded + + + QMdiArea::SubWindowView + + + true + + + true + + + true + + + QTabWidget::South + + + + + + @@ -67,7 +132,7 @@ 0 0 - 569 + 869 21 diff --git a/common.h b/common.h index 74dac74..d4e2cb9 100644 --- a/common.h +++ b/common.h @@ -4,10 +4,8 @@ #include -#ifdef _MSC_VER - #define myDebug qDebug() -#else + #define myDebug qDebug() << __FILE__ << "(" << __LINE__ << ") - " << __PRETTY_FUNCTION__ << ":" -#endif + #endif // COMMON_H diff --git a/cstorybook.cpp b/cstorybook.cpp index 6a52947..7dcbc28 100644 --- a/cstorybook.cpp +++ b/cstorybook.cpp @@ -1,7 +1,311 @@ #include "cstorybook.h" +#include "common.h" + +#include + +#include +#include + +#include -cStoryBook::cStoryBook() +cStoryBook::cStoryBook(const QString &szProjectPath) : + m_szProjectPath(szProjectPath), + m_bIsOpen(false) { + if(!openDatabase()) + return; + if(!loadBook()) + return; + + m_bIsOpen = true; +} + +cStoryBook::~cStoryBook() +{ + if(m_db.isOpen()) + m_db.close(); +} + +bool cStoryBook::openDatabase() +{ + m_db = QSqlDatabase::addDatabase("QSQLITE"); + m_db.setHostName("localhost"); + m_db.setDatabaseName(QString("%1%2storyBook.project").arg(m_szProjectPath).arg(QDir::separator())); + + if(!m_db.open()) + { + myDebug << m_db.lastError().text(); + return(false); + } + + if(!m_db.tables().contains("config")) + createDatabase(); + else + { + QSqlQuery query; + + query.prepare("SELECT value FROM config WHERE key = 'version';"); + if(!query.exec()) + { + myDebug << query.lastError().text(); + m_db.close(); + return(false); + } + + query.first(); + if(query.value("value").toDouble() < 0.1) + updateDatabase(); + } + + return(true); +} + +bool cStoryBook::createTable(const QString& szSQL) +{ + QSqlQuery query; + + query.prepare(szSQL); + + if(!query.exec()) + { + myDebug << szSQL << "\n" << query.lastError().text(); + return(false); + } + return(true); +} + +bool cStoryBook::createDatabase() +{ + QSqlQuery query; + + if(!createTable("CREATE TABLE config ( " + " key STRING, " + " value STRING " + ");")) + return(false); + + query.prepare("INSERT INTO config (key, value) VALUES (:key, :value);"); + query.bindValue(":key", "version"); + query.bindValue(":value", 0.1); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(false); + } + + if(!createTable("CREATE TABLE book ( " + " title TEXT, " + " subTitle TEXT, " + " shortDescription TEXT, " + " description TEXT, " + " author TEXT, " + " startedAt DATE, " + " finishedAt DATE, " + " targetDate DATE " + ");")) + return(false); + + if(!createTable("CREATE TABLE chapter ( " + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " partID INTEGER REFERENCES part (id), " + " name TEXT, " + " [order] INTEGER, " + " description TEXT, " + " file TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE character ( " + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " mainCharacter BOOLEAN, " + " gender BOOLEAN, " + " title TEXT, " + " firstName TEXT, " + " middleName TEXT, " + " lastName 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 TEXT " + ");")) + 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, " + " type TEXT, " + " name TEXT, " + " description TEXT, " + " file TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE object ( " + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " name TEXT, " + " type TEXT, " + " description TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE objectImage ( " + " 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 TEXT, " + " file TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE place ( " + " id INTEGER PRIMARY KEY AUTOINCREMENT " + " UNIQUE, " + " name TEXT, " + " location TEXT, " + " type TEXT, " + " description TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE placeImage ( " + " 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 TEXT, " + " link TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE rechercheCharacter ( " + " 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) " + ");")) + return(false); + + if(!createTable("CREATE TABLE rechercheObject ( " + " 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) " + ");")) + 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 DATE, " + " finishedAt DATE, " + " targetDate DATE, " + " file TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE sceneCharacter ( " + " sceneID INTEGER REFERENCES scene (id), " + " characterID INTEGER REFERENCES character (id), " + " description TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE sceneObject ( " + " sceneID INTEGER REFERENCES scene (id), " + " objectID INTEGER REFERENCES object (id), " + " description TEXT " + ");")) + return(false); + + if(!createTable("CREATE TABLE scenePlace ( " + " sceneID INTEGER REFERENCES scene (id), " + " placeID INTEGER REFERENCES place (id), " + " description TEXT " + ");")) + return(false); + + return(true); +} + +bool cStoryBook::updateDatabase() +{ + return(true); +} + +bool cStoryBook::verify() +{ + return(true); +} + +bool cStoryBook::loadBook() +{ + QSqlQuery query; + + query.prepare("SELECT title, subTitle, shortDescription, description, author, startedAt, finishedAt, targetDate FROM book;"); + if(!query.exec()) + { + myDebug << query.lastError().text(); + return(false); + } + + query.first(); + + m_book.setTitle(query.value("title").toString()); + m_book.setSubTitle(query.value("subtitle").toString()); + m_book.setShortDescription(query.value("shortDescription").toString()); + m_book.setDescription(query.value("description").toString()); + m_book.setAuthor(query.value("author").toString()); + m_book.setStartedAt(query.value("startedAt").toDate()); + m_book.setFinishedAt(query.value("finishedAt").toDate()); + m_book.setTargetDate(query.value("targetDate").toDate()); + + return(true); } diff --git a/cstorybook.h b/cstorybook.h index 2a6d8c0..039ff4e 100644 --- a/cstorybook.h +++ b/cstorybook.h @@ -2,10 +2,32 @@ #define CSTORYBOOK_H +#include "cbook.h" + +#include + +#include + + class cStoryBook { public: - cStoryBook(); + cStoryBook(const QString& szProjectPath); + ~cStoryBook(); + + bool openDatabase(); + bool verify(); +private: + QString m_szProjectPath; + bool m_bIsOpen; + QSqlDatabase m_db; + cBook m_book; + + bool createDatabase(); + bool updateDatabase(); + bool createTable(const QString& szSQL); + + bool loadBook(); }; -#endif // CSTORYBOOK_H \ No newline at end of file +#endif // CSTORYBOOK_H diff --git a/qtStoryWriter.pro b/qtStoryWriter.pro index db66ae2..aa0444b 100644 --- a/qtStoryWriter.pro +++ b/qtStoryWriter.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui xml +QT += core gui xml sql greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -62,7 +62,8 @@ SOURCES += \ ctextedit.cpp \ cstructurewindow.cpp \ cwidget.cpp \ - cstorybook.cpp + cstorybook.cpp \ + cbook.cpp HEADERS += \ cmainwindow.h \ @@ -73,13 +74,16 @@ HEADERS += \ ctextedit.h \ cstructurewindow.h \ cwidget.h \ - cstorybook.h + cstorybook.h \ + common.h \ + cbook.h FORMS += \ cmainwindow.ui \ cstructurewindow.ui -DISTFILES += +DISTFILES += \ + storyBook.project RESOURCES += \ qtstorywriter.qrc diff --git a/storyBook.project b/storyBook.project new file mode 100644 index 0000000..0c63125 Binary files /dev/null and b/storyBook.project differ