StarDelegate.cpp
Go to the documentation of this file.
1 #include "StarDelegate.h"
2 
3 
4 //! Qt include files
5 #include <QPainter>
6 
7 //! STL include files
8 #include <cmath>
9 
10 QPainterPath getStar()
11 {
12  QPainterPath star, aux;
13 
14  aux.setFillRule(Qt::WindingFill);
15 
16  aux.moveTo(1.0, 0.5);
17  for(int i = 1; i < 5; ++i)
18  {
19  QPointF pt(0.5 + 0.5 * cos(0.8 * i * 3.14),
20  0.5 + 0.5 * sin(0.8 * i * 3.14));
21  aux.lineTo(pt);
22  star = aux.simplified();
23  }
24 
25  return star;
26 }
27 
28 QPainterPath recalculateMark(const QPainterPath& markShape, const int& markSize)
29 {
30  QMatrix m;
31  m.scale(markSize*2, markSize*2);
32 
33  return m.map(markShape);
34 }
35 
36 QImage getStarImage(const QSize& size, const QColor& c)
37 {
38  QImage img(size, QImage::Format_ARGB32_Premultiplied);
39 
40  img.fill(Qt::color0);
41 
42  QPainter p(&img);
43  QPainterPath star = getStar();
44  star = recalculateMark(star, size.height()/2);
45 
46  p.translate(img.rect().center()-star.boundingRect().center());
47 
48  p.fillPath(star, Qt::color1);
49  p.setPen(QPen(Qt::color1, 1));
50  p.drawPath(star);
51 
52  p.fillPath(star, c);
53  p.setPen(QPen(Qt::black, 1));
54  p.drawPath(star);
55 
56  return img;
57 }
58 
59 StarDelegate::StarDelegate(const size_t nClass) :
60 te::qt::widgets::HLDelegateDecorator(te::qt::widgets::HLDelegateDecorator::getDelegate(nClass))
61 {
62  init();
63 }
64 
66 te::qt::widgets::HLDelegateDecorator(decorated)
67 {
68  init();
69 }
70 
71 void StarDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
72 {
73  if(toHighlight(index))
74  {
75  QItemDelegate::paint(painter, option, index);
76 
77  if(index.column() == 0)
78  {
79  QRect r = option.rect;
80 
81  if(r.size() != m_img.size())
82  m_img = getStarImage(r.size(), m_color);
83 
84  painter->save();
85  painter->setOpacity(.5);
86  painter->drawImage(r, m_img);
87  painter->restore();
88  }
89  }
90  else
91  HLDelegateDecorator::paint(painter, option, index);
92 }
93 
94 void StarDelegate::setHighlightColor(const QColor & value)
95 {
96  HighlightDelegate::setHighlightColor(value);
97  m_img = getStarImage(m_img.size(), m_color);
98 }
99 
101 {
102  m_img = getStarImage(QSize(10,10), m_color);
103  HighlightDelegate::setGroupName(tr("Starred"));
104 }
An specialization of QItemDelegate to be used with te::map::AbstractTable objects.
QPainterPath getStar()
Qt include files.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QImage getStarImage(const QSize &size, const QColor &c)
URI C++ Library.
Definition: Attributes.h:37
te::gm::Polygon * p
QImage m_img
Definition: StarDelegate.h:27
StarDelegate(const size_t nClass=0)
void setHighlightColor(const QColor &value)
QPainterPath recalculateMark(const QPainterPath &markShape, const int &markSize)
void paint(te::qt::widgets::Canvas *c, bool generatePNG, std::string fileName)