color_delegate.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_DELEGATE_HPP
  23. #define COLOR_DELEGATE_HPP
  24. #include "colorwidgets_global.hpp"
  25. #include <QStyledItemDelegate>
  26. namespace color_widgets {
  27. class QCP_EXPORT ReadOnlyColorDelegate : public QStyledItemDelegate
  28. {
  29. Q_OBJECT
  30. public:
  31. using QStyledItemDelegate::QStyledItemDelegate;
  32. void paint(QPainter *painter, const QStyleOptionViewItem &option,
  33. const QModelIndex &index) const Q_DECL_OVERRIDE;
  34. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
  35. void setSizeHintForColor(const QSize& size_hint);
  36. const QSize& sizeHintForColor() const;
  37. protected:
  38. void paintItem(QPainter *painter, const QStyleOptionViewItem &option,
  39. const QModelIndex &index, const QBrush& brush) const;
  40. private:
  41. QSize size_hint{24, 16};
  42. };
  43. /**
  44. Delegate to use a ColorSelector in a color list
  45. */
  46. class QCP_EXPORT ColorDelegate : public ReadOnlyColorDelegate
  47. {
  48. Q_OBJECT
  49. public:
  50. using ReadOnlyColorDelegate::ReadOnlyColorDelegate;
  51. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  52. const QModelIndex &index) const Q_DECL_OVERRIDE;
  53. void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
  54. void setModelData(QWidget *editor, QAbstractItemModel *model,
  55. const QModelIndex &index) const Q_DECL_OVERRIDE;
  56. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  57. const QModelIndex &index) const Q_DECL_OVERRIDE;
  58. protected:
  59. bool eventFilter(QObject * watched, QEvent * event) Q_DECL_OVERRIDE;
  60. private slots:
  61. void close_editor();
  62. void color_changed();
  63. };
  64. } // namespace color_widgets
  65. #endif // COLOR_DELEGATE_HPP