subsurface/core/connectionlistmodel.cpp
Dirk Hohndel b241d09694 mobile/models: correctly reset the connection model
Qt hates empty ranges, and even for a non-empty range, this is better
implemented as a reset than a remove.

This fixes a crash that I have been able to create on iOS by rescanning
for devices on the download page.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2020-04-10 17:18:17 -07:00

56 lines
1.2 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include "core/connectionlistmodel.h"
ConnectionListModel::ConnectionListModel(QObject *parent) :
QAbstractListModel(parent)
{
}
QHash <int, QByteArray> ConnectionListModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "display";
return roles;
}
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_addresses.count())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
return m_addresses[index.row()];
}
int ConnectionListModel::rowCount(const QModelIndex&) const
{
return m_addresses.count();
}
void ConnectionListModel::addAddress(const QString &address)
{
if (!m_addresses.contains(address)) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_addresses.append(address);
endInsertRows();
}
}
void ConnectionListModel::removeAllAddresses()
{
if (rowCount() == 0)
return;
beginResetModel();
m_addresses.clear();
endResetModel();
}
int ConnectionListModel::indexOf(const QString &address) const
{
for (int i = 0; i < m_addresses.count(); i++)
if (m_addresses.at(i).contains(address, Qt::CaseInsensitive))
return i;
return -1;
}