diff --git a/src/models/sacndiscoveredsourcelistmodel.cpp b/src/models/sacndiscoveredsourcelistmodel.cpp index 269bdaf..500a1b2 100644 --- a/src/models/sacndiscoveredsourcelistmodel.cpp +++ b/src/models/sacndiscoveredsourcelistmodel.cpp @@ -21,384 +21,390 @@ #include -void sACNDiscoveredSourceListModel::modelContainer::addSource(const CID &cid) +void sACNDiscoveredSourceListModel::modelContainer::addSource(const CID& cid) { - QMutexLocker locker(&listMutex); - if (!sources.contains(cid)) - { - sources.append(cid); - universes.append(universeList_t()); - assert(sources.size() == universes.size()); - } + QMutexLocker locker(&listMutex); + if (!sources.contains(cid)) + { + sources.append(cid); + universes.append(universeList_t()); + assert(sources.size() == universes.size()); + } } -void sACNDiscoveredSourceListModel::modelContainer::removeSource(const CID &cid) +void sACNDiscoveredSourceListModel::modelContainer::removeSource(const CID& cid) { - QMutexLocker locker(&listMutex); + QMutexLocker locker(&listMutex); - const auto sourceRow = sources.indexOf(cid); - if (sourceRow == -1) - return; + const auto sourceRow = sources.indexOf(cid); + if (sourceRow == -1) + return; - sources.removeAt(sourceRow); - universes.removeAt(sourceRow); - assert(sources.size() == universes.size()); + sources.removeAt(sourceRow); + universes.removeAt(sourceRow); + assert(sources.size() == universes.size()); } -void sACNDiscoveredSourceListModel::modelContainer::addUniverse(const CID &cid, quint16 universe) +void sACNDiscoveredSourceListModel::modelContainer::addUniverse(const CID& cid, quint16 universe) { - QMutexLocker locker(&listMutex); + QMutexLocker locker(&listMutex); - const auto sourceRow = sources.indexOf(cid); - if (sourceRow == -1) - return; + const auto sourceRow = sources.indexOf(cid); + if (sourceRow == -1) + return; - if (!universes.at(sourceRow).contains(universe)) - universes[sourceRow].append(universe); + if (!universes.at(sourceRow).contains(universe)) + universes[sourceRow].append(universe); } -void sACNDiscoveredSourceListModel::modelContainer::removeUniverse(const CID &cid, quint16 universe) +void sACNDiscoveredSourceListModel::modelContainer::removeUniverse(const CID& cid, quint16 universe) { - QMutexLocker locker(&listMutex); + QMutexLocker locker(&listMutex); - const auto sourceRow = sources.indexOf(cid); - if (sourceRow == -1) - return; + const auto sourceRow = sources.indexOf(cid); + if (sourceRow == -1) + return; - const auto universeRow = universes.at(sourceRow).indexOf(universe); - if (universeRow == -1) - return; + const auto universeRow = universes.at(sourceRow).indexOf(universe); + if (universeRow == -1) + return; - universes[sourceRow].removeAt(universeRow); + universes[sourceRow].removeAt(universeRow); } CID sACNDiscoveredSourceListModel::modelContainer::getCID(unsigned int row) const { - QMutexLocker locker(&listMutex); + QMutexLocker locker(&listMutex); - if (row >= static_cast(sources.size())) - return CID(); + if (row >= static_cast(sources.size())) + return CID(); - return sources.at(row); + return sources.at(row); } sACNDiscoveredSourceListModel::modelContainer::universe_t - sACNDiscoveredSourceListModel::modelContainer::getUniverse(const CID &cid, unsigned int row) const +sACNDiscoveredSourceListModel::modelContainer::getUniverse(const CID& cid, unsigned int row) const { - QMutexLocker locker(&listMutex); + QMutexLocker locker(&listMutex); - if (!sources.contains(cid)) - return 0; + if (!sources.contains(cid)) + return 0; - const auto sourceRow = sources.indexOf(cid); + const auto sourceRow = sources.indexOf(cid); + if (sourceRow >= universes.size()) + return 0; - if (row >= static_cast(universes.size())) - return 0; + if (row >= static_cast(universes.at(sourceRow).size())) + return 0; - return universes.at(sourceRow).at(row); + return universes.at(sourceRow).at(row); } -int sACNDiscoveredSourceListModel::modelContainer::getRow(const CID &cid) const +int sACNDiscoveredSourceListModel::modelContainer::getRow(const CID& cid) const { - QMutexLocker locker(&listMutex); - return sources.indexOf(cid); + QMutexLocker locker(&listMutex); + return sources.indexOf(cid); } -int sACNDiscoveredSourceListModel::modelContainer::getRow(const CID &cid, universe_t universe) const +int sACNDiscoveredSourceListModel::modelContainer::getRow(const CID& cid, universe_t universe) const { - QMutexLocker locker(&listMutex); - const auto sourceRow = sources.indexOf(cid); - if (sourceRow == -1) - return -1; + QMutexLocker locker(&listMutex); + const auto sourceRow = sources.indexOf(cid); + if (sourceRow == -1) + return -1; - return universes.at(sourceRow).indexOf(universe); + return universes.at(sourceRow).indexOf(universe); } qsizetype sACNDiscoveredSourceListModel::modelContainer::count() const { - QMutexLocker locker(&listMutex); - return sources.count(); + QMutexLocker locker(&listMutex); + return sources.count(); } -qsizetype sACNDiscoveredSourceListModel::modelContainer::count(const CID &cid) const +qsizetype sACNDiscoveredSourceListModel::modelContainer::count(const CID& cid) const { - QMutexLocker locker(&listMutex); + QMutexLocker locker(&listMutex); - const auto sourceRow = sources.indexOf(cid); - if (sourceRow == -1) - return 0; - return universes.at(sourceRow).count(); + const auto sourceRow = sources.indexOf(cid); + if (sourceRow == -1) + return 0; + return universes.at(sourceRow).count(); } -sACNDiscoveredSourceListModel::sACNDiscoveredSourceListModel(QObject *parent) : QAbstractItemModel(parent), - m_discoveryInstance(sACNDiscoveryRX::getInstance()), - m_displayDDOnlySource(Preferences::Instance().GetETCDisplayDDOnly()) +sACNDiscoveredSourceListModel::sACNDiscoveredSourceListModel(QObject* parent) : QAbstractItemModel(parent), +m_discoveryInstance(sACNDiscoveryRX::getInstance()), +m_displayDDOnlySource(Preferences::Instance().GetETCDisplayDDOnly()) { - // Add existing - auto it = m_discoveryInstance->getDiscoveryList().constBegin(); - while (it != m_discoveryInstance->getDiscoveryList().constEnd()) - newSource(it.key()); - - connect(m_discoveryInstance, SIGNAL(newSource(CID)), this, SLOT(newSource(CID))); - connect(m_discoveryInstance, SIGNAL(expiredSource(CID)), this, SLOT(expiredSource(CID))); - connect(m_discoveryInstance, SIGNAL(newUniverse(CID,quint16)), this, SLOT(newUniverse(CID,quint16))); - connect(m_discoveryInstance, SIGNAL(expiredUniverse(CID,quint16)), this, SLOT(expiredUniverse(CID,quint16))); + // Add existing + auto it = m_discoveryInstance->getDiscoveryList().constBegin(); + while (it != m_discoveryInstance->getDiscoveryList().constEnd()) + newSource(it.key()); + + connect(m_discoveryInstance, SIGNAL(newSource(CID)), this, SLOT(newSource(CID))); + connect(m_discoveryInstance, SIGNAL(expiredSource(CID)), this, SLOT(expiredSource(CID))); + connect(m_discoveryInstance, SIGNAL(newUniverse(CID, quint16)), this, SLOT(newUniverse(CID, quint16))); + connect(m_discoveryInstance, SIGNAL(expiredUniverse(CID, quint16)), this, SLOT(expiredUniverse(CID, quint16))); } void sACNDiscoveredSourceListModel::newSource(CID cid) { - if (m_sources.getRow(cid) == -1) - { - const auto childRow = m_sources.count(); - const bool resetModel = (childRow == 0); - if (resetModel) - beginResetModel(); // Reset dummy help row - else - beginInsertRows(QModelIndex(), childRow, childRow); - - m_sources.addSource(cid); - - if (resetModel) - endResetModel(); - else - endInsertRows(); - } + if (m_sources.getRow(cid) == -1) + { + const auto childRow = m_sources.count(); + + const bool appendSource = (childRow != 0); + if (appendSource) + beginInsertRows(QModelIndex(), childRow, childRow); + + m_sources.addSource(cid); + + if (appendSource) + endInsertRows(); + else + emit dataChanged(index(0,0), index(0,0)); // Update the first row + } } void sACNDiscoveredSourceListModel::expiredSource(CID cid) { - qDebug() << "******************* Expired source" << CID::CIDIntoQString(cid); + qDebug() << "******************* Expired source" << CID::CIDIntoQString(cid); + + if (m_sources.getRow(cid) != -1) + { + const auto childRow = m_sources.getRow(cid); + beginRemoveRows(QModelIndex(), childRow, childRow); + m_sources.removeSource(cid); + endRemoveRows(); - if(m_sources.getRow(cid) != -1) + if (m_sources.count() == 0) { - const auto childRow = m_sources.getRow(cid); - beginRemoveRows(QModelIndex(), childRow, childRow); - m_sources.removeSource(cid); - endRemoveRows(); - - if (m_sources.count() == 0) - { - // Restore dummy help row - beginResetModel(); - endResetModel(); - } + // Restore dummy help row + beginResetModel(); + endResetModel(); } + } } void sACNDiscoveredSourceListModel::newUniverse(CID cid, quint16 universe) { - if (m_sources.getRow(cid) != -1) + if (m_sources.getRow(cid) != -1) + { + const auto parentRow = m_sources.getRow(cid); + auto parent = index(parentRow, 0, QModelIndex()); + + if (m_sources.getRow(cid, universe) == -1) { - const auto parentRow = m_sources.getRow(cid); - auto parent = index(parentRow,0, QModelIndex()); - - if (m_sources.getRow(cid, universe) == -1) - { - const auto childRow = m_sources.count(cid); - beginInsertRows(parent, childRow, childRow); - m_sources.addUniverse(cid, universe); - endInsertRows(); - } + const auto childRow = m_sources.count(cid); + beginInsertRows(parent, childRow, childRow); + m_sources.addUniverse(cid, universe); + endInsertRows(); } + } } void sACNDiscoveredSourceListModel::expiredUniverse(CID cid, quint16 universe) { - qDebug() << " Expired universe" << CID::CIDIntoQString(cid) << universe; + qDebug() << " Expired universe" << CID::CIDIntoQString(cid) << universe; + + if (m_sources.getRow(cid) != -1) + { + const auto parentRow = m_sources.getRow(cid); + auto parent = index(parentRow, 0, QModelIndex()); + const auto childRow = m_sources.getRow(cid, universe); - if (m_sources.getRow(cid) != -1) + if (childRow != -1) { - const auto parentRow = m_sources.getRow(cid); - auto parent = index(parentRow,0, QModelIndex()); - const auto childRow = m_sources.getRow(cid, universe); - - if (childRow != -1) - { - beginRemoveRows(parent, childRow, childRow); - m_sources.removeUniverse(cid, universe); - endRemoveRows(); - } + beginRemoveRows(parent, childRow, childRow); + m_sources.removeUniverse(cid, universe); + endRemoveRows(); } + } } -int sACNDiscoveredSourceListModel::indexToUniverse(const QModelIndex &index) +int sACNDiscoveredSourceListModel::indexToUniverse(const QModelIndex& index) { - if(!index.isValid()) - return 0; + if (!index.isValid()) + return 0; - if(index.internalPointer()==Q_NULLPTR) - // Source - return 0; + if (index.internalPointer() == Q_NULLPTR) + // Source + return 0; - if (index.parent().isValid()) - { - const auto cid = m_sources.getCID(index.parent().row()); - if (cid == CID()) - return 0; + if (index.parent().isValid()) + { + const auto cid = m_sources.getCID(index.parent().row()); + if (cid == CID()) + return 0; - return m_sources.getUniverse(cid, index.row()); - } + return m_sources.getUniverse(cid, index.row()); + } - return 0; + return 0; } -int sACNDiscoveredSourceListModel::columnCount(const QModelIndex &parent) const +int sACNDiscoveredSourceListModel::columnCount(const QModelIndex& parent) const { - Q_UNUSED(parent); - return 1; + Q_UNUSED(parent); + return 1; } -QVariant sACNDiscoveredSourceListModel::data(const QModelIndex &index, int role) const +QVariant sACNDiscoveredSourceListModel::data(const QModelIndex& index, int role) const { - if(!index.isValid()) return QVariant(); - - // Details that no sources found - if (!m_sources.count()) { - if(role==Qt::DisplayRole) - { - return QVariant(tr("No discovery sources found")); - } else if(role==Qt::ToolTipRole) { - return QVariant(tr("Discovery relies upon\n" - "E1-31:2016 universe discovery packets\n" - "very few sources support this")); - } - return QVariant(); + if (!index.isValid()) return QVariant(); + + // Details that no sources found + if (!m_sources.count()) { + if (role == Qt::DisplayRole) + { + return QVariant(tr("No discovery sources found")); + } + else if (role == Qt::ToolTipRole) { + return QVariant(tr("Discovery relies upon\n" + "E1-31:2016 universe discovery packets\n" + "very few sources support this")); } + return QVariant(); + } + + switch (role) + { + default: break; + case Qt::ToolTipRole: + if (!index.parent().isValid()) + { + // Show CID as a tooltip + const auto cid = m_sources.getCID(index.row()); + return QVariant(CID::CIDIntoQString(cid)); + } + break; - if(role==Qt::ToolTipRole) + case Qt::DisplayRole: + if (index.parent().isValid()) { - if(index.parent().isValid()) - { - return QVariant(); - } - else - { - // Show CID as a tooltip - const auto cid = m_sources.getCID(index.row()); - return QVariant(CID::CIDIntoQString(cid)); - } + // Display universes + const auto cid = m_sources.getCID(index.parent().row()); + const auto universe = m_sources.getUniverse(cid, index.row()); + if (universe < MIN_SACN_UNIVERSE) + return tr("Error"); + return tr("Universe %1").arg(universe); } - if(role==Qt::DisplayRole) + else { - if(index.parent().isValid()) - { - // Display universes - const auto cid = m_sources.getCID(index.parent().row()); - return QVariant( - QString("Universe %1") - .arg(m_sources.getUniverse(cid, index.row()))); - } - else - { - // Source Name - const auto cid = m_sources.getCID(index.row()); - return QVariant(m_discoveryInstance->getDiscoveryList().find(cid).value()->Name); - } + // Source Name + const auto cid = m_sources.getCID(index.row()); + const auto it = m_discoveryInstance->getDiscoveryList().find(cid); + if (it != m_discoveryInstance->getDiscoveryList().end()) + return tr("%1 (%2 universes)").arg(it.value()->Name).arg(it.value()->Universe.size()); } + break; - if(role==Qt::UserRole && index.parent().isValid()) + case Qt::UserRole: + if (index.parent().isValid()) { - // Universe number - const auto cid = m_sources.getCID(index.parent().row()); - return QVariant(m_sources.getUniverse(cid, index.row())); + // Universe number + const auto cid = m_sources.getCID(index.parent().row()); + return QVariant(m_sources.getUniverse(cid, index.row())); } - return QVariant(); + break; + } + return QVariant(); } -QModelIndex sACNDiscoveredSourceListModel::index(int row, int column, const QModelIndex &parent) const +QModelIndex sACNDiscoveredSourceListModel::index(int row, int column, const QModelIndex& parent) const { - if(!hasIndex(row, column, parent)) - return QModelIndex(); - if(!parent.isValid()) // This is a root item - return createIndex(row, column); - if(parent.isValid() && !parent.parent().isValid()) + if (!hasIndex(row, column, parent)) + return QModelIndex(); + if (!parent.isValid()) // This is a root item + return createIndex(row, column); + if (parent.isValid() && !parent.parent().isValid()) + { + const auto cid = m_sources.getCID(parent.row()); + if (cid == CID()) + return QModelIndex(); + + if (m_sources.count(cid) > row) { - const auto cid = m_sources.getCID(parent.row()); - if (cid == CID()) - return QModelIndex(); - - if (m_sources.count(cid) > row) - { - return createIndex(row, column, parent.row() + 1); - } + return createIndex(row, column, parent.row() + 1); } + } - return QModelIndex(); + return QModelIndex(); } -QModelIndex sACNDiscoveredSourceListModel::parent(const QModelIndex &index) const +QModelIndex sACNDiscoveredSourceListModel::parent(const QModelIndex& index) const { - if(!index.isValid()) - return QModelIndex(); + if (!index.isValid()) + return QModelIndex(); - if(index.internalId()) - { - QModelIndex ret = createIndex(index.internalId() - 1, 0); - return ret; - } + if (index.internalId()) + { + QModelIndex ret = createIndex(index.internalId() - 1, 0); + return ret; + } - return QModelIndex(); + return QModelIndex(); } -int sACNDiscoveredSourceListModel::rowCount(const QModelIndex &parent) const +int sACNDiscoveredSourceListModel::rowCount(const QModelIndex& parent) const { - // Sources count - if(!parent.isValid()) - { - if (!m_sources.count()) return 1; // Details that no sources found - return m_sources.count(); - } - - // Universe count of source - if(parent.isValid() && parent.internalPointer()==Q_NULLPTR) - { - const auto cid = m_sources.getCID(parent.row()); - if (cid == CID()) - return 0; - return m_sources.count(cid); - } - - return 0; + // Sources count + if (!parent.isValid()) + { + if (!m_sources.count()) return 1; // Details that no sources found + return m_sources.count(); + } + + // Universe count of source + if (parent.isValid() && parent.internalPointer() == Q_NULLPTR) + { + const auto cid = m_sources.getCID(parent.row()); + if (cid == CID()) + return 0; + return m_sources.count(cid); + } + + return 0; } /******************************************* sACNDiscoveredSourceListProxy *********************************************/ -sACNDiscoveredSourceListProxy::sACNDiscoveredSourceListProxy(QObject *parent) : - QSortFilterProxyModel (parent), - collator() +sACNDiscoveredSourceListProxy::sACNDiscoveredSourceListProxy(QObject* parent) : + QSortFilterProxyModel(parent), + collator() { - // Used for sorting of the list - collator.setNumericMode(true); - collator.setCaseSensitivity(sortCaseSensitivity()); + // Used for sorting of the list + collator.setNumericMode(true); + collator.setCaseSensitivity(sortCaseSensitivity()); } void sACNDiscoveredSourceListProxy::setSortCaseSensitivity(Qt::CaseSensitivity cs) { - collator.setCaseSensitivity(cs); - QSortFilterProxyModel::setSortCaseSensitivity(cs); + collator.setCaseSensitivity(cs); + QSortFilterProxyModel::setSortCaseSensitivity(cs); } -bool sACNDiscoveredSourceListProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const +bool sACNDiscoveredSourceListProxy::lessThan(const QModelIndex& left, const QModelIndex& right) const { - switch (sortColumn()) - { - case 0: // Source Names - { - QString leftString = sourceModel()->data(left).toString(); - QString rightString = sourceModel()->data(right).toString(); - - return collator.compare(leftString, rightString) < 0; - } - - case 1: // Universe number - { - int leftUniverse = sourceModel()->data(left, Qt::UserRole).toInt(); - int rightUniverse = sourceModel()->data(right, Qt::UserRole).toInt(); - return leftUniverse < rightUniverse; - } - - default: - return false; - } + switch (sortColumn()) + { + case 0: // Source Names + { + QString leftString = sourceModel()->data(left).toString(); + QString rightString = sourceModel()->data(right).toString(); + + return collator.compare(leftString, rightString) < 0; + } + + case 1: // Universe number + { + int leftUniverse = sourceModel()->data(left, Qt::UserRole).toInt(); + int rightUniverse = sourceModel()->data(right, Qt::UserRole).toInt(); + return leftUniverse < rightUniverse; + } + + default: + return false; + } } diff --git a/src/models/sacndiscoveredsourcelistmodel.h b/src/models/sacndiscoveredsourcelistmodel.h index 43cc3b9..fb36f4d 100644 --- a/src/models/sacndiscoveredsourcelistmodel.h +++ b/src/models/sacndiscoveredsourcelistmodel.h @@ -17,74 +17,74 @@ */ class sACNDiscoveredSourceListModel : public QAbstractItemModel { - Q_OBJECT + Q_OBJECT public: - sACNDiscoveredSourceListModel(QObject *parent = Q_NULLPTR); + sACNDiscoveredSourceListModel(QObject* parent = Q_NULLPTR); - int indexToUniverse(const QModelIndex &index); + int indexToUniverse(const QModelIndex& index); - int rowCount(const QModelIndex &parent = QModelIndex()) const override ; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + int rowCount(const QModelIndex& parent = QModelIndex()) const override; + int columnCount(const QModelIndex& parent = QModelIndex()) const override; + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; protected: - QModelIndex index(int row, int column, const QModelIndex &parent) const override; - QModelIndex parent(const QModelIndex &index) const override; + QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; + QModelIndex parent(const QModelIndex& index) const override; public slots: private slots: - void newSource(CID cid); - void expiredSource(CID cid); - void newUniverse(CID cid, quint16 universe); - void expiredUniverse(CID cid, quint16 universe); + void newSource(CID cid); + void expiredSource(CID cid); + void newUniverse(CID cid, quint16 universe); + void expiredUniverse(CID cid, quint16 universe); private: - mutable QReadWriteLock rwlock_ModelIndex; - sACNDiscoveryRX *m_discoveryInstance; - bool m_displayDDOnlySource; + mutable QReadWriteLock rwlock_ModelIndex; + sACNDiscoveryRX* m_discoveryInstance; + bool m_displayDDOnlySource; - class modelContainer - { - public: - typedef quint16 universe_t; + class modelContainer + { + public: + typedef quint16 universe_t; - void addSource(const CID &cid); - void removeSource(const CID &cid); + void addSource(const CID& cid); + void removeSource(const CID& cid); - void addUniverse(const CID &cid, universe_t universe); - void removeUniverse(const CID &cid, universe_t universe); + void addUniverse(const CID& cid, universe_t universe); + void removeUniverse(const CID& cid, universe_t universe); - CID getCID(unsigned int row) const; - universe_t getUniverse(const CID &cid, unsigned int row) const; + CID getCID(unsigned int row) const; + universe_t getUniverse(const CID& cid, unsigned int row) const; - int getRow(const CID &cid) const; - int getRow(const CID &cid, universe_t universe) const; + int getRow(const CID& cid) const; + int getRow(const CID& cid, universe_t universe) const; - /** - * @brief Count - * @return Number of sources - */ - qsizetype count() const; + /** + * @brief Count + * @return Number of sources + */ + qsizetype count() const; - /** - * @brief Count - * @param cid Source - * @return Number of universes for source - */ - qsizetype count(const CID &cid) const; + /** + * @brief Count + * @param cid Source + * @return Number of universes for source + */ + qsizetype count(const CID& cid) const; - private: - mutable QMutex listMutex; + private: + mutable QMutex listMutex; - QList sources; // List of sources CIDs - typedef QList universeList_t; - QList universes; // List of universes for source, key is m_sources row - }; + QList sources; // List of sources CIDs + typedef QList universeList_t; + QList universes; // List of universes for source, key is m_sources row + }; - modelContainer m_sources; + modelContainer m_sources; }; /** @@ -94,18 +94,18 @@ private slots: */ class sACNDiscoveredSourceListProxy : public QSortFilterProxyModel { - Q_OBJECT + Q_OBJECT public: - sACNDiscoveredSourceListProxy(QObject *parent = Q_NULLPTR); + sACNDiscoveredSourceListProxy(QObject* parent = Q_NULLPTR); - void setSortCaseSensitivity(Qt::CaseSensitivity cs); + void setSortCaseSensitivity(Qt::CaseSensitivity cs); protected: - virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; + virtual bool lessThan(const QModelIndex& left, const QModelIndex& right) const override; private: - QCollator collator; + QCollator collator; }; #endif // sACNDiscoveredSourceListModel_H