|
| 1 | +#include "LabelSelectionController_p.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <iterator> |
| 5 | + |
| 6 | +#include <QLoggingCategory> |
| 7 | +#include <QQuickWindow> |
| 8 | + |
| 9 | +#include <ScopicFlowCore/LabelViewModel.h> |
| 10 | + |
| 11 | +#include <dspxmodel/Label.h> |
| 12 | +#include <dspxmodel/LabelSelectionModel.h> |
| 13 | +#include <dspxmodel/LabelSequence.h> |
| 14 | +#include <dspxmodel/Model.h> |
| 15 | +#include <dspxmodel/SelectionModel.h> |
| 16 | +#include <dspxmodel/Timeline.h> |
| 17 | + |
| 18 | +#include <coreplugin/DspxDocument.h> |
| 19 | +#include <coreplugin/ProjectDocumentContext.h> |
| 20 | +#include <coreplugin/ProjectWindowInterface.h> |
| 21 | + |
| 22 | +#include <visualeditor/ProjectViewModelContext.h> |
| 23 | + |
| 24 | +namespace VisualEditor { |
| 25 | + |
| 26 | + Q_STATIC_LOGGING_CATEGORY(lcLabelSelectionController, "diffscope.visualeditor.labelselectioncontroller") |
| 27 | + |
| 28 | + LabelSelectionController::LabelSelectionController(ProjectViewModelContext *parent) |
| 29 | + : SelectionController(parent), q(parent) { |
| 30 | + labelSequence = q->windowHandle()->projectDocumentContext()->document()->model()->timeline()->labels(); |
| 31 | + selectionModel = q->windowHandle()->projectDocumentContext()->document()->selectionModel(); |
| 32 | + labelSelectionModel = q->windowHandle()->projectDocumentContext()->document()->selectionModel()->labelSelectionModel(); |
| 33 | + connect(labelSelectionModel, &dspx::LabelSelectionModel::currentItemChanged, this, &SelectionController::currentItemChanged); |
| 34 | + } |
| 35 | + |
| 36 | + LabelSelectionController::~LabelSelectionController() = default; |
| 37 | + |
| 38 | + QObjectList LabelSelectionController::getSelectedItems() const { |
| 39 | + QObjectList viewItems; |
| 40 | + std::ranges::transform(labelSelectionModel->selectedItems(), std::back_inserter(viewItems), [=, this](dspx::Label *item) { |
| 41 | + auto viewItem = q->getLabelViewItemFromDocumentItem(item); |
| 42 | + Q_ASSERT(viewItem); |
| 43 | + return viewItem; |
| 44 | + }); |
| 45 | + return viewItems; |
| 46 | + } |
| 47 | + |
| 48 | + QObjectList LabelSelectionController::getItemsBetween(QObject *startItem, QObject *endItem) const { |
| 49 | + auto startDocumentItem = startItem ? q->getLabelDocumentItemFromViewItem(qobject_cast<sflow::LabelViewModel *>(startItem)) : labelSequence->firstItem(); |
| 50 | + auto endDocumentItem = endItem ? q->getLabelDocumentItemFromViewItem(qobject_cast<sflow::LabelViewModel *>(endItem)) : labelSequence->lastItem(); |
| 51 | + Q_ASSERT(startDocumentItem && endDocumentItem); |
| 52 | + QObjectList viewItems; |
| 53 | + if (startDocumentItem->pos() > endDocumentItem->pos()) { |
| 54 | + std::swap(startDocumentItem, endDocumentItem); |
| 55 | + } |
| 56 | + for (auto item = startDocumentItem; item; item = labelSequence->nextItem(item)) { |
| 57 | + auto viewItem = q->getLabelViewItemFromDocumentItem(item); |
| 58 | + Q_ASSERT(viewItem); |
| 59 | + viewItems.append(viewItem); |
| 60 | + if (item == endDocumentItem) { |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + return viewItems; |
| 65 | + } |
| 66 | + |
| 67 | + void LabelSelectionController::select(QObject *item, SelectionCommand command) { |
| 68 | + qCDebug(lcLabelSelectionController) << "Label view item selected" << item << command; |
| 69 | + dspx::SelectionModel::SelectionCommand documentSelectionCommand = {}; |
| 70 | + if (command & Select) { |
| 71 | + documentSelectionCommand |= dspx::SelectionModel::Select; |
| 72 | + } |
| 73 | + if (command & Deselect) { |
| 74 | + documentSelectionCommand |= dspx::SelectionModel::Deselect; |
| 75 | + } |
| 76 | + if (command & ClearPreviousSelection) { |
| 77 | + documentSelectionCommand |= dspx::SelectionModel::ClearPreviousSelection; |
| 78 | + } |
| 79 | + if (command & SetCurrentItem) { |
| 80 | + documentSelectionCommand |= dspx::SelectionModel::SetCurrentItem; |
| 81 | + } |
| 82 | + auto documentItem = q->getLabelDocumentItemFromViewItem(qobject_cast<sflow::LabelViewModel *>(item)); |
| 83 | + Q_ASSERT(!item || documentItem); |
| 84 | + selectionModel->select(documentItem, documentSelectionCommand, dspx::SelectionModel::ST_Label); |
| 85 | + } |
| 86 | + |
| 87 | + QObject *LabelSelectionController::currentItem() const { |
| 88 | + return q->getLabelViewItemFromDocumentItem(labelSelectionModel->currentItem()); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments