QskComboBoxSkinlet::sizeHint fixed

This commit is contained in:
Uwe Rathmann 2024-12-17 12:03:33 +01:00
parent a6992c2f08
commit 9e1e8931bf

View File

@ -8,16 +8,44 @@
#include "QskGraphic.h" #include "QskGraphic.h"
#include "QskLabelData.h" #include "QskLabelData.h"
#include "QskTextOptions.h"
#include "QskSGNode.h" #include "QskSGNode.h"
#include "QskSubcontrolLayoutEngine.h" #include "QskSubcontrolLayoutEngine.h"
#include "QskTextRenderer.h"
#include <qfontmetrics.h>
static int qskWidestIndex( const QskComboBox* box )
{
const auto font = box->effectiveFont( QskComboBox::Text );
int index = -1;
qreal w = 0.0;
for ( int i = 0; i < box->count(); i++ )
{
const auto option = box->optionAt( i );
const auto size = QskTextRenderer::textSize(
option.text(), font, QskTextOptions() );
if ( size.width() > w )
{
w = size.width();
index = i;
}
}
return index;
}
namespace namespace
{ {
class LayoutEngine : public QskSubcontrolLayoutEngine class LayoutEngine : public QskSubcontrolLayoutEngine
{ {
public: public:
LayoutEngine( const QskComboBox* box ) LayoutEngine( const QskComboBox* box, int index = -1 )
: QskSubcontrolLayoutEngine( Qt::Horizontal ) : QskSubcontrolLayoutEngine( Qt::Horizontal )
{ {
setSpacing( box->spacingHint( QskComboBox::Panel ) ); setSpacing( box->spacingHint( QskComboBox::Panel ) );
@ -25,16 +53,26 @@ namespace
QSizeF graphicSize; QSizeF graphicSize;
QString text; QString text;
if ( box->currentIndex() >= 0 ) if ( index < 0 )
{ {
const auto option = box->optionAt( box->currentIndex() ); if ( box->currentIndex() >= 0 )
{
const auto option = box->optionAt( box->currentIndex() );
graphicSize = option.icon().graphic().defaultSize(); graphicSize = option.icon().graphic().defaultSize();
text = option.text(); text = option.text();
}
else
{
text = box->placeholderText();
}
} }
else else
{ {
text = box->placeholderText(); const auto option = box->optionAt( index );
graphicSize = option.icon().graphic().defaultSize();
text = option.text();
} }
setGraphicTextElements( box, setGraphicTextElements( box,
@ -143,7 +181,7 @@ QSizeF QskComboBoxSkinlet::sizeHint( const QskSkinnable* skinnable,
const auto box = static_cast< const QskComboBox* >( skinnable ); const auto box = static_cast< const QskComboBox* >( skinnable );
LayoutEngine layoutEngine( box ); LayoutEngine layoutEngine( box, qskWidestIndex( box ) );
auto size = layoutEngine.sizeHint( which, QSizeF() ); auto size = layoutEngine.sizeHint( which, QSizeF() );
const auto spacingHint = box->spacingHint( Q::Panel ); const auto spacingHint = box->spacingHint( Q::Panel );