#include "AbstractTimelineModel.h" #include namespace LtsTimelineClassic { AbstractTimelineModel::AbstractTimelineModel(QObject *parent) : QAbstractItemModel(parent) , m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("Timeline.AbstractTimelineModel")) , m_highestPositionedItem(0) , m_lowestPositionedItem(0) , m_getEventListsRecursive(true) , m_rootItem(new Item(QLatin1String("#mwille/r00t/"))) , m_markersItem(new Item(tr("Markers"), QStringLiteral("Markers"))) { m_rootItem->addItem(m_markersItem); } AbstractTimelineModel::~AbstractTimelineModel() { delete m_rootItem; } void AbstractTimelineModel::retranslate() { m_markersItem->setName(tr("Markers")); } void AbstractTimelineModel::invalidateTimeline() { const LtsTimelineClassic::ItemList il = m_rootItem->getFullItemList(true); for (auto i = il.constBegin(); i != il.constEnd(); ++i) { LtsTimelineClassic::Item *item = *i; if (item != nullptr) item->setEventListChanged(); } } Event *AbstractTimelineModel::getEvent(int uniqueId) { const LtsTimelineClassic::ItemList il = m_rootItem->getFullItemList(true); for (auto i = il.constBegin(); i != il.constEnd(); ++i) { LtsTimelineClassic::Item *item = *i; if (item != nullptr && item->getIdentifier() != QStringLiteral("Markers")) { const LtsTimelineClassic::EventList el = item->getFullEventList(true, true); for (LtsTimelineClassic::Event *event : el) { if (event->uniqueId() == uniqueId) return event; } } } return nullptr; } void AbstractTimelineModel::addEvent(Event *event) { QStringList actuatorPath = event->getActuator(); Item *currentItem = m_rootItem; QModelIndex currentIndex = QModelIndex(); for (const QString &curPath : qAsConst(actuatorPath)) { Item *oldItem = currentItem; const ItemList *itemList = currentItem->getChildren(); if (itemList != nullptr) { for (int row = 0; row < itemList->count(); row++) { Item *childItem = itemList->at(row); if (childItem->getName() == curPath) { currentIndex = index(row, 0, currentIndex); currentItem = childItem; break; } } } if (oldItem == currentItem) { beginInsertRows(currentIndex, currentItem->childCount(), currentItem->childCount()); Item *newItem = new Item(curPath); currentItem->addItem(newItem); endInsertRows(); currentIndex = index(currentItem->childCount() - 1, 0, currentIndex); currentItem = newItem; } } currentItem->addEvent(event); } Item *AbstractTimelineModel::getRootItem() { return m_rootItem; } Item *AbstractTimelineModel::itemFromIndex(const QModelIndex &idx) const { if (!idx.isValid()) return m_rootItem; return static_cast(idx.internalPointer()); } QVariant AbstractTimelineModel::data(const QModelIndex &index, int role) const { if (role != Qt::DisplayRole || !index.isValid()) return QVariant(); Item *item = itemFromIndex(index); if (!item) return QVariant(); if (index.column() == 1) { PassiveEventContainer *pevcon = item->getPevcon(); return QVariant::fromValue(pevcon); } QString name(item->getName()); if (name.isEmpty()) name = tr("[unknown]"); return QVariant(name); } Qt::ItemFlags AbstractTimelineModel::flags(const QModelIndex &index) const { if (!index.isValid()) return {}; return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; } QVariant AbstractTimelineModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { switch (section) { case 0: return QString(tr("Actuator") + QStringLiteral("\r\n")); case 1: return QString(tr("Events") + QStringLiteral("\r\n")); } } return QVariant(); } QModelIndex AbstractTimelineModel::index(int row, int column, const QModelIndex &parent) const { if (!hasIndex(row, column, parent)) return QModelIndex(); Item *item = itemFromIndex(parent); Item *child = item ? item->child(row) : nullptr; if (child) { return createIndex(row, column, child); } return QModelIndex(); } QModelIndex AbstractTimelineModel::parent(const QModelIndex &index) const { if (!index.isValid()) return QModelIndex(); Item *item = itemFromIndex(index); Item *parent = item ? item->getParent() : nullptr; if (parent == m_rootItem || parent == nullptr) return QModelIndex(); int idx = parent->indexOfParent(); if (idx == -1) return QModelIndex(); return createIndex(idx, 0, parent); } int AbstractTimelineModel::rowCount(const QModelIndex &parent) const { if (!parent.isValid()) { int r = m_rootItem->visibleChildCount(); return r; } Item *item = itemFromIndex(parent); if (item) { int r = item->visibleChildCount(); return r; } return 0; } int AbstractTimelineModel::columnCount(const QModelIndex &) const { return 2; } }