112 lines
2.3 KiB
C++
112 lines
2.3 KiB
C++
#include "particlelistmodel.h"
|
|
#include <QApplication>
|
|
#include <QStyle>
|
|
|
|
ParticleListModel::ParticleListModel(Data *data, QObject *parent)
|
|
: QAbstractListModel(parent), m_data(data), m_currentStreamId()
|
|
{
|
|
}
|
|
|
|
int ParticleListModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid())
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (m_currentStreamId.isEmpty())
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return m_data->particleCount(m_currentStreamId);
|
|
}
|
|
|
|
QVariant ParticleListModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid())
|
|
{
|
|
return QVariant();
|
|
}
|
|
|
|
if (m_currentStreamId.isEmpty())
|
|
{
|
|
return QVariant();
|
|
}
|
|
|
|
const QList<Particle> &particles = m_data->particlesForStream(m_currentStreamId);
|
|
|
|
if (index.row() < 0 || index.row() >= particles.size())
|
|
{
|
|
return QVariant();
|
|
}
|
|
|
|
const Particle &particle = particles.at(index.row());
|
|
|
|
if (role == Qt::DisplayRole)
|
|
{
|
|
QString emailPrefix = particle.createdBy.split("@")[0];
|
|
QString display = QString("[%1] - %2").arg(emailPrefix, particle.summary);
|
|
if (!particle.seen)
|
|
{
|
|
display += " •";
|
|
}
|
|
return display;
|
|
}
|
|
else if (role == Qt::DecorationRole)
|
|
{
|
|
// Return icon based on type
|
|
QStyle::StandardPixmap pixmap;
|
|
if (particle.type == "text")
|
|
pixmap = QStyle::SP_FileIcon;
|
|
else if (particle.type == "link")
|
|
pixmap = QStyle::SP_CommandLink;
|
|
else if (particle.type == "image")
|
|
pixmap = QStyle::SP_FileDialogContentsView;
|
|
else
|
|
return QVariant();
|
|
|
|
return qApp->style()->standardIcon(pixmap);
|
|
}
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
void ParticleListModel::setStreamId(const QString &streamId)
|
|
{
|
|
beginResetModel();
|
|
m_currentStreamId = streamId;
|
|
endResetModel();
|
|
}
|
|
|
|
int ParticleListModel::firstUnseenRow() const
|
|
{
|
|
if (m_currentStreamId.isEmpty())
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
const QList<Particle> &particles = m_data->particlesForStream(m_currentStreamId);
|
|
for (int i = 0; i < particles.size(); ++i)
|
|
{
|
|
if (!particles.at(i).seen)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
|
|
return -1; // All seen or empty
|
|
}
|
|
|
|
const Particle *ParticleListModel::particleAtRow(int row) const
|
|
{
|
|
if (m_currentStreamId.isEmpty())
|
|
return nullptr;
|
|
|
|
const QList<Particle> &particles = m_data->particlesForStream(m_currentStreamId);
|
|
if (row >= 0 && row < particles.size())
|
|
return &particles.at(row);
|
|
|
|
return nullptr;
|
|
}
|