21 #include "kget_interface.h"
23 #include "../../../core/transferhandler.h"
25 #include <plasma/dataengine.h>
26 #include <plasma/theme.h>
27 #include <plasma/widgets/iconwidget.h>
28 #include <plasma/widgets/meter.h>
30 #include <QGraphicsSceneDragDropEvent>
31 #include <QGraphicsLinearLayout>
33 #include <QStyleOptionGraphicsItem>
34 #include <QProgressBar>
35 #include <QGraphicsProxyWidget>
37 #include <QtDBus/QDBusConnectionInterface>
42 const int ProxyWidget::MARGIN = 20;
43 const int ProxyWidget::TOP_MARGIN = 55;
44 const int ProxyWidget::LEFT_MARGIN = 15;
45 const int ProxyWidget::SPACING = 4;
55 m_layout =
new QGraphicsLinearLayout(Qt::Vertical,
this);
56 m_layout->setSpacing(SPACING);
61 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(themeChanged()));
70 const QRect rect = option->rect;
72 p->setRenderHint(QPainter::SmoothPixmapTransform);
74 QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
76 font.setPointSize(15);
79 p->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
81 QRect iconRect(QPoint(rect.x() + SPACING + 10, rect.y() + SPACING + 10), QSize(m_textHeight, m_textHeight));
83 KIcon(
"kget").paint(p, iconRect);
84 p->drawText(QRectF(rect.x() + SPACING * 2 + 10 + iconRect.width(), rect.y() + SPACING + 10,
85 m_textWidth, m_textHeight), i18n(
"KGet"));
86 p->drawLine(QPointF(rect.x() + SPACING + 10, rect.y() + SPACING * 2 + 10 + m_textHeight),
87 QPointF(rect.width() - SPACING - 10, rect.y() + SPACING * 2 + 10 + m_textHeight));
89 QGraphicsWidget::paint(p, option, widget);
92 void ProxyWidget::themeChanged()
94 QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
96 font.setPointSize(15);
98 QFontMetrics metrics(font);
99 m_textWidth = metrics.width(i18n(
"KGet"));
100 m_textHeight = metrics.height();
102 m_layout->setContentsMargins(MARGIN, MARGIN + m_textHeight + SPACING + 10, MARGIN, MARGIN);
107 if (m_layout->count())
108 m_layout->removeAt(0);
109 m_layout->addItem(widget);
110 m_dataWidget = widget;
119 : Plasma::PopupApplet(parent, args),
129 setAspectRatioMode(Plasma::IgnoreAspectRatio);
130 setBackgroundHints(Applet::DefaultBackground);
131 setAcceptDrops(
true);
141 KGlobal::locale()->insertCatalog(
"plasma_applet_kget");
143 setPopupIcon(
"kget");
146 m_engine->connectSource(
"KGet",
this);
148 kDebug(5001) <<
"KGet Engine could not be loaded";
156 void KGetApplet::slotKgetStarted()
164 kDebug() << layout()->count();
165 if (data[
"error"].toBool()) {
168 connect(
m_errorWidget, SIGNAL(kgetStarted()),
this, SLOT(slotKgetStarted()));
175 }
else if (!data[
"error"].toBool()) {
183 transferAdded(data[
"transfers"].toMap());
185 if (data.contains(
"transferAdded")) {
186 transferAdded(data[
"transferAdded"].toMap());
188 if (data.contains(
"transferRemoved")) {
189 transferRemoved(data[
"transferRemoved"].toMap());
195 void KGetApplet::transferAdded(
const QVariantMap &transfer)
197 QList<OrgKdeKgetTransferInterface*> added;
198 QVariantMap::const_iterator it;
199 QVariantMap::const_iterator itEnd = transfer.constEnd();
200 for (it = transfer.constBegin(); it != itEnd; ++it) {
201 OrgKdeKgetTransferInterface *newTransfer =
new OrgKdeKgetTransferInterface(
"org.kde.kget", it.value().toString(), QDBusConnection::sessionBus(),
this);
202 connect(newTransfer, SIGNAL(transferChangedEvent(
int)),
this, SLOT(slotUpdateTransfer(
int)));
204 added.append(newTransfer);
206 m_transfers[newTransfer].downloadedSize = newTransfer->downloadedSize();
207 m_transfers[newTransfer].size = newTransfer->totalSize();
212 if (!added.isEmpty()) {
215 updateGlobalProgress();
219 void KGetApplet::transferRemoved(
const QVariantMap &transfer)
223 QList<OrgKdeKgetTransferInterface*> removed;
225 QHash<OrgKdeKgetTransferInterface*, Data>::iterator it;
226 QHash<OrgKdeKgetTransferInterface*, Data>::iterator itEnd =
m_transfers.end();
228 const KUrl url = KUrl(it.key()->source().value());
230 if (url.protocol().isEmpty()) {
231 removed.append(it.key());
242 if (!removed.isEmpty()) {
245 updateGlobalProgress();
249 void KGetApplet::slotUpdateTransfer(
int transferChange)
251 OrgKdeKgetTransferInterface *transfer = qobject_cast<OrgKdeKgetTransferInterface*>(QObject::sender());
258 m_transfers[transfer].size = transfer->totalSize();
259 m_transfers[transfer].downloadedSize = transfer->downloadedSize();
263 updateGlobalProgress();
269 m_transfers[transfer].downloadedSize = transfer->downloadedSize();
272 updateGlobalProgress();
278 void KGetApplet::updateGlobalProgress()
294 if (constraints & Plasma::SizeConstraint) {
295 QGraphicsLayoutItem *widget = layout()->itemAt(0);
296 Plasma::IconWidget *icon = 0;
297 if (!
m_icon && (icon = dynamic_cast<Plasma::IconWidget*>(widget))) {
301 kDebug() <<
"remove progressbar";
303 dynamic_cast<QGraphicsLinearLayout*
>(layout())->removeItem(
m_globalProgress);
305 QGraphicsLinearLayout *lay =
dynamic_cast<QGraphicsLinearLayout*
>(layout());
306 kDebug() <<
"switch to progressbar";
308 m_icon->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
309 m_icon->setPreferredSize(size().height(), size().height());
310 m_globalProgress->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
311 for (
int i = 0; i != 2; i++) {
332 switch (event->type())
334 case QEvent::GraphicsSceneDrop:
335 dropEvent(static_cast<QGraphicsSceneDragDropEvent*>(event));
338 dropEvent(static_cast<QDropEvent*>(event));
343 return Plasma::Applet::sceneEventFilter(watched, event);
352 if (event->mimeData()->hasUrls())
354 foreach (
const KUrl &url, event->mimeData()->urls())
355 urls.append(url.url());
363 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(
KGET_DBUS_SERVICE))
366 QDBusConnection::sessionBus());
368 kget_interface.showNewTransferDialog(urls);
372 QProcess::startDetached(
"kget", urls);
382 if (event->mimeData()->hasUrls())
384 foreach (
const KUrl &url, event->mimeData()->urls())
385 urls.append(url.url());
393 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(
KGET_DBUS_SERVICE))
396 QDBusConnection::sessionBus());
398 kget_interface.showNewTransferDialog(urls);
403 QProcess::startDetached(
"kget", urls);
408 #include "kgetapplet.moc"
void transfersAdded(const QList< OrgKdeKgetTransferInterface * > &transfers)
QGraphicsWidget * m_dataWidget
Plasma::IconWidget * m_icon
Plasma::Meter * m_globalProgress
virtual void dropEvent(QGraphicsSceneDragDropEvent *event)
static const QString KGET_DBUS_PATH
Plasma::DataEngine * m_engine
void transfersRemoved(const QList< OrgKdeKgetTransferInterface * > &transfers)
QHash< OrgKdeKgetTransferInterface *, Data > m_transfers
static const QString KGET_DBUS_SERVICE
virtual void constraintsEvent(Plasma::Constraints constraints)
virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
KGetApplet(QObject *parent, const QVariantList &args)
void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data)
void setDataWidget(QGraphicsWidget *widget)
ProxyWidget * m_proxyWidget
ErrorWidget * m_errorWidget
KIO::filesize_t m_totalSize
KIO::filesize_t m_downloadedSize