QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskDialogWindow.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskDialogWindow.h"
7#include "QskDialogButtonBox.h"
8#include "QskLinearBox.h"
9#include "QskPushButton.h"
10#include "QskQuick.h"
11#include "QskEvent.h"
12
13#include <qeventloop.h>
14#include <qpointer.h>
15
16class QskDialogWindow::PrivateData
17{
18 public:
19 PrivateData()
20 : result( QskDialog::Rejected )
21 , inDone( false )
22 {
23 }
24
25 QskDialog::Actions actions = QskDialog::NoAction;
26
27 QPointer< QQuickItem > dialogContentItem;
28 QskDialogButtonBox* buttonBox = nullptr;
29 QskLinearBox* layoutBox;
30
31 QskDialog::DialogCode result : 1;
32 bool inDone : 1; // flag blocking recursive done calls
33};
34
35QskDialogWindow::QskDialogWindow( QWindow* parent )
36 : Inherited( parent )
37 , m_data( new PrivateData() )
38{
39 setFlags( Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint |
40 Qt::WindowSystemMenuHint | Qt::WindowContextHelpButtonHint );
41
42 // doing the layout manually instead ???
43 setAutoLayoutChildren( true );
44 m_data->layoutBox = new QskLinearBox( Qt::Vertical, contentItem() );
45 m_data->layoutBox->setPadding( 5 );
46 m_data->layoutBox->setPanel( true ); // for a themed background
47}
48
49QskDialogWindow::~QskDialogWindow()
50{
51}
52
53void QskDialogWindow::setDialogActions( QskDialog::Actions actions )
54{
55 if ( m_data->actions == actions )
56 return;
57
58 m_data->actions = actions;
59
60 if ( actions == QskDialog::NoAction )
61 {
62 delete m_data->buttonBox;
63 m_data->buttonBox = nullptr;
64 }
65 else
66 {
67 if ( m_data->buttonBox == nullptr )
68 {
69 m_data->buttonBox = createButtonBox();
70
71 if ( m_data->buttonBox )
72 {
73 m_data->layoutBox->addItem( m_data->buttonBox );
74
75 connect( m_data->buttonBox, &QskDialogButtonBox::accepted,
76 this, &QskDialogWindow::accept, Qt::UniqueConnection );
77
78 connect( m_data->buttonBox, &QskDialogButtonBox::rejected,
79 this, &QskDialogWindow::reject, Qt::UniqueConnection );
80 }
81 }
82
83 if ( m_data->buttonBox )
84 m_data->buttonBox->setActions( actions );
85 }
86}
87
88QskDialog::Actions QskDialogWindow::dialogActions() const
89{
90 if ( m_data->buttonBox )
91 return m_data->buttonBox->actions();
92
93 return QskDialog::NoAction;
94}
95
96void QskDialogWindow::setDialogContentItem( QQuickItem* item )
97{
98 if ( item == m_data->dialogContentItem )
99 return;
100
101 if ( m_data->dialogContentItem )
102 {
103 m_data->layoutBox->removeAt( 0 );
104 if ( m_data->dialogContentItem->parent() == m_data->layoutBox )
105 delete m_data->dialogContentItem;
106 }
107
108 m_data->dialogContentItem = item;
109
110 if ( item )
111 m_data->layoutBox->insertItem( 0, item );
112}
113
114QQuickItem* QskDialogWindow::dialogContentItem() const
115{
116 return m_data->dialogContentItem;
117}
118
119void QskDialogWindow::setDefaultButton( QskPushButton* button )
120{
121 if ( !qskIsAncestorOf( m_data->buttonBox, button ) )
122 {
123#if defined( QT_DEBUG )
124 qWarning( "Only buttons of the QskDialogButtonBox can be the default button." );
125#endif
126 return;
127 }
128
129 m_data->buttonBox->setDefaultButton( button );
130}
131
132QskPushButton* QskDialogWindow::defaultButton() const
133{
134 if ( m_data->buttonBox == nullptr )
135 return nullptr;
136
137 return m_data->buttonBox->defaultButton();
138}
139
140void QskDialogWindow::setDefaultDialogAction( QskDialog::Action action )
141{
142 QskPushButton* button = nullptr;
143
144 if ( m_data->buttonBox )
145 button = m_data->buttonBox->button( action );
146
147 setDefaultButton( button );
148}
149
150QskDialogButtonBox* QskDialogWindow::buttonBox()
151{
152 return m_data->buttonBox;
153}
154
155const QskDialogButtonBox* QskDialogWindow::buttonBox() const
156{
157 return m_data->buttonBox;
158}
159
160QskDialog::Action QskDialogWindow::clickedAction() const
161{
162 if ( m_data->buttonBox )
163 return m_data->buttonBox->clickedAction();
164
165 return QskDialog::NoAction;
166}
167
168void QskDialogWindow::setResult( QskDialog::DialogCode result )
169{
170 m_data->result = result;
171}
172
173QskDialog::DialogCode QskDialogWindow::result() const
174{
175 return m_data->result;
176}
177
178QskDialog::DialogCode QskDialogWindow::exec()
179{
180 const bool deleteOnClose = this->deleteOnClose();
181 setDeleteOnClose( false );
182
183 show();
184
185 QEventLoop eventLoop;
186
187 connect( this, &QskDialogWindow::finished, &eventLoop, &QEventLoop::quit );
188 ( void ) eventLoop.exec( QEventLoop::DialogExec );
189
190 const auto result = static_cast< QskDialog::DialogCode >( m_data->result );
191
192 if ( deleteOnClose )
193 delete this;
194
195 return result;
196}
197
198void QskDialogWindow::done( QskDialog::DialogCode result )
199{
200 m_data->result = result;
201
202 Q_EMIT finished( result );
203
204 if ( result == QskDialog::Accepted )
205 Q_EMIT accepted();
206 else
207 Q_EMIT rejected();
208
209 const QPointer< QObject > that = this;
210
211 m_data->inDone = true;
212
213 close();
214
215 if ( that )
216 {
217 // we might be deleted during close
218 m_data->inDone = false;
219 }
220}
221
222void QskDialogWindow::accept()
223{
224 done( QskDialog::Accepted );
225}
226
227void QskDialogWindow::reject()
228{
229 done( QskDialog::Rejected );
230}
231
232bool QskDialogWindow::event( QEvent* event )
233{
234 if ( event->type() == QEvent::Close )
235 {
236 if ( !m_data->inDone )
237 done( QskDialog::Rejected );
238 }
239
240 return Inherited::event( event );
241}
242
243void QskDialogWindow::keyPressEvent( QKeyEvent* event )
244{
245 if ( m_data->buttonBox &&
246 QskDialogButtonBox::isDefaultButtonKeyEvent( event ) )
247 {
248 auto button = m_data->buttonBox->defaultButton();
249 if ( button && button->isEnabled() )
250 button->click();
251 }
252
253 if ( qskIsStandardKeyInput( event, QKeySequence::Cancel ) )
254 {
255 // using shortcuts instead ???
256
257 reject();
258 return;
259 }
260
261 Inherited::keyPressEvent( event );
262}
263
264QskDialogButtonBox* QskDialogWindow::createButtonBox()
265{
266 return new QskDialogButtonBox();
267}
268
269#include "moc_QskDialogWindow.cpp"
Layout stringing items in rows and columns.