3
0

QtViewPaneEffects.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "QtViewPaneEffects.h"
  9. #include <AzCore/std/smart_ptr/unique_ptr.h>
  10. #include <QGraphicsEffect>
  11. #include <QWidget>
  12. #include <QVariant>
  13. namespace AzQtComponents
  14. {
  15. static const char* s_viewPaneManagerDisableEffectPropertyName = "QtViewPaneManagerDisableEffect";
  16. static const float s_disabledPaneOpacity = 0.4f;
  17. // put the widget into a grayed out state to indicate it cannot be interacted with
  18. static void DisableViewPaneDisabledGraphicsEffect(QWidget* widget)
  19. {
  20. // only remove effects from widgets that we added them to
  21. QGraphicsEffect* effect = widget->graphicsEffect();
  22. if (effect != nullptr && !effect->property(s_viewPaneManagerDisableEffectPropertyName).isNull())
  23. {
  24. widget->setGraphicsEffect(nullptr);
  25. }
  26. }
  27. // force the widget to be repainted
  28. static void ForceWidgetRedraw(QWidget* widget)
  29. {
  30. widget->hide();
  31. widget->resize(widget->size());
  32. widget->show();
  33. }
  34. // restore the widget to its normal state to show it can now be interacted with
  35. static void EnableViewPaneDisabledGraphicsEffect(QWidget* widget)
  36. {
  37. // only apply effects to widgets that didn't have them already...
  38. if (widget->graphicsEffect() == nullptr)
  39. {
  40. auto effect = AZStd::make_unique<QGraphicsOpacityEffect>();
  41. effect->setOpacity(s_disabledPaneOpacity);
  42. // flag this as our effect so we can get rid of it later
  43. effect->setProperty(s_viewPaneManagerDisableEffectPropertyName, true);
  44. widget->setGraphicsEffect(effect.release());
  45. // ensure the widget is redrawn with the updated graphical effect
  46. ForceWidgetRedraw(widget);
  47. }
  48. }
  49. void SetWidgetInteractEnabled(QWidget* widget, const bool on)
  50. {
  51. widget->setEnabled(on);
  52. if (on)
  53. {
  54. DisableViewPaneDisabledGraphicsEffect(widget);
  55. }
  56. else
  57. {
  58. EnableViewPaneDisabledGraphicsEffect(widget);
  59. }
  60. }
  61. } // namespace AzQtComponents