~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to libs/koreport/wrtembed/KoReportDesignerItemRectBase.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * OpenRPT report writer and rendering engine
 
3
 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
 
4
 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
 
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 "KoReportDesignerItemRectBase.h"
 
21
#include "reportsceneview.h"
 
22
#include <koproperty/Set.h>
 
23
#include <koproperty/Property.h>
 
24
#include <koproperty/EditorView.h>
 
25
#include <QGraphicsSceneMouseEvent>
 
26
#include "KoReportDesigner.h"
 
27
#include <KoDpi.h>
 
28
#include <kdebug.h>
 
29
#include <krpos.h>
 
30
#include <krsize.h>
 
31
#include "reportscene.h"
 
32
 
 
33
 
 
34
KoReportDesignerItemRectBase::KoReportDesignerItemRectBase(KoReportDesigner *r)
 
35
        : QGraphicsRectItem(), KoReportDesignerItemBase(r)
 
36
{
 
37
    m_dpiX = KoDpi::dpiX();
 
38
    m_dpiY = KoDpi::dpiY();
 
39
 
 
40
    m_ppos = 0;
 
41
    m_psize = 0;
 
42
 
 
43
    setAcceptsHoverEvents(true);
 
44
 
 
45
#if QT_VERSION >= 0x040600
 
46
    setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
 
47
#else
 
48
    setFlags(ItemIsSelectable | ItemIsMovable);
 
49
#endif
 
50
}
 
51
 
 
52
void KoReportDesignerItemRectBase::init(KRPos* p, KRSize* s, KoProperty::Set* se)
 
53
{
 
54
    m_ppos = p;
 
55
    m_psize = s;
 
56
    m_pset = se;
 
57
}
 
58
 
 
59
KoReportDesignerItemRectBase::~KoReportDesignerItemRectBase()
 
60
{
 
61
}
 
62
 
 
63
QRectF KoReportDesignerItemRectBase::sceneRect()
 
64
{
 
65
    return QRectF(m_ppos->toScene(), m_psize->toScene());
 
66
}
 
67
 
 
68
QRectF KoReportDesignerItemRectBase::pointRect()
 
69
{
 
70
    if (m_ppos && m_psize)
 
71
        return QRectF(m_ppos->toPoint(), m_psize->toPoint());
 
72
    else
 
73
        return QRectF(0, 0, 0, 0);
 
74
}
 
75
 
 
76
void KoReportDesignerItemRectBase::setSceneRect(const QPointF& topLeft, const QSizeF& size, UpdatePropertyFlag update)
 
77
{
 
78
    setSceneRect(QRectF(topLeft, size), update);
 
79
}
 
80
 
 
81
void KoReportDesignerItemRectBase::setSceneRect(const QRectF& rect, UpdatePropertyFlag update)
 
82
{
 
83
    QGraphicsRectItem::setPos(rect.x(), rect.y());
 
84
    setRect(0, 0, rect.width(), rect.height());
 
85
    if (update == UpdateProperty) {
 
86
        m_ppos->setScenePos(QPointF(rect.x(), rect.y()));
 
87
        m_psize->setSceneSize(QSizeF(rect.width(), rect.height()));
 
88
    }
 
89
    this->update();
 
90
}
 
91
 
 
92
void KoReportDesignerItemRectBase::mousePressEvent(QGraphicsSceneMouseEvent * event)
 
93
{
 
94
    //Update and show properties
 
95
    m_ppos->setScenePos(QPointF(sceneRect().x(), sceneRect().y()));
 
96
    m_reportDesigner->changeSet(m_pset);
 
97
    setSelected(true);
 
98
    scene()->update();
 
99
 
 
100
    QGraphicsItem::mousePressEvent(event);
 
101
}
 
102
 
 
103
void KoReportDesignerItemRectBase::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
 
104
{
 
105
    //Keep the size and position in sync
 
106
    m_ppos->setScenePos(pos());
 
107
    m_psize->setSceneSize(QSizeF(rect().width(), rect().height()));
 
108
 
 
109
    QGraphicsItem::mouseReleaseEvent(event);
 
110
}
 
111
 
 
112
void KoReportDesignerItemRectBase::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
 
113
{
 
114
    //kDebug() << m_grabAction;
 
115
 
 
116
    qreal w, h;
 
117
 
 
118
    QPointF p  = dynamic_cast<ReportScene*>(scene())->gridPoint(event->scenePos());
 
119
    w = p.x() - scenePos().x();
 
120
    h = p.y() - scenePos().y();
 
121
 
 
122
    //TODO use an enum for the directions
 
123
 
 
124
    switch (m_grabAction) {
 
125
    case 1:
 
126
        if (sceneRect().y() - p.y() + rect().height() > 0 && sceneRect().x() - p.x() + rect().width() >= 0)
 
127
            setSceneRect(QPointF(p.x(), p.y()), QSizeF(sceneRect().x() - p.x() + rect().width(), sceneRect().y() - p.y() + rect().height()));
 
128
        break;
 
129
    case 2:
 
130
        if (sceneRect().y() - p.y() + rect().height() >= 0)
 
131
            setSceneRect(QPointF(sceneRect().x(), p.y()), QSizeF(rect().width(), sceneRect().y() - p.y() + rect().height()));
 
132
        break;
 
133
    case 3:
 
134
        if (sceneRect().y() - p.y() + rect().height() >= 0 && w >= 0)
 
135
            setSceneRect(QPointF(sceneRect().x(), p.y()), QSizeF(w, sceneRect().y() - p.y() + rect().height()));
 
136
        break;
 
137
    case 4:
 
138
        if (w >= 0)
 
139
            setSceneRect(QPointF(sceneRect().x(), sceneRect().y()), QSizeF(w, (rect().height())));
 
140
        break;
 
141
    case 5:
 
142
        if (h >= 0 && w >= 0)
 
143
            setSceneRect(QPointF(sceneRect().x(), sceneRect().y()), QSizeF(w, h));
 
144
        break;
 
145
    case 6:
 
146
        if (h >= 0)
 
147
            setSceneRect(QPointF(sceneRect().x(), sceneRect().y()), QSizeF((rect().width()), h));
 
148
        break;
 
149
    case 7:
 
150
        if (sceneRect().x() - p.x() + rect().width() >= 0 && h >= 0)
 
151
            setSceneRect(QPointF(p.x(), sceneRect().y()), QSizeF(sceneRect().x() - p.x() + rect().width(), h));
 
152
        break;
 
153
        break;
 
154
    case 8:
 
155
        if (sceneRect().x() - p.x() + rect().width() >= 0)
 
156
            setSceneRect(QPointF(p.x(), sceneRect().y()), QSizeF(sceneRect().x() - p.x() + rect().width(), rect().height()));
 
157
        break;
 
158
    default:
 
159
        QGraphicsItem::mouseMoveEvent(event);
 
160
    }
 
161
}
 
162
 
 
163
void KoReportDesignerItemRectBase::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
 
164
{
 
165
    //m_grabAction = 0;
 
166
 
 
167
    if (isSelected()) {
 
168
        m_grabAction = grabHandle(event->pos());
 
169
        switch (m_grabAction) {
 
170
        case 1:
 
171
            setCursor(Qt::SizeFDiagCursor);
 
172
            break;
 
173
        case 2:
 
174
            setCursor(Qt::SizeVerCursor);
 
175
            break;
 
176
        case 3:
 
177
            setCursor(Qt::SizeBDiagCursor);
 
178
            break;
 
179
        case 4:
 
180
            setCursor(Qt::SizeHorCursor);
 
181
            break;
 
182
        case 5:
 
183
            setCursor(Qt::SizeFDiagCursor);
 
184
            break;
 
185
        case 6:
 
186
            setCursor(Qt::SizeVerCursor);
 
187
            break;
 
188
        case 7:
 
189
            setCursor(Qt::SizeBDiagCursor);
 
190
            break;
 
191
        case 8:
 
192
            setCursor(Qt::SizeHorCursor);
 
193
            break;
 
194
        default:
 
195
            unsetCursor();
 
196
        }
 
197
    }
 
198
    //kDebug() << m_grabAction;
 
199
}
 
200
 
 
201
void KoReportDesignerItemRectBase::drawHandles(QPainter *painter)
 
202
{
 
203
    if (isSelected()) {
 
204
        // draw a selected border for visual purposes
 
205
        painter->setPen(QPen(QColor(128, 128, 255), 0, Qt::DotLine));
 
206
 
 
207
        painter->drawRect(rect());
 
208
 
 
209
        const QRectF r = rect();
 
210
        double halfW = (r.width() / 2);
 
211
        double halfH = (r.height() / 2);
 
212
        QPointF center = r.center();
 
213
 
 
214
        center += QPointF(0.75,0.75);
 
215
        
 
216
        painter->fillRect(center.x() - halfW, center.y() - halfH , 5, 5, QColor(128, 128, 255));
 
217
        painter->fillRect(center.x() - 2, center.y() - halfH , 5, 5, QColor(128, 128, 255));
 
218
        painter->fillRect(center.x() + halfW - 4, center.y() - halfH, 5, 5, QColor(128, 128, 255));
 
219
 
 
220
        painter->fillRect(center.x() + (halfW - 4), center.y() - 2, 5, 5, QColor(128, 128, 255));
 
221
 
 
222
        painter->fillRect(center.x() +  halfW - 4 , center.y() + halfH - 4 , 5, 5, QColor(128, 128, 255));
 
223
        painter->fillRect(center.x() - 2, center.y() + halfH - 4, 5, 5, QColor(128, 128, 255));
 
224
        painter->fillRect(center.x() - halfW, center.y() + halfH - 4 , 5, 5, QColor(128, 128, 255));
 
225
 
 
226
        painter->fillRect(center.x() - halfW, center.y() - 2, 5, 5, QColor(128, 128, 255));
 
227
 
 
228
    }
 
229
}
 
230
 
 
231
/**
 
232
 @return 1 2 3
 
233
  8 0 4
 
234
  7 6 5
 
235
*/
 
236
int KoReportDesignerItemRectBase::grabHandle(QPointF pos)
 
237
{
 
238
    QRectF r = boundingRect();
 
239
    int halfW = (int)(r.width() / 2);
 
240
    int halfH = (int)(r.height() / 2);
 
241
    QPointF center = r.center();
 
242
 
 
243
    if (QRectF(center.x() - (halfW), center.y() - (halfH), 5, 5).contains(pos)) {
 
244
        // we are over the top-left handle
 
245
        return 1;
 
246
    } else if (QRectF(center.x() - 2, center.y() - (halfH), 5, 5).contains(pos)) {
 
247
        // top-middle handle
 
248
        return 2;
 
249
    } else if (QRectF(center.x() + (halfW - 4), center.y() - (halfH), 5, 5).contains(pos)) {
 
250
        // top-right
 
251
        return 3;
 
252
    } else if (QRectF(center.x() + (halfW - 4), center.y() - 2, 5, 5).contains(pos)) {
 
253
        // middle-right
 
254
        return 4;
 
255
    } else if (QRectF(center.x() + (halfW - 4), center.y() + (halfH - 4), 5, 5).contains(pos)) {
 
256
        // bottom-left
 
257
        return 5;
 
258
    } else if (QRectF(center.x() - 2, center.y() + (halfH - 4), 5, 5).contains(pos)) {
 
259
        // bottom-middle
 
260
        return 6;
 
261
    } else if (QRectF(center.x() - (halfW), center.y() + (halfH - 4), 5, 5).contains(pos)) {
 
262
        // bottom-right
 
263
        return 7;
 
264
    } else if (QRectF(center.x() - (halfW), center.y() - 2, 5, 5).contains(pos)) {
 
265
        // middle-right
 
266
        return 8;
 
267
    }
 
268
    return 0;
 
269
}
 
270
 
 
271
QVariant KoReportDesignerItemRectBase::itemChange(GraphicsItemChange change, const QVariant &value)
 
272
{
 
273
    if (change == ItemPositionChange && scene()) {
 
274
        QPointF newPos = value.toPointF();
 
275
 
 
276
        newPos = dynamic_cast<ReportScene*>(scene())->gridPoint(newPos);
 
277
        if (newPos.x() < 0)
 
278
            newPos.setX(0);
 
279
        else if (newPos.x() > (scene()->width() - rect().width()))
 
280
            newPos.setX(scene()->width() - rect().width());
 
281
 
 
282
        if (newPos.y() < 0)
 
283
            newPos.setY(0);
 
284
        else if (newPos.y() > (scene()->height() - rect().height()))
 
285
            newPos.setY(scene()->height() - rect().height());
 
286
 
 
287
        return newPos;
 
288
    } else if (change == ItemPositionHasChanged && scene()) {
 
289
        m_ppos->setScenePos(value.toPointF());
 
290
    } else if (change == ItemSceneHasChanged && scene() && m_psize) {
 
291
        QPointF newPos = pos();
 
292
 
 
293
        newPos = dynamic_cast<ReportScene*>(scene())->gridPoint(newPos);
 
294
        if (newPos.x() < 0)
 
295
            newPos.setX(0);
 
296
        else if (newPos.x() > (scene()->width() - rect().width()))
 
297
            newPos.setX(scene()->width() - rect().width());
 
298
 
 
299
        if (newPos.y() < 0)
 
300
            newPos.setY(0);
 
301
        else if (newPos.y() > (scene()->height() - rect().height()))
 
302
            newPos.setY(scene()->height() - rect().height());
 
303
 
 
304
        setSceneRect(newPos, m_psize->toScene());
 
305
    }
 
306
 
 
307
    return QGraphicsItem::itemChange(change, value);
 
308
}
 
309
 
 
310
void KoReportDesignerItemRectBase::propertyChanged(const KoProperty::Set &s, const KoProperty::Property &p)
 
311
{
 
312
    Q_UNUSED(s)
 
313
    
 
314
    if (p.name() == "Position") {
 
315
        m_ppos->setUnitPos(p.value().toPointF(), KRPos::DontUpdateProperty);
 
316
    } else if (p.name() == "Size") {
 
317
        m_psize->setUnitSize(p.value().toSizeF(), KRPos::DontUpdateProperty);
 
318
    }
 
319
 
 
320
    setSceneRect(m_ppos->toScene(), m_psize->toScene(), DontUpdateProperty);
 
321
}
 
322
 
 
323
void KoReportDesignerItemRectBase::move(const QPointF& m)
 
324
{
 
325
//! TODO    
 
326
}
 
327