ScrollZoomer.cc 12 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8
#include <qevent.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_layout.h>
#include <qwt_scale_engine.h>
#include <qwt_scale_widget.h>
#include <Scrollbar.h>
#include <ScrollZoomer.h>

9 10
class ScrollData
{
pixhawk's avatar
pixhawk committed
11 12 13 14 15 16 17 18 19 20 21 22
public:
    ScrollData():
        scrollBar(NULL),
        position(ScrollZoomer::OppositeToScale),
#if QT_VERSION < 0x040000
        mode(QScrollView::Auto)
#else
        mode(Qt::ScrollBarAsNeeded)
#endif
    {
    }

23
    ~ScrollData() {
pixhawk's avatar
pixhawk committed
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
        delete scrollBar;
    }

    ScrollBar *scrollBar;
    ScrollZoomer::ScrollBarPosition position;
#if QT_VERSION < 0x040000
    QScrollView::ScrollBarMode mode;
#else
    Qt::ScrollBarPolicy mode;
#endif
};

ScrollZoomer::ScrollZoomer(QwtPlotCanvas *canvas):
    QwtPlotZoomer(canvas),
    d_cornerWidget(NULL),
    d_hScrollData(NULL),
    d_vScrollData(NULL),
    d_inZoom(false)
{
    if ( !canvas )
        return;

    d_hScrollData = new ScrollData;
    d_vScrollData = new ScrollData;
}

ScrollZoomer::~ScrollZoomer()
{
    delete d_cornerWidget;
    delete d_vScrollData;
    delete d_hScrollData;
}

void ScrollZoomer::rescale()
{
    QwtScaleWidget *xScale = plot()->axisWidget(xAxis());
    QwtScaleWidget *yScale = plot()->axisWidget(yAxis());

62 63
    if ( zoomRectIndex() <= 0 ) {
        if ( d_inZoom ) {
pixhawk's avatar
pixhawk committed
64 65 66 67
            xScale->setMinBorderDist(0, 0);
            yScale->setMinBorderDist(0, 0);
            d_inZoom = false;
        }
68 69
    } else {
        if ( !d_inZoom ) {
pixhawk's avatar
pixhawk committed
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
            /*
             We set a minimum border distance.
             Otherwise the canvas size changes when scrolling,
             between situations where the major ticks are at
             the canvas borders (requiring extra space for the label)
             and situations where all labels can be painted below/top
             or left/right of the canvas.
             */
            int start, end;

            xScale->getBorderDistHint(start, end);
            xScale->setMinBorderDist(start, end);

            yScale->getBorderDistHint(start, end);
            yScale->setMinBorderDist(start, end);

            d_inZoom = false;
        }
    }

    QwtPlotZoomer::rescale();
    updateScrollBars();
}

ScrollBar *ScrollZoomer::scrollBar(Qt::Orientation o)
{
    ScrollBar *&sb = (o == Qt::Vertical)
97
                     ? d_vScrollData->scrollBar : d_hScrollData->scrollBar;
pixhawk's avatar
pixhawk committed
98

99
    if ( sb == NULL ) {
pixhawk's avatar
pixhawk committed
100 101 102
        sb = new ScrollBar(o, canvas());
        sb->hide();
        connect(sb,
103 104
                SIGNAL(valueChanged(Qt::Orientation, double, double)),
                SLOT(scrollBarMoved(Qt::Orientation, double, double)));
pixhawk's avatar
pixhawk committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    }
    return sb;
}

ScrollBar *ScrollZoomer::horizontalScrollBar() const
{
    return d_hScrollData->scrollBar;
}

ScrollBar *ScrollZoomer::verticalScrollBar() const
{
    return d_vScrollData->scrollBar;
}

#if QT_VERSION < 0x040000
void ScrollZoomer::setHScrollBarMode(QScrollView::ScrollBarMode mode)
#else
void ScrollZoomer::setHScrollBarMode(Qt::ScrollBarPolicy mode)
#endif
{
125
    if ( hScrollBarMode() != mode ) {
pixhawk's avatar
pixhawk committed
126 127 128 129 130 131 132 133 134 135 136
        d_hScrollData->mode = mode;
        updateScrollBars();
    }
}

#if QT_VERSION < 0x040000
void ScrollZoomer::setVScrollBarMode(QScrollView::ScrollBarMode mode)
#else
void ScrollZoomer::setVScrollBarMode(Qt::ScrollBarPolicy mode)
#endif
{
137
    if ( vScrollBarMode() != mode ) {
pixhawk's avatar
pixhawk committed
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
        d_vScrollData->mode = mode;
        updateScrollBars();
    }
}

#if QT_VERSION < 0x040000
QScrollView::ScrollBarMode ScrollZoomer::hScrollBarMode() const
#else
Qt::ScrollBarPolicy ScrollZoomer::hScrollBarMode() const
#endif
{
    return d_hScrollData->mode;
}

#if QT_VERSION < 0x040000
QScrollView::ScrollBarMode ScrollZoomer::vScrollBarMode() const
#else
Qt::ScrollBarPolicy ScrollZoomer::vScrollBarMode() const
#endif
{
    return d_vScrollData->mode;
}

void ScrollZoomer::setHScrollBarPosition(ScrollBarPosition pos)
{
163
    if ( d_hScrollData->position != pos ) {
pixhawk's avatar
pixhawk committed
164 165 166 167 168 169 170
        d_hScrollData->position = pos;
        updateScrollBars();
    }
}

void ScrollZoomer::setVScrollBarPosition(ScrollBarPosition pos)
{
171
    if ( d_vScrollData->position != pos ) {
pixhawk's avatar
pixhawk committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        d_vScrollData->position = pos;
        updateScrollBars();
    }
}

ScrollZoomer::ScrollBarPosition ScrollZoomer::hScrollBarPosition() const
{
    return d_hScrollData->position;
}

ScrollZoomer::ScrollBarPosition ScrollZoomer::vScrollBarPosition() const
{
    return d_vScrollData->position;
}

void ScrollZoomer::setCornerWidget(QWidget *w)
{
189 190
    if ( w != d_cornerWidget ) {
        if ( canvas() ) {
pixhawk's avatar
pixhawk committed
191 192
            delete d_cornerWidget;
            d_cornerWidget = w;
193
            if ( d_cornerWidget->parent() != canvas() ) {
pixhawk's avatar
pixhawk committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#if QT_VERSION < 0x040000
                d_cornerWidget->reparent(canvas(), QPoint(0, 0));
#else
                d_cornerWidget->setParent(canvas());
#endif
            }

            updateScrollBars();
        }
    }
}

QWidget *ScrollZoomer::cornerWidget() const
{
    return d_cornerWidget;
}

bool ScrollZoomer::eventFilter(QObject *o, QEvent *e)
{
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
    if (  o == canvas() ) {
        switch(e->type()) {
        case QEvent::Resize: {
            const int fw = ((QwtPlotCanvas *)canvas())->frameWidth();

            QRect rect;
            rect.setSize(((QResizeEvent *)e)->size());
            rect.setRect(rect.x() + fw, rect.y() + fw,
                         rect.width() - 2 * fw, rect.height() - 2 * fw);

            layoutScrollBars(rect);
            break;
        }
        case QEvent::ChildRemoved: {
            const QObject *child = ((QChildEvent *)e)->child();
            if ( child == d_cornerWidget )
                d_cornerWidget = NULL;
            else if ( child == d_hScrollData->scrollBar )
                d_hScrollData->scrollBar = NULL;
            else if ( child == d_vScrollData->scrollBar )
                d_vScrollData->scrollBar = NULL;
            break;
        }
        default:
            break;
pixhawk's avatar
pixhawk committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251
        }
    }
    return QwtPlotZoomer::eventFilter(o, e);
}

bool ScrollZoomer::needScrollBar(Qt::Orientation o) const
{
#if QT_VERSION < 0x040000
    QScrollView::ScrollBarMode mode;
#else
    Qt::ScrollBarPolicy mode;
#endif
    double zoomMin, zoomMax, baseMin, baseMax;

252
    if ( o == Qt::Horizontal ) {
pixhawk's avatar
pixhawk committed
253 254 255 256 257
        mode = d_hScrollData->mode;
        baseMin = zoomBase().left();
        baseMax = zoomBase().right();
        zoomMin = zoomRect().left();
        zoomMax = zoomRect().right();
258
    } else {
pixhawk's avatar
pixhawk committed
259 260 261 262 263 264 265 266
        mode = d_vScrollData->mode;
        baseMin = zoomBase().top();
        baseMax = zoomBase().bottom();
        zoomMin = zoomRect().top();
        zoomMax = zoomRect().bottom();
    }

    bool needed = false;
267
    switch(mode) {
pixhawk's avatar
pixhawk committed
268
#if QT_VERSION < 0x040000
269
    case QScrollView::AlwaysOn:
pixhawk's avatar
pixhawk committed
270
#else
271
    case Qt::ScrollBarAlwaysOn:
pixhawk's avatar
pixhawk committed
272
#endif
273 274
        needed = true;
        break;
pixhawk's avatar
pixhawk committed
275
#if QT_VERSION < 0x040000
276
    case QScrollView::AlwaysOff:
pixhawk's avatar
pixhawk committed
277
#else
278
    case Qt::ScrollBarAlwaysOff:
pixhawk's avatar
pixhawk committed
279
#endif
280 281 282 283 284 285 286
        needed = false;
        break;
    default: {
        if ( baseMin < zoomMin || baseMax > zoomMax )
            needed = true;
        break;
    }
pixhawk's avatar
pixhawk committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    }
    return needed;
}

void ScrollZoomer::updateScrollBars()
{
    if ( !canvas() )
        return;

    const int xAxis = QwtPlotZoomer::xAxis();
    const int yAxis = QwtPlotZoomer::yAxis();

    int xScrollBarAxis = xAxis;
    if ( hScrollBarPosition() == OppositeToScale )
        xScrollBarAxis = oppositeAxis(xScrollBarAxis);

    int yScrollBarAxis = yAxis;
    if ( vScrollBarPosition() == OppositeToScale )
        yScrollBarAxis = oppositeAxis(yScrollBarAxis);


    QwtPlotLayout *layout = plot()->plotLayout();

    bool showHScrollBar = needScrollBar(Qt::Horizontal);
311
    if ( showHScrollBar ) {
pixhawk's avatar
pixhawk committed
312 313 314 315 316 317 318 319 320 321
        ScrollBar *sb = scrollBar(Qt::Horizontal);

        sb->setPalette(plot()->palette());

        const QwtScaleEngine *se = plot()->axisScaleEngine(xAxis);
        sb->setInverted(se->testAttribute(QwtScaleEngine::Inverted));

        sb->setBase(zoomBase().left(), zoomBase().right());
        sb->moveSlider(zoomRect().left(), zoomRect().right());

322
        if ( !sb->isVisibleTo(canvas()) ) {
pixhawk's avatar
pixhawk committed
323 324
            sb->show();
            layout->setCanvasMargin(layout->canvasMargin(xScrollBarAxis)
325
                                    + sb->extent(), xScrollBarAxis);
pixhawk's avatar
pixhawk committed
326
        }
327 328
    } else {
        if ( horizontalScrollBar() ) {
pixhawk's avatar
pixhawk committed
329 330
            horizontalScrollBar()->hide();
            layout->setCanvasMargin(layout->canvasMargin(xScrollBarAxis)
331
                                    - horizontalScrollBar()->extent(), xScrollBarAxis);
pixhawk's avatar
pixhawk committed
332 333 334 335
        }
    }

    bool showVScrollBar = needScrollBar(Qt::Vertical);
336
    if ( showVScrollBar ) {
pixhawk's avatar
pixhawk committed
337 338 339 340 341 342 343 344 345 346
        ScrollBar *sb = scrollBar(Qt::Vertical);

        sb->setPalette(plot()->palette());

        const QwtScaleEngine *se = plot()->axisScaleEngine(xAxis);
        sb->setInverted(!(se->testAttribute(QwtScaleEngine::Inverted)));

        sb->setBase(zoomBase().top(), zoomBase().bottom());
        sb->moveSlider(zoomRect().top(), zoomRect().bottom());

347
        if ( !sb->isVisibleTo(canvas()) ) {
pixhawk's avatar
pixhawk committed
348 349
            sb->show();
            layout->setCanvasMargin(layout->canvasMargin(yScrollBarAxis)
350
                                    + sb->extent(), yScrollBarAxis);
pixhawk's avatar
pixhawk committed
351
        }
352 353
    } else {
        if ( verticalScrollBar() ) {
pixhawk's avatar
pixhawk committed
354 355
            verticalScrollBar()->hide();
            layout->setCanvasMargin(layout->canvasMargin(yScrollBarAxis)
356
                                    - verticalScrollBar()->extent(), yScrollBarAxis);
pixhawk's avatar
pixhawk committed
357 358 359
        }
    }

360 361
    if ( showHScrollBar && showVScrollBar ) {
        if ( d_cornerWidget == NULL ) {
pixhawk's avatar
pixhawk committed
362 363 364 365 366 367 368
            d_cornerWidget = new QWidget(canvas());
#if QT_VERSION >= 0x040100
            d_cornerWidget->setAutoFillBackground(true);
#endif
            d_cornerWidget->setPalette(plot()->palette());
        }
        d_cornerWidget->show();
369
    } else {
pixhawk's avatar
pixhawk committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        if ( d_cornerWidget )
            d_cornerWidget->hide();
    }

    layoutScrollBars(((QwtPlotCanvas *)canvas())->contentsRect());
    plot()->updateLayout();
}

void ScrollZoomer::layoutScrollBars(const QRect &rect)
{
    int hPos = xAxis();
    if ( hScrollBarPosition() == OppositeToScale )
        hPos = oppositeAxis(hPos);

    int vPos = yAxis();
    if ( vScrollBarPosition() == OppositeToScale )
        vPos = oppositeAxis(vPos);

    ScrollBar *hScrollBar = horizontalScrollBar();
    ScrollBar *vScrollBar = verticalScrollBar();

    const int hdim = hScrollBar ? hScrollBar->extent() : 0;
    const int vdim = vScrollBar ? vScrollBar->extent() : 0;

394
    if ( hScrollBar && hScrollBar->isVisible() ) {
pixhawk's avatar
pixhawk committed
395 396
        int x = rect.x();
        int y = (hPos == QwtPlot::xTop)
397
                ? rect.top() : rect.bottom() - hdim + 1;
pixhawk's avatar
pixhawk committed
398 399
        int w = rect.width();

400
        if ( vScrollBar && vScrollBar->isVisible() ) {
pixhawk's avatar
pixhawk committed
401 402 403 404 405 406 407
            if ( vPos == QwtPlot::yLeft )
                x += vdim;
            w -= vdim;
        }

        hScrollBar->setGeometry(x, y, w, hdim);
    }
408
    if ( vScrollBar && vScrollBar->isVisible() ) {
pixhawk's avatar
pixhawk committed
409 410 411 412 413
        int pos = yAxis();
        if ( vScrollBarPosition() == OppositeToScale )
            pos = oppositeAxis(pos);

        int x = (vPos == QwtPlot::yLeft)
414
                ? rect.left() : rect.right() - vdim + 1;
pixhawk's avatar
pixhawk committed
415 416 417 418
        int y = rect.y();

        int h = rect.height();

419
        if ( hScrollBar && hScrollBar->isVisible() ) {
pixhawk's avatar
pixhawk committed
420 421 422 423 424 425 426 427 428
            if ( hPos == QwtPlot::xTop )
                y += hdim;

            h -= hdim;
        }

        vScrollBar->setGeometry(x, y, vdim, h);
    }
    if ( hScrollBar && hScrollBar->isVisible() &&
429 430
            vScrollBar && vScrollBar->isVisible() ) {
        if ( d_cornerWidget ) {
pixhawk's avatar
pixhawk committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
            QRect cornerRect(
                vScrollBar->pos().x(), hScrollBar->pos().y(),
                vdim, hdim);
            d_cornerWidget->setGeometry(cornerRect);
        }
    }
}

void ScrollZoomer::scrollBarMoved(Qt::Orientation o, double min, double)
{
    if ( o == Qt::Horizontal )
        move(min, zoomRect().top());
    else
        move(zoomRect().left(), min);

    emit zoomed(zoomRect());
}

int ScrollZoomer::oppositeAxis(int axis) const
{
451 452 453 454 455 456 457 458 459 460 461
    switch(axis) {
    case QwtPlot::xBottom:
        return QwtPlot::xTop;
    case QwtPlot::xTop:
        return QwtPlot::xBottom;
    case QwtPlot::yLeft:
        return QwtPlot::yRight;
    case QwtPlot::yRight:
        return QwtPlot::yLeft;
    default:
        break;
pixhawk's avatar
pixhawk committed
462 463 464 465
    }

    return axis;
}