code cleanup

This commit is contained in:
Uwe Rathmann 2018-11-05 11:42:46 +01:00
parent 04c1c883f6
commit 11a4c79f0f
3 changed files with 41 additions and 46 deletions

View File

@ -9,6 +9,7 @@
#include "QskSkin.h" #include "QskSkin.h"
#include <qevent.h> #include <qevent.h>
#include <qvector.h>
#include <qpa/qplatformdialoghelper.h> #include <qpa/qplatformdialoghelper.h>
#include <qpa/qplatformtheme.h> #include <qpa/qplatformtheme.h>
@ -27,26 +28,24 @@ static void qskSendEventTo( QObject* object, QEvent::Type type )
static inline QskDialog::ButtonRole qskButtonRole( static inline QskDialog::ButtonRole qskButtonRole(
QskDialog::StandardButton standardButton ) QskDialog::StandardButton standardButton )
{ {
QPlatformDialogHelper::StandardButton sb = const auto role = QPlatformDialogHelper::buttonRole(
static_cast< QPlatformDialogHelper::StandardButton >( standardButton ); static_cast< QPlatformDialogHelper::StandardButton >( standardButton ) );
QPlatformDialogHelper::ButtonRole role = QPlatformDialogHelper::buttonRole( sb );
return static_cast< QskDialog::ButtonRole >( role ); return static_cast< QskDialog::ButtonRole >( role );
} }
static void qskAddToLayout( const QList< QskPushButton* >& buttonList, static void qskAddToLayout( const QVector< QskPushButton* >& buttons,
bool reverse, QskLinearBox* layoutBox ) bool reverse, QskLinearBox* layoutBox )
{ {
if ( reverse ) if ( reverse )
{ {
for ( int i = buttonList.count() - 1; i >= 0; i-- ) for ( int i = buttons.count() - 1; i >= 0; i-- )
layoutBox->addItem( buttonList[ i ] ); layoutBox->addItem( buttons[ i ] );
} }
else else
{ {
for ( int i = 0; i < buttonList.count(); i++ ) for ( int i = 0; i < buttons.count(); i++ )
layoutBox->addItem( buttonList[ i ] ); layoutBox->addItem( buttons[ i ] );
} }
} }
@ -54,21 +53,18 @@ class QskDialogButtonBox::PrivateData
{ {
public: public:
PrivateData() PrivateData()
: layoutBox( nullptr ) : centeredButtons( false )
, centeredButtons( false )
, dirtyLayout( false ) , dirtyLayout( false )
, clickedButton( QskDialog::NoButton )
{ {
} }
QskLinearBox* layoutBox; QskLinearBox* layoutBox = nullptr;
QVector< QskPushButton* > buttons[ QskDialog::NButtonRoles ];
QList< QskPushButton* > buttonLists[ QskDialog::NButtonRoles ]; QskDialog::StandardButton clickedButton = QskDialog::NoButton;
bool centeredButtons : 1; bool centeredButtons : 1;
bool dirtyLayout : 1; bool dirtyLayout : 1;
QskDialog::StandardButton clickedButton;
}; };
QskDialogButtonBox::QskDialogButtonBox( QQuickItem* parent ) QskDialogButtonBox::QskDialogButtonBox( QQuickItem* parent )
@ -157,7 +153,7 @@ void QskDialogButtonBox::updateLayout()
void QskDialogButtonBox::rearrangeButtons() void QskDialogButtonBox::rearrangeButtons()
{ {
// Result differs from the widget counter parts. Needs more // Result differs from QDialogButtonBox. Needs more
// investigation - TODO ... // investigation - TODO ...
auto layoutBox = m_data->layoutBox; auto layoutBox = m_data->layoutBox;
@ -188,7 +184,7 @@ void QskDialogButtonBox::rearrangeButtons()
} }
case QPlatformDialogHelper::AcceptRole: case QPlatformDialogHelper::AcceptRole:
{ {
const auto& buttons = m_data->buttonLists[ role ]; const auto& buttons = m_data->buttons[ role ];
if ( !buttons.isEmpty() ) if ( !buttons.isEmpty() )
layoutBox->addItem( buttons.first() ); layoutBox->addItem( buttons.first() );
@ -197,7 +193,7 @@ void QskDialogButtonBox::rearrangeButtons()
} }
case QPlatformDialogHelper::AlternateRole: case QPlatformDialogHelper::AlternateRole:
{ {
const auto& buttons = m_data->buttonLists[ QskDialog::AcceptRole ]; const auto& buttons = m_data->buttons[ QskDialog::AcceptRole ];
if ( buttons.size() > 1 ) if ( buttons.size() > 1 )
qskAddToLayout( buttons.mid( 1 ), reverse, layoutBox ); qskAddToLayout( buttons.mid( 1 ), reverse, layoutBox );
@ -213,7 +209,7 @@ void QskDialogButtonBox::rearrangeButtons()
case QPlatformDialogHelper::ApplyRole: case QPlatformDialogHelper::ApplyRole:
case QPlatformDialogHelper::ResetRole: case QPlatformDialogHelper::ResetRole:
{ {
const auto& buttons = m_data->buttonLists[ role ]; const auto& buttons = m_data->buttons[ role ];
if ( !buttons.isEmpty() ) if ( !buttons.isEmpty() )
qskAddToLayout( buttons, reverse, layoutBox ); qskAddToLayout( buttons, reverse, layoutBox );
@ -230,7 +226,7 @@ void QskDialogButtonBox::rearrangeButtons()
layoutBox->setActive( isActive ); layoutBox->setActive( isActive );
// organizing the tab chain ??? // reorganizing the tab chain ???
} }
void QskDialogButtonBox::setCenteredButtons( bool centered ) void QskDialogButtonBox::setCenteredButtons( bool centered )
@ -262,7 +258,7 @@ void QskDialogButtonBox::addButton( QskPushButton* button, QskDialog::ButtonRole
connect( button, &QskPushButton::clicked, this, connect( button, &QskPushButton::clicked, this,
&QskDialogButtonBox::onButtonClicked ); &QskDialogButtonBox::onButtonClicked );
m_data->buttonLists[ role ] += button; m_data->buttons[ role ] += button;
invalidateLayout(); invalidateLayout();
} }
} }
@ -283,7 +279,7 @@ void QskDialogButtonBox::removeButton( QskPushButton* button )
for ( int i = 0; i < QskDialog::NButtonRoles; i++ ) for ( int i = 0; i < QskDialog::NButtonRoles; i++ )
{ {
auto& buttons = m_data->buttonLists[ i ]; auto& buttons = m_data->buttons[ i ];
if ( buttons.removeOne( button ) ) if ( buttons.removeOne( button ) )
{ {
disconnect( button, &QskPushButton::clicked, disconnect( button, &QskPushButton::clicked,
@ -305,8 +301,8 @@ void QskDialogButtonBox::clear()
{ {
for ( int i = 0; i < QskDialog::NButtonRoles; i++ ) for ( int i = 0; i < QskDialog::NButtonRoles; i++ )
{ {
for ( auto button : qskAsConst( m_data->buttonLists[ i ] ) ) qDeleteAll( m_data->buttons[ i ] );
delete button; m_data->buttons[ i ].clear();
} }
invalidateLayout(); invalidateLayout();
@ -316,8 +312,8 @@ void QskDialogButtonBox::setStandardButtons( QskDialog::StandardButtons standard
{ {
for ( int i = 0; i < QskDialog::NButtonRoles; i++ ) for ( int i = 0; i < QskDialog::NButtonRoles; i++ )
{ {
for ( auto button : qskAsConst( m_data->buttonLists[ i ] ) ) qDeleteAll( m_data->buttons[ i ] );
delete button; m_data->buttons[ i ].clear();
} }
for ( int i = QskDialog::Ok; i <= QskDialog::RestoreDefaults; i <<= 1 ) for ( int i = QskDialog::Ok; i <= QskDialog::RestoreDefaults; i <<= 1 )
@ -330,12 +326,12 @@ void QskDialogButtonBox::setStandardButtons( QskDialog::StandardButtons standard
invalidateLayout(); invalidateLayout();
} }
QList< QskPushButton* > QskDialogButtonBox::buttons() const QVector< QskPushButton* > QskDialogButtonBox::buttons() const
{ {
QList< QskPushButton* > buttons; QVector< QskPushButton* > buttons;
for ( int i = 0; i < QskDialog::NButtonRoles; i++ ) for ( int i = 0; i < QskDialog::NButtonRoles; i++ )
buttons += m_data->buttonLists[ i ]; buttons += m_data->buttons[ i ];
return buttons; return buttons;
} }
@ -344,7 +340,7 @@ QskDialog::ButtonRole QskDialogButtonBox::buttonRole( const QskPushButton* butto
{ {
for ( int i = 0; i < QskDialog::NButtonRoles; i++ ) for ( int i = 0; i < QskDialog::NButtonRoles; i++ )
{ {
const auto& buttons = m_data->buttonLists[ i ]; const auto& buttons = m_data->buttons[ i ];
for ( const auto btn : buttons ) for ( const auto btn : buttons )
{ {
@ -371,9 +367,7 @@ QskDialog::StandardButton QskDialogButtonBox::defaultButtonCandidate() const
for ( auto role : fallBackRoles ) for ( auto role : fallBackRoles )
{ {
QskDialog::StandardButton defaultButton = const auto defaultButton = standardButton( buttonFromRole( role ) );
standardButton( buttonFromRole( role ) );
if ( defaultButton != QskDialog::NoButton ) if ( defaultButton != QskDialog::NoButton )
return defaultButton; return defaultButton;
} }
@ -383,7 +377,7 @@ QskDialog::StandardButton QskDialogButtonBox::defaultButtonCandidate() const
void QskDialogButtonBox::onButtonClicked() void QskDialogButtonBox::onButtonClicked()
{ {
QskPushButton* button = qobject_cast< QskPushButton* >( sender() ); auto button = qobject_cast< QskPushButton* >( sender() );
if ( button == nullptr ) if ( button == nullptr )
return; return;
@ -424,7 +418,7 @@ QskDialog::StandardButtons QskDialogButtonBox::standardButtons() const
for ( int i = 0; i < QskDialog::NButtonRoles; i++ ) for ( int i = 0; i < QskDialog::NButtonRoles; i++ )
{ {
const auto& buttons = m_data->buttonLists[ i ]; const auto& buttons = m_data->buttons[ i ];
for ( const auto btn : buttons ) for ( const auto btn : buttons )
standardButtons |= this->standardButton( btn ); standardButtons |= this->standardButton( btn );
@ -452,7 +446,7 @@ QskPushButton* QskDialogButtonBox::button( QskDialog::StandardButton standardBut
{ {
for ( int i = 0; i < QskDialog::NButtonRoles; i++ ) for ( int i = 0; i < QskDialog::NButtonRoles; i++ )
{ {
const auto& buttons = m_data->buttonLists[ i ]; const auto& buttons = m_data->buttons[ i ];
for ( auto btn : buttons ) for ( auto btn : buttons )
{ {
if ( this->standardButton( btn ) == standardButton ) if ( this->standardButton( btn ) == standardButton )
@ -468,8 +462,8 @@ QskPushButton* QskDialogButtonBox::buttonFromRole( QskDialog::ButtonRole role )
if ( role < 0 || role >= QskDialog::NButtonRoles ) if ( role < 0 || role >= QskDialog::NButtonRoles )
return nullptr; return nullptr;
const auto& buttonList = m_data->buttonLists[ role ]; const auto& buttons = m_data->buttons[ role ];
return buttonList.isEmpty() ? nullptr : buttonList.first(); return buttons.isEmpty() ? nullptr : buttons.first();
} }
QskDialog::StandardButton QskDialogButtonBox::clickedButton() const QskDialog::StandardButton QskDialogButtonBox::clickedButton() const
@ -501,6 +495,8 @@ bool QskDialogButtonBox::isDefaultButtonKeyEvent( const QKeyEvent* event )
QString QskDialogButtonBox::buttonText( QskDialog::StandardButton standardButton ) QString QskDialogButtonBox::buttonText( QskDialog::StandardButton standardButton )
{ {
// should be redirected through the skin !
const QPlatformTheme* theme = QGuiApplicationPrivate::platformTheme(); const QPlatformTheme* theme = QGuiApplicationPrivate::platformTheme();
QString text = theme->standardButtonText( standardButton ); QString text = theme->standardButtonText( standardButton );

View File

@ -10,6 +10,7 @@
#include "QskDialog.h" #include "QskDialog.h"
class QskPushButton; class QskPushButton;
template <typename T> class QVector;
class QSK_EXPORT QskDialogButtonBox : public QskBox class QSK_EXPORT QskDialogButtonBox : public QskBox
{ {
@ -42,7 +43,7 @@ class QSK_EXPORT QskDialogButtonBox : public QskBox
void removeButton( QskPushButton* button ); void removeButton( QskPushButton* button );
void clear(); void clear();
QList< QskPushButton* > buttons() const; QVector< QskPushButton* > buttons() const;
QskDialog::ButtonRole buttonRole( const QskPushButton* ) const; QskDialog::ButtonRole buttonRole( const QskPushButton* ) const;
void setStandardButtons( QskDialog::StandardButtons buttons ); void setStandardButtons( QskDialog::StandardButtons buttons );
@ -80,10 +81,8 @@ class QSK_EXPORT QskDialogButtonBox : public QskBox
void invalidateLayout(); void invalidateLayout();
private Q_SLOTS:
void onButtonClicked();
private: private:
void onButtonClicked();
void rearrangeButtons(); void rearrangeButtons();
class PrivateData; class PrivateData;

View File

@ -140,9 +140,9 @@ void QskInputSubWindow::setStandardButtons( QskDialog::StandardButtons buttons )
m_data->buttonBox->setVisible( buttons != QskDialog::NoButton ); m_data->buttonBox->setVisible( buttons != QskDialog::NoButton );
#if 1 #if 1
const QList< QskPushButton* > b = m_data->buttonBox->buttons(); const auto btns = m_data->buttonBox->buttons();
if ( !b.isEmpty() ) if ( !btns.isEmpty() )
b[ 0 ]->setFocus( true ); btns[ 0 ]->setFocus( true );
#endif #endif
} }