00001
00002
00003 #include "mythuishape.h"
00004
00005
00006 #include <algorithm>
00007 using namespace std;
00008
00009
00010 #include <QDomDocument>
00011 #include <QPainter>
00012 #include <QSize>
00013 #include <QColor>
00014
00015
00016 #include "mythlogging.h"
00017 #include "mythpainter.h"
00018 #include "mythimage.h"
00019 #include "mythmainwindow.h"
00020
00021 MythUIShape::MythUIShape(MythUIType *parent, const QString &name)
00022 : MythUIType(parent, name)
00023 {
00024 m_type = "box";
00025 m_fillBrush = QBrush(Qt::NoBrush);
00026 m_linePen = QPen(Qt::NoPen);
00027 m_cornerRadius = 10;
00028 m_cropRect = MythRect(0, 0, 0, 0);
00029 }
00030
00031 void MythUIShape::SetCropRect(int x, int y, int width, int height)
00032 {
00033 SetCropRect(MythRect(x, y, width, height));
00034 }
00035
00036 void MythUIShape::SetCropRect(const MythRect &rect)
00037 {
00038 m_cropRect = rect;
00039 SetRedraw();
00040 }
00041
00042 void MythUIShape::SetFillBrush(QBrush fill)
00043 {
00044 m_fillBrush = fill;
00045 }
00046
00047 void MythUIShape::SetLinePen(QPen pen)
00048 {
00049 m_linePen = pen;
00050 }
00051
00055 void MythUIShape::DrawSelf(MythPainter *p, int xoffset, int yoffset,
00056 int alphaMod, QRect clipRect)
00057 {
00058 int alpha = CalcAlpha(alphaMod);
00059 QRect area = GetArea();
00060 m_cropRect.CalculateArea(area);
00061
00062 if (!m_cropRect.isEmpty())
00063 area &= m_cropRect.toQRect();
00064
00065 area.translate(xoffset, yoffset);
00066
00067 if (m_type == "box")
00068 p->DrawRect(area, m_fillBrush, m_linePen, alpha);
00069 else if (m_type == "roundbox")
00070 p->DrawRoundRect(area, m_cornerRadius, m_fillBrush, m_linePen, alpha);
00071 else if (m_type == "ellipse")
00072 p->DrawEllipse(area, m_fillBrush, m_linePen, alpha);
00073 }
00074
00078 bool MythUIShape::ParseElement(
00079 const QString &filename, QDomElement &element, bool showWarnings)
00080 {
00081 if (element.tagName() == "type")
00082 {
00083 QString type = getFirstText(element);
00084
00085 if (type == "box" || type == "roundbox" || type == "ellipse")
00086 m_type = type;
00087 }
00088 else if (element.tagName() == "fill")
00089 {
00090 QString style = element.attribute("style", "solid");
00091 QString color = element.attribute("color", "");
00092 int alpha = element.attribute("alpha", "255").toInt();
00093
00094 if (style == "solid" && !color.isEmpty())
00095 {
00096 m_fillBrush.setStyle(Qt::SolidPattern);
00097 QColor brushColor = QColor(color);
00098 brushColor.setAlpha(alpha);
00099 m_fillBrush.setColor(brushColor);
00100 }
00101 else if (style == "gradient")
00102 {
00103 for (QDomNode child = element.firstChild(); !child.isNull();
00104 child = child.nextSibling())
00105 {
00106 QDomElement childElem = child.toElement();
00107
00108 if (childElem.tagName() == "gradient")
00109 m_fillBrush = parseGradient(childElem);
00110 }
00111 }
00112 else
00113 m_fillBrush.setStyle(Qt::NoBrush);
00114 }
00115 else if (element.tagName() == "line")
00116 {
00117 QString style = element.attribute("style", "solid");
00118 QString color = element.attribute("color", "");
00119
00120 if (style == "solid" && !color.isEmpty())
00121 {
00122 int orig_width = element.attribute("width", "1").toInt();
00123 int width = (orig_width) ? max(NormX(orig_width), 1) : 0;
00124 int alpha = element.attribute("alpha", "255").toInt();
00125 QColor lineColor = QColor(color);
00126 lineColor.setAlpha(alpha);
00127 m_linePen.setColor(lineColor);
00128 m_linePen.setWidth(width);
00129 m_linePen.setStyle(Qt::SolidLine);
00130 }
00131 else
00132 m_linePen.setStyle(Qt::NoPen);
00133 }
00134 else if (element.tagName() == "cornerradius")
00135 {
00136 m_cornerRadius = NormX(getFirstText(element).toInt());
00137 }
00138 else
00139 {
00140 return MythUIType::ParseElement(filename, element, showWarnings);
00141 }
00142
00143 return true;
00144 }
00145
00149 void MythUIShape::CopyFrom(MythUIType *base)
00150 {
00151 MythUIShape *shape = dynamic_cast<MythUIShape *>(base);
00152
00153 if (!shape)
00154 {
00155 LOG(VB_GENERAL, LOG_ERR, "ERROR, bad parsing");
00156 return;
00157 }
00158
00159 m_type = shape->m_type;
00160 m_fillBrush = shape->m_fillBrush;
00161 m_linePen = shape->m_linePen;
00162 m_cornerRadius = shape->m_cornerRadius;
00163 m_cropRect = shape->m_cropRect;
00164
00165 MythUIType::CopyFrom(base);
00166 }
00167
00171 void MythUIShape::CreateCopy(MythUIType *parent)
00172 {
00173 MythUIShape *shape = new MythUIShape(parent, objectName());
00174 shape->CopyFrom(this);
00175 }
00176