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
17 
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  auto 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) ? std::max(NormX(orig_width), 1) : 0;
125  int alpha = element.attribute("alpha", "255").toInt();
126  auto 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 }
MythUIType::CalcAlpha
int CalcAlpha(int alphamod) const
Definition: mythuitype.cpp:475
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:172
MythUIShape::DrawSelf
void DrawSelf(MythPainter *p, int xoffset, int yoffset, int alphaMod, QRect clipRect) override
Definition: mythuishape.cpp:55
MythUIShape::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuishape.cpp:150
MythUIShape::SetLinePen
void SetLinePen(QPen pen)
Definition: mythuishape.cpp:47
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythRect
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:17
MythUIShape::m_cropRect
MythRect m_cropRect
Definition: mythuishape.h:45
MythUIShape::SetFillBrush
void SetFillBrush(QBrush fill)
Definition: mythuishape.cpp:42
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:886
MythUIShape::SetCropRect
void SetCropRect(int x, int y, int width, int height)
Definition: mythuishape.cpp:31
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:79
mythlogging.h
hardwareprofile.config.p
p
Definition: config.py:33
XMLParseBase::getFirstText
static QString getFirstText(QDomElement &element)
Definition: xmlparsebase.cpp:52
MythRect::CalculateArea
void CalculateArea(QRect parentArea)
Definition: mythrect.cpp:64
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1176
MythUIShape::m_linePen
QPen m_linePen
Definition: mythuishape.h:43
mythpainter.h
XMLParseBase::parseGradient
static QBrush parseGradient(const QDomElement &element)
Definition: xmlparsebase.cpp:218
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:85
MythUIShape
A widget for rendering primitive shapes and lines.
Definition: mythuishape.h:21
mythimage.h
MythPainter
Definition: mythpainter.h:34
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:22
MythUIType::NormX
static int NormX(int width)
Definition: mythuitype.cpp:1163
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:1242
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
MythUIType::SetRedraw
void SetRedraw(void)
Definition: mythuitype.cpp:310
mythuishape.h