File indexing completed on 2024-05-19 04:44:33
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com) 0003 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk) 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Lesser General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2.1 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Lesser General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Lesser General Public 0016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 0017 */ 0018 0019 #include "KReportDesignerItemLabel.h" 0020 #include "KReportDesignerItemBase.h" 0021 #include "KReportDesigner.h" 0022 #include "KReportDesignerSectionScene.h" 0023 #include "KReportLineStyle.h" 0024 0025 #include <QKeyEvent> 0026 #include <QDomDocument> 0027 #include <QPainter> 0028 #include <QGraphicsScene> 0029 #include <QGraphicsSceneMouseEvent> 0030 #include <QTextCursor> 0031 #include <QTextDocument> 0032 0033 void KReportDesignerItemLabel::init(QGraphicsScene *scene) 0034 { 0035 if (scene) 0036 scene->addItem(this); 0037 0038 connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)), 0039 this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&))); 0040 0041 setZValue(z()); 0042 setFlag(ItemIsFocusable); 0043 0044 m_inlineEdit = new BoundedTextItem(this); 0045 m_inlineEdit->setVisible(false); 0046 m_inlineEdit->setFlag(ItemIsFocusable); 0047 m_inlineEdit->setFlag(ItemIsSelectable, false); 0048 QTextDocument *doc = new QTextDocument(m_inlineEdit); 0049 doc->setDocumentMargin(0); 0050 doc->setPlainText(text()); 0051 m_inlineEdit->setDocument(doc); 0052 0053 connect(m_inlineEdit, SIGNAL(exitEditMode()), this, SLOT(exitInlineEditingMode())); 0054 } 0055 0056 // methods (constructors) 0057 KReportDesignerItemLabel::KReportDesignerItemLabel(KReportDesigner* d, QGraphicsScene * scene, const QPointF &pos) 0058 : KReportDesignerItemRectBase(d, this) 0059 { 0060 Q_UNUSED(pos); 0061 init(scene); 0062 setSceneRect(properRect(*d, getTextRect().width(), getTextRect().height())); 0063 nameProperty()->setValue(designer()->suggestEntityName(typeName())); 0064 0065 enterInlineEditingMode(); 0066 } 0067 0068 KReportDesignerItemLabel::KReportDesignerItemLabel(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s) 0069 : KReportItemLabel(element), KReportDesignerItemRectBase(d, this), m_inlineEdit(nullptr) 0070 { 0071 init(s); 0072 setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size())); 0073 } 0074 0075 KReportDesignerItemLabel* KReportDesignerItemLabel::clone() 0076 { 0077 QDomDocument d; 0078 QDomElement e = d.createElement(QLatin1String("clone")); 0079 QDomNode n; 0080 buildXML(&d, &e); 0081 n = e.firstChild(); 0082 return new KReportDesignerItemLabel(n, designer(), nullptr); 0083 } 0084 0085 // methods (deconstructor) 0086 KReportDesignerItemLabel::~KReportDesignerItemLabel() 0087 {} 0088 0089 QRectF KReportDesignerItemLabel::getTextRect() const 0090 { 0091 return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), m_text->value().toString()); 0092 } 0093 0094 void KReportDesignerItemLabel::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 0095 { 0096 Q_UNUSED(option); 0097 Q_UNUSED(widget); 0098 0099 if (m_inlineEdit->isVisible()) { 0100 return; 0101 } 0102 0103 // store any values we plan on changing so we can restore them 0104 QFont f = painter->font(); 0105 QPen p = painter->pen(); 0106 0107 painter->setFont(font()); 0108 painter->setBackgroundMode(Qt::TransparentMode); 0109 0110 QColor bg = m_backgroundColor->value().value<QColor>(); 0111 bg.setAlphaF(m_backgroundOpacity->value().toReal() * 0.01); 0112 0113 painter->setPen(m_foregroundColor->value().value<QColor>()); 0114 0115 painter->fillRect(QGraphicsRectItem::rect(), bg); 0116 painter->drawText(rect(), textFlags(), text()); 0117 0118 if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) { 0119 painter->setPen(QPen(QColor(224, 224, 224))); 0120 } else { 0121 painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt())); 0122 } 0123 0124 painter->drawRect(QGraphicsRectItem::rect()); 0125 painter->setPen(m_foregroundColor->value().value<QColor>()); 0126 0127 drawHandles(painter); 0128 0129 // restore an values before we started just in case 0130 painter->setFont(f); 0131 painter->setPen(p); 0132 } 0133 0134 void KReportDesignerItemLabel::buildXML(QDomDocument *doc, QDomElement *parent) 0135 { 0136 //kreportpluginDebug(); 0137 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName()); 0138 0139 // properties 0140 addPropertyAsAttribute(&entity, nameProperty()); 0141 addPropertyAsAttribute(&entity, m_text); 0142 addPropertyAsAttribute(&entity, m_verticalAlignment); 0143 addPropertyAsAttribute(&entity, m_horizontalAlignment); 0144 entity.setAttribute(QLatin1String("report:z-index"), z()); 0145 0146 // bounding rect 0147 buildXMLRect(doc, &entity, this); 0148 0149 //text style info 0150 buildXMLTextStyle(doc, &entity, textStyle()); 0151 0152 //Line Style 0153 buildXMLLineStyle(doc, &entity, lineStyle()); 0154 0155 parent->appendChild(entity); 0156 } 0157 0158 void KReportDesignerItemLabel::slotPropertyChanged(KPropertySet &s, KProperty &p) 0159 { 0160 Q_UNUSED(s); 0161 0162 if (p.name() == "name") { 0163 //For some reason p.oldValue returns an empty string 0164 if (!designer()->isEntityNameUnique(p.value().toString(), this)) { 0165 p.setValue(oldName()); 0166 } else { 0167 setOldName(p.value().toString()); 0168 } 0169 } else if (p.name() == "caption") { 0170 m_inlineEdit->setPlainText(p.value().toString()); 0171 } 0172 0173 KReportDesignerItemRectBase::propertyChanged(s, p); 0174 if (designer()) designer()->setModified(true); 0175 0176 } 0177 0178 void KReportDesignerItemLabel::enterInlineEditingMode() 0179 { 0180 if (!m_inlineEdit->isVisible()) { 0181 m_inlineEdit->setVisible(true); 0182 m_inlineEdit->setPlainText(text()); 0183 m_inlineEdit->setFocus(); 0184 0185 QTextCursor c = m_inlineEdit->textCursor(); 0186 c.select(QTextCursor::Document); 0187 m_inlineEdit->setTextCursor(c); 0188 0189 m_inlineEdit->setFont(m_font->value().value<QFont>()); 0190 m_inlineEdit->setDefaultTextColor(m_foregroundColor->value().value<QColor>()); 0191 m_inlineEdit->setBackgroudColor(m_backgroundColor->value().value<QColor>()); 0192 m_inlineEdit->setBackgroudOpacity(m_backgroundOpacity->value().toDouble() / 100.0); 0193 m_inlineEdit->setForegroundColor(m_foregroundColor->value().value<QColor>()); 0194 m_inlineEdit->setFont(m_font->value().value<QFont>()); 0195 0196 update(); 0197 } 0198 } 0199 0200 void KReportDesignerItemLabel::exitInlineEditingMode() 0201 { 0202 if (m_inlineEdit->isVisible()) { 0203 m_inlineEdit->setVisible(false); 0204 setText(m_inlineEdit->toPlainText()); 0205 } 0206 } 0207 0208 void KReportDesignerItemLabel::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) 0209 { 0210 Q_UNUSED(event); 0211 enterInlineEditingMode(); 0212 } 0213 0214 void KReportDesignerItemLabel::keyReleaseEvent(QKeyEvent* event) 0215 { 0216 if (event->key() == Qt::Key_F2) { 0217 enterInlineEditingMode(); 0218 } else { 0219 QGraphicsRectItem::keyReleaseEvent(event); 0220 } 0221 }