juce_ProgressBar.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. ProgressBar::ProgressBar (double& progress_)
  22. : progress (progress_),
  23. displayPercentage (true),
  24. lastCallbackTime (0)
  25. {
  26. currentValue = jlimit (0.0, 1.0, progress);
  27. }
  28. ProgressBar::~ProgressBar()
  29. {
  30. }
  31. //==============================================================================
  32. void ProgressBar::setPercentageDisplay (const bool shouldDisplayPercentage)
  33. {
  34. displayPercentage = shouldDisplayPercentage;
  35. repaint();
  36. }
  37. void ProgressBar::setTextToDisplay (const String& text)
  38. {
  39. displayPercentage = false;
  40. displayedMessage = text;
  41. }
  42. void ProgressBar::lookAndFeelChanged()
  43. {
  44. setOpaque (getLookAndFeel().isProgressBarOpaque (*this));
  45. }
  46. void ProgressBar::colourChanged()
  47. {
  48. lookAndFeelChanged();
  49. }
  50. void ProgressBar::paint (Graphics& g)
  51. {
  52. String text;
  53. if (displayPercentage)
  54. {
  55. if (currentValue >= 0 && currentValue <= 1.0)
  56. text << roundToInt (currentValue * 100.0) << '%';
  57. }
  58. else
  59. {
  60. text = displayedMessage;
  61. }
  62. getLookAndFeel().drawProgressBar (g, *this,
  63. getWidth(), getHeight(),
  64. currentValue, text);
  65. }
  66. void ProgressBar::visibilityChanged()
  67. {
  68. if (isVisible())
  69. startTimer (30);
  70. else
  71. stopTimer();
  72. }
  73. void ProgressBar::timerCallback()
  74. {
  75. double newProgress = progress;
  76. const uint32 now = Time::getMillisecondCounter();
  77. const int timeSinceLastCallback = (int) (now - lastCallbackTime);
  78. lastCallbackTime = now;
  79. if (currentValue != newProgress
  80. || newProgress < 0 || newProgress >= 1.0
  81. || currentMessage != displayedMessage)
  82. {
  83. if (currentValue < newProgress
  84. && newProgress >= 0 && newProgress < 1.0
  85. && currentValue >= 0 && currentValue < 1.0)
  86. {
  87. newProgress = jmin (currentValue + 0.0008 * timeSinceLastCallback,
  88. newProgress);
  89. }
  90. currentValue = newProgress;
  91. currentMessage = displayedMessage;
  92. repaint();
  93. }
  94. }
  95. } // namespace juce