From aa5f1054b23d0f575f86eac52ef375bbb5f85585 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Fri, 28 Jul 2017 16:16:13 +0200 Subject: [PATCH] inputpanel example extended, but still some way to go until it works --- playground/inputpanel/main.cpp | 119 +++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 4 deletions(-) diff --git a/playground/inputpanel/main.cpp b/playground/inputpanel/main.cpp index 2d3ecc32..920934e3 100644 --- a/playground/inputpanel/main.cpp +++ b/playground/inputpanel/main.cpp @@ -12,13 +12,16 @@ #include #include #include +#include #include #include +#include #include #include +#include #define STRINGIFY(x) #x #define STRING(x) STRINGIFY(x) @@ -28,10 +31,9 @@ class InputBox : public QskLinearBox { public: - InputBox( QQuickItem* parentItem = nullptr ) : + InputBox( QQuickItem* parentItem = nullptr ): QskLinearBox( Qt::Vertical, parentItem ) { - setBackgroundColor( "PapayaWhip" ); setDefaultAlignment( Qt::AlignHCenter | Qt::AlignTop ); setMargins( 10 ); @@ -56,6 +58,107 @@ public: } }; +class LocaleListView : public QskListView +{ +public: + LocaleListView( QQuickItem* parentItem = nullptr ): + QskListView( parentItem ), + m_maxWidth( 0.0 ) + { + append( QLocale::Bulgarian, QStringLiteral( "български език" ) ); + append( QLocale::Czech, QStringLiteral( "Čeština" ) ); + append( QLocale::German, QStringLiteral( "Deutsch" ) ); + append( QLocale::Danish, QStringLiteral( "Dansk" ) ); + + append( QLocale( QLocale::English, QLocale::UnitedStates ), QStringLiteral( "English (US)" ) ); + append( QLocale( QLocale::English, QLocale::UnitedKingdom ), QStringLiteral( "English (UK)" ) ); + + append( QLocale::Spanish, QStringLiteral( "Español" ) ); + append( QLocale::Finnish, QStringLiteral( "Suomi" ) ); + append( QLocale::French, QStringLiteral( "Français" ) ); + append( QLocale::Hungarian, QStringLiteral( "Magyar" ) ); + append( QLocale::Italian, QStringLiteral( "Italiano" ) ); + append( QLocale::Japanese, QStringLiteral( "日本語" ) ); + append( QLocale::Latvian, QStringLiteral( "Latviešu" ) ); + append( QLocale::Lithuanian, QStringLiteral( "Lietuvių") ); + append( QLocale::Dutch, QStringLiteral( "Nederlands" ) ); + append( QLocale::Portuguese, QStringLiteral( "Português" ) ); + append( QLocale::Romanian, QStringLiteral( "Română" ) ); + append( QLocale::Russian, QStringLiteral( "Русский" ) ); + append( QLocale::Slovenian, QStringLiteral( "Slovenščina" ) ); + append( QLocale::Slovak, QStringLiteral( "Slovenčina" ) ); + append( QLocale::Turkish, QStringLiteral( "Türkçe" ) ); + append( QLocale::Chinese, QStringLiteral( "中文" ) ); + + setSizePolicy( Qt::Horizontal, QskSizePolicy::Fixed ); + setPreferredWidth( columnWidth( 0 ) + 20 ); + + setScrollableSize( QSizeF( columnWidth( 0 ), rowCount() * rowHeight() ) ); + + // TODO: + // 1) changing the keyboard layouts does not yet work + // 2) QskInputPanel does not work properly in the threaded environment +#if 1 + connect( this, &QskListView::selectedRowChanged, + this, [ = ] { qskSetup->inputPanel()->setLocale( m_values[selectedRow()].second ); } ); +#else + connect( this, &QskListView::selectedRowChanged, + this, [ = ] { QLocale::setDefault( m_values[selectedRow()].second ); } ); +#endif + } + + virtual int rowCount() const override final + { + return m_values.count(); + } + + virtual int columnCount() const override final + { + return 1; + } + + virtual qreal columnWidth( int ) const override + { + if ( m_maxWidth == 0.0 ) + { + using namespace QskAspect; + + QFontMetricsF fm( effectiveFont( Text ) ); + + for ( auto entry : m_values ) + m_maxWidth = qMax( m_maxWidth, fm.width( entry.first ) ); + + m_maxWidth += metric( Cell | Padding | LeftEdge ) + + metric( Cell | Padding | RightEdge ); + } + + return m_maxWidth; + } + + virtual qreal rowHeight() const override + { + using namespace QskAspect; + + return QFontMetrics( effectiveFont( Text ) ).height() + + metric( Cell | Padding | TopEdge ) + + metric( Cell | Padding | BottomEdge ); + } + + virtual QVariant valueAt( int row, int ) const override final + { + return m_values[row].first; + } + +private: + inline void append( QLocale locale, const QString& name ) + { + m_values += qMakePair( QString( name ), locale ); + } + + QVector< QPair< QString, QLocale > > m_values; + mutable qreal m_maxWidth; +}; + int main( int argc, char* argv[] ) { #ifdef ITEM_STATISTICS @@ -76,11 +179,19 @@ int main( int argc, char* argv[] ) qskDialog->setPolicy( QskDialog::EmbeddedBox ); #endif + auto box = new QskLinearBox( Qt::Horizontal ); + box->setSpacing( 10 ); + box->setMargins( 20 ); + + (void) new LocaleListView( box ); + (void) new InputBox( box ); + QskWindow window; - window.addItem( new InputBox() ); + window.setColor( "PapayaWhip" ); + window.addItem( box ); window.addItem( new QskFocusIndicator() ); - window.resize( 800, 400 ); + window.resize( 800, 300 ); window.show(); return app.exec();