QskMenu::exec added
This commit is contained in:
parent
89a2a395fd
commit
9cef7705d8
@ -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
|
||||
};
|
||||
|
@ -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"
|
||||
|
@ -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& );
|
||||
|
Loading…
x
Reference in New Issue
Block a user