diff --git a/src/items/label/KReportBoundedTextItem.cpp b/src/items/label/KReportBoundedTextItem.cpp index f284eef3..39b1ac59 100644 --- a/src/items/label/KReportBoundedTextItem.cpp +++ b/src/items/label/KReportBoundedTextItem.cpp @@ -1,84 +1,91 @@ /* This file is part of the KDE project Copyright (C) 2014 Adam Pigg + Copyright (C) 2016 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KReportBoundedTextItem.h" #include #include #include BoundedTextItem::BoundedTextItem(QGraphicsItem* parent): QGraphicsTextItem(parent) + , m_backgroundOpacity(1.0) { setTextInteractionFlags(Qt::TextEditorInteraction); setCursor(QCursor(Qt::IBeamCursor)); } QRectF BoundedTextItem::boundingRect() const { if (parentItem()) { return parentItem()->boundingRect(); } return QGraphicsTextItem::boundingRect(); } void BoundedTextItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w) { QColor bg = m_backgroundColor.isValid() ? m_backgroundColor : o->palette.base().color(); bg.setAlphaF(m_backgroundOpacity * 0.01); QColor fc = m_foregroundColor.isValid() ? m_foregroundColor : o->palette.text().color(); painter->setBrush(bg); painter->setPen(fc); painter->drawRect(boundingRect()); QStyleOptionGraphicsItem opt(*o); opt.state &= ~QStyle::State_HasFocus; QGraphicsTextItem::paint(painter, &opt, w); } void BoundedTextItem::setBackgroudColor(const QColor& bc) { m_backgroundColor = bc; } void BoundedTextItem::setForegroundColor(const QColor& fc) { m_foregroundColor = fc; } void BoundedTextItem::setDisplayFont(const QFont& f) { m_font = f; setFont(m_font); } -void BoundedTextItem::setBackgroudOpacity(int o) +qreal BoundedTextItem::backgroudOpacity() const { - m_backgroundOpacity = o; + return m_backgroundOpacity; +} + +void BoundedTextItem::setBackgroudOpacity(qreal opacity) +{ + m_backgroundOpacity = std::min(1.0, std::max(0.0, opacity)); } void BoundedTextItem::keyReleaseEvent(QKeyEvent* event) { if (event->key() == Qt::Key_Escape) { emit exitEditMode(); } else { QGraphicsTextItem::keyReleaseEvent(event); } } diff --git a/src/items/label/KReportBoundedTextItem.h b/src/items/label/KReportBoundedTextItem.h index 337dfca9..3bf1f500 100644 --- a/src/items/label/KReportBoundedTextItem.h +++ b/src/items/label/KReportBoundedTextItem.h @@ -1,62 +1,69 @@ /* This file is part of the KDE project Copyright (C) 2014 Adam Pigg + Copyright (C) 2016 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KREPORT_BOUNDEDTEXTITEM_H #define KREPORT_BOUNDEDTEXTITEM_H #include #include /** * @brief Subclass of QGraphicsTextItem which simply forces * its boundingRect to be the same as its parent. * By default a QGraphicsTextItem will size to its text and * we want it to size to the parent item. * */ class BoundedTextItem : public QGraphicsTextItem { Q_OBJECT public: explicit BoundedTextItem(QGraphicsItem *parent); virtual QRectF boundingRect() const; virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w); void setBackgroudColor(const QColor &bc); void setForegroundColor(const QColor &fc); - void setBackgroudOpacity(int o); + + //! @return background opacity, 0..1.0 + qreal backgroudOpacity() const; + + //! Sets background opacity, 0..1.0 + void setBackgroudOpacity(qreal opacity); + void setDisplayFont(const QFont &f); protected: virtual void keyReleaseEvent ( QKeyEvent * event ); private: QColor m_backgroundColor; QColor m_foregroundColor; QFont m_font; - int m_backgroundOpacity; + qreal m_backgroundOpacity; Q_SIGNALS: void exitEditMode(); }; #endif // KREPORT_BOUNDEDTEXTITEM_H diff --git a/src/items/label/KReportDesignerItemLabel.cpp b/src/items/label/KReportDesignerItemLabel.cpp index 9e6cf19c..8584dace 100644 --- a/src/items/label/KReportDesignerItemLabel.cpp +++ b/src/items/label/KReportDesignerItemLabel.cpp @@ -1,223 +1,223 @@ /* This file is part of the KDE project * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com) * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "KReportDesignerItemLabel.h" #include "KReportDesignerItemBase.h" #include "KReportDesigner.h" #include "KReportDesignerSectionScene.h" #include "KReportLineStyle.h" #include #include #include #include #include #include #include void KReportDesignerItemLabel::init(QGraphicsScene *scene, KReportDesigner *d) { if (scene) scene->addItem(this); KReportDesignerItemRectBase::init(&m_pos, &m_size, m_set, d); connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)), this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&))); setZValue(Z); setFlag(ItemIsFocusable); m_inlineEdit = new BoundedTextItem(this); m_inlineEdit->setVisible(false); m_inlineEdit->setFlag(ItemIsFocusable); m_inlineEdit->setFlag(ItemIsSelectable, false); QTextDocument *doc = new QTextDocument; doc->setDocumentMargin(0); doc->setPlainText(text()); m_inlineEdit->setDocument(doc); connect(m_inlineEdit, SIGNAL(exitEditMode()), this, SLOT(exitInlineEditingMode())); } // methods (constructors) KReportDesignerItemLabel::KReportDesignerItemLabel(KReportDesigner* d, QGraphicsScene * scene, const QPointF &pos) : KReportDesignerItemRectBase(d) { Q_UNUSED(pos); init(scene, d); setSceneRect(properRect(*d, getTextRect().width(), getTextRect().height())); m_name->setValue(m_reportDesigner->suggestEntityName(typeName())); enterInlineEditingMode(); } KReportDesignerItemLabel::KReportDesignerItemLabel(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s) : KReportItemLabel(element), KReportDesignerItemRectBase(d), m_inlineEdit(0) { init(s, d); setSceneRect(m_pos.toScene(), m_size.toScene()); } KReportDesignerItemLabel* KReportDesignerItemLabel::clone() { QDomDocument d; QDomElement e = d.createElement(QLatin1String("clone")); QDomNode n; buildXML(&d, &e); n = e.firstChild(); return new KReportDesignerItemLabel(n, designer(), 0); } // methods (deconstructor) KReportDesignerItemLabel::~KReportDesignerItemLabel() {} QRectF KReportDesignerItemLabel::getTextRect() const { return QFontMetrics(font()).boundingRect(x(), y(), 0, 0, textFlags(), m_text->value().toString()); } void KReportDesignerItemLabel::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { Q_UNUSED(option); Q_UNUSED(widget); if (m_inlineEdit->isVisible()) { return; } // store any values we plan on changing so we can restore them QFont f = painter->font(); QPen p = painter->pen(); painter->setFont(font()); painter->setBackgroundMode(Qt::TransparentMode); QColor bg = m_backgroundColor->value().value(); bg.setAlphaF(m_backgroundOpacity->value().toReal() * 0.01); painter->setPen(m_foregroundColor->value().value()); painter->fillRect(QGraphicsRectItem::rect(), bg); painter->drawText(rect(), textFlags(), text()); if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) { painter->setPen(QPen(QColor(224, 224, 224))); } else { painter->setPen(QPen(m_lineColor->value().value(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt())); } painter->drawRect(QGraphicsRectItem::rect()); painter->setPen(m_foregroundColor->value().value()); drawHandles(painter); // restore an values before we started just in case painter->setFont(f); painter->setPen(p); } void KReportDesignerItemLabel::buildXML(QDomDocument *doc, QDomElement *parent) { //kreportpluginDebug(); QDomElement entity = doc->createElement(QLatin1String("report:") + typeName()); // properties addPropertyAsAttribute(&entity, m_name); addPropertyAsAttribute(&entity, m_text); addPropertyAsAttribute(&entity, m_verticalAlignment); addPropertyAsAttribute(&entity, m_horizontalAlignment); entity.setAttribute(QLatin1String("report:z-index"), zValue()); // bounding rect buildXMLRect(doc, &entity, &m_pos, &m_size); //text style info buildXMLTextStyle(doc, &entity, textStyle()); //Line Style buildXMLLineStyle(doc, &entity, lineStyle()); parent->appendChild(entity); } void KReportDesignerItemLabel::slotPropertyChanged(KPropertySet &s, KProperty &p) { Q_UNUSED(s); if (p.name() == "name") { //For some reason p.oldValue returns an empty string if (!m_reportDesigner->isEntityNameUnique(p.value().toString(), this)) { p.setValue(m_oldName); } else { m_oldName = p.value().toString(); } } else if (p.name() == "caption") { m_inlineEdit->setPlainText(p.value().toString()); } KReportDesignerItemRectBase::propertyChanged(s, p); if (m_reportDesigner) m_reportDesigner->setModified(true); } void KReportDesignerItemLabel::enterInlineEditingMode() { if (!m_inlineEdit->isVisible()) { m_inlineEdit->setVisible(true); m_inlineEdit->setPlainText(text()); m_inlineEdit->setFocus(); QTextCursor c = m_inlineEdit->textCursor(); c.select(QTextCursor::Document); m_inlineEdit->setTextCursor(c); m_inlineEdit->setFont(m_font->value().value()); m_inlineEdit->setDefaultTextColor(m_foregroundColor->value().value()); m_inlineEdit->setBackgroudColor(m_backgroundColor->value().value()); - m_inlineEdit->setBackgroudOpacity(m_backgroundOpacity->value().toInt()); + m_inlineEdit->setBackgroudOpacity(m_backgroundOpacity->value().toDouble() / 100.0); m_inlineEdit->setForegroundColor(m_foregroundColor->value().value()); m_inlineEdit->setFont(m_font->value().value()); update(); } } void KReportDesignerItemLabel::exitInlineEditingMode() { if (m_inlineEdit->isVisible()) { m_inlineEdit->setVisible(false); setText(m_inlineEdit->toPlainText()); } } void KReportDesignerItemLabel::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) { Q_UNUSED(event); enterInlineEditingMode(); } void KReportDesignerItemLabel::keyReleaseEvent(QKeyEvent* event) { if (event->key() == Qt::Key_F2) { enterInlineEditingMode(); } else { QGraphicsRectItem::keyReleaseEvent(event); } }