~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/utils/WidgetDragFilter.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include "WidgetDragFilter.h"
 
20
 
 
21
#include <QMouseEvent>
 
22
#include <QApplication>
 
23
#include <QMenuBar>
 
24
 
 
25
#include "utils/Logger.h"
 
26
 
 
27
 
 
28
WidgetDragFilter::WidgetDragFilter( QObject* parent )
 
29
    : QObject( parent )
 
30
    , m_dragStarted( false )
 
31
{
 
32
    Q_ASSERT( parent->isWidgetType() );
 
33
    m_target = QWeakPointer<QWidget>(static_cast<QWidget*>(parent));
 
34
    m_target.data()->installEventFilter( this );
 
35
}
 
36
 
 
37
 
 
38
bool
 
39
WidgetDragFilter::eventFilter( QObject* obj, QEvent* event )
 
40
{
 
41
    if ( m_target.isNull() || m_target.data() != obj )
 
42
        return false;
 
43
 
 
44
    if ( event->type() == QEvent::MouseButtonPress )
 
45
    {
 
46
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event );
 
47
        if ( !canDrag( obj, mouseEvent ) )
 
48
            return false;
 
49
        if ( !( mouseEvent->modifiers() == Qt::NoModifier && mouseEvent->button() == Qt::LeftButton ) )
 
50
            return false;
 
51
 
 
52
        m_dragPoint = mouseEvent->pos();
 
53
        m_dragStarted = true;
 
54
        return false;
 
55
    }
 
56
    else if ( event->type() == QEvent::MouseMove )
 
57
    {
 
58
        if ( !m_dragStarted )
 
59
            return false;
 
60
 
 
61
        QMouseEvent* e = static_cast<QMouseEvent* >(event);
 
62
        if ( !canDrag( obj, e ) )
 
63
        {
 
64
            m_dragStarted = false;
 
65
            return false;
 
66
        }
 
67
 
 
68
        if ( e->buttons().testFlag( Qt::LeftButton ) )
 
69
        {
 
70
            m_target.data()->window()->move( m_target.data()->window()->pos() + ( e->pos() - m_dragPoint ) );
 
71
            return true;
 
72
        }
 
73
    }
 
74
    else if ( event->type() == QEvent::MouseButtonRelease )
 
75
        m_dragStarted = false;
 
76
 
 
77
    return false;
 
78
}
 
79
 
 
80
 
 
81
/**
 
82
 * Make sure we can really drag this widget. Checks inspired by Oxygen's oxygenwindowmanager.cpp
 
83
 */
 
84
bool
 
85
WidgetDragFilter::canDrag( QObject* obj, QMouseEvent* ev ) const
 
86
{
 
87
    if ( !obj->isWidgetType() )
 
88
        return false;
 
89
 
 
90
    QWidget* w = static_cast< QWidget* >( obj );
 
91
 
 
92
    if ( QWidget::mouseGrabber() )
 
93
        return false;
 
94
 
 
95
    if ( w->cursor().shape() != Qt::ArrowCursor )
 
96
        return false;
 
97
 
 
98
    // Now we check various things about the child position and mouse
 
99
    QPoint position( ev->pos() );
 
100
    QWidget* child = w->childAt( position );
 
101
 
 
102
    if ( child && child->cursor().shape() != Qt::ArrowCursor )
 
103
        return false;
 
104
 
 
105
    // Don't want to drag menubars when selecting an action
 
106
    if ( QMenuBar* menuBar = qobject_cast<QMenuBar*>( w ) )
 
107
    {
 
108
        // check if there is an active action
 
109
        if ( menuBar->activeAction() && menuBar->activeAction()->isEnabled() )
 
110
            return false;
 
111
 
 
112
        // check if action at position exists and is enabled
 
113
        if ( QAction* action = menuBar->actionAt( position ) )
 
114
        {
 
115
            if ( action->isSeparator() )
 
116
                return true;
 
117
            if ( action->isEnabled() )
 
118
                return false;
 
119
        }
 
120
    }
 
121
 
 
122
    return true;
 
123
}