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 "QskDialogSubWindow.h"
|
2018-11-06 17:54:21 +00:00
|
|
|
#include "QskDialogButtonBox.h"
|
|
|
|
#include "QskLinearBox.h"
|
|
|
|
#include "QskPushButton.h"
|
|
|
|
#include "QskQuick.h"
|
2018-07-19 12:10:48 +00:00
|
|
|
|
|
|
|
#include <qeventloop.h>
|
|
|
|
#include <qquickwindow.h>
|
2018-11-06 17:54:21 +00:00
|
|
|
#include <qpointer.h>
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2018-07-02 06:08:38 +00:00
|
|
|
static inline void qskSetRejectOnClose( QskDialogSubWindow* subWindow, bool on )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2018-07-02 06:08:38 +00:00
|
|
|
if ( on )
|
|
|
|
{
|
|
|
|
QObject::connect( subWindow, &QskPopup::closed,
|
|
|
|
subWindow, &QskDialogSubWindow::reject );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QObject::disconnect( subWindow, &QskPopup::closed,
|
|
|
|
subWindow, &QskDialogSubWindow::reject );
|
|
|
|
}
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:54:21 +00:00
|
|
|
class QskDialogSubWindow::PrivateData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QskDialog::Actions actions = QskDialog::NoAction;
|
|
|
|
|
|
|
|
QPointer< QQuickItem > contentItem;
|
|
|
|
QskDialogButtonBox* buttonBox = nullptr;
|
|
|
|
QskLinearBox* layoutBox;
|
|
|
|
|
|
|
|
QskDialog::DialogCode result = QskDialog::Rejected;
|
|
|
|
};
|
|
|
|
|
2018-08-03 06:15:28 +00:00
|
|
|
QskDialogSubWindow::QskDialogSubWindow( QQuickItem* parent )
|
|
|
|
: Inherited( parent )
|
2018-11-06 17:54:21 +00:00
|
|
|
, m_data( new PrivateData() )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2018-11-06 17:54:21 +00:00
|
|
|
// doing the layout manually instead ???
|
|
|
|
setAutoLayoutChildren( true );
|
|
|
|
m_data->layoutBox = new QskLinearBox( Qt::Vertical, this );
|
|
|
|
|
2018-07-02 06:08:38 +00:00
|
|
|
qskSetRejectOnClose( this, true );
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 06:08:38 +00:00
|
|
|
QskDialogSubWindow::~QskDialogSubWindow()
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:54:21 +00:00
|
|
|
void QskDialogSubWindow::setDialogActions( QskDialog::Actions actions )
|
|
|
|
{
|
|
|
|
if ( m_data->actions == actions )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_data->actions = actions;
|
|
|
|
|
|
|
|
if ( actions == QskDialog::NoAction )
|
|
|
|
{
|
|
|
|
delete m_data->buttonBox;
|
|
|
|
m_data->buttonBox = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( m_data->buttonBox == nullptr )
|
|
|
|
{
|
|
|
|
m_data->buttonBox = createButtonBox();
|
|
|
|
|
|
|
|
if ( m_data->buttonBox )
|
|
|
|
{
|
|
|
|
m_data->layoutBox->addItem( m_data->buttonBox );
|
|
|
|
|
|
|
|
connect( m_data->buttonBox, &QskDialogButtonBox::accepted,
|
|
|
|
this, &QskDialogSubWindow::accept, Qt::UniqueConnection );
|
|
|
|
|
|
|
|
connect( m_data->buttonBox, &QskDialogButtonBox::rejected,
|
|
|
|
this, &QskDialogSubWindow::reject, Qt::UniqueConnection );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_data->buttonBox )
|
|
|
|
m_data->buttonBox->setActions( actions );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QskDialog::Actions QskDialogSubWindow::dialogActions() const
|
|
|
|
{
|
|
|
|
if ( m_data->buttonBox )
|
|
|
|
return m_data->buttonBox->actions();
|
|
|
|
|
|
|
|
return QskDialog::NoAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskDialogSubWindow::setContentItem( QQuickItem* item )
|
|
|
|
{
|
|
|
|
if ( item == m_data->contentItem )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( m_data->contentItem )
|
|
|
|
{
|
|
|
|
m_data->layoutBox->removeAt( 0 );
|
|
|
|
if ( m_data->contentItem->parent() == m_data->layoutBox )
|
|
|
|
delete m_data->contentItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_data->contentItem = item;
|
|
|
|
|
|
|
|
if ( item )
|
|
|
|
m_data->layoutBox->insertItem( 0, item );
|
|
|
|
}
|
|
|
|
|
|
|
|
QQuickItem* QskDialogSubWindow::contentItem() const
|
|
|
|
{
|
|
|
|
return m_data->contentItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskDialogSubWindow::setDefaultDialogAction( QskDialog::Action action )
|
|
|
|
{
|
|
|
|
QskPushButton* button = nullptr;
|
|
|
|
|
|
|
|
if ( m_data->buttonBox )
|
|
|
|
button = m_data->buttonBox->button( action );
|
|
|
|
|
|
|
|
setDefaultButton( button );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskDialogSubWindow::setDefaultButton( QskPushButton* button )
|
|
|
|
{
|
|
|
|
if ( !qskIsAncestorOf( m_data->buttonBox, button ) )
|
|
|
|
{
|
|
|
|
#if defined( QT_DEBUG )
|
|
|
|
qWarning( "Only buttons of the QskDialogButtonBox can be the default button." );
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_data->buttonBox->setDefaultButton( button );
|
|
|
|
}
|
|
|
|
|
|
|
|
QskPushButton* QskDialogSubWindow::defaultButton() const
|
|
|
|
{
|
|
|
|
if ( m_data->buttonBox == nullptr )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return m_data->buttonBox->defaultButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
QskDialogButtonBox* QskDialogSubWindow::buttonBox()
|
|
|
|
{
|
|
|
|
return m_data->buttonBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QskDialogButtonBox* QskDialogSubWindow::buttonBox() const
|
|
|
|
{
|
|
|
|
return m_data->buttonBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
QskDialog::Action QskDialogSubWindow::clickedAction() const
|
|
|
|
{
|
|
|
|
if ( m_data->buttonBox )
|
|
|
|
return m_data->buttonBox->clickedAction();
|
|
|
|
|
|
|
|
return QskDialog::NoAction;
|
|
|
|
}
|
|
|
|
|
2017-07-21 16:21:34 +00:00
|
|
|
void QskDialogSubWindow::setResult( QskDialog::DialogCode result )
|
|
|
|
{
|
2018-11-06 17:54:21 +00:00
|
|
|
m_data->result = result;
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QskDialog::DialogCode QskDialogSubWindow::result() const
|
|
|
|
{
|
2018-11-06 17:54:21 +00:00
|
|
|
return m_data->result;
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QskDialog::DialogCode QskDialogSubWindow::exec()
|
|
|
|
{
|
|
|
|
if ( window() == nullptr )
|
|
|
|
{
|
|
|
|
qWarning( "trying to exec a subwindow without window. " );
|
|
|
|
return QskDialog::Rejected;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( QQuickItem* mouseGrabber = window()->mouseGrabberItem() )
|
|
|
|
{
|
|
|
|
// when being called from QQuickWindow::mouseReleaseEvent
|
|
|
|
// the mouse grabber has not yet been released.
|
|
|
|
|
|
|
|
mouseGrabber->ungrabMouse();
|
|
|
|
}
|
|
|
|
|
2018-10-10 06:55:03 +00:00
|
|
|
open();
|
2017-07-21 16:21:34 +00:00
|
|
|
|
|
|
|
QEventLoop eventLoop;
|
|
|
|
|
|
|
|
connect( this, &QskDialogSubWindow::finished, &eventLoop, &QEventLoop::quit );
|
|
|
|
( void ) eventLoop.exec( QEventLoop::DialogExec );
|
|
|
|
|
2018-11-06 17:54:21 +00:00
|
|
|
return m_data->result;;
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskDialogSubWindow::done( QskDialog::DialogCode result )
|
|
|
|
{
|
2018-11-06 17:54:21 +00:00
|
|
|
m_data->result = result;
|
2017-07-21 16:21:34 +00:00
|
|
|
|
2018-07-02 06:08:38 +00:00
|
|
|
if ( !isOpen() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
qskSetRejectOnClose( this, false );
|
|
|
|
close();
|
|
|
|
|
2017-07-21 16:21:34 +00:00
|
|
|
Q_EMIT finished( result );
|
|
|
|
|
|
|
|
if ( result == QskDialog::Accepted )
|
|
|
|
Q_EMIT accepted();
|
|
|
|
else
|
|
|
|
Q_EMIT rejected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskDialogSubWindow::accept()
|
|
|
|
{
|
|
|
|
done( QskDialog::Accepted );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskDialogSubWindow::reject()
|
|
|
|
{
|
|
|
|
done( QskDialog::Rejected );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskDialogSubWindow::keyPressEvent( QKeyEvent* event )
|
|
|
|
{
|
2018-11-06 17:54:21 +00:00
|
|
|
if ( m_data->buttonBox &&
|
|
|
|
QskDialogButtonBox::isDefaultButtonKeyEvent( event ) )
|
|
|
|
{
|
|
|
|
auto button = m_data->buttonBox->defaultButton();
|
|
|
|
if ( button && button->isEnabled() )
|
|
|
|
button->click();
|
|
|
|
}
|
|
|
|
|
2017-07-21 16:21:34 +00:00
|
|
|
if ( event->matches( QKeySequence::Cancel ) )
|
|
|
|
{
|
|
|
|
// using shortcuts instead ???
|
|
|
|
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Inherited::keyPressEvent( event );
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:54:21 +00:00
|
|
|
QskDialogButtonBox* QskDialogSubWindow::createButtonBox()
|
|
|
|
{
|
|
|
|
return new QskDialogButtonBox();
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:11:37 +00:00
|
|
|
void QskDialogSubWindow::aboutToShow()
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
2018-06-26 09:11:37 +00:00
|
|
|
if ( size().isEmpty() )
|
2017-07-21 16:21:34 +00:00
|
|
|
{
|
|
|
|
// setting an initial size from the hint, centered inside the window
|
|
|
|
|
|
|
|
QRectF rect;
|
|
|
|
rect.setSize( sizeHint() );
|
2017-12-07 16:04:05 +00:00
|
|
|
rect.moveCenter( QPointF( 0.5 * parentItem()->width(), 0.5 * parentItem()->height() ) );
|
2017-07-21 16:21:34 +00:00
|
|
|
|
|
|
|
setGeometry( rect );
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:11:37 +00:00
|
|
|
Inherited::aboutToShow();
|
2017-07-21 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskDialogSubWindow.cpp"
|