~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/ui/reports/items/text/PlanReportDesignerItemText.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
 
3
 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
 
4
 * Copyright (C) 2016 by Dag Andersen <danders@get2net.dk>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "PlanReportDesignerItemText.h"
 
21
 
 
22
#include "KReportDesignerItemBase.h"
 
23
#include "KReportDesigner.h"
 
24
#include "KReportLineStyle.h"
 
25
 
 
26
#include <KPropertySet>
 
27
#include <QDomDocument>
 
28
#include <QPainter>
 
29
#include <QGraphicsScene>
 
30
#include <QGraphicsSceneMouseEvent>
 
31
 
 
32
 
 
33
void PlanReportDesignerItemText::init(QGraphicsScene *scene, KReportDesigner *d)
 
34
{
 
35
    //setFlags(ItemIsSelectable | ItemIsMovable);
 
36
    if (scene)
 
37
        scene->addItem(this);
 
38
 
 
39
    connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
 
40
            this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
 
41
 
 
42
    KReportDesignerItemRectBase::init(&m_pos, &m_size, m_set, d);
 
43
 
 
44
    m_controlSource->setListData(m_reportDesigner->fieldKeys(), m_reportDesigner->fieldNames());
 
45
    setZValue(Z);
 
46
 
 
47
    updateRenderText(m_controlSource->value().toString(), m_itemValue->value().toString(),
 
48
                     QLatin1String("textarea"));
 
49
}
 
50
 
 
51
PlanReportDesignerItemText::PlanReportDesignerItemText(KReportDesigner * rw, QGraphicsScene * scene, const QPointF &pos)
 
52
        : KReportDesignerItemRectBase(rw)
 
53
{
 
54
    Q_UNUSED(pos);
 
55
    init(scene, rw);
 
56
    setSceneRect(properRect(*rw, getTextRect().width(), getTextRect().height()));
 
57
    m_name->setValue(m_reportDesigner->suggestEntityName(typeName()));
 
58
}
 
59
 
 
60
PlanReportDesignerItemText::PlanReportDesignerItemText(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s)
 
61
        : PlanReportItemText(element), KReportDesignerItemRectBase(d)
 
62
{
 
63
    init(s, d);
 
64
    setSceneRect(m_pos.toScene(), m_size.toScene());
 
65
}
 
66
 
 
67
PlanReportDesignerItemText* PlanReportDesignerItemText::clone()
 
68
{
 
69
    QDomDocument d;
 
70
    QDomElement e = d.createElement(QLatin1String("clone"));
 
71
    QDomNode n;
 
72
    buildXML(&d, &e);
 
73
    n = e.firstChild();
 
74
    return new PlanReportDesignerItemText(n, designer(), 0);
 
75
}
 
76
 
 
77
PlanReportDesignerItemText::~PlanReportDesignerItemText()
 
78
{}
 
79
 
 
80
QRect PlanReportDesignerItemText::getTextRect() const
 
81
{
 
82
    return QFontMetrics(font()).boundingRect(int (x()), int (y()), 0, 0, textFlags(), m_renderText);
 
83
}
 
84
 
 
85
void PlanReportDesignerItemText::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
 
86
{
 
87
    Q_UNUSED(option);
 
88
    Q_UNUSED(widget)
 
89
 
 
90
    // store any values we plan on changing so we can restore them
 
91
    QFont f = painter->font();
 
92
    QPen  p = painter->pen();
 
93
 
 
94
    painter->setFont(font());
 
95
    painter->setBackgroundMode(Qt::TransparentMode);
 
96
 
 
97
    QColor bg = m_backgroundColor->value().value<QColor>();
 
98
    bg.setAlphaF(m_backgroundOpacity->value().toReal()*0.01);
 
99
 
 
100
    painter->setPen(m_foregroundColor->value().value<QColor>());
 
101
 
 
102
    painter->fillRect(rect(),  bg);
 
103
    painter->drawText(rect(), textFlags(), m_renderText);
 
104
 
 
105
    if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) {
 
106
        painter->setPen(QPen(Qt::lightGray));
 
107
    } else {
 
108
        painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt()));
 
109
    }
 
110
    painter->drawRect(rect());
 
111
 
 
112
    painter->setPen(m_foregroundColor->value().value<QColor>());
 
113
 
 
114
    drawHandles(painter);
 
115
 
 
116
    // restore an values before we started just in case
 
117
    painter->setFont(f);
 
118
    painter->setPen(p);
 
119
}
 
120
 
 
121
void PlanReportDesignerItemText::buildXML(QDomDocument *doc, QDomElement *parent)
 
122
{
 
123
    //kreportpluginDebug();
 
124
    QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
 
125
 
 
126
    // properties
 
127
    addPropertyAsAttribute(&entity, m_name);
 
128
    addPropertyAsAttribute(&entity, m_controlSource);
 
129
    addPropertyAsAttribute(&entity, m_verticalAlignment);
 
130
    addPropertyAsAttribute(&entity, m_horizontalAlignment);
 
131
    entity.setAttribute(QLatin1String("report:bottom-padding"), m_bottomPadding);
 
132
    entity.setAttribute(QLatin1String("report:z-index"), zValue());
 
133
    addPropertyAsAttribute(&entity, m_itemValue);
 
134
 
 
135
    // bounding rect
 
136
    buildXMLRect(doc, &entity, &m_pos, &m_size);
 
137
 
 
138
    //text style info
 
139
    buildXMLTextStyle(doc, &entity, textStyle());
 
140
 
 
141
    //Line Style
 
142
    buildXMLLineStyle(doc, &entity, lineStyle());
 
143
 
 
144
    parent->appendChild(entity);
 
145
}
 
146
 
 
147
void PlanReportDesignerItemText::mousePressEvent(QGraphicsSceneMouseEvent * event)
 
148
{
 
149
    m_controlSource->setListData(m_reportDesigner->fieldKeys(), m_reportDesigner->fieldNames());
 
150
    KReportDesignerItemRectBase::mousePressEvent(event);
 
151
}
 
152
 
 
153
 
 
154
void PlanReportDesignerItemText::slotPropertyChanged(KPropertySet &s, KProperty &p)
 
155
{
 
156
    Q_UNUSED(s);
 
157
 
 
158
    if (p.name() == "position") {
 
159
        m_pos.setUnitPos(p.value().toPointF(), KReportPosition::DontUpdateProperty);
 
160
    } else if (p.name() == "size") {
 
161
        m_size.setUnitSize(p.value().toSizeF(), KReportSize::DontUpdateProperty);
 
162
    } else if (p.name() == "name") {
 
163
        //For some reason p.oldValue returns an empty string
 
164
        if (!m_reportDesigner->isEntityNameUnique(p.value().toString(), this)) {
 
165
            p.setValue(m_oldName);
 
166
        } else {
 
167
            m_oldName = p.value().toString();
 
168
        }
 
169
    }
 
170
 
 
171
    setSceneRect(m_pos.toScene(), m_size.toScene(), DontUpdateProperty);
 
172
    if (m_reportDesigner)
 
173
        m_reportDesigner->setModified(true);
 
174
    if (scene())
 
175
        scene()->update();
 
176
 
 
177
    updateRenderText(m_controlSource->value().toString(), m_itemValue->value().toString(),
 
178
                     QLatin1String("textarea"));
 
179
}