diff --git a/src/toplevel.cpp b/src/toplevel.cpp index 60f0642..a3c1cac 100644 --- a/src/toplevel.cpp +++ b/src/toplevel.cpp @@ -1,386 +1,386 @@ /* * Copyright 1998-1999 by Matthias Hoelzer-Kluepfel * Copyright 2002-2003 by Martin Willers * Copyright 2007-2009 by Stefan Böhmann * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "toplevel.h" #include "timeedit.h" #include "settings.h" #include "tea.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include TopLevel::TopLevel(const KAboutData *aboutData, const QString &icon, QWidget *parent) : QSystemTrayIcon( parent ), m_popup( new KPassivePopup ), m_iconName(icon), m_runningTeaTime( 0 ), m_nextNotificationTime( 0 ) { repaintTrayIcon(); // Set initial icon KSharedConfigPtr config = KSharedConfig::openConfig(); KConfigGroup tealistGroup( config, "Tealist" ); if( tealistGroup.exists() ) { QString key, time; for(unsigned index=0; tealistGroup.hasKey(time.sprintf("Tea%d Time", index)) && tealistGroup.hasKey(key.sprintf("Tea%d Name", index)); ++index) { if( int temp = ( tealistGroup.readEntry( time, QString() ) ).toUInt() ) { m_tealist.append( Tea( tealistGroup.readEntry( key, i18n( "Unknown Tea" ) ), temp ) ); } } } // If the list of teas is empty insert a set of default teas. if( m_tealist.isEmpty() ) { m_tealist.append( Tea(i18n( "Black Tea" ), 180 ) ); m_tealist.append( Tea(i18n( "Earl Grey" ), 300 ) ); m_tealist.append( Tea(i18n( "Fruit Tea" ), 480 ) ); m_tealist.append( Tea(i18n( "Green Tea" ), 120 ) ); } m_teaActionGroup=new QActionGroup( this ); m_actionCollection = new KActionCollection(this); // build the context menu action = m_actionCollection->addAction( QStringLiteral( "stop" )); action->setIcon(QIcon::fromTheme( QStringLiteral( "edit-delete" ) ) ); action->setText( i18n( "&Stop" ) ); action->setEnabled( false ); connect(action, &QAction::triggered, this, &TopLevel::cancelTea); action = m_actionCollection->addAction( QStringLiteral( "configure" )); action->setIcon(QIcon::fromTheme( QStringLiteral( "configure" ) ) ); action->setText(i18n( "&Configure..." ) ); connect(action, &QAction::triggered, this, &TopLevel::showSettingsDialog); action = KStandardAction::configureNotifications(this, &TopLevel::configureNotifications, m_actionCollection); action = KStandardAction::quit(qApp, &QCoreApplication::quit, m_actionCollection); action->setShortcut(0); action = m_actionCollection->addAction( QStringLiteral( "anonymous" )); action->setText(i18n( "&Anonymous..." ) ); connect(action, &QAction::triggered, this, &TopLevel::showTimeEditDialog); m_helpMenu = new KHelpMenu( 0, *aboutData, false ); loadTeaMenuItems(); contextMenu()->addSeparator(); contextMenu()->addAction( m_actionCollection->action(QStringLiteral("stop")) ); contextMenu()->addAction( m_actionCollection->action(QStringLiteral("anonymous")) ); contextMenu()->addSeparator(); contextMenu()->addAction( m_actionCollection->action(QStringLiteral("configure")) ); contextMenu()->addAction( m_actionCollection->action(QLatin1String(KStandardAction::name(KStandardAction::ConfigureNotifications))) ); contextMenu()->addMenu( m_helpMenu->menu() ); contextMenu()->addSeparator(); contextMenu()->addAction( m_actionCollection->action(QLatin1String(KStandardAction::name(KStandardAction::Quit))) ); m_timer = new QTimer( this ); connect(m_timer, &QTimer::timeout, this, &TopLevel::teaTimeEvent); connect(contextMenu(), static_cast(&QMenu::triggered), this, static_cast(&TopLevel::runTea)); connect(this, &TopLevel::activated, this, &TopLevel::showPopup); loadConfig(); checkState(); show(); } TopLevel::~TopLevel() { delete m_helpMenu; delete m_timer; delete m_popup; delete m_teaActionGroup; } void TopLevel::checkState() { const bool state = m_runningTeaTime != 0; m_teaActionGroup->setEnabled( !state ); m_actionCollection->action(QStringLiteral("stop"))->setEnabled( state ); m_actionCollection->action(QStringLiteral("anonymous"))->setEnabled( !state ); if( !state ) { setTooltipText( i18n( "No steeping tea." ) ); } } void TopLevel::setTeaList(const QList &tealist) { m_tealist=tealist; // Save list... KSharedConfigPtr config = KSharedConfig::openConfig(); KConfigGroup tealistGroup( config, "Tealist" ); tealistGroup.deleteGroup(); for(int i=0; isync(); const auto actions = m_teaActionGroup->actions(); for ( QAction* a : actions ) { m_teaActionGroup->removeAction( a ); } contextMenu()->clear(); loadTeaMenuItems(); contextMenu()->addSeparator(); contextMenu()->addAction( m_actionCollection->action(QStringLiteral("stop")) ); contextMenu()->addAction( m_actionCollection->action(QStringLiteral("anonymous")) ); contextMenu()->addSeparator(); contextMenu()->addAction( m_actionCollection->action(QStringLiteral("configure")) ); contextMenu()->addAction( m_actionCollection->action(QLatin1String(KStandardAction::name(KStandardAction::ConfigureNotifications))) ); contextMenu()->addMenu(m_helpMenu->menu() ); contextMenu()->addSeparator(); contextMenu()->addAction( m_actionCollection->action(QLatin1String(KStandardAction::name(KStandardAction::Quit))) ); connect(contextMenu(), static_cast(&QMenu::triggered), this, static_cast(&TopLevel::runTea)); loadConfig(); } void TopLevel::loadTeaMenuItems() { int i=0; setContextMenu(new QMenu); foreach(const Tea &t, m_tealist) { QAction *a = contextMenu()->addAction( i18nc( "%1 - name of the tea, %2 - the predefined time for " "the tea", "%1 (%2)", t.name(), t.timeToString() ) ); a->setData( ++i ); m_teaActionGroup->addAction( a ); } } void TopLevel::showSettingsDialog() { SettingsDialog( this, m_tealist ).exec(); } void TopLevel::showTimeEditDialog() { TimeEditDialog( this ).exec(); } void TopLevel::runTea(QAction *a) { int index = a->data().toInt(); if( index <= 0 ) { return; } --index; if( index > m_tealist.size() ) { return; } runTea( m_tealist.at( index ) ); } void TopLevel::runTea(const Tea &tea) { m_runningTea = tea; m_runningTeaTime = m_runningTea.time(); checkState(); repaintTrayIcon(); m_timer->start( 1000 ); } void TopLevel::repaintTrayIcon() { QPixmap icon( KIconLoader::global()->loadIcon( m_iconName, KIconLoader::Panel ) ); if ( m_runningTeaTime <= 0) { setIcon(icon); return; } if (m_usevisualize) { QPainter painter( &icon ); painter.setRenderHint( QPainter::Antialiasing ); const QRectF rectangle( 1, icon.height() / 3 + 1, icon.width() / 1.5 - 2, icon.height() / 1.5 - 2 ); const int startAngle = 90 * 16; - const int angleSpan = -( 360*16 / m_runningTea.time() * m_runningTeaTime ); + const int angleSpan = -( 360*16.0 / m_runningTea.time() * m_runningTeaTime ); painter.setBrush( QColor( 0, 255, 0, 90 ) ); painter.drawPie( rectangle, startAngle, 360*16 + angleSpan ); painter.setBrush( QColor( 255, 0, 0, 90 ) ); painter.drawPie( rectangle, startAngle, angleSpan ); } else { KIconLoader::global()->drawOverlays(QStringList() << QStringLiteral( "alarm-symbolic" ), icon, KIconLoader::Panel); } setIcon(icon); } void TopLevel::teaTimeEvent() { QString title = i18n( "The Tea Cooker" ); if( m_runningTeaTime == 0 ) { m_timer->stop(); QString content = i18n( "%1 is now ready!", m_runningTea.name() ); //NOTICE Timeout is set t ~o24 days when no auto hide is request. Ok - nearly the same... if( m_usepopup ) { showMessage( title, content, QSystemTrayIcon::Information, m_autohide ? m_autohidetime*1000 : 2100000000 ); } KNotification::event( QStringLiteral( "ready" ), content ); if( m_usereminder && m_remindertime > 0 ) { setTooltipText( content ); m_timer->start( 1000 ); m_nextNotificationTime -= m_remindertime; --m_runningTeaTime; } checkState(); repaintTrayIcon(); } else if(m_runningTeaTime<0) { QString content = i18n( "%1 is ready since %2!", m_runningTea.name(), Tea::int2time( m_runningTeaTime*-1, true ) ); setTooltipText( content ); if( m_runningTeaTime == m_nextNotificationTime ) { if( m_usepopup ) { showMessage( title, content, QSystemTrayIcon::Information, m_autohide ? m_autohidetime*1000 : 2100000000 ); } KNotification::event( QLatin1String( "reminder" ), content ); m_nextNotificationTime -= m_remindertime; } --m_runningTeaTime; } else { --m_runningTeaTime; setTooltipText( i18nc( "%1 is the time, %2 is the name of the tea", "%1 left for %2.", Tea::int2time( m_runningTeaTime, true ), m_runningTea.name() ) ); } if( m_usevisualize ) { repaintTrayIcon(); } } void TopLevel::cancelTea() { m_timer->stop(); m_runningTeaTime = 0; checkState(); repaintTrayIcon(); } void TopLevel::configureNotifications() { KNotifyConfigWidget::configure(); } void TopLevel::loadConfig() { KSharedConfigPtr config = KSharedConfig::openConfig(); KConfigGroup generalGroup( config, "General" ); m_usepopup=generalGroup.readEntry( "UsePopup", true ); m_autohide=generalGroup.readEntry( "PopupAutoHide", false ); m_autohidetime=generalGroup.readEntry( "PopupAutoHideTime", 30 ); m_usereminder=generalGroup.readEntry( "UseReminder", false ); m_remindertime=generalGroup.readEntry( "ReminderTime", 60 ); m_usevisualize=generalGroup.readEntry( "UseVisualize", true ); } void TopLevel::showPopup(QSystemTrayIcon::ActivationReason reason) { if ( reason != QSystemTrayIcon::Trigger ) { return; } if ( m_popup->isVisible() ) { m_popup->hide(); } else { QPoint p = geometry().topLeft(); QRect r = QApplication::desktop()->screenGeometry( p ); QSize popupSize = m_popup->minimumSizeHint(); if ( p.x() + popupSize.width() > r.right() ) { p.rx() -= popupSize.width(); } if ( p.y() + popupSize.height() > r.bottom() ) { p.ry() -= popupSize.height(); } m_popup->show( p ); } } void TopLevel::setTooltipText(const QString& content) { const QString title = i18n( "The Tea Cooker" ); setToolTip( content ); m_popup->setView( title, content, KIconLoader::global()->loadIcon( m_iconName, KIconLoader::MainToolbar ) ); } // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on; // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: