| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2009 Werner Mayer <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QElapsedTimer> |
| 26 |
#include <QMessageBox> |
| 27 |
#include <QPushButton> |
| 28 |
#include <QThread> |
| 29 |
#include <QTime> |
| 30 |
|
| 31 |
|
| 32 |
#include "ProgressDialog.h" |
| 33 |
#include "MainWindow.h" |
| 34 |
|
| 35 |
|
| 36 |
using namespace Gui; |
| 37 |
|
| 38 |
namespace Gui |
| 39 |
{ |
| 40 |
struct SequencerDialogPrivate |
| 41 |
{ |
| 42 |
ProgressDialog* dlg; |
| 43 |
QElapsedTimer measureTime; |
| 44 |
QElapsedTimer progressTime; |
| 45 |
QString text; |
| 46 |
bool guiThread; |
| 47 |
bool canabort; |
| 48 |
}; |
| 49 |
} // namespace Gui |
| 50 |
|
| 51 |
SequencerDialog* SequencerDialog::_pclSingleton = nullptr; |
| 52 |
|
| 53 |
SequencerDialog* SequencerDialog::instance() |
| 54 |
{ |
| 55 |
// not initialized? |
| 56 |
if (!_pclSingleton) { |
| 57 |
_pclSingleton = new SequencerDialog(); |
| 58 |
} |
| 59 |
return _pclSingleton; |
| 60 |
} |
| 61 |
|
| 62 |
SequencerDialog::SequencerDialog() |
| 63 |
{ |
| 64 |
d = new SequencerDialogPrivate; |
| 65 |
d->dlg = new ProgressDialog(this, getMainWindow()); |
| 66 |
d->dlg->reset(); // stops the timer to force showing the dialog |
| 67 |
d->dlg->hide(); |
| 68 |
d->guiThread = true; |
| 69 |
d->canabort = false; |
| 70 |
} |
| 71 |
|
| 72 |
SequencerDialog::~SequencerDialog() |
| 73 |
{ |
| 74 |
delete d; |
| 75 |
} |
| 76 |
|
| 77 |
void SequencerDialog::pause() |
| 78 |
{} |
| 79 |
|
| 80 |
void SequencerDialog::resume() |
| 81 |
{} |
| 82 |
|
| 83 |
void SequencerDialog::startStep() |
| 84 |
{ |
| 85 |
QThread* currentThread = QThread::currentThread(); |
| 86 |
QThread* thr = d->dlg->thread(); // this is the main thread |
| 87 |
if (thr != currentThread) { |
| 88 |
d->guiThread = false; |
| 89 |
QMetaObject::invokeMethod( |
| 90 |
d->dlg, |
| 91 |
"setRangeEx", |
| 92 |
Qt::QueuedConnection, |
| 93 |
Q_ARG(int, 0), |
| 94 |
Q_ARG(int, (int)nTotalSteps) |
| 95 |
); |
| 96 |
d->dlg->setModal(false); |
| 97 |
if (nTotalSteps == 0) { |
| 98 |
d->progressTime.start(); |
| 99 |
} |
| 100 |
|
| 101 |
d->measureTime.start(); |
| 102 |
QMetaObject::invokeMethod(d->dlg, "setValueEx", Qt::QueuedConnection, Q_ARG(int, 0)); |
| 103 |
QMetaObject::invokeMethod(d->dlg, "aboutToShow", Qt::QueuedConnection); |
| 104 |
} |
| 105 |
else { |
| 106 |
d->guiThread = true; |
| 107 |
d->dlg->setRangeEx(0, (int)nTotalSteps); |
| 108 |
d->dlg->setModal(true); |
| 109 |
if (nTotalSteps == 0) { |
| 110 |
d->progressTime.start(); |
| 111 |
} |
| 112 |
|
| 113 |
d->measureTime.start(); |
| 114 |
d->dlg->setValueEx(0); |
| 115 |
d->dlg->aboutToShow(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void SequencerDialog::nextStep(bool canAbort) |
| 120 |
{ |
| 121 |
d->canabort = canAbort; |
| 122 |
QThread* currentThread = QThread::currentThread(); |
| 123 |
QThread* thr = d->dlg->thread(); // this is the main thread |
| 124 |
if (thr != currentThread) { |
| 125 |
if (wasCanceled() && canAbort) { |
| 126 |
abort(); |
| 127 |
} |
| 128 |
else { |
| 129 |
setValue((int)nProgress + 1); |
| 130 |
} |
| 131 |
} |
| 132 |
else { |
| 133 |
if (wasCanceled() && canAbort) { |
| 134 |
// restore cursor |
| 135 |
pause(); |
| 136 |
bool ok = d->dlg->canAbort(); |
| 137 |
// continue and show up wait cursor if needed |
| 138 |
resume(); |
| 139 |
|
| 140 |
// force to abort the operation |
| 141 |
if (ok) { |
| 142 |
abort(); |
| 143 |
} |
| 144 |
else { |
| 145 |
rejectCancel(); |
| 146 |
setValue((int)nProgress + 1); |
| 147 |
} |
| 148 |
} |
| 149 |
else { |
| 150 |
setValue((int)nProgress + 1); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void SequencerDialog::setProgress(size_t step) |
| 156 |
{ |
| 157 |
QThread* currentThread = QThread::currentThread(); |
| 158 |
QThread* thr = d->dlg->thread(); // this is the main thread |
| 159 |
if (thr != currentThread) { |
| 160 |
QMetaObject::invokeMethod(d->dlg, "show", Qt::QueuedConnection); |
| 161 |
} |
| 162 |
else { |
| 163 |
d->dlg->show(); |
| 164 |
} |
| 165 |
|
| 166 |
setValue((int)step); |
| 167 |
} |
| 168 |
|
| 169 |
void SequencerDialog::setValue(int step) |
| 170 |
{ |
| 171 |
QThread* currentThread = QThread::currentThread(); |
| 172 |
QThread* thr = d->dlg->thread(); // this is the main thread |
| 173 |
// if number of total steps is unknown then increment only by one |
| 174 |
if (nTotalSteps == 0) { |
| 175 |
int elapsed = d->progressTime.elapsed(); |
| 176 |
// allow an update every 500 milliseconds only |
| 177 |
if (elapsed > 500) { |
| 178 |
d->progressTime.restart(); |
| 179 |
if (thr != currentThread) { |
| 180 |
QMetaObject::invokeMethod( |
| 181 |
d->dlg, |
| 182 |
"setValueEx", |
| 183 |
Qt::/*Blocking*/ QueuedConnection, |
| 184 |
Q_ARG(int, d->dlg->value() + 1) |
| 185 |
); |
| 186 |
} |
| 187 |
else { |
| 188 |
d->dlg->setValueEx(d->dlg->value() + 1); |
| 189 |
qApp->processEvents(); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
else { |
| 194 |
if (thr != currentThread) { |
| 195 |
QMetaObject::invokeMethod( |
| 196 |
d->dlg, |
| 197 |
"setValueEx", |
| 198 |
Qt::/*Blocking*/ QueuedConnection, |
| 199 |
Q_ARG(int, step) |
| 200 |
); |
| 201 |
if (d->dlg->isVisible()) { |
| 202 |
showRemainingTime(); |
| 203 |
} |
| 204 |
} |
| 205 |
else { |
| 206 |
d->dlg->setValueEx(step); |
| 207 |
if (d->dlg->isVisible()) { |
| 208 |
showRemainingTime(); |
| 209 |
} |
| 210 |
qApp->processEvents(); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void SequencerDialog::showRemainingTime() |
| 216 |
{ |
| 217 |
QThread* currentThread = QThread::currentThread(); |
| 218 |
QThread* thr = d->dlg->thread(); // this is the main thread |
| 219 |
|
| 220 |
int elapsed = d->measureTime.elapsed(); |
| 221 |
int progress = d->dlg->value(); |
| 222 |
int totalSteps = d->dlg->maximum() - d->dlg->minimum(); |
| 223 |
|
| 224 |
QString txt = d->text; |
| 225 |
// More than 5 percent complete or more than 5 secs have elapsed. |
| 226 |
if (progress * 20 > totalSteps || elapsed > 5000) { |
| 227 |
int rest = (int)((double)totalSteps / progress * elapsed) - elapsed; |
| 228 |
|
| 229 |
// more than 1 secs have elapsed and at least 100 ms are remaining |
| 230 |
if (elapsed > 1000 && rest > 100) { |
| 231 |
QTime time(0, 0, 0); |
| 232 |
time = time.addSecs(rest / 1000); |
| 233 |
QString remain = Gui::ProgressDialog::tr("Remaining: %1").arg(time.toString()); |
| 234 |
QString status = QStringLiteral("%1\t[%2]").arg(txt, remain); |
| 235 |
|
| 236 |
if (thr != currentThread) { |
| 237 |
QMetaObject::invokeMethod( |
| 238 |
d->dlg, |
| 239 |
"setLabelText", |
| 240 |
Qt::/*Blocking*/ QueuedConnection, |
| 241 |
Q_ARG(QString, status) |
| 242 |
); |
| 243 |
} |
| 244 |
else { |
| 245 |
d->dlg->setLabelText(status); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void SequencerDialog::resetData() |
| 252 |
{ |
| 253 |
QThread* currentThread = QThread::currentThread(); |
| 254 |
QThread* thr = d->dlg->thread(); // this is the main thread |
| 255 |
if (thr != currentThread) { |
| 256 |
QMetaObject::invokeMethod(d->dlg, "resetEx", Qt::QueuedConnection); |
| 257 |
QMetaObject::invokeMethod(d->dlg, "hide", Qt::QueuedConnection); |
| 258 |
QMetaObject::invokeMethod( |
| 259 |
d->dlg, |
| 260 |
"setLabelText", |
| 261 |
Qt::/*Blocking*/ QueuedConnection, |
| 262 |
Q_ARG(QString, QString()) |
| 263 |
); |
| 264 |
} |
| 265 |
else { |
| 266 |
d->dlg->resetEx(); |
| 267 |
// Note: Under Qt 4.1.4 this forces to run QWindowsStyle::eventFilter() twice |
| 268 |
// handling the same event thus a warning is printed. Possibly, this is a bug |
| 269 |
// in Qt. The message is QEventDispatcherUNIX::unregisterTimer: invalid argument. |
| 270 |
d->dlg->hide(); |
| 271 |
d->dlg->setLabelText(QString()); |
| 272 |
} |
| 273 |
|
| 274 |
SequencerBase::resetData(); |
| 275 |
} |
| 276 |
|
| 277 |
bool SequencerDialog::canAbort() const |
| 278 |
{ |
| 279 |
return d->canabort; |
| 280 |
} |
| 281 |
|
| 282 |
void SequencerDialog::abort() |
| 283 |
{ |
| 284 |
// resets |
| 285 |
resetData(); |
| 286 |
Base::AbortException exc("User aborted"); |
| 287 |
throw exc; |
| 288 |
} |
| 289 |
|
| 290 |
void SequencerDialog::setText(const char* pszTxt) |
| 291 |
{ |
| 292 |
QThread* currentThread = QThread::currentThread(); |
| 293 |
QThread* thr = d->dlg->thread(); // this is the main thread |
| 294 |
|
| 295 |
// set label text of the dialog |
| 296 |
d->text = pszTxt ? QString::fromUtf8(pszTxt) : QLatin1String(""); |
| 297 |
if (thr != currentThread) { |
| 298 |
QMetaObject::invokeMethod( |
| 299 |
d->dlg, |
| 300 |
"setLabelText", |
| 301 |
Qt::/*Blocking*/ QueuedConnection, |
| 302 |
Q_ARG(QString, d->text) |
| 303 |
); |
| 304 |
} |
| 305 |
else { |
| 306 |
d->dlg->setLabelText(d->text); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
bool SequencerDialog::isBlocking() const |
| 311 |
{ |
| 312 |
return d->guiThread; |
| 313 |
} |
| 314 |
|
| 315 |
// ------------------------------------------------------- |
| 316 |
|
| 317 |
/* TRANSLATOR Gui::ProgressDialog */ |
| 318 |
|
| 319 |
ProgressDialog::ProgressDialog(SequencerDialog* s, QWidget* parent) |
| 320 |
: QProgressDialog(parent, Qt::FramelessWindowHint) |
| 321 |
, sequencer(s) |
| 322 |
{ |
| 323 |
#ifdef QT_WINEXTRAS_LIB |
| 324 |
m_taskbarButton = nullptr; |
| 325 |
m_taskbarButton = nullptr; |
| 326 |
#endif |
| 327 |
connect(this, &QProgressDialog::canceled, this, &ProgressDialog::onCancel); |
| 328 |
} |
| 329 |
|
| 330 |
ProgressDialog::~ProgressDialog() = default; |
| 331 |
|
| 332 |
void ProgressDialog::onCancel() |
| 333 |
{ |
| 334 |
sequencer->tryToCancel(); |
| 335 |
} |
| 336 |
|
| 337 |
bool ProgressDialog::canAbort() const |
| 338 |
{ |
| 339 |
auto ret = QMessageBox::question( |
| 340 |
getMainWindow(), |
| 341 |
tr("Aborting"), |
| 342 |
tr("Abort the operation?"), |
| 343 |
QMessageBox::Yes | QMessageBox::No, |
| 344 |
QMessageBox::No |
| 345 |
); |
| 346 |
|
| 347 |
return (ret == QMessageBox::Yes) ? true : false; |
| 348 |
} |
| 349 |
|
| 350 |
void ProgressDialog::showEvent(QShowEvent* ev) |
| 351 |
{ |
| 352 |
QPushButton* btn = findChild<QPushButton*>(); |
| 353 |
if (btn) { |
| 354 |
btn->setEnabled(sequencer->canAbort()); |
| 355 |
} |
| 356 |
QProgressDialog::showEvent(ev); |
| 357 |
} |
| 358 |
|
| 359 |
void ProgressDialog::hideEvent(QHideEvent* ev) |
| 360 |
{ |
| 361 |
QProgressDialog::hideEvent(ev); |
| 362 |
} |
| 363 |
|
| 364 |
void ProgressDialog::resetEx() |
| 365 |
{ |
| 366 |
QProgressDialog::reset(); |
| 367 |
#ifdef QT_WINEXTRAS_LIB |
| 368 |
setupTaskBarProgress(); |
| 369 |
m_taskbarProgress->reset(); |
| 370 |
#endif |
| 371 |
} |
| 372 |
|
| 373 |
void ProgressDialog::setRangeEx(int minimum, int maximum) |
| 374 |
{ |
| 375 |
QProgressDialog::setRange(minimum, maximum); |
| 376 |
#ifdef QT_WINEXTRAS_LIB |
| 377 |
setupTaskBarProgress(); |
| 378 |
m_taskbarProgress->setRange(minimum, maximum); |
| 379 |
#endif |
| 380 |
} |
| 381 |
|
| 382 |
void ProgressDialog::setValueEx(int value) |
| 383 |
{ |
| 384 |
QProgressDialog::setValue(value); |
| 385 |
#ifdef QT_WINEXTRAS_LIB |
| 386 |
setupTaskBarProgress(); |
| 387 |
m_taskbarProgress->setValue(value); |
| 388 |
#endif |
| 389 |
} |
| 390 |
|
| 391 |
void ProgressDialog::aboutToShow() |
| 392 |
{ |
| 393 |
#ifdef QT_WINEXTRAS_LIB |
| 394 |
setupTaskBarProgress(); |
| 395 |
m_taskbarProgress->show(); |
| 396 |
#endif |
| 397 |
} |
| 398 |
|
| 399 |
void ProgressDialog::aboutToHide() |
| 400 |
{ |
| 401 |
hide(); |
| 402 |
#ifdef QT_WINEXTRAS_LIB |
| 403 |
setupTaskBarProgress(); |
| 404 |
m_taskbarProgress->hide(); |
| 405 |
#endif |
| 406 |
} |
| 407 |
|
| 408 |
#ifdef QT_WINEXTRAS_LIB |
| 409 |
void ProgressDialog::setupTaskBarProgress() |
| 410 |
{ |
| 411 |
if (!m_taskbarButton || !m_taskbarProgress) { |
| 412 |
m_taskbarButton = new QWinTaskbarButton(this); |
| 413 |
m_taskbarButton->setWindow(MainWindow::getInstance()->windowHandle()); |
| 414 |
// m_myButton->setOverlayIcon(QIcon("")); |
| 415 |
|
| 416 |
m_taskbarProgress = m_taskbarButton->progress(); |
| 417 |
} |
| 418 |
} |
| 419 |
#endif |
| 420 |
|
| 421 |
#include "moc_ProgressDialog.cpp" |