@@ -41,14 +41,40 @@ MediaPlayerUtils::MediaPlayerUtils(QObject *parent) : QObject(parent) {
41
41
m_startTimer = new QTimer (this );
42
42
m_startTimer->setSingleShot (true );
43
43
m_startTimer->setInterval (1000 );
44
+
45
+ m_worker = new MediaPlayerUtilsWorker ();
46
+ m_workerThread = new QThread (this );
47
+
48
+ QObject::connect (m_worker, &MediaPlayerUtilsWorker::processingDone, this , &MediaPlayerUtils::onProcessingDone);
49
+
50
+ m_worker->moveToThread (m_workerThread);
51
+ m_workerThread->start ();
44
52
}
45
53
46
54
MediaPlayerUtils::~MediaPlayerUtils () {
47
55
qCDebug (CLASS_LC ()) << this << " Destructor called" ;
48
56
m_engine->removeImageProvider (m_imageProviderId);
49
57
qCDebug (CLASS_LC ()) << this << " Destructor: Image provider removed:" << m_imageProviderId;
50
58
51
- deleteWorker ();
59
+ if (m_worker != nullptr ) {
60
+ qCDebug (CLASS_LC ()) << this << " Destructor: Worker was not null" ;
61
+ m_worker->disconnect ();
62
+ qCDebug (CLASS_LC ()) << this << " Destructor: Signal disconnected from Worker class" ;
63
+ delete m_worker;
64
+ m_worker = nullptr ;
65
+ qCDebug (CLASS_LC ()) << this << " Destructor: Worker deleted" ;
66
+ }
67
+
68
+ if (m_workerThread != nullptr ) {
69
+ qCDebug (CLASS_LC ()) << this << " Destructor: WorkerThread was not null" ;
70
+ if (m_workerThread->isRunning ()) {
71
+ m_workerThread->exit ();
72
+ m_workerThread->wait (5000 );
73
+ }
74
+ delete m_workerThread;
75
+ m_workerThread = nullptr ;
76
+ qCDebug (CLASS_LC ()) << this << " Destructor: WorkerThread was deleted" ;
77
+ }
52
78
53
79
if (m_startTimer->isActive ()) {
54
80
m_startTimer->stop ();
@@ -93,8 +119,6 @@ void MediaPlayerUtils::onProcessingDone(const QColor &pixelColor, const QImage &
93
119
m_pixelColor = pixelColor;
94
120
emit pixelColorChanged ();
95
121
96
- deleteWorker ();
97
-
98
122
qCDebug (CLASS_LC ()) << this << " Processing done" ;
99
123
}
100
124
@@ -105,49 +129,10 @@ void MediaPlayerUtils::generateImages(const QString &url) {
105
129
m_prevImageURL = url;
106
130
emit processingStarted ();
107
131
108
- if (m_startTimer->isActive ()) {
109
- m_startTimer->stop ();
110
- qCDebug (CLASS_LC ()) << this << " Timer stopped" ;
111
- }
112
-
113
- deleteWorker ();
114
-
115
- m_worker = new MediaPlayerUtilsWorker ();
116
- m_workerThread = new QThread (this );
117
-
118
- qCDebug (CLASS_LC ()) << this << " New Worker and WorkerThread created" ;
119
-
120
- QObject::connect (m_worker, &MediaPlayerUtilsWorker::processingDone, this , &MediaPlayerUtils::onProcessingDone);
121
-
122
- m_worker->moveToThread (m_workerThread);
123
- m_workerThread->start ();
124
132
m_worker->generateImages (url);
125
133
}
126
134
}
127
135
128
- void MediaPlayerUtils::deleteWorker () {
129
- if (m_worker != nullptr ) {
130
- qCDebug (CLASS_LC ()) << this << " Deleteworker: Worker was not null" ;
131
- m_worker->disconnect ();
132
- qCDebug (CLASS_LC ()) << this << " Deleteworker: Signal disconnected from Worker class" ;
133
- delete m_worker;
134
- m_worker = nullptr ;
135
- qCDebug (CLASS_LC ()) << this << " Deleteworker: Worker deleted" ;
136
- }
137
-
138
- if (m_workerThread != nullptr ) {
139
- qCDebug (CLASS_LC ()) << this << " Deleteworker: WorkerThread was not null" ;
140
- if (m_workerThread->isRunning ()) {
141
- m_workerThread->exit ();
142
- m_workerThread->wait (5000 );
143
- }
144
- delete m_workerThread;
145
- m_workerThread = nullptr ;
146
- qCDebug (CLASS_LC ()) << this << " Deleteworker: WorkerThread was deleted" ;
147
- }
148
- qCDebug (CLASS_LC ()) << this << " Deleteworker: End" ;
149
- }
150
-
151
136
MediaPlayerUtilsWorker::MediaPlayerUtilsWorker (QObject *parent) : QObject(parent) {
152
137
m_manager = new QNetworkAccessManager ();
153
138
}
@@ -167,70 +152,73 @@ void MediaPlayerUtilsWorker::generateImages(const QString &url) {
167
152
}
168
153
169
154
void MediaPlayerUtilsWorker::generateImagesReply () {
170
- if (m_reply->error () == QNetworkReply::NoError) {
171
- // run image processing in different thread
172
- QImage image (280 , 280 , QImage::Format_RGB888);
173
- image.fill (Qt::black);
174
-
175
- if (!image.load (m_reply, nullptr )) {
176
- qCWarning (CLASS_LC) << " ERROR LOADING IMAGE" ;
177
- if (m_reply) {
178
- m_reply->deleteLater ();
179
- m_reply = nullptr ;
155
+ if (m_reply != nullptr ) {
156
+ m_reply->disconnect ();
157
+
158
+ if (m_reply->error () == QNetworkReply::NoError) {
159
+ // run image processing in different thread
160
+ QImage image (280 , 280 , QImage::Format_RGB888);
161
+ image.fill (Qt::black);
162
+
163
+ if (!image.load (m_reply, nullptr )) {
164
+ qCWarning (CLASS_LC) << " ERROR LOADING IMAGE" ;
165
+ if (m_reply) {
166
+ m_reply->deleteLater ();
167
+ m_reply = nullptr ;
168
+ }
169
+ return ;
170
+ } else {
171
+ // //////////////////////////////////////////////////////////////////
172
+ // / GET DOMINANT COLOR
173
+ // //////////////////////////////////////////////////////////////////
174
+ qCDebug (CLASS_LC ()) << this << " Getting dominant color" ;
175
+ // QColor m_pixelColor = dominantColor(image);
176
+
177
+ // shrink down image to 1x1 pixel and measure the color
178
+ QImage onePixel = image;
179
+ onePixel.scaled (1 , 1 , Qt::IgnoreAspectRatio);
180
+ QColor m_pixelColor = onePixel.pixel (1 , 1 );
181
+
182
+ // change the brightness of the color if it's too bright
183
+ if (m_pixelColor.lightness () > 150 ) {
184
+ m_pixelColor.setHsv (m_pixelColor.hue (), m_pixelColor.saturation (), (m_pixelColor.value () - 80 ));
185
+ }
186
+
187
+ // if the color is close to white, return black instead
188
+ if (m_pixelColor.lightness () > 210 ) {
189
+ m_pixelColor = QColor (" black" );
190
+ }
191
+
192
+ // //////////////////////////////////////////////////////////////////
193
+ // / CREATE LARGE BACKGROUND IMAGE
194
+ // //////////////////////////////////////////////////////////////////
195
+ qCDebug (CLASS_LC ()) << this << " Creating image" ;
196
+ // resize image
197
+ image.scaledToHeight (280 , Qt::SmoothTransformation);
198
+
199
+ // create noise layer
200
+ QImage noise (" :/images/mini-music-player/noise.png" );
201
+ noise.scaledToHeight (280 , Qt::SmoothTransformation);
202
+ noise = noise.convertToFormat (QImage::Format_ARGB32);
203
+
204
+ // merge the images together
205
+ QPainter painter (&image);
206
+ painter.drawImage (image.rect (), noise);
207
+ painter.end ();
208
+
209
+ qCDebug (CLASS_LC ()) << this << " Creating image DONE" ;
210
+
211
+ emit processingDone (m_pixelColor, image);
180
212
}
181
- return ;
182
213
} else {
183
- // //////////////////////////////////////////////////////////////////
184
- // / GET DOMINANT COLOR
185
- // //////////////////////////////////////////////////////////////////
186
- qCDebug (CLASS_LC ()) << this << " Getting dominant color" ;
187
- // QColor m_pixelColor = dominantColor(image);
188
-
189
- // shrink down image to 1x1 pixel and measure the color
190
- QImage onePixel = image;
191
- onePixel.scaled (1 , 1 , Qt::IgnoreAspectRatio);
192
- QColor m_pixelColor = onePixel.pixel (1 , 1 );
193
-
194
- // change the brightness of the color if it's too bright
195
- if (m_pixelColor.lightness () > 150 ) {
196
- m_pixelColor.setHsv (m_pixelColor.hue (), m_pixelColor.saturation (), (m_pixelColor.value () - 80 ));
197
- }
198
-
199
- // if the color is close to white, return black instead
200
- if (m_pixelColor.lightness () > 210 ) {
201
- m_pixelColor = QColor (" black" );
202
- }
203
-
204
- // //////////////////////////////////////////////////////////////////
205
- // / CREATE LARGE BACKGROUND IMAGE
206
- // //////////////////////////////////////////////////////////////////
207
- qCDebug (CLASS_LC ()) << this << " Creating image" ;
208
- // resize image
209
- image.scaledToHeight (280 , Qt::SmoothTransformation);
210
-
211
- // create noise layer
212
- QImage noise (" :/images/mini-music-player/noise.png" );
213
- noise.scaledToHeight (280 , Qt::SmoothTransformation);
214
- noise = noise.convertToFormat (QImage::Format_ARGB32);
215
-
216
- // merge the images together
217
- QPainter painter (&image);
218
- painter.drawImage (image.rect (), noise);
219
- painter.end ();
220
-
221
- qCDebug (CLASS_LC ()) << this << " Creating image DONE" ;
222
-
223
- emit processingDone (m_pixelColor, image);
214
+ qCWarning (CLASS_LC) << this << " NETWORK REPLY ERROR" << m_reply->errorString ();
215
+ emit processingDone (QColor (" black" ), QImage ());
224
216
}
225
- } else {
226
- qCWarning (CLASS_LC) << this << " NETWORK REPLY ERROR" << m_reply->errorString ();
227
- emit processingDone (QColor (" black" ), QImage ());
228
- }
229
- if (m_reply != nullptr ) {
217
+
230
218
delete m_reply;
231
219
m_reply = nullptr ;
220
+ qCDebug (CLASS_LC ()) << this << " Network reply deleted" ;
232
221
}
233
- qCDebug (CLASS_LC ()) << this << " Network reply deleted" ;
234
222
}
235
223
236
224
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments