This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QSizePolicy is a Q_GADGET and exposed by QWidget, but does not have any Q_PROPERTY declarations to make it useful. This commit wraps QSizePolicy in a QObject wrapper and overrides the QWidget sizePolicy property to expose the wrapper. The wrapper object is only created the first time it is read. Task-Id: RND-193
- Loading branch information
Showing
7 changed files
with
460 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
#include "declarativesizepolicy_p.h" | ||
|
||
#include <QWidget> | ||
|
||
DeclarativeSizePolicy::DeclarativeSizePolicy(QWidget *extendedWidget, QObject *parent) | ||
: QObject(parent) | ||
, m_extendedWidget(extendedWidget) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
} | ||
|
||
DeclarativeSizePolicy::ControlType DeclarativeSizePolicy::controlType() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return static_cast<DeclarativeSizePolicy::ControlType>(m_extendedWidget->sizePolicy().controlType()); | ||
} | ||
|
||
DeclarativeSizePolicy::Policy DeclarativeSizePolicy::horizontalPolicy() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return static_cast<DeclarativeSizePolicy::Policy>(m_extendedWidget->sizePolicy().horizontalPolicy()); | ||
} | ||
|
||
DeclarativeSizePolicy::Policy DeclarativeSizePolicy::verticalPolicy() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return static_cast<DeclarativeSizePolicy::Policy>(m_extendedWidget->sizePolicy().verticalPolicy()); | ||
} | ||
|
||
Qt::Orientations DeclarativeSizePolicy::expandingDirections() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return m_extendedWidget->sizePolicy().expandingDirections(); | ||
} | ||
|
||
bool DeclarativeSizePolicy::hasHeightForWidth() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return m_extendedWidget->sizePolicy().hasHeightForWidth(); | ||
} | ||
|
||
bool DeclarativeSizePolicy::hasWidthForHeight() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return m_extendedWidget->sizePolicy().hasWidthForHeight(); | ||
} | ||
|
||
int DeclarativeSizePolicy::horizontalStretch() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return m_extendedWidget->sizePolicy().horizontalStretch(); | ||
} | ||
|
||
int DeclarativeSizePolicy::verticalStretch() const | ||
{ | ||
|
||
Q_ASSERT(m_extendedWidget); | ||
|
||
return m_extendedWidget->sizePolicy().verticalStretch(); | ||
} | ||
|
||
bool DeclarativeSizePolicy::retainSizeWhenHidden() const | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
return m_extendedWidget->sizePolicy().retainSizeWhenHidden(); | ||
} | ||
|
||
void DeclarativeSizePolicy::setControlType(DeclarativeSizePolicy::ControlType controlType) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.controlType() == static_cast<QSizePolicy::ControlType>(controlType)) | ||
return; | ||
policy.setControlType(static_cast<QSizePolicy::ControlType>(controlType)); | ||
m_extendedWidget->setSizePolicy(policy); | ||
emit controlTypeChanged(controlType); | ||
} | ||
|
||
void DeclarativeSizePolicy::setHorizontalPolicy(DeclarativeSizePolicy::Policy horizontalPolicy) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.horizontalPolicy() == static_cast<QSizePolicy::Policy>(horizontalPolicy)) | ||
return; | ||
|
||
const Qt::Orientations oldExpandingDirections = policy.expandingDirections(); | ||
policy.setHorizontalPolicy(static_cast<QSizePolicy::Policy>(horizontalPolicy)); | ||
const Qt::Orientations newExpandingDirections = policy.expandingDirections(); | ||
m_extendedWidget->setSizePolicy(policy); | ||
|
||
emit horizontalPolicyChanged(horizontalPolicy); | ||
if (newExpandingDirections != oldExpandingDirections) | ||
emit expandingDirectionsChanged(newExpandingDirections); | ||
} | ||
|
||
void DeclarativeSizePolicy::setVerticalPolicy(DeclarativeSizePolicy::Policy verticalPolicy) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.verticalPolicy() == static_cast<QSizePolicy::Policy>(verticalPolicy)) | ||
return; | ||
|
||
const Qt::Orientations oldExpandingDirections = policy.expandingDirections(); | ||
policy.setVerticalPolicy(static_cast<QSizePolicy::Policy>(verticalPolicy)); | ||
const Qt::Orientations newExpandingDirections = policy.expandingDirections(); | ||
m_extendedWidget->setSizePolicy(policy); | ||
|
||
emit verticalPolicyChanged(verticalPolicy); | ||
if (newExpandingDirections != oldExpandingDirections) | ||
emit expandingDirectionsChanged(newExpandingDirections); | ||
} | ||
|
||
void DeclarativeSizePolicy::setHeightForWidth(bool heightForWidth) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.hasHeightForWidth() == heightForWidth) | ||
return; | ||
policy.setHeightForWidth(heightForWidth); | ||
m_extendedWidget->setSizePolicy(policy); | ||
emit hasHeightForWidthChanged(heightForWidth); | ||
} | ||
|
||
void DeclarativeSizePolicy::setWidthForHeight(bool widthForHeight) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.hasWidthForHeight() == widthForHeight) | ||
return; | ||
policy.setWidthForHeight(widthForHeight); | ||
m_extendedWidget->setSizePolicy(policy); | ||
emit hasWidthForHeightChanged(widthForHeight); | ||
} | ||
|
||
void DeclarativeSizePolicy::setHorizontalStretch(int horizontalStretch) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.horizontalStretch() == horizontalStretch) | ||
return; | ||
policy.setHorizontalStretch(horizontalStretch); | ||
m_extendedWidget->setSizePolicy(policy); | ||
emit horizontalStretchChanged(horizontalStretch); | ||
} | ||
|
||
void DeclarativeSizePolicy::setVerticalStretch(int verticalStretch) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.verticalStretch() == verticalStretch) | ||
return; | ||
policy.setVerticalStretch(verticalStretch); | ||
m_extendedWidget->setSizePolicy(policy); | ||
emit verticalStretchChanged(verticalStretch); | ||
} | ||
|
||
void DeclarativeSizePolicy::setRetainSizeWhenHidden(bool retainSizeWhenHidden) | ||
{ | ||
Q_ASSERT(m_extendedWidget); | ||
|
||
QSizePolicy policy = m_extendedWidget->sizePolicy(); | ||
if (policy.retainSizeWhenHidden() == retainSizeWhenHidden) | ||
return; | ||
policy.setRetainSizeWhenHidden(retainSizeWhenHidden); | ||
m_extendedWidget->setSizePolicy(policy); | ||
emit retainSizeWhenHiddenChanged(retainSizeWhenHidden); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#ifndef DECLARATIVESIZEPOLICYEXTENSION_H | ||
#define DECLARATIVESIZEPOLICYEXTENSION_H | ||
|
||
#include <QObject> | ||
|
||
class DeclarativeSizePolicy : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
Q_PROPERTY(ControlType controlType READ controlType WRITE setControlType NOTIFY controlTypeChanged) | ||
|
||
Q_PROPERTY(Policy horizontalPolicy READ horizontalPolicy WRITE setHorizontalPolicy NOTIFY horizontalPolicyChanged) | ||
Q_PROPERTY(Policy verticalPolicy READ verticalPolicy WRITE setVerticalPolicy NOTIFY verticalPolicyChanged) | ||
|
||
Q_PROPERTY(Qt::Orientations expandingDirections READ expandingDirections STORED false NOTIFY expandingDirectionsChanged) | ||
|
||
Q_PROPERTY(bool hasHeightForWidth READ hasHeightForWidth WRITE setHeightForWidth NOTIFY hasHeightForWidthChanged) | ||
Q_PROPERTY(bool hasWidthForHeight READ hasWidthForHeight WRITE setWidthForHeight NOTIFY hasWidthForHeightChanged) | ||
|
||
Q_PROPERTY(int horizontalStretch READ horizontalStretch WRITE setHorizontalStretch NOTIFY horizontalStretchChanged) | ||
Q_PROPERTY(int verticalStretch READ verticalStretch WRITE setVerticalStretch NOTIFY verticalStretchChanged) | ||
|
||
Q_PROPERTY(bool retainSizeWhenHidden READ retainSizeWhenHidden WRITE setRetainSizeWhenHidden NOTIFY retainSizeWhenHiddenChanged) | ||
|
||
public: | ||
enum PolicyFlag { | ||
GrowFlag = 1, | ||
ExpandFlag = 2, | ||
ShrinkFlag = 4, | ||
IgnoreFlag = 8 | ||
}; | ||
|
||
enum Policy { | ||
Fixed = 0, | ||
Minimum = GrowFlag, | ||
Maximum = ShrinkFlag, | ||
Preferred = GrowFlag | ShrinkFlag, | ||
MinimumExpanding = GrowFlag | ExpandFlag, | ||
Expanding = GrowFlag | ShrinkFlag | ExpandFlag, | ||
Ignored = ShrinkFlag | GrowFlag | IgnoreFlag | ||
}; | ||
Q_ENUM(Policy) | ||
|
||
enum ControlType { | ||
DefaultType = 0x00000001, | ||
ButtonBox = 0x00000002, | ||
CheckBox = 0x00000004, | ||
ComboBox = 0x00000008, | ||
Frame = 0x00000010, | ||
GroupBox = 0x00000020, | ||
Label = 0x00000040, | ||
Line = 0x00000080, | ||
LineEdit = 0x00000100, | ||
PushButton = 0x00000200, | ||
RadioButton = 0x00000400, | ||
Slider = 0x00000800, | ||
SpinBox = 0x00001000, | ||
TabWidget = 0x00002000, | ||
ToolButton = 0x00004000 | ||
}; | ||
Q_ENUM(ControlType) | ||
Q_DECLARE_FLAGS(ControlTypes, ControlType) | ||
Q_FLAG(ControlTypes) | ||
|
||
explicit DeclarativeSizePolicy(QWidget* extendedWidget, QObject* parent); | ||
|
||
ControlType controlType() const; | ||
Policy horizontalPolicy() const; | ||
Policy verticalPolicy() const; | ||
Qt::Orientations expandingDirections() const; | ||
bool hasHeightForWidth() const; | ||
bool hasWidthForHeight() const; | ||
int horizontalStretch() const; | ||
int verticalStretch() const; | ||
bool retainSizeWhenHidden() const; | ||
|
||
public slots: | ||
void setControlType(ControlType controlType); | ||
void setHorizontalPolicy(Policy horizontalPolicy); | ||
void setVerticalPolicy(Policy verticalPolicy); | ||
void setHeightForWidth(bool heightForWidth); | ||
void setWidthForHeight(bool widthForHeight); | ||
void setHorizontalStretch(int horizontalStretch); | ||
void setVerticalStretch(int verticalStretch); | ||
void setRetainSizeWhenHidden(bool retainSizeWhenHidden); | ||
|
||
signals: | ||
void controlTypeChanged(ControlType controlType); | ||
void horizontalPolicyChanged(Policy horizontalPolicy); | ||
void verticalPolicyChanged(Policy verticalPolicy); | ||
void expandingDirectionsChanged(Qt::Orientations expandingDirections); | ||
void hasHeightForWidthChanged(bool hasHeightForWidth); | ||
void hasWidthForHeightChanged(bool hasWidthForHeight); | ||
void horizontalStretchChanged(int horizontalStretch); | ||
void verticalStretchChanged(int verticalStretch); | ||
void retainSizeWhenHiddenChanged(bool retainSizeWhenHidden); | ||
|
||
private: | ||
QWidget* m_extendedWidget; | ||
}; | ||
|
||
#endif // DECLARATIVESIZEPOLICYEXTENSION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.