QskTextInput improved

This commit is contained in:
Uwe Rathmann 2018-04-18 10:46:11 +02:00
parent 5d9f22a757
commit 3f8616c084
6 changed files with 135 additions and 21 deletions

View File

@ -146,6 +146,11 @@ void QskInputCompositionModel::composeKey( int key )
return; return;
} }
case Qt::Key_Escape:
{
sendKeyEvents( Qt::Key_Escape );
return;
}
default: default:
{ {
if ( !spaceLeft ) if ( !spaceLeft )

View File

@ -44,6 +44,7 @@ public:
auto* textInput2 = new QskTextInput( this ); auto* textInput2 = new QskTextInput( this );
textInput2->setText( "Press and edit Me." ); textInput2->setText( "Press and edit Me." );
textInput2->setSizePolicy( Qt::Horizontal, QskSizePolicy::Preferred ); textInput2->setSizePolicy( Qt::Horizontal, QskSizePolicy::Preferred );
textInput2->setActivationModes( QskTextInput::ActivationOnAll );
auto* textInput3 = new QskTextInput( this ); auto* textInput3 = new QskTextInput( this );
textInput3->setReadOnly( true ); textInput3->setReadOnly( true );

View File

@ -202,6 +202,8 @@ void QskMaterialSkin::initTextInputHints()
const ColorPalette& pal = m_data->palette; const ColorPalette& pal = m_data->palette;
setColor( Q::Text, pal.textColor ); setColor( Q::Text, pal.textColor );
setColor( Q::PanelSelected, pal.accentColor );
setColor( Q::TextSelected, pal.contrastColor );
setMargins( Q::Panel | Padding, 5 ); setMargins( Q::Panel | Padding, 5 );
setBoxShape( Q::Panel, 4 ); setBoxShape( Q::Panel, 4 );

View File

@ -308,6 +308,8 @@ void QskSquiekSkin::initTextInputHints()
static_cast<int>( Qt::AlignLeft | Qt::AlignTop ) ); static_cast<int>( Qt::AlignLeft | Qt::AlignTop ) );
setColor( Q::Text, pal.themeForeground ); setColor( Q::Text, pal.themeForeground );
setColor( Q::PanelSelected, pal.highlighted );
setColor( Q::TextSelected, pal.highlightedText );
setMargins( Q::Panel | Padding, 5 ); setMargins( Q::Panel | Padding, 5 );
setBoxBorderMetrics( Q::Panel, 2 ); setBoxBorderMetrics( Q::Panel, 2 );

View File

@ -21,6 +21,8 @@ QSK_QT_PRIVATE_END
QSK_SUBCONTROL( QskTextInput, Panel ) QSK_SUBCONTROL( QskTextInput, Panel )
QSK_SUBCONTROL( QskTextInput, Text ) QSK_SUBCONTROL( QskTextInput, Text )
QSK_SUBCONTROL( QskTextInput, PanelSelected )
QSK_SUBCONTROL( QskTextInput, TextSelected )
QSK_STATE( QskTextInput, ReadOnly, QskAspect::FirstSystemState << 1 ) QSK_STATE( QskTextInput, ReadOnly, QskAspect::FirstSystemState << 1 )
QSK_STATE( QskTextInput, Editing, QskAspect::FirstSystemState << 2 ) QSK_STATE( QskTextInput, Editing, QskAspect::FirstSystemState << 2 )
@ -70,6 +72,8 @@ namespace
{ {
class TextInput final : public QQuickTextInput class TextInput final : public QQuickTextInput
{ {
using Inherited = QQuickTextInput;
public: public:
TextInput( QskTextInput* ); TextInput( QskTextInput* );
@ -94,13 +98,19 @@ namespace
return isAcceptable; return isAcceptable;
} }
inline bool handleEvent( QEvent* event ) { return this->event( event ); } void updateColors();
void updateMetrics();
inline bool handleEvent( QEvent* event )
{
return this->event( event );
}
protected: protected:
virtual void geometryChanged( virtual void geometryChanged(
const QRectF& newGeometry, const QRectF& oldGeometry ) override const QRectF& newGeometry, const QRectF& oldGeometry ) override
{ {
QQuickTextInput::geometryChanged( newGeometry, oldGeometry ); Inherited::geometryChanged( newGeometry, oldGeometry );
updateClip(); updateClip();
} }
@ -109,6 +119,13 @@ namespace
setClip( ( contentWidth() > width() ) || setClip( ( contentWidth() > width() ) ||
( contentHeight() > height() ) ); ( contentHeight() > height() ) );
} }
virtual QSGNode* updatePaintNode(
QSGNode *oldNode, UpdatePaintNodeData* data ) override
{
updateColors();
return Inherited::updatePaintNode( oldNode, data );
}
}; };
TextInput::TextInput( QskTextInput* textInput ): TextInput::TextInput( QskTextInput* textInput ):
@ -134,6 +151,7 @@ namespace
return; return;
setCursorVisible( on ); setCursorVisible( on );
d->setBlinkingCursorEnabled( on );
if ( !on ) if ( !on )
{ {
@ -151,6 +169,55 @@ namespace
polish(); polish();
update(); update();
} }
void TextInput::updateMetrics()
{
auto input = static_cast< const QskTextInput* >( parentItem() );
setAlignment( input->alignment() );
setFont( input->font() );
}
void TextInput::updateColors()
{
auto input = static_cast< const QskTextInput* >( parentItem() );
auto d = QQuickTextInputPrivate::get( this );
bool isDirty = false;
QColor color;
color = input->color( QskTextInput::Text );
if ( d->color != color )
{
d->color = color;
isDirty = true;
}
if ( d->hasSelectedText() )
{
color = input->color( QskTextInput::PanelSelected );
if ( d->selectionColor != color )
{
d->selectionColor = color;
isDirty = true;
}
color = input->color( QskTextInput::TextSelected );
if ( d->selectedTextColor != color )
{
d->selectedTextColor = color;
isDirty = true;
}
}
if ( isDirty )
{
d->textLayoutDirty = true;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
}
}
} }
class QskTextInput::PrivateData class QskTextInput::PrivateData
@ -216,21 +283,33 @@ void QskTextInput::keyPressEvent( QKeyEvent* event )
{ {
if ( isEditing() ) if ( isEditing() )
{ {
if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return ) switch( event->key() )
{
case Qt::Key_Enter:
case Qt::Key_Return:
{ {
if ( m_data->textInput->fixup() ) if ( m_data->textInput->fixup() )
{ {
auto inputMethod = QGuiApplication::inputMethod(); QGuiApplication::inputMethod()->commit();
inputMethod->commit();
if ( !( inputMethodHints() & Qt::ImhMultiLine) ) if ( !( inputMethodHints() & Qt::ImhMultiLine) )
setEditing( false ); setEditing( false );
} }
break;
} }
else #if 1
case Qt::Key_Escape:
{
QGuiApplication::inputMethod()->hide();
setEditing( false );
break;
}
#endif
default:
{ {
m_data->textInput->handleEvent( event ); m_data->textInput->handleEvent( event );
} }
}
return; return;
} }
@ -285,6 +364,19 @@ void QskTextInput::inputMethodEvent( QInputMethodEvent* event )
void QskTextInput::focusInEvent( QFocusEvent* event ) void QskTextInput::focusInEvent( QFocusEvent* event )
{ {
if ( m_data->activationModes & ActivationOnFocus )
{
switch( event->reason() )
{
case Qt::ActiveWindowFocusReason:
case Qt::PopupFocusReason:
break;
default:
setEditing( true );
}
}
Inherited::focusInEvent( event ); Inherited::focusInEvent( event );
} }
@ -315,8 +407,12 @@ QSizeF QskTextInput::contentsSizeHint() const
{ {
using namespace QskAspect; using namespace QskAspect;
const qreal w = m_data->textInput->implicitWidth(); auto input = m_data->textInput;
const qreal h = m_data->textInput->implicitHeight();
input->updateMetrics();
const qreal w = input->implicitWidth();
const qreal h = input->implicitHeight();
const QSizeF minSize( metric( Panel | MinimumWidth ), const QSizeF minSize( metric( Panel | MinimumWidth ),
metric( Panel | MinimumHeight ) ); metric( Panel | MinimumHeight ) );
@ -328,12 +424,16 @@ void QskTextInput::updateLayout()
{ {
auto input = m_data->textInput; auto input = m_data->textInput;
input->setAlignment( alignment() ); input->updateMetrics();
input->setFont( font() );
qskSetItemGeometry( input, subControlRect( Text ) ); qskSetItemGeometry( input, subControlRect( Text ) );
} }
void QskTextInput::updateNode( QSGNode* node )
{
m_data->textInput->updateColors();
Inherited::updateNode( node );
}
QString QskTextInput::text() const QString QskTextInput::text() const
{ {
return m_data->textInput->text(); return m_data->textInput->text();

View File

@ -31,7 +31,7 @@ class QSK_EXPORT QskTextInput : public QskControl
using Inherited = QskControl; using Inherited = QskControl;
public: public:
QSK_SUBCONTROLS( Panel, Text ) QSK_SUBCONTROLS( Panel, Text, PanelSelected, TextSelected )
QSK_STATES( ReadOnly, Editing ) QSK_STATES( ReadOnly, Editing )
enum ActivationMode enum ActivationMode
@ -40,7 +40,10 @@ public:
ActivationOnFocus = 1 << 0 , ActivationOnFocus = 1 << 0 ,
ActivationOnMouse = 1 << 1, ActivationOnMouse = 1 << 1,
ActivationOnKey = 1 << 2 ActivationOnKey = 1 << 2,
ActivationOnInput = ActivationOnMouse | ActivationOnKey,
ActivationOnAll = ActivationOnFocus | ActivationOnMouse | ActivationOnKey
}; };
Q_ENUM( ActivationMode ) Q_ENUM( ActivationMode )
@ -156,6 +159,7 @@ protected:
virtual void keyReleaseEvent( QKeyEvent* ) override; virtual void keyReleaseEvent( QKeyEvent* ) override;
virtual void updateLayout() override; virtual void updateLayout() override;
virtual void updateNode( QSGNode*) override;
private: private:
class PrivateData; class PrivateData;