1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qgraphicslayoutstyleinfo_p.h"
5
6#include "qgraphicslayout_p.h"
7#include "qgraphicswidget.h"
8#include <QtWidgets/qstyle.h>
9#include <QtWidgets/qwidget.h>
10#include <QtWidgets/qapplication.h>
11
12QT_BEGIN_NAMESPACE
13
14QGraphicsLayoutStyleInfo::QGraphicsLayoutStyleInfo(const QGraphicsLayoutPrivate *layout)
15 : m_layout(layout), m_style(nullptr)
16{
17 m_widget.reset(p: new QWidget); // pixelMetric might need a widget ptr
18 m_styleOption.initFrom(w: m_widget.get());
19 m_isWindow = m_styleOption.state & QStyle::State_Window;
20}
21
22QGraphicsLayoutStyleInfo::~QGraphicsLayoutStyleInfo()
23{
24}
25
26qreal QGraphicsLayoutStyleInfo::combinedLayoutSpacing(QLayoutPolicy::ControlTypes controls1,
27 QLayoutPolicy::ControlTypes controls2,
28 Qt::Orientation orientation) const
29{
30 Q_ASSERT(style());
31 return style()->combinedLayoutSpacing(controls1: QSizePolicy::ControlTypes(int(controls1)), controls2: QSizePolicy::ControlTypes(int(controls2)),
32 orientation, option: const_cast<QStyleOption*>(&m_styleOption), widget: widget());
33}
34
35qreal QGraphicsLayoutStyleInfo::perItemSpacing(QLayoutPolicy::ControlType control1,
36 QLayoutPolicy::ControlType control2,
37 Qt::Orientation orientation) const
38{
39 Q_ASSERT(style());
40 return style()->layoutSpacing(control1: QSizePolicy::ControlType(control1), control2: QSizePolicy::ControlType(control2),
41 orientation, option: const_cast<QStyleOption*>(&m_styleOption), widget: widget());
42}
43
44qreal QGraphicsLayoutStyleInfo::spacing(Qt::Orientation orientation) const
45{
46 Q_ASSERT(style());
47 return style()->pixelMetric(metric: orientation == Qt::Horizontal
48 ? QStyle::PM_LayoutHorizontalSpacing : QStyle::PM_LayoutVerticalSpacing,
49 option: &m_styleOption);
50}
51
52qreal QGraphicsLayoutStyleInfo::windowMargin(Qt::Orientation orientation) const
53{
54 return style()->pixelMetric(metric: orientation == Qt::Vertical
55 ? QStyle::PM_LayoutBottomMargin
56 : QStyle::PM_LayoutRightMargin,
57 option: const_cast<QStyleOption*>(&m_styleOption), widget: widget());
58}
59
60QWidget *QGraphicsLayoutStyleInfo::widget() const { return m_widget.get(); }
61
62QStyle *QGraphicsLayoutStyleInfo::style() const
63{
64 if (!m_style) {
65 Q_ASSERT(m_layout);
66 QGraphicsItem *item = m_layout->parentItem();
67 m_style = (item && item->isWidget()) ? static_cast<QGraphicsWidget*>(item)->style() : QApplication::style();
68 }
69 return m_style;
70}
71
72QT_END_NAMESPACE
73

source code of qtbase/src/widgets/graphicsview/qgraphicslayoutstyleinfo.cpp