/
coreguiutils.cpp.inl
482 lines (345 loc) · 15.2 KB
/
coreguiutils.cpp.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// Core GUI utilities
//==============================================================================
static const auto SettingsPosition = QStringLiteral("Position");
static const auto SettingsSize = QStringLiteral("Size");
//==============================================================================
Dialog::Dialog(QSettings *pSettings, QWidget *pParent) :
QDialog(pParent),
mSettings(pSettings)
{
}
//==============================================================================
Dialog::Dialog(QWidget *pParent) :
QDialog(pParent),
mSettings(0)
{
}
//==============================================================================
void Dialog::resizeEvent(QResizeEvent *pEvent)
{
// Default handling of the event
QDialog::resizeEvent(pEvent);
// Set our minimum size
setMinimumSize(minimumWidgetSize(this));
}
//==============================================================================
int Dialog::exec()
{
// Retrieve our position and size, if possible
if (mSettings) {
QPoint position = mSettings->value(SettingsPosition).toPoint();
QSize size = mSettings->value(SettingsSize).toSize();
if (!position.isNull() && !size.isNull()) {
move(position);
resize(size);
}
}
// Execute ourselves
int res = QDialog::exec();
// Keep track of our position and size, if possible
if (mSettings) {
mSettings->setValue(SettingsPosition, pos());
mSettings->setValue(SettingsSize, size());
}
// Return the result of our execution
return res;
}
//==============================================================================
int Dialog::exec(QSettings *pSettings)
{
// Keep track of the given settings and execute ourselves
mSettings = pSettings;
return exec();
}
//==============================================================================
bool Dialog::hasPositionAndSize()
{
// Return whether we already have a position and size, if possible
return mSettings?
!mSettings->value(SettingsPosition).toPoint().isNull()
&& !mSettings->value(SettingsSize).toSize().isNull():
false;
}
//==============================================================================
StyledItemDelegate::StyledItemDelegate(QObject *pParent) :
QStyledItemDelegate(pParent)
{
}
//==============================================================================
QSize StyledItemDelegate::sizeHint(const QStyleOptionViewItem &pOption,
const QModelIndex &pIndex) const
{
// Slightly reduce our height, on Windows and macOS, if possible and if we
// have an icon
QSize res = QStyledItemDelegate::sizeHint(pOption, pIndex);
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
static const QSize ZeroSize = QSize(0, 0);
static const QSize SmallSize = QSize(0, 2);
const QStandardItemModel *standardItemModel = qobject_cast<const QStandardItemModel *>(pIndex.model());
if (standardItemModel) {
QStandardItem *item = standardItemModel->itemFromIndex(pIndex);
res -= (item && !item->icon().isNull())?SmallSize:ZeroSize;
}
const QFileSystemModel *fileSystemModel = qobject_cast<const QFileSystemModel *>(pIndex.model());
if (fileSystemModel)
res -= SmallSize;
#endif
return res;
}
//==============================================================================
QMainWindow * mainWindow()
{
// Retrieve and return our main window
static bool firstTime = true;
static QMainWindow *res = 0;
if (firstTime) {
foreach (QWidget *widget, qApp->topLevelWidgets()) {
if (widget->inherits("OpenCOR::MainWindow")) {
res = qobject_cast<QMainWindow *>(widget);
break;
}
}
firstTime = false;
}
return res;
}
//==============================================================================
bool aboutToQuit()
{
// Return whether we are about to quit
return qApp->property("OpenCOR::aboutToQuit()").toBool();
}
//==============================================================================
QSize minimumWidgetSize(QWidget *pWidget)
{
// Determine the minimum size of the given widget
// Note: this is based on QWidgetPrivate::adjustedSize()...
if (!pWidget)
return QSize();
QSize minimumSize = pWidget->sizeHint();
if (pWidget->isWindow()) {
Qt::Orientations expandingDirections;
QLayout *layout = pWidget->layout();
if (layout) {
if (layout->hasHeightForWidth())
minimumSize.setHeight(layout->totalHeightForWidth(minimumSize.width()));
expandingDirections = layout->expandingDirections();
} else {
if (pWidget->sizePolicy().hasHeightForWidth())
minimumSize.setHeight(pWidget->heightForWidth(minimumSize.width()));
expandingDirections = pWidget->sizePolicy().expandingDirections();
}
if (expandingDirections & Qt::Horizontal)
minimumSize.setWidth(qMax(minimumSize.width(), 200));
if (expandingDirections & Qt::Vertical)
minimumSize.setHeight(qMax(minimumSize.height(), 100));
QRect screen = QApplication::desktop()->screenGeometry(pWidget->pos());
minimumSize.setWidth(qMin(minimumSize.width(), screen.width()*2/3));
minimumSize.setHeight(qMin(minimumSize.height(), screen.height()*2/3));
}
if (!minimumSize.isValid()) {
QRect childrenRect = pWidget->childrenRect();
if (!childrenRect.isNull())
minimumSize = childrenRect.size()+QSize(2*childrenRect.x(), 2*childrenRect.y());
}
return minimumSize;
}
//==============================================================================
void adjustWidgetSize(QWidget *pWidget)
{
// Retrieve the minimum size of the given , and resize and recenter it, if
// needed
QSize minimumSize = minimumWidgetSize(pWidget);
if (!minimumSize.isValid())
return;
QSize oldSize = pWidget->size();
QSize newSize = QSize(qMax(minimumSize.width(), oldSize.width()),
qMax(minimumSize.height(), oldSize.height()));
if (newSize != oldSize) {
// Note: if the given widget is not visible then we only resize it
// otherwise we also recenter it since being visible we are sure
// that it has an original position to recenter against...
if (pWidget->isVisible()) {
pWidget->setGeometry(pWidget->geometry().x()-((newSize.width()-oldSize.width()) >> 1),
pWidget->geometry().y()-((newSize.height()-oldSize.height()) >> 1),
newSize.width(), newSize.height());
} else {
pWidget->resize(newSize);
}
}
}
//==============================================================================
void showEnableAction(QAction *pAction, const bool &pVisible,
const bool &pEnabled)
{
// Show/enable or hide/disable the given action
pAction->setVisible(pVisible);
pAction->setEnabled(pVisible && pEnabled);
}
//==============================================================================
QColor baseColor()
{
// Return the base colour
return qApp->palette().color(QPalette::Base);
}
//==============================================================================
QColor borderColor()
{
// Return the mid colour, which we consider as the colour to be used for a
// 'normal' border
// Note: this is not quite the correct colour, but nonetheless the one that
// is closest to it. Indeed, to use a hidden widget of sorts to
// retrieve the colour of a border works fine on all our supported
// operating systems except Windows 10 (while it works fine on
// previous versions of Windows) where it has a side effect that
// prevents OpenCOR from retrieving the size of its main window. Not
// only that, but we can still see the 'hidden' widget...
return qApp->palette().color(QPalette::Mid);
}
//==============================================================================
QColor highlightColor()
{
// Return the highlight colour
return qApp->palette().color(QPalette::Highlight);
}
//==============================================================================
QColor shadowColor()
{
// Return the shadow colour
return qApp->palette().color(QPalette::Shadow);
}
//==============================================================================
QColor windowColor()
{
// Return the window colour
return qApp->palette().color(QPalette::Window);
}
//==============================================================================
QMessageBox::StandardButton showMessageBox(QWidget *pParent,
const QMessageBox::Icon &pIcon,
const Qt::TextInteractionFlags &pFlags,
const QString &pTitle,
const QString &pText,
const QMessageBox::StandardButtons &pButtons,
const QMessageBox::StandardButton &pDefaultButton)
{
// Create, show and return the result of a message box with the given
// properties
QMessageBox messageBox(pIcon, pTitle, pText, QMessageBox::NoButton, pParent);
messageBox.setTextInteractionFlags(pFlags);
QDialogButtonBox *buttonBox = messageBox.findChild<QDialogButtonBox*>();
Q_ASSERT(buttonBox);
uint mask = QMessageBox::FirstButton;
while (mask <= QMessageBox::LastButton) {
uint sb = pButtons & mask;
mask <<= 1;
if (!sb)
continue;
QPushButton *button = messageBox.addButton((QMessageBox::StandardButton)sb);
if (messageBox.defaultButton())
continue;
if ( ( (pDefaultButton == QMessageBox::NoButton)
&& (buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole))
|| ( (pDefaultButton != QMessageBox::NoButton) &&
(sb == uint(pDefaultButton)))) {
messageBox.setDefaultButton(button);
}
}
if (messageBox.exec() == -1)
return QMessageBox::Cancel;
return messageBox.standardButton(messageBox.clickedButton());
}
//==============================================================================
QMessageBox::StandardButton informationMessageBox(const QString &pTitle,
const QString &pText,
const QMessageBox::StandardButtons &pButtons,
const QMessageBox::StandardButton &pDefaultButton)
{
// Return the result of an information message box
return showMessageBox(mainWindow(), QMessageBox::Information,
Qt::LinksAccessibleByMouse, pTitle, pText, pButtons,
pDefaultButton);
}
//==============================================================================
QMessageBox::StandardButton questionMessageBox(const QString &pTitle,
const QString &pText,
const QMessageBox::StandardButtons &pButtons,
const QMessageBox::StandardButton &pDefaultButton)
{
// Return the result of a question message box
return showMessageBox(mainWindow(), QMessageBox::Question,
Qt::LinksAccessibleByMouse, pTitle, pText, pButtons,
pDefaultButton);
}
//==============================================================================
QMessageBox::StandardButton warningMessageBox(const QString &pTitle,
const QString &pText,
const QMessageBox::StandardButtons &pButtons,
const QMessageBox::StandardButton &pDefaultButton)
{
// Return the result of a warning message box
return showMessageBox(mainWindow(), QMessageBox::Warning,
Qt::TextSelectableByMouse|Qt::LinksAccessibleByMouse,
pTitle, pText, pButtons, pDefaultButton);
}
//==============================================================================
QMessageBox::StandardButton criticalMessageBox(const QString &pTitle,
const QString &pText,
const QMessageBox::StandardButtons &pButtons,
const QMessageBox::StandardButton &pDefaultButton)
{
// Return the result of a critical message box
return showMessageBox(mainWindow(), QMessageBox::Critical,
Qt::TextSelectableByMouse|Qt::LinksAccessibleByMouse,
pTitle, pText, pButtons, pDefaultButton);
}
//==============================================================================
void aboutMessageBox(const QString &pTitle, const QString &pText)
{
// Show an about message box
#ifdef Q_OS_MAC
static QPointer<QMessageBox> oldMessageBox;
if (oldMessageBox && !oldMessageBox->text().compare(pText)) {
oldMessageBox->show();
oldMessageBox->raise();
oldMessageBox->activateWindow();
return;
}
#endif
QMessageBox *messageBox = new QMessageBox( pTitle, pText, QMessageBox::Information, 0, 0, 0, mainWindow()
#ifdef Q_OS_MAC
, Qt::WindowTitleHint|Qt::WindowSystemMenuHint
#endif
);
messageBox->setTextInteractionFlags(Qt::LinksAccessibleByMouse);
messageBox->setAttribute(Qt::WA_DeleteOnClose);
QIcon icon = messageBox->windowIcon();
QSize size = icon.actualSize(QSize(64, 64));
messageBox->setIconPixmap(icon.pixmap(size));
#ifdef Q_OS_MAC
oldMessageBox = messageBox;
QDialogButtonBox *buttonBox = messageBox->findChild<QDialogButtonBox*>();
Q_ASSERT(buttonBox);
buttonBox->setCenterButtons(true);
messageBox->show();
#else
messageBox->exec();
#endif
}
//==============================================================================
// End of file
//==============================================================================