2023-02-14 07:58:37 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2023 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "QskComboBoxSkinlet.h"
|
|
|
|
#include "QskComboBox.h"
|
|
|
|
|
|
|
|
#include "QskGraphic.h"
|
|
|
|
#include "QskSkin.h"
|
|
|
|
#include "QskSGNode.h"
|
|
|
|
#include "QskStandardSymbol.h"
|
|
|
|
#include "QskSubcontrolLayoutEngine.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
#if 1 // unify with the implementation from QskMenu
|
|
|
|
template< class T >
|
|
|
|
static inline QVariant qskSampleAt( const QskComboBox* box )
|
|
|
|
{
|
|
|
|
if( std::is_same< T, QString >() )
|
|
|
|
{
|
|
|
|
return box->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
const int index = box->currentIndex();
|
|
|
|
|
|
|
|
if( index < 0 )
|
|
|
|
return QVariant::fromValue( T() );
|
|
|
|
|
|
|
|
const auto list = box->optionAt( index );
|
|
|
|
for ( const auto& value : list )
|
|
|
|
{
|
|
|
|
if ( value.canConvert< T >() )
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
template< class T >
|
|
|
|
static inline T qskValueAt( const QskComboBox* box )
|
|
|
|
{
|
|
|
|
const auto sample = qskSampleAt< T >( box );
|
|
|
|
return sample.template value< T >();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class LayoutEngine : public QskSubcontrolLayoutEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LayoutEngine( const QskComboBox* box )
|
|
|
|
: QskSubcontrolLayoutEngine( Qt::Horizontal )
|
|
|
|
{
|
|
|
|
setSpacing( box->spacingHint( QskComboBox::Panel ) );
|
|
|
|
|
|
|
|
setGraphicTextElements( box,
|
|
|
|
QskComboBox::Text, qskValueAt< QString >( box ),
|
|
|
|
QskComboBox::Graphic, qskValueAt< QskGraphic >( box ).defaultSize() );
|
|
|
|
|
|
|
|
const auto alignment = box->alignmentHint( QskComboBox::Panel, Qt::AlignLeft );
|
|
|
|
setFixedContent( QskComboBox::Text, Qt::Horizontal, alignment );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
QskComboBoxSkinlet::QskComboBoxSkinlet( QskSkin* skin )
|
|
|
|
: Inherited( skin )
|
|
|
|
{
|
2023-03-01 09:51:46 +00:00
|
|
|
setNodeRoles( { PanelRole, SplashRole, GraphicRole, TextRole, OpenMenuGraphicRole } );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QskComboBoxSkinlet::~QskComboBoxSkinlet() = default;
|
|
|
|
|
|
|
|
QRectF QskComboBoxSkinlet::subControlRect( const QskSkinnable* skinnable,
|
|
|
|
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
|
|
|
|
{
|
|
|
|
using Q = QskComboBox;
|
|
|
|
|
|
|
|
const auto* box = static_cast< const QskComboBox* >( skinnable );
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
if ( subControl == Q::Panel )
|
|
|
|
return contentsRect;
|
|
|
|
|
|
|
|
if ( subControl == Q::Splash )
|
|
|
|
return splashRect( box, contentsRect );
|
|
|
|
|
2023-02-14 07:58:37 +00:00
|
|
|
if ( subControl == Q::Text || subControl == Q::Graphic )
|
|
|
|
{
|
|
|
|
const auto r = box->subControlContentsRect( contentsRect, Q::Panel );
|
|
|
|
|
|
|
|
LayoutEngine layoutEngine( box );
|
|
|
|
layoutEngine.setGeometries( r );
|
|
|
|
|
|
|
|
return layoutEngine.subControlRect( subControl );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( subControl == Q::OpenMenuGraphic )
|
|
|
|
{
|
|
|
|
auto rect = box->innerBox( Q::Panel, contentsRect );
|
|
|
|
const auto size = box->strutSizeHint( Q::OpenMenuGraphic );
|
|
|
|
rect.setLeft( rect.right() - size.width() );
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Inherited::subControlRect( skinnable, contentsRect, subControl );
|
|
|
|
}
|
|
|
|
|
|
|
|
QSGNode* QskComboBoxSkinlet::updateSubNode(
|
|
|
|
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
|
|
|
|
{
|
|
|
|
using Q = QskComboBox;
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
const auto box = static_cast< const QskComboBox* >( skinnable );
|
2023-02-14 07:58:37 +00:00
|
|
|
|
|
|
|
switch ( nodeRole )
|
|
|
|
{
|
|
|
|
case PanelRole:
|
|
|
|
{
|
2023-03-01 09:51:46 +00:00
|
|
|
return updateBoxNode( box, node, Q::Panel );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case GraphicRole:
|
|
|
|
{
|
2023-03-01 09:51:46 +00:00
|
|
|
return updateGraphicNode( box, node, box->graphic(), Q::Graphic );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case TextRole:
|
|
|
|
{
|
2023-03-01 09:51:46 +00:00
|
|
|
return updateTextNode( box, node );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case OpenMenuGraphicRole:
|
|
|
|
{
|
2023-03-01 09:51:46 +00:00
|
|
|
const auto symbol = box->isPopupOpen()
|
|
|
|
? QskStandardSymbol::ComboBoxSymbolPopupOpen
|
|
|
|
: QskStandardSymbol::ComboBoxSymbolPopupClosed;
|
|
|
|
|
|
|
|
const auto graphic = box->effectiveSkin()->symbol( symbol );
|
|
|
|
return updateGraphicNode( box, node, graphic, Q::OpenMenuGraphic );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Inherited::updateSubNode( skinnable, nodeRole, node );
|
|
|
|
}
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
QRectF QskComboBoxSkinlet::splashRect(
|
|
|
|
const QskComboBox* box, const QRectF& contentsRect ) const
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
|
|
|
using Q = QskComboBox;
|
|
|
|
|
|
|
|
QRectF rect;
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
const auto ratio = box->metric( Q::Splash | QskAspect::Size );
|
2023-02-14 07:58:37 +00:00
|
|
|
if ( ratio > 0.0 )
|
|
|
|
{
|
2023-03-01 09:51:46 +00:00
|
|
|
rect = subControlRect( box, contentsRect, Q::Panel );
|
2023-02-14 07:58:37 +00:00
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
const auto pos = box->positionHint( Q::Splash );
|
|
|
|
const qreal w = 2.0 * rect.width() * ratio;
|
2023-02-14 07:58:37 +00:00
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
rect.setX( pos - 0.5 * w );
|
|
|
|
rect.setWidth( w );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSGNode* QskComboBoxSkinlet::updateTextNode(
|
|
|
|
const QskComboBox* box, QSGNode* node ) const
|
|
|
|
{
|
|
|
|
using Q = QskComboBox;
|
|
|
|
|
|
|
|
const auto rect = box->subControlRect( Q::Text ).toAlignedRect();
|
|
|
|
|
|
|
|
const auto textHeight = box->effectiveFontHeight( Q::Text );
|
|
|
|
if ( !box->clip() && ( rect.height() < textHeight ) )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto alignment = box->alignmentHint( Q::Text, Qt::AlignLeft | Qt::AlignVCenter );
|
|
|
|
|
|
|
|
return QskSkinlet::updateTextNode( box, node, rect,
|
|
|
|
alignment, box->text(), Q::Text );
|
|
|
|
}
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
QSGNode* QskComboBoxSkinlet::updateSplashNode(
|
|
|
|
const QskComboBox* box, QSGNode* node ) const
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
|
|
|
using Q = QskComboBox;
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
const auto splashRect = box->subControlRect( Q::Splash );
|
|
|
|
if ( splashRect.isEmpty() )
|
2023-02-14 07:58:37 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
auto clipNode = updateBoxClipNode( box, node,
|
|
|
|
box->subControlRect( Q::Panel ), Q::Panel );
|
2023-02-14 07:58:37 +00:00
|
|
|
|
|
|
|
if ( clipNode )
|
|
|
|
{
|
2023-03-01 09:51:46 +00:00
|
|
|
auto boxNode = QskSGNode::findChildNode( clipNode, SplashRole );
|
|
|
|
boxNode = updateBoxNode( box, boxNode, splashRect, Q::Splash );
|
2023-02-14 07:58:37 +00:00
|
|
|
|
|
|
|
if ( boxNode == nullptr )
|
|
|
|
return nullptr;
|
|
|
|
|
2023-03-01 09:51:46 +00:00
|
|
|
QskSGNode::setNodeRole( boxNode, SplashRole );
|
2023-02-14 07:58:37 +00:00
|
|
|
if ( boxNode->parent() != clipNode )
|
|
|
|
clipNode->appendChildNode( boxNode );
|
|
|
|
}
|
|
|
|
|
|
|
|
return clipNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF QskComboBoxSkinlet::sizeHint( const QskSkinnable* skinnable,
|
|
|
|
Qt::SizeHint which, const QSizeF& ) const
|
|
|
|
{
|
|
|
|
using Q = QskComboBox;
|
|
|
|
|
|
|
|
if ( which != Qt::PreferredSize )
|
|
|
|
return QSizeF();
|
|
|
|
|
|
|
|
const auto box = static_cast< const QskComboBox* >( skinnable );
|
|
|
|
|
|
|
|
LayoutEngine layoutEngine( box );
|
|
|
|
auto size = layoutEngine.sizeHint( which, QSizeF() );
|
|
|
|
|
|
|
|
const auto spacingHint = box->spacingHint( Q::Panel );
|
|
|
|
const auto menuGraphicHint = box->strutSizeHint( Q::OpenMenuGraphic );
|
|
|
|
|
|
|
|
size.rwidth() += spacingHint + menuGraphicHint.width();
|
|
|
|
|
|
|
|
size = box->outerBoxSize( Q::Panel, size );
|
|
|
|
size = size.expandedTo( box->strutSizeHint( Q::Panel ) );
|
|
|
|
size = size.grownBy( skinnable->marginHint( Q::Panel ) );
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskComboBoxSkinlet.cpp"
|