QskMenu::exec added

This commit is contained in:
Uwe Rathmann 2021-12-30 11:13:48 +01:00
parent 89a2a395fd
commit 9cef7705d8
3 changed files with 82 additions and 9 deletions

View File

@ -85,18 +85,20 @@ class GraphicLabel : public QskGraphicLabel
protected:
void mousePressEvent( QMouseEvent* event ) override
{
auto menu = new QskMenu( this );
QskMenu menu( this );
menu.setPopupFlag( QskPopup::DeleteOnClose, false );
menu->addItem( "image://shapes/Rectangle/White", "Print" );
menu->addItem( "image://shapes/Diamond/Yellow", "Save As" );
menu->addItem( "image://shapes/Ellipse/Red", "Setup" );
menu->addItem( "image://shapes/Hexagon/PapayaWhip", "Help" );
menu.addItem( "image://shapes/Rectangle/White", "Print" );
menu.addItem( "image://shapes/Diamond/Yellow", "Save As" );
menu.addItem( "image://shapes/Ellipse/Red", "Setup" );
menu.addItem( "image://shapes/Hexagon/PapayaWhip", "Help" );
menu->setOrigin( qskMousePosition( event ) );
menu->open();
menu.setOrigin( qskMousePosition( event ) );
connect( menu, &QskMenu::triggered,
this, []( int index ) { qDebug() << index; } );
if ( int result = menu.exec() )
{
qDebug() << result;
}
}
#endif
};

View File

@ -9,6 +9,7 @@
#include <qvector.h>
#include <qvariant.h>
#include <qeventloop.h>
QSK_SUBCONTROL( QskMenu, Panel )
QSK_SUBCONTROL( QskMenu, Cell )
@ -37,6 +38,8 @@ class QskMenu::PrivateData
QPointF origin;
int currentIndex = -1;
int triggeredIndex = -1;
bool isPressed = false;
};
@ -328,7 +331,9 @@ void QskMenu::setSelectedIndex( int index )
if ( index >= 0 )
setCurrentIndex( index );
m_data->triggeredIndex = index;
Q_EMIT triggered( index );
close();
}
@ -344,4 +349,68 @@ int QskMenu::indexAtPosition( const QPointF& pos ) const
this, contentsRect(), QskMenu::Cell, pos );
}
namespace
{
class EventLoop : public QEventLoop
{
public:
EventLoop( QskMenu* menu )
: QEventLoop( menu )
{
/*
We want menu being the parent, so that the loop can be found
by menu->findChild< QEventLoop* >().
*/
connect( menu, &QObject::destroyed, this, &EventLoop::reject );
connect( menu, &QskMenu::fadingChanged, this, &EventLoop::maybeQuit );
}
private:
void reject()
{
setParent( nullptr );
quit();
}
void maybeQuit()
{
auto menu = static_cast< const QskMenu* >( parent() );
if ( !menu->isOpen() && !menu->isFading() )
quit();
}
};
}
int QskMenu::exec()
{
if ( isOpen() || isFading() )
{
qWarning() << "QskMenu::exec: menu is already opened";
return -1;
}
const bool deleteOnClose = popupFlags() & QskPopup::DeleteOnClose;
if ( deleteOnClose )
setPopupFlag( QskPopup::DeleteOnClose, false );
QPointer< QskMenu > menu = this;
menu->open();
EventLoop loop( this );
(void )loop.exec( QEventLoop::DialogExec );
int result = -1;
if ( menu )
{
result = menu->m_data->triggeredIndex;
if ( deleteOnClose )
delete menu;
}
return result;
}
#include "moc_QskMenu.cpp"

View File

@ -66,6 +66,8 @@ class QSK_EXPORT QskMenu : public QskPopup
QRectF cellRect( int index ) const;
int indexAtPosition( const QPointF& ) const;
Q_INVOKABLE int exec();
Q_SIGNALS:
void cascadingChanged( bool );
void originChanged( const QPointF& );