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
22MythUIShape::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
31void 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
43{
44 m_fillBrush = std::move(fill);
45}
46
48{
49 m_linePen = std::move(pen);
50}
51
55void 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 {
115 m_fillBrush.setStyle(Qt::NoBrush);
116 }
117 }
118 else if (element.tagName() == "line")
119 {
120 QString style = element.attribute("style", "solid");
121 QString color = element.attribute("color", "");
122
123 if (style == "solid" && !color.isEmpty())
124 {
125 int orig_width = element.attribute("width", "1").toInt();
126 int width = (orig_width) ? std::max(NormX(orig_width), 1) : 0;
127 int alpha = element.attribute("alpha", "255").toInt();
128 auto lineColor = QColor(color);
129 lineColor.setAlpha(alpha);
130 m_linePen.setColor(lineColor);
131 m_linePen.setWidth(width);
132 m_linePen.setStyle(Qt::SolidLine);
133 }
134 else
135 {
136 m_linePen.setStyle(Qt::NoPen);
137 }
138 }
139 else if (element.tagName() == "cornerradius")
140 {
141 m_cornerRadius = NormX(getFirstText(element).toInt());
142 }
143 else
144 {
145 return MythUIType::ParseElement(filename, element, showWarnings);
146 }
147
148 return true;
149}
150
155{
156 auto *shape = dynamic_cast<MythUIShape *>(base);
157
158 if (!shape)
159 {
160 LOG(VB_GENERAL, LOG_ERR, "ERROR, bad parsing");
161 return;
162 }
163
164 m_type = shape->m_type;
165 m_fillBrush = shape->m_fillBrush;
166 m_linePen = shape->m_linePen;
167 m_cornerRadius = shape->m_cornerRadius;
168 m_cropRect = shape->m_cropRect;
169
171}
172
177{
178 auto *shape = new MythUIShape(parent, objectName());
179 shape->CopyFrom(this);
180}
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:18
void CalculateArea(QRect parentArea)
Definition: mythrect.cpp:64
QRect toQRect(void) const
Definition: mythrect.cpp:405
A widget for rendering primitive shapes and lines.
Definition: mythuishape.h:22
QString m_type
Definition: mythuishape.h:41
void DrawSelf(MythPainter *p, int xoffset, int yoffset, int alphaMod, QRect clipRect) override
Definition: mythuishape.cpp:55
MythRect m_cropRect
Definition: mythuishape.h:45
void SetFillBrush(QBrush fill)
Definition: mythuishape.cpp:42
QPen m_linePen
Definition: mythuishape.h:43
QBrush m_fillBrush
Definition: mythuishape.h:42
MythUIShape(MythUIType *parent, const QString &name)
Definition: mythuishape.cpp:22
int m_cornerRadius
Definition: mythuishape.h:44
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
void SetLinePen(QPen pen)
Definition: mythuishape.cpp:47
void SetCropRect(int x, int y, int width, int height)
Definition: mythuishape.cpp:31
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
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
The base class on which all widgets and screens are based.
Definition: mythuitype.h:86
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
void SetRedraw(void)
Definition: mythuitype.cpp:313
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:885
int CalcAlpha(int alphamod) const
Definition: mythuitype.cpp:474
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
static int NormX(int width)
static QBrush parseGradient(const QDomElement &element)
static QString getFirstText(QDomElement &element)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39