File indexing completed on 2024-04-14 05:39:34

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 - 2023 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <ConfigureDesktopDialog.hxx>
0022 
0023 #include <QStandardPaths>
0024 #include <QMimeDatabase>
0025 #include <QImageReader>
0026 #include <QApplication>
0027 #include <QScreen>
0028 #include <QDebug>
0029 
0030 #include <knewstuff_version.h>
0031 #if 0 && KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 80, 0)
0032 #  include <KNS3/QtQuickDialogWrapper>
0033 #else
0034 #  include <KNS3/DownloadDialog>
0035 #endif
0036 
0037 //--------------------------------------------------------------------------------
0038 
0039 ConfigureDesktopDialog::ConfigureDesktopDialog(QWidget *parent, const DesktopWidget::Wallpaper &wp)
0040   : QDialog(parent), wallpaper(wp)
0041 {
0042   ui.setupUi(this);
0043   ui.iconView->setIconSize(QSize(200, 200));
0044   connect(ui.iconView, &QListWidget::itemClicked,
0045           [this](QListWidgetItem *item)
0046           {
0047             ui.kurlrequester->setUrl(QUrl::fromLocalFile(item->data(Qt::UserRole).toString()));
0048             wallpaper.fileName = ui.kurlrequester->url().toLocalFile();
0049             emit changed();
0050           });
0051 
0052   showImages();
0053 
0054   QPushButton *newstuff = ui.buttonBox->addButton(i18n("Get New Wallpapers..."), QDialogButtonBox::ActionRole);
0055   newstuff->setIcon(QIcon::fromTheme("get-hot-new-stuff"));
0056   connect(newstuff, &QPushButton::clicked,
0057           [this]()
0058           {
0059 #if 0 && KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 80, 0)
0060             KNS3::QtQuickDialogWrapper *dialog = new KNS3::QtQuickDialogWrapper("wallpaper.knsrc", this);
0061             dialog->open();
0062             connect(dialog, &KNS3::QtQuickDialogWrapper::closed, dialog,
0063                     [this, dialog]()
0064                     {
0065                       if ( dialog->changedEntries().count() )
0066                         showImages();
0067 
0068                       dialog->deleteLater();
0069                     });
0070 #else
0071             KNS3::DownloadDialog dialog("wallpaper.knsrc", this);
0072             dialog.setTitle(i18n("Download Wallpapers"));
0073             dialog.exec();
0074             if ( dialog.changedEntries().count() )
0075               showImages();
0076 #endif
0077           });
0078 
0079   ui.kcolorcombo->setColor(wallpaper.color);
0080   ui.kurlrequester->setUrl(QUrl::fromLocalFile(wallpaper.fileName));
0081 
0082   connect(ui.kcolorcombo, &KColorCombo::activated,
0083           [this](const QColor &col) { wallpaper.color = col; emit changed(); });
0084 
0085   connect(ui.kurlrequester, &KUrlRequester::urlSelected,
0086           [this](const QUrl &url) { wallpaper.fileName = url.toLocalFile(); emit changed(); });
0087 
0088   // older compiler can't use this
0089   //connect(ui.kurlrequester, QOverload<const QString &>::of(&KUrlRequester::returnPressed),
0090   //        [this](const QString &text) { wallpaper.fileName = text; emit changed(); });
0091   connect(ui.kurlrequester, SIGNAL(returnPressed(QString)), this, SLOT(returnPressed(QString)));
0092 
0093   if ( wallpaper.mode == "Scaled" )
0094     ui.scaledIgnoreRatioButton->setChecked(true);
0095   else if ( wallpaper.mode == "ScaledKeepRatio" )
0096     ui.scaledKeepRatioButton->setChecked(true);
0097   else if ( wallpaper.mode == "ScaledKeepRatioExpand" )
0098     ui.scaledKeepRatioClipButton->setChecked(true);
0099   else
0100     ui.origSizeButton->setChecked(true);
0101 
0102   buttonGroup.addButton(ui.origSizeButton);
0103   buttonGroup.addButton(ui.scaledIgnoreRatioButton);
0104   buttonGroup.addButton(ui.scaledKeepRatioButton);
0105   buttonGroup.addButton(ui.scaledKeepRatioClipButton);
0106 
0107   connect(&buttonGroup, SIGNAL(buttonClicked(QAbstractButton *)),
0108           this, SLOT(buttonClicked(QAbstractButton *)));
0109 }
0110 
0111 //--------------------------------------------------------------------------------
0112 
0113 void ConfigureDesktopDialog::returnPressed(const QString &text)
0114 {
0115   wallpaper.fileName = text;
0116   emit changed();
0117 }
0118 
0119 //--------------------------------------------------------------------------------
0120 
0121 void ConfigureDesktopDialog::buttonClicked(QAbstractButton *button)
0122 {
0123   if ( button == ui.origSizeButton )
0124     wallpaper.mode = "";
0125   else if ( button == ui.scaledIgnoreRatioButton )
0126     wallpaper.mode = "Scaled";
0127   else if ( button == ui.scaledKeepRatioButton )
0128     wallpaper.mode = "ScaledKeepRatio";
0129   else if ( button == ui.scaledKeepRatioClipButton )
0130     wallpaper.mode = "ScaledKeepRatioExpand";
0131 
0132   emit changed();
0133 }
0134 
0135 //--------------------------------------------------------------------------------
0136 
0137 void ConfigureDesktopDialog::showImages()
0138 {
0139   // create filter (list of patterns) for image files we can read
0140   QMimeDatabase db;
0141   QStringList filterList;
0142   foreach (const QByteArray &type, QImageReader::supportedMimeTypes())
0143   {
0144     QMimeType mime(db.mimeTypeForName(QString::fromLatin1(type)));
0145     if ( mime.isValid() )
0146     {
0147       foreach (const QString &pattern, mime.globPatterns())
0148         filterList << pattern;
0149     }
0150   }
0151 
0152   ui.iconView->clear();
0153 
0154   QStringList dirNames = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation,
0155                                                    "wallpapers", QStandardPaths::LocateDirectory);
0156 
0157   const QString geometryString = QString("%1x%2")
0158                                          .arg(QApplication::primaryScreen()->size().width())
0159                                          .arg(QApplication::primaryScreen()->size().height());
0160 
0161   for (const QString &dirName : dirNames)
0162   {
0163     // check for file directly in this folder
0164     QStringList fileNames = QDir(dirName).entryList(filterList, QDir::Files | QDir::Readable);
0165     for (const QString &fileName : fileNames)
0166     {
0167       QPixmap pixmap(dirName + '/' + fileName);
0168       QListWidgetItem *item = new QListWidgetItem(pixmap, fileName, ui.iconView);
0169       item->setData(Qt::UserRole, dirName + '/' + fileName);
0170       item->setToolTip(QString("%1 (%2x%3)").arg(fileName).arg(pixmap.width()).arg(pixmap.height()));
0171     }
0172 
0173     // check for files in the special subdirs
0174     for (const QString &subdir : QDir(dirName).entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable))
0175     {
0176       QDir dir(dirName + '/' + subdir + "/contents/images");
0177       QString chosenFilePath;
0178       for (const QString &fileName : dir.entryList(filterList, QDir::Files | QDir::Readable))
0179       {
0180         chosenFilePath = dir.absoluteFilePath(fileName);
0181         if ( fileName.startsWith(geometryString) )
0182           break; // just take one
0183       }
0184 
0185       if ( !chosenFilePath.isEmpty() )
0186       {
0187         QPixmap pixmap(chosenFilePath);
0188         QListWidgetItem *item = new QListWidgetItem(pixmap, subdir, ui.iconView);
0189         item->setData(Qt::UserRole, chosenFilePath);
0190         item->setToolTip(QString("%1 (%2x%3)").arg(subdir).arg(pixmap.width()).arg(pixmap.height()));
0191       }
0192     }
0193   }
0194 }
0195 
0196 //--------------------------------------------------------------------------------