7#include "QskDialogButtonBox.h"
9#include "QskMessageSubWindow.h"
10#include "QskMessageWindow.h"
12#include "QskSelectionSubWindow.h"
13#include "QskSelectionWindow.h"
15#include "QskFocusIndicator.h"
17#include <qguiapplication.h>
19#include <qquickwindow.h>
21#include <qpa/qplatformdialoghelper.h>
28 const QskDialog::ActionRole candidates[] =
30 QskDialog::AcceptRole, QskDialog::YesRole,
31 QskDialog::RejectRole, QskDialog::NoRole, QskDialog::DestructiveRole,
32 QskDialog::UserRole, QskDialog::ResetRole,
33 QskDialog::ApplyRole, QskDialog::HelpRole
36 for (
auto role : candidates )
38 const auto& buttons = buttonBox->buttons( role );
39 if ( !buttons.isEmpty() )
40 return buttonBox->action( buttons.first() );
43 return QskDialog::NoAction;
50 focusIndicator->setObjectName( QStringLiteral(
"DialogFocusIndicator" ) );
51 dialogWindow->addItem( focusIndicator );
54 return dialogWindow->exec();
57static QQuickWindow* qskSomeQuickWindow()
62 const auto windows = QGuiApplication::topLevelWindows();
63 for (
auto window : windows )
65 if ( window->isVisible() )
67 if (
auto quickWindow = qobject_cast< QQuickWindow* >( window ) )
75static void qskSetupSubWindow(
76 const QString& title, QskDialog::Actions actions,
79 subWindow->setPopupFlag( QskPopup::DeleteOnClose );
80 subWindow->setModal(
true );
82 subWindow->setWindowTitle( ... );
84 subWindow->setTitle( title );
85 subWindow->setDialogActions( actions );
87 if ( actions != QskDialog::NoAction && defaultAction == QskDialog::NoAction )
88 defaultAction = qskActionCandidate( subWindow->buttonBox() );
90 subWindow->setDefaultDialogAction( defaultAction );
93static void qskSetupWindow(
94 QWindow* transientParent,
const QString& title,
95 QskDialog::Actions actions, QskDialog::Action defaultAction,
98 window->setTransientParent( transientParent );
100 window->setTitle( title );
101 window->setDialogActions( actions );
103 if ( actions != QskDialog::NoAction && defaultAction == QskDialog::NoAction )
104 defaultAction = qskActionCandidate( window->buttonBox() );
106 window->setDefaultDialogAction( defaultAction );
108 window->setModality( transientParent ? Qt::WindowModal : Qt::ApplicationModal );
110 const QSize size = window->sizeConstraint();
112 if ( window->parent() )
114 QRect r( QPoint(), size );
115 r.moveCenter( QRect( QPoint(), window->parent()->size() ).center() );
117 window->setGeometry( r );
120 if ( size.isValid() )
122 window->setFlags( window->flags() | Qt::MSWindowsFixedSizeDialogHint );
123 window->setFixedSize( size );
126 window->setModality( Qt::ApplicationModal );
129static QskDialog::Action qskMessageSubWindow(
130 QQuickWindow* window,
const QString& title,
131 const QString& text, uint priority, QskDialog::Actions actions,
132 QskDialog::Action defaultAction )
135 subWindow->setPriority( priority );
136 subWindow->setText( text );
138 qskSetupSubWindow( title, actions, defaultAction, subWindow );
139 ( void ) subWindow->exec();
141 auto clickedAction = subWindow->clickedAction();
142 if ( clickedAction == QskDialog::NoAction )
145 clickedAction = QskDialog::Cancel;
148 return clickedAction;
151static QskDialog::Action qskMessageWindow(
152 QWindow* transientParent,
const QString& title,
153 const QString& text, uint priority, QskDialog::Actions actions,
154 QskDialog::Action defaultAction )
156 Q_UNUSED( priority );
159 messageWindow.setText( text );
161 qskSetupWindow( transientParent, title, actions, defaultAction, &messageWindow );
162 ( void ) qskExec( &messageWindow );
164 auto clickedAction = messageWindow.clickedAction();
165 if ( clickedAction == QskDialog::NoAction )
168 clickedAction = QskDialog::Cancel;
171 return clickedAction;
174static QString qskSelectSubWindow(
175 QQuickWindow* window,
const QString& title,
176 QskDialog::Actions actions, QskDialog::Action defaultAction,
177 const QStringList& entries,
int selectedRow )
180 subWindow->setEntries( entries );
181 subWindow->setSelectedRow( selectedRow );
183 QString selectedEntry;
185 qskSetupSubWindow( title, actions, defaultAction, subWindow );
186 if ( subWindow->exec() == QskDialog::Accepted )
187 selectedEntry = subWindow->selectedEntry();
189 return selectedEntry;
192static QString qskSelectWindow(
193 QWindow* transientParent,
const QString& title,
194 QskDialog::Actions actions, QskDialog::Action defaultAction,
195 const QStringList& entries,
int selectedRow )
198 window.setEntries( entries );
199 window.setSelectedRow( selectedRow );
201 QString selectedEntry = window.selectedEntry();
203 qskSetupWindow( transientParent, title, actions, defaultAction, &window );
204 if ( qskExec( &window ) == QskDialog::Accepted )
205 selectedEntry = window.selectedEntry();
207 return selectedEntry;
210class QskDialog::PrivateData
213 QPointer< QWindow > transientParent;
214 QskDialog::Policy policy = QskDialog::TopLevelWindow;
217QskDialog::QskDialog()
218 : m_data( new PrivateData )
222QskDialog::~QskDialog()
232void QskDialog::setPolicy( Policy policy )
234 if ( policy != m_data->policy )
236 m_data->policy = policy;
237 Q_EMIT policyChanged();
241QskDialog::Policy QskDialog::policy()
const
243 return m_data->policy;
246void QskDialog::setTransientParent( QWindow* window )
248 if ( m_data->transientParent != window )
250 m_data->transientParent = window;
251 Q_EMIT transientParentChanged();
255QWindow* QskDialog::transientParent()
const
257 return m_data->transientParent;
260QskDialog::Action QskDialog::message(
261 const QString& title,
const QString& text, uint priority,
262 Actions actions, Action defaultAction )
const
264 if ( m_data->policy == EmbeddedBox )
266 auto quickWindow = qobject_cast< QQuickWindow* >( m_data->transientParent );
268 if ( quickWindow ==
nullptr )
269 quickWindow = qskSomeQuickWindow();
273 return qskMessageSubWindow( quickWindow,
274 title, text, priority, actions, defaultAction );
278 return qskMessageWindow( m_data->transientParent,
279 title, text, priority, actions, defaultAction );
282QskDialog::Action QskDialog::information(
283 const QString& title,
const QString& text,
284 Actions actions, Action defaultAction )
const
286 return QskDialog::message( title, text, 0, actions, defaultAction );
289QskDialog::Action QskDialog::question(
290 const QString& title,
const QString& text,
291 Actions actions, Action defaultAction )
const
293 return QskDialog::message( title, text, 0, actions, defaultAction );
296QString QskDialog::select(
const QString& title,
297 const QStringList& entries,
int selectedRow )
const
301 const auto actions = QskDialog::Ok | QskDialog::Cancel;
302 const auto defaultAction = QskDialog::Ok;
305 if ( m_data->policy == EmbeddedBox )
307 auto quickWindow = qobject_cast< QQuickWindow* >( m_data->transientParent );
309 if ( quickWindow ==
nullptr )
310 quickWindow = qskSomeQuickWindow();
314 return qskSelectSubWindow( quickWindow,
315 title, actions, defaultAction, entries, selectedRow );
319 return qskSelectWindow( m_data->transientParent, title,
320 actions, defaultAction, entries, selectedRow );
324QskDialog::ActionRole QskDialog::actionRole( Action action )
326 using Q = QPlatformDialogHelper;
328 const auto role = Q::buttonRole(
static_cast< Q::StandardButton
>( action ) );
329 return static_cast< ActionRole
>( role );
332#include "moc_QskDialog.cpp"