File: WidgetDragMonitor.cpp

package info (click to toggle)
qtop 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,840 kB
  • ctags: 5,775
  • sloc: cpp: 38,795; makefile: 9
file content (245 lines) | stat: -rw-r--r-- 7,659 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program.  If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "WidgetDragMonitor.h"
#include "XcbUtil.h"

#include <QMouseEvent>
#include <QTimer>

//_________________________________________________________
WidgetDragMonitor::WidgetDragMonitor( QWidget* parent ):
    QObject( parent ),
    Counter( "WidgetDragMonitor" ),
    mode_( DragMove ),
    enabled_( false ),
    clickCounter_( this, 2 ),
    button_( Qt::NoButton ),
    target_( nullptr ),
    direction_( XcbDefines::_NET_WM_MOVERESIZE_MOVE ),
    isDragging_( false )
{ parent->installEventFilter(this); }

//_______________________________________________________
bool WidgetDragMonitor::eventFilter( QObject* object, QEvent* event )
{
    switch( event->type() )
    {
        case QEvent::MouseButtonDblClick:
        case QEvent::MouseButtonPress:
        {

            QWidget* widget( qobject_cast<QWidget*>( object ) );
            if( !widget ) return false;

            QMouseEvent* mouseEvent( static_cast<QMouseEvent*>( event ) );
            button_ = mouseEvent->button();
            if( button_ == Qt::LeftButton )
            {

                event->accept();
                target_ = widget;
                isDragging_ = false;
                dragPosition_ = mouseEvent->pos();
                dragSize_ = widget->window()->size();

                // check modifiers
                if( ( mode_ & DragResize ) && enabled_ && mouseEvent->modifiers() & Qt::ShiftModifier )
                {

                    direction_ = _direction( widget, mouseEvent->pos() );

                } else if( mode_ & DragMove ) {

                    direction_ = XcbDefines::_NET_WM_MOVERESIZE_MOVE;
                    timer_.start( QApplication::doubleClickInterval(), this );

                    // handle multiple clicks
                    clickCounter_.increment();
                    if( clickCounter_.counts() == 2 )
                    {
                        emit stateChangeRequest();
                        timer_.stop();
                    }

                    return true;

                } else return false;

            }

            return false;

        }

        case QEvent::MouseButtonRelease:
        {
            event->accept();
            _resetDrag();
            return true;
        }

        case QEvent::MouseMove:
        {
            if( button_ != Qt::LeftButton ) return false;

            timer_.stop();

            // check against drag distance
            QMouseEvent* mouseEvent( static_cast<QMouseEvent*>( event ) );
            if( QPoint( mouseEvent->pos() - dragPosition_ ).manhattanLength() < QApplication::startDragDistance() )
            { return false; }

            if( !(target_ && object == target_ ) ) return false;

            if( !enabled_ )
            {
                if( mode_ & DragMove )
                {

                    emit stateChangeRequest();

                    event->accept();
                    return true;

                } else {

                    return false;

                }

            } else {

                if( (mode_ & DragMove) && direction_ == XcbDefines::_NET_WM_MOVERESIZE_MOVE )
                {

                    // drag
                    if( !_startDrag() )
                    {
                        const QPoint position( target_->window() == target_  ? dragPosition_ : target_->mapTo( target_->window(), dragPosition_ ) );
                        target_->window()->move( mouseEvent->globalPos() - position );
                    }

                    event->accept();
                    return true;

                } else if( mode_ & DragResize ) {

                    QRect newRect( target_->window()->geometry() );
                    const QPoint offset( mouseEvent->pos() - dragPosition_ );

                    switch( direction_ )
                    {

                        default:
                        case XcbDefines::_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
                        newRect.setWidth( dragSize_.width() + offset.x() );
                        newRect.setHeight( dragSize_.height() + offset.y() );
                        break;

                        case XcbDefines::_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
                        newRect.setLeft( mouseEvent->globalPos().x() );
                        newRect.setHeight( dragSize_.height() + offset.y() );
                        break;

                        case XcbDefines::_NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
                        newRect.setWidth( dragSize_.width() + offset.x() );
                        newRect.setTop( mouseEvent->globalPos().y() );
                        break;

                        case XcbDefines::_NET_WM_MOVERESIZE_SIZE_TOPLEFT:
                        newRect.setLeft( mouseEvent->globalPos().x() );
                        newRect.setTop( mouseEvent->globalPos().y() );
                        break;
                    }

                    target_->window()->setGeometry( newRect );
                    event->accept();
                    return true;

                }

            }

            return false;
        }

        default: return false;

    }

}

//_______________________________________________________
void WidgetDragMonitor::timerEvent( QTimerEvent* event )
{
    if( event->timerId() != timer_.timerId() ) return QObject::timerEvent( event );

    timer_.stop();
    if( button_ != Qt::LeftButton ) return;

    if( !enabled_ ) emit stateChangeRequest();
    if( !enabled_ ) return;

    QTimer::singleShot( 100, this, SLOT(_startDrag()));

}

//________________________________________________
XcbDefines::Direction WidgetDragMonitor::_direction( QWidget* widget, const QPoint& point ) const
{
    const int halfWidth( widget->width()/2 );
    const int halfHeight( widget->height()/2 );
    if( point.x() < halfWidth ) return point.y() < halfHeight ?  XcbDefines::_NET_WM_MOVERESIZE_SIZE_TOPLEFT: XcbDefines::_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT;
    else return point.y() < halfHeight ? XcbDefines::_NET_WM_MOVERESIZE_SIZE_TOPRIGHT: XcbDefines::_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT;
}

//___________________________________________________________
bool WidgetDragMonitor::_startDrag( void )
{

    if( !target_ ) return false;

    isDragging_ = true;

    #if HAVE_XCB
    if( XcbUtil::get().moveWidget( target_->window(), target_->mapToGlobal( dragPosition_ ) ) )
    {

        _resetDrag();
        return true;

    }
    #endif

    return false;

}

//___________________________________________________________
void WidgetDragMonitor::_resetDrag( void )
{
    if( target_ ) target_->unsetCursor();
    target_ = nullptr;
    direction_ = XcbDefines::_NET_WM_MOVERESIZE_MOVE;
    button_ = Qt::NoButton;
    dragPosition_ = QPoint();
    timer_.stop();
    isDragging_ = false;
}