Imported Upstream version 1.0.1
[gammaray-debian.git] / qmldebugcontrol / canvas / qdeclarativetiledcanvas.cpp
blobaf3c6d40aebe8d0c51260cc07ba5d5429bca1956
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (info@qt.nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
23 ** permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 ** $QT_END_LICENSE$
38 ****************************************************************************/
40 #include "qdeclarativetiledcanvas.h"
42 #include "qdeclarativecontext2d.h"
44 #include <QtGui/qpixmap.h>
45 #include <QtGui/qpainter.h>
47 TiledCanvas::TiledCanvas()
48 : m_context2d(new Context2D(this)), m_canvasSize(-1, -1), m_tileSize(100, 100)
50 setFlag(QGraphicsItem::ItemHasNoContents, false);
51 setAcceptedMouseButtons(Qt::LeftButton);
52 setCacheMode(QGraphicsItem::DeviceCoordinateCache);
55 QSizeF TiledCanvas::canvasSize() const
57 return m_canvasSize;
60 void TiledCanvas::setCanvasSize(const QSizeF &v)
62 if (m_canvasSize != v) {
63 m_canvasSize = v;
64 emit canvasSizeChanged();
65 update();
69 QSize TiledCanvas::tileSize() const
71 return m_tileSize;
74 void TiledCanvas::setTileSize(const QSize &v)
76 if (v != m_tileSize) {
77 m_tileSize = v;
78 emit tileSizeChanged();
79 update();
83 QRectF TiledCanvas::canvasWindow() const
85 return m_canvasWindow;
88 void TiledCanvas::setCanvasWindow(const QRectF &v)
90 if (m_canvasWindow != v) {
91 m_canvasWindow = v;
92 emit canvasWindowChanged();
93 update();
97 void TiledCanvas::requestPaint()
99 update();
102 void TiledCanvas::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
104 if (m_context2d->size() != m_tileSize)
105 m_context2d->setSize(m_tileSize);
107 const int tw = m_tileSize.width();
108 const int th = m_tileSize.height();
110 int h1 = m_canvasWindow.left() / tw;
111 int htiles = ((m_canvasWindow.right() - h1 * tw) + tw - 1) / tw;
113 int v1 = m_canvasWindow.top() / th;
114 int vtiles = ((m_canvasWindow.bottom() - v1 * th) + th - 1) / th;
116 for (int yy = 0; yy < vtiles; ++yy) {
117 for (int xx = 0; xx < htiles; ++xx) {
118 int ht = xx + h1;
119 int vt = yy + v1;
121 m_context2d->reset();
122 m_context2d->setPainterTranslate(QPoint(-ht * tw, -vt * th));
124 emit drawRegion(m_context2d, QRect(ht * tw, vt * th, tw, th));
126 p->drawPixmap(-m_canvasWindow.x() + ht * tw, -m_canvasWindow.y() + vt * th, m_context2d->pixmap());
131 void TiledCanvas::componentComplete()
133 const QMetaObject *metaObject = this->metaObject();
134 int propertyCount = metaObject->propertyCount();
135 int requestPaintMethod = metaObject->indexOfMethod("requestPaint()");
136 for (int ii = TiledCanvas::staticMetaObject.propertyCount(); ii < propertyCount; ++ii) {
137 QMetaProperty p = metaObject->property(ii);
138 if (p.hasNotifySignal())
139 QMetaObject::connect(this, p.notifySignalIndex(), this, requestPaintMethod, 0, 0);
141 QDeclarativeItem::componentComplete();
145 void TiledCanvas::mousePressEvent(QGraphicsSceneMouseEvent *event)
147 Q_UNUSED(event);
148 qWarning("MPE");
151 QPixmap TiledCanvas::getTile(int xx, int yy)
153 QPixmap pix(m_tileSize);
155 pix.fill(Qt::green);
157 QString text = QString::number(xx) + QLatin1Char(' ') + QString::number(yy);
159 QPainter p(&pix);
160 p.drawText(pix.rect(), Qt::AlignHCenter | Qt::AlignVCenter, text);
162 return pix;
166 #include "qdeclarativetiledcanvas.moc"