/
commonwidget.cpp
219 lines (153 loc) · 5.86 KB
/
commonwidget.cpp
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
/*******************************************************************************
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/>.
*******************************************************************************/
//==============================================================================
// Common widget
//==============================================================================
#include "busywidget.h"
#include "centralwidget.h"
#include "commonwidget.h"
#include "corecliutils.h"
#include "coreguiutils.h"
//==============================================================================
#include <QApplication>
#include <QDesktopWidget>
#include <QDockWidget>
#include <QMainWindow>
#include <QPainter>
#include <QPen>
#include <QSettings>
//==============================================================================
namespace OpenCOR {
namespace Core {
//==============================================================================
CommonWidget::CommonWidget(QWidget *pParent) :
mParent(pParent),
mBusyWidget(0),
mCounter(0)
{
}
//==============================================================================
QSize CommonWidget::defaultSize(const double &pRatio) const
{
// The default size of a widget is a ratio of the available geometry
QRect desktopGeometry = qApp->desktop()->availableGeometry();
return QSize(pRatio*desktopGeometry.width(),
pRatio*desktopGeometry.height());
}
//==============================================================================
void CommonWidget::loadSettings(QSettings *pSettings)
{
Q_UNUSED(pSettings);
// Nothing to do by default...
}
//==============================================================================
void CommonWidget::saveSettings(QSettings *pSettings) const
{
Q_UNUSED(pSettings);
// Nothing to do by default...
}
//==============================================================================
void CommonWidget::retranslateUi()
{
// Nothing to do by default...
}
//==============================================================================
bool CommonWidget::isBusyWidgetVisible() const
{
// Return whether our busy widget is visible, i.e. whether it exists
return mBusyWidget;
}
//==============================================================================
void CommonWidget::doShowBusyWidget(const double &pProgress)
{
// Create and show our new busy widget resized, and then disable our parent
if (++mCounter == 1) {
mBusyWidget = new BusyWidget(mParent, pProgress);
resizeBusyWidget();
if (mParent == centralWidget())
mainWindow()->setEnabled(false);
else
mParent->setEnabled(false);
// Make sure that our busy widget is shown straightaway
// Note: indeed, depending on the operating system (e.g. macOS) and on
// what we do next (e.g. retrieving a remote file), our busy
// widget may or not show straightaway...
QCoreApplication::sendPostedEvents();
QCoreApplication::processEvents();
}
}
//==============================================================================
void CommonWidget::showBusyWidget()
{
// Show a 'normal' busy widget
doShowBusyWidget();
}
//==============================================================================
void CommonWidget::showProgressBusyWidget()
{
// Show a progress busy widget
doShowBusyWidget(0.0);
}
//==============================================================================
void CommonWidget::hideBusyWidget(const bool &pForceHiding)
{
// Make sure that we have a busy widget
if (!mBusyWidget)
return;
// Check whether we want to force hiding our busy widget
if (pForceHiding)
mCounter = 1;
// Enable ourselves (or OpenCOR itself in case we are the central widget)
// and hide our busy widget by deleting it
if (--mCounter == 0) {
if (mParent == centralWidget())
mainWindow()->setEnabled(true);
else
mParent->setEnabled(true);
delete mBusyWidget;
mBusyWidget = 0;
}
}
//==============================================================================
void CommonWidget::resizeBusyWidget()
{
// Make sure that we have a busy widget
if (!mBusyWidget)
return;
// Resize our busy widget
mBusyWidget->resize();
}
//==============================================================================
void CommonWidget::setBusyWidgetProgress(const double &pProgress)
{
// Make sure that we have a busy widget
if (!mBusyWidget)
return;
// Set the progress of our busy widget
mBusyWidget->setProgress(pProgress);
}
//==============================================================================
void CommonWidget::processEvents()
{
// Process events, but only if we or our central widget is not showing our
// or its busy widget
if (!mBusyWidget && !centralWidget()->isBusyWidgetVisible())
QCoreApplication::processEvents();
}
//==============================================================================
} // namespace Core
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================