• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • ui
droptarget.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2002 Patrick Charbonnier <pch@freeshell.org>
4  Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
5  Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 */
12 
13 #include "ui/droptarget.h"
14 
15 #include "core/kget.h"
16 #include "core/transferhandler.h"
17 #include "core/transfergrouphandler.h"
18 #include "core/transfertreemodel.h"
19 #include "settings.h"
20 #include "mainwindow.h"
21 #include "ui/newtransferdialog.h"
22 
23 #include <kwindowsystem.h>
24 #include <kmenu.h>
25 #include <kmessagebox.h>
26 #include <KPassivePopup>
27 #include <kapplication.h>
28 
29 #include <QDesktopWidget>
30 #include <QBitmap>
31 #include <QPainter>
32 #include <QTimer>
33 #include <QToolTip>
34 #include <QClipboard>
35 #include <QStringList>
36 
37 #include <math.h>
38 
39 #define TARGET_SIZE 64
40 #define TARGET_ANI_MS 20
41 #define TARGET_TOOLTIP_MS 1000
42 
43 DropTarget::DropTarget(MainWindow * mw)
44  : QWidget(0, Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint),
45  parentWidget(mw), animTimer(0), showInformation(false)
46 {
47  KWindowSystem::setState(winId(), NET::SkipTaskbar);
48 
49  QRect screenGeo = qApp->desktop()->screenGeometry(Settings::dropPosition());
50  if ((screenGeo.x() + screenGeo.width() >= Settings::dropPosition().x() &&
51  screenGeo.y() + screenGeo.height() >= Settings::dropPosition().y()) && Settings::dropPosition().y() >= 0 && Settings::dropPosition().x() >= 0)
52  position = QPoint(Settings::dropPosition());
53  else
54  position = QPoint(screenGeo.x() + screenGeo.width() / 2, screenGeo.y() + screenGeo.height() / 2);
55  setFixedSize(TARGET_SIZE, TARGET_SIZE);
56 
57  if(Settings::dropSticky())
58  KWindowSystem::setState(winId(), KWindowSystem::Sticky);
59 
60  cachedPixmap = DesktopIcon("kget", TARGET_SIZE);
61  if (!cachedPixmap.mask().isNull())
62  {
63  QBitmap mask(size());
64  mask.fill(Qt::color0);
65  QBitmap pixMask = cachedPixmap.mask();
66  QPainter p(&mask);
67  p.drawPixmap((mask.width() - pixMask.width())/2, (mask.height() - pixMask.height())/2,
68  pixMask);
69  setMask(mask);
70  }
71  else
72  setMask(QBitmap());
73 
74  // popup menu for right mouse button
75  popupMenu = new KMenu(this);
76  popupMenu->addTitle(mw->windowTitle());
77 
78  QAction * downloadAction = mw->actionCollection()->action("start_all_download");
79  popupMenu->addAction( downloadAction );
80  connect( downloadAction, SIGNAL(toggled(bool)), this, SLOT(slotStartStopToggled(bool)) );
81  popupMenu->addSeparator();
82  pop_show = popupMenu->addAction( QString(), this, SLOT(toggleMinimizeRestore()) );
83  popupMenu->addAction(parentWidget->actionCollection()->action("show_drop_target"));
84  pop_sticky = popupMenu->addAction(i18nc("fix position for droptarget", "Sticky"), this, SLOT(toggleSticky()));
85  pop_sticky->setCheckable(true);
86  pop_sticky->setChecked(Settings::dropSticky());
87  popupMenu->addSeparator();
88  popupMenu->addAction( mw->actionCollection()->action("preferences") );
89 
90  QAction *quitAction = new QAction(this);
91  quitAction->setText(i18n("Quit KGet"));
92  quitAction->setIcon(KIcon("system-shutdown"));
93  connect(quitAction, SIGNAL(triggered()), mw, SLOT(slotQuit()));
94  popupMenu->addAction(quitAction);
95 
96  isdragging = false;
97 
98  // Enable dropping
99  setAcceptDrops(true);
100 
101  if ( Settings::showDropTarget() && Settings::firstRun() ) {
102  showInformation = true;
103  }
104 
105  animTimer = new QTimer(this);
106  popupTimer = new QTimer(this);
107 
108  setMouseTracking(true);
109 
110  connect(KGet::model(), SIGNAL(transfersChangedEvent(QMap<TransferHandler*,Transfer::ChangesFlags>)),
111  this, SLOT(slotToolTipUpdate()));
112 
113  connect(popupTimer, SIGNAL(timeout()),
114  this, SLOT(slotToolTipTimer()));
115 }
116 
117 
118 DropTarget::~DropTarget()
119 {
120  Settings::setDropPosition( pos() );
121  Settings::setShowDropTarget( !isHidden() );
122  Settings::self()->writeConfig();
123 // unsigned long state = KWindowSystem::windowInfo(kdrop->winId()).state();
124 // // state will be 0L if droptarget is hidden. Sigh.
125 // config->writeEntry("State", state ? state : DEFAULT_DOCK_STATE );
126 }
127 
128 void DropTarget::setDropTargetVisible( bool shown, bool internal )
129 {
130  if (shown == !isHidden())
131  return;
132 
133  if ( internal )
134  Settings::setShowDropTarget( shown );
135 
136  if (!shown)
137  {
138  Settings::setDropPosition( pos() );
139  position = pos();
140  if ( Settings::animateDropTarget() )
141  playAnimationHide();
142  else
143  hide();
144  }
145  else
146  {
147  if ( Settings::animateDropTarget() ) {
148  playAnimationShow();
149  } else {
150  move(position);
151  show();
152  }
153  slotToolTipUpdate();
154  }
155 }
156 
157 void DropTarget::playAnimationShow()
158 {
159  if (animTimer->isActive())
160  animTimer->stop();
161  animTimer->disconnect();
162  connect( animTimer, SIGNAL(timeout()),
163  this, SLOT(slotAnimateShow()));
164 
165  move(position.x(), -TARGET_SIZE);
166 
167  ani_y = -1;
168  ani_vy = 0;
169  show();
170  animTimer->start(TARGET_ANI_MS);
171 }
172 
173 void DropTarget::playAnimationHide()
174 {
175  if (animTimer->isActive())
176  animTimer->stop();
177 
178  animTimer->disconnect();
179  connect( animTimer, SIGNAL(timeout()),
180  this, SLOT(slotAnimateHide()));
181  ani_y = (float)y();
182  ani_vy = 0;
183  animTimer->start(TARGET_ANI_MS);
184 }
185 
186 void DropTarget::playAnimationSync()
187 {
188  if (animTimer->isActive())
189  animTimer->stop();
190 
191  animTimer->disconnect();
192  connect( animTimer, SIGNAL(timeout()),
193  this, SLOT(slotAnimateSync()));
194  ani_y = (float)y();
195  ani_vy = -1;
196  animTimer->start(TARGET_ANI_MS);
197 }
198 
199 void DropTarget::slotStartStopToggled( bool started )
200 {
201  if ( started && Settings::animateDropTarget() )
202  playAnimationSync();
203 }
204 
205 
208 void DropTarget::dragEnterEvent(QDragEnterEvent * event)
209 {
210  event->setAccepted(KUrl::List::canDecode(event->mimeData())
211  || event->mimeData()->hasText());
212 }
213 
214 
215 void DropTarget::dropEvent(QDropEvent * event)
216 {
217  KUrl::List list = KUrl::List::fromMimeData(event->mimeData());
218  QString str;
219 
220  if (!list.isEmpty())
221  {
222  if (list.count() == 1 && list.first().url().endsWith(QLatin1String(".kgt")))
223  {
224  int msgBoxResult = KMessageBox::questionYesNoCancel(this, i18n("The dropped file is a KGet Transfer List"), "KGet",
225  KGuiItem(i18n("&Download"), KIcon("document-save")),
226  KGuiItem(i18n("&Load transfer list"), KIcon("list-add")), KStandardGuiItem::cancel());
227 
228  if (msgBoxResult == 3) //Download
229  NewTransferDialogHandler::showNewTransferDialog(list.first().url());
230  if (msgBoxResult == 4) //Load
231  KGet::load(list.first().url());
232  }
233  else
234  {
235  if (list.count() == 1)
236  {
237  str = event->mimeData()->text();
238  NewTransferDialogHandler::showNewTransferDialog(str);
239  }
240  else
241  NewTransferDialogHandler::showNewTransferDialog(list);
242  }
243  }
244  else
245  {
246  NewTransferDialogHandler::showNewTransferDialog();
247  }
248 
249  if ( Settings::animateDropTarget() )
250  playAnimationSync();
251 }
252 
253 
254 void DropTarget::closeEvent( QCloseEvent * e )
255 {
256  if( kapp->sessionSaving() )
257  e->ignore();
258  else
259  {
260  setVisible( false );
261  e->accept();
262  }
263 }
264 
265 void DropTarget::mousePressEvent(QMouseEvent * e)
266 {
267  // If the user click on the droptarget, stop any animation that is going on
268  if(animTimer)
269  {
270  animTimer->stop();
271  }
272 
273  if (e->button() == Qt::LeftButton)
274  {
275  isdragging = true;
276  dx = e->globalPos().x() - pos().x();
277  dy = e->globalPos().y() - pos().y();
278  }
279  else if (e->button() == Qt::RightButton)
280  {
281  pop_show->setText(parentWidget->isHidden() ?
282  i18n("Show Main Window") :
283  i18n("Hide Main Window") );
284  popupMenu->popup(e->globalPos());
285  }
286  else if (e->button() == Qt::MidButton)
287  {
288  //Here we paste the transfer
289  QString newtransfer = QApplication::clipboard()->text();
290  newtransfer = newtransfer.trimmed();
291 
292  if(!newtransfer.isEmpty())
293  KGet::addTransfer(KUrl(newtransfer), QString(), QString(), true);
294  }
295 }
296 
297 void DropTarget::mouseReleaseEvent(QMouseEvent *)
298 {
299  isdragging = false;
300 }
301 
302 void DropTarget::mouseDoubleClickEvent(QMouseEvent * e)
303 {
304  if (e->button() == Qt::LeftButton)
305  toggleMinimizeRestore();
306 }
307 
308 void DropTarget::mouseMoveEvent(QMouseEvent * e)
309 {
310  Q_UNUSED(e)
311  if ( isdragging && !Settings::dropSticky() )
312  {
313  move( QCursor::pos().x() - dx, QCursor::pos().y() - dy );
314  e->accept();
315  }
316 }
317 
318 void DropTarget::enterEvent(QEvent * event)
319 {
320  Q_UNUSED(event)
321  popupTimer->start(2000);
322 }
323 
324 void DropTarget::leaveEvent(QEvent * event)
325 {
326  Q_UNUSED(event)
327  popupTimer->stop();
328 }
329 
330 void DropTarget::paintEvent( QPaintEvent * )
331 {
332  QPainter p(this);
333  p.drawPixmap(0, 0, cachedPixmap);
334 }
335 
336 void DropTarget::toggleSticky()
337 {
338  Settings::setDropSticky( !Settings::dropSticky() );
339  pop_sticky->setChecked(Settings::dropSticky());
340 
341  if ( Settings::dropSticky() )
342  KWindowSystem::setState(winId(), KWindowSystem::SkipTaskbar | KWindowSystem::StaysOnTop | KWindowSystem::Sticky);
343  else
344  KWindowSystem::clearState(winId(), KWindowSystem::Sticky);
345 }
346 
347 void DropTarget::toggleMinimizeRestore()
348 {
349  bool nextState = parentWidget->isHidden();
350  Settings::setShowMain( nextState );
351  parentWidget->setVisible( nextState );
352  if(nextState)
353  {
354  KWindowSystem::activateWindow(static_cast<KXmlGuiWindow *>(parentWidget)->winId());
355  }
356 }
357 
359 void DropTarget::slotAnimateShow()
360 {
361  static float dT = TARGET_ANI_MS / 1000.0;
362 
363  ani_vy -= ani_y * 30 * dT;
364  ani_vy *= 0.95;
365  ani_y += ani_vy * dT;
366 
367  move(x(), qRound(position.y() * (1 + ani_y)));
368 
369  if ( fabs(ani_y) < 0.01 && fabs(ani_vy) < 0.01 && animTimer->isActive() )
370  {
371  animTimer->stop();
372 
373  if (showInformation)
374  KPassivePopup::message(i18n("Drop Target"),
375  i18n("You can drag download links into the drop target."), this);
376  }
377 }
378 
379 void DropTarget::slotAnimateHide()
380 {
381  static float dT = TARGET_ANI_MS / 1000.0;
382 
383  ani_vy += -2000 * dT;
384  float new_y = y() + ani_vy * dT;
385 
386  if ( new_y < -height() )
387  {
388  animTimer->stop();
389  hide();
390  move( x(), qRound(ani_y) );
391  } else
392  move( x(), qRound(new_y) );
393 }
394 
395 void DropTarget::slotAnimateSync()
396 {
397  static float dT = TARGET_ANI_MS / 1000.0;
398 
399  ani_vy += 4 * dT; // from -1 to 1 in 0.5 seconds
400  float i = 2 * M_PI * ani_vy; // from -2PI to 2PI
401  float j = (i == 0.0) ? 1 : (sin( i ) / i) * (1 + fabs(ani_vy));
402 
403  if ( ani_vy >= 1 )
404  {
405  animTimer->stop();
406  move( x(), qRound(ani_y) );
407  } else
408  move( x(), qRound(ani_y + 6*j) );
409 }
410 
411 void DropTarget::slotToolTipUpdate()
412 {
413  QStringList dataList;
414  QString data;
415 
416  foreach (TransferHandler *transfer, KGet::allTransfers()) {
417  data.clear();
418  switch (transfer->status()) {
419  case Job::Finished:
420  data = i18nc("%1 filename, %2 total size, %3 status", "%1(%2) %3",
421  transfer->source().fileName(),
422  KIO::convertSize(transfer->totalSize()),
423  transfer->statusText());
424  break;
425  case Job::Running:
426  data = i18nc("%1 filename, %2 percent complete, %3 downloaded out of %4 total size", "%1(%2% %3/%4) Speed:%5/s",
427  transfer->source().fileName(),
428  transfer->percent(),
429  KIO::convertSize(transfer->downloadedSize()),
430  KIO::convertSize(transfer->totalSize()),
431  KIO::convertSize(transfer->downloadSpeed()));
432  break;
433  default:
434  data = i18nc("%1 filename, %2 percent complete, %3 downloaded out of %4 total size, %5 status", "%1(%2% %3/%4) %5",
435  transfer->source().fileName(),
436  transfer->percent(),
437  KIO::convertSize(transfer->downloadedSize()),
438  KIO::convertSize(transfer->totalSize()),
439  transfer->statusText());
440  break;
441  }
442  dataList << data;
443  }
444 
445  if (!dataList.empty())
446  tooltipText = dataList.join("\n");
447  else
448  tooltipText = i18n("Ready");
449 }
450 
451 void DropTarget::slotToolTipTimer()
452 {
453  if (!popupMenu->isVisible() && isVisible() && mask().contains(mapFromGlobal(QCursor::pos())))
454  QToolTip::showText(QCursor::pos(),tooltipText,this,rect());
455 }
456 
457 void DropTarget::slotClose()
458 {
459  setVisible( false );
460 }
461 
462 #include "droptarget.moc"
DropTarget::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *e)
Definition: droptarget.cpp:308
Settings::dropSticky
static bool dropSticky()
Get DropSticky.
Definition: settings.h:905
TransferHandler::status
Job::Status status() const
Definition: transferhandler.h:64
KGet::addTransfer
static TransferHandler * addTransfer(KUrl srcUrl, QString destDir=QString(), QString suggestedFileName=QString(), QString groupName=QString(), bool start=false)
Adds a new transfer to the KGet.
Definition: kget.cpp:179
TARGET_ANI_MS
#define TARGET_ANI_MS
Definition: droptarget.cpp:40
TransferHandler
Class TransferHandler:
Definition: transferhandler.h:48
DropTarget::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *e)
Definition: droptarget.cpp:297
Job::Finished
The job is stopped, but this also indicates that it stopped because an error occurred.
Definition: job.h:47
DropTarget::enterEvent
void enterEvent(QEvent *event)
Definition: droptarget.cpp:318
Settings::setDropPosition
static void setDropPosition(const QPoint &v)
Set DropPosition.
Definition: settings.h:876
transfertreemodel.h
Settings::dropPosition
static QPoint dropPosition()
Get DropPosition.
Definition: settings.h:886
QWidget
DropTarget::paintEvent
void paintEvent(QPaintEvent *)
Definition: droptarget.cpp:330
DropTarget::DropTarget
DropTarget(MainWindow *parent)
Definition: droptarget.cpp:43
TransferHandler::totalSize
KIO::filesize_t totalSize() const
Definition: transferhandler.cpp:86
DropTarget::dropEvent
void dropEvent(QDropEvent *)
Definition: droptarget.cpp:215
DropTarget::mouseDoubleClickEvent
void mouseDoubleClickEvent(QMouseEvent *e)
Definition: droptarget.cpp:302
DropTarget::setDropTargetVisible
void setDropTargetVisible(bool shown, bool internal=true)
Definition: droptarget.cpp:128
TARGET_SIZE
#define TARGET_SIZE
Definition: droptarget.cpp:39
Settings::self
static Settings * self()
Definition: settings.cpp:17
KGet::allTransfers
static QList< TransferHandler * > allTransfers()
Gets all transfers.
Definition: kget.cpp:657
DropTarget::~DropTarget
~DropTarget()
Definition: droptarget.cpp:118
Job::Running
Definition: job.h:43
KGet::model
static TransferTreeModel * model()
Definition: kget.cpp:487
Settings::setShowDropTarget
static void setShowDropTarget(bool v)
Set ShowDropTarget.
Definition: settings.h:154
transfergrouphandler.h
TransferHandler::statusText
QString statusText() const
Definition: transferhandler.h:210
DropTarget::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *)
widget events
Definition: droptarget.cpp:208
mainwindow.h
newtransferdialog.h
TransferHandler::source
const KUrl & source() const
Definition: transferhandler.h:93
DropTarget::playAnimationSync
void playAnimationSync()
Definition: droptarget.cpp:186
KGet::load
static void load(QString filename=QString())
Imports the transfers and groups included in the provided xml file.
Definition: kget.cpp:508
DropTarget::leaveEvent
void leaveEvent(QEvent *event)
Definition: droptarget.cpp:324
droptarget.h
TransferHandler::downloadSpeed
int downloadSpeed() const
Definition: transferhandler.cpp:106
transferhandler.h
TransferHandler::percent
int percent() const
Definition: transferhandler.cpp:101
DropTarget::closeEvent
void closeEvent(QCloseEvent *)
Definition: droptarget.cpp:254
TransferHandler::downloadedSize
KIO::filesize_t downloadedSize() const
Definition: transferhandler.cpp:91
settings.h
Settings::setShowMain
static void setShowMain(bool v)
Set ShowMain.
Definition: settings.h:97
DropTarget::playAnimationShow
void playAnimationShow()
Definition: droptarget.cpp:157
NewTransferDialogHandler::showNewTransferDialog
static void showNewTransferDialog(const KUrl &url=KUrl())
Definition: newtransferdialog.cpp:493
kget.h
Settings::animateDropTarget
static bool animateDropTarget()
Get AnimateDropTarget.
Definition: settings.h:183
Settings::showDropTarget
static bool showDropTarget()
Get ShowDropTarget.
Definition: settings.h:164
Settings::firstRun
static bool firstRun()
Get FirstRun.
Definition: settings.h:924
Settings::setDropSticky
static void setDropSticky(bool v)
Set DropSticky.
Definition: settings.h:895
DropTarget::mousePressEvent
void mousePressEvent(QMouseEvent *e)
Definition: droptarget.cpp:265
DropTarget::playAnimationHide
void playAnimationHide()
Definition: droptarget.cpp:173
MainWindow
The main window of KGet.
Definition: mainwindow.h:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

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

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal