qtmaterialappbar.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "qtmaterialappbar.h"
  2. #include "qtmaterialappbar_p.h"
  3. #include <QtWidgets/QGraphicsDropShadowEffect>
  4. #include <QPainter>
  5. #include "lib/qtmaterialstyle.h"
  6. /*!
  7. * \class QtMaterialAppBarPrivate
  8. * \internal
  9. */
  10. /*!
  11. * \internal
  12. */
  13. QtMaterialAppBarPrivate::QtMaterialAppBarPrivate(QtMaterialAppBar *q)
  14. : q_ptr(q)
  15. {
  16. }
  17. /*!
  18. * \internal
  19. */
  20. QtMaterialAppBarPrivate::~QtMaterialAppBarPrivate()
  21. {
  22. }
  23. /*!
  24. * \internal
  25. */
  26. void QtMaterialAppBarPrivate::init()
  27. {
  28. Q_Q(QtMaterialAppBar);
  29. useThemeColors = true;
  30. QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
  31. effect->setBlurRadius(11);
  32. effect->setColor(QColor(0, 0, 0, 50));
  33. effect->setOffset(0, 3);
  34. q->setGraphicsEffect(effect);
  35. QHBoxLayout *layout = new QHBoxLayout;
  36. q->setLayout(layout);
  37. }
  38. /*!
  39. * \class QtMaterialAppBar
  40. */
  41. QtMaterialAppBar::QtMaterialAppBar(QWidget *parent)
  42. : QWidget(parent),
  43. d_ptr(new QtMaterialAppBarPrivate(this))
  44. {
  45. d_func()->init();
  46. }
  47. QtMaterialAppBar::~QtMaterialAppBar()
  48. {
  49. }
  50. QSize QtMaterialAppBar::sizeHint() const
  51. {
  52. return QSize(-1, 64);
  53. }
  54. void QtMaterialAppBar::paintEvent(QPaintEvent *event)
  55. {
  56. Q_UNUSED(event)
  57. QPainter painter(this);
  58. painter.fillRect(rect(), backgroundColor());
  59. }
  60. void QtMaterialAppBar::setUseThemeColors(bool value)
  61. {
  62. Q_D(QtMaterialAppBar);
  63. if (d->useThemeColors == value) {
  64. return;
  65. }
  66. d->useThemeColors = value;
  67. update();
  68. }
  69. bool QtMaterialAppBar::useThemeColors() const
  70. {
  71. Q_D(const QtMaterialAppBar);
  72. return d->useThemeColors;
  73. }
  74. void QtMaterialAppBar::setForegroundColor(const QColor &color)
  75. {
  76. Q_D(QtMaterialAppBar);
  77. d->foregroundColor = color;
  78. if (d->useThemeColors == true) {
  79. d->useThemeColors = false;
  80. }
  81. update();
  82. }
  83. QColor QtMaterialAppBar::foregroundColor() const
  84. {
  85. Q_D(const QtMaterialAppBar);
  86. if (d->useThemeColors || !d->foregroundColor.isValid()) {
  87. return QtMaterialStyle::instance().themeColor("primary1");
  88. } else {
  89. return d->foregroundColor;
  90. }
  91. }
  92. void QtMaterialAppBar::setBackgroundColor(const QColor &color)
  93. {
  94. Q_D(QtMaterialAppBar);
  95. d->backgroundColor = color;
  96. if (d->useThemeColors == true) {
  97. d->useThemeColors = false;
  98. }
  99. update();
  100. }
  101. QColor QtMaterialAppBar::backgroundColor() const
  102. {
  103. Q_D(const QtMaterialAppBar);
  104. if (d->useThemeColors || !d->backgroundColor.isValid()) {
  105. return QtMaterialStyle::instance().themeColor("primary1");
  106. } else {
  107. return d->backgroundColor;
  108. }
  109. }