• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

jobwidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright 2008 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>  *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU General Public License     *
00015  *   along with this program; if not, write to the                         *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
00018  ***************************************************************************/
00019 
00020 #include "jobwidget.h"
00021 #include "../core/job.h"
00022 
00023 #include <KGlobalSettings>
00024 
00025 #include <QFont>
00026 #include <QAction>
00027 #include <QGraphicsGridLayout>
00028 
00029 #include <plasma/widgets/meter.h>
00030 #include <Plasma/DataEngine>
00031 #include <Plasma/Service>
00032 #include <Plasma/ExtenderItem>
00033 #include <Plasma/Theme>
00034 #include <Plasma/Label>
00035 #include <Plasma/Meter>
00036 
00037 JobWidget::JobWidget(SystemTray::Job *job, Plasma::ExtenderItem *parent)
00038     : QGraphicsWidget(parent),
00039     m_extenderItem(parent),
00040     m_job(job)
00041 {
00042     Q_ASSERT(m_extenderItem);
00043 
00044     m_meter = new Plasma::Meter(this);
00045     m_meter->setSvg("widgets/bar_meter_horizontal");
00046     m_meter->setMeterType(Plasma::Meter::BarMeterHorizontal);
00047     m_meter->setMaximumHeight(16);
00048     m_meter->setMaximum(100);
00049     m_meter->setValue(0);
00050 
00051     m_fromNameLabel = new Plasma::Label(this);
00052     m_fromLabel = new Plasma::Label(this);
00053     m_toNameLabel = new Plasma::Label(this);
00054     m_toLabel = new Plasma::Label(this);
00055     m_speedLabel = new Plasma::Label(this);
00056     m_processedLabel = new Plasma::Label(this);
00057     m_totalBytesLabel = new Plasma::Label(this);
00058 
00059     m_fromNameLabel->setAlignment(Qt::AlignRight);
00060     m_fromLabel->setAlignment(Qt::AlignLeft);
00061     m_toNameLabel->setAlignment(Qt::AlignRight);
00062     m_toLabel->setAlignment(Qt::AlignLeft);
00063     m_speedLabel->setAlignment(Qt::AlignRight);
00064     m_processedLabel->setAlignment(Qt::AlignLeft);
00065     m_totalBytesLabel->setAlignment(Qt::AlignRight);
00066 
00067     QGraphicsGridLayout *layout = new QGraphicsGridLayout(this);
00068     layout->addItem(m_fromNameLabel, 0, 0);
00069     layout->addItem(m_fromLabel, 0, 1, 1, 3);
00070     layout->addItem(m_toNameLabel, 1, 0);
00071     layout->addItem(m_toLabel, 1, 1, 1, 3);
00072 
00073     layout->addItem(m_speedLabel, 2, 0);
00074     layout->addItem(m_processedLabel, 2, 1);
00075     layout->addItem(m_totalBytesLabel, 2, 3);
00076 
00077     layout->addItem(m_meter, 3, 1, 1, 3);
00078 
00079     setMinimumWidth(350);
00080 
00081     if (m_job) {
00082         connect(m_job, SIGNAL(changed(SystemTray::Job*)),
00083                 this, SLOT(updateJob()));
00084         connect(m_job, SIGNAL(destroyed(SystemTray::Job*)),
00085                 this, SLOT(destroy()));
00086 
00087         //the suspend action
00088         QAction *suspendAction = new QAction(m_extenderItem);
00089         suspendAction->setIcon(KIcon("media-playback-pause"));
00090         suspendAction->setEnabled(true);
00091         suspendAction->setVisible(false);
00092         suspendAction->setToolTip(i18n("Pause job"));
00093         m_extenderItem->addAction("suspend", suspendAction);
00094         connect(suspendAction, SIGNAL(triggered()), m_job,
00095                 SLOT(suspend()));
00096 
00097         //the resume action
00098         QAction *resumeAction = new QAction(m_extenderItem);
00099         resumeAction->setIcon(KIcon("media-playback-start"));
00100         resumeAction->setEnabled(true);
00101         resumeAction->setVisible(false);
00102         resumeAction->setToolTip(i18n("Resume job"));
00103         m_extenderItem->addAction("resume", resumeAction);
00104         connect(resumeAction, SIGNAL(triggered()), m_job,
00105                 SLOT(resume()));
00106 
00107         //the friendly stop action
00108         QAction *stopAction = new QAction(m_extenderItem);
00109         stopAction->setIcon(KIcon("media-playback-stop"));
00110         stopAction->setEnabled(true);
00111         stopAction->setVisible(true);
00112         stopAction->setToolTip(i18n("Cancel job"));
00113         m_extenderItem->addAction("stop", stopAction);
00114         connect(stopAction, SIGNAL(triggered()), m_job,
00115                 SLOT(stop()));
00116 
00117         updateJob();
00118     } else {
00119         m_extenderItem->showCloseButton();
00120 
00121         labelName0 = m_extenderItem->config().readEntry("labelName0", "");
00122         label0= m_extenderItem->config().readEntry("label0", "");
00123         labelName1 = m_extenderItem->config().readEntry("labelName1", "");
00124         label1 = m_extenderItem->config().readEntry("label1", "");
00125 
00126         updateLabels();
00127     }
00128 }
00129 
00130 JobWidget::~JobWidget()
00131 {
00132 }
00133 
00134 void JobWidget::destroy()
00135 {
00136     if (!m_extenderItem->isDetached()) {
00137         //TODO: make configurable:
00138         m_extenderItem->setAutoExpireDelay(15000);
00139         updateJob();
00140         m_meter->setValue(100);
00141     }
00142 }
00143 
00144 void JobWidget::updateJob()
00145 {
00146     m_meter->setValue(m_job->percentage());
00147 
00148     Plasma::ExtenderItem *item = m_extenderItem;
00149 
00150     if (m_job) {
00151         if (m_job->labels().count() > 0) {
00152             labelName0 = m_job->labels().value(0).first;
00153             label0 = m_job->labels().value(0).second;
00154         }
00155         if (m_job->labels().count() > 1) {
00156             labelName1 = m_job->labels().value(1).first;
00157             label1 = m_job->labels().value(1).second;
00158         }
00159         KConfigGroup cg = m_extenderItem->config();
00160         cg.writeEntry("labelName0", labelName0);
00161         cg.writeEntry("label0", label0);
00162         cg.writeEntry("labelName1", labelName1);
00163         cg.writeEntry("label1", label1);
00164     }
00165 
00166     updateLabels();
00167 
00168     //show the current status in the title.
00169     if (!m_job->error().isEmpty()) {
00170         item->setTitle(m_job->error());
00171     } else if (m_job->state() == SystemTray::Job::Running) {
00172         item->setTitle(m_job->message());
00173     } else if (m_job->state() == SystemTray::Job::Suspended) {
00174         item->setTitle(
00175             i18nc("%1 is the name of the job, can be things like Copying, deleting, moving",
00176                   "(paused) %1", m_job->message()));
00177     } else {
00178         item->setTitle(
00179             i18nc("%1 is the name of the job, can be things like Copying, deleting, moving",
00180                   "(finished) %1", m_job->message()));
00181         item->showCloseButton();
00182     }
00183 
00184     //set the correct actions to visible.
00185     if (item->action("suspend")) {
00186         item->action("suspend")->setVisible(m_job->isSuspendable() &&
00187                                             m_job->state() == SystemTray::Job::Running);
00188     }
00189     if (item->action("resume")) {
00190         item->action("resume")->setVisible(m_job->isSuspendable() &&
00191                                            m_job->state() == SystemTray::Job::Suspended);
00192     }
00193     if (item->action("stop")) {
00194         item->action("stop")->setVisible(m_job->isKillable() &&
00195                                          m_job->state() != SystemTray::Job::Stopped);
00196     }
00197 
00198     m_speedLabel->setText(m_job->speed());
00199     m_processedLabel->setText(KGlobal::locale()->formatByteSize(m_job->processedAmounts()["bytes"]));
00200     m_totalBytesLabel->setText(KGlobal::locale()->formatByteSize(m_job->totalAmounts()["bytes"]));
00201 
00202     item->setIcon(m_job->applicationIconName());
00203 }
00204 
00205 void JobWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
00206 {
00207     Q_UNUSED(event)
00208     updateLabels();
00209 }
00210 
00211 void JobWidget::updateLabels()
00212 {
00213     Plasma::Theme *theme = Plasma::Theme::defaultTheme();
00214     QFont font = theme->font(Plasma::Theme::DefaultFont);
00215     QFontMetricsF fm(font);
00216     if (!labelName0.isEmpty()) {
00217         m_fromNameLabel->setText(QString("%1: ").arg(labelName0));
00218     }
00219     if (label0.startsWith("file://")) {
00220         label0 = KUrl(label0).toLocalFile();
00221     }
00222 
00223     m_fromLabel->setText(fm.elidedText(label0, Qt::ElideMiddle, m_fromLabel->size().width()));
00224 
00225     if (!labelName1.isEmpty()) {
00226         m_toNameLabel->setText(QString("%1: ").arg(labelName1));
00227     }
00228     if (label1.startsWith("file://")) {
00229         label1 = KUrl(label1).toLocalFile();
00230     }
00231     m_toLabel->setText(fm.elidedText(label1, Qt::ElideMiddle, m_toLabel->size().width()));
00232 }
00233 
00234 #include "jobwidget.moc"
00235 

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal