| 1 |
/* |
| 2 |
This file is part of Icecream. |
| 3 |
|
| 4 |
Copyright (c) 2012 Kevin Funk <[email protected]> |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along |
| 17 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "hostlistmodel.h" |
| 22 |
#include "monitor.h" |
| 23 |
|
| 24 |
#include <QLocale> |
| 25 |
#include <QApplication> |
| 26 |
#include <QPalette> |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
|
| 30 |
HostListModel::HostListModel(QObject *parent) |
| 31 |
: QAbstractListModel(parent) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
Monitor *HostListModel::monitor() const |
| 36 |
{ |
| 37 |
return m_monitor; |
| 38 |
} |
| 39 |
|
| 40 |
void HostListModel::setMonitor(Monitor *monitor) |
| 41 |
{ |
| 42 |
if (m_monitor == monitor) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if (m_monitor) { |
| 47 |
disconnect(m_monitor.data(), SIGNAL(nodeRemoved(HostId)), this, SLOT(removeNodeById(HostId))); |
| 48 |
disconnect(m_monitor.data(), SIGNAL(nodeUpdated(HostId)), this, SLOT(checkNode(HostId))); |
| 49 |
} |
| 50 |
|
| 51 |
beginResetModel(); |
| 52 |
m_hostInfos.clear(); |
| 53 |
m_monitor = monitor; |
| 54 |
fill(); |
| 55 |
endResetModel(); |
| 56 |
|
| 57 |
if (m_monitor) { |
| 58 |
connect(m_monitor.data(), SIGNAL(nodeRemoved(HostId)), this, SLOT(removeNodeById(HostId))); |
| 59 |
connect(m_monitor.data(), SIGNAL(nodeUpdated(HostId)), this, SLOT(checkNode(HostId))); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
QVariant HostListModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 64 |
{ |
| 65 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 66 |
switch (section) { |
| 67 |
case ColumnID: |
| 68 |
return tr("ID"); |
| 69 |
case ColumnName: |
| 70 |
return tr("Name"); |
| 71 |
case ColumnNoRemote: |
| 72 |
return tr("No remote?"); |
| 73 |
case ColumnColor: |
| 74 |
return tr("Color"); |
| 75 |
case ColumnIP: |
| 76 |
return tr("IP"); |
| 77 |
case ColumnPlatform: |
| 78 |
return tr("Platform"); |
| 79 |
case ColumnProtocol: |
| 80 |
return tr("Protocol"); |
| 81 |
case ColumnFeatures: |
| 82 |
return tr("Features"); |
| 83 |
case ColumnMaxJobs: |
| 84 |
return tr("Max Jobs"); |
| 85 |
case ColumnSpeed: |
| 86 |
return tr("Speed"); |
| 87 |
case ColumnLoad: |
| 88 |
return tr("Load"); |
| 89 |
default: |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return QAbstractItemModel::headerData(section, orientation, role); |
| 95 |
} |
| 96 |
|
| 97 |
QVariant HostListModel::data(const QModelIndex &index, int role) const |
| 98 |
{ |
| 99 |
if (!index.isValid()) { |
| 100 |
return QVariant(); |
| 101 |
} |
| 102 |
|
| 103 |
const HostInfo info = hostInfoForIndex(index); |
| 104 |
const int column = index.column(); |
| 105 |
if (role == HostIdRole) { |
| 106 |
return info.id(); |
| 107 |
} else if (role == Qt::DisplayRole) { |
| 108 |
switch (column) { |
| 109 |
case ColumnID: |
| 110 |
return info.id(); |
| 111 |
case ColumnName: |
| 112 |
return info.name(); |
| 113 |
case ColumnNoRemote: |
| 114 |
return info.noRemote() ? tr("Yes") : QLatin1String(""); |
| 115 |
case ColumnColor: |
| 116 |
return HostInfo::colorName(info.color()); |
| 117 |
case ColumnIP: |
| 118 |
return info.ip(); |
| 119 |
case ColumnPlatform: |
| 120 |
return info.platform(); |
| 121 |
case ColumnProtocol: |
| 122 |
return info.protocol() > 0 ? QString::number(info.protocol()) : QStringLiteral("?"); |
| 123 |
case ColumnFeatures: |
| 124 |
return info.features().isEmpty() ? QStringLiteral( " - " ) : info.features(); |
| 125 |
case ColumnMaxJobs: |
| 126 |
return info.maxJobs(); |
| 127 |
case ColumnSpeed: |
| 128 |
return int( info.serverSpeed()); |
| 129 |
case ColumnLoad: |
| 130 |
return info.serverLoad(); |
| 131 |
default: |
| 132 |
break; |
| 133 |
} |
| 134 |
} else if (role == Qt::TextAlignmentRole) { |
| 135 |
switch (column) { |
| 136 |
case ColumnID: |
| 137 |
return Qt::AlignRight; |
| 138 |
case ColumnNoRemote: |
| 139 |
return Qt::AlignCenter; |
| 140 |
case ColumnProtocol: |
| 141 |
return Qt::AlignRight; |
| 142 |
case ColumnMaxJobs: |
| 143 |
return Qt::AlignRight; |
| 144 |
case ColumnSpeed: |
| 145 |
return Qt::AlignRight; |
| 146 |
case ColumnLoad: |
| 147 |
return Qt::AlignRight; |
| 148 |
default: |
| 149 |
break; |
| 150 |
} |
| 151 |
} else if (role == Qt::BackgroundRole) { |
| 152 |
if (info.noRemote()) { |
| 153 |
return QApplication::palette().color(QPalette::Disabled, QPalette::Base); |
| 154 |
} |
| 155 |
} else if (role == Qt::ForegroundRole) { |
| 156 |
if (info.noRemote()) { |
| 157 |
return QApplication::palette().color(QPalette::Disabled, QPalette::Text); |
| 158 |
} |
| 159 |
} |
| 160 |
return QVariant(); |
| 161 |
} |
| 162 |
|
| 163 |
int HostListModel::columnCount(const QModelIndex &parent) const |
| 164 |
{ |
| 165 |
Q_UNUSED(parent); |
| 166 |
return _ColumnCount; |
| 167 |
} |
| 168 |
|
| 169 |
int HostListModel::rowCount(const QModelIndex &parent) const |
| 170 |
{ |
| 171 |
Q_UNUSED(parent); |
| 172 |
return m_hostInfos.size(); |
| 173 |
} |
| 174 |
|
| 175 |
QModelIndex HostListModel::parent(const QModelIndex &child) const |
| 176 |
{ |
| 177 |
Q_UNUSED(child); |
| 178 |
return QModelIndex(); |
| 179 |
} |
| 180 |
|
| 181 |
HostInfo HostListModel::hostInfoForIndex(const QModelIndex &index) const |
| 182 |
{ |
| 183 |
return m_hostInfos.value(index.row()); |
| 184 |
} |
| 185 |
|
| 186 |
QModelIndex HostListModel::indexForHostInfo(const HostInfo &info, int column) const |
| 187 |
{ |
| 188 |
const int i = m_hostInfos.indexOf(info); |
| 189 |
return index(i, column); |
| 190 |
} |
| 191 |
|
| 192 |
void HostListModel::checkNode(unsigned int hostid) |
| 193 |
{ |
| 194 |
Q_ASSERT(m_monitor); |
| 195 |
|
| 196 |
const HostInfo *info = m_monitor->hostInfoManager()->find(hostid); |
| 197 |
if (!info) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
const int index = m_hostInfos.indexOf(*info); |
| 202 |
if (index != -1) { |
| 203 |
if (info->isOffline()) { |
| 204 |
removeNodeById(hostid); |
| 205 |
} else { |
| 206 |
m_hostInfos[index] = *info; |
| 207 |
emit dataChanged(indexForHostInfo(*info, 0), indexForHostInfo(*info, _ColumnCount - 1)); |
| 208 |
} |
| 209 |
} else if (!info->isOffline()) { |
| 210 |
beginInsertRows(QModelIndex(), m_hostInfos.size(), m_hostInfos.size()); |
| 211 |
m_hostInfos << *info; |
| 212 |
endInsertRows(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void HostListModel::removeNodeById(unsigned int hostId) |
| 217 |
{ |
| 218 |
auto it = std::find_if(m_hostInfos.begin(), m_hostInfos.end(), |
| 219 |
[hostId](const HostInfo &info) { |
| 220 |
return info.id() == hostId; |
| 221 |
}); |
| 222 |
if (it == m_hostInfos.end()) { |
| 223 |
return; |
| 224 |
} |
| 225 |
int index = std::distance(m_hostInfos.begin(), it); |
| 226 |
beginRemoveRows(QModelIndex(), index, index); |
| 227 |
m_hostInfos.erase(it); |
| 228 |
endRemoveRows(); |
| 229 |
} |
| 230 |
|
| 231 |
void HostListModel::fill() |
| 232 |
{ |
| 233 |
if (!m_monitor) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
const HostInfoManager *manager = m_monitor->hostInfoManager(); |
| 238 |
const HostInfoManager::HostMap hosts(manager->hostMap()); |
| 239 |
foreach(int hostid, hosts.keys()) { |
| 240 |
const HostInfo *info = m_monitor->hostInfoManager()->find(hostid); |
| 241 |
if (!info) { |
| 242 |
continue; |
| 243 |
} |
| 244 |
|
| 245 |
m_hostInfos << *info; |
| 246 |
} |
| 247 |
} |