Changeset View
Changeset View
Standalone View
Standalone View
wallpapers/image/image.cpp
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Line(s) | |||||
48 | #include <krun.h> | 48 | #include <krun.h> | ||
49 | #include <KNewStuff3/KNS3/DownloadDialog> | 49 | #include <KNewStuff3/KNS3/DownloadDialog> | ||
50 | #include <klocalizedstring.h> | 50 | #include <klocalizedstring.h> | ||
51 | 51 | | |||
52 | #include <Plasma/Theme> | 52 | #include <Plasma/Theme> | ||
53 | #include <Plasma/PluginLoader> | 53 | #include <Plasma/PluginLoader> | ||
54 | #include <qstandardpaths.h> | 54 | #include <qstandardpaths.h> | ||
55 | #include "backgroundlistmodel.h" | 55 | #include "backgroundlistmodel.h" | ||
56 | #include "slidemodel.h" | ||||
56 | 57 | | |||
57 | #include <KPackage/PackageLoader> | 58 | #include <KPackage/PackageLoader> | ||
58 | 59 | | |||
59 | Image::Image(QObject *parent) | 60 | Image::Image(QObject *parent) | ||
60 | : QObject(parent), | 61 | : QObject(parent), | ||
61 | m_ready(false), | 62 | m_ready(false), | ||
62 | m_delay(10), | 63 | m_delay(10), | ||
63 | m_dirWatch(new KDirWatch(this)), | 64 | m_dirWatch(new KDirWatch(this)), | ||
64 | m_mode(SingleImage), | 65 | m_mode(SingleImage), | ||
65 | m_currentSlide(-1), | 66 | m_currentSlide(-1), | ||
66 | m_model(nullptr), | 67 | m_model(nullptr), | ||
68 | m_slideshowModel(nullptr), | ||||
67 | m_dialog(nullptr) | 69 | m_dialog(nullptr) | ||
68 | { | 70 | { | ||
69 | m_wallpaperPackage = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); | 71 | m_wallpaperPackage = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); | ||
70 | 72 | | |||
71 | connect(&m_timer, &QTimer::timeout, this, &Image::nextSlide); | 73 | connect(&m_timer, &QTimer::timeout, this, &Image::nextSlide); | ||
72 | 74 | | |||
73 | connect(m_dirWatch, &KDirWatch::created, this, &Image::pathCreated); | 75 | connect(m_dirWatch, &KDirWatch::created, this, &Image::pathCreated); | ||
74 | connect(m_dirWatch, &KDirWatch::dirty, this, &Image::pathDirty); | 76 | connect(m_dirWatch, &KDirWatch::dirty, this, &Image::pathDirty); | ||
▲ Show 20 Lines • Show All 195 Lines • ▼ Show 20 Line(s) | 268 | if (!m_model) { | |||
270 | 272 | | |||
271 | m_model = new BackgroundListModel(this, this); | 273 | m_model = new BackgroundListModel(this, this); | ||
272 | m_model->reload(m_usersWallpapers); | 274 | m_model->reload(m_usersWallpapers); | ||
273 | } | 275 | } | ||
274 | 276 | | |||
275 | return m_model; | 277 | return m_model; | ||
276 | } | 278 | } | ||
277 | 279 | | |||
280 | QAbstractItemModel* Image::slideshowModel() | ||||
281 | { | ||||
282 | if (!m_slideshowModel) { | ||||
283 | m_slideshowModel = new SlideModel(this, this); | ||||
284 | m_slideshowModel->reload(m_slidePaths); | ||||
285 | } | ||||
286 | return m_slideshowModel; | ||||
287 | } | ||||
288 | | ||||
278 | int Image::slideTimer() const | 289 | int Image::slideTimer() const | ||
279 | { | 290 | { | ||
280 | return m_delay; | 291 | return m_delay; | ||
281 | } | 292 | } | ||
282 | 293 | | |||
283 | void Image::setSlideTimer(int time) | 294 | void Image::setSlideTimer(int time) | ||
284 | { | 295 | { | ||
285 | if (time == m_delay) { | 296 | if (time == m_delay) { | ||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | 334 | { | |||
331 | if (m_slidePaths.isEmpty()) { | 342 | if (m_slidePaths.isEmpty()) { | ||
332 | m_slidePaths << QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("share/wallpapers"), QStandardPaths::LocateDirectory); | 343 | m_slidePaths << QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("share/wallpapers"), QStandardPaths::LocateDirectory); | ||
333 | } | 344 | } | ||
334 | 345 | | |||
335 | if (m_mode == SlideShow) { | 346 | if (m_mode == SlideShow) { | ||
336 | updateDirWatch(m_slidePaths); | 347 | updateDirWatch(m_slidePaths); | ||
337 | startSlideshow(); | 348 | startSlideshow(); | ||
338 | } | 349 | } | ||
339 | 350 | if (m_slideshowModel) { | |||
351 | m_slideshowModel->reload(m_slidePaths); | ||||
352 | } | ||||
340 | emit slidePathsChanged(); | 353 | emit slidePathsChanged(); | ||
341 | } | 354 | } | ||
342 | 355 | | |||
343 | void Image::showAddSlidePathsDialog() | 356 | void Image::showAddSlidePathsDialog() | ||
344 | { | 357 | { | ||
345 | QFileDialog *dialog = new QFileDialog(nullptr, i18n("Directory with the wallpaper to show slides from"), QString()); | 358 | QFileDialog *dialog = new QFileDialog(nullptr, i18n("Directory with the wallpaper to show slides from"), QString()); | ||
346 | dialog->setAttribute(Qt::WA_DeleteOnClose, true ); | 359 | dialog->setAttribute(Qt::WA_DeleteOnClose, true ); | ||
347 | dialog->setOptions(QFileDialog::ShowDirsOnly); | 360 | dialog->setOptions(QFileDialog::ShowDirsOnly); | ||
348 | dialog->setAcceptMode(QFileDialog::AcceptOpen); | 361 | dialog->setAcceptMode(QFileDialog::AcceptOpen); | ||
349 | connect(dialog, &QDialog::accepted, this, &Image::addDirFromSelectionDialog); | 362 | connect(dialog, &QDialog::accepted, this, &Image::addDirFromSelectionDialog); | ||
350 | dialog->show(); | 363 | dialog->show(); | ||
351 | } | 364 | } | ||
352 | 365 | | |||
353 | void Image::addSlidePath(const QString &path) | 366 | void Image::addSlidePath(const QString &path) | ||
354 | { | 367 | { | ||
355 | if (!path.isEmpty() && !m_slidePaths.contains(path)) { | 368 | if (!path.isEmpty() && !m_slidePaths.contains(path)) { | ||
356 | m_slidePaths.append(path); | 369 | m_slidePaths.append(path); | ||
357 | if (m_mode == SlideShow) { | 370 | if (m_mode == SlideShow) { | ||
358 | updateDirWatch(m_slidePaths); | 371 | updateDirWatch(m_slidePaths); | ||
359 | } | 372 | } | ||
360 | 373 | if (m_slideshowModel) { | |||
374 | m_slideshowModel->addDirs({m_slidePaths}); | ||||
375 | } | ||||
361 | emit slidePathsChanged(); | 376 | emit slidePathsChanged(); | ||
362 | startSlideshow(); | 377 | startSlideshow(); | ||
363 | } | 378 | } | ||
364 | } | 379 | } | ||
365 | 380 | | |||
366 | void Image::removeSlidePath(const QString &path) | 381 | void Image::removeSlidePath(const QString &path) | ||
367 | { | 382 | { | ||
368 | if (m_slidePaths.contains(path)) { | 383 | if (m_slidePaths.contains(path)) { | ||
369 | m_slidePaths.removeAll(path); | 384 | m_slidePaths.removeAll(path); | ||
370 | if (m_mode == SlideShow) { | 385 | if (m_mode == SlideShow) { | ||
371 | updateDirWatch(m_slidePaths); | 386 | updateDirWatch(m_slidePaths); | ||
372 | } | 387 | } | ||
388 | if (m_slideshowModel) { | ||||
389 | bool haveParent = false; | ||||
390 | QStringList children; | ||||
391 | for (const QString& slidePath : m_slidePaths) { | ||||
392 | if (path.startsWith(slidePath)) { | ||||
393 | haveParent = true; | ||||
394 | } | ||||
395 | if (slidePath.startsWith(path)) { | ||||
396 | children.append(slidePath); | ||||
397 | } | ||||
398 | } | ||||
399 | /*If we have the parent directory do nothing since the directories are recursively searched. | ||||
400 | * If we have child directories just reload since removing the parent and then readding the children would | ||||
401 | * induce a race.*/ | ||||
402 | if (!haveParent) { | ||||
403 | if (children.size() > 0) { | ||||
404 | m_slideshowModel->reload(m_slidePaths); | ||||
405 | } else { | ||||
406 | m_slideshowModel->removeDir(path); | ||||
407 | } | ||||
408 | } | ||||
409 | } | ||||
373 | 410 | | |||
374 | emit slidePathsChanged(); | 411 | emit slidePathsChanged(); | ||
375 | startSlideshow(); | 412 | startSlideshow(); | ||
376 | } | 413 | } | ||
377 | } | 414 | } | ||
378 | 415 | | |||
379 | void Image::pathDirty(const QString& path) | 416 | void Image::pathDirty(const QString& path) | ||
380 | { | 417 | { | ||
▲ Show 20 Lines • Show All 196 Lines • ▼ Show 20 Line(s) | 610 | { | |||
577 | 614 | | |||
578 | m_findToken.clear(); | 615 | m_findToken.clear(); | ||
579 | 616 | | |||
580 | if(m_scanDirty) { | 617 | if(m_scanDirty) { | ||
581 | m_scanDirty = false; | 618 | m_scanDirty = false; | ||
582 | startSlideshow(); | 619 | startSlideshow(); | ||
583 | return; | 620 | return; | ||
584 | } | 621 | } | ||
585 | | ||||
586 | m_slideshowBackgrounds = paths; | 622 | m_slideshowBackgrounds = paths; | ||
587 | m_unseenSlideshowBackgrounds.clear(); | 623 | m_unseenSlideshowBackgrounds.clear(); | ||
588 | // start slideshow | 624 | // start slideshow | ||
589 | if (m_slideshowBackgrounds.isEmpty()) { | 625 | if (m_slideshowBackgrounds.isEmpty()) { | ||
590 | // no image has been found, which is quite weird... try again later (this is useful for events which | 626 | // no image has been found, which is quite weird... try again later (this is useful for events which | ||
591 | // are not detected by KDirWatch, like a NFS directory being mounted) | 627 | // are not detected by KDirWatch, like a NFS directory being mounted) | ||
592 | QTimer::singleShot(1000, this, &Image::startSlideshow); | 628 | QTimer::singleShot(1000, this, &Image::startSlideshow); | ||
593 | } else { | 629 | } else { | ||
▲ Show 20 Lines • Show All 257 Lines • ▼ Show 20 Line(s) | 886 | if (!m_model) { | |||
851 | return; | 887 | return; | ||
852 | } | 888 | } | ||
853 | 889 | | |||
854 | for (const QString &wallpaperCandidate : m_model->wallpapersAwaitingDeletion()) { | 890 | for (const QString &wallpaperCandidate : m_model->wallpapersAwaitingDeletion()) { | ||
855 | removeWallpaper(wallpaperCandidate); | 891 | removeWallpaper(wallpaperCandidate); | ||
856 | } | 892 | } | ||
857 | } | 893 | } | ||
858 | 894 | | |||
895 | void Image::openFolder(const QString& path) | ||||
896 | { | ||||
897 | new KRun(QUrl::fromLocalFile(path), nullptr); | ||||
ngraham: This generates a warning (variable `krun` is unused). I would suggest using `KIO… | |||||
Yes I also saw this method since the backgroundlist uses it but the Header said to use KRun if you want to open a folder instead of it. davidre: Yes I also saw this method since the backgroundlist uses it but the Header said to use KRun if… | |||||
Well it just depends on what you want to do. If you want to open the folder, then yeah KRun is better. But if you want to highlight it in a file manager window, then highlightInFileManager() is the way to go. ngraham: Well it just depends on what you want to do. If you want to open the folder, then yeah KRun is… | |||||
898 | } | ||||
You update m_uncheckedSlides but you're not updating m_slideshowBackgrounds so it does nothing till you restart davidedmundson: You update m_uncheckedSlides but you're not updating m_slideshowBackgrounds so it does nothing… | |||||
davidre: Thanks for spotting this, will try testing this. |
This generates a warning (variable krun is unused). I would suggest using KIO::highlightInFileManager() instead. For example: https://cgit.kde.org/spectacle.git/tree/src/Gui/KSMainWindow.cpp#n327