qskinny/src/dialogs/QskDialogButtonBox.cpp

510 lines
13 KiB
C++
Raw Normal View History

2017-07-21 16:21:34 +00:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskDialogButtonBox.h"
#include "QskDialogButton.h"
#include "QskLinearBox.h"
#include "QskSkin.h"
2018-07-19 12:10:48 +00:00
#include <qevent.h>
2018-11-06 17:54:21 +00:00
#include <qpointer.h>
2018-11-05 10:42:46 +00:00
#include <qvector.h>
2018-11-06 17:54:21 +00:00
2017-07-21 16:21:34 +00:00
#include <qpa/qplatformdialoghelper.h>
2018-08-03 06:15:28 +00:00
#include <qpa/qplatformtheme.h>
2017-07-21 16:21:34 +00:00
2018-07-19 12:10:48 +00:00
QSK_QT_PRIVATE_BEGIN
#include <private/qguiapplication_p.h>
QSK_QT_PRIVATE_END
2017-07-21 16:21:34 +00:00
QSK_SUBCONTROL( QskDialogButtonBox, Panel )
static void qskSendEventTo( QObject* object, QEvent::Type type )
{
QEvent event( type );
QCoreApplication::sendEvent( object, &event );
}
static inline QskDialog::ActionRole qskActionRole( QskDialog::Action action )
2017-07-21 16:21:34 +00:00
{
2019-01-04 12:42:16 +00:00
const auto role = QPlatformDialogHelper::buttonRole(
static_cast< QPlatformDialogHelper::StandardButton >( action ) );
2017-07-21 16:21:34 +00:00
return static_cast< QskDialog::ActionRole >( role );
2017-07-21 16:21:34 +00:00
}
2018-11-05 10:42:46 +00:00
static void qskAddToLayout( const QVector< QskPushButton* >& buttons,
2017-07-21 16:21:34 +00:00
bool reverse, QskLinearBox* layoutBox )
{
if ( reverse )
{
2018-11-05 10:42:46 +00:00
for ( int i = buttons.count() - 1; i >= 0; i-- )
layoutBox->addItem( buttons[ i ] );
2017-07-21 16:21:34 +00:00
}
else
{
2018-11-05 10:42:46 +00:00
for ( int i = 0; i < buttons.count(); i++ )
layoutBox->addItem( buttons[ i ] );
2017-07-21 16:21:34 +00:00
}
}
class QskDialogButtonBox::PrivateData
{
2018-08-03 06:15:28 +00:00
public:
PrivateData()
2018-11-05 10:42:46 +00:00
: centeredButtons( false )
2018-08-03 06:15:28 +00:00
, dirtyLayout( false )
2017-07-21 16:21:34 +00:00
{
}
2018-11-05 10:42:46 +00:00
QskLinearBox* layoutBox = nullptr;
QVector< QskPushButton* > buttons[ QskDialog::NActionRoles ];
2018-11-06 17:54:21 +00:00
QPointer< QskPushButton > defaultButton;
2017-07-21 16:21:34 +00:00
QskDialog::Action clickedAction = QskDialog::NoAction;
2017-07-21 16:21:34 +00:00
bool centeredButtons : 1;
bool dirtyLayout : 1;
};
2018-08-03 06:15:28 +00:00
QskDialogButtonBox::QskDialogButtonBox( QQuickItem* parent )
: QskDialogButtonBox( Qt::Horizontal, parent )
2017-07-21 16:21:34 +00:00
{
}
2018-08-03 06:15:28 +00:00
QskDialogButtonBox::QskDialogButtonBox( Qt::Orientation orientation, QQuickItem* parent )
: Inherited( parent )
, m_data( new PrivateData() )
2017-07-21 16:21:34 +00:00
{
setPolishOnResize( true );
setOrientation( orientation );
}
QskDialogButtonBox::~QskDialogButtonBox()
{
}
void QskDialogButtonBox::setOrientation( Qt::Orientation orientation )
{
if ( m_data->layoutBox && m_data->layoutBox->orientation() == orientation )
return;
delete m_data->layoutBox;
m_data->layoutBox = new QskLinearBox( orientation, this );
m_data->layoutBox->setObjectName( QStringLiteral( "DialogButtonBoxLayout" ) );
if ( orientation == Qt::Horizontal )
setSizePolicy( QskSizePolicy::Preferred, QskSizePolicy::Fixed );
else
setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Preferred );
invalidateLayout();
Q_EMIT orientationChanged();
}
Qt::Orientation QskDialogButtonBox::orientation() const
{
return m_data->layoutBox->orientation();
}
QskAspect::Subcontrol QskDialogButtonBox::substitutedSubcontrol(
2017-07-21 16:21:34 +00:00
QskAspect::Subcontrol subControl ) const
{
if ( subControl == QskBox::Panel )
return QskDialogButtonBox::Panel;
return Inherited::substitutedSubcontrol( subControl );
2017-07-21 16:21:34 +00:00
}
QSizeF QskDialogButtonBox::layoutSizeHint(
Qt::SizeHint which, const QSizeF& constraint ) const
2017-07-21 16:21:34 +00:00
{
if ( m_data->dirtyLayout )
{
const_cast< QskDialogButtonBox* >( this )->rearrangeButtons();
m_data->dirtyLayout = false;
}
return m_data->layoutBox->effectiveSizeHint( which, constraint );
2017-07-21 16:21:34 +00:00
}
void QskDialogButtonBox::invalidateLayout()
{
m_data->dirtyLayout = true;
resetImplicitSize();
polish();
}
void QskDialogButtonBox::updateLayout()
{
if ( m_data->dirtyLayout )
{
rearrangeButtons();
m_data->dirtyLayout = false;
if ( parentItem() )
qskSendEventTo( parentItem(), QEvent::LayoutRequest );
}
m_data->layoutBox->setGeometry( layoutRect() );
}
void QskDialogButtonBox::rearrangeButtons()
{
2018-11-05 10:42:46 +00:00
// Result differs from QDialogButtonBox. Needs more
2017-07-21 16:21:34 +00:00
// investigation - TODO ...
auto layoutBox = m_data->layoutBox;
layoutBox->clear();
const int* currentLayout = effectiveSkin()->dialogButtonLayout( orientation() );
if ( m_data->centeredButtons )
2021-10-27 11:02:34 +00:00
layoutBox->addStretch( 1 );
2017-07-21 16:21:34 +00:00
while ( *currentLayout != QPlatformDialogHelper::EOL )
{
const int role = ( *currentLayout & ~QPlatformDialogHelper::Reverse );
const bool reverse = ( *currentLayout & QPlatformDialogHelper::Reverse );
switch ( role )
{
case QPlatformDialogHelper::Stretch:
{
if ( !m_data->centeredButtons )
layoutBox->addStretch( 1 );
break;
}
case QPlatformDialogHelper::AcceptRole:
{
2018-11-05 10:42:46 +00:00
const auto& buttons = m_data->buttons[ role ];
2017-07-21 16:21:34 +00:00
if ( !buttons.isEmpty() )
layoutBox->addItem( buttons.first() );
break;
}
case QPlatformDialogHelper::AlternateRole:
{
2018-11-05 10:42:46 +00:00
const auto& buttons = m_data->buttons[ QskDialog::AcceptRole ];
2017-07-21 16:21:34 +00:00
if ( buttons.size() > 1 )
qskAddToLayout( buttons.mid( 1 ), reverse, layoutBox );
break;
}
case QPlatformDialogHelper::DestructiveRole:
case QPlatformDialogHelper::RejectRole:
case QPlatformDialogHelper::ActionRole:
case QPlatformDialogHelper::HelpRole:
case QPlatformDialogHelper::YesRole:
case QPlatformDialogHelper::NoRole:
case QPlatformDialogHelper::ApplyRole:
case QPlatformDialogHelper::ResetRole:
{
2018-11-05 10:42:46 +00:00
const auto& buttons = m_data->buttons[ role ];
2017-07-21 16:21:34 +00:00
if ( !buttons.isEmpty() )
qskAddToLayout( buttons, reverse, layoutBox );
break;
}
}
++currentLayout;
}
if ( m_data->centeredButtons )
layoutBox->addStretch( 1 );
2018-11-05 10:42:46 +00:00
// reorganizing the tab chain ???
2017-07-21 16:21:34 +00:00
}
void QskDialogButtonBox::setCenteredButtons( bool centered )
{
if ( centered != m_data->centeredButtons )
{
m_data->centeredButtons = centered;
invalidateLayout();
Q_EMIT centeredButtonsChanged();
}
}
bool QskDialogButtonBox::centeredButtons() const
{
return m_data->centeredButtons;
}
void QskDialogButtonBox::addButton(
QskPushButton* button, QskDialog::ActionRole role )
2017-07-21 16:21:34 +00:00
{
if ( role < 0 || role >= QskDialog::NActionRoles )
2017-07-21 16:21:34 +00:00
return;
if ( button )
{
if ( button->parent() == nullptr )
button->setParent( this );
2017-07-21 16:21:34 +00:00
2018-11-06 17:54:21 +00:00
/*
2018-11-06 18:00:42 +00:00
To have a proper ownership. Inserting the buttons
2019-01-04 12:42:16 +00:00
according to the layout rules will be done later
2018-11-06 17:54:21 +00:00
*/
button->setParentItem( m_data->layoutBox );
connect( button, &QskPushButton::clicked, this,
&QskDialogButtonBox::onButtonClicked );
2018-11-05 10:42:46 +00:00
m_data->buttons[ role ] += button;
invalidateLayout();
}
2017-07-21 16:21:34 +00:00
}
2018-11-08 12:33:30 +00:00
void QskDialogButtonBox::addAction( QskDialog::Action action )
2017-07-21 16:21:34 +00:00
{
QskPushButton* button = createButton( action );
2017-07-21 16:21:34 +00:00
if ( button )
addButton( button, qskActionRole( action ) );
2017-07-21 16:21:34 +00:00
}
void QskDialogButtonBox::removeButton( QskPushButton* button )
{
// ChildRemove Events !!!
if ( button == nullptr )
return;
for ( int i = 0; i < QskDialog::NActionRoles; i++ )
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
auto& buttons = m_data->buttons[ i ];
2017-07-21 16:21:34 +00:00
if ( buttons.removeOne( button ) )
{
disconnect( button, &QskPushButton::clicked,
this, &QskDialogButtonBox::onButtonClicked );
invalidateLayout();
return;
}
}
}
QskPushButton* QskDialogButtonBox::createButton(
QskDialog::Action action ) const
2017-07-21 16:21:34 +00:00
{
return new QskDialogButton( action );
2017-07-21 16:21:34 +00:00
}
void QskDialogButtonBox::clear()
{
for ( int i = 0; i < QskDialog::NActionRoles; i++ )
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
qDeleteAll( m_data->buttons[ i ] );
m_data->buttons[ i ].clear();
2017-07-21 16:21:34 +00:00
}
invalidateLayout();
}
2018-11-06 17:54:21 +00:00
void QskDialogButtonBox::setDefaultButton( QskPushButton* button )
{
m_data->defaultButton = button;
}
QskPushButton* QskDialogButtonBox::defaultButton() const
{
return m_data->defaultButton;
}
void QskDialogButtonBox::setActions( QskDialog::Actions actions )
2017-07-21 16:21:34 +00:00
{
for ( int i = 0; i < QskDialog::NActionRoles; i++ )
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
qDeleteAll( m_data->buttons[ i ] );
m_data->buttons[ i ].clear();
2017-07-21 16:21:34 +00:00
}
for ( int i = QskDialog::Ok; i <= QskDialog::RestoreDefaults; i <<= 1 )
{
const auto action = static_cast< QskDialog::Action >( i );
if ( action & actions )
2018-11-08 12:33:30 +00:00
addAction( action );
2017-07-21 16:21:34 +00:00
}
invalidateLayout();
}
2018-11-05 10:42:46 +00:00
QVector< QskPushButton* > QskDialogButtonBox::buttons() const
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
QVector< QskPushButton* > buttons;
2017-07-21 16:21:34 +00:00
for ( int i = 0; i < QskDialog::NActionRoles; i++ )
2018-11-05 10:42:46 +00:00
buttons += m_data->buttons[ i ];
2017-07-21 16:21:34 +00:00
return buttons;
}
2018-11-06 17:54:21 +00:00
QVector< QskPushButton* > QskDialogButtonBox::buttons(
QskDialog::ActionRole role ) const
{
if ( role < 0 || role >= QskDialog::NActionRoles )
return QVector< QskPushButton* >();
return m_data->buttons[ role ];
}
QskDialog::ActionRole QskDialogButtonBox::actionRole(
const QskPushButton* button ) const
2017-07-21 16:21:34 +00:00
{
for ( int i = 0; i < QskDialog::NActionRoles; i++ )
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
const auto& buttons = m_data->buttons[ i ];
2017-07-21 16:21:34 +00:00
2018-08-03 06:15:28 +00:00
for ( const auto btn : buttons )
2017-07-21 16:21:34 +00:00
{
if ( button == btn )
return static_cast< QskDialog::ActionRole >( i );
2017-07-21 16:21:34 +00:00
}
}
return QskDialog::InvalidRole;
}
void QskDialogButtonBox::onButtonClicked()
{
2018-11-05 10:42:46 +00:00
auto button = qobject_cast< QskPushButton* >( sender() );
2017-07-21 16:21:34 +00:00
if ( button == nullptr )
return;
switch ( actionRole( button ) )
2017-07-21 16:21:34 +00:00
{
case QskDialog::AcceptRole:
case QskDialog::YesRole:
{
m_data->clickedAction = action( button );
2017-07-21 16:21:34 +00:00
Q_EMIT clicked( button );
Q_EMIT accepted();
break;
}
case QskDialog::RejectRole:
case QskDialog::NoRole:
case QskDialog::DestructiveRole:
{
m_data->clickedAction = action( button );
2017-07-21 16:21:34 +00:00
Q_EMIT clicked( button );
Q_EMIT rejected();
break;
}
default:
{
m_data->clickedAction = QskDialog::NoAction;
2017-07-21 16:21:34 +00:00
Q_EMIT clicked( button );
break;
}
}
}
QskDialog::Actions QskDialogButtonBox::actions() const
2017-07-21 16:21:34 +00:00
{
QskDialog::Actions actions;
2017-07-21 16:21:34 +00:00
for ( int i = 0; i < QskDialog::NActionRoles; i++ )
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
const auto& buttons = m_data->buttons[ i ];
2017-07-21 16:21:34 +00:00
2018-08-03 06:15:28 +00:00
for ( const auto btn : buttons )
actions |= action( btn );
2017-07-21 16:21:34 +00:00
}
return actions;
2017-07-21 16:21:34 +00:00
}
QskDialog::Action QskDialogButtonBox::action( const QskPushButton* button ) const
2017-07-21 16:21:34 +00:00
{
if ( button )
{
if ( auto dialogButton = qobject_cast< const QskDialogButton* >( button ) )
return dialogButton->action();
2017-07-21 16:21:34 +00:00
const QVariant value = button->property( "standardButton" );
if ( value.canConvert< int >() )
return static_cast< QskDialog::Action >( value.toInt() );
2017-07-21 16:21:34 +00:00
}
return QskDialog::NoAction;
2017-07-21 16:21:34 +00:00
}
QskPushButton* QskDialogButtonBox::button( QskDialog::Action action ) const
2017-07-21 16:21:34 +00:00
{
for ( int i = 0; i < QskDialog::NActionRoles; i++ )
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
const auto& buttons = m_data->buttons[ i ];
2018-08-03 06:15:28 +00:00
for ( auto btn : buttons )
2017-07-21 16:21:34 +00:00
{
if ( this->action( btn ) == action )
2017-07-21 16:21:34 +00:00
return btn;
}
}
return nullptr;
}
QskDialog::Action QskDialogButtonBox::clickedAction() const
2017-07-21 16:21:34 +00:00
{
return m_data->clickedAction;
2017-07-21 16:21:34 +00:00
}
bool QskDialogButtonBox::event( QEvent* event )
{
if ( event->type() == QEvent::LayoutRequest )
{
if ( !m_data->dirtyLayout )
resetImplicitSize();
}
2017-07-21 16:21:34 +00:00
return Inherited::event( event );
}
bool QskDialogButtonBox::isDefaultButtonKeyEvent( const QKeyEvent* event )
{
if ( event->modifiers() & Qt::KeypadModifier && event->key() == Qt::Key_Enter )
{
return ( event->modifiers() & Qt::KeypadModifier )
&& ( event->key() == Qt::Key_Enter );
}
else
{
return ( event->key() == Qt::Key_Enter ) ||
( event->key() == Qt::Key_Return );
}
}
QString QskDialogButtonBox::buttonText( QskDialog::Action action )
2017-07-21 16:21:34 +00:00
{
2018-11-05 10:42:46 +00:00
// should be redirected through the skin !
2017-07-21 16:21:34 +00:00
const QPlatformTheme* theme = QGuiApplicationPrivate::platformTheme();
QString text = theme->standardButtonText( action );
2017-07-21 16:21:34 +00:00
#if QT_VERSION < QT_VERSION_CHECK( 5, 7, 0 )
text.remove( '&' );
#else
text = QPlatformTheme::removeMnemonics( text );
#endif
return text;
}
#include "moc_QskDialogButtonBox.cpp"