File indexing completed on 2024-04-28 04:02:02

0001 /*
0002     This file is part of the KDE project "KBounce"
0003 
0004     SPDX-FileCopyrightText: 2010 Andreas Scherf <ascherfy@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "backgroundselector.h"
0010 #include "ui_backgroundselector.h"
0011 
0012 #include <KColorScheme>
0013 #include <KLocalizedString>
0014 
0015 #include "renderer.h"
0016 
0017 #include <QPainter>
0018 
0019 BackgroundSelector::BackgroundSelector(QWidget* parent, KConfigSkeleton* config) :
0020     QWidget(parent),
0021     ui(new Ui::KBounceBackgroundSelector),m_config(config)
0022 {
0023     ui->setupUi(this);
0024     setupData();
0025 }
0026 
0027 BackgroundSelector::~BackgroundSelector()
0028 {
0029     delete ui;
0030 }
0031 
0032 void BackgroundSelector::changeEvent(QEvent *e)
0033 {
0034     QWidget::changeEvent(e);
0035     switch (e->type()) {
0036         case QEvent::LanguageChange:
0037             ui->retranslateUi(this);
0038             break;
0039         default:
0040             break;
0041     }
0042 }
0043 
0044 
0045 void BackgroundSelector::setupData()
0046 {
0047     if (m_config)
0048     {
0049         //The lineEdit widget holds our bg path, but the user does not manipulate it directly
0050         ui->kcfg_BackgroundPicturePath->hide();
0051         connect(ui->kurlrequester, &KUrlRequester::textChanged, this, &BackgroundSelector::imagePathChanged);
0052         connect(ui->kcfg_UseRandomBackgroundPictures, &QCheckBox::toggled, this, &BackgroundSelector::useRandomBackgroundPicturesChanged);
0053 
0054         //Get our currently configured Tileset entry
0055         KConfig * config = m_config->config();
0056         KConfigGroup group = config->group(QStringLiteral("General"));
0057         QString picturePath = group.readPathEntry("BackgroundPicturePath",QDir::homePath() );
0058         ui->kurlrequester->setUrl(QUrl(picturePath));
0059         ui->kurlrequester->setStartDir(QUrl(picturePath));
0060     }
0061 }
0062 
0063 void BackgroundSelector::useRandomBackgroundPicturesChanged(bool toggled)
0064 {
0065     if (toggled)
0066     {
0067         enableSettings(true);
0068         previewBackgroundPicture();
0069     }
0070     else
0071     {
0072         enableSettings(false);
0073     }
0074 }
0075 
0076 void BackgroundSelector::enableSettings(bool enable)
0077 {
0078     ui->kurlrequester->setEnabled(enable);
0079     ui->backgroundPicturePreview->setEnabled(enable);
0080 }
0081 
0082 void BackgroundSelector::imagePathChanged(const QString& path)
0083 {
0084     ui->kcfg_BackgroundPicturePath->setText(path);
0085     previewBackgroundPicture();
0086 }
0087 
0088 void BackgroundSelector::previewBackgroundPicture()
0089 {
0090     KBounceRenderer render;
0091     QSize previewSize = ui->backgroundPicturePreview->size();
0092     QPixmap previewPixmap = render.getRandomBackgroundPixmap(ui->kcfg_BackgroundPicturePath->text());
0093     if (previewPixmap.isNull())
0094     {
0095         previewPixmap = QPixmap(previewSize);
0096         previewPixmap.fill( Qt::transparent );
0097         QPainter p( &previewPixmap );
0098         QString text=i18n("No background pictures found.");
0099         QFont font;
0100         font.setPointSize( 11 );
0101         p.setFont( font );
0102         KColorScheme kcs(QPalette::Normal,KColorScheme::Window);
0103         p.setPen(kcs.foreground(KColorScheme::NeutralText).color());
0104         p.drawText( p.viewport(), Qt::AlignCenter | Qt::AlignTop | Qt::TextWordWrap, text );
0105         p.end();
0106     }
0107     ui->backgroundPicturePreview->setPixmap(previewPixmap.scaled(previewSize,Qt::KeepAspectRatio,Qt::SmoothTransformation));
0108 }
0109 
0110 #include "moc_backgroundselector.cpp"