Relicense all GPLv2 only code to GPLv2+.
[kdenetwork.git] / kget / ui / droptarget.cpp
blob0a96839b9c0e34ec94d778063abd139c1c1add3c
1 /* This file is part of the KDE project
3 Copyright (C) 2002 Patrick Charbonnier <pch@freeshell.org>
4 Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
12 #include "ui/droptarget.h"
14 #include "core/kget.h"
15 #include "settings.h"
16 #include "mainwindow.h"
17 #include "ui/newtransferdialog.h"
19 #include <kwindowsystem.h>
20 #include <klocale.h>
21 #include <kmenu.h>
22 #include <kmessagebox.h>
23 #include <KPassivePopup>
24 #include <kstandarddirs.h>
25 #include <kactioncollection.h>
26 #include <kapplication.h>
27 #include <kiconloader.h>
28 #include <KGlobalSettings>
30 #include <QBitmap>
31 #include <QClipboard>
32 #include <QPainter>
33 #include <QTimer>
35 #include <math.h>
37 #define TARGET_SIZE 64
38 #define TARGET_ANI_MS 20
40 DropTarget::DropTarget(MainWindow * mw)
41 : QWidget(0, Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint),
42 parentWidget(mw), animTimer(0), showInformation(false)
44 QRect desk = KGlobalSettings::desktopGeometry(this);
45 desk.setRight( desk.right() - TARGET_SIZE );
46 desk.setBottom( desk.bottom() - TARGET_SIZE );
48 if (desk.contains(Settings::dropPosition())
49 && ((Settings::dropPosition().x() != 0) || (Settings::dropPosition().y() != 0)))
50 position = QPoint(Settings::dropPosition());
51 else
52 position = QPoint((int)desk.width() / 2, (int)desk.height() / 2);
54 resize(TARGET_SIZE, TARGET_SIZE);
56 if(Settings::dropSticky())
57 KWindowSystem::setState(winId(), KWindowSystem::Sticky);
59 cachedPixmap = DesktopIcon("kget", TARGET_SIZE);
60 if (!cachedPixmap.mask().isNull())
62 QBitmap mask(size());
63 mask.fill(Qt::color0);
64 QBitmap pixMask = cachedPixmap.mask();
65 QPainter p(&mask);
66 p.drawPixmap((mask.width() - pixMask.width())/2, (mask.height() - pixMask.height())/2,
67 pixMask);
68 setMask(mask);
70 else
71 setMask(QBitmap());
73 update();
75 // popup menu for right mouse button
76 popupMenu = new KMenu();
77 popupMenu->addTitle(mw->windowTitle());
79 QAction * downloadAction = mw->actionCollection()->action("start_all_download");
80 popupMenu->addAction( downloadAction );
81 connect( downloadAction, SIGNAL( toggled(bool) ), this, SLOT( slotStartStopToggled(bool) ) );
82 popupMenu->addSeparator();
83 pop_show = popupMenu->addAction( QString(), this, SLOT( toggleMinimizeRestore() ) );
84 popupMenu->addAction(i18n("Hide Drop Target"), this, SLOT(slotClose()));
85 pop_sticky = popupMenu->addAction(i18n("Sticky"), this, SLOT(toggleSticky()));
86 pop_sticky->setCheckable(true);
87 pop_sticky->setChecked(Settings::dropSticky());
88 popupMenu->addSeparator();
89 popupMenu->addAction( mw->actionCollection()->action("preferences") );
91 QAction *quitAction = new QAction(this);
92 quitAction->setText(i18n("Quit KGet"));
93 quitAction->setIcon(KIcon("system-log-out"));
94 connect(quitAction, SIGNAL(triggered()), mw, SLOT(slotQuit()));
95 popupMenu->addAction(quitAction);
97 isdragging = false;
99 // Enable dropping
100 setAcceptDrops(true);
102 if ( Settings::showDropTarget() && Settings::firstRun() ) {
103 showInformation = true;
109 DropTarget::~DropTarget()
111 Settings::setDropPosition( pos() );
112 Settings::setShowDropTarget( !isHidden() );
113 // unsigned long state = KWindowSystem::windowInfo(kdrop->winId()).state();
114 // // state will be 0L if droptarget is hidden. Sigh.
115 // config->writeEntry("State", state ? state : DEFAULT_DOCK_STATE );
116 delete popupMenu;
117 delete animTimer;
120 void DropTarget::setVisible( bool shown, bool internal )
122 if (shown == !isHidden())
123 return;
125 if ( internal )
126 Settings::setShowDropTarget( shown );
128 if (!shown)
130 Settings::setDropPosition( pos() );
131 if ( Settings::animateDropTarget() )
132 playAnimationHide();
133 else
134 hide();
136 else
138 show();
139 if ( Settings::animateDropTarget() )
140 playAnimationShow();
141 else
142 move(position);
146 void DropTarget::playAnimationShow()
148 if ( animTimer )
149 delete animTimer;
150 animTimer = new QTimer;
151 connect( animTimer, SIGNAL( timeout() ),
152 this, SLOT( slotAnimateShow() ));
154 move(position.x(), -TARGET_SIZE);
156 ani_y = -1;
157 ani_vy = 0;
158 animTimer->start(TARGET_ANI_MS);
161 void DropTarget::playAnimationHide()
163 if ( animTimer )
165 if ( animTimer->isActive() )
166 move( x(), (int)(ani_y) );
167 delete animTimer;
169 animTimer = new QTimer;
170 connect( animTimer, SIGNAL( timeout() ),
171 this, SLOT( slotAnimateHide() ));
172 ani_y = (float)y();
173 ani_vy = 0;
174 animTimer->start(TARGET_ANI_MS);
177 void DropTarget::playAnimationSync()
179 if ( animTimer )
181 if ( animTimer->isActive() )
182 move( x(), (int)(ani_y) );
183 delete animTimer;
185 animTimer = new QTimer;
186 connect( animTimer, SIGNAL( timeout() ),
187 this, SLOT( slotAnimateSync() ));
188 ani_y = (float)y();
189 ani_vy = -1;
190 animTimer->start(TARGET_ANI_MS);
193 void DropTarget::slotStartStopToggled( bool started )
195 if ( started && Settings::animateDropTarget() )
196 playAnimationSync();
200 /** widget events */
202 void DropTarget::dragEnterEvent(QDragEnterEvent * event)
204 event->setAccepted(KUrl::List::canDecode(event->mimeData())
205 || event->mimeData()->hasText());
209 void DropTarget::dropEvent(QDropEvent * event)
211 KUrl::List list = KUrl::List::fromMimeData(event->mimeData());
212 QString str;
214 if (!list.isEmpty())
216 if (list.count() == 1)
218 str = event->mimeData()->text();
219 NewTransferDialog::showNewTransferDialog(str);
221 else
222 NewTransferDialog::showNewTransferDialog(list);
224 else
226 NewTransferDialog::showNewTransferDialog();
229 if ( Settings::animateDropTarget() )
230 playAnimationSync();
234 void DropTarget::closeEvent( QCloseEvent * e )
236 if( kapp->sessionSaving() )
237 e->ignore();
238 else
240 setVisible( false );
241 e->accept();
245 void DropTarget::mousePressEvent(QMouseEvent * e)
247 // If the user click on the droptarget, stop any animation that is going on
248 if(animTimer)
250 animTimer->stop();
251 delete animTimer;
252 animTimer = 0;
255 if (e->button() == Qt::LeftButton)
257 isdragging = true;
258 dx = e->globalPos().x() - pos().x();
259 dy = e->globalPos().y() - pos().y();
261 else if (e->button() == Qt::RightButton)
263 pop_show->setText(parentWidget->isHidden() ?
264 i18n("Show Main Window") :
265 i18n("Hide Main Window") );
266 popupMenu->popup(e->globalPos());
268 else if (e->button() == Qt::MidButton)
270 //Here we paste the transfer
271 QString newtransfer = QApplication::clipboard()->text();
272 newtransfer = newtransfer.trimmed();
274 if(!newtransfer.isEmpty())
275 KGet::addTransfer(KUrl(newtransfer), QString(), QString(), true);
279 void DropTarget::mouseReleaseEvent(QMouseEvent *)
281 isdragging = false;
284 void DropTarget::mouseDoubleClickEvent(QMouseEvent * e)
286 if (e->button() == Qt::LeftButton)
287 toggleMinimizeRestore();
290 void DropTarget::mouseMoveEvent(QMouseEvent * e)
292 Q_UNUSED(e);
293 if ( isdragging && !Settings::dropSticky() )
295 move( QCursor::pos().x() - dx, QCursor::pos().y() - dy );
296 e->accept();
300 void DropTarget::paintEvent( QPaintEvent * )
302 QPainter p(this);
303 p.drawPixmap(0, 0, cachedPixmap);
306 void DropTarget::toggleSticky()
308 Settings::setDropSticky( !Settings::dropSticky() );
309 pop_sticky->setChecked(Settings::dropSticky());
311 if ( Settings::dropSticky() )
312 KWindowSystem::setState(winId(), KWindowSystem::SkipTaskbar | KWindowSystem::StaysOnTop | KWindowSystem::Sticky);
313 else
314 KWindowSystem::clearState(winId(), KWindowSystem::Sticky);
317 void DropTarget::toggleMinimizeRestore()
319 bool nextState = parentWidget->isHidden();
320 Settings::setShowMain( nextState );
321 parentWidget->setVisible( nextState );
324 /** widget animations */
325 void DropTarget::slotAnimateShow()
327 static float dT = TARGET_ANI_MS / 1000.0;
329 ani_vy -= ani_y * 30 * dT;
330 ani_vy *= 0.95;
331 ani_y += ani_vy * dT;
333 move(x(), (int)(position.y() * (1 + ani_y)));
335 if ( fabs(ani_y) < 0.01 && fabs(ani_vy) < 0.01 && animTimer )
337 animTimer->stop();
338 delete animTimer;
339 animTimer = 0;
341 if (showInformation)
342 KPassivePopup::message(i18n("Drop Target"),
343 i18n("You can drag download links into the drop target."), this);
347 void DropTarget::slotAnimateHide()
349 static float dT = TARGET_ANI_MS / 1000.0;
351 ani_vy += -2000 * dT;
352 float new_y = y() + ani_vy * dT;
354 if ( new_y < -height() )
356 animTimer->stop();
357 delete animTimer;
358 animTimer = 0;
359 move( x(), (int)(ani_y) );
360 hide();
361 } else
362 move( x(), (int)(new_y) );
365 void DropTarget::slotAnimateSync()
367 static float dT = TARGET_ANI_MS / 1000.0;
369 ani_vy += 4 * dT; // from -1 to 1 in 0.5 seconds
370 float i = 2 * M_PI * ani_vy; // from -2PI to 2PI
371 float j = (i == 0.0) ? 1 : (sin( i ) / i) * (1 + fabs(ani_vy));
373 if ( ani_vy >= 1 )
375 animTimer->stop();
376 delete animTimer;
377 animTimer = 0;
378 move( x(), (int)(ani_y) );
379 } else
380 move( x(), (int)(ani_y + 6*j) );
383 void DropTarget::slotClose()
385 setVisible( false );
386 if (!Settings::expertMode())
388 KMessageBox::information(parentWidget,
389 i18n("Drop target has been hidden. If you want to show it "
390 "again, go to Settings->Configure KGet->Look & Feel."),
391 i18n("Hiding drop target"),
392 "CloseDroptarget");
396 #include "droptarget.moc"