1
- #include < ctime>
2
1
#include < iostream>
3
2
#include < fstream>
4
3
@@ -18,7 +17,7 @@ Controller::Controller(Settings* _settings, Environment* _enviro) :
18
17
m_enviro(_enviro),
19
18
m_set(NULL )
20
19
{
21
- m_randomEngine. seed (( unsigned int ) time ( NULL ) );
20
+ m_generator = new WallpaperGenerator ( this );
22
21
23
22
m_mainTimer = new QTimer (this );
24
23
connect (m_mainTimer, SIGNAL (timeout ()), this , SLOT (onUpdate ()));
@@ -107,58 +106,53 @@ bool Controller::startPause()
107
106
/* *
108
107
* @brief Update the wallpaper
109
108
*/
110
- void Controller::onUpdate ()
109
+ void Controller::onUpdate (bool _forceRefresh )
111
110
{
112
111
qxtLog->info (" Update !" );
113
112
114
113
// update delay if needed
115
114
int delay = m_settings->get (" delay" ).toInt ()*1000 ;
116
115
if (delay != m_mainTimer->interval ())
117
116
{
118
- qxtLog->debug (" Timer delay changed to: " +delay);
117
+ qxtLog->debug (" Timer delay changed to: " +QString::number ( delay) );
119
118
m_mainTimer->setInterval (delay);
120
119
}
121
120
122
121
// update config
123
- if (m_settings->get (" check" ).toBool ())
122
+ if (_forceRefresh || m_settings->get (" check" ).toBool ())
124
123
{
125
124
m_settings->updateSets ();
126
125
m_enviro->refreshMonitors ();
127
126
emit listChanged (false );
128
127
}
129
128
130
- int totalSets = m_settings->nbActiveSets (true );
129
+ // get random files
130
+ m_set = m_generator->getRandomSet ();
131
131
132
- if (totalSets == 0 )
132
+ if (m_set == NULL )
133
133
{
134
134
qxtLog->warning (" No active set" );
135
135
return ;
136
136
}
137
137
138
- // get random files
139
- m_set = getRandomSet (totalSets);
140
138
qxtLog->debug (" Current set: " +m_set->name ());
141
139
142
- m_files. clear ( );
140
+ m_files = m_generator-> getFiles (m_set );
143
141
144
- if (m_set-> type () == UM::W_MONITOR )
142
+ if (qxtLog-> isLogLevelEnabled ( " debug " , QxtLogger::DebugLevel) )
145
143
{
146
- for ( int i= 0 , l=m_enviro-> get ( " nb_monitors " ). toInt (); i<l; i++ )
144
+ foreach (QString file, m_files )
147
145
{
148
- m_files. append ( getRandomFile (m_set) );
146
+ qxtLog-> debug ( " Current file: " +file );
149
147
}
150
148
}
151
- else
152
- {
153
- m_files.append (getRandomFile (m_set));
154
- }
155
149
156
- QVector<QString> files = adaptFilesToFillMode (m_files, m_set );
150
+ QVector<QString> tempFiles = m_generator-> adaptFiles (m_set, m_files );
157
151
158
152
QString filename = m_enviro->get (" wallpath" ).toString () + QString::fromAscii (APP_WALLPAPER_FILE);
159
153
160
154
// generate .wallpaper file
161
- generateFile (filename, files , m_set);
155
+ generateFile (filename, tempFiles , m_set);
162
156
163
157
// remove old BMP file
164
158
QFile::remove (m_enviro->get (" bmppath" ).toString ());
@@ -172,70 +166,6 @@ void Controller::onUpdate()
172
166
emit wallpaperChanged ();
173
167
}
174
168
175
- /* *
176
- * @brief Get a random Set among all active sets
177
- * @param int _total - total number of Sets
178
- * @return Set*
179
- */
180
- Set* Controller::getRandomSet (int _total)
181
- {
182
- if (_total == 1 )
183
- {
184
- return m_settings->activeSet (0 );
185
- }
186
-
187
- uniform_int<int > unif (0 , _total-1 );
188
- int counter = unif (m_randomEngine);
189
-
190
- return m_settings->activeSet (counter);
191
- }
192
-
193
- /* *
194
- * @brief Get a random file within a Set
195
- * @param Set* _set
196
- * @return string
197
- */
198
- QString Controller::getRandomFile (Set* _set)
199
- {
200
- int total = _set->count ();
201
-
202
- // only one file in the set ?!
203
- if (total == 1 )
204
- {
205
- return _set->file (0 );
206
- }
207
-
208
- // rare case for very small sets
209
- if (total <= m_files.size ())
210
- {
211
- uniform_int<int > unif (0 , m_files.size ()-1 );
212
- int counter = unif (m_randomEngine);
213
- return m_files.at (counter);
214
- }
215
-
216
- // search a random unused file
217
- short loop = 10 ;
218
- QString file;
219
- uniform_int<int > unif (0 , total-1 );
220
-
221
- while (loop > 0 )
222
- {
223
- int counter = unif (m_randomEngine);
224
- file = _set->file (counter);
225
-
226
- if (!m_files.contains (file))
227
- {
228
- loop = 0 ;
229
- }
230
-
231
- loop--;
232
- }
233
-
234
- qxtLog->debug (" Current file: " +file);
235
-
236
- return file;
237
- }
238
-
239
169
/* *
240
170
* @brief Generate AutoChanger.wallpaper file
241
171
* @param string _filename
@@ -266,12 +196,16 @@ void Controller::generateFile(const QString &_filename, const QVector<QString> &
266
196
{
267
197
UM::WP_MONITOR_FILE wall;
268
198
wall.bgType = UM::BG_SOLID;
269
- wall.color1 = 0x00000000 ;
199
+ wall.color1 = m_settings-> monitor (i). color ;
270
200
wall.color2 = 0x00000000 ;
271
201
wall.imgStyle = wp_style;
272
202
273
203
memset (wall.imgFile , 0 , 260 *sizeof (wchar_t ));
274
- _files.at (i).toWCharArray ((wchar_t *)wall.imgFile );
204
+
205
+ if (!_files.at (i).isEmpty ())
206
+ {
207
+ _files.at (i).toWCharArray ((wchar_t *)wall.imgFile );
208
+ }
275
209
276
210
buffer.append ((char *)&wall, sizeof (UM::WP_MONITOR_FILE));
277
211
}
@@ -284,73 +218,23 @@ void Controller::generateFile(const QString &_filename, const QVector<QString> &
284
218
}
285
219
286
220
/* *
287
- * @brief Resize image files and return an array of new paths in cache folder
288
- * @param string[] _files
289
- * @param Set* _set
290
- * @return string[] _files
221
+ * @brief Move a file to trash bin
222
+ * @param string _filename
223
+ * @return bool
291
224
*/
292
- QVector<QString> Controller::adaptFilesToFillMode (const QVector< QString> &_files, const Set* _set )
225
+ bool Controller::moveFileToTrash (const QString &_filename )
293
226
{
294
- if (_set-> style () != UM::IM_FILL)
295
- {
296
- return _files ;
297
- }
227
+ wchar_t path[MAX_PATH];
228
+ memset (path, 0 , sizeof (path));
229
+ int l = _filename. toWCharArray (path) ;
230
+ path[l] = ' \0 ' ;
298
231
299
- QVector<QString> newFiles;
300
- int nb_walls = _files.size ();
232
+ SHFILEOPSTRUCT shfos = {0 };
233
+ shfos.wFunc = FO_DELETE;
234
+ shfos.pFrom = path;
235
+ shfos.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
301
236
302
- QString tmpRoot = QDir::toNativeSeparators (QFileInfo (QString::fromAscii (APP_CACHE_DIR)).absoluteFilePath ()+" /" );
303
-
304
- for (int i=0 ; i<nb_walls; i++)
305
- {
306
- // target size
307
- QSize size;
308
- if (_set->type () == UM::W_DESKTOP)
309
- {
310
- size = m_enviro->wpSize (-1 );
311
- }
312
- else
313
- {
314
- size = m_enviro->wpSize (i);
315
- }
316
-
317
- if (!size.isEmpty ())
318
- {
319
- QFileInfo file (_files.at (i));
320
- QString tmpPath = tmpRoot+file.completeBaseName ()+" -" +QString::number (size.width ())+" x" +QString::number (size.height ())+" ." +file.suffix ();
321
-
322
- if (!QFile::exists (tmpPath))
323
- {
324
- QImage image (file.absoluteFilePath ());
325
-
326
- // if image ratio is almost the same, do not waste time in image croppping
327
- double curRatio = (double )image.size ().width ()/image.size ().height ();
328
- double targetRatio = (double )size.width ()/size.height ();
329
-
330
- if (qAbs (curRatio - targetRatio) < 0.02 )
331
- {
332
- newFiles.append (_files.at (i));
333
- continue ;
334
- }
335
-
336
- // scale
337
- image = image.scaled (size, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
338
-
339
- // crop
340
- int diffW = image.width ()-size.width ();
341
- int diffH = image.height ()-size.height ();
342
- image = image.copy (diffW/2 , diffH/2 , size.width (), size.height ());
343
-
344
- image.save (tmpPath);
345
- }
346
-
347
- newFiles.append (tmpPath);
348
- }
349
- else
350
- {
351
- newFiles.append (_files.at (i));
352
- }
353
- }
237
+ int ret = SHFileOperation (&shfos);
354
238
355
- return newFiles ;
239
+ return ret == 0 ;
356
240
}
0 commit comments