| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "molequeuequeuelistmodel.h" |
| 7 |
|
| 8 |
#include <QtCore/QDebug> |
| 9 |
|
| 10 |
#include <limits> |
| 11 |
|
| 12 |
namespace Avogadro::MoleQueue { |
| 13 |
|
| 14 |
namespace { |
| 15 |
// Internal id used for queue model indices |
| 16 |
static const quint32 QueueInternalId(std::numeric_limits<quint32>::max()); |
| 17 |
|
| 18 |
// Internal id used for invalid indices |
| 19 |
static const quint32 InvalidInternalId(std::numeric_limits<quint32>::max() - 1); |
| 20 |
|
| 21 |
// Maximum assignable internal id. Must be last: |
| 22 |
static const quint32 MaxInternalId(std::numeric_limits<quint32>::max() - 2); |
| 23 |
} // namespace |
| 24 |
|
| 25 |
MoleQueueQueueListModel::MoleQueueQueueListModel(QObject* parent_) |
| 26 |
: QAbstractItemModel(parent_), m_uidCounter(0) |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
void MoleQueueQueueListModel::setQueueList(QList<QString> queueList, |
| 31 |
QList<QStringList> programList) |
| 32 |
{ |
| 33 |
const int numQueues = queueList.size(); |
| 34 |
if (numQueues != programList.size()) { |
| 35 |
qWarning() << "Error setting molequeue queuelist data in model: " |
| 36 |
"number of queues does not match size of program table."; |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// Sync our data structures with the arguments: |
| 41 |
int newInd = 0; |
| 42 |
int oldInd = 0; |
| 43 |
while (newInd < queueList.size() && oldInd < m_queueList.size()) { |
| 44 |
const QString& newQueue = queueList[newInd]; |
| 45 |
const QString& oldQueue = m_queueList[oldInd]; |
| 46 |
if (newQueue < oldQueue) { |
| 47 |
const QStringList& newProgs = programList[newInd]; |
| 48 |
insertQueue(oldInd, newQueue, newProgs); |
| 49 |
++oldInd; |
| 50 |
++newInd; |
| 51 |
} else if (oldQueue < newQueue) { |
| 52 |
removeQueue(oldInd); |
| 53 |
} else { // newQueue == oldQueue |
| 54 |
const QStringList& newProgs = programList[newInd]; |
| 55 |
mergeQueue(oldInd, newProgs); |
| 56 |
++oldInd; |
| 57 |
++newInd; |
| 58 |
} |
| 59 |
} |
| 60 |
// Add any remaining new queues |
| 61 |
for (; newInd < queueList.size(); ++newInd, ++oldInd) |
| 62 |
insertQueue(m_queueList.size(), queueList[newInd], programList[newInd]); |
| 63 |
// or remove any stale old queues. |
| 64 |
while (oldInd < m_queueList.size()) |
| 65 |
removeQueue(oldInd); |
| 66 |
} |
| 67 |
|
| 68 |
QStringList MoleQueueQueueListModel::queues() const |
| 69 |
{ |
| 70 |
return m_queueList; |
| 71 |
} |
| 72 |
|
| 73 |
QStringList MoleQueueQueueListModel::programs(const QString& queue) const |
| 74 |
{ |
| 75 |
int ind = m_queueList.indexOf(queue); |
| 76 |
return ind >= 0 ? m_programList[ind] : QStringList(); |
| 77 |
} |
| 78 |
|
| 79 |
QModelIndexList MoleQueueQueueListModel::findQueueIndices( |
| 80 |
const QString& filter) const |
| 81 |
{ |
| 82 |
return match(index(0, 0), Qt::DisplayRole, filter, -1, Qt::MatchContains); |
| 83 |
} |
| 84 |
|
| 85 |
QModelIndexList MoleQueueQueueListModel::findProgramIndices( |
| 86 |
const QString& programFilter, const QString& queueFilter) const |
| 87 |
{ |
| 88 |
QModelIndexList result; |
| 89 |
foreach (const QModelIndex& idx, findQueueIndices(queueFilter)) { |
| 90 |
result << match(index(0, 0, idx), Qt::DisplayRole, programFilter, -1, |
| 91 |
Qt::MatchContains); |
| 92 |
} |
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
bool MoleQueueQueueListModel::lookupProgram(const QModelIndex& idx, |
| 97 |
QString& queueName, |
| 98 |
QString& programName) const |
| 99 |
{ |
| 100 |
QVariant resultVariant = data(idx, QueueProgramRole); |
| 101 |
if (resultVariant.type() == QVariant::StringList) { |
| 102 |
QStringList resultList(resultVariant.toStringList()); |
| 103 |
if (resultList.size() == 2) { |
| 104 |
queueName = resultList[0]; |
| 105 |
programName = resultList[1]; |
| 106 |
return true; |
| 107 |
} |
| 108 |
} |
| 109 |
queueName.clear(); |
| 110 |
programName.clear(); |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
QVariant MoleQueueQueueListModel::data(const QModelIndex& idx, int role) const |
| 115 |
{ |
| 116 |
if (!idx.isValid() || (role != Qt::DisplayRole && role != QueueProgramRole)) { |
| 117 |
return QVariant(); |
| 118 |
} |
| 119 |
|
| 120 |
if (isQueueIndex(idx)) { |
| 121 |
if (role == Qt::DisplayRole) { |
| 122 |
return m_queueList[idx.row()]; |
| 123 |
} |
| 124 |
} else { |
| 125 |
const int queueIndex(idx.parent().row()); |
| 126 |
if (queueIndex < m_queueList.size()) { |
| 127 |
const QStringList& progs(m_programList[queueIndex]); |
| 128 |
if (idx.row() < progs.size()) { |
| 129 |
const QString& prog(progs[idx.row()]); |
| 130 |
switch (role) { |
| 131 |
case Qt::DisplayRole: |
| 132 |
return prog; |
| 133 |
case QueueProgramRole: { |
| 134 |
QStringList result = m_uidLookup.value( |
| 135 |
static_cast<quint32>(idx.internalId()), QStringList()); |
| 136 |
if (result.size() == 2) |
| 137 |
return result; |
| 138 |
break; |
| 139 |
} |
| 140 |
default: |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
return QVariant(); |
| 147 |
} |
| 148 |
|
| 149 |
Qt::ItemFlags MoleQueueQueueListModel::flags(const QModelIndex& idx) const |
| 150 |
{ |
| 151 |
if (!idx.isValid()) |
| 152 |
return Qt::NoItemFlags; |
| 153 |
|
| 154 |
if (isQueueIndex(idx)) |
| 155 |
return Qt::ItemIsEnabled; |
| 156 |
else |
| 157 |
return Qt::ItemIsSelectable | Qt::ItemIsEnabled; |
| 158 |
} |
| 159 |
|
| 160 |
QVariant MoleQueueQueueListModel::headerData(int, Qt::Orientation, int) const |
| 161 |
{ |
| 162 |
return QVariant(); |
| 163 |
} |
| 164 |
|
| 165 |
QModelIndex MoleQueueQueueListModel::index(int row, int column, |
| 166 |
const QModelIndex& parent_) const |
| 167 |
{ |
| 168 |
if (!hasIndex(row, column, parent_)) |
| 169 |
return QModelIndex(); |
| 170 |
|
| 171 |
// Queue Index -- parent is invalid. |
| 172 |
if (!parent_.isValid() && row < m_queueList.size() && column == 0) { |
| 173 |
return createIndex(row, column, QueueInternalId); |
| 174 |
} |
| 175 |
// Program index |
| 176 |
else if (isQueueIndex(parent_)) { |
| 177 |
const QStringList& progs(m_programList[parent_.row()]); |
| 178 |
if (row < progs.size() && column == 0) { |
| 179 |
const QString& queue(m_queueList[parent_.row()]); |
| 180 |
const QString& prog(progs[row]); |
| 181 |
QStringList val = QStringList() << queue << prog; |
| 182 |
quint32 key = m_uidLookup.key(val, InvalidInternalId); |
| 183 |
if (key != InvalidInternalId) |
| 184 |
return createIndex(row, column, key); |
| 185 |
} |
| 186 |
} |
| 187 |
// fail. |
| 188 |
return QModelIndex(); |
| 189 |
} |
| 190 |
|
| 191 |
QModelIndex MoleQueueQueueListModel::parent(const QModelIndex& child) const |
| 192 |
{ |
| 193 |
if (child.isValid()) { |
| 194 |
const auto childId = static_cast<quint32>(child.internalId()); |
| 195 |
|
| 196 |
// Child is queue -- return invalid parent. |
| 197 |
if (childId == QueueInternalId) |
| 198 |
return QModelIndex(); |
| 199 |
|
| 200 |
// Child is program -- lookup and return queue index. |
| 201 |
const int queueRow = programUidToQueueRow(childId); |
| 202 |
if (queueRow >= 0) |
| 203 |
return createIndex(queueRow, 0, QueueInternalId); |
| 204 |
} |
| 205 |
return QModelIndex(); |
| 206 |
} |
| 207 |
|
| 208 |
int MoleQueueQueueListModel::rowCount(const QModelIndex& parent_) const |
| 209 |
{ |
| 210 |
// Queue count: |
| 211 |
if (!parent_.isValid()) |
| 212 |
return m_queueList.size(); |
| 213 |
else if (isQueueIndex(parent_)) |
| 214 |
return m_programList[parent_.row()].size(); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
int MoleQueueQueueListModel::columnCount(const QModelIndex& parent_) const |
| 219 |
{ |
| 220 |
return (!parent_.isValid() || isQueueIndex(parent_)) ? 1 : 0; |
| 221 |
} |
| 222 |
|
| 223 |
void MoleQueueQueueListModel::insertQueue(int row, const QString& queue, |
| 224 |
const QStringList& progs) |
| 225 |
{ |
| 226 |
beginInsertRows(QModelIndex(), row, row); |
| 227 |
m_queueList.insert(row, queue); |
| 228 |
m_programList.insert(row, QStringList()); |
| 229 |
endInsertRows(); |
| 230 |
|
| 231 |
beginInsertRows(createIndex(row, 0, QueueInternalId), 0, progs.size() - 1); |
| 232 |
m_programList[row] = progs; |
| 233 |
foreach (const QString& progName, progs) |
| 234 |
m_uidLookup.insert(nextUid(), QStringList() << queue << progName); |
| 235 |
endInsertRows(); |
| 236 |
} |
| 237 |
|
| 238 |
void MoleQueueQueueListModel::removeQueue(int row) |
| 239 |
{ |
| 240 |
const QString queue(m_queueList[row]); |
| 241 |
QStringList& progs = m_programList[row]; |
| 242 |
|
| 243 |
beginRemoveRows(createIndex(row, 0, QueueInternalId), 0, progs.size() - 1); |
| 244 |
foreach (const QString& prog, progs) |
| 245 |
m_uidLookup.remove(lookupUid(queue, prog)); |
| 246 |
progs.clear(); |
| 247 |
endRemoveRows(); |
| 248 |
|
| 249 |
beginRemoveRows(QModelIndex(), row, row); |
| 250 |
m_queueList.removeAt(row); |
| 251 |
m_programList.removeAt(row); |
| 252 |
endRemoveRows(); |
| 253 |
} |
| 254 |
|
| 255 |
void MoleQueueQueueListModel::mergeQueue(int row, const QStringList& newProgs) |
| 256 |
{ |
| 257 |
QStringList& oldProgs(m_programList[row]); |
| 258 |
|
| 259 |
int oldInd = 0; |
| 260 |
int newInd = 0; |
| 261 |
while (oldInd < oldProgs.size() && newInd < newProgs.size()) { |
| 262 |
const QString& oldProg(oldProgs[oldInd]); |
| 263 |
const QString& newProg(newProgs[newInd]); |
| 264 |
if (newProg < oldProg) { |
| 265 |
insertProgram(row, oldInd, newProg); |
| 266 |
++newInd; |
| 267 |
++oldInd; |
| 268 |
} else if (oldProg < newProg) { |
| 269 |
removeProgram(row, oldInd); |
| 270 |
} else { // Program exists |
| 271 |
++newInd; |
| 272 |
++oldInd; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
// Add any remaining new programs |
| 277 |
for (; newInd < newProgs.size(); ++newInd, ++oldInd) |
| 278 |
insertProgram(row, m_programList[row].size(), newProgs[newInd]); |
| 279 |
|
| 280 |
// Or remove any old programs. |
| 281 |
while (oldInd < m_programList[row].size()) |
| 282 |
removeProgram(row, oldInd); |
| 283 |
} |
| 284 |
|
| 285 |
void MoleQueueQueueListModel::insertProgram(int queueRow, int progRow, |
| 286 |
const QString& progName) |
| 287 |
{ |
| 288 |
beginInsertRows(createIndex(queueRow, 0, QueueInternalId), progRow, progRow); |
| 289 |
m_programList[queueRow].insert(progRow, progName); |
| 290 |
m_uidLookup.insert(nextUid(), QStringList() |
| 291 |
<< m_queueList[queueRow] << progName); |
| 292 |
endInsertRows(); |
| 293 |
} |
| 294 |
|
| 295 |
void MoleQueueQueueListModel::removeProgram(int queueRow, int progRow) |
| 296 |
{ |
| 297 |
beginRemoveRows(createIndex(queueRow, 0, QueueInternalId), progRow, progRow); |
| 298 |
m_uidLookup.remove(lookupUid(queueRow, progRow)); |
| 299 |
m_programList[queueRow].removeAt(progRow); |
| 300 |
endRemoveRows(); |
| 301 |
} |
| 302 |
|
| 303 |
bool MoleQueueQueueListModel::isQueueIndex(const QModelIndex& i) const |
| 304 |
{ |
| 305 |
return i.isValid() && |
| 306 |
static_cast<quint32>(i.internalId()) == QueueInternalId && |
| 307 |
i.row() < m_queueList.size() && i.column() == 0; |
| 308 |
} |
| 309 |
|
| 310 |
bool MoleQueueQueueListModel::isProgramIndex(const QModelIndex& i) const |
| 311 |
{ |
| 312 |
return i.isValid() && |
| 313 |
m_uidLookup.contains(static_cast<quint32>(i.internalId())); |
| 314 |
} |
| 315 |
|
| 316 |
quint32 MoleQueueQueueListModel::lookupUid(const QString& queue, |
| 317 |
const QString& prog) |
| 318 |
{ |
| 319 |
return m_uidLookup.key(QStringList() << queue << prog, InvalidInternalId); |
| 320 |
} |
| 321 |
|
| 322 |
quint32 MoleQueueQueueListModel::lookupUid(const int queueRow, |
| 323 |
const int progRow) |
| 324 |
{ |
| 325 |
if (queueRow < m_queueList.size()) { |
| 326 |
QStringList& progs = m_programList[queueRow]; |
| 327 |
if (progRow < progs.size()) |
| 328 |
return lookupUid(m_queueList[queueRow], progs[progRow]); |
| 329 |
} |
| 330 |
return InvalidInternalId; |
| 331 |
} |
| 332 |
|
| 333 |
int MoleQueueQueueListModel::programUidToQueueRow(quint32 uid) const |
| 334 |
{ |
| 335 |
const QStringList val(m_uidLookup.value(uid, QStringList())); |
| 336 |
if (val.size() == 2) { |
| 337 |
const QString& queue = val[0]; |
| 338 |
const int queueRow = m_queueList.indexOf(queue); |
| 339 |
if (queueRow >= 0) |
| 340 |
return queueRow; |
| 341 |
} |
| 342 |
return -1; |
| 343 |
} |
| 344 |
|
| 345 |
quint32 MoleQueueQueueListModel::nextUid() |
| 346 |
{ |
| 347 |
if (m_uidCounter++ >= MaxInternalId) |
| 348 |
m_uidCounter = 0; |
| 349 |
return m_uidCounter; |
| 350 |
} |
| 351 |
|
| 352 |
} // namespace Avogadro::MoleQueue |