QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskInputPanelBox.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskInputPanelBox.h"
7#include "QskInputPredictionBar.h"
8#include "QskLinearBox.h"
9#include "QskTextInput.h"
10#include "QskTextLabel.h"
11#include "QskVirtualKeyboard.h"
12
13#include <qpointer.h>
14#include <qstring.h>
15
16namespace
17{
18 class TextInputProxy final : public QskTextInput
19 {
20 public:
21 TextInputProxy( QskInputPanelBox* panelBox, QQuickItem* parentItem = nullptr )
22 : QskTextInput( parentItem )
23 , m_panelBox( panelBox )
24 {
25 setObjectName( QStringLiteral( "InputBoxProxy" ) );
26
27 setFocusPolicy( Qt::NoFocus );
28 initSizePolicy( QskSizePolicy::Ignored, QskSizePolicy::Fixed );
29 }
30
31 protected:
32 QskAspect::Subcontrol substitutedSubcontrol(
33 QskAspect::Subcontrol subControl ) const override
34 {
35 if ( subControl == QskTextInput::Panel )
36 return m_panelBox->effectiveSubcontrol( QskInputPanelBox::ProxyPanel );
37
38 if ( subControl == QskTextInput::Text )
39 return m_panelBox->effectiveSubcontrol( QskInputPanelBox::ProxyText );
40
41 return subControl;
42 }
43
44 void focusInEvent( QFocusEvent* ) override
45 {
46 }
47
48 void focusOutEvent( QFocusEvent* ) override
49 {
50 }
51
52 private:
53 QskInputPanelBox* m_panelBox;
54 };
55}
56
57QSK_SUBCONTROL( QskInputPanelBox, Panel )
58QSK_SUBCONTROL( QskInputPanelBox, ProxyPanel )
59QSK_SUBCONTROL( QskInputPanelBox, ProxyText )
60
61class QskInputPanelBox::PrivateData
62{
63 public:
64 QPointer< QQuickItem > inputItem;
65
66 QskLinearBox* layout;
67 QskTextLabel* prompt;
68 TextInputProxy* inputProxy;
69 QskInputPredictionBar* predictionBar;
70 QskVirtualKeyboard* keyboard;
71
72 QskInputPanelBox::PanelHints panelHints = QskInputPanelBox::InputProxy;
73};
74
75QskInputPanelBox::QskInputPanelBox( QQuickItem* parent )
76 : Inherited( parent )
77 , m_data( new PrivateData() )
78{
79 setAutoLayoutChildren( true );
80
81 m_data->prompt = new QskTextLabel();
82 m_data->prompt->setVisible( false );
83
84 m_data->inputProxy = new TextInputProxy( this, nullptr );
85 m_data->inputProxy->setVisible(
86 m_data->panelHints & QskInputPanelBox::InputProxy );
87
88 m_data->predictionBar = new QskInputPredictionBar();
89 m_data->predictionBar->setVisible(
90 m_data->panelHints & QskInputPanelBox::Prediction );
91
92 m_data->keyboard = new QskVirtualKeyboard();
93
94 auto layout = new QskLinearBox( Qt::Vertical, this );
95 layout->setDefaultAlignment( Qt::AlignLeft | Qt::AlignHCenter );
96 layout->setSpacing( 0 );
97
98 layout->addItem( m_data->prompt );
99 layout->addItem( m_data->inputProxy );
100 layout->addStretch( 10 );
101 layout->addItem( m_data->predictionBar );
102 layout->addItem( m_data->keyboard );
103
104 m_data->layout = layout;
105
106 connect( m_data->predictionBar, &QskInputPredictionBar::predictiveTextSelected,
107 this, &QskInputPanelBox::predictiveTextSelected );
108
109 connect( m_data->keyboard, &QskVirtualKeyboard::keySelected,
110 this, &QskInputPanelBox::keySelected );
111}
112
113QskInputPanelBox::~QskInputPanelBox()
114{
115}
116
117void QskInputPanelBox::setPanelHint( PanelHint hint, bool on )
118{
119 if ( on )
120 setPanelHints( m_data->panelHints | hint );
121 else
122 setPanelHints( m_data->panelHints & ~hint );
123}
124
125void QskInputPanelBox::setPanelHints( PanelHints hints )
126{
127 if ( hints == m_data->panelHints )
128 return;
129
130 m_data->panelHints = hints;
131
132 m_data->inputProxy->setVisible( hints & QskInputPanelBox::InputProxy );
133 m_data->predictionBar->setVisible( hints & QskInputPanelBox::Prediction );
134
135 const bool showPrompt = ( hints & QskInputPanelBox::InputProxy ) &&
136 !m_data->prompt->text().isEmpty();
137
138 m_data->prompt->setVisible( showPrompt );
139
140 Q_EMIT panelHintsChanged();
141}
142
143QskInputPanelBox::PanelHints QskInputPanelBox::panelHints() const
144{
145 return m_data->panelHints;
146}
147
148void QskInputPanelBox::attachInputItem( QQuickItem* item )
149{
150 if ( item == m_data->inputItem )
151 return;
152
153 m_data->inputItem = item;
154
155 if ( item )
156 {
157 if ( m_data->panelHints & QskInputPanelBox::InputProxy )
158 {
159 m_data->inputProxy->setupFrom( item );
160 m_data->inputProxy->setEditing( true );
161 }
162 }
163}
164
165QQuickItem* QskInputPanelBox::attachedInputItem() const
166{
167 return m_data->inputItem;
168}
169
170QQuickItem* QskInputPanelBox::inputProxy() const
171{
172 if ( m_data->panelHints & QskInputPanelBox::InputProxy )
173 return m_data->inputProxy;
174
175 return nullptr;
176}
177
178QskAspect::Subcontrol QskInputPanelBox::substitutedSubcontrol(
179 QskAspect::Subcontrol subControl ) const
180{
181 if ( subControl == QskBox::Panel )
182 return QskInputPanelBox::Panel;
183
184#if 1
185 // TODO ...
186 if ( subControl == QskInputPanelBox::ProxyPanel )
187 return QskTextInput::Panel;
188
189 if ( subControl == QskInputPanelBox::ProxyText )
190 return QskTextInput::Text;
191#endif
192
193 return subControl;
194}
195
196QString QskInputPanelBox::inputPrompt() const
197{
198 return m_data->prompt->text();
199}
200
201void QskInputPanelBox::setInputPrompt( const QString& text )
202{
203 auto prompt = m_data->prompt;
204
205 if ( text != prompt->text() )
206 {
207 prompt->setText( text );
208
209 if ( m_data->panelHints & QskInputPanelBox::InputProxy )
210 prompt->setVisible( !text.isEmpty() );
211
212 Q_EMIT inputPromptChanged( text );
213 }
214}
215
216void QskInputPanelBox::setPrediction( const QStringList& prediction )
217{
218 m_data->predictionBar->setPrediction( prediction );
219}
220
221void QskInputPanelBox::setKeyboard( QskVirtualKeyboard* keyboard )
222{
223 if( m_data->keyboard )
224 {
225 m_data->keyboard->deleteLater();
226 }
227
228 m_data->keyboard = keyboard;
229 m_data->layout->addItem( m_data->keyboard );
230
231 connect( m_data->keyboard, &QskVirtualKeyboard::keySelected,
232 this, &QskInputPanelBox::keySelected );
233}
234
235void QskInputPanelBox::keyPressEvent( QKeyEvent* event )
236{
237 int keyCode = -1;
238
239 switch ( event->key() )
240 {
241 case Qt::Key_Return:
242 case Qt::Key_Escape:
243 {
244 keyCode = event->key();
245 break;
246 }
247
248 default:
249 {
250 const auto text = event->text();
251
252 if ( !text.isEmpty() )
253 keyCode = text[ 0 ].unicode();
254 else
255 keyCode = event->key();
256 }
257 }
258
259 if ( m_data->keyboard->hasKey( keyCode ) )
260 {
261 // animating the corresponding key button ???
262 Q_EMIT keySelected( keyCode );
263 }
264}
265
266#include "moc_QskInputPanelBox.cpp"
Subcontrol
For use within the rendering or lay-outing of a specific QskSkinnable.
Definition QskAspect.h:104
void initSizePolicy(QskSizePolicy::Policy, QskSizePolicy::Policy)
Layout stringing items in rows and columns.