summaryrefslogtreecommitdiffstats
path: root/src/gui/general/RosegardenCanvasView.cpp
blob: 0798140cbb7e7bf30c2e1368d0aa4d2cfda81765 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    This program 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.  See the file
    COPYING included with this distribution for more information.
*/


#include "RosegardenCanvasView.h"

#include "misc/Debug.h"
#include "gui/general/CanvasItemGC.h"
#include <tqcanvas.h>
#include <tqcursor.h>
#include <tqpoint.h>
#include <tqrect.h>
#include <tqscrollbar.h>
#include <tqsize.h>
#include <tqsizepolicy.h>
#include <tqtimer.h>
#include <tqwidget.h>


namespace Rosegarden
{

RosegardenCanvasView::RosegardenCanvasView(TQCanvas* canvas,
        TQWidget* tqparent,
        const char* name, WFlags f)
        : TQCanvasView(canvas, tqparent, name, f),
        m_bottomWidget(0),
        m_currentBottomWidgetHeight( -1),
        m_leftWidget(0),
        m_smoothScroll(true),
        m_smoothScrollTimeInterval(DefaultSmoothScrollTimeInterval),
        m_minDeltaScroll(DefaultMinDeltaScroll),
        m_autoScrollTime(InitialScrollTime),
        m_autoScrollAccel(InitialScrollAccel),
        m_autoScrollXMargin(0),
        m_autoScrollYMargin(0),
        m_currentScrollDirection(None),
        m_scrollDirectionConstraint(NoFollow),
        m_autoScrolling(false)
{
    setDragAutoScroll(true);
    connect( &m_autoScrollTimer, TQT_SIGNAL( timeout() ),
             this, TQT_SLOT( doAutoScroll() ) );
}

void RosegardenCanvasView::fitWidthToContents()
{
    TQRect allItemsBoundingRect;

    TQCanvasItemList items = canvas()->allItems();

    TQCanvasItemList::Iterator it;

    for (it = items.begin(); it != items.end(); ++it) {
        allItemsBoundingRect |= (*it)->boundingRect();
    }

    TQSize currentSize = canvas()->size();
    resizeContents(allItemsBoundingRect.width(), currentSize.height());
}

void RosegardenCanvasView::setBottomFixedWidget(TQWidget* w)
{
    m_bottomWidget = w;
    if (m_bottomWidget) {
        int lww = m_leftWidget ? m_leftWidget->tqsizeHint().width() : 0;
        m_bottomWidget->reparent(this, 0, TQPoint(0, 0));
        m_bottomWidget->tqsetSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed));
        setMargins(lww, 0, 0, m_bottomWidget->tqsizeHint().height());
    }
}

void RosegardenCanvasView::slotUpdate()
{
    CanvasItemGC::gc();
    canvas()->update();
}

// Smooth scroll checks
//

const int RosegardenCanvasView::AutoscrollMargin = 16;
const int RosegardenCanvasView::InitialScrollTime = 30;
const int RosegardenCanvasView::InitialScrollAccel = 5;
const int RosegardenCanvasView::MaxScrollDelta = 100;      // max a.scroll speed
const double RosegardenCanvasView::ScrollAccelValue = 1.04;// acceleration rate

const int RosegardenCanvasView::DefaultSmoothScrollTimeInterval = 10;
const double RosegardenCanvasView::DefaultMinDeltaScroll = 1.2;

void RosegardenCanvasView::startAutoScroll()
{
    //    RG_DEBUG << "RosegardenCanvasView::startAutoScroll()\n";

    if ( !m_autoScrollTimer.isActive() ) {
        m_autoScrollTime = InitialScrollTime;
        m_autoScrollAccel = InitialScrollAccel;
        m_autoScrollTimer.start( m_autoScrollTime );
    }

    TQPoint autoScrollStartPoint = viewport()->mapFromGlobal( TQCursor::pos() );
    m_autoScrollYMargin = autoScrollStartPoint.y() / 10;
    m_autoScrollXMargin = autoScrollStartPoint.x() / 10;

    m_autoScrolling = true;
}

void RosegardenCanvasView::startAutoScroll(int directionConstraint)
{
    setScrollDirectionConstraint(directionConstraint);
    startAutoScroll();
}

void RosegardenCanvasView::stopAutoScroll()
{
    //    RG_DEBUG << "RosegardenCanvasView::stopAutoScroll()\n";

    m_autoScrollTimer.stop();
    m_minDeltaScroll = DefaultMinDeltaScroll;
    m_currentScrollDirection = None;

    m_autoScrolling = false;
}

void RosegardenCanvasView::doAutoScroll()
{
    //    RG_DEBUG << "RosegardenCanvasView::doAutoScroll()\n";

    TQPoint p = viewport()->mapFromGlobal( TQCursor::pos() );
    TQPoint dp = p - m_previousP;
    m_previousP = p;

    m_autoScrollTimer.start( m_autoScrollTime );
    ScrollDirection scrollDirection = None;

    int dx = 0, dy = 0;
    if (m_scrollDirectionConstraint & FollowVertical) {
        if ( p.y() < m_autoScrollYMargin ) {
            dy = -(int(m_minDeltaScroll));
            scrollDirection = Top;
        } else if ( p.y() > visibleHeight() - m_autoScrollYMargin ) {
            dy = + (int(m_minDeltaScroll));
            scrollDirection = Bottom;
        }
    }
    bool startDecelerating = false;
    if (m_scrollDirectionConstraint & FollowHorizontal) {

        //        RG_DEBUG << "p.x() : " << p.x() << " - visibleWidth : " << visibleWidth() << " - autoScrollXMargin : " << m_autoScrollXMargin << endl;

        if ( p.x() < m_autoScrollXMargin ) {
            if ( dp.x() > 0 ) {
                startDecelerating = true;
                m_minDeltaScroll /= ScrollAccelValue;
            }
            dx = -(int(m_minDeltaScroll));
            scrollDirection = Left;
        } else if ( p.x() > visibleWidth() - m_autoScrollXMargin ) {
            if ( dp.x() < 0 ) {
                startDecelerating = true;
                m_minDeltaScroll /= ScrollAccelValue;
            }
            dx = + (int(m_minDeltaScroll));
            scrollDirection = Right;
        }
    }

    //     RG_DEBUG << "dx: " << dx << ", dy: " << dy << endl;

    if ( (dx || dy) &&
            ((scrollDirection == m_currentScrollDirection) || (m_currentScrollDirection == None)) ) {
        scrollBy(dx, dy);
        if ( startDecelerating )
            m_minDeltaScroll /= ScrollAccelValue;
        else
            m_minDeltaScroll *= ScrollAccelValue;
        if (m_minDeltaScroll > MaxScrollDelta )
            m_minDeltaScroll = MaxScrollDelta;
        m_currentScrollDirection = scrollDirection;

    } else {
        // Don't automatically stopAutoScroll() here, the mouse button
        // is presumably still pressed.
        m_minDeltaScroll = DefaultMinDeltaScroll;
        m_currentScrollDirection = None;
    }

}

bool RosegardenCanvasView::isTimeForSmoothScroll()
{
    if (m_smoothScroll) {
        int ta = m_scrollAccelerationTimer.elapsed();
        int t = m_scrollTimer.elapsed();

        //	RG_DEBUG << "t = " << t << ", ta = " << ta << ", int " << m_smoothScrollTimeInterval << ", delta " << m_minDeltaScroll << endl;

        if (t < m_smoothScrollTimeInterval) {

            return false;

        } else {

            if (ta > 300) {
                // reset smoothScrollTimeInterval
                m_smoothScrollTimeInterval = DefaultSmoothScrollTimeInterval;
                m_minDeltaScroll = DefaultMinDeltaScroll;
                m_scrollAccelerationTimer.restart();
            } else if (ta > 50) {
                //                 m_smoothScrollTimeInterval /= 2;
                m_minDeltaScroll *= 1.08;
                m_scrollAccelerationTimer.restart();
            }

            m_scrollTimer.restart();
            return true;
        }
    }

    return true;
}

void RosegardenCanvasView::slotScrollHoriz(int hpos)
{
    TQScrollBar* hbar = getMainHorizontalScrollBar();
    int currentContentYPos = contentsY();

    /* Lots of performance hitting debug
    RG_DEBUG << "RosegardenCanvasView::slotScrollHoriz: hpos is " << hpos
      << ", contentsX is " << contentsX() << ", visibleWidth is "
      << visibleWidth() << endl;
             */

    if (hpos == 0) {

        // returning to zero
        //         hbar->setValue(0);
        setContentsPos(0, currentContentYPos);

    } else if (hpos > (contentsX() +
                       visibleWidth() * 1.6) ||
               hpos < (contentsX() -
                       visibleWidth() * 0.7)) {

        // miles off one side or the other
        // 	hbar->setValue(hpos - int(visibleWidth() * 0.4));
        setContentsPos(hpos - int(visibleWidth() * 0.4), currentContentYPos);

    } else if (hpos > (contentsX() +
                       visibleWidth() * 0.9)) {

        // moving off the right hand side of the view
        // 	hbar->setValue(hbar->value() + int(visibleWidth() * 0.6));
        setContentsPos(hbar->value() + int(visibleWidth() * 0.6), currentContentYPos);

    } else if (hpos < (contentsX() +
                       visibleWidth() * 0.1)) {

        // moving off the left
        // 	hbar->setValue(hbar->value() - int(visibleWidth() * 0.6));
        setContentsPos(hbar->value() - int(visibleWidth() * 0.6), currentContentYPos);
    }
}

void RosegardenCanvasView::slotScrollHorizSmallSteps(int hpos)
{
    TQScrollBar* hbar = getMainHorizontalScrollBar();
    int currentContentYPos = contentsY();

    int diff = 0;

    if (hpos == 0) {

        // returning to zero
        //         hbar->setValue(0);
        setContentsPos(0, currentContentYPos);

    } else if ((diff = int(hpos - (contentsX() +
                                   visibleWidth() * 0.90))) > 0) {

        // moving off the right hand side of the view

        int delta = diff / 6;
        int diff10 = std::min(diff, (int)m_minDeltaScroll);
        delta = std::max(delta, diff10);

        // 	hbar->setValue(hbar->value() + delta);
        setContentsPos(hbar->value() + delta, currentContentYPos);

    } else if ((diff = int(hpos - (contentsX() +
                                   visibleWidth() * 0.10))) < 0) {
        // moving off the left

        int delta = -diff / 6;
        int diff10 = std::min( -diff, (int)m_minDeltaScroll);
        delta = std::max(delta, diff10);

        // 	hbar->setValue(hbar->value() - delta);
        setContentsPos(hbar->value() - delta, currentContentYPos);

    }
}

void RosegardenCanvasView::slotScrollVertSmallSteps(int vpos)
{
    TQScrollBar* vbar = verticalScrollBar();

    //    RG_DEBUG << "RosegardenCanvasView::slotScrollVertSmallSteps: vpos is " << vpos << ", contentsY is " << contentsY() << ", visibleHeight is " << visibleHeight() << endl;

    // As a special case (or hack), ignore any request made before we've
    // actually been rendered and sized
    if (visibleHeight() <= 1)
        return ;

    int diff = 0;

    if (vpos == 0) {

        // returning to zero
        vbar->setValue(0);

    } else if ((diff = int(vpos - (contentsY() +
                                   visibleHeight() * 0.90))) > 0) {

        // moving off up

        int delta = diff / 6;
        int diff10 = std::min(diff, (int)m_minDeltaScroll);
        delta = std::max(delta, diff10);

        vbar->setValue(vbar->value() + diff);

    } else if ((diff = int(vpos - (contentsY() +
                                   visibleHeight() * 0.10))) < 0) {

        // moving off down

        int delta = -diff / 6;
        int diff10 = std::min( -diff, (int)m_minDeltaScroll);
        delta = std::max(delta, diff10);

        vbar->setValue(vbar->value() - delta);

    }
}

void RosegardenCanvasView::slotScrollVertToTop(int vpos)
{
    TQScrollBar* vbar = verticalScrollBar();
    if (vpos < visibleHeight() / 3)
        vbar->setValue(0);
    else
        vbar->setValue(vpos - visibleHeight() / 5);
}

void RosegardenCanvasView::slotSetScrollPos(const TQPoint &pos)
{
    getMainHorizontalScrollBar()->setValue(pos.x());
    verticalScrollBar()->setValue(pos.y());
}

void RosegardenCanvasView::resizeEvent(TQResizeEvent* e)
{
    TQCanvasView::resizeEvent(e);
    if (!horizontalScrollBar()->isVisible())
        updateBottomWidgetGeometry();
        updateLeftWidgetGeometry();
}

void RosegardenCanvasView::setHBarGeometry(TQScrollBar &hbar, int x, int y, int w, int h)
{
    TQCanvasView::setHBarGeometry(hbar, x, y, w, h);
    updateBottomWidgetGeometry();
}

void RosegardenCanvasView::updateBottomWidgetGeometry()
{
    if (!m_bottomWidget)
        return ;

    int bottomWidgetHeight = m_bottomWidget->tqsizeHint().height();

    int leftWidgetWidth = 0;
    if (m_leftWidget && m_leftWidget->isVisible()) {
        TQScrollView * qsv = dynamic_cast<TQScrollView *>(m_leftWidget);
        leftWidgetWidth = qsv->contentsWidth()+2;
        qsv->setFixedWidth(leftWidgetWidth);
    }

    setMargins(leftWidgetWidth, 0, 0, bottomWidgetHeight);

    TQRect r = frameRect();
    int hScrollBarHeight = 0;
    if (horizontalScrollBar()->isVisible())
        hScrollBarHeight = horizontalScrollBar()->height() + 2;
                            // + 2 offset : needed to preserve border shadow

    int vScrollBarWidth = 0;
    if (verticalScrollBar()->isVisible())
        vScrollBarWidth = verticalScrollBar()->width();

    m_bottomWidget->setGeometry(r.x() + leftWidgetWidth,
                                r.y() + r.height() - bottomWidgetHeight - hScrollBarHeight,
                                r.width() - vScrollBarWidth - leftWidgetWidth,
                                bottomWidgetHeight);

    if (bottomWidgetHeight != m_currentBottomWidgetHeight) {
        emit bottomWidgetHeightChanged(bottomWidgetHeight);
        m_currentBottomWidgetHeight = bottomWidgetHeight;
    }
}

void RosegardenCanvasView::wheelEvent(TQWheelEvent *e)
{
    if (e->state() & ControlButton) {
        if (e->delta() > 0)
            emit zoomIn();
        else if (e->delta() < 0)
            emit zoomOut();
        return ;
    }
    TQCanvasView::wheelEvent(e);
}

void RosegardenCanvasView::setLeftFixedWidget(TQWidget* w)
{
    m_leftWidget = w;
    if (m_leftWidget) {
        int bwh = m_bottomWidget ? m_bottomWidget->tqsizeHint().height() : 0;
        m_leftWidget->reparent(this, 0, TQPoint(0, 0));
        m_leftWidget->tqsetSizePolicy(TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Preferred));
        setMargins(m_leftWidget->tqsizeHint().width(), 0, 0, bwh);
    }
}

void RosegardenCanvasView::updateLeftWidgetGeometry()
{
    if (!m_leftWidget)
        return ;

    int leftWidgetWidth = 0;
    if (m_leftWidget->isVisible()) {
        TQScrollView * qsv = dynamic_cast<TQScrollView *>(m_leftWidget);
        leftWidgetWidth = qsv->contentsWidth() + 2;
    }
    m_leftWidget->setFixedWidth(leftWidgetWidth);

    int bottomWidgetHeight = m_bottomWidget ? 
                                m_bottomWidget->tqsizeHint().height() : 0;

    setMargins(leftWidgetWidth, 0, 0, bottomWidgetHeight);

    TQRect r = frameRect();
    int hScrollBarHeight = 0;
    if (horizontalScrollBar()->isVisible())
        hScrollBarHeight = horizontalScrollBar()->height() + 2;
                            // + 2 offset : needed to preserve border shadow

    m_leftWidget->setFixedHeight(r.height() - bottomWidgetHeight - hScrollBarHeight);
}


}
#include "RosegardenCanvasView.moc"