gradient_delegate.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * \file
  3. *
  4. * \author Mattia Basaglia
  5. *
  6. * \copyright Copyright (C) 2013-2020 Mattia Basaglia
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef COLOR_WIDGETS_GRADIENT_DELEGATE_HPP
  23. #define COLOR_WIDGETS_GRADIENT_DELEGATE_HPP
  24. #include <QStyledItemDelegate>
  25. #include <QPainter>
  26. #include "QtColorWidgets/gradient_editor.hpp"
  27. namespace color_widgets {
  28. /**
  29. * \brief Item delegate to edits gradients
  30. *
  31. * In order to make it work, return as edit data from the model a QBrush with a gradient
  32. */
  33. class QCP_EXPORT GradientDelegate : public QStyledItemDelegate
  34. {
  35. Q_OBJECT
  36. public:
  37. QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const Q_DECL_OVERRIDE
  38. {
  39. QVariant data = index.data(Qt::EditRole);
  40. if ( data.canConvert<QBrush>() )
  41. {
  42. QBrush brush = data.value<QBrush>();
  43. if ( brush.gradient() )
  44. {
  45. GradientEditor* editor = new GradientEditor(parent);
  46. editor->setStops(brush.gradient()->stops());
  47. return editor;
  48. }
  49. }
  50. return QStyledItemDelegate::createEditor(parent, option, index);
  51. }
  52. void setModelData(QWidget * widget, QAbstractItemModel * model, const QModelIndex & index) const Q_DECL_OVERRIDE
  53. {
  54. if ( GradientEditor* editor = qobject_cast<GradientEditor*>(widget) )
  55. model->setData(index, QBrush(editor->gradient()), Qt::EditRole);
  56. else
  57. QStyledItemDelegate::setModelData(widget, model, index);
  58. }
  59. void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const Q_DECL_OVERRIDE
  60. {
  61. QVariant display_data = index.data(Qt::DisplayRole);
  62. QVariant gradient_data = display_data.isValid() ? display_data : index.data(Qt::EditRole);
  63. if ( gradient_data.canConvert<QBrush>() )
  64. {
  65. QBrush brush = gradient_data.value<QBrush>();
  66. if ( brush.gradient() )
  67. {
  68. QBrush background;
  69. background.setTexture(QPixmap(QStringLiteral(":/color_widgets/alphaback.png")));
  70. painter->fillRect(option.rect, background);
  71. QLinearGradient g(option.rect.topLeft(), option.rect.topRight());
  72. g.setStops(brush.gradient()->stops());
  73. painter->fillRect(option.rect, g);
  74. if ( option.state & QStyle::State_Selected )
  75. {
  76. int border = 2;
  77. painter->setBrush(Qt::transparent);
  78. painter->setPen(QPen(option.palette.highlight(), border));
  79. painter->drawRect(option.rect.adjusted(border/2, border/2, -border/2, -border/2));
  80. }
  81. return;
  82. }
  83. }
  84. QStyledItemDelegate::paint(painter, option, index);
  85. }
  86. };
  87. } // namespace color_widgets
  88. #endif // COLOR_WIDGETS_GRADIENT_DELEGATE_HPP