on xenial help sign in
Trying to get rid of the whole QSharedPointer mess.
5105786Mirco Müller
tests/mocks/Unity/Notifications/MockNotificationModel.cpp+36−43
@@ -32,18 +32,14 @@
32
32
33using namespace unity::shell::notifications;
33using namespace unity::shell::notifications;
34
34
35−struct MockNotificationModelPrivate {
36− QList<QSharedPointer<MockNotification> > queue;
37−};
38−
39−MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent), p(new MockNotificationModelPrivate) {
35+MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent) {
40}
36}
41
37
42MockNotificationModel::~MockNotificationModel() {
38MockNotificationModel::~MockNotificationModel() {
43}
39}
44
40
45int MockNotificationModel::rowCount(const QModelIndex &parent) const {
41int MockNotificationModel::rowCount(const QModelIndex &parent) const {
46− return p->queue.size();
42+ return m_queue.size();
47}
43}
48
44
49QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
45QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
@@ -53,69 +49,67 @@ QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
53
49
54 switch(role) {
50 switch(role) {
55 case ModelInterface::RoleType:
51 case ModelInterface::RoleType:
56− return QVariant(p->queue[index.row()]->getType());
52+ return QVariant(m_queue[index.row()]->getType());
57
53
58 case ModelInterface::RoleId:
54 case ModelInterface::RoleId:
59− return QVariant(p->queue[index.row()]->getID());
55+ return QVariant(m_queue[index.row()]->getID());
60
56
61 case ModelInterface::RoleSummary:
57 case ModelInterface::RoleSummary:
62− return QVariant(p->queue[index.row()]->getSummary());
58+ return QVariant(m_queue[index.row()]->getSummary());
63
59
64 case ModelInterface::RoleBody:
60 case ModelInterface::RoleBody:
65− return QVariant(p->queue[index.row()]->getBody());
61+ return QVariant(m_queue[index.row()]->getBody());
66
62
67 case ModelInterface::RoleValue:
63 case ModelInterface::RoleValue:
68− return QVariant(p->queue[index.row()]->getValue());
64+ return QVariant(m_queue[index.row()]->getValue());
69
65
70 case ModelInterface::RoleIcon:
66 case ModelInterface::RoleIcon:
71− return QVariant(p->queue[index.row()]->getIcon());
67+ return QVariant(m_queue[index.row()]->getIcon());
72
68
73 case ModelInterface::RoleSecondaryIcon:
69 case ModelInterface::RoleSecondaryIcon:
74− return QVariant(p->queue[index.row()]->getSecondaryIcon());
70+ return QVariant(m_queue[index.row()]->getSecondaryIcon());
75
71
76 case ModelInterface::RoleActions:
72 case ModelInterface::RoleActions:
77− return QVariant::fromValue(p->queue[index.row()]->getActions());
73+ return QVariant::fromValue(m_queue[index.row()]->getActions());
78
74
79 case ModelInterface::RoleHints:
75 case ModelInterface::RoleHints:
80− return QVariant(p->queue[index.row()]->getHints());
76+ return QVariant(m_queue[index.row()]->getHints());
81
77
82 case ModelInterface::RoleNotification:
78 case ModelInterface::RoleNotification:
83− return QVariant(p->queue[index.row()]);
79+ return QVariant(m_queue[index.row()]);
84
80
85 default:
81 default:
86 return QVariant();
82 return QVariant();
87 }
83 }
88}
84}
89
85
90−void MockNotificationModel::append(const QSharedPointer<MockNotification> &n) {
86+void MockNotificationModel::append(MockNotification* n) {
91− int location = p->queue.size();
87+ int location = m_queue.size();
92 QModelIndex insertionPoint = QModelIndex();
88 QModelIndex insertionPoint = QModelIndex();
93 beginInsertRows(insertionPoint, location, location);
89 beginInsertRows(insertionPoint, location, location);
94− p->queue.insert(location, n);
90+ m_queue.insert(location, QSharedPointer<MockNotification>(n));
95 endInsertRows();
91 endInsertRows();
96− qDebug() << "queue-size:" << p->queue.size();
92+ qDebug() << "queue-size:" << m_queue.size();
97}
93}
98
94
99−QSharedPointer<MockNotification> MockNotificationModel::getNotification(unsigned int id) const {
95+MockNotification* MockNotificationModel::getNotification(unsigned int id) const {
100− for(int i=0; i<p->queue.size(); i++) {
96+ for(int i=0; i < m_queue.size(); i++) {
101− if(p->queue[i]->getID() == id) {
97+ if(m_queue[i]->getID() == id) {
102− return p->queue[i];
98+ return m_queue[i];
103 }
99 }
104 }
100 }
105
101
106− QSharedPointer<MockNotification> empty;
107− return empty;
102+ return nullptr;
108}
103}
109
104
110−QSharedPointer<MockNotification> MockNotificationModel::getNotification(const QString &summary) const {
105+MockNotification* MockNotificationModel::getNotification(const QString &summary) const {
111− for(int i=0; i<p->queue.size(); i++) {
106+ for(int i=0; i < m_queue.size(); i++) {
112− if(p->queue[i]->getSummary() == summary) {
107+ if(m_queue[i]->getSummary() == summary) {
113− return p->queue[i];
108+ return m_queue[i];
114 }
109 }
115 }
110 }
116
111
117− QSharedPointer<MockNotification> empty;
118− return empty;
112+ return nullptr;
119}
113}
120
114
121bool MockNotificationModel::hasNotification(unsigned int id) const {
115bool MockNotificationModel::hasNotification(unsigned int id) const {
@@ -123,8 +117,8 @@ bool MockNotificationModel::hasNotification(unsigned int id) const {
123}
117}
124
118
125void MockNotificationModel::removeNotification(const unsigned int id) {
119void MockNotificationModel::removeNotification(const unsigned int id) {
126− for(int i=0; i<p->queue.size(); i++) {
120+ for(int i = 0; i < m_queue.size(); i++) {
127− if(p->queue[i]->getID() == id) {
121+ if(m_queue[i]->getID() == id) {
128 deleteFromVisible(i);
122 deleteFromVisible(i);
129 return;
123 return;
130 }
124 }
@@ -133,7 +127,7 @@ void MockNotificationModel::removeNotification(const unsigned int id) {
133}
127}
134
128
135void MockNotificationModel::deleteFirst() {
129void MockNotificationModel::deleteFirst() {
136− if(p->queue.empty())
130+ if(m_queue.empty())
137 return;
131 return;
138 deleteFromVisible(0);
132 deleteFromVisible(0);
139}
133}
@@ -141,16 +135,15 @@ void MockNotificationModel::deleteFirst() {
141void MockNotificationModel::deleteFromVisible(int loc) {
135void MockNotificationModel::deleteFromVisible(int loc) {
142 QModelIndex deletePoint = QModelIndex();
136 QModelIndex deletePoint = QModelIndex();
143 beginRemoveRows(deletePoint, loc, loc);
137 beginRemoveRows(deletePoint, loc, loc);
144− p->queue.erase(p->queue.begin() + loc);
138+ m_queue.erase(m_queue.begin() + loc);
145 endRemoveRows();
139 endRemoveRows();
146}
140}
147
141
148MockNotification* MockNotificationModel::getRaw(const unsigned int notificationId) const {
142MockNotification* MockNotificationModel::getRaw(const unsigned int notificationId) const {
149 qDebug() << "::getRaw()";
143 qDebug() << "::getRaw()";
150− for(int i=0; i<p->queue.size(); i++) {
144+ for(int i = 0; i < m_queue.size(); i++) {
151− if(p->queue[i]->getID() == notificationId) {
145+ if(m_queue[i]->getID() == notificationId) {
152− MockNotification* n = p->queue[i].data();
146+ MockNotification* n = m_queue[i];
153− //QQmlEngine::setObjectOwnership(n, QQmlEngine::CppOwnership);
154 return n;
147 return n;
155 }
148 }
156 }
149 }
@@ -159,7 +152,7 @@ MockNotification* MockNotificationModel::getRaw(const unsigned int notificationI
159}
152}
160
153
161int MockNotificationModel::queued() const {
154int MockNotificationModel::queued() const {
162− return p->queue.size();
155+ return m_queue.size();
163}
156}
164
157
165QHash<int, QByteArray> MockNotificationModel::roleNames() const {
158QHash<int, QByteArray> MockNotificationModel::roleNames() const {
@@ -181,8 +174,8 @@ QHash<int, QByteArray> MockNotificationModel::roleNames() const {
181}
174}
182
175
183void MockNotificationModel::onDataChanged(unsigned int id) {
176void MockNotificationModel::onDataChanged(unsigned int id) {
184− for(int i=0; i<p->queue.size(); i++) {
177+ for(int i = 0; i < m_queue.size(); i++) {
185− if(p->queue[i]->getID() == id) {
178+ if(m_queue[i]->getID() == id) {
186 Q_EMIT dataChanged(index(i, 0), index(i, 0));
179 Q_EMIT dataChanged(index(i, 0), index(i, 0));
187 break;
180 break;
188 }
181 }
tests/mocks/Unity/Notifications/MockNotificationModel.h+4−7
@@ -27,8 +27,6 @@
27
27
28class MockNotification;
28class MockNotification;
29
29
30−struct MockNotificationModelPrivate;
31−
32class MockNotificationModel : public QAbstractListModel {
30class MockNotificationModel : public QAbstractListModel {
33 Q_OBJECT
31 Q_OBJECT
34
32
@@ -40,9 +38,9 @@ public:
40 virtual QVariant data(const QModelIndex &index, int role) const;
38 virtual QVariant data(const QModelIndex &index, int role) const;
41 virtual QHash<int, QByteArray> roleNames() const;
39 virtual QHash<int, QByteArray> roleNames() const;
42
40
43− Q_INVOKABLE void append(const QSharedPointer<MockNotification> &n);
41+ Q_INVOKABLE void append(MockNotification* n);
44− QSharedPointer<MockNotification> getNotification(unsigned int id) const;
42+ MockNotification* getNotification(unsigned int id) const;
45− QSharedPointer<MockNotification> getNotification(const QString &summary) const;
43+ MockNotification* getNotification(const QString &summary) const;
46 bool hasNotification(unsigned int id) const;
44 bool hasNotification(unsigned int id) const;
47
45
48 // getRaw() is only meant to be used from QML, since QML cannot handle
46 // getRaw() is only meant to be used from QML, since QML cannot handle
@@ -59,8 +57,7 @@ Q_SIGNALS:
59 void queueSizeChanged(int newSize);
57 void queueSizeChanged(int newSize);
60
58
61private:
59private:
62− QScopedPointer<MockNotificationModelPrivate> p;
60+ QList<MockNotification*> m_queue;
63−
64 void deleteFromVisible(int loc);
61 void deleteFromVisible(int loc);
65 void deleteFirst();
62 void deleteFirst();
66};
63};
tests/qmltests/Notifications/tst_Notifications.qml+1−1
@@ -125,7 +125,7 @@ Item {
125
125
126 // add the default/PlaceHolder notification to the model
126 // add the default/PlaceHolder notification to the model
127 Component.onCompleted: {
127 Component.onCompleted: {
128− append(mockList[0])
128+ //append(mockList[0])
129 }
129 }
130 }
130 }
131
131