From c53b826d907ad56cb7ce85dfc713536533f88ce6 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Wed, 3 Oct 2018 18:15:35 +0200 Subject: [PATCH] implementation of reordering (outline) --- cmainwindow.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ cmainwindow.h | 7 +++++ cmainwindow.ui | 20 ++++++++++-- cstorybook.cpp | 1 + ctreeview.cpp | 45 ++++++++++++++++++++++++++ ctreeview.h | 14 +++++++++ 6 files changed, 169 insertions(+), 2 deletions(-) diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 048d67f..b35a4ff 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -46,6 +46,42 @@ #include +QDataStream& operator<<(QDataStream& out, cPart* const &rhs) +{ + out.writeRawData(reinterpret_cast(&rhs), sizeof(rhs)); + return out; +} + +QDataStream& operator>>(QDataStream& in, cPart* &rhs) +{ + in.readRawData(reinterpret_cast(&rhs), sizeof(rhs)); + return in; +} + +QDataStream& operator<<(QDataStream& out, cChapter* const &rhs) +{ + out.writeRawData(reinterpret_cast(&rhs), sizeof(rhs)); + return out; +} + +QDataStream& operator>>(QDataStream& in, cChapter* &rhs) +{ + in.readRawData(reinterpret_cast(&rhs), sizeof(rhs)); + return in; +} + +QDataStream& operator<<(QDataStream& out, cScene* const &rhs) +{ + out.writeRawData(reinterpret_cast(&rhs), sizeof(rhs)); + return out; +} + +QDataStream& operator>>(QDataStream& in, cScene* &rhs) +{ + in.readRawData(reinterpret_cast(&rhs), sizeof(rhs)); + return in; +} + cMainWindow::cMainWindow(cSplashScreen *lpSplashScreen, QTranslator *lpTranslator, QWidget *parent) : QMainWindow(parent), ui(new Ui::cMainWindow), @@ -72,6 +108,10 @@ cMainWindow::cMainWindow(cSplashScreen *lpSplashScreen, QTranslator *lpTranslato m_lpOldTextEdit(0), m_lpOptionsDialog(0) { + qRegisterMetaTypeStreamOperators("cPart*"); + qRegisterMetaTypeStreamOperators("cChapter*"); + qRegisterMetaTypeStreamOperators("cScene*"); + initUI(); createActions(); onLanguageChanged(); @@ -229,6 +269,8 @@ void cMainWindow::createActions() connect(ui->m_lpPlaceList, &QTreeView::customContextMenuRequested, this, &cMainWindow::onPlaceContextMenu); connect(ui->m_lpObjectList, &QTreeView::customContextMenuRequested, this, &cMainWindow::onObjectContextMenu); connect(ui->m_lpRechercheList, &QTreeView::customContextMenuRequested, this, &cMainWindow::onRechercheContextMenu); + + connect(ui->m_lpOutlineList, &cTreeView::dropped, this, &cMainWindow::onOutlineDropped); } void cMainWindow::createFileActions() @@ -953,6 +995,48 @@ void cMainWindow::onFontComboBoxLostFocus(cFontComboBox* /*lpComboBox*/) { } +void cMainWindow::onOutlineDropped(cTreeView* /*lpTreeView*/, QModelIndex from, QModelIndex /*to*/) +{ + qint32 parts = m_lpOutlineModel->rowCount(QModelIndex()); + + for(qint32 part = 0;part < parts;part++) + { + QModelIndex partIndex = m_lpOutlineModel->index(part, 0, QModelIndex()); + cPart* lpPart = qvariant_cast(m_lpOutlineModel->itemFromIndex(partIndex)->data()); + qint32 chapters = m_lpOutlineModel->rowCount(partIndex); + + if(partIndex != from) + lpPart->setSortOrder(part); + + for(qint32 chapter = 0;chapter < chapters;chapter++) + { + QModelIndex chapterIndex = m_lpOutlineModel->index(chapter, 0, partIndex); + cChapter* lpChapter = qvariant_cast(m_lpOutlineModel->itemFromIndex(chapterIndex)->data()); + qint32 scenes = m_lpOutlineModel->rowCount(chapterIndex); + + if(chapterIndex != from) + { + lpChapter->setPart(lpPart); + lpChapter->setSortOrder(chapter); + } + + lpChapter = lpChapter; + for(qint32 scene = 0;scene < scenes;scene++) + { + QModelIndex sceneIndex = m_lpOutlineModel->index(scene, 0, chapterIndex); + cScene* lpScene = qvariant_cast(m_lpOutlineModel->itemFromIndex(sceneIndex)->data()); + + if(sceneIndex != from) + { + lpScene->setChapter(lpChapter); + lpScene->setSortOrder(scene); + } + } + } + } + somethingChanged(); +} + void cMainWindow::onMainTabCurrentChanged(int /*index*/) { if(m_bUpdatingTab) diff --git a/cmainwindow.h b/cmainwindow.h index ad12792..7fc8d64 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -258,6 +258,13 @@ public slots: \param lpComboBox */ void onFontComboBoxLostFocus(cFontComboBox* lpComboBox); + /*! + \brief + + \fn onOutlineDropped + \param lpTreeView + */ + void onOutlineDropped(cTreeView* lpTreeView, QModelIndex from, QModelIndex to); private slots: /*! \brief diff --git a/cmainwindow.ui b/cmainwindow.ui index d337097..1de465d 100644 --- a/cmainwindow.ui +++ b/cmainwindow.ui @@ -28,7 +28,7 @@ QFrame::Sunken - 4 + 0 @@ -44,13 +44,22 @@ - + Qt::CustomContextMenu QAbstractItemView::NoEditTriggers + + true + + + QAbstractItemView::InternalMove + + + Qt::MoveAction + true @@ -311,6 +320,13 @@ + + + cTreeView + QTreeView +
ctreeview.h
+
+
diff --git a/cstorybook.cpp b/cstorybook.cpp index ff34581..1ff7d2c 100644 --- a/cstorybook.cpp +++ b/cstorybook.cpp @@ -788,6 +788,7 @@ bool cStoryBook::updateDatabase01() { QSqlQuery query; + query.exec("DROP TABLE config_old;"); if(!query.exec("ALTER TABLE config RENAME TO config_old;")) { myDebug << query.lastError().text(); diff --git a/ctreeview.cpp b/ctreeview.cpp index 7af43c9..4ceca69 100644 --- a/ctreeview.cpp +++ b/ctreeview.cpp @@ -5,6 +5,14 @@ #include "ctreeview.h" +#include "cpart.h" +#include "cchapter.h" +#include "cscene.h" + +#include +#include + + cTreeView::cTreeView(QWidget* parent) : QTreeView(parent) { @@ -21,3 +29,40 @@ void cTreeView::focusOutEvent(QFocusEvent* event) lostFocus(this); QTreeView::focusOutEvent(event); } + +qint16 level(QModelIndex index) +{ + QModelIndex tmp = index; + qint16 level = 0; + + while(tmp.parent().isValid()) + { + level++; + tmp = tmp.parent(); + } + return(level); +} + +void cTreeView::dropEvent(QDropEvent* event) +{ + QModelIndex fromIndex = selectedIndexes()[0]; + QModelIndex droppedIndex = indexAt(event->pos()); + + qint16 fromLevel = level(fromIndex); + qint16 droppedLevel = level(droppedIndex); + +// QStandardItemModel* lpModel = (QStandardItemModel*)this->model(); +// QStandardItem* lpToItem = lpModel->itemFromIndex(droppedIndex); + +// if(droppedLevel < fromLevel-1) +// return; +// else if(droppedLevel > fromLevel) +// return; + + if(fromLevel != droppedLevel) + return; + + QTreeView::dropEvent(event); + QModelIndex toIndex = selectedIndexes()[0]; + dropped(this, fromIndex, toIndex); +} diff --git a/ctreeview.h b/ctreeview.h index 0f4c5d8..aa65752 100644 --- a/ctreeview.h +++ b/ctreeview.h @@ -44,6 +44,13 @@ signals: \param lpTreeView */ void lostFocus(cTreeView* lpTreeView); + /*! + \brief + + \fn dropped + \param lpTreeView + */ + void dropped(cTreeView* lpTreeView, QModelIndex from, QModelIndex to); protected: /*! @@ -60,6 +67,13 @@ protected: \param event */ void focusOutEvent(QFocusEvent *event); + /*! + \brief + + \fn dropEvent + \param event + */ + void dropEvent(QDropEvent *event); }; Q_DECLARE_METATYPE(cTreeView*)