qwt_dyngrid_layout.cpp 16.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
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
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qwidget.h>
#include "qwt_dyngrid_layout.h"
#include "qwt_math.h"

#if QT_VERSION < 0x040000
#include <qvaluelist.h>
#else
#include <qlist.h>
#endif

class QwtDynGridLayout::PrivateData
{
public:

#if QT_VERSION < 0x040000
    class LayoutIterator: public QGLayoutIterator
    {
    public:
        LayoutIterator(PrivateData *data):
29
            d_data(data) {
pixhawk's avatar
pixhawk committed
30 31 32
            d_iterator = d_data->itemList.begin();
        }

33
        virtual QLayoutItem *current() {
pixhawk's avatar
pixhawk committed
34
            if (d_iterator == d_data->itemList.end())
35
                return NULL;
pixhawk's avatar
pixhawk committed
36 37 38 39

            return *d_iterator;
        }

40
        virtual QLayoutItem *next() {
pixhawk's avatar
pixhawk committed
41
            if (d_iterator == d_data->itemList.end())
42
                return NULL;
pixhawk's avatar
pixhawk committed
43 44 45

            d_iterator++;
            if (d_iterator == d_data->itemList.end())
46
                return NULL;
pixhawk's avatar
pixhawk committed
47 48 49 50

            return *d_iterator;
        }

51
        virtual QLayoutItem *takeCurrent() {
pixhawk's avatar
pixhawk committed
52 53 54 55 56 57 58 59 60 61 62
            if ( d_iterator == d_data->itemList.end() )
                return NULL;

            QLayoutItem *item = *d_iterator;

            d_data->isDirty = true;
            d_iterator = d_data->itemList.remove(d_iterator);
            return item;
        }

    private:
63

pixhawk's avatar
pixhawk committed
64 65 66 67 68 69
        QValueListIterator<QLayoutItem*> d_iterator;
        QwtDynGridLayout::PrivateData *d_data;
    };
#endif

    PrivateData():
70
        isDirty(true) {
pixhawk's avatar
pixhawk committed
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
    }

#if QT_VERSION < 0x040000
    typedef QValueList<QLayoutItem*> LayoutItemList;
#else
    typedef QList<QLayoutItem*> LayoutItemList;
#endif

    mutable LayoutItemList itemList;

    uint maxCols;
    uint numRows;
    uint numCols;

#if QT_VERSION < 0x040000
    QSizePolicy::ExpandData expanding;
#else
    Qt::Orientations expanding;
#endif

    bool isDirty;
    QwtArray<QSize> itemSizeHints;
};


/*!
  \param parent Parent widget
  \param margin Margin
  \param spacing Spacing
*/

102 103
QwtDynGridLayout::QwtDynGridLayout(QWidget *parent,
                                   int margin, int spacing):
pixhawk's avatar
pixhawk committed
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
    QLayout(parent)
{
    init();

    setSpacing(spacing);
    setMargin(margin);
}

#if QT_VERSION < 0x040000
/*!
  \param parent Parent widget
  \param spacing Spacing
*/
QwtDynGridLayout::QwtDynGridLayout(QLayout *parent, int spacing):
    QLayout(parent, spacing)
{
    init();
}
#endif

/*!
  \param spacing Spacing
*/

QwtDynGridLayout::QwtDynGridLayout(int spacing)
{
    init();
    setSpacing(spacing);
}

/*!
  Initialize the layout with default values.
*/
void QwtDynGridLayout::init()
{
    d_data = new QwtDynGridLayout::PrivateData;
140 141
    d_data->maxCols = d_data->numRows
                      = d_data->numCols = 0;
pixhawk's avatar
pixhawk committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155

#if QT_VERSION < 0x040000
    d_data->expanding = QSizePolicy::NoDirection;
    setSupportsMargin(true);
#else
    d_data->expanding = 0;
#endif
}

//! Destructor

QwtDynGridLayout::~QwtDynGridLayout()
{
#if QT_VERSION < 0x040000
156
    deleteAllItems();
pixhawk's avatar
pixhawk committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#endif

    delete d_data;
}

void QwtDynGridLayout::invalidate()
{
    d_data->isDirty = true;
    QLayout::invalidate();
}

void QwtDynGridLayout::updateLayoutCache()
{
    d_data->itemSizeHints.resize(itemCount());

    int index = 0;

    for (PrivateData::LayoutItemList::iterator it = d_data->itemList.begin();
175
            it != d_data->itemList.end(); ++it, index++) {
pixhawk's avatar
pixhawk committed
176 177 178 179 180 181 182 183 184 185 186
        d_data->itemSizeHints[int(index)] = (*it)->sizeHint();
    }

    d_data->isDirty = false;
}

/*!
  Limit the number of columns.
  \param maxCols upper limit, 0 means unlimited
  \sa QwtDynGridLayout::maxCols()
*/
187

pixhawk's avatar
pixhawk committed
188 189 190 191 192 193 194 195 196 197 198
void QwtDynGridLayout::setMaxCols(uint maxCols)
{
    d_data->maxCols = maxCols;
}

/*!
  Return the upper limit for the number of columns.
  0 means unlimited, what is the default.
  \sa QwtDynGridLayout::setMaxCols()
*/

199 200 201
uint QwtDynGridLayout::maxCols() const
{
    return d_data->maxCols;
pixhawk's avatar
pixhawk committed
202 203 204 205 206 207 208 209 210 211
}

//! Adds item to the next free position.

void QwtDynGridLayout::addItem(QLayoutItem *item)
{
    d_data->itemList.append(item);
    invalidate();
}

212 213
/*!
  \return true if this layout is empty.
pixhawk's avatar
pixhawk committed
214 215 216 217 218 219 220
*/

bool QwtDynGridLayout::isEmpty() const
{
    return d_data->itemList.isEmpty();
}

221
/*!
pixhawk's avatar
pixhawk committed
222 223 224 225 226 227 228 229 230
  \return number of layout items
*/

uint QwtDynGridLayout::itemCount() const
{
    return d_data->itemList.count();
}

#if  QT_VERSION < 0x040000
231
/*!
pixhawk's avatar
pixhawk committed
232 233 234 235
  \return An iterator over the children of this layout.
*/

QLayoutIterator QwtDynGridLayout::iterator()
236 237 238
{
    return QLayoutIterator(
               new QwtDynGridLayout::PrivateData::LayoutIterator(d_data) );
pixhawk's avatar
pixhawk committed
239 240 241
}

/*!
242 243 244 245
  Set whether this layout can make use of more space than sizeHint().
  A value of Vertical or Horizontal means that it wants to grow in only
  one dimension, while BothDirections means that it wants to grow in
  both dimensions. The default value is NoDirection.
pixhawk's avatar
pixhawk committed
246 247 248 249 250 251 252 253 254
  \sa QwtDynGridLayout::expanding()
*/

void QwtDynGridLayout::setExpanding(QSizePolicy::ExpandData expanding)
{
    d_data->expanding = expanding;
}

/*!
255 256 257 258
  Returns whether this layout can make use of more space than sizeHint().
  A value of Vertical or Horizontal means that it wants to grow in only
  one dimension, while BothDirections means that it wants to grow in
  both dimensions.
pixhawk's avatar
pixhawk committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  \sa QwtDynGridLayout::setExpanding()
*/

QSizePolicy::ExpandData QwtDynGridLayout::expanding() const
{
    return d_data->expanding;
}

#else // QT_VERSION >= 0x040000

QLayoutItem *QwtDynGridLayout::itemAt( int index ) const
{
    if ( index < 0 || index >= d_data->itemList.count() )
        return NULL;

    return d_data->itemList.at(index);
}
276

pixhawk's avatar
pixhawk committed
277 278 279 280
QLayoutItem *QwtDynGridLayout::takeAt( int index )
{
    if ( index < 0 || index >= d_data->itemList.count() )
        return NULL;
281

pixhawk's avatar
pixhawk committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    d_data->isDirty = true;
    return d_data->itemList.takeAt(index);
}

int QwtDynGridLayout::count() const
{
    return d_data->itemList.count();
}

void QwtDynGridLayout::setExpandingDirections(Qt::Orientations expanding)
{
    d_data->expanding = expanding;
}

Qt::Orientations QwtDynGridLayout::expandingDirections() const
{
    return d_data->expanding;
}

#endif

/*!
304 305
  Reorganizes columns and rows and resizes managed widgets within
  the rectangle rect.
pixhawk's avatar
pixhawk committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
*/

void QwtDynGridLayout::setGeometry(const QRect &rect)
{
    QLayout::setGeometry(rect);

    if ( isEmpty() )
        return;

    d_data->numCols = columnsForWidth(rect.width());
    d_data->numRows = itemCount() / d_data->numCols;
    if ( itemCount() % d_data->numCols )
        d_data->numRows++;

#if QT_VERSION < 0x040000
    QValueList<QRect> itemGeometries = layoutItems(rect, d_data->numCols);
#else
    QList<QRect> itemGeometries = layoutItems(rect, d_data->numCols);
#endif

    int index = 0;
    for (PrivateData::LayoutItemList::iterator it = d_data->itemList.begin();
328
            it != d_data->itemList.end(); ++it) {
pixhawk's avatar
pixhawk committed
329
        QWidget *w = (*it)->widget();
330
        if ( w ) {
pixhawk's avatar
pixhawk committed
331 332 333 334 335 336
            w->setGeometry(itemGeometries[index]);
            index++;
        }
    }
}

337
/*!
pixhawk's avatar
pixhawk committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  Calculate the number of columns for a given width. It tries to
  use as many columns as possible (limited by maxCols())

  \param width Available width for all columns
  \sa QwtDynGridLayout::maxCols(), QwtDynGridLayout::setMaxCols()
*/

uint QwtDynGridLayout::columnsForWidth(int width) const
{
    if ( isEmpty() )
        return 0;

    const int maxCols = (d_data->maxCols > 0) ? d_data->maxCols : itemCount();
    if ( maxRowWidth(maxCols) <= width )
        return maxCols;

354
    for (int numCols = 2; numCols <= maxCols; numCols++ ) {
pixhawk's avatar
pixhawk committed
355 356 357 358 359 360 361 362
        const int rowWidth = maxRowWidth(numCols);
        if ( rowWidth > width )
            return numCols - 1;
    }

    return 1; // At least 1 column
}

363
/*!
pixhawk's avatar
pixhawk committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  Calculate the width of a layout for a given number of
  columns.

  \param numCols Given number of columns
  \param itemWidth Array of the width hints for all items
*/
int QwtDynGridLayout::maxRowWidth(int numCols) const
{
    int col;

    QwtArray<int> colWidth(numCols);
    for ( col = 0; col < (int)numCols; col++ )
        colWidth[col] = 0;

    if ( d_data->isDirty )
        ((QwtDynGridLayout*)this)->updateLayoutCache();

381 382
    for ( uint index = 0;
            index < (uint)d_data->itemSizeHints.count(); index++ ) {
pixhawk's avatar
pixhawk committed
383
        col = index % numCols;
384 385
        colWidth[col] = qwtMax(colWidth[col],
                               d_data->itemSizeHints[int(index)].width());
pixhawk's avatar
pixhawk committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    }

    int rowWidth = 2 * margin() + (numCols - 1) * spacing();
    for ( col = 0; col < (int)numCols; col++ )
        rowWidth += colWidth[col];

    return rowWidth;
}

/*!
  \return the maximum width of all layout items
*/

int QwtDynGridLayout::maxItemWidth() const
{
    if ( isEmpty() )
        return 0;

    if ( d_data->isDirty )
        ((QwtDynGridLayout*)this)->updateLayoutCache();

    int w = 0;
408
    for ( uint i = 0; i < (uint)d_data->itemSizeHints.count(); i++ ) {
pixhawk's avatar
pixhawk committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        const int itemW = d_data->itemSizeHints[int(i)].width();
        if ( itemW > w )
            w = itemW;
    }

    return w;
}

/*!
  Calculate the geometries of the layout items for a layout
  with numCols columns and a given rect.
  \param rect Rect where to place the items
  \param numCols Number of columns
  \return item geometries
*/

#if QT_VERSION < 0x040000
QValueList<QRect> QwtDynGridLayout::layoutItems(const QRect &rect,
427
        uint numCols) const
pixhawk's avatar
pixhawk committed
428 429
#else
QList<QRect> QwtDynGridLayout::layoutItems(const QRect &rect,
430
        uint numCols) const
pixhawk's avatar
pixhawk committed
431 432 433 434 435 436 437 438 439 440 441 442 443
#endif
{
#if QT_VERSION < 0x040000
    QValueList<QRect> itemGeometries;
#else
    QList<QRect> itemGeometries;
#endif
    if ( numCols == 0 || isEmpty() )
        return itemGeometries;

    uint numRows = itemCount() / numCols;
    if ( numRows % itemCount() )
        numRows++;
444

pixhawk's avatar
pixhawk committed
445 446
    QwtArray<int> rowHeight(numRows);
    QwtArray<int> colWidth(numCols);
447

pixhawk's avatar
pixhawk committed
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
    layoutGrid(numCols, rowHeight, colWidth);

    bool expandH, expandV;
#if QT_VERSION >= 0x040000
    expandH = expandingDirections() & Qt::Horizontal;
    expandV = expandingDirections() & Qt::Vertical;
#else
    expandH = expanding() & QSizePolicy::Horizontally;
    expandV = expanding() & QSizePolicy::Vertically;
#endif

    if ( expandH || expandV )
        stretchGrid(rect, numCols, rowHeight, colWidth);

    QwtDynGridLayout *that = (QwtDynGridLayout *)this;
    const int maxCols = d_data->maxCols;
    that->d_data->maxCols = numCols;
    const QRect alignedRect = alignmentRect(rect);
    that->d_data->maxCols = maxCols;

    const int xOffset = expandH ? 0 : alignedRect.x();
    const int yOffset = expandV ? 0 : alignedRect.y();

    QwtArray<int> colX(numCols);
    QwtArray<int> rowY(numRows);

    const int xySpace = spacing();

    rowY[0] = yOffset + margin();
    for ( int r = 1; r < (int)numRows; r++ )
        rowY[r] = rowY[r-1] + rowHeight[r-1] + xySpace;

    colX[0] = xOffset + margin();
    for ( int c = 1; c < (int)numCols; c++ )
        colX[c] = colX[c-1] + colWidth[c-1] + xySpace;
483

pixhawk's avatar
pixhawk committed
484
    const int itemCount = d_data->itemList.size();
485
    for ( int i = 0; i < itemCount; i++ ) {
pixhawk's avatar
pixhawk committed
486 487 488
        const int row = i / numCols;
        const int col = i % numCols;

489 490
        QRect itemGeometry(colX[col], rowY[row],
                           colWidth[col], rowHeight[row]);
pixhawk's avatar
pixhawk committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        itemGeometries.append(itemGeometry);
    }

    return itemGeometries;
}


/*!
  Calculate the dimensions for the columns and rows for a grid
  of numCols columns.
  \param numCols Number of columns.
  \param rowHeight Array where to fill in the calculated row heights.
  \param colWidth Array where to fill in the calculated column widths.
*/

506 507
void QwtDynGridLayout::layoutGrid(uint numCols,
                                  QwtArray<int>& rowHeight, QwtArray<int>& colWidth) const
pixhawk's avatar
pixhawk committed
508 509 510 511 512 513 514
{
    if ( numCols <= 0 )
        return;

    if ( d_data->isDirty )
        ((QwtDynGridLayout*)this)->updateLayoutCache();

515 516
    for ( uint index = 0;
            index < (uint)d_data->itemSizeHints.count(); index++ ) {
pixhawk's avatar
pixhawk committed
517 518 519 520 521
        const int row = index / numCols;
        const int col = index % numCols;

        const QSize &size = d_data->itemSizeHints[int(index)];

522 523 524 525
        rowHeight[row] = (col == 0)
                         ? size.height() : qwtMax(rowHeight[row], size.height());
        colWidth[col] = (row == 0)
                        ? size.width() : qwtMax(colWidth[col], size.width());
pixhawk's avatar
pixhawk committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539
    }
}

/*!
  \return true: QwtDynGridLayout implements heightForWidth.
  \sa QwtDynGridLayout::heightForWidth()
*/

bool QwtDynGridLayout::hasHeightForWidth() const
{
    return true;
}

/*!
540
  \return The preferred height for this layout, given the width w.
pixhawk's avatar
pixhawk committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  \sa QwtDynGridLayout::hasHeightForWidth()
*/

int QwtDynGridLayout::heightForWidth(int width) const
{
    if ( isEmpty() )
        return 0;

    const uint numCols = columnsForWidth(width);
    uint numRows = itemCount() / numCols;
    if ( itemCount() % numCols )
        numRows++;

    QwtArray<int> rowHeight(numRows);
    QwtArray<int> colWidth(numCols);

    layoutGrid(numCols, rowHeight, colWidth);

    int h = 2 * margin() + (numRows - 1) * spacing();
    for ( int row = 0; row < (int)numRows; row++ )
        h += rowHeight[row];

    return h;
}

/*!
  Stretch columns in case of expanding() & QSizePolicy::Horizontal and
  rows in case of expanding() & QSizePolicy::Vertical to fill the entire
  rect. Rows and columns are stretched with the same factor.
  \sa QwtDynGridLayout::setExpanding(), QwtDynGridLayout::expanding()
*/

573 574
void QwtDynGridLayout::stretchGrid(const QRect &rect,
                                   uint numCols, QwtArray<int>& rowHeight, QwtArray<int>& colWidth) const
pixhawk's avatar
pixhawk committed
575 576 577 578 579 580 581 582 583 584 585 586 587
{
    if ( numCols == 0 || isEmpty() )
        return;

    bool expandH, expandV;
#if QT_VERSION >= 0x040000
    expandH = expandingDirections() & Qt::Horizontal;
    expandV = expandingDirections() & Qt::Vertical;
#else
    expandH = expanding() & QSizePolicy::Horizontally;
    expandV = expanding() & QSizePolicy::Vertically;
#endif

588
    if ( expandH ) {
pixhawk's avatar
pixhawk committed
589 590 591 592
        int xDelta = rect.width() - 2 * margin() - (numCols - 1) * spacing();
        for ( int col = 0; col < (int)numCols; col++ )
            xDelta -= colWidth[col];

593 594
        if ( xDelta > 0 ) {
            for ( int col = 0; col < (int)numCols; col++ ) {
pixhawk's avatar
pixhawk committed
595 596 597 598 599 600 601
                const int space = xDelta / (numCols - col);
                colWidth[col] += space;
                xDelta -= space;
            }
        }
    }

602
    if ( expandV ) {
pixhawk's avatar
pixhawk committed
603 604 605 606 607 608 609 610
        uint numRows = itemCount() / numCols;
        if ( itemCount() % numCols )
            numRows++;

        int yDelta = rect.height() - 2 * margin() - (numRows - 1) * spacing();
        for ( int row = 0; row < (int)numRows; row++ )
            yDelta -= rowHeight[row];

611 612
        if ( yDelta > 0 ) {
            for ( int row = 0; row < (int)numRows; row++ ) {
pixhawk's avatar
pixhawk committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
                const int space = yDelta / (numRows - row);
                rowHeight[row] += space;
                yDelta -= space;
            }
        }
    }
}

/*!
   Return the size hint. If maxCols() > 0 it is the size for
   a grid with maxCols() columns, otherwise it is the size for
   a grid with only one row.
   \sa QwtDynGridLayout::maxCols(), QwtDynGridLayout::setMaxCols()
*/

QSize QwtDynGridLayout::sizeHint() const
{
    if ( isEmpty() )
        return QSize();

    const uint numCols = (d_data->maxCols > 0 ) ? d_data->maxCols : itemCount();
    uint numRows = itemCount() / numCols;
    if ( itemCount() % numCols )
        numRows++;

    QwtArray<int> rowHeight(numRows);
    QwtArray<int> colWidth(numCols);

    layoutGrid(numCols, rowHeight, colWidth);

    int h = 2 * margin() + (numRows - 1) * spacing();
    for ( int row = 0; row < (int)numRows; row++ )
        h += rowHeight[row];

647
    int w = 2 * margin() + (numCols - 1) * spacing();
pixhawk's avatar
pixhawk committed
648 649 650 651 652 653 654 655 656 657 658
    for ( int col = 0; col < (int)numCols; col++ )
        w += colWidth[col];

    return QSize(w, h);
}

/*!
  \return Number of rows of the current layout.
  \sa QwtDynGridLayout::numCols
  \warning The number of rows might change whenever the geometry changes
*/
659 660 661
uint QwtDynGridLayout::numRows() const
{
    return d_data->numRows;
pixhawk's avatar
pixhawk committed
662 663 664 665 666 667 668
}

/*!
  \return Number of columns of the current layout.
  \sa QwtDynGridLayout::numRows
  \warning The number of columns might change whenever the geometry changes
*/
669 670 671
uint QwtDynGridLayout::numCols() const
{
    return d_data->numCols;
pixhawk's avatar
pixhawk committed
672
}