File indexing completed on 2024-04-21 05:46:35

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 - 2020 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 <DeviceNotifier.hxx>
0022 #include <DeviceList.hxx>
0023 
0024 #include <QIcon>
0025 #include <QMouseEvent>
0026 #include <QDebug>
0027 
0028 #include <KLocalizedString>
0029 
0030 //--------------------------------------------------------------------------------
0031 
0032 DeviceNotifier::DeviceNotifier(QWidget *parent)
0033   : SysTrayItem(parent, "device-notifier")
0034 {
0035   setToolTip(i18n("Device Notifier"));
0036 
0037   deviceList = new DeviceList(this);
0038   deviceList->setWindowTitle(i18n("Device List"));
0039 
0040   if ( deviceList->isEmpty() )
0041     hide();
0042 
0043   connect(deviceList, &DeviceList::deviceWasRemoved, this, &DeviceNotifier::checkDeviceList);
0044   connect(deviceList, &DeviceList::deviceWasAdded,
0045           [this]()
0046           {
0047             if ( !deviceList->isVisible() )
0048               timer.start();  // auto-hide
0049 
0050             showDetailsList();
0051           });
0052 
0053   // if the user did not activate the device list window, auto-hide it
0054   // but keep it if the mouse is over it (e.g. the user wants to click)
0055   timer.setInterval(4000);
0056   timer.setSingleShot(true);
0057 
0058   connect(&timer, &QTimer::timeout, this,
0059           [this]()
0060           {
0061             if ( deviceList->underMouse() )
0062               timer.start();
0063             else
0064               deviceList->hide();
0065           });
0066 
0067   deviceList->installEventFilter(this);
0068 }
0069 
0070 //--------------------------------------------------------------------------------
0071 
0072 QWidget *DeviceNotifier::getDetailsList()
0073 {
0074   deviceList->adjustSize();
0075   deviceList->resize(deviceList->size().expandedTo(QSize(300, 100)));
0076   setVisible(!deviceList->isEmpty());
0077   return deviceList;
0078 }
0079 
0080 //--------------------------------------------------------------------------------
0081 
0082 void DeviceNotifier::checkDeviceList()
0083 {
0084   if ( deviceList->isEmpty() )
0085   {
0086     deviceList->hide();
0087     hide();
0088   }
0089   else if ( deviceList->isVisible() )
0090     showDetailsList();  // reposition
0091 }
0092 
0093 //--------------------------------------------------------------------------------
0094 
0095 bool DeviceNotifier::eventFilter(QObject *watched, QEvent *event)
0096 {
0097   Q_UNUSED(watched)
0098 
0099   if ( event->type() == QEvent::WindowActivate )
0100     timer.stop();
0101 
0102   return false;
0103 }
0104 
0105 //--------------------------------------------------------------------------------