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"
16 #include "mainwindow.h"
17 #include "ui/newtransferdialog.h"
19 #include <kwindowsystem.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>
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());
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())
63 mask
.fill(Qt::color0
);
64 QBitmap pixMask
= cachedPixmap
.mask();
66 p
.drawPixmap((mask
.width() - pixMask
.width())/2, (mask
.height() - pixMask
.height())/2,
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
);
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 );
120 void DropTarget::setVisible( bool shown
, bool internal
)
122 if (shown
== !isHidden())
126 Settings::setShowDropTarget( shown
);
130 Settings::setDropPosition( pos() );
131 if ( Settings::animateDropTarget() )
139 if ( Settings::animateDropTarget() )
146 void DropTarget::playAnimationShow()
150 animTimer
= new QTimer
;
151 connect( animTimer
, SIGNAL( timeout() ),
152 this, SLOT( slotAnimateShow() ));
154 move(position
.x(), -TARGET_SIZE
);
158 animTimer
->start(TARGET_ANI_MS
);
161 void DropTarget::playAnimationHide()
165 if ( animTimer
->isActive() )
166 move( x(), (int)(ani_y
) );
169 animTimer
= new QTimer
;
170 connect( animTimer
, SIGNAL( timeout() ),
171 this, SLOT( slotAnimateHide() ));
174 animTimer
->start(TARGET_ANI_MS
);
177 void DropTarget::playAnimationSync()
181 if ( animTimer
->isActive() )
182 move( x(), (int)(ani_y
) );
185 animTimer
= new QTimer
;
186 connect( animTimer
, SIGNAL( timeout() ),
187 this, SLOT( slotAnimateSync() ));
190 animTimer
->start(TARGET_ANI_MS
);
193 void DropTarget::slotStartStopToggled( bool started
)
195 if ( started
&& Settings::animateDropTarget() )
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());
216 if (list
.count() == 1)
218 str
= event
->mimeData()->text();
219 NewTransferDialog::showNewTransferDialog(str
);
222 NewTransferDialog::showNewTransferDialog(list
);
226 NewTransferDialog::showNewTransferDialog();
229 if ( Settings::animateDropTarget() )
234 void DropTarget::closeEvent( QCloseEvent
* e
)
236 if( kapp
->sessionSaving() )
245 void DropTarget::mousePressEvent(QMouseEvent
* e
)
247 // If the user click on the droptarget, stop any animation that is going on
255 if (e
->button() == Qt::LeftButton
)
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
*)
284 void DropTarget::mouseDoubleClickEvent(QMouseEvent
* e
)
286 if (e
->button() == Qt::LeftButton
)
287 toggleMinimizeRestore();
290 void DropTarget::mouseMoveEvent(QMouseEvent
* e
)
293 if ( isdragging
&& !Settings::dropSticky() )
295 move( QCursor::pos().x() - dx
, QCursor::pos().y() - dy
);
300 void DropTarget::paintEvent( QPaintEvent
* )
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
);
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
;
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
)
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() )
359 move( x(), (int)(ani_y
) );
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
));
378 move( x(), (int)(ani_y
) );
380 move( x(), (int)(ani_y
+ 6*j
) );
383 void DropTarget::slotClose()
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"),
396 #include "droptarget.moc"