示例#1
0
void tst_QGraphicsLayout::moveAndResizeWidgetInWidget()
{
    QFETCH(bool, instantInvalidatePropagation);

    QGraphicsLayout::setInstantInvalidatePropagation(instantInvalidatePropagation);
    QGraphicsScene scene;

    QGraphicsWidget *widget = new QGraphicsWidget;
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(widget);
    layout->setContentsMargins(0,0,0,0);
    WidgetToTestResizeEvents *innerWidget = new WidgetToTestResizeEvents;
    QGraphicsLinearLayout *innerLayout = new QGraphicsLinearLayout(innerWidget);
    innerLayout->setContentsMargins(0,0,0,0);
    QCOMPARE(widget->maximumSize(), QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
    layout->addItem(innerWidget);
    widget->setMinimumSize(1,1);
    widget->setPreferredSize(1000,1000);
    widget->setMaximumSize(2000,2000);
    widget->resize(widget->preferredSize());
    innerWidget->setMinimumSize(1,1);
    qApp->processEvents();
    innerWidget->resizeEventCalled = false;

    QCOMPARE(widget->size(), QSizeF(1000, 1000));
    QCOMPARE(layout->geometry().size(), QSizeF(1000, 1000));
    QCOMPARE(innerWidget->size(), QSizeF(1000, 1000));

    innerLayout->invalidate();
    widget->setMaximumHeight(500);
    widget->setX(1);
    qApp->processEvents();
    QCOMPARE(widget->size(), QSizeF(1000, 500));
    QCOMPARE(innerWidget->size(), QSizeF(1000, 500));
    QVERIFY(innerWidget->resizeEventCalled);
}
示例#2
0
void QDeclarativeLoaderPrivate::_q_updateSize(bool loaderGeometryChanged)
{
    Q_Q(QDeclarativeLoader);
    if (!item || updatingSize)
        return;

    updatingSize = true;
    if (QDeclarativeItem *qmlItem = qobject_cast<QDeclarativeItem*>(item)) {
        if (!itemWidthValid)
            q->setImplicitWidth(qmlItem->implicitWidth());
        else
            q->setImplicitWidth(qmlItem->width());
        if (loaderGeometryChanged && q->widthValid())
            qmlItem->setWidth(q->width());
        if (!itemHeightValid)
            q->setImplicitHeight(qmlItem->implicitHeight());
        else
            q->setImplicitHeight(qmlItem->height());
        if (loaderGeometryChanged && q->heightValid())
            qmlItem->setHeight(q->height());
    } else if (item && item->isWidget()) {
        QGraphicsWidget *widget = static_cast<QGraphicsWidget*>(item);
        QSizeF widgetSize = widget->size();
        q->setImplicitWidth(widgetSize.width());
        if (loaderGeometryChanged && q->widthValid())
            widgetSize.setWidth(q->width());
        q->setImplicitHeight(widgetSize.height());
        if (loaderGeometryChanged && q->heightValid())
            widgetSize.setHeight(q->height());
        if (widget->size() != widgetSize)
            widget->resize(widgetSize);
    }
    updatingSize = false;
}
/*!
    Activates the layout, causing all items in the layout to be immediately
    rearranged. This function is based on calling count() and itemAt(), and
    then calling setGeometry() on all items sequentially. When activated,
    the layout will adjust its geometry to its parent's contentsRect().
    The parent will then invalidate any layout of its own.

    If called in sequence or recursively, e.g., by one of the arranged items
    in response to being resized, this function will do nothing.

    Note that the layout is free to use geometry caching to optimize this
    process.  To forcefully invalidate any such cache, you can call
    invalidate() before calling activate().

    \sa invalidate()
*/
void QGraphicsLayout::activate()
{
    Q_D(QGraphicsLayout);
    if (d->activated)
        return;

    d->activateRecursive(this);

    // we don't call activate on a sublayout, but somebody might.
    // Therefore, we walk to the parentitem of the toplevel layout.
    QGraphicsLayoutItem *parentItem = this;
    while (parentItem && parentItem->isLayout())
        parentItem = parentItem->parentLayoutItem();
    if (!parentItem)
        return;
    Q_ASSERT(!parentItem->isLayout());

    if (QGraphicsLayout::instantInvalidatePropagation()) {
        QGraphicsWidget *parentWidget = static_cast<QGraphicsWidget*>(parentItem);
        if (!parentWidget->parentLayoutItem()) {
            // we've reached the topmost widget, resize it
            bool wasResized = parentWidget->testAttribute(Qt::WA_Resized);
            parentWidget->resize(parentWidget->size());
            parentWidget->setAttribute(Qt::WA_Resized, wasResized);
        }

        setGeometry(parentItem->contentsRect());    // relayout children
    } else {
        setGeometry(parentItem->contentsRect());    // relayout children
        parentLayoutItem()->updateGeometry();
    }
}
    void updateChildGeometry()
    {
        if (!mChild) {
            return;
        }
        if (mInsideUpdateChildGeometry) {
            return;
        }
        mInsideUpdateChildGeometry = true;

        int posX, posY;
        int childWidth, childHeight;
        int parentWidth, parentHeight;

        childWidth = mChild->size().width();
        childHeight = mChild->size().height();

        parentWidth = mParent->size().width();
        parentHeight = mParent->size().height();

        if (mAlignment & Qt::AlignLeft) {
            posX = mHorizontalMargin;
        } else if (mAlignment & Qt::AlignHCenter) {
            posX = (parentWidth - childWidth) / 2;
        } else if (mAlignment & Qt::AlignJustify) {
            posX = mHorizontalMargin;
            childWidth = parentWidth - 2 * mHorizontalMargin;
            QRectF childGeometry = mChild->geometry();
            childGeometry.setWidth(childWidth);
            mChild->setGeometry(childGeometry);
        } else {
            posX = parentWidth - childWidth - mHorizontalMargin;
        }

        if (mAlignment & Qt::AlignTop) {
            posY = mVerticalMargin;
        } else if (mAlignment & Qt::AlignVCenter) {
            posY = (parentHeight - childHeight) / 2;
        } else {
            posY = parentHeight - childHeight - mVerticalMargin;
        }

        mChild->setGeometry(posX, posY, childWidth, childHeight);

        mInsideUpdateChildGeometry = false;
    }
示例#5
0
void tst_QGraphicsLayout::changingMinimumSize()
{
    QFETCH(bool, instantInvalidatePropagation);
    QGraphicsLayout::setInstantInvalidatePropagation(instantInvalidatePropagation);
    QGraphicsWidget *widget = new QGraphicsWidget;
    qApp->processEvents();
    widget->setMinimumSize(300,300);
    qApp->processEvents();
    QCOMPARE(widget->size(), QSizeF(300,300));
    QGraphicsLayout::setInstantInvalidatePropagation(false);
}
void PeopleApplication::scrollTo(qreal pos)
{
    // effects: scrolls to the given vertical position, up to the maximum

    MApplicationPage *page = activeApplicationWindow()->currentPage();
    QGraphicsWidget *widget = page->centralWidget();
    MPannableWidget *viewport = page->pannableViewport();
    if (!widget || !viewport)
        return;

    QRectF exposed = page->exposedContentRect();
    int max = widget->size().height() - exposed.height();
    if (pos > max)
        pos = max;

    viewport->physics()->stop();
    viewport->physics()->setPosition(QPointF(0, pos));
}
void Ut_MPannableViewport::disabledViewportShouldNotAllowWigetToBeBiggerThanViewport()
{
    subject->setVerticalPanningPolicy(MPannableWidget::PanningAlwaysOff);
    subject->setHorizontalPanningPolicy(MPannableWidget::PanningAlwaysOff);

    QGraphicsWidget *widget = new QGraphicsWidget();
    widget->setPreferredSize(1000,1000);
    subject->setWidget(widget);


    // Forcing the size of subject to some value
    subject->setMinimumSize(QSizeF(500, 300));
    subject->setMaximumSize(QSizeF(500, 300));

    subject->adjustSize();

    QCOMPARE(widget->size(), QSizeF(500,300));
    subject->setVerticalPanningPolicy(MPannableWidget::PanningAlwaysOn);
    subject->setHorizontalPanningPolicy(MPannableWidget::PanningAlwaysOff);

}
/*!
  Traverse QObject based items.
*/
void TasQtTraverse::traverseObject(TasObject* objectInfo, QObject* object, TasCommand* command)
{
     // Embedded apps must use coordinates for operations, as the parent has no knowledge of the
    // Actual items
    bool embeddedApp = false;
    if (command && command->parameter("embedded") == "true") {
        embeddedApp = true;
    }
    //TasLogger::logger()->debug("TasQtTraverse::traverseObject in");
    mTraverseUtils->addObjectDetails(objectInfo, object);
    QGraphicsObject* graphicsObject = qobject_cast<QGraphicsObject*>(object);
    if(graphicsObject){
        objectInfo->addAttribute("objectType", embeddedApp? TYPE_WEB : TYPE_GRAPHICS_VIEW);
        mTraverseUtils->addGraphicsItemCoordinates(objectInfo, graphicsObject, command);
        mTraverseUtils->printGraphicsItemProperties(objectInfo, graphicsObject);

        //add details only for graphicsitems
        QGraphicsWidget* graphicsWidget = qobject_cast<QGraphicsWidget*>(object);
        if(graphicsWidget){
            mTraverseUtils->addFont(objectInfo, graphicsWidget->font());
            // Elided format "this is a text" -> "this is a..." text for
            // items that have the "text" property.
            QVariant text = graphicsWidget->property("text");
            if (text.isValid()) {
                mTraverseUtils->addTextInfo(objectInfo, text.toString(), graphicsWidget->font(), graphicsWidget->size().width());

            }
            QVariant plainText = graphicsWidget->property("plainText");
            if (plainText.isValid()) {
                mTraverseUtils->addTextInfo(objectInfo, plainText.toString(), graphicsWidget->font(), graphicsWidget->size().width());
            }
        }
    }
    else{
        QQuickItem* quickObject = qobject_cast<QQuickItem*>(object);

        if (quickObject) {
            objectInfo->addAttribute("objectType", TYPE_QSCENEGRAPH );
        }
        //make sure that we are dealing with a widget
        else if (object->isWidgetType()){
            QWidget* widget = qobject_cast<QWidget*>(object);
            objectInfo->addAttribute("objectType", TYPE_STANDARD_VIEW );

            addWidgetCoordinates(objectInfo, widget,command);
            mTraverseUtils->addFont(objectInfo, widget->font());
            //check is the widget a viewport to graphicsscene
            QWidget* parentWidget = widget->parentWidget();
            bool isViewPort = false;
            if(parentWidget && parentWidget->inherits("QGraphicsView")){
                QGraphicsView* view = qobject_cast<QGraphicsView*>(parentWidget);
                if(view->viewport() == widget){
                    isViewPort = true;
                }
            }
            //add transformation details
            QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(object);
            if(graphicsView){
                objectInfo->addBooleanAttribute("isTransformed", graphicsView->isTransformed());
                if(graphicsView->isTransformed()){
                    QTransform tr = graphicsView->transform();
                    objectInfo->addAttribute("transformM11",tr.m11());
                    objectInfo->addAttribute("transformM12",tr.m12());
                    objectInfo->addAttribute("transformM13",tr.m13());
                    objectInfo->addAttribute("transformM21",tr.m21());
                    objectInfo->addAttribute("transformM22",tr.m22());
                    objectInfo->addAttribute("transformM23",tr.m23());
                    objectInfo->addAttribute("transformM31",tr.m31());
                    objectInfo->addAttribute("transformM32",tr.m32());
                    objectInfo->addAttribute("transformM33",tr.m33());
                }
            }
            objectInfo->addBooleanAttribute("isViewPort", isViewPort);
// Add special window id attribute into qwidget atttributes
#if defined(Q_WS_X11)
            unsigned long wid = static_cast<unsigned long>(widget->effectiveWinId());
            objectInfo->addAttribute("xWindow", (int)wid); // can this fail due to precision?
#endif


        } else if(object->isWindowType()) {
            objectInfo->addAttribute("objectType", TYPE_WINDOW_VIEW);
        } else {
            if(object != qApp){
                objectInfo->addAttribute("objectType", embeddedApp? TYPE_WEB : TYPE_STANDARD_VIEW );
            }
        }

    }
}
QSize ComponentsKoCanvasController::viewportSize() const
{
    QGraphicsWidget *canvasWidget = dynamic_cast<QGraphicsWidget*>(d->canvas);
    return canvasWidget->size().toSize();
}
示例#10
0
bool QtPrinterTableView::print()
{
    Q_D(QtPrinterTableView);
    if (! d->printer->isValid()) {
        qWarning() << "QtPrinterTableView::print: printer not valid, please setup the printer before calling print";
        return false;
    }

    // next use a view just for the filling of the options...
    QGraphicsScene scene;
    QtGraphicsTableView view;
    scene.addItem(&view);
    view.setModel(d->model);


    class QTableHeaderDataProvider2 : public QtGraphicsHeaderDataProvider
    {
    public:
        QTableHeaderDataProvider2(QtTableModelInterface *model_) : model(model_) {}
        QHash<int,QVariant> data(int logicalIndex, const QList<int> &roles) const
        {
            // ### call the model - temporary implementation
            QHash<int,QVariant> hash;
            for (int i = 0; i < roles.count(); ++i)
                if (roles.at(i) == Qt::DisplayRole)
                    hash.insert(Qt::DisplayRole, logicalIndex + 1);
            return hash;
        }
        QtTableModelInterface *model;
    };

    if (d->horizontalHeader) {
        QtGraphicsHeader *header = new QtGraphicsHeader(Qt::Horizontal, &view);
        header->setDataProvider(new QTableHeaderDataProvider2(d->model));
        header->copySections(*d->horizontalHeader);
        view.setHorizontalHeader(header);
    }
    if (d->verticalHeader) {
        QtGraphicsHeader *header = new QtGraphicsHeader(Qt::Vertical, &view);
        header->setDataProvider(new QTableHeaderDataProvider2(d->model));
        header->copySections(*d->verticalHeader);
        view.setVerticalHeader(header);
    }

    QPainter painter;
    if (!painter.begin(d->printer)) {
        qWarning() << "QtPrinterTableView::print: printer fails to begin(), sorry can't print";
        return false;
    }

    // re-layout the header footer to the current page size.
    if (d->header) {
        d->header->documentLayout()->setPaintDevice(d->printer);
        d->header->setPageSize(d->printer->pageRect().size());
    }
    if (d->footer) {
        d->footer->documentLayout()->setPaintDevice(d->printer);
        d->footer->setPageSize(d->printer->pageRect().size());
    }

    const qreal headerSize = d->printHeader(painter);
    const qreal footerSize = d->printFooter(painter);
    const bool vertical = d->orientation == Qt::Vertical;
    const qreal scaleX = d->printer->logicalDpiX() / (qreal) qt_defaultDpiX();
    const qreal scaleY = d->printer->logicalDpiY() / (qreal) qt_defaultDpiY();
    painter.scale(scaleX, scaleY);

    const QRect rect = d->printer->pageRect();
    QSizeF visibleSize(rect.width() / scaleX, rect.height() / scaleY);
    if (vertical)
        visibleSize = QSizeF(visibleSize.width(), visibleSize.height() - headerSize - footerSize);
    else // then rotate it.
        visibleSize = QSizeF(visibleSize.height() - headerSize - footerSize, visibleSize.width());

    view.setGeometry(0, 0, visibleSize.width(), visibleSize.height());
    view.setHorizontalOffset(0);

    int row = 0;
    int column;
    bool first = true;
    qreal offsetInColumn = 0; // for those columns too wide and thus split over more than one page
    while (row < d->model->rowCount()) {
        column = 0;
        view.setFirstRow(row);
        qreal height = visibleSize.height();
        while (true) {
            const qreal rowHeight = view.rowHeight(row);
            if (height - rowHeight < 0)
                break;
            if (row >= d->model->rowCount())
                break;
            ++row;
            height -= rowHeight;
        }
        while (column < d->model->columnCount()) {
            if (!first) {
                d->printer->newPage();
                d->printHeader(painter);
                d->printFooter(painter);
            }
            first = false;
            view.setFirstColumn(column);
            view.setHorizontalOffset(offsetInColumn);

            qreal width = visibleSize.width();
            while (true) {
                const qreal columnWidth = view.columnWidth(column);
                if (width == visibleSize.width() && columnWidth - offsetInColumn > visibleSize.width()) {
                    // the column doesn't fit on a page. Lets split it.
                    offsetInColumn += visibleSize.width();
                    width = 0;
                    break;
                } else if (offsetInColumn > 0) { // we still have a part of a split column to print!
                    width -= columnWidth - offsetInColumn;
                    offsetInColumn = 0;
                    ++column;
                    continue;
                }

                if (width - columnWidth + offsetInColumn < 0)
                    break;
                if (column >= d->model->columnCount())
                    break;
                ++column;
                width -= columnWidth;
            }
            view.doLayout();

            painter.save(); // for each page
            painter.translate(0, headerSize);
            if (!vertical) {
                painter.translate(0, visibleSize.width());
                painter.rotate(-90);
            }
#ifdef DEBUG_TABLES
            painter.setPen(QPen(QColor(Qt::green)));
            painter.drawRect(QRectF(0, 0, visibleSize.width() - width, visibleSize.height() - height));
#endif
            painter.setClipRect(QRectF(0, 0, visibleSize.width() - width, visibleSize.height() - height));
            // find and paint children which are the cells
            QList<QGraphicsItem*> items = scene.items(view.rect());
            QList<QGraphicsItem*>::Iterator iter = items.begin();
            for (;iter != items.end(); ++iter) {
                QGraphicsItem *parent = *iter;
                while (parent != &view && parent != 0) // only draw children of our view
                    parent = parent->parentItem();
                if (parent == 0)
                    continue;
                if (!(*iter)->isVisible())
                    continue;
                painter.save();
                painter.translate((*iter)->mapToItem(&view, QPointF()));
#ifdef DEBUG_TABLES
                QGraphicsWidget *w = dynamic_cast<QGraphicsWidget*>(*iter);
                if (w) {
                    painter.save();
                    painter.setPen(QPen(QColor(Qt::red)));
                    painter.drawRect(QRectF(QPointF(), w->size()));
                    painter.restore();
                }
#endif
                (*iter)->paint(&painter, 0);
                painter.restore();
            }
            painter.restore(); // for each page
        }
    }
    return true;
}