Debian Codegraph @

avogadro/molequeue/molequeuequeuelistmodel.h

raw
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 #ifndef AVOGADRO_MOLEQUEUE_MOLEQUEUEQUEUELISTMODEL_H
7 #define AVOGADRO_MOLEQUEUE_MOLEQUEUEQUEUELISTMODEL_H
8
9 #include "avogadromolequeueexport.h"
10 #include <QtCore/QAbstractItemModel>
11
12 #include <QtCore/QStringList>
13
14 // for gtest unit testing access
15 class MoleQueueQueueListModelTestBridge;
16
17 namespace Avogadro::MoleQueue {
18 class MoleQueueManager;
19
20 /**
21 * @class MoleQueueQueueListModel molequeuequeuelistmodel.h
22 * <avogadro/molequeue/molequeuequeuelistmodel.h>
23 * @brief The MoleQueueQueueListModel class is Qt item model representing the
24 * tree of available queues and programs in a running MoleQueue process.
25 *
26 * This class provides access to the available MoleQueue queues and programs in
27 * a convenient tree item model. Resources can be queried directly using the
28 * queues() and programs() methods, or this item model may be used with a Qt
29 * model view class, such a QTreeView.
30 *
31 * QModelIndex objects that match a queue or program node may be found using the
32 * findQueueIndices() and findProgramIndices() methods. A program model index
33 * may be translated into queue and program strings using the lookupProgram()
34 * method.
35 *
36 * An instance of this class is obtained by calling
37 * MoleQueueManager::instance().queueListModel(), and can be updated by calling
38 * MoleQueueManager::instance().requestQueueList() and waiting for the
39 * MoleQueueManager::queueListUpdated() signal.
40 */
41 class AVOGADROMOLEQUEUE_EXPORT MoleQueueQueueListModel
42 : public QAbstractItemModel
43 {
44 Q_OBJECT
45 public:
46 ~MoleQueueQueueListModel() override = default;
47
48 /**
49 * @return A list of the available queues.
50 */
51 QStringList queues() const;
52
53 /**
54 * @return A list of programs belonging to @a queue.
55 */
56 QStringList programs(const QString& queue) const;
57
58 /**
59 * @return A QModelIndexList containing indices for queues that contain the
60 * string @a filter. Matches are case-insensitive.
61 */
62 QModelIndexList findQueueIndices(const QString& filter = QString()) const;
63
64 /**
65 * @return A QModelIndexList containing indices for programs that contain the
66 * string @a programFilter and belong to queues that contain @a queueFilter.
67 * Matches are case-insensitive.
68 */
69 QModelIndexList findProgramIndices(
70 const QString& programFilter = QString(),
71 const QString& queueFilter = QString()) const;
72
73 /**
74 * Translate a QModelIndex for a program node into queue and program strings.
75 * @param idx The model index.
76 * @param queueName String reference to be overwritten with the queue name.
77 * @param programName String reference to be overwritten with the queue name.
78 * @return True if the index matched a program node, false otherwise.
79 */
80 bool lookupProgram(const QModelIndex& idx, QString& queueName,
81 QString& programName) const;
82
83 // QAbstractItemModel virtuals
84 QVariant data(const QModelIndex& idx, int role) const override;
85 Qt::ItemFlags flags(const QModelIndex& idx = QModelIndex()) const override;
86 QVariant headerData(int section, Qt::Orientation orientation,
87 int role) const override;
88 QModelIndex index(int row, int column,
89 const QModelIndex& parent_ = QModelIndex()) const override;
90 QModelIndex parent(const QModelIndex& child) const override;
91 int rowCount(const QModelIndex& parent_ = QModelIndex()) const override;
92 int columnCount(const QModelIndex& parent_ = QModelIndex()) const override;
93
94 protected:
95 friend class MoleQueueManager;
96 friend class ::MoleQueueQueueListModelTestBridge;
97
98 /**
99 * Protected constructor. Keeps objects isolated to MoleQueueManager ivars.
100 */
101 explicit MoleQueueQueueListModel(QObject* parent_ = nullptr);
102
103 /**
104 * Used to retrieve a QStringList with [queueName, programName] from data()
105 * given a program model index.
106 */
107 enum
108 {
109 QueueProgramRole = Qt::UserRole
110 };
111
112 /**
113 * Merge the queue and program lists with the existing model.
114 */
115 void setQueueList(QList<QString> queueList, QList<QStringList> programList);
116
117 private:
118 void insertQueue(int row, const QString& queue, const QStringList& progs);
119 void removeQueue(int row);
120 void mergeQueue(int row, const QStringList& newProgs);
121 void insertProgram(int queueRow, int progRow, const QString& progName);
122 void removeProgram(int queueRow, int progRow);
123
124 bool isQueueIndex(const QModelIndex& i) const;
125 bool isProgramIndex(const QModelIndex& i) const;
126
127 quint32 lookupUid(const QString& queue, const QString& prog);
128 quint32 lookupUid(const int queueRow, const int progRow);
129 int programUidToQueueRow(quint32 uid) const;
130
131 quint32 nextUid();
132
133 QList<QString> m_queueList;
134 QList<QStringList> m_programList;
135 // maps program index internal id to [queueName, programName] QStringList
136 QMap<quint32, QStringList> m_uidLookup;
137 quint32 m_uidCounter;
138 };
139
140 } // namespace Avogadro::MoleQueue
141
142 #endif // AVOGADRO_MOLEQUEUE_MOLEQUEUEQUEUELISTMODEL_H