Merge lp:~ahayzen/ubuntu-ui-extras/job-model-filters-for-queue-dialog into lp:~phablet-team/ubuntu-ui-extras/printer-staging

Proposed by Andrew Hayzen
Status: Merged
Approved by: Jonas G. Drange
Approved revision: 144
Merged at revision: 144
Proposed branch: lp:~ahayzen/ubuntu-ui-extras/job-model-filters-for-queue-dialog
Merge into: lp:~phablet-team/ubuntu-ui-extras/printer-staging
Diff against target: 254 lines (+123/-17)
6 files modified
modules/Ubuntu/Components/Extras/Printers/enums.h (+5/-0)
modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp (+75/-3)
modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h (+17/-2)
modules/Ubuntu/Components/Extras/Printers/printers/printers.cpp (+12/-0)
modules/Ubuntu/Components/Extras/Printers/printers/printers.h (+2/-0)
tests/unittests/Printers/tst_jobmodel.cpp (+12/-12)
To merge this branch: bzr merge lp:~ahayzen/ubuntu-ui-extras/job-model-filters-for-queue-dialog
Reviewer Review Type Date Requested Status
Jonas G. Drange (community) Approve
Review via email: mp+319048@code.launchpad.net

Commit message

* Add filters for active, paused and queued
* Add sorting by creationTime and then falling back to id
* Change time related roles in JobModel to return QDateTime, not QString, so sorting doesn't break
* Update tests

Description of the change

* Add filters for active, paused and queued
* Add sorting by creationTime and then falling back to id
* Change time related roles in JobModel to return QDateTime, not QString, so sorting doesn't break
* Update tests

To post a comment you must log in.
Revision history for this message
Jonas G. Drange (jonas-drange) wrote :

Couple of minor comments, but looks real fine!

review: Needs Fixing
145. By Andrew Hayzen

* Change QList to QSet
* Add qHash for JobState
* Make Q_INVOKABLE methods Q_SLOTS
* Add comment about QML ownership

Revision history for this message
Jonas G. Drange (jonas-drange) wrote :

good show!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'modules/Ubuntu/Components/Extras/Printers/enums.h'
2--- modules/Ubuntu/Components/Extras/Printers/enums.h 2017-02-23 14:04:26 +0000
3+++ modules/Ubuntu/Components/Extras/Printers/enums.h 2017-03-06 13:19:21 +0000
4@@ -103,4 +103,9 @@
5 Q_ENUM(PrinterType)
6 };
7
8+inline uint qHash(const PrinterEnum::JobState &state, uint seed)
9+{
10+ return qHash((int) state, seed);
11+}
12+
13 #endif // USC_PRINTERS_ENUMS_H
14
15=== modified file 'modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp'
16--- modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp 2017-03-02 14:52:47 +0000
17+++ modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp 2017-03-06 13:19:21 +0000
18@@ -175,13 +175,13 @@
19 break;
20 }
21 case CompletedTimeRole:
22- ret = job->completedTime().toString(QLocale::system().dateTimeFormat());
23+ ret = job->completedTime();
24 break;
25 case CopiesRole:
26 ret = job->copies();
27 break;
28 case CreationTimeRole:
29- ret = job->creationTime().toString(QLocale::system().dateTimeFormat());
30+ ret = job->creationTime();
31 break;
32 case DuplexRole: {
33 if (job->printer()) {
34@@ -217,7 +217,7 @@
35 ret = QVariant::fromValue<PrinterEnum::PrintRange>(job->printRangeMode());
36 break;
37 case ProcessingTimeRole:
38- ret = job->processingTime().toString(QLocale::system().dateTimeFormat());
39+ ret = job->processingTime();
40 break;
41 case QualityRole: {
42 if (job->printer()) {
43@@ -359,6 +359,33 @@
44 return rowCount();
45 }
46
47+void JobFilter::filterOnActive()
48+{
49+ m_activeStates = QSet<PrinterEnum::JobState>{
50+ PrinterEnum::JobState::Processing,
51+ };
52+ m_activeFilterEnabled = true;
53+ invalidate();
54+}
55+
56+void JobFilter::filterOnPaused()
57+{
58+ m_pausedStates = QSet<PrinterEnum::JobState>{
59+ PrinterEnum::JobState::Held,
60+ };
61+ m_pausedFilterEnabled = true;
62+ invalidate();
63+}
64+
65+void JobFilter::filterOnQueued()
66+{
67+ m_queuedStates = QSet<PrinterEnum::JobState>{
68+ PrinterEnum::JobState::Pending,
69+ };
70+ m_queuedFilterEnabled = true;
71+ invalidate();
72+}
73+
74 void JobFilter::filterOnPrinterName(const QString &name)
75 {
76 m_printerName = name;
77@@ -378,5 +405,50 @@
78 accepts = m_printerName == printerName;
79 }
80
81+ if (accepts && m_activeFilterEnabled) {
82+ PrinterEnum::JobState state = childIndex.model()->data(
83+ childIndex, JobModel::StateRole
84+ ).value<PrinterEnum::JobState>();
85+
86+ accepts = m_activeStates.contains(state);
87+ }
88+
89+ if (accepts && m_pausedFilterEnabled) {
90+ PrinterEnum::JobState state = childIndex.model()->data(
91+ childIndex, JobModel::StateRole
92+ ).value<PrinterEnum::JobState>();
93+
94+ accepts = m_pausedStates.contains(state);
95+ }
96+
97+ if (accepts && m_queuedFilterEnabled) {
98+ PrinterEnum::JobState state = childIndex.model()->data(
99+ childIndex, JobModel::StateRole
100+ ).value<PrinterEnum::JobState>();
101+
102+ accepts = m_queuedStates.contains(state);
103+ }
104+
105 return accepts;
106 }
107+
108+bool JobFilter::lessThan(const QModelIndex &source_left,
109+ const QModelIndex &source_right) const
110+{
111+ QVariant leftData = sourceModel()->data(source_left, sortRole());
112+ QVariant rightData = sourceModel()->data(source_right, sortRole());
113+
114+ if (sortRole() == (int) JobModel::CreationTimeRole) {
115+ // If creationDateTime is the same, fallback to Id
116+ if (leftData.toDateTime() == rightData.toDateTime()) {
117+ int leftId = sourceModel()->data(source_left, JobModel::IdRole).toInt();
118+ int rightId = sourceModel()->data(source_right, JobModel::IdRole).toInt();
119+
120+ return leftId < rightId;
121+ } else {
122+ return leftData.toDateTime() < rightData.toDateTime();
123+ }
124+ } else {
125+ return leftData < rightData;
126+ }
127+}
128
129=== modified file 'modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h'
130--- modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h 2017-03-01 12:53:55 +0000
131+++ modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h 2017-03-06 13:19:21 +0000
132@@ -103,13 +103,19 @@
133 explicit JobFilter(QObject *parent = Q_NULLPTR);
134 ~JobFilter();
135
136- Q_INVOKABLE QVariantMap get(const int row) const;
137-
138 void filterOnPrinterName(const QString &name);
139 int count() const;
140+public Q_SLOTS:
141+ QVariantMap get(const int row) const;
142+
143+ void filterOnActive();
144+ void filterOnQueued();
145+ void filterOnPaused();
146 protected:
147 virtual bool filterAcceptsRow(
148 int sourceRow, const QModelIndex &sourceParent) const override;
149+ virtual bool lessThan(const QModelIndex &source_left,
150+ const QModelIndex &source_right) const;
151
152 Q_SIGNALS:
153 void countChanged();
154@@ -121,6 +127,15 @@
155 private:
156 QString m_printerName = QString::null;
157 bool m_printerNameFilterEnabled = false;
158+
159+ bool m_activeFilterEnabled = false;
160+ QSet<PrinterEnum::JobState> m_activeStates;
161+
162+ bool m_queuedFilterEnabled = false;
163+ QSet<PrinterEnum::JobState> m_queuedStates;
164+
165+ bool m_pausedFilterEnabled = false;
166+ QSet<PrinterEnum::JobState> m_pausedStates;
167 };
168
169 #endif // USC_JOB_MODEL_H
170
171=== modified file 'modules/Ubuntu/Components/Extras/Printers/printers/printers.cpp'
172--- modules/Ubuntu/Components/Extras/Printers/printers/printers.cpp 2017-03-02 14:19:56 +0000
173+++ modules/Ubuntu/Components/Extras/Printers/printers/printers.cpp 2017-03-06 13:19:21 +0000
174@@ -144,6 +144,18 @@
175 return new PrinterJob(printerName, m_backend);
176 }
177
178+QAbstractItemModel* Printers::createJobFilter()
179+{
180+ // Note: If called by QML, it gains ownership of the job filter.
181+ JobFilter *filter = new JobFilter();
182+ filter->setSourceModel(&m_jobs);
183+
184+ filter->setSortRole(JobModel::Roles::CreationTimeRole);
185+ filter->sort(0, Qt::AscendingOrder);
186+
187+ return filter;
188+}
189+
190 void Printers::cancelJob(const QString &printerName, const int jobId)
191 {
192 m_backend->cancelJob(printerName, jobId);
193
194=== modified file 'modules/Ubuntu/Components/Extras/Printers/printers/printers.h'
195--- modules/Ubuntu/Components/Extras/Printers/printers/printers.h 2017-03-02 14:19:56 +0000
196+++ modules/Ubuntu/Components/Extras/Printers/printers/printers.h 2017-03-06 13:19:21 +0000
197@@ -62,6 +62,8 @@
198
199 public Q_SLOTS:
200 PrinterJob* createJob(const QString &printerName);
201+ QAbstractItemModel* createJobFilter();
202+
203 void cancelJob(const QString &printerName, const int jobId);
204 void holdJob(const QString &printerName, const int jobId);
205 void releaseJob(const QString &printerName, const int jobId);
206
207=== modified file 'tests/unittests/Printers/tst_jobmodel.cpp'
208--- tests/unittests/Printers/tst_jobmodel.cpp 2017-03-02 12:49:06 +0000
209+++ tests/unittests/Printers/tst_jobmodel.cpp 2017-03-06 13:19:21 +0000
210@@ -192,10 +192,10 @@
211 m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
212
213 QTRY_COMPARE(m_model->count(), 2);
214- QCOMPARE(m_model->data(m_model->index(0), JobModel::CompletedTimeRole).toString(),
215- dateTimeA.toString(QLocale::system().dateTimeFormat()));
216- QCOMPARE(m_model->data(m_model->index(1), JobModel::CompletedTimeRole).toString(),
217- dateTimeB.toString(QLocale::system().dateTimeFormat()));
218+ QCOMPARE(m_model->data(m_model->index(0), JobModel::CompletedTimeRole).toDateTime(),
219+ dateTimeA);
220+ QCOMPARE(m_model->data(m_model->index(1), JobModel::CompletedTimeRole).toDateTime(),
221+ dateTimeB);
222 }
223 void testCopiesRole()
224 {
225@@ -227,10 +227,10 @@
226 m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
227
228 QTRY_COMPARE(m_model->count(), 2);
229- QCOMPARE(m_model->data(m_model->index(0), JobModel::CreationTimeRole).toString(),
230- dateTimeA.toString(QLocale::system().dateTimeFormat()));
231- QCOMPARE(m_model->data(m_model->index(1), JobModel::CreationTimeRole).toString(),
232- dateTimeB.toString(QLocale::system().dateTimeFormat()));
233+ QCOMPARE(m_model->data(m_model->index(0), JobModel::CreationTimeRole).toDateTime(),
234+ dateTimeA);
235+ QCOMPARE(m_model->data(m_model->index(1), JobModel::CreationTimeRole).toDateTime(),
236+ dateTimeB);
237 }
238 void testDuplexRole()
239 {
240@@ -385,10 +385,10 @@
241 m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
242
243 QTRY_COMPARE(m_model->count(), 2);
244- QCOMPARE(m_model->data(m_model->index(0), JobModel::ProcessingTimeRole).toString(),
245- dateTimeA.toString(QLocale::system().dateTimeFormat()));
246- QCOMPARE(m_model->data(m_model->index(1), JobModel::ProcessingTimeRole).toString(),
247- dateTimeB.toString(QLocale::system().dateTimeFormat()));
248+ QCOMPARE(m_model->data(m_model->index(0), JobModel::ProcessingTimeRole).toDateTime(),
249+ dateTimeA);
250+ QCOMPARE(m_model->data(m_model->index(1), JobModel::ProcessingTimeRole).toDateTime(),
251+ dateTimeB);
252 }
253 void testQualityRole()
254 {

Subscribers

People subscribed via source and target branches