File indexing completed on 2024-05-19 04:44:31

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2009-2010 by Adam Pigg (adam@piggz.co.uk)
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "KReportDesignerItemCheckBox.h"
0019 #include "KReportDesignerItemRectBase.h"
0020 #include "KReportDesigner.h"
0021 #include "KReportLineStyle.h"
0022 
0023 #include <QDomDocument>
0024 #include <QPainter>
0025 #include <QGraphicsScene>
0026 #include <QGraphicsSceneMouseEvent>
0027 
0028 //
0029 // class ReportEntityCheck
0030 //
0031 
0032 void KReportDesignerItemCheckBox::init(QGraphicsScene *scene)
0033 {
0034     if (scene)
0035         scene->addItem(this);
0036 
0037     connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
0038             this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
0039 
0040     setZValue(z());
0041 }
0042 
0043 // methods (constructors)
0044 KReportDesignerItemCheckBox::KReportDesignerItemCheckBox(KReportDesigner* d, QGraphicsScene * scene, const QPointF &pos)
0045         : KReportDesignerItemRectBase(d, this)
0046 {
0047     Q_UNUSED(pos);
0048     init(scene);
0049     setSceneRect(properRect(*d, KREPORT_ITEM_CHECK_DEFAULT_WIDTH, KREPORT_ITEM_CHECK_DEFAULT_HEIGHT));
0050     nameProperty()->setValue(designer()->suggestEntityName(typeName()));
0051 }
0052 
0053 KReportDesignerItemCheckBox::KReportDesignerItemCheckBox(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s)
0054         : KReportItemCheckBox(element), KReportDesignerItemRectBase(d, this)
0055 {
0056     init(s);
0057     setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
0058 }
0059 
0060 KReportDesignerItemCheckBox* KReportDesignerItemCheckBox::clone()
0061 {
0062     QDomDocument d;
0063     QDomElement e = d.createElement(QLatin1String("clone"));
0064     QDomNode n;
0065     buildXML(&d, &e);
0066     n = e.firstChild();
0067     return new KReportDesignerItemCheckBox(n, designer(), nullptr);
0068 }
0069 
0070 // methods (deconstructor)
0071 KReportDesignerItemCheckBox::~KReportDesignerItemCheckBox()
0072 {}
0073 
0074 void KReportDesignerItemCheckBox::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
0075 {
0076     Q_UNUSED(option);
0077     Q_UNUSED(widget);
0078 
0079     // store any values we plan on changing so we can restore them
0080     QFont f = painter->font();
0081     QPen  p = painter->pen();
0082     QBrush b = painter->brush();
0083 
0084     painter->setBackgroundMode(Qt::OpaqueMode);
0085     painter->setRenderHint(QPainter::Antialiasing);
0086 
0087     painter->setPen(m_foregroundColor->value().value<QColor>());
0088 
0089     if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) {
0090         painter->setPen(QPen(Qt::lightGray));
0091     } else {
0092         painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt()));
0093     }
0094 
0095     QSizeF sceneSize = this->sceneSize(size());
0096     qreal ox = sceneSize.width() / 5;
0097     qreal oy = sceneSize.height() / 5;
0098 
0099     //Checkbox Style
0100     if (m_checkStyle->value().toString() == QLatin1String("Cross")) {
0101         painter->drawRoundedRect(QGraphicsRectItem::rect(), sceneSize.width() / 10 , sceneSize.height() / 10);
0102 
0103         QPen lp;
0104         lp.setColor(m_foregroundColor->value().value<QColor>());
0105         lp.setWidth(ox > oy ? oy : ox);
0106         painter->setPen(lp);
0107         painter->drawLine(ox, oy, sceneSize.width() - ox, sceneSize.height() - oy);
0108         painter->drawLine(ox, sceneSize.height() - oy, sceneSize.width() - ox, oy);
0109     } else if (m_checkStyle->value().toString() == QLatin1String("Dot")) {
0110         //Radio Style
0111         painter->drawEllipse(QGraphicsRectItem::rect());
0112 
0113         QBrush lb(m_foregroundColor->value().value<QColor>());
0114         painter->setBrush(lb);
0115         painter->setPen(Qt::NoPen);
0116         painter->drawEllipse(rect().center(), sceneSize.width() / 2 - ox, sceneSize.height() / 2 - oy);
0117     } else {
0118         //Tickbox Style
0119         painter->drawRoundedRect(QGraphicsRectItem::rect(), sceneSize.width() / 10 , sceneSize.height() / 10);
0120 
0121         QPen lp;
0122         lp.setColor(m_foregroundColor->value().value<QColor>());
0123         lp.setWidth(ox > oy ? oy : ox);
0124         painter->setPen(lp);
0125         painter->drawLine(ox, sceneSize.height() / 2, sceneSize.width() / 2, sceneSize.height() - oy);
0126         painter->drawLine(sceneSize.width() / 2, sceneSize.height() - oy, sceneSize.width() - ox, oy);
0127 
0128     }
0129 
0130     painter->setBackgroundMode(Qt::TransparentMode);
0131     painter->setPen(m_foregroundColor->value().value<QColor>());
0132 
0133     // restore an values before we started just in case
0134     painter->setFont(f);
0135     painter->setPen(p);
0136     painter->setBrush(b);
0137 
0138     drawHandles(painter);
0139 }
0140 
0141 void KReportDesignerItemCheckBox::buildXML(QDomDocument *doc, QDomElement *parent)
0142 {
0143     //kreportpluginDebug();
0144     QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
0145 
0146     //properties
0147     addPropertyAsAttribute(&entity, nameProperty());
0148     addPropertyAsAttribute(&entity, dataSourceProperty());
0149     entity.setAttribute(QLatin1String("fo:foreground-color"), m_foregroundColor->value().toString());
0150     addPropertyAsAttribute(&entity, m_checkStyle);
0151     addPropertyAsAttribute(&entity, m_staticValue);
0152 
0153     // bounding rect
0154     buildXMLRect(doc, &entity, this);
0155 
0156     //Line Style
0157     buildXMLLineStyle(doc, &entity, lineStyle());
0158 
0159     parent->appendChild(entity);
0160 }
0161 
0162 void KReportDesignerItemCheckBox::slotPropertyChanged(KPropertySet &s, KProperty &p)
0163 {
0164     Q_UNUSED(s)
0165 
0166     if (p.name() == "name") {
0167         //For some reason p.oldValue returns an empty string
0168         if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
0169             p.setValue(oldName());
0170         } else {
0171             setOldName(p.value().toString());
0172         }
0173     }
0174 
0175     KReportDesignerItemRectBase::propertyChanged(s, p);
0176     if (designer()) designer()->setModified(true);
0177 }