leocad/common/lc_timelinewidget.cpp

667 lines
16 KiB
C++
Raw Normal View History

2016-10-05 23:28:52 +02:00
#include "lc_global.h"
#include "lc_timelinewidget.h"
#include "lc_model.h"
#include "piece.h"
#include "pieceinf.h"
#include "lc_mainwindow.h"
2020-12-18 02:59:11 +01:00
#include "lc_viewwidget.h"
#include "lc_previewwidget.h"
2016-10-05 23:28:52 +02:00
lcTimelineWidget::lcTimelineWidget(QWidget* Parent)
: QTreeWidget(Parent)
{
mCurrentStepItem = nullptr;
2016-10-05 23:28:52 +02:00
mIgnoreUpdates = false;
setSelectionMode(QAbstractItemView::ExtendedSelection);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::InternalMove);
setUniformRowHeights(true);
setHeaderHidden(true);
setContextMenuPolicy(Qt::CustomContextMenu);
invisibleRootItem()->setFlags(invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled);
connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), SLOT(CurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
2016-10-05 23:28:52 +02:00
connect(this, SIGNAL(itemSelectionChanged()), SLOT(ItemSelectionChanged()));
connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(CustomMenuRequested(QPoint)));
}
lcTimelineWidget::~lcTimelineWidget()
{
}
void lcTimelineWidget::CustomMenuRequested(QPoint Pos)
{
QMenu* Menu = new QMenu(this);
lcObject* FocusObject = gMainWindow->GetActiveModel()->GetFocusObject();
2016-10-05 23:28:52 +02:00
if (FocusObject && FocusObject->IsPiece())
{
lcPiece* Piece = (lcPiece*)FocusObject;
if (Piece->mPieceInfo->IsModel())
{
Menu->addAction(gMainWindow->mActions[LC_PIECE_EDIT_SELECTED_SUBMODEL]);
Menu->addAction(gMainWindow->mActions[LC_PIECE_VIEW_SELECTED_MODEL]);
Menu->addAction(gMainWindow->mActions[LC_PIECE_INLINE_SELECTED_MODELS]);
2016-10-05 23:28:52 +02:00
Menu->addSeparator();
}
}
Menu->addAction(gMainWindow->mActions[LC_TIMELINE_SET_CURRENT]);
Menu->addAction(gMainWindow->mActions[LC_TIMELINE_INSERT_BEFORE]);
Menu->addAction(gMainWindow->mActions[LC_TIMELINE_INSERT_AFTER]);
Menu->addAction(gMainWindow->mActions[LC_TIMELINE_DELETE]);
Menu->addSeparator();
Menu->addAction(gMainWindow->mActions[LC_TIMELINE_MOVE_SELECTION]);
Menu->addAction(gMainWindow->mActions[LC_TIMELINE_MOVE_SELECTION_BEFORE]);
Menu->addAction(gMainWindow->mActions[LC_TIMELINE_MOVE_SELECTION_AFTER]);
2016-10-05 23:28:52 +02:00
Menu->addSeparator();
Menu->addAction(gMainWindow->mActions[LC_PIECE_HIDE_SELECTED]);
Menu->addAction(gMainWindow->mActions[LC_PIECE_HIDE_UNSELECTED]);
Menu->addAction(gMainWindow->mActions[LC_PIECE_UNHIDE_SELECTED]);
Menu->addAction(gMainWindow->mActions[LC_PIECE_UNHIDE_ALL]);
Menu->exec(viewport()->mapToGlobal(Pos));
delete Menu;
2016-10-05 23:28:52 +02:00
}
void lcTimelineWidget::Update(bool Clear, bool UpdateItems)
{
if (mIgnoreUpdates)
return;
lcModel* Model = gMainWindow->GetActiveModel();
bool Blocked = blockSignals(true);
2016-10-05 23:28:52 +02:00
if (!Model)
{
mCurrentStepItem = nullptr;
2016-10-05 23:28:52 +02:00
mItems.clear();
clear();
blockSignals(Blocked);
2016-10-05 23:28:52 +02:00
return;
}
if (Clear)
{
mCurrentStepItem = nullptr;
2016-10-05 23:28:52 +02:00
mItems.clear();
clear();
}
lcStep LastStep = Model->GetLastStep();
if (Model->HasPieces())
LastStep++;
LastStep = lcMax(LastStep, Model->GetCurrentStep());
2016-10-05 23:28:52 +02:00
for (int TopLevelItemIdx = LastStep; TopLevelItemIdx < topLevelItemCount(); )
{
QTreeWidgetItem* StepItem = topLevelItem(TopLevelItemIdx);
while (StepItem->childCount())
{
QTreeWidgetItem* PieceItem = StepItem->child(0);
lcPiece* Piece = (lcPiece*)PieceItem->data(0, Qt::UserRole).value<uintptr_t>();
mItems.remove(Piece);
delete PieceItem;
}
if (mCurrentStepItem == StepItem)
mCurrentStepItem = nullptr;
2016-10-05 23:28:52 +02:00
delete StepItem;
}
for (unsigned int TopLevelItemIdx = topLevelItemCount(); TopLevelItemIdx < LastStep; TopLevelItemIdx++)
{
QTreeWidgetItem* StepItem = new QTreeWidgetItem(this, QStringList(tr("Step %1").arg(TopLevelItemIdx + 1)));
StepItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
addTopLevelItem(StepItem);
StepItem->setExpanded(true);
}
2024-06-20 04:22:18 +02:00
const std::vector<std::unique_ptr<lcPiece>>& Pieces = Model->GetPieces();
QTreeWidgetItem* StepItem = nullptr;
2016-10-05 23:28:52 +02:00
int PieceItemIndex = 0;
lcStep Step = 0;
2024-06-20 04:22:18 +02:00
for (const std::unique_ptr<lcPiece>& Piece : Pieces)
2016-10-05 23:28:52 +02:00
{
while (Step != Piece->GetStepShow())
{
if (StepItem)
{
while (PieceItemIndex < StepItem->childCount())
{
QTreeWidgetItem* PieceItem = StepItem->child(PieceItemIndex);
lcPiece* RemovePiece = (lcPiece*)PieceItem->data(0, Qt::UserRole).value<uintptr_t>();
2024-06-20 04:22:18 +02:00
auto RemoveIt = std::find_if(Pieces.begin(), Pieces.end(), [RemovePiece](const std::unique_ptr<lcPiece>& CheckPiece){ return CheckPiece.get() == RemovePiece; });
2016-10-05 23:28:52 +02:00
2024-06-20 04:22:18 +02:00
if (RemoveIt == Pieces.end())
2016-10-05 23:28:52 +02:00
{
mItems.remove(RemovePiece);
delete PieceItem;
}
else
{
PieceItem->parent()->removeChild(PieceItem);
topLevelItem(RemovePiece->GetStepShow() - 1)->addChild(PieceItem);
}
}
}
Step++;
StepItem = topLevelItem(Step - 1);
PieceItemIndex = 0;
}
2024-06-20 04:22:18 +02:00
QTreeWidgetItem* PieceItem = mItems.value(Piece.get());
2016-10-05 23:28:52 +02:00
bool UpdateItem = UpdateItems;
2017-02-13 03:05:20 +01:00
if (StepItem)
2016-10-05 23:28:52 +02:00
{
2017-02-13 03:05:20 +01:00
if (!PieceItem)
{
PieceItem = new QTreeWidgetItem();
PieceItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
2024-06-20 04:22:18 +02:00
PieceItem->setData(0, Qt::UserRole, QVariant::fromValue<uintptr_t>((uintptr_t)Piece.get()));
2017-02-13 03:05:20 +01:00
StepItem->insertChild(PieceItemIndex, PieceItem);
2024-06-20 04:22:18 +02:00
mItems[Piece.get()] = PieceItem;
2016-10-05 23:28:52 +02:00
2017-02-13 03:05:20 +01:00
UpdateItem = true;
}
else
2016-10-05 23:28:52 +02:00
{
2017-02-13 03:05:20 +01:00
if (PieceItemIndex >= StepItem->childCount() || PieceItem != StepItem->child(PieceItemIndex))
{
QTreeWidgetItem* PieceParent = PieceItem->parent();
2016-10-05 23:28:52 +02:00
2017-02-13 03:05:20 +01:00
if (PieceParent)
PieceParent->removeChild(PieceItem);
2016-10-05 23:28:52 +02:00
2017-02-13 03:05:20 +01:00
StepItem->insertChild(PieceItemIndex, PieceItem);
}
2016-10-05 23:28:52 +02:00
}
}
if (UpdateItem)
{
PieceItem->setText(0, Piece->mPieceInfo->m_strDescription);
2021-01-17 03:27:39 +01:00
int ColorIndex = Piece->GetColorIndex();
2016-10-05 23:28:52 +02:00
if (!mIcons.contains(ColorIndex))
{
int Size = rowHeight(indexFromItem(PieceItem));
QImage Image(Size, Size, QImage::Format_ARGB32);
Image.fill(0);
float* Color = gColorList[ColorIndex].Value;
QPainter Painter(&Image);
Painter.setPen(Qt::darkGray);
2016-10-05 23:28:52 +02:00
Painter.setBrush(QColor::fromRgbF(Color[0], Color[1], Color[2]));
Painter.drawEllipse(0, 0, Size - 1, Size - 1);
2016-10-05 23:28:52 +02:00
mIcons[ColorIndex] = QIcon(QPixmap::fromImage(Image));
}
PieceItem->setIcon(0, mIcons[ColorIndex]);
QColor Color = palette().text().color();
if (Piece->mPieceInfo->IsPlaceholder())
Color = QColor(208, 0, 0);
else if (Piece->IsHidden())
2016-10-05 23:28:52 +02:00
Color.setAlpha(128);
2020-12-11 23:51:46 +01:00
PieceItem->setForeground(0, Color);
2016-10-05 23:28:52 +02:00
}
PieceItem->setSelected(Piece->IsSelected());
PieceItemIndex++;
}
if (Step == 0)
{
Step = 1;
StepItem = topLevelItem(0);
}
2017-02-13 03:05:20 +01:00
while (Step <= LastStep && StepItem)
2016-10-05 23:28:52 +02:00
{
while (PieceItemIndex < StepItem->childCount())
{
QTreeWidgetItem* PieceItem = StepItem->child(PieceItemIndex);
lcPiece* RemovePiece = (lcPiece*)PieceItem->data(0, Qt::UserRole).value<uintptr_t>();
mItems.remove(RemovePiece);
delete PieceItem;
}
Step++;
StepItem = topLevelItem(Step - 1);
PieceItemIndex = 0;
}
UpdateCurrentStepItem();
2016-10-05 23:28:52 +02:00
blockSignals(Blocked);
}
void lcTimelineWidget::UpdateCurrentStepItem()
{
lcModel* Model = gMainWindow->GetActiveModel();
lcStep CurrentStep = Model->GetCurrentStep();
QTreeWidgetItem* CurrentStepItem = topLevelItem(CurrentStep - 1);
if (CurrentStepItem != mCurrentStepItem)
{
if (mCurrentStepItem)
{
QFont Font = mCurrentStepItem->font(0);
Font.setBold(false);
mCurrentStepItem->setFont(0, Font);
}
if (CurrentStepItem)
{
QFont Font = CurrentStepItem->font(0);
Font.setBold(true);
CurrentStepItem->setFont(0, Font);
setCurrentItem(CurrentStepItem);
}
mCurrentStepItem = CurrentStepItem;
}
}
2016-10-05 23:28:52 +02:00
void lcTimelineWidget::UpdateSelection()
{
if (mIgnoreUpdates)
return;
QItemSelection ItemSelection;
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<uintptr_t>();
2017-07-03 05:12:17 +02:00
if (Piece && Piece->IsSelected())
2016-10-05 23:28:52 +02:00
{
QModelIndex Index = indexFromItem(PieceItem);
ItemSelection.select(Index, Index);
}
}
}
bool Blocked = blockSignals(true);
selectionModel()->select(ItemSelection, QItemSelectionModel::ClearAndSelect);
blockSignals(Blocked);
}
void lcTimelineWidget::InsertStepBefore()
2016-10-05 23:28:52 +02:00
{
QTreeWidgetItem* CurrentItem = currentItem();
if (!CurrentItem)
return;
if (CurrentItem->parent())
CurrentItem = CurrentItem->parent();
int Step = indexOfTopLevelItem(CurrentItem);
if (Step == -1)
return;
gMainWindow->GetActiveModel()->InsertStep(Step + 1);
2016-10-05 23:28:52 +02:00
}
void lcTimelineWidget::InsertStepAfter()
{
QTreeWidgetItem* CurrentItem = currentItem();
if (!CurrentItem)
return;
if (CurrentItem->parent())
CurrentItem = CurrentItem->parent();
int Step = indexOfTopLevelItem(CurrentItem);
if (Step == -1)
return;
gMainWindow->GetActiveModel()->InsertStep(Step + 2);
}
2016-10-05 23:28:52 +02:00
void lcTimelineWidget::RemoveStep()
{
QTreeWidgetItem* CurrentItem = currentItem();
if (!CurrentItem)
return;
if (CurrentItem->parent())
CurrentItem = CurrentItem->parent();
int Step = indexOfTopLevelItem(CurrentItem);
if (Step == -1)
return;
gMainWindow->GetActiveModel()->RemoveStep(Step + 1);
2016-10-05 23:28:52 +02:00
}
void lcTimelineWidget::MoveSelection()
{
QTreeWidgetItem* CurrentItem = currentItem();
if (!CurrentItem)
return;
if (CurrentItem->parent())
CurrentItem = CurrentItem->parent();
int Step = indexOfTopLevelItem(CurrentItem);
if (Step == -1)
return;
Step++;
QList<QTreeWidgetItem*> SelectedItems = selectedItems();
2018-02-22 01:12:18 +01:00
for (QTreeWidgetItem* PieceItem : SelectedItems)
{
QTreeWidgetItem* Parent = PieceItem->parent();
if (!Parent)
continue;
int ChildIndex = Parent->indexOfChild(PieceItem);
CurrentItem->addChild(Parent->takeChild(ChildIndex));
}
UpdateModel();
lcModel* Model = gMainWindow->GetActiveModel();
if (Step > static_cast<int>(Model->GetCurrentStep()))
Model->SetCurrentStep(Step);
}
void lcTimelineWidget::MoveSelectionBefore()
{
QTreeWidgetItem* CurrentItem = currentItem();
if (!CurrentItem)
return;
if (CurrentItem->parent())
CurrentItem = CurrentItem->parent();
int Step = indexOfTopLevelItem(CurrentItem);
if (Step == -1)
return;
Step++;
QList<QTreeWidgetItem*> SelectedItems = selectedItems();
gMainWindow->GetActiveModel()->InsertStep(Step);
CurrentItem = topLevelItem(Step - 1);
for (QTreeWidgetItem* PieceItem : SelectedItems)
{
QTreeWidgetItem* Parent = PieceItem->parent();
if (!Parent)
continue;
int ChildIndex = Parent->indexOfChild(PieceItem);
CurrentItem->addChild(Parent->takeChild(ChildIndex));
}
UpdateModel();
lcModel* Model = gMainWindow->GetActiveModel();
if (Step > static_cast<int>(Model->GetCurrentStep()))
Model->SetCurrentStep(Step);
}
void lcTimelineWidget::MoveSelectionAfter()
{
QTreeWidgetItem* CurrentItem = currentItem();
if (!CurrentItem)
return;
if (CurrentItem->parent())
CurrentItem = CurrentItem->parent();
int Step = indexOfTopLevelItem(CurrentItem);
if (Step == -1)
return;
Step += 2;
QList<QTreeWidgetItem*> SelectedItems = selectedItems();
gMainWindow->GetActiveModel()->InsertStep(Step);
for (int TopLevelItemIdx = topLevelItemCount(); TopLevelItemIdx < Step; TopLevelItemIdx++)
{
QTreeWidgetItem* StepItem = new QTreeWidgetItem(this, QStringList(tr("Step %1").arg(TopLevelItemIdx + 1)));
StepItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
addTopLevelItem(StepItem);
StepItem->setExpanded(true);
}
CurrentItem = topLevelItem(Step - 1);
for (QTreeWidgetItem* PieceItem : SelectedItems)
{
QTreeWidgetItem* Parent = PieceItem->parent();
if (!Parent)
continue;
int ChildIndex = Parent->indexOfChild(PieceItem);
CurrentItem->addChild(Parent->takeChild(ChildIndex));
}
UpdateModel();
lcModel* Model = gMainWindow->GetActiveModel();
if (Step > static_cast<int>(Model->GetCurrentStep()))
Model->SetCurrentStep(Step);
}
void lcTimelineWidget::SetCurrentStep()
{
QTreeWidgetItem* CurrentItem = currentItem();
if (!CurrentItem)
return;
if (CurrentItem->parent())
CurrentItem = CurrentItem->parent();
int Step = indexOfTopLevelItem(CurrentItem);
if (Step == -1)
return;
gMainWindow->GetActiveModel()->SetCurrentStep(Step + 1);
}
void lcTimelineWidget::CurrentItemChanged(QTreeWidgetItem* Current, QTreeWidgetItem* Previous)
{
Q_UNUSED(Previous);
if (Current && !Current->parent())
SetCurrentStep();
}
2016-10-05 23:28:52 +02:00
void lcTimelineWidget::ItemSelectionChanged()
{
2024-05-26 22:01:34 +02:00
std::vector<lcObject*> Selection;
2016-10-05 23:28:52 +02:00
lcStep LastStep = 1;
2018-02-22 02:27:24 +01:00
QList<QTreeWidgetItem*> SelectedItems = selectedItems();
2016-10-05 23:28:52 +02:00
2018-02-22 02:27:24 +01:00
for (QTreeWidgetItem* PieceItem : SelectedItems)
2016-10-05 23:28:52 +02:00
{
lcPiece* Piece = (lcPiece*)PieceItem->data(0, Qt::UserRole).value<uintptr_t>();
2017-07-03 05:12:17 +02:00
if (Piece)
{
LastStep = lcMax(LastStep, Piece->GetStepShow());
2024-05-26 22:01:34 +02:00
Selection.emplace_back(Piece);
2017-07-03 05:12:17 +02:00
}
2016-10-05 23:28:52 +02:00
}
lcPiece* CurrentPiece = nullptr;
QTreeWidgetItem* CurrentItem = currentItem();
2024-05-26 22:01:34 +02:00
2016-10-05 23:28:52 +02:00
if (CurrentItem && CurrentItem->isSelected())
CurrentPiece = (lcPiece*)CurrentItem->data(0, Qt::UserRole).value<uintptr_t>();
bool Blocked = blockSignals(true);
mIgnoreUpdates = true;
lcModel* Model = gMainWindow->GetActiveModel();
2024-05-26 22:01:34 +02:00
2016-10-05 23:28:52 +02:00
if (LastStep > Model->GetCurrentStep())
{
2016-10-05 23:28:52 +02:00
Model->SetCurrentStep(LastStep);
UpdateCurrentStepItem();
}
2024-05-26 22:01:34 +02:00
Model->SetSelectionAndFocus(Selection, CurrentPiece, LC_PIECE_SECTION_POSITION, false);
2016-10-05 23:28:52 +02:00
mIgnoreUpdates = false;
blockSignals(Blocked);
}
void lcTimelineWidget::dropEvent(QDropEvent* Event)
{
QTreeWidgetItem* DropItem = itemAt(Event->pos());
lcModel* Model = gMainWindow->GetActiveModel();
QList<QTreeWidgetItem*> SelectedItems = selectedItems();
clearSelection();
if (DropItem)
{
QTreeWidgetItem* ParentItem = DropItem->parent();
lcStep Step = indexOfTopLevelItem(ParentItem ? ParentItem : DropItem) + 1;
if (Step > Model->GetCurrentStep())
Model->SetCurrentStep(Step);
}
auto SortItems = [this](QTreeWidgetItem* Item1, QTreeWidgetItem* Item2)
{
QTreeWidgetItem* StepItem1 = Item1->parent();
QTreeWidgetItem* StepItem2 = Item2->parent();
if (StepItem1 == StepItem2)
return StepItem1->indexOfChild(Item1) < StepItem1->indexOfChild(Item2);
return indexOfTopLevelItem(StepItem1) < indexOfTopLevelItem(StepItem2);
};
std::sort(SelectedItems.begin(), SelectedItems.end(), SortItems);
for (QTreeWidgetItem* SelectedItem : SelectedItems)
SelectedItem->setSelected(true);
2016-10-05 23:28:52 +02:00
QTreeWidget::dropEvent(Event);
UpdateModel();
Update(false, false);
}
void lcTimelineWidget::mousePressEvent(QMouseEvent* Event)
{
if (Event->button() == Qt::RightButton)
{
QItemSelection Selection = selectionModel()->selection();
bool Blocked = blockSignals(true);
QTreeWidget::mousePressEvent(Event);
blockSignals(Blocked);
selectionModel()->select(Selection, QItemSelectionModel::ClearAndSelect);
}
else
QTreeWidget::mousePressEvent(Event);
}
2016-10-05 23:28:52 +02:00
2020-12-18 21:15:35 +01:00
void lcTimelineWidget::mouseDoubleClickEvent(QMouseEvent* MouseEvent)
{
2020-12-18 21:15:35 +01:00
if (MouseEvent->button() == Qt::LeftButton)
{
2020-12-18 21:15:35 +01:00
QTreeWidgetItem* CurrentItem = currentItem();
PreviewSelection(CurrentItem);
}
2020-12-18 21:15:35 +01:00
QTreeWidget::mouseDoubleClickEvent(MouseEvent);
}
2020-12-18 21:15:35 +01:00
void lcTimelineWidget::PreviewSelection(QTreeWidgetItem* CurrentItem)
{
2020-12-18 21:15:35 +01:00
if (!CurrentItem)
return;
lcPiece* Piece = (lcPiece*)CurrentItem->data(0, Qt::UserRole).value<uintptr_t>();
if (!Piece)
return;
PieceInfo* Info = Piece->mPieceInfo;
2020-12-18 21:15:35 +01:00
if (!Info)
return;
2021-01-17 03:27:39 +01:00
gMainWindow->PreviewPiece(Info->mFileName, Piece->GetColorCode(), false);
}
void lcTimelineWidget::UpdateModel()
{
2024-06-20 04:22:18 +02:00
std::vector<std::pair<lcPiece*, lcStep>> PieceSteps;
2016-10-05 23:28:52 +02:00
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<uintptr_t>();
2024-06-20 04:22:18 +02:00
PieceSteps.emplace_back(std::pair<lcPiece*, lcStep>(Piece, TopLevelItemIdx + 1));
2016-10-05 23:28:52 +02:00
}
}
mIgnoreUpdates = true;
gMainWindow->GetActiveModel()->SetPieceSteps(PieceSteps);
2016-10-05 23:28:52 +02:00
mIgnoreUpdates = false;
}