File indexing completed on 2024-12-01 11:20:18

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "solidshape.h"
0012 #include "libraryitem.h"
0013 #include "resizeoverlay.h"
0014 
0015 #include <KLocalizedString>
0016 #include <QPainter>
0017 #include <cmath>
0018 
0019 #include <algorithm>
0020 
0021 // BEGIN class DPRectangle
0022 Item *DPRectangle::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0023 {
0024     return new DPRectangle(itemDocument, newItem, id);
0025 }
0026 
0027 LibraryItem *DPRectangle::libraryItem()
0028 {
0029     return new LibraryItem(QStringList(QString("dp/rectangle")), i18n("Rectangle"), i18n("Other"), QIcon::fromTheme("text"), LibraryItem::lit_drawpart, DPRectangle::construct);
0030 }
0031 
0032 DPRectangle::DPRectangle(ItemDocument *itemDocument, bool newItem, const char *id)
0033     : DrawPart(itemDocument, newItem, id ? id : "rectangle")
0034 {
0035     m_pRectangularOverlay = new RectangularOverlay(this);
0036     m_name = i18n("Rectangle");
0037 
0038     createProperty("background", Variant::Type::Bool);
0039     property("background")->setValue(false);
0040     property("background")->setCaption(i18n("Display Background"));
0041     property("background")->setAdvanced(true);
0042 
0043     createProperty("background-color", Variant::Type::Color);
0044     property("background-color")->setValue(QColor(Qt::white));
0045     property("background-color")->setCaption(i18n("Background Color"));
0046     property("background-color")->setAdvanced(true);
0047 
0048     createProperty("line-color", Variant::Type::Color);
0049     property("line-color")->setValue(QColor(Qt::black));
0050     property("line-color")->setCaption(i18n("Line Color"));
0051     property("line-color")->setAdvanced(true);
0052 
0053     createProperty("line-width", Variant::Type::Int);
0054     property("line-width")->setCaption(i18n("Line Width"));
0055     property("line-width")->setMinValue(1);
0056     property("line-width")->setMaxValue(1000);
0057     property("line-width")->setValue(1);
0058     property("line-width")->setAdvanced(true);
0059 
0060     createProperty("line-style", Variant::Type::PenStyle);
0061     property("line-style")->setAdvanced(true);
0062     property("line-style")->setCaption(i18n("Line Style"));
0063     setDataPenStyle("line-style", Qt::SolidLine);
0064 }
0065 
0066 DPRectangle::~DPRectangle()
0067 {
0068 }
0069 
0070 void DPRectangle::setSelected(bool yes)
0071 {
0072     if (yes == isSelected())
0073         return;
0074 
0075     DrawPart::setSelected(yes);
0076     m_pRectangularOverlay->showResizeHandles(yes);
0077 }
0078 
0079 void DPRectangle::dataChanged()
0080 {
0081     bool displayBackground = dataBool("background");
0082     QColor line_color = dataColor("line-color");
0083     unsigned width = unsigned(dataInt("line-width"));
0084     Qt::PenStyle style = getDataPenStyle("line-style");
0085 
0086     setPen(QPen(line_color, width, style));
0087 
0088     if (displayBackground)
0089         setBrush(dataColor("background-color"));
0090     else
0091         setBrush(Qt::NoBrush);
0092 
0093     postResize();
0094     update();
0095 }
0096 
0097 QSize DPRectangle::minimumSize() const
0098 {
0099     int side = std::max(16, pen().width() + 2);
0100     return QSize(side, side);
0101 }
0102 
0103 void DPRectangle::postResize()
0104 {
0105     setItemPoints(m_sizeRect, false);
0106 }
0107 
0108 QRect DPRectangle::drawRect() const
0109 {
0110     int lw = pen().width();
0111 
0112     if (lw > m_sizeRect.width())
0113         lw = m_sizeRect.width();
0114 
0115     if (lw > m_sizeRect.height())
0116         lw = m_sizeRect.height();
0117 
0118     return QRect(int(x() + m_sizeRect.x() + lw / 2), int(y() + m_sizeRect.y() + lw / 2), m_sizeRect.width() - lw, m_sizeRect.height() - lw);
0119 }
0120 
0121 void DPRectangle::drawShape(QPainter &p)
0122 {
0123     p.drawRect(drawRect());
0124 }
0125 // END class DPRectangle
0126 
0127 // BEGIN class DPEllipse
0128 Item *DPEllipse::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0129 {
0130     return new DPEllipse(itemDocument, newItem, id);
0131 }
0132 
0133 LibraryItem *DPEllipse::libraryItem()
0134 {
0135     return new LibraryItem(QStringList(QString("dp/ellipse")), i18n("Ellipse"), i18n("Other"), QIcon::fromTheme("text"), LibraryItem::lit_drawpart, DPEllipse::construct);
0136 }
0137 
0138 DPEllipse::DPEllipse(ItemDocument *itemDocument, bool newItem, const char *id)
0139     : DPRectangle(itemDocument, newItem, id ? id : "ellipse")
0140 {
0141     m_name = i18n("Ellipse");
0142 }
0143 
0144 DPEllipse::~DPEllipse()
0145 {
0146 }
0147 
0148 void DPEllipse::postResize()
0149 {
0150     QRect br = m_sizeRect;
0151 
0152     // Make octagon that roughly covers ellipse
0153     QPolygon pa(8);
0154     pa[0] = QPoint(br.x() + br.width() / 4, br.y());
0155     pa[1] = QPoint(br.x() + 3 * br.width() / 4, br.y());
0156     pa[2] = QPoint(br.x() + br.width(), br.y() + br.height() / 4);
0157     pa[3] = QPoint(br.x() + br.width(), br.y() + 3 * br.height() / 4);
0158     pa[4] = QPoint(br.x() + 3 * br.width() / 4, br.y() + br.height());
0159     pa[5] = QPoint(br.x() + br.width() / 4, br.y() + br.height());
0160     pa[6] = QPoint(br.x(), br.y() + 3 * br.height() / 4);
0161     pa[7] = QPoint(br.x(), br.y() + br.height() / 4);
0162 
0163     setItemPoints(pa, false);
0164 }
0165 
0166 void DPEllipse::drawShape(QPainter &p)
0167 {
0168     p.drawEllipse(drawRect());
0169 }
0170 // END class SolidShape