6#include "QskSimpleListBox.h"
8#include "QskFunctions.h"
10#include <qfontmetrics.h>
12static inline qreal qskMaxWidth(
13 const QFont& font,
const QStringList& list )
15 const QFontMetricsF fm( font );
18 for (
int i = 0; i < list.size(); i++ )
20 const qreal w = qskHorizontalAdvance( fm, list[ i ] );
28class QskSimpleListBox::PrivateData
33 , columnWidthHint( 0.0 )
39 qreal columnWidthHint;
44QskSimpleListBox::QskSimpleListBox( QQuickItem* parent )
46 , m_data( new PrivateData() )
48 connect(
this, &Inherited::selectedRowChanged,
49 this, [
this ](
int row ) { Q_EMIT selectedEntryChanged( entryAt( row ) ); } );
52QskSimpleListBox::~QskSimpleListBox()
56QString QskSimpleListBox::entryAt(
int row )
const
58 if ( row >= 0 && row < m_data->entries.size() )
59 return m_data->entries[ row ];
64QVariant QskSimpleListBox::valueAt(
int row,
int col )
const
66 if ( col == 0 && row >= 0 && row < m_data->entries.size() )
67 return m_data->entries[ row ];
72void QskSimpleListBox::setColumnWidthHint(
int column, qreal width )
77 if ( width != m_data->columnWidthHint )
79 m_data->columnWidthHint = qMax( width, qreal( 0.0 ) );
81 if ( m_data->columnWidthHint > 0.0 )
82 m_data->maxTextWidth = m_data->columnWidthHint;
84 m_data->maxTextWidth = qskMaxWidth(
effectiveFont( Text ), m_data->entries );
86 updateScrollableSize();
90qreal QskSimpleListBox::columnWidthHint(
int column )
const
93 return m_data->columnWidthHint;
98void QskSimpleListBox::insert(
const QStringList& list,
int index )
100 if ( list.isEmpty() )
103 if ( m_data->columnWidthHint <= 0.0 )
106 if ( w > m_data->maxTextWidth )
107 m_data->maxTextWidth = w;
110 if ( m_data->entries.isEmpty() )
112 m_data->entries = list;
114 else if ( index < 0 || index >= m_data->entries.size() )
116 m_data->entries += list;
121 for (
int i = 0; i < list.size(); i++ )
122 m_data->entries.insert( index + i, list[ i ] );
128void QskSimpleListBox::setEntries(
const QStringList& entries )
130 if ( m_data->entries.isEmpty() && entries.isEmpty() )
133 m_data->entries.clear();
135 if ( m_data->columnWidthHint <= 0.0 )
136 m_data->maxTextWidth = 0.0;
138 insert( entries, -1 );
141QStringList QskSimpleListBox::entries()
const
143 return m_data->entries;
146void QskSimpleListBox::insert(
const QString& text,
int index )
148 if ( m_data->columnWidthHint <= 0.0 )
150 const auto w = qskHorizontalAdvance(
effectiveFont( Cell ), text );
151 if ( w > m_data->maxTextWidth )
152 m_data->maxTextWidth = w;
156 m_data->entries.append( text );
158 m_data->entries.insert( index, text );
163void QskSimpleListBox::removeAt(
int index )
165 auto& entries = m_data->entries;
167 if ( index < 0 || index >= entries.size() )
170 if ( m_data->columnWidthHint <= 0.0 )
172 const auto w = qskHorizontalAdvance(
effectiveFont( Cell ), entries[ index ] );
173 if ( w >= m_data->maxTextWidth )
174 m_data->maxTextWidth = qskMaxWidth(
effectiveFont( Text ), entries );
177 entries.removeAt( index );
181 int row = selectedRow();
184 row = qMin( row, m_data->entries.size() - 1 );
186 if ( row != selectedRow() )
187 setSelectedRow( row );
189 Q_EMIT selectedRowChanged( row );
193void QskSimpleListBox::removeBulk(
int from,
int to )
198 if ( to < 0 || to >= m_data->entries.size() - 1 )
199 to = m_data->entries.size() - 1;
204 for (
int i = to; i >= from; i-- )
205 m_data->entries.removeAt( i );
207 if ( m_data->columnWidthHint <= 0.0 )
208 m_data->maxTextWidth = qskMaxWidth(
effectiveFont( Text ), m_data->entries );
212 int row = selectedRow();
215 if ( m_data->entries.isEmpty() )
219 else if ( row < from )
223 else if ( row <= to )
226 row = qMin( from, m_data->entries.size() - 1 );
230 row -= ( to - from + 1 );
233 if ( row != selectedRow() )
235 setSelectedRow( row );
240void QskSimpleListBox::clear()
242 if ( m_data->entries.isEmpty() )
245 m_data->entries.clear();
247 if ( m_data->columnWidthHint <= 0.0 )
248 m_data->maxTextWidth = 0.0;
251 setSelectedRow( -1 );
254void QskSimpleListBox::propagateEntries()
259 updateScrollableSize();
262 Q_EMIT entriesChanged();
265int QskSimpleListBox::rowCount()
const
267 return m_data->entries.size();
270int QskSimpleListBox::columnCount()
const
275qreal QskSimpleListBox::columnWidth(
int col )
const
277 if ( col >= columnCount() )
281 return m_data->maxTextWidth + padding.left() + padding.right();
284qreal QskSimpleListBox::rowHeight()
const
289 qreal h = effectiveFontHeight( Text );
290 h += padding.top() + padding.bottom();
292 return qMax( h, hint.height() );
295#include "moc_QskSimpleListBox.cpp"
QMarginsF paddingHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a padding hint.
QFont effectiveFont(QskAspect) const
QSizeF strutSizeHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a strut size hint.