1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
5
* Tomahawk is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Tomahawk is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
19
#include "WidgetDragFilter.h"
21
#include <QMouseEvent>
22
#include <QApplication>
25
#include "utils/Logger.h"
28
WidgetDragFilter::WidgetDragFilter( QObject* parent )
30
, m_dragStarted( false )
32
Q_ASSERT( parent->isWidgetType() );
33
m_target = QWeakPointer<QWidget>(static_cast<QWidget*>(parent));
34
m_target.data()->installEventFilter( this );
39
WidgetDragFilter::eventFilter( QObject* obj, QEvent* event )
41
if ( m_target.isNull() || m_target.data() != obj )
44
if ( event->type() == QEvent::MouseButtonPress )
46
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event );
47
if ( !canDrag( obj, mouseEvent ) )
49
if ( !( mouseEvent->modifiers() == Qt::NoModifier && mouseEvent->button() == Qt::LeftButton ) )
52
m_dragPoint = mouseEvent->pos();
56
else if ( event->type() == QEvent::MouseMove )
61
QMouseEvent* e = static_cast<QMouseEvent* >(event);
62
if ( !canDrag( obj, e ) )
64
m_dragStarted = false;
68
if ( e->buttons().testFlag( Qt::LeftButton ) )
70
m_target.data()->window()->move( m_target.data()->window()->pos() + ( e->pos() - m_dragPoint ) );
74
else if ( event->type() == QEvent::MouseButtonRelease )
75
m_dragStarted = false;
82
* Make sure we can really drag this widget. Checks inspired by Oxygen's oxygenwindowmanager.cpp
85
WidgetDragFilter::canDrag( QObject* obj, QMouseEvent* ev ) const
87
if ( !obj->isWidgetType() )
90
QWidget* w = static_cast< QWidget* >( obj );
92
if ( QWidget::mouseGrabber() )
95
if ( w->cursor().shape() != Qt::ArrowCursor )
98
// Now we check various things about the child position and mouse
99
QPoint position( ev->pos() );
100
QWidget* child = w->childAt( position );
102
if ( child && child->cursor().shape() != Qt::ArrowCursor )
105
// Don't want to drag menubars when selecting an action
106
if ( QMenuBar* menuBar = qobject_cast<QMenuBar*>( w ) )
108
// check if there is an active action
109
if ( menuBar->activeAction() && menuBar->activeAction()->isEnabled() )
112
// check if action at position exists and is enabled
113
if ( QAction* action = menuBar->actionAt( position ) )
115
if ( action->isSeparator() )
117
if ( action->isEnabled() )