File indexing completed on 2024-04-21 16:30:35

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2018 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 <PictureFrameApplet.hxx>
0022 
0023 #include <QAction>
0024 #include <QPainter>
0025 
0026 #include <KConfig>
0027 #include <KConfigGroup>
0028 
0029 //--------------------------------------------------------------------------------
0030 
0031 PictureFrameApplet::PictureFrameApplet(QWidget *parent, const QString &theId)
0032   : DesktopApplet(parent, theId)
0033 {
0034   setAutoFillBackground(true);
0035   setContentsMargins(2, 2, 2, 2);
0036 }
0037 
0038 //--------------------------------------------------------------------------------
0039 
0040 QSize PictureFrameApplet::sizeHint() const
0041 {
0042   return QSize(400, 400);
0043 }
0044 
0045 //--------------------------------------------------------------------------------
0046 
0047 void PictureFrameApplet::resizeEvent(QResizeEvent *)
0048 {
0049   loadImage();
0050 }
0051 
0052 //--------------------------------------------------------------------------------
0053 
0054 void PictureFrameApplet::paintEvent(QPaintEvent *)
0055 {
0056   QPainter painter(this);
0057 
0058   painter.drawPixmap(contentsRect().x() + (contentsRect().width() - pixmap.width()) / 2,
0059                      contentsRect().y() + (contentsRect().height() - pixmap.height()) / 2,
0060                      pixmap);
0061 
0062   const int frameWidth = contentsMargins().left();
0063   const int fw2 = frameWidth / 2;
0064 
0065   QPen pen(palette().color(foregroundRole()), frameWidth);
0066   pen.setJoinStyle(Qt::MiterJoin);
0067   painter.setPen(pen);
0068   painter.drawRect(rect().adjusted(fw2, fw2, -fw2, -fw2));
0069 }
0070 
0071 //--------------------------------------------------------------------------------
0072 
0073 void PictureFrameApplet::loadConfig()
0074 {
0075   KConfig config;
0076   KConfigGroup group = config.group(id);
0077   imagePath = group.readEntry("imagePath", QString());
0078 
0079   DesktopApplet::loadConfig();
0080   loadImage();
0081 }
0082 
0083 //--------------------------------------------------------------------------------
0084 
0085 void PictureFrameApplet::loadImage()
0086 {
0087   pixmap.load(imagePath);
0088   if ( !pixmap.isNull() )
0089   {
0090     pixmap = pixmap.scaled(contentsRect().size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
0091     update();
0092   }
0093 }
0094 
0095 //--------------------------------------------------------------------------------
0096 
0097 void PictureFrameApplet::configure()
0098 {
0099   if ( dialog )
0100   {
0101     dialog->raise();
0102     dialog->activateWindow();
0103     return;
0104   }
0105 
0106   dialog = new PictureFrameAppletConfigureDialog(this);
0107   dialog->setWindowTitle(i18n("Configure PictureFrame Applet"));
0108 
0109   dialog->setAttribute(Qt::WA_DeleteOnClose);
0110   dialog->show();
0111 
0112   connect(dialog.data(), &QDialog::accepted, this,
0113           [this]()
0114           {
0115             saveConfig();
0116 
0117             KConfig config;
0118             KConfigGroup group = config.group(id);
0119             group.writeEntry("imagePath", imagePath);
0120           });
0121 }
0122 
0123 //--------------------------------------------------------------------------------