From 7cb71e121c260d193edfaa11a942362413cffedb Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 8 Mar 2015 00:27:11 +0000 Subject: [PATCH] Added drag and drop to the timeline. --- common/lc_mainwindow.cpp | 1 + common/lc_model.cpp | 37 +++++++++++++++++++++++++++++++ common/lc_model.h | 1 + common/lc_timelinewidget.cpp | 43 +++++++++++++++++++++++++++++++----- common/lc_timelinewidget.h | 3 +++ 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index e4e5b066..7a45810c 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -546,6 +546,7 @@ void lcMainWindow::CreateToolBars() mTimelineToolBar = new QDockWidget(tr("Timeline"), this); mTimelineToolBar->setObjectName("TimelineToolbar"); mTimelineToolBar->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); + mTimelineToolBar->setAcceptDrops(true); mTimelineWidget = new lcTimelineWidget(mTimelineToolBar); diff --git a/common/lc_model.cpp b/common/lc_model.cpp index cdb430be..d2a2b82c 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -1922,6 +1922,43 @@ void lcModel::ShowSelectedPiecesLater() SaveCheckpoint("Modifying"); gMainWindow->UpdateAllViews(); UpdateSelection(); + gMainWindow->UpdateFocusObject(GetFocusObject()); + } +} + +void lcModel::SetPieceSteps(const QList>& PieceSteps) +{ + if (PieceSteps.size() != mPieces.GetSize()) + return; + + bool Modified = false; + + for (int PieceIdx = 0; PieceIdx < PieceSteps.size(); PieceIdx++) + { + const QPair& PieceStep = PieceSteps[PieceIdx]; + lcPiece* Piece = mPieces[PieceIdx]; + + if (Piece != PieceStep.first || Piece->GetStepShow() != PieceStep.second) + { + Piece = PieceStep.first; + lcStep Step = PieceStep.second; + + mPieces[PieceIdx] = Piece; + Piece->SetStepShow(Step); + + if (Step > mCurrentStep) + Piece->SetSelected(false); + + Modified = true; + } + } + + if (Modified) + { + SaveCheckpoint("Modifying"); + gMainWindow->UpdateAllViews(); + UpdateSelection(); + gMainWindow->UpdateFocusObject(GetFocusObject()); } } diff --git a/common/lc_model.h b/common/lc_model.h index 553c1c43..cab84931 100644 --- a/common/lc_model.h +++ b/common/lc_model.h @@ -195,6 +195,7 @@ public: void DeleteSelectedObjects(); void ShowSelectedPiecesEarlier(); void ShowSelectedPiecesLater(); + void SetPieceSteps(const QList>& PieceSteps); lcGroup* AddGroup(const char* Prefix, lcGroup* Parent); lcGroup* GetGroup(const char* Name, bool CreateIfMissing); diff --git a/common/lc_timelinewidget.cpp b/common/lc_timelinewidget.cpp index d00c4f93..caf6a0a2 100644 --- a/common/lc_timelinewidget.cpp +++ b/common/lc_timelinewidget.cpp @@ -8,7 +8,11 @@ lcTimelineWidget::lcTimelineWidget(QWidget* Parent) : QTreeWidget(Parent) { - header()->hide(); + setSelectionMode(QAbstractItemView::ExtendedSelection); + setDragEnabled(true); + setDragDropMode(QAbstractItemView::InternalMove); + setUniformRowHeights(true); + setHeaderHidden(true); } lcTimelineWidget::~lcTimelineWidget() @@ -25,19 +29,48 @@ void lcTimelineWidget::Update() return; int Steps = Model->GetLastStep(); - QList StepItems; - StepItems.reserve(Steps); + QList StepItems; + StepItems.reserve(Steps); for (int Step = 0; Step < Steps; Step++) - StepItems.append(new QTreeWidgetItem(this, QStringList(tr("Step %1").arg(Step + 1)))); + { + QTreeWidgetItem* StepItem = new QTreeWidgetItem(this, QStringList(tr("Step %1").arg(Step + 1))); + StepItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled); + StepItems.append(StepItem); + } const lcArray& Pieces = Model->GetPieces(); for (int PieceIdx = 0; PieceIdx < Pieces.GetSize(); PieceIdx++) { lcPiece* Piece = Pieces[PieceIdx]; - new QTreeWidgetItem(StepItems[Piece->GetStepShow() - 1], QStringList(Piece->mPieceInfo->m_strDescription)); + QTreeWidgetItem* PieceItem = new QTreeWidgetItem(StepItems[Piece->GetStepShow() - 1], QStringList(Piece->mPieceInfo->m_strDescription)); + PieceItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled); + PieceItem->setData(0, Qt::UserRole, qVariantFromValue((uintptr_t)Piece)); } insertTopLevelItems(0, StepItems); + expandAll(); +} + +void lcTimelineWidget::dropEvent(QDropEvent* Event) +{ + QTreeWidget::dropEvent(Event); + + QList> PieceSteps; + + for (int TopLevelItemIdx = 0; TopLevelItemIdx < topLevelItemCount(); TopLevelItemIdx++) + { + QTreeWidgetItem* StepItem = topLevelItem(TopLevelItemIdx); + + for (int PieceItemIdx = 0; PieceItemIdx < StepItem->childCount(); PieceItemIdx++) + { + QTreeWidgetItem* PieceItem = StepItem->child(PieceItemIdx); + lcPiece* Piece = (lcPiece*)PieceItem->data(0, Qt::UserRole).value(); + + PieceSteps.append(QPair(Piece, TopLevelItemIdx + 1)); + } + } + + lcGetActiveModel()->SetPieceSteps(PieceSteps); } diff --git a/common/lc_timelinewidget.h b/common/lc_timelinewidget.h index d38de73f..2162eff0 100644 --- a/common/lc_timelinewidget.h +++ b/common/lc_timelinewidget.h @@ -8,6 +8,9 @@ public: virtual ~lcTimelineWidget(); void Update(); + +protected: + virtual void dropEvent(QDropEvent* Event); }; #endif // _LC_TIMELINEWIDGET_H_