1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (info@qt.nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
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
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
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
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."
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
60 void TiledCanvas::setCanvasSize(const QSizeF
&v
)
62 if (m_canvasSize
!= v
) {
64 emit
canvasSizeChanged();
69 QSize
TiledCanvas::tileSize() const
74 void TiledCanvas::setTileSize(const QSize
&v
)
76 if (v
!= m_tileSize
) {
78 emit
tileSizeChanged();
83 QRectF
TiledCanvas::canvasWindow() const
85 return m_canvasWindow
;
88 void TiledCanvas::setCanvasWindow(const QRectF
&v
)
90 if (m_canvasWindow
!= v
) {
92 emit
canvasWindowChanged();
97 void TiledCanvas::requestPaint()
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
) {
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
)
151 QPixmap
TiledCanvas::getTile(int xx
, int yy
)
153 QPixmap
pix(m_tileSize
);
157 QString text
= QString::number(xx
) + QLatin1Char(' ') + QString::number(yy
);
160 p
.drawText(pix
.rect(), Qt::AlignHCenter
| Qt::AlignVCenter
, text
);
166 #include "qdeclarativetiledcanvas.moc"