MythTV  master
mythuishape.cpp
Go to the documentation of this file.
1 
2 // Own header
3 #include "mythuishape.h"
4 
5 // C++
6 #include <algorithm>
7 using namespace std;
8 
9 // qt
10 #include <QColor>
11 #include <QDomDocument>
12 #include <QPainter>
13 #include <QSize>
14 #include <utility>
15 
16 // myth
17 #include "mythlogging.h"
18 #include "mythpainter.h"
19 #include "mythimage.h"
20 #include "mythmainwindow.h"
21 
22 MythUIShape::MythUIShape(MythUIType *parent, const QString &name)
23  : MythUIType(parent, name)
24 {
25  // Workaround a feature in Qt 4.8 where a QPen constructed with
26  // style Qt::NoPen returns width = 1;
27  // Qt 5.11 source still seems to do the same.
28  m_linePen.setWidth(0);
29 }
30 
31 void MythUIShape::SetCropRect(int x, int y, int width, int height)
32 {
33  SetCropRect(MythRect(x, y, width, height));
34 }
35 
37 {
38  m_cropRect = rect;
39  SetRedraw();
40 }
41 
42 void MythUIShape::SetFillBrush(QBrush fill)
43 {
44  m_fillBrush = std::move(fill);
45 }
46 
48 {
49  m_linePen = std::move(pen);
50 }
51 
55 void MythUIShape::DrawSelf(MythPainter *p, int xoffset, int yoffset,
56  int alphaMod, QRect clipRect)
57 {
58  int alpha = CalcAlpha(alphaMod);
59  QRect area = GetArea();
61 
62  if (!m_cropRect.isEmpty())
63  area &= m_cropRect.toQRect();
64 
65  area.translate(xoffset, yoffset);
66 
67  p->SetClipRect(clipRect);
68  if (m_type == "box")
69  p->DrawRect(area, m_fillBrush, m_linePen, alpha);
70  else if (m_type == "roundbox")
71  p->DrawRoundRect(area, m_cornerRadius, m_fillBrush, m_linePen, alpha);
72  else if (m_type == "ellipse")
73  p->DrawEllipse(area, m_fillBrush, m_linePen, alpha);
74 }
75 
80  const QString &filename, QDomElement &element, bool showWarnings)
81 {
82  if (element.tagName() == "type")
83  {
84  QString type = getFirstText(element);
85 
86  if (type == "box" || type == "roundbox" || type == "ellipse") // Validate input
87  m_type = type;
88  }
89  else if (element.tagName() == "fill")
90  {
91  QString style = element.attribute("style", "solid");
92  QString color = element.attribute("color", "");
93  int alpha = element.attribute("alpha", "255").toInt();
94 
95  if (style == "solid" && !color.isEmpty())
96  {
97  m_fillBrush.setStyle(Qt::SolidPattern);
98  QColor brushColor = QColor(color);
99  brushColor.setAlpha(alpha);
100  m_fillBrush.setColor(brushColor);
101  }
102  else if (style == "gradient")
103  {
104  for (QDomNode child = element.firstChild(); !child.isNull();
105  child = child.nextSibling())
106  {
107  QDomElement childElem = child.toElement();
108 
109  if (childElem.tagName() == "gradient")
110  m_fillBrush = parseGradient(childElem);
111  }
112  }
113  else
114  m_fillBrush.setStyle(Qt::NoBrush);
115  }
116  else if (element.tagName() == "line")
117  {
118  QString style = element.attribute("style", "solid");
119  QString color = element.attribute("color", "");
120 
121  if (style == "solid" && !color.isEmpty())
122  {
123  int orig_width = element.attribute("width", "1").toInt();
124  int width = (orig_width) ? max(NormX(orig_width), 1) : 0;
125  int alpha = element.attribute("alpha", "255").toInt();
126  QColor lineColor = QColor(color);
127  lineColor.setAlpha(alpha);
128  m_linePen.setColor(lineColor);
129  m_linePen.setWidth(width);
130  m_linePen.setStyle(Qt::SolidLine);
131  }
132  else
133  m_linePen.setStyle(Qt::NoPen);
134  }
135  else if (element.tagName() == "cornerradius")
136  {
137  m_cornerRadius = NormX(getFirstText(element).toInt());
138  }
139  else
140  {
141  return MythUIType::ParseElement(filename, element, showWarnings);
142  }
143 
144  return true;
145 }
146 
151 {
152  auto *shape = dynamic_cast<MythUIShape *>(base);
153 
154  if (!shape)
155  {
156  LOG(VB_GENERAL, LOG_ERR, "ERROR, bad parsing");
157  return;
158  }
159 
160  m_type = shape->m_type;
161  m_fillBrush = shape->m_fillBrush;
162  m_linePen = shape->m_linePen;
163  m_cornerRadius = shape->m_cornerRadius;
164  m_cropRect = shape->m_cropRect;
165 
166  MythUIType::CopyFrom(base);
167 }
168 
173 {
174  auto *shape = new MythUIShape(parent, objectName());
175  shape->CopyFrom(this);
176 }
void CalculateArea(const MythRect &parentArea)
Definition: mythrect.cpp:32
void DrawSelf(MythPainter *p, int xoffset, int yoffset, int alphaMod, QRect clipRect) override
Definition: mythuishape.cpp:55
static int NormX(const int width)
void SetRedraw(void)
Definition: mythuitype.cpp:295
MythRect m_cropRect
Definition: mythuishape.h:45
void SetFillBrush(QBrush fill)
Definition: mythuishape.cpp:42
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
Definition: mythuishape.cpp:79
int CalcAlpha(int alphamod)
Definition: mythuitype.cpp:460
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
QPen m_linePen
Definition: mythuishape.h:43
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
QRect toQRect(void) const
Definition: mythrect.cpp:354
void SetLinePen(QPen pen)
Definition: mythuishape.cpp:47
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:866
static QString getFirstText(QDomElement &element)
void SetCropRect(int x, int y, int width, int height)
Definition: mythuishape.cpp:31
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:17
QBrush m_fillBrush
Definition: mythuishape.h:42
MythUIShape(MythUIType *parent, const QString &name)
Definition: mythuishape.cpp:22
QString m_type
Definition: mythuishape.h:41
int m_cornerRadius
Definition: mythuishape.h:44
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static QBrush parseGradient(const QDomElement &element)
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.