1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include "./syncthingmodel.h"
#include "./syncthingicons.h"
#include <syncthingconnector/syncthingconnection.h>
namespace Data {
SyncthingModel::SyncthingModel(SyncthingConnection &connection, QObject *parent)
: QAbstractItemModel(parent)
, m_connection(connection)
, m_brightColors(false)
, m_singleColumnMode(true)
{
connect(&m_connection, &SyncthingConnection::newConfig, this, &SyncthingModel::handleConfigInvalidated);
connect(&m_connection, &SyncthingConnection::newConfigApplied, this, &SyncthingModel::handleNewConfigAvailable);
const auto &iconManager = IconManager::instance();
connect(&iconManager, &IconManager::statusIconsChanged, this, &SyncthingModel::handleStatusIconsChanged);
connect(&iconManager, &IconManager::forkAwesomeIconsChanged, this, &SyncthingModel::handleForkAwesomeIconsChanged);
}
const QVector<int> &SyncthingModel::colorRoles() const
{
static const QVector<int> colorRoles;
return colorRoles;
}
void SyncthingModel::invalidateTopLevelIndicies(const QVector<int> &affectedRoles)
{
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1), affectedRoles);
}
void SyncthingModel::invalidateNestedIndicies(const QVector<int> &affectedRoles)
{
for (auto i = 0, rows = rowCount(); i != rows; ++i) {
const auto parentIndex = index(i, 0);
const auto childRows = rowCount(parentIndex);
if (childRows > 0) {
emit dataChanged(index(0, 0, parentIndex), index(childRows - 1, columnCount(parentIndex) - 1, parentIndex), affectedRoles);
}
}
}
void SyncthingModel::invalidateAllIndicies(const QVector<int> &affectedRoles, const QModelIndex &parentIndex)
{
const auto rows = rowCount(parentIndex);
const auto columns = columnCount(parentIndex);
if (rows <= 0 || columns <= 0) {
return;
}
const auto topLeftIndex = index(0, 0, parentIndex);
const auto bottomRightIndex = index(rows - 1, columns - 1, parentIndex);
emit dataChanged(topLeftIndex, bottomRightIndex, affectedRoles);
for (auto row = 0; row != rows; ++row) {
if (const auto idx = index(row, 0, parentIndex); idx.isValid()) {
invalidateAllIndicies(affectedRoles, idx);
}
}
}
void SyncthingModel::invalidateAllIndicies(const QVector<int> &affectedRoles, int column, const QModelIndex &parentIndex)
{
const auto rows = rowCount(parentIndex);
const auto columns = columnCount(parentIndex);
if (rows <= 0 || column >= columns) {
return;
}
const auto topLeftIndex = index(0, column, parentIndex);
const auto bottomRightIndex = index(rows - 1, column, parentIndex);
emit dataChanged(topLeftIndex, bottomRightIndex, affectedRoles);
for (auto row = 0; row != rows; ++row) {
if (const auto idx = index(row, 0, parentIndex); idx.isValid()) {
invalidateAllIndicies(affectedRoles, column, idx);
}
}
}
void SyncthingModel::setBrightColors(bool brightColors)
{
if (m_brightColors == brightColors) {
return;
}
m_brightColors = brightColors;
handleBrightColorsChanged();
}
void SyncthingModel::handleConfigInvalidated()
{
beginResetModel();
}
void SyncthingModel::handleNewConfigAvailable()
{
endResetModel();
}
void SyncthingModel::handleStatusIconsChanged()
{
}
void SyncthingModel::handleForkAwesomeIconsChanged()
{
}
void SyncthingModel::handleBrightColorsChanged()
{
if (const QVector<int> &affectedRoles = colorRoles(); !affectedRoles.isEmpty()) {
invalidateTopLevelIndicies(affectedRoles);
}
}
/*!
* \brief Sets whether the model should only have a single column.
* \remarks
* - This is the default but ignored by some derived models.
* - This is used so all the data can be rendered as a single column avoiding the inflexible behavior of QTreeView
* when it comes to sizing columns.
*/
void SyncthingModel::setSingleColumnMode(bool singleColumnModeEnabled)
{
if (m_singleColumnMode != singleColumnModeEnabled) {
if (m_singleColumnMode) {
beginInsertColumns(QModelIndex(), 1, 1);
m_singleColumnMode = true;
endInsertColumns();
} else {
beginRemoveColumns(QModelIndex(), 1, 1);
m_singleColumnMode = false;
endRemoveColumns();
}
}
}
/*!
* \brief Implements columnCount() in a way that makes sense for most derived classes taking singleColumnMode() into
* account.
*/
int SyncthingModel::columnCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return singleColumnMode() ? 1 : 2; // label/name/ID, status/buttons
} else if (!parent.parent().isValid()) {
return singleColumnMode() ? 1 : 2; // field name and value (or file and progress)
} else {
return 0;
}
}
} // namespace Data
|