WaitProgress.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "WaitProgress.h"
  10. #include "MainWindow.h" // for MainWindow
  11. #include "CryEdit.h" // for EditorIdleProcessingBus
  12. #include "MainStatusBar.h" // for MainStatusBar
  13. #include <QProgressBar>
  14. bool CWaitProgress::m_bInProgressNow = false;
  15. CWaitProgress::CWaitProgress(const QString& lpszText, bool bStart)
  16. : m_strText(lpszText)
  17. , m_bStarted(false)
  18. , m_progressBar(nullptr)
  19. {
  20. m_bIgnore = false;
  21. if (bStart)
  22. {
  23. Start();
  24. }
  25. }
  26. CWaitProgress::~CWaitProgress()
  27. {
  28. if (m_bStarted)
  29. {
  30. Stop();
  31. }
  32. }
  33. void CWaitProgress::Start()
  34. {
  35. if (m_bStarted)
  36. {
  37. Stop();
  38. }
  39. if (m_bInProgressNow)
  40. {
  41. // Do not affect previously started progress bar.
  42. m_bIgnore = true;
  43. m_bStarted = false;
  44. return;
  45. }
  46. // display text in the status bar
  47. GetIEditor()->SetStatusText(m_strText);
  48. // switch on wait cursor
  49. qApp->setOverrideCursor(Qt::BusyCursor);
  50. m_bStarted = true;
  51. m_percent = 0;
  52. // because Idle Processing was constantly adding new events that made the event loop in Step() spin forever...
  53. EditorIdleProcessingBus::Broadcast(&EditorIdleProcessing::DisableIdleProcessing);
  54. }
  55. void CWaitProgress::Stop()
  56. {
  57. if (!m_bStarted)
  58. {
  59. return;
  60. }
  61. delete m_progressBar;
  62. m_progressBar = nullptr;
  63. // switch off wait cursor
  64. qApp->restoreOverrideCursor();
  65. m_bInProgressNow = false;
  66. m_bStarted = false;
  67. EditorIdleProcessingBus::Broadcast(&EditorIdleProcessing::EnableIdleProcessing);
  68. }
  69. bool CWaitProgress::Step(int nPercentage)
  70. {
  71. if (m_bIgnore)
  72. {
  73. return true;
  74. }
  75. if (!m_bStarted)
  76. {
  77. Start();
  78. }
  79. if (m_percent == nPercentage)
  80. {
  81. return true;
  82. }
  83. m_percent = nPercentage;
  84. if (nPercentage >= 0)
  85. {
  86. if (nPercentage > 100)
  87. {
  88. nPercentage = 100;
  89. }
  90. // create or update a progress control in the status bar
  91. if (!m_progressBar)
  92. {
  93. CreateProgressControl();
  94. }
  95. if (m_progressBar)
  96. {
  97. m_progressBar->setValue(nPercentage);
  98. }
  99. }
  100. // Use the oportunity to process windows messages here.
  101. static const AZ::u64 TIMEOUT_MS = 1;
  102. qApp->processEvents(QEventLoop::AllEvents, TIMEOUT_MS);
  103. return true;
  104. }
  105. void CWaitProgress::SetText(const QString& lpszText)
  106. {
  107. if (m_bIgnore)
  108. {
  109. return;
  110. }
  111. m_strText = lpszText;
  112. GetIEditor()->SetStatusText(m_strText);
  113. }
  114. void CWaitProgress::CreateProgressControl()
  115. {
  116. assert(m_progressBar == nullptr);
  117. MainStatusBar* statusBar = MainWindow::instance()->StatusBar();
  118. m_progressBar = new QProgressBar();
  119. m_progressBar->setRange(0, 100);
  120. statusBar->insertWidget(1, m_progressBar, 250);
  121. m_bInProgressNow = true;
  122. }