213 lines
6.0 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include <SkinnyFont.h>
#include <SkinnyShortcut.h>
#include <QskInputPanel.h>
2017-07-28 14:47:11 +02:00
#include <QskDialog.h>
#include <QskFocusIndicator.h>
#include <QskLinearBox.h>
#include <QskListView.h>
#include <QskTextInput.h>
2018-04-06 17:30:24 +02:00
#include <QskInputPanel.h>
2017-07-28 14:47:11 +02:00
#include <QskWindow.h>
#include <QskSetup.h>
#include <QskAspect.h>
2017-07-28 14:47:11 +02:00
2017-07-21 18:21:34 +02:00
#include <QskObjectCounter.h>
#include <QGuiApplication>
#include <QFontMetricsF>
2017-07-21 18:21:34 +02:00
#define STRINGIFY(x) #x
#define STRING(x) STRINGIFY(x)
2017-07-28 14:47:11 +02:00
class InputBox : public QskLinearBox
{
public:
InputBox( QQuickItem* parentItem = nullptr ):
2017-07-28 14:47:11 +02:00
QskLinearBox( Qt::Vertical, parentItem )
{
2018-04-11 17:33:43 +02:00
setExtraSpacingAt( Qt::BottomEdge | Qt::RightEdge );
2017-07-28 14:47:11 +02:00
setMargins( 10 );
setSpacing( 10 );
2018-04-11 17:33:43 +02:00
auto* textInput1 = new QskTextInput( this );
2018-04-13 16:24:08 +02:00
textInput1->setText( "Press and edit Me." );
2018-04-11 17:33:43 +02:00
textInput1->setSizePolicy( Qt::Horizontal, QskSizePolicy::Preferred );
auto* textInput2 = new QskTextInput( this );
2018-04-13 16:24:08 +02:00
textInput2->setText( "Press and edit Me." );
2018-04-11 17:33:43 +02:00
textInput2->setSizePolicy( Qt::Horizontal, QskSizePolicy::Preferred );
2018-04-18 10:46:11 +02:00
textInput2->setActivationModes( QskTextInput::ActivationOnAll );
2018-04-13 16:24:08 +02:00
auto* textInput3 = new QskTextInput( this );
textInput3->setReadOnly( true );
textInput3->setText( "Read Only information." );
textInput3->setSizePolicy( Qt::Horizontal, QskSizePolicy::Preferred );
auto* textInput4 = new QskTextInput( this );
textInput4->setEchoMode( QskTextInput::PasswordEchoOnEdit );
textInput4->setMaxLength( 8 );
textInput4->setText( "12345678" );
textInput4->setSizePolicy( Qt::Horizontal, QskSizePolicy::Preferred );
2017-07-28 14:47:11 +02:00
}
};
class LocaleListView : public QskListView
{
public:
LocaleListView( QQuickItem* parentItem = nullptr ):
QskListView( parentItem ),
m_maxWidth( 0.0 )
{
2018-04-06 17:30:24 +02:00
for ( auto language :
{
QLocale::Bulgarian, QLocale::Czech, QLocale::German,
QLocale::Danish, QLocale::English, QLocale::Spanish,
QLocale::Finnish, QLocale::French, QLocale::Hungarian,
QLocale::Italian, QLocale::Japanese, QLocale::Latvian,
QLocale::Lithuanian, QLocale::Dutch, QLocale::Portuguese,
QLocale::Romanian, QLocale::Russian, QLocale::Slovenian,
QLocale::Slovak, QLocale::Turkish, QLocale::Chinese
} )
{
if ( language == QLocale::English )
{
append( QLocale( QLocale::English, QLocale::UnitedStates ) );
append( QLocale( QLocale::English, QLocale::UnitedKingdom ) );
}
else
{
append( QLocale( language ) );
}
}
setSizePolicy( Qt::Horizontal, QskSizePolicy::Fixed );
setPreferredWidth( columnWidth( 0 ) + 20 );
setScrollableSize( QSizeF( columnWidth( 0 ), rowCount() * rowHeight() ) );
}
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;
const QFontMetricsF fm( effectiveFont( Text ) );
2017-10-30 12:06:19 +01:00
for ( const auto& entry : m_values )
m_maxWidth = qMax( m_maxWidth, fm.width( entry.first ) );
const QMarginsF padding = marginsHint( Cell | Padding );
m_maxWidth += padding.left() + padding.right();
}
return m_maxWidth;
}
virtual qreal rowHeight() const override
{
using namespace QskAspect;
const QFontMetricsF fm( effectiveFont( Text ) );
const QMarginsF padding = marginsHint( Cell | Padding );
return fm.height() + padding.top() + padding.bottom();
}
virtual QVariant valueAt( int row, int ) const override final
{
return m_values[row].first;
}
2018-04-04 20:19:47 +02:00
QLocale localeAt( int row ) const
{
if ( row >= 0 && row < m_values.size() )
return m_values[row].second;
return QLocale();
}
private:
2018-04-06 17:30:24 +02:00
inline void append( const QLocale& locale )
{
2018-04-06 17:30:24 +02:00
m_values += qMakePair( qskNativeLocaleString( locale ), locale );
}
QVector< QPair< QString, QLocale > > m_values;
mutable qreal m_maxWidth;
};
2017-07-21 18:21:34 +02:00
int main( int argc, char* argv[] )
{
#ifdef ITEM_STATISTICS
QskObjectCounter counter( true );
#endif
qputenv( "QT_IM_MODULE", "skinny" );
qputenv( "QT_PLUGIN_PATH", STRING( PLUGIN_PATH ) );
2017-07-21 18:21:34 +02:00
QGuiApplication app( argc, argv );
SkinnyFont::init( &app );
SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts );
2017-07-21 18:21:34 +02:00
2018-04-12 12:03:51 +02:00
#if 1
2017-07-28 14:47:11 +02:00
// We don't want to have a top level window.
qskDialog->setPolicy( QskDialog::EmbeddedBox );
#endif
2018-04-11 17:33:43 +02:00
#if 0
/*
QskInputContext is connected to QskSetup::inputPanelChanged,
making it the system input. If no input panel has been assigned
QskInputContext would create a window or subwindow on the fly.
*/
qskSetup->setInputPanel( new QskInputPanel() );
#endif
auto box = new QskLinearBox( Qt::Horizontal );
box->setSpacing( 10 );
box->setMargins( 20 );
2018-04-04 20:19:47 +02:00
auto listView = new LocaleListView( box );
auto inputBox = new InputBox( box );
/*
Disable Qt::ClickFocus, so that the input panel stays open
when selecting a different locale
*/
listView->setFocusPolicy( Qt::TabFocus );
2018-04-04 20:19:47 +02:00
QObject::connect( listView, &QskListView::selectedRowChanged,
inputBox, [ = ]( int row ) { inputBox->setLocale( listView->localeAt( row ) ); } );
2017-07-28 14:47:11 +02:00
QskWindow window;
window.setColor( "PapayaWhip" );
window.addItem( box );
2017-07-28 14:47:11 +02:00
window.addItem( new QskFocusIndicator() );
2018-04-12 12:03:51 +02:00
window.resize( 600, 600 );
2017-07-28 14:47:11 +02:00
window.show();
2017-07-21 18:21:34 +02:00
return app.exec();
}