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