6#include "QskInputPredictionBar.h"
7#include "QskLinearBox.h"
8#include "QskPushButton.h"
9#include "QskTextOptions.h"
11#include <qfontmetrics.h>
12#include <qstringlist.h>
23 Button( QQuickItem* parent )
27 options.setElideMode( Qt::ElideRight );
29 setTextOptions( options );
32 QSizeF contentsSizeHint(
33 Qt::SizeHint which,
const QSizeF& )
const override
35 if ( which != Qt::PreferredSize )
38 auto size = QFontMetricsF( font() ).size( Qt::TextSingleLine, text() );
49 if ( subControl == QskPushButton::Panel )
50 return QskInputPredictionBar::ButtonPanel;
52 if ( subControl == QskPushButton::Text )
53 return QskInputPredictionBar::ButtonText;
60class QskInputPredictionBar::PrivateData
64 QStringList candidates;
67 const int buttonCount = 12;
70QskInputPredictionBar::QskInputPredictionBar( QQuickItem* parent )
72 , m_data( new PrivateData )
74 setAutoLayoutChildren(
true );
75 initSizePolicy( QskSizePolicy::Expanding, QskSizePolicy::Fixed );
77 m_data->layoutBox =
new QskLinearBox( Qt::Horizontal,
this );
79 for (
int i = 0; i < m_data->buttonCount; i++ )
81 auto button =
new Button( m_data->layoutBox );
82 button->setVisible(
false );
83 button->setSizePolicy( Qt::Horizontal, QskSizePolicy::Maximum );
85 connect( button, &QskPushButton::clicked,
86 this, &QskInputPredictionBar::buttonClicked );
96QskInputPredictionBar::~QskInputPredictionBar()
103 if ( subControl == QskBox::Panel )
104 return QskInputPredictionBar::Panel;
106 return Inherited::substitutedSubcontrol( subControl );
109void QskInputPredictionBar::setPrediction(
const QStringList& candidates )
111 if ( m_data->candidates != candidates )
113 m_data->candidates = candidates;
114 setScrollOffset( 0 );
118QStringList QskInputPredictionBar::candidates()
const
120 return m_data->candidates;
123void QskInputPredictionBar::setScrollOffset(
int offset )
125 m_data->scrollOffset = offset;
127 const int candidateCount = m_data->candidates.length();
128 const int count = std::min( candidateCount, m_data->buttonCount );
129 const bool continueLeft = m_data->scrollOffset > 0;
130 const bool continueRight = ( candidateCount - m_data->scrollOffset ) > count;
132 for (
int i = 0; i < count; i++ )
134 auto button = qobject_cast< QskPushButton* >(
135 m_data->layoutBox->itemAtIndex( i ) );
137 if ( continueLeft && i == 0 )
139 button->setText( QChar( 0x2B05 ) );
141 else if ( continueRight && ( i == m_data->buttonCount - 1 ) )
143 button->setText( QChar( 0x27A1 ) );
147 const int index = i + m_data->scrollOffset;
148 button->setText( m_data->candidates[ index ] );
151 button->setVisible(
true );
154 for (
int i = count; i < m_data->buttonCount; ++i )
155 m_data->layoutBox->itemAtIndex( i )->setVisible(
false );
158void QskInputPredictionBar::buttonClicked()
160 const int index = m_data->layoutBox->indexOf(
161 qobject_cast< QQuickItem* > ( sender() ) );
163 const int offset = m_data->scrollOffset;
169 setScrollOffset( offset - 1 );
173 else if ( index == m_data->buttonCount - 1 )
175 if ( m_data->candidates.count() - offset > m_data->buttonCount )
177 setScrollOffset( offset + 1 );
182 Q_EMIT predictiveTextSelected( offset + index );
185#include "moc_QskInputPredictionBar.cpp"
Subcontrol
For use within the rendering or lay-outing of a specific QskSkinnable.
Layout stringing items in rows and columns.
QSizeF strutSizeHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a strut size hint.
QSizeF outerBoxSize(QskAspect, const QSizeF &innerBoxSize) const
Calculate the size, when being expanded by paddings, indentations.