diff --git a/examples/gallery/selector/SelectorPage.cpp b/examples/gallery/selector/SelectorPage.cpp index 054577d4..81e90e85 100644 --- a/examples/gallery/selector/SelectorPage.cpp +++ b/examples/gallery/selector/SelectorPage.cpp @@ -31,7 +31,7 @@ namespace auto bar = new QskSegmentedBar( orientation, this ); for ( const auto text: texts ) - bar->addText( text ); + bar->addOption( {}, text ); } { @@ -45,7 +45,7 @@ namespace auto bar = new QskSegmentedBar( orientation, this ); for ( uint i = 0; i < sizeof( icons ) / sizeof( icons[ 0 ] ); ++i ) - bar->addGraphicAndText( QUrl( QString( icons[ i ] ) ), texts[ i ] ); + bar->addOption( QUrl( QString( icons[ i ] ) ), texts[ i ] ); } setExtraSpacingAt( Qt::LeftEdge | Qt::BottomEdge ); diff --git a/src/controls/QskSegmentedBar.cpp b/src/controls/QskSegmentedBar.cpp index a01314a9..c31fe12e 100644 --- a/src/controls/QskSegmentedBar.cpp +++ b/src/controls/QskSegmentedBar.cpp @@ -137,35 +137,13 @@ QskTextOptions QskSegmentedBar::textOptions() const return textOptionsHint( Text ); } -int QskSegmentedBar::addText( const QString& text ) -{ - m_data->addOption( this, Option( QUrl(), text ) ); - return count() - 1; -} - -int QskSegmentedBar::addGraphic( const QUrl& graphicSource ) -{ - m_data->addOption( this, Option( graphicSource, QString() ) ); - return count() - 1; -} - -int QskSegmentedBar::addGraphicAndText( const QUrl& graphicSource, const QString& text ) +int QskSegmentedBar::addOption( const QUrl& graphicSource, const QString& text ) { m_data->addOption( this, Option( graphicSource, text ) ); return count() - 1; } -QString QskSegmentedBar::textAt( int index ) const -{ - return m_data->options.at( index ).text; -} - -QskGraphic QskSegmentedBar::graphicAt( int index ) const -{ - return m_data->options.at( index ).graphic; -} - -QVariant QskSegmentedBar::optionAt( int index ) const +QVariantList QskSegmentedBar::optionAt( int index ) const { const auto& options = m_data->options; @@ -174,14 +152,11 @@ QVariant QskSegmentedBar::optionAt( int index ) const const auto& option = options[ index ]; - QVariant value; + QVariantList list; + list += QVariant::fromValue( option.graphic ); + list += QVariant::fromValue( option.text ); - if ( option.graphicSource.isValid() ) - value = QVariant::fromValue( option.graphic ); - else - value = QVariant::fromValue( option.text ); - - return value; + return list; } QskAspect::Placement QskSegmentedBar::effectivePlacement() const diff --git a/src/controls/QskSegmentedBar.h b/src/controls/QskSegmentedBar.h index d7fb6b23..c726ba25 100644 --- a/src/controls/QskSegmentedBar.h +++ b/src/controls/QskSegmentedBar.h @@ -46,12 +46,7 @@ class QSK_EXPORT QskSegmentedBar : public QskControl void setTextOptions( const QskTextOptions& ); QskTextOptions textOptions() const; - int addText( const QString& ); - int addGraphic( const QUrl& ); - int addGraphicAndText( const QUrl&, const QString& ); - - QString textAt( int ) const; - QskGraphic graphicAt( int ) const; + int addOption( const QUrl&, const QString& ); void clear(); @@ -60,7 +55,7 @@ class QSK_EXPORT QskSegmentedBar : public QskControl int count() const; - QVariant optionAt( int ) const; + QVariantList optionAt( int ) const; void setSegmentEnabled( int, bool ); bool isSegmentEnabled( int ) const; diff --git a/src/controls/QskSegmentedBarSkinlet.cpp b/src/controls/QskSegmentedBarSkinlet.cpp index a79442b0..91dab9c1 100644 --- a/src/controls/QskSegmentedBarSkinlet.cpp +++ b/src/controls/QskSegmentedBarSkinlet.cpp @@ -18,6 +18,28 @@ namespace { +#if 1 // unify with the implementation from QskMenu + template< class T > + static inline QVariant qskSampleAt( const QskSegmentedBar* bar, int index ) + { + const auto list = bar->optionAt( index ); + for ( const auto& value : list ) + { + if ( value.canConvert< T >() ) + return value; + } + + return QVariant(); + } + + template< class T > + static inline T qskValueAt( const QskSegmentedBar* bar, int index ) + { + const auto sample = qskSampleAt< T >( bar, index ); + return sample.template value< T >(); + } +#endif + QskGraphic graphicAt( const QskSegmentedBar* bar, const int index ) { // note: It is a Material 3 peculiarity that the selected element @@ -26,7 +48,7 @@ namespace // subclass. const auto graphic = ( bar->selectedIndex() == index ) ? bar->effectiveSkin()->symbol( QskStandardSymbol::SegmentedBarCheckMark ) - : bar->graphicAt( index ); + : qskValueAt< QskGraphic >( bar, index ); return graphic; } @@ -40,7 +62,7 @@ namespace setSpacing( bar->spacingHint( QskSegmentedBar::Panel ) ); setGraphicTextElements( bar, - QskSegmentedBar::Text, bar->textAt( index ), + QskSegmentedBar::Text, qskValueAt< QString >( bar, index ), QskSegmentedBar::Graphic, graphicAt( bar, index ).defaultSize() ); if( bar->orientation() == Qt::Horizontal ) @@ -211,7 +233,7 @@ QSizeF QskSegmentedBarSkinlet::segmentSizeHint( const QskSegmentedBar* bar, Qt:: // We want to know how big the element can grow when it is selected, // i.e. when it has the checkmark symbol: layoutEngine.setGraphicTextElements( bar, - QskSegmentedBar::Text, bar->textAt( i ), + QskSegmentedBar::Text, qskValueAt< QString >( bar, i ), QskSegmentedBar::Graphic, graphic.defaultSize() ); const auto size = layoutEngine.sizeHint( which, QSizeF() ); @@ -345,7 +367,7 @@ QSGNode* QskSegmentedBarSkinlet::updateSampleNode( const QskSkinnable* skinnable if ( subControl == Q::Text ) { - const auto text = bar->textAt( index ); + const auto text = qskValueAt< QString >( bar, index ); if( !text.isEmpty() ) {