File indexing completed on 2024-04-21 05:46: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 <DesktopApplet.hxx>
0022 #include <KWinCompat.hxx>
0023 
0024 #include <QAction>
0025 #include <QIcon>
0026 #include <QAbstractButton>
0027 #include <QMessageBox>
0028 #include <QDebug>
0029 
0030 #include <KLocalizedString>
0031 #include <KConfig>
0032 #include <KConfigGroup>
0033 
0034 //--------------------------------------------------------------------------------
0035 
0036 DesktopApplet::DesktopApplet(QWidget *parent, const QString &theId, bool addConfigureAction)
0037   : QFrame(parent), id(theId)
0038 {
0039   setFrameShape(QFrame::NoFrame);
0040   setContextMenuPolicy(Qt::ActionsContextMenu);
0041 
0042   if ( addConfigureAction )
0043   {
0044     QAction *action = new QAction(this);
0045     action->setText(i18n("Configure..."));
0046     action->setIcon(QIcon::fromTheme("configure"));
0047     addAction(action);
0048     connect(action, &QAction::triggered, this, &DesktopApplet::configure);
0049   }
0050 
0051   QAction *action = new QAction(this);
0052   action->setIcon(QIcon::fromTheme("preferences-system-windows-move"));
0053   action->setText(i18n("Change Size && Position"));
0054   addAction(action);
0055   connect(action, &QAction::triggered, this, &DesktopApplet::startGeometryChange);
0056 
0057   action = new QAction(this);
0058   action->setIcon(QIcon::fromTheme("window-close"));
0059   action->setText(i18n("Remove this Applet"));
0060   addAction(action);
0061   connect(action, &QAction::triggered, this, &DesktopApplet::removeThisApplet);
0062 
0063   buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0064   buttons->adjustSize();
0065   buttons->hide();
0066   connect(buttons, &QDialogButtonBox::clicked, this, &DesktopApplet::finishGeometryChange);
0067 }
0068 
0069 //--------------------------------------------------------------------------------
0070 
0071 void DesktopApplet::loadConfig()
0072 {
0073   KConfig config;
0074   KConfigGroup group = config.group(id);
0075   setGeometry(group.readEntry("rect", QRect(QPoint(30, 30), sizeHint())));
0076   onDesktop = group.readEntry("onDesktop", int(NET::OnAllDesktops));
0077 
0078   QColor textCol = group.readEntry("textCol", QColor(Qt::white));
0079   QColor backCol = group.readEntry("backCol", QColor(32, 56, 92, 190));
0080   QPalette pal = palette();
0081   pal.setColor(foregroundRole(), textCol);
0082   pal.setColor(backgroundRole(), backCol);
0083   setPalette(pal);
0084 }
0085 
0086 //--------------------------------------------------------------------------------
0087 
0088 void DesktopApplet::saveConfig()
0089 {
0090   KConfig config;
0091   KConfigGroup group = config.group(id);
0092   group.writeEntry("rect", geometry());
0093   group.writeEntry("onDesktop", onDesktop);
0094   group.writeEntry("textCol", palette().color(foregroundRole()));
0095   group.writeEntry("backCol", palette().color(backgroundRole()));
0096 }
0097 
0098 //--------------------------------------------------------------------------------
0099 
0100 void DesktopApplet::startGeometryChange()
0101 {
0102   buttons->raise();
0103   buttons->show();
0104 
0105   oldRect = geometry();
0106   setWindowFlags(Qt::Window);
0107   setWindowTitle(i18n("%1: Change Size & Position", id));
0108 
0109   if ( onDesktop == NET::OnAllDesktops )
0110     KWinCompat::setOnAllDesktops(winId(), true);
0111 
0112   show();
0113   setGeometry(oldRect);
0114 }
0115 
0116 //--------------------------------------------------------------------------------
0117 
0118 void DesktopApplet::finishGeometryChange(QAbstractButton *clicked)
0119 {
0120   KWindowInfo info(winId(), NET::WMDesktop);
0121   if ( info.valid() )
0122     onDesktop = info.desktop();
0123 
0124   buttons->hide();
0125   QRect rect = geometry();
0126   setWindowFlags(Qt::Widget);
0127   show();
0128 
0129   if ( buttons->buttonRole(clicked) == QDialogButtonBox::AcceptRole )
0130   {
0131     setGeometry(rect);
0132     KConfig config;
0133     KConfigGroup group = config.group(id);
0134     group.writeEntry("rect", rect);
0135     group.writeEntry("onDesktop", onDesktop);
0136   }
0137   else
0138   {
0139     setGeometry(oldRect);
0140   }
0141 }
0142 
0143 //--------------------------------------------------------------------------------
0144 
0145 void DesktopApplet::removeThisApplet()
0146 {
0147   if ( QMessageBox::question(this, i18n("Remove Applet"),
0148                              i18n("Really remove this applet?")) == QMessageBox::Yes )
0149   {
0150     KConfig config;
0151     config.deleteGroup(id);
0152     emit removeThis(this);
0153   }
0154 }
0155 
0156 //--------------------------------------------------------------------------------