First commit
This commit is contained in:
19
examples/canvas_typed/CMakeLists.txt
Normal file
19
examples/canvas_typed/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
# Tell CMake to run moc when necessary:
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
# As moc files are generated in the binary dir, tell CMake
|
||||
# to always look for includes there:
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
SET(example_name canvas_typed)
|
||||
|
||||
SET(KIT_SRCS
|
||||
main.cpp
|
||||
mainwindow.cpp
|
||||
mainwindow.h
|
||||
qtcanvas.cpp
|
||||
qtcanvas.h
|
||||
)
|
||||
|
||||
ADD_EXECUTABLE(${example_name} ${KIT_SRCS})
|
||||
TARGET_LINK_LIBRARIES(${example_name} ${PROJECT_NAME} ${QT_TARGETS})
|
||||
96
examples/canvas_typed/canvas_typed.qdoc
Normal file
96
examples/canvas_typed/canvas_typed.qdoc
Normal file
@@ -0,0 +1,96 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
**
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of a Qt Solutions component.
|
||||
**
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
/*!
|
||||
\page qtpropertybrowser-example-canvas_typed.html
|
||||
|
||||
\title Type Based Canvas Example
|
||||
|
||||
This example demonstrates how to use different
|
||||
QtAbstractPropertyManager subclasses for different property
|
||||
types. Using this approach, the developer interfaces with a
|
||||
type-safe API (since each manager has its own property-type
|
||||
specific API).
|
||||
|
||||
\image canvas_typed.png
|
||||
|
||||
The example presents a canvas filled up with items of different
|
||||
types, and a tree property browser which displays the currently
|
||||
selected item's properties.
|
||||
|
||||
All item types has a few common properties like "Position X", "Position Y"
|
||||
or "Position Z", but each type also adds its own type-specific
|
||||
properties (e.g. the text items provide "Text" and "Font"
|
||||
properties, and the line items provide a "Vector" property).
|
||||
|
||||
The source files can be found in examples/canvas_typed directory
|
||||
of the package.
|
||||
|
||||
\section1 Third party copyright notice
|
||||
|
||||
The canvas class used in this example contains third party code
|
||||
with the following copyright notice:
|
||||
|
||||
\legalese
|
||||
\code
|
||||
|
||||
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
Permission to use, copy, modify, and distribute this software and its
|
||||
documentation for any purpose and without fee is hereby granted,
|
||||
provided that the above copyright notice appear in all copies and that
|
||||
both that copyright notice and this permission notice appear in
|
||||
supporting documentation, and that the name of Digital not be
|
||||
used in advertising or publicity pertaining to distribution of the
|
||||
software without specific, written prior permission.
|
||||
|
||||
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
|
||||
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
|
||||
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
|
||||
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
SOFTWARE.
|
||||
|
||||
\endcode
|
||||
\endlegalese
|
||||
|
||||
*/
|
||||
51
examples/canvas_typed/main.cpp
Normal file
51
examples/canvas_typed/main.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
**
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of a Qt Solutions component.
|
||||
**
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QApplication>
|
||||
#include "mainwindow.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
MainWindow mw;
|
||||
mw.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
521
examples/canvas_typed/mainwindow.cpp
Normal file
521
examples/canvas_typed/mainwindow.cpp
Normal file
@@ -0,0 +1,521 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
**
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of a Qt Solutions component.
|
||||
**
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "qtpropertymanager.h"
|
||||
#include "qteditorfactory.h"
|
||||
#include "qttreepropertybrowser.h"
|
||||
#include <QMatrix>
|
||||
#include <QMouseEvent>
|
||||
#include <QMenuBar>
|
||||
#include <QMenu>
|
||||
#include <QAction>
|
||||
#include <QDockWidget>
|
||||
|
||||
void CanvasView::contentsMousePressEvent(QMouseEvent* event)
|
||||
{
|
||||
handleMouseClickEvent(event);
|
||||
}
|
||||
|
||||
void CanvasView::contentsMouseDoubleClickEvent(QMouseEvent* event)
|
||||
{
|
||||
handleMouseClickEvent(event);
|
||||
}
|
||||
|
||||
void CanvasView::handleMouseClickEvent(QMouseEvent* event)
|
||||
{
|
||||
QPoint p = inverseWorldMatrix().map(event->pos());
|
||||
QtCanvasItemList l = canvas()->collisions(p);
|
||||
moving = 0;
|
||||
if (!l.isEmpty())
|
||||
moving = l.first();
|
||||
moving_start = p;
|
||||
emit itemClicked(moving);
|
||||
}
|
||||
|
||||
void CanvasView::contentsMouseMoveEvent(QMouseEvent* event)
|
||||
{
|
||||
if (moving) {
|
||||
QPoint p = inverseWorldMatrix().map(event->pos());
|
||||
moving->moveBy(p.x() - moving_start.x(), p.y() - moving_start.y());
|
||||
moving_start = p;
|
||||
canvas()->update();
|
||||
emit itemMoved(moving);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
{
|
||||
QMenu *editMenu = menuBar()->addMenu(tr("Edit"));
|
||||
QMenu *newObjectMenu = editMenu->addMenu(tr("New Object"));
|
||||
|
||||
QAction *newRectangleAction = new QAction(tr("Rectangle"), this);
|
||||
connect(newRectangleAction, SIGNAL(triggered(bool)), this, SLOT(newRectangle()));
|
||||
newObjectMenu->addAction(newRectangleAction);
|
||||
|
||||
QAction *newLineAction = new QAction(tr("Line"), this);
|
||||
connect(newLineAction, SIGNAL(triggered(bool)), this, SLOT(newLine()));
|
||||
newObjectMenu->addAction(newLineAction);
|
||||
|
||||
QAction *newEllipseAction = new QAction(tr("Ellipse"), this);
|
||||
connect(newEllipseAction, SIGNAL(triggered(bool)), this, SLOT(newEllipse()));
|
||||
newObjectMenu->addAction(newEllipseAction);
|
||||
|
||||
QAction *newTextAction = new QAction(tr("Text"), this);
|
||||
connect(newTextAction, SIGNAL(triggered(bool)), this, SLOT(newText()));
|
||||
newObjectMenu->addAction(newTextAction);
|
||||
|
||||
deleteAction = new QAction(tr("Delete Object"), this);
|
||||
connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(deleteObject()));
|
||||
editMenu->addAction(deleteAction);
|
||||
|
||||
QAction *clearAction = new QAction(tr("Clear All"), this);
|
||||
connect(clearAction, SIGNAL(triggered(bool)), this, SLOT(clearAll()));
|
||||
editMenu->addAction(clearAction);
|
||||
|
||||
QAction *fillAction = new QAction(tr("Fill View"), this);
|
||||
connect(fillAction, SIGNAL(triggered(bool)), this, SLOT(fillView()));
|
||||
editMenu->addAction(fillAction);
|
||||
|
||||
doubleManager = new QtDoublePropertyManager(this);
|
||||
stringManager = new QtStringPropertyManager(this);
|
||||
colorManager = new QtColorPropertyManager(this);
|
||||
fontManager = new QtFontPropertyManager(this);
|
||||
pointManager = new QtPointPropertyManager(this);
|
||||
sizeManager = new QtSizePropertyManager(this);
|
||||
|
||||
connect(doubleManager, SIGNAL(valueChanged(QtProperty *, double)),
|
||||
this, SLOT(valueChanged(QtProperty *, double)));
|
||||
connect(stringManager, SIGNAL(valueChanged(QtProperty *, const QString &)),
|
||||
this, SLOT(valueChanged(QtProperty *, const QString &)));
|
||||
connect(colorManager, SIGNAL(valueChanged(QtProperty *, const QColor &)),
|
||||
this, SLOT(valueChanged(QtProperty *, const QColor &)));
|
||||
connect(fontManager, SIGNAL(valueChanged(QtProperty *, const QFont &)),
|
||||
this, SLOT(valueChanged(QtProperty *, const QFont &)));
|
||||
connect(pointManager, SIGNAL(valueChanged(QtProperty *, const QPoint &)),
|
||||
this, SLOT(valueChanged(QtProperty *, const QPoint &)));
|
||||
connect(sizeManager, SIGNAL(valueChanged(QtProperty *, const QSize &)),
|
||||
this, SLOT(valueChanged(QtProperty *, const QSize &)));
|
||||
|
||||
QtDoubleSpinBoxFactory *doubleSpinBoxFactory = new QtDoubleSpinBoxFactory(this);
|
||||
QtCheckBoxFactory *checkBoxFactory = new QtCheckBoxFactory(this);
|
||||
QtSpinBoxFactory *spinBoxFactory = new QtSpinBoxFactory(this);
|
||||
QtLineEditFactory *lineEditFactory = new QtLineEditFactory(this);
|
||||
QtEnumEditorFactory *comboBoxFactory = new QtEnumEditorFactory(this);
|
||||
|
||||
canvas = new QtCanvas(800, 600);
|
||||
canvasView = new CanvasView(canvas, this);
|
||||
setCentralWidget(canvasView);
|
||||
|
||||
QDockWidget *dock = new QDockWidget(this);
|
||||
addDockWidget(Qt::RightDockWidgetArea, dock);
|
||||
|
||||
propertyEditor = new QtTreePropertyBrowser(dock);
|
||||
propertyEditor->setFactoryForManager(doubleManager, doubleSpinBoxFactory);
|
||||
propertyEditor->setFactoryForManager(stringManager, lineEditFactory);
|
||||
propertyEditor->setFactoryForManager(colorManager->subIntPropertyManager(), spinBoxFactory);
|
||||
propertyEditor->setFactoryForManager(fontManager->subIntPropertyManager(), spinBoxFactory);
|
||||
propertyEditor->setFactoryForManager(fontManager->subBoolPropertyManager(), checkBoxFactory);
|
||||
propertyEditor->setFactoryForManager(fontManager->subEnumPropertyManager(), comboBoxFactory);
|
||||
propertyEditor->setFactoryForManager(pointManager->subIntPropertyManager(), spinBoxFactory);
|
||||
propertyEditor->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
|
||||
dock->setWidget(propertyEditor);
|
||||
|
||||
currentItem = 0;
|
||||
|
||||
connect(canvasView, SIGNAL(itemClicked(QtCanvasItem *)),
|
||||
this, SLOT(itemClicked(QtCanvasItem *)));
|
||||
connect(canvasView, SIGNAL(itemMoved(QtCanvasItem *)),
|
||||
this, SLOT(itemMoved(QtCanvasItem *)));
|
||||
|
||||
fillView();
|
||||
itemClicked(0);
|
||||
}
|
||||
|
||||
void MainWindow::newRectangle()
|
||||
{
|
||||
QtCanvasItem *item = addRectangle();
|
||||
canvas->update();
|
||||
itemClicked(item);
|
||||
}
|
||||
|
||||
void MainWindow::newEllipse()
|
||||
{
|
||||
QtCanvasItem *item = addEllipse();
|
||||
canvas->update();
|
||||
itemClicked(item);
|
||||
}
|
||||
|
||||
void MainWindow::newLine()
|
||||
{
|
||||
QtCanvasItem *item = addLine();
|
||||
canvas->update();
|
||||
itemClicked(item);
|
||||
}
|
||||
|
||||
void MainWindow::newText()
|
||||
{
|
||||
QtCanvasItem *item = addText();
|
||||
canvas->update();
|
||||
itemClicked(item);
|
||||
}
|
||||
|
||||
void MainWindow::deleteObject()
|
||||
{
|
||||
if (!currentItem)
|
||||
return;
|
||||
|
||||
delete currentItem;
|
||||
itemClicked(0);
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
void MainWindow::clearAll()
|
||||
{
|
||||
QtCanvasItemList list = canvas->allItems();
|
||||
qDeleteAll(list);
|
||||
itemClicked(0);
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
void MainWindow::fillView()
|
||||
{
|
||||
for (int i = 0; i < 10; i++) {
|
||||
addRectangle();
|
||||
addEllipse();
|
||||
addLine();
|
||||
addText();
|
||||
}
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
QtCanvasItem *MainWindow::addRectangle()
|
||||
{
|
||||
QtCanvasPolygonalItem *item = new QtCanvasRectangle(rand() % canvas->width(),
|
||||
rand() % canvas->height(), 50, 50, canvas);
|
||||
int z = rand() % 256;
|
||||
item->setBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
|
||||
item->setPen(QPen(QColor(rand() % 32*8, rand() % 32*8, rand() % 32*8), 4));
|
||||
item->setZ(z);
|
||||
item->show();
|
||||
return item;
|
||||
}
|
||||
|
||||
QtCanvasItem *MainWindow::addEllipse()
|
||||
{
|
||||
QtCanvasPolygonalItem *item = new QtCanvasEllipse(50, 50, canvas);
|
||||
item->setBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
|
||||
item->move(rand() % canvas->width(), rand() % canvas->height());
|
||||
item->setZ(rand() % 256);
|
||||
item->show();
|
||||
return item;
|
||||
}
|
||||
|
||||
QtCanvasItem *MainWindow::addLine()
|
||||
{
|
||||
QtCanvasLine *item = new QtCanvasLine(canvas);
|
||||
item->setPoints(0, 0, rand() % canvas->width() - canvas->width() / 2,
|
||||
rand() % canvas->height() - canvas->height() / 2);
|
||||
item->move(rand() % canvas->width(), rand() % canvas->height());
|
||||
item->setPen(QPen(QColor(rand() % 32*8, rand() % 32*8, rand() % 32*8), 6));
|
||||
item->setZ(rand() % 256);
|
||||
item->show();
|
||||
return item;
|
||||
}
|
||||
|
||||
QtCanvasItem *MainWindow::addText()
|
||||
{
|
||||
QtCanvasText *item = new QtCanvasText(canvas);
|
||||
item->setText(tr("Text"));
|
||||
item->setColor(QColor(rand() % 32*8, rand() % 32*8, rand() % 32*8));
|
||||
item->move(rand() % canvas->width(), rand() % canvas->height());
|
||||
item->setZ(rand() % 256);
|
||||
item->show();
|
||||
return item;
|
||||
}
|
||||
|
||||
void MainWindow::itemMoved(QtCanvasItem *item)
|
||||
{
|
||||
if (item != currentItem)
|
||||
return;
|
||||
|
||||
doubleManager->setValue(idToProperty[QLatin1String("xpos")], item->x());
|
||||
doubleManager->setValue(idToProperty[QLatin1String("ypos")], item->y());
|
||||
doubleManager->setValue(idToProperty[QLatin1String("zpos")], item->z());
|
||||
}
|
||||
|
||||
void MainWindow::updateExpandState()
|
||||
{
|
||||
QList<QtBrowserItem *> list = propertyEditor->topLevelItems();
|
||||
QListIterator<QtBrowserItem *> it(list);
|
||||
while (it.hasNext()) {
|
||||
QtBrowserItem *item = it.next();
|
||||
QtProperty *prop = item->property();
|
||||
idToExpanded[propertyToId[prop]] = propertyEditor->isExpanded(item);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::itemClicked(QtCanvasItem *item)
|
||||
{
|
||||
updateExpandState();
|
||||
|
||||
QMap<QtProperty *, QString>::ConstIterator itProp = propertyToId.constBegin();
|
||||
while (itProp != propertyToId.constEnd()) {
|
||||
delete itProp.key();
|
||||
itProp++;
|
||||
}
|
||||
propertyToId.clear();
|
||||
idToProperty.clear();
|
||||
|
||||
currentItem = item;
|
||||
if (!currentItem) {
|
||||
deleteAction->setEnabled(false);
|
||||
return;
|
||||
}
|
||||
|
||||
deleteAction->setEnabled(true);
|
||||
|
||||
QtProperty *property;
|
||||
|
||||
property = doubleManager->addProperty(tr("Position X"));
|
||||
doubleManager->setRange(property, 0, canvas->width());
|
||||
doubleManager->setValue(property, item->x());
|
||||
addProperty(property, QLatin1String("xpos"));
|
||||
|
||||
property = doubleManager->addProperty(tr("Position Y"));
|
||||
doubleManager->setRange(property, 0, canvas->height());
|
||||
doubleManager->setValue(property, item->y());
|
||||
addProperty(property, QLatin1String("ypos"));
|
||||
|
||||
property = doubleManager->addProperty(tr("Position Z"));
|
||||
doubleManager->setRange(property, 0, 256);
|
||||
doubleManager->setValue(property, item->z());
|
||||
addProperty(property, QLatin1String("zpos"));
|
||||
|
||||
if (item->rtti() == QtCanvasItem::Rtti_Rectangle) {
|
||||
QtCanvasRectangle *i = (QtCanvasRectangle *)item;
|
||||
|
||||
property = colorManager->addProperty(tr("Brush Color"));
|
||||
colorManager->setValue(property, i->brush().color());
|
||||
addProperty(property, QLatin1String("brush"));
|
||||
|
||||
property = colorManager->addProperty(tr("Pen Color"));
|
||||
colorManager->setValue(property, i->pen().color());
|
||||
addProperty(property, QLatin1String("pen"));
|
||||
|
||||
property = sizeManager->addProperty(tr("Size"));
|
||||
sizeManager->setValue(property, i->size());
|
||||
addProperty(property, QLatin1String("size"));
|
||||
} else if (item->rtti() == QtCanvasItem::Rtti_Line) {
|
||||
QtCanvasLine *i = (QtCanvasLine *)item;
|
||||
|
||||
property = colorManager->addProperty(tr("Pen Color"));
|
||||
colorManager->setValue(property, i->pen().color());
|
||||
addProperty(property, QLatin1String("pen"));
|
||||
|
||||
property = pointManager->addProperty(tr("Vector"));
|
||||
pointManager->setValue(property, i->endPoint());
|
||||
addProperty(property, QLatin1String("endpoint"));
|
||||
} else if (item->rtti() == QtCanvasItem::Rtti_Ellipse) {
|
||||
QtCanvasEllipse *i = (QtCanvasEllipse *)item;
|
||||
|
||||
property = colorManager->addProperty(tr("Brush Color"));
|
||||
colorManager->setValue(property, i->brush().color());
|
||||
addProperty(property, QLatin1String("brush"));
|
||||
|
||||
property = sizeManager->addProperty(tr("Size"));
|
||||
sizeManager->setValue(property, QSize(i->width(), i->height()));
|
||||
sizeManager->setRange(property, QSize(0, 0), QSize(1000, 1000));
|
||||
addProperty(property, QLatin1String("size"));
|
||||
} else if (item->rtti() == QtCanvasItem::Rtti_Text) {
|
||||
QtCanvasText *i = (QtCanvasText *)item;
|
||||
|
||||
property = colorManager->addProperty(tr("Color"));
|
||||
colorManager->setValue(property, i->color());
|
||||
addProperty(property, QLatin1String("color"));
|
||||
|
||||
property = stringManager->addProperty(tr("Text"));
|
||||
stringManager->setValue(property, i->text());
|
||||
addProperty(property, QLatin1String("text"));
|
||||
|
||||
property = fontManager->addProperty(tr("Font"));
|
||||
fontManager->setValue(property, i->font());
|
||||
addProperty(property, QLatin1String("font"));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::addProperty(QtProperty *property, const QString &id)
|
||||
{
|
||||
propertyToId[property] = id;
|
||||
idToProperty[id] = property;
|
||||
QtBrowserItem *item = propertyEditor->addProperty(property);
|
||||
if (idToExpanded.contains(id))
|
||||
propertyEditor->setExpanded(item, idToExpanded[id]);
|
||||
}
|
||||
|
||||
void MainWindow::valueChanged(QtProperty *property, double value)
|
||||
{
|
||||
if (!propertyToId.contains(property))
|
||||
return;
|
||||
|
||||
if (!currentItem)
|
||||
return;
|
||||
|
||||
QString id = propertyToId[property];
|
||||
if (id == QLatin1String("xpos")) {
|
||||
currentItem->setX(value);
|
||||
} else if (id == QLatin1String("ypos")) {
|
||||
currentItem->setY(value);
|
||||
} else if (id == QLatin1String("zpos")) {
|
||||
currentItem->setZ(value);
|
||||
}
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
void MainWindow::valueChanged(QtProperty *property, const QString &value)
|
||||
{
|
||||
if (!propertyToId.contains(property))
|
||||
return;
|
||||
|
||||
if (!currentItem)
|
||||
return;
|
||||
|
||||
QString id = propertyToId[property];
|
||||
if (id == QLatin1String("text")) {
|
||||
if (currentItem->rtti() == QtCanvasItem::Rtti_Text) {
|
||||
QtCanvasText *i = (QtCanvasText *)currentItem;
|
||||
i->setText(value);
|
||||
}
|
||||
}
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
void MainWindow::valueChanged(QtProperty *property, const QColor &value)
|
||||
{
|
||||
if (!propertyToId.contains(property))
|
||||
return;
|
||||
|
||||
if (!currentItem)
|
||||
return;
|
||||
|
||||
QString id = propertyToId[property];
|
||||
if (id == QLatin1String("color")) {
|
||||
if (currentItem->rtti() == QtCanvasItem::Rtti_Text) {
|
||||
QtCanvasText *i = (QtCanvasText *)currentItem;
|
||||
i->setColor(value);
|
||||
}
|
||||
} else if (id == QLatin1String("brush")) {
|
||||
if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle ||
|
||||
currentItem->rtti() == QtCanvasItem::Rtti_Ellipse) {
|
||||
QtCanvasPolygonalItem *i = (QtCanvasPolygonalItem *)currentItem;
|
||||
QBrush b = i->brush();
|
||||
b.setColor(value);
|
||||
i->setBrush(b);
|
||||
}
|
||||
} else if (id == QLatin1String("pen")) {
|
||||
if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle ||
|
||||
currentItem->rtti() == QtCanvasItem::Rtti_Line) {
|
||||
QtCanvasPolygonalItem *i = (QtCanvasPolygonalItem *)currentItem;
|
||||
QPen p = i->pen();
|
||||
p.setColor(value);
|
||||
i->setPen(p);
|
||||
}
|
||||
}
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
void MainWindow::valueChanged(QtProperty *property, const QFont &value)
|
||||
{
|
||||
if (!propertyToId.contains(property))
|
||||
return;
|
||||
|
||||
if (!currentItem)
|
||||
return;
|
||||
|
||||
QString id = propertyToId[property];
|
||||
if (id == QLatin1String("font")) {
|
||||
if (currentItem->rtti() == QtCanvasItem::Rtti_Text) {
|
||||
QtCanvasText *i = (QtCanvasText *)currentItem;
|
||||
i->setFont(value);
|
||||
}
|
||||
}
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
void MainWindow::valueChanged(QtProperty *property, const QPoint &value)
|
||||
{
|
||||
if (!propertyToId.contains(property))
|
||||
return;
|
||||
|
||||
if (!currentItem)
|
||||
return;
|
||||
|
||||
QString id = propertyToId[property];
|
||||
if (currentItem->rtti() == QtCanvasItem::Rtti_Line) {
|
||||
QtCanvasLine *i = (QtCanvasLine *)currentItem;
|
||||
if (id == QLatin1String("endpoint")) {
|
||||
i->setPoints(i->startPoint().x(), i->startPoint().y(), value.x(), value.y());
|
||||
}
|
||||
}
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
void MainWindow::valueChanged(QtProperty *property, const QSize &value)
|
||||
{
|
||||
if (!propertyToId.contains(property))
|
||||
return;
|
||||
|
||||
if (!currentItem)
|
||||
return;
|
||||
|
||||
QString id = propertyToId[property];
|
||||
if (id == QLatin1String("size")) {
|
||||
if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle) {
|
||||
QtCanvasRectangle *i = (QtCanvasRectangle *)currentItem;
|
||||
i->setSize(value.width(), value.height());
|
||||
} else if (currentItem->rtti() == QtCanvasItem::Rtti_Ellipse) {
|
||||
QtCanvasEllipse *i = (QtCanvasEllipse *)currentItem;
|
||||
i->setSize(value.width(), value.height());
|
||||
}
|
||||
}
|
||||
canvas->update();
|
||||
}
|
||||
|
||||
120
examples/canvas_typed/mainwindow.h
Normal file
120
examples/canvas_typed/mainwindow.h
Normal file
@@ -0,0 +1,120 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
**
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of a Qt Solutions component.
|
||||
**
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QMap>
|
||||
#include "qtcanvas.h"
|
||||
|
||||
class QtProperty;
|
||||
|
||||
class CanvasView : public QtCanvasView
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
CanvasView(QWidget *parent = 0)
|
||||
: QtCanvasView(parent), moving(0) { }
|
||||
CanvasView(QtCanvas *canvas, QWidget *parent = 0)
|
||||
: QtCanvasView(canvas, parent), moving(0) { }
|
||||
signals:
|
||||
void itemClicked(QtCanvasItem *item);
|
||||
void itemMoved(QtCanvasItem *item);
|
||||
protected:
|
||||
void contentsMousePressEvent(QMouseEvent *event);
|
||||
void contentsMouseDoubleClickEvent(QMouseEvent *event);
|
||||
void contentsMouseMoveEvent(QMouseEvent* event);
|
||||
private:
|
||||
void handleMouseClickEvent(QMouseEvent *event);
|
||||
QPoint moving_start;
|
||||
QtCanvasItem *moving;
|
||||
};
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MainWindow(QWidget *parent = 0);
|
||||
|
||||
private slots:
|
||||
void newRectangle();
|
||||
void newEllipse();
|
||||
void newLine();
|
||||
void newText();
|
||||
void deleteObject();
|
||||
void clearAll();
|
||||
void fillView();
|
||||
|
||||
void itemClicked(QtCanvasItem *item);
|
||||
void itemMoved(QtCanvasItem *item);
|
||||
void valueChanged(QtProperty *property, double value);
|
||||
void valueChanged(QtProperty *property, const QString &value);
|
||||
void valueChanged(QtProperty *property, const QColor &value);
|
||||
void valueChanged(QtProperty *property, const QFont &value);
|
||||
void valueChanged(QtProperty *property, const QPoint &value);
|
||||
void valueChanged(QtProperty *property, const QSize &value);
|
||||
private:
|
||||
|
||||
QtCanvasItem *addRectangle();
|
||||
QtCanvasItem *addEllipse();
|
||||
QtCanvasItem *addLine();
|
||||
QtCanvasItem *addText();
|
||||
void addProperty(QtProperty *property, const QString &id);
|
||||
void updateExpandState();
|
||||
|
||||
QAction *deleteAction;
|
||||
|
||||
class QtDoublePropertyManager *doubleManager;
|
||||
class QtStringPropertyManager *stringManager;
|
||||
class QtColorPropertyManager *colorManager;
|
||||
class QtFontPropertyManager *fontManager;
|
||||
class QtPointPropertyManager *pointManager;
|
||||
class QtSizePropertyManager *sizeManager;
|
||||
|
||||
class QtTreePropertyBrowser *propertyEditor;
|
||||
CanvasView *canvasView;
|
||||
QtCanvas *canvas;
|
||||
QtCanvasItem *currentItem;
|
||||
QMap<QtProperty *, QString> propertyToId;
|
||||
QMap<QString, QtProperty *> idToProperty;
|
||||
QMap<QString, bool> idToExpanded;
|
||||
};
|
||||
|
||||
#endif
|
||||
5906
examples/canvas_typed/qtcanvas.cpp
Normal file
5906
examples/canvas_typed/qtcanvas.cpp
Normal file
File diff suppressed because it is too large
Load Diff
778
examples/canvas_typed/qtcanvas.h
Normal file
778
examples/canvas_typed/qtcanvas.h
Normal file
@@ -0,0 +1,778 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
**
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of a Qt Solutions component.
|
||||
**
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
||||
** the names of its contributors may be used to endorse or promote
|
||||
** products derived from this software without specific prior written
|
||||
** permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QTCANVAS_H
|
||||
#define QTCANVAS_H
|
||||
|
||||
#include <QPixmap>
|
||||
#include <QBrush>
|
||||
#include <QPen>
|
||||
#include <QPolygon>
|
||||
#include <QScrollArea>
|
||||
|
||||
class QtCanvasSprite;
|
||||
class QtCanvasPolygonalItem;
|
||||
class QtCanvasRectangle;
|
||||
class QtCanvasPolygon;
|
||||
class QtCanvasEllipse;
|
||||
class QtCanvasText;
|
||||
class QtCanvasLine;
|
||||
class QtCanvasChunk;
|
||||
class QtCanvas;
|
||||
class QtCanvasItem;
|
||||
class QtCanvasView;
|
||||
class QtCanvasPixmap;
|
||||
|
||||
typedef QList<QtCanvasItem *> QtCanvasItemList;
|
||||
|
||||
|
||||
class QtCanvasItemExtra;
|
||||
|
||||
class QtCanvasItem
|
||||
{
|
||||
public:
|
||||
QtCanvasItem(QtCanvas* canvas);
|
||||
virtual ~QtCanvasItem();
|
||||
|
||||
double x() const
|
||||
{ return myx; }
|
||||
double y() const
|
||||
{ return myy; }
|
||||
double z() const
|
||||
{ return myz; } // (depth)
|
||||
|
||||
virtual void moveBy(double dx, double dy);
|
||||
void move(double x, double y);
|
||||
void setX(double a) { move(a,y()); }
|
||||
void setY(double a) { move(x(),a); }
|
||||
void setZ(double a) { myz=a; changeChunks(); }
|
||||
|
||||
bool animated() const;
|
||||
virtual void setAnimated(bool y);
|
||||
virtual void setVelocity(double vx, double vy);
|
||||
void setXVelocity(double vx) { setVelocity(vx,yVelocity()); }
|
||||
void setYVelocity(double vy) { setVelocity(xVelocity(),vy); }
|
||||
double xVelocity() const;
|
||||
double yVelocity() const;
|
||||
virtual void advance(int stage);
|
||||
|
||||
virtual bool collidesWith(const QtCanvasItem*) const=0;
|
||||
|
||||
QtCanvasItemList collisions(bool exact /* NO DEFAULT */) const;
|
||||
|
||||
virtual void setCanvas(QtCanvas*);
|
||||
|
||||
virtual void draw(QPainter&)=0;
|
||||
|
||||
void show();
|
||||
void hide();
|
||||
|
||||
virtual void setVisible(bool yes);
|
||||
bool isVisible() const
|
||||
{ return (bool)vis; }
|
||||
virtual void setSelected(bool yes);
|
||||
bool isSelected() const
|
||||
{ return (bool)sel; }
|
||||
virtual void setEnabled(bool yes);
|
||||
bool isEnabled() const
|
||||
{ return (bool)ena; }
|
||||
virtual void setActive(bool yes);
|
||||
bool isActive() const
|
||||
{ return (bool)act; }
|
||||
bool visible() const
|
||||
{ return (bool)vis; }
|
||||
bool selected() const
|
||||
{ return (bool)sel; }
|
||||
bool enabled() const
|
||||
{ return (bool)ena; }
|
||||
bool active() const
|
||||
{ return (bool)act; }
|
||||
|
||||
enum RttiValues {
|
||||
Rtti_Item = 0,
|
||||
Rtti_Sprite = 1,
|
||||
Rtti_PolygonalItem = 2,
|
||||
Rtti_Text = 3,
|
||||
Rtti_Polygon = 4,
|
||||
Rtti_Rectangle = 5,
|
||||
Rtti_Ellipse = 6,
|
||||
Rtti_Line = 7,
|
||||
Rtti_Spline = 8
|
||||
};
|
||||
|
||||
virtual int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
virtual QRect boundingRect() const=0;
|
||||
virtual QRect boundingRectAdvanced() const;
|
||||
|
||||
QtCanvas* canvas() const
|
||||
{ return cnv; }
|
||||
|
||||
protected:
|
||||
void update() { changeChunks(); }
|
||||
|
||||
private:
|
||||
// For friendly subclasses...
|
||||
|
||||
friend class QtCanvasPolygonalItem;
|
||||
friend class QtCanvasSprite;
|
||||
friend class QtCanvasRectangle;
|
||||
friend class QtCanvasPolygon;
|
||||
friend class QtCanvasEllipse;
|
||||
friend class QtCanvasText;
|
||||
friend class QtCanvasLine;
|
||||
|
||||
virtual QPolygon chunks() const;
|
||||
virtual void addToChunks();
|
||||
virtual void removeFromChunks();
|
||||
virtual void changeChunks();
|
||||
virtual bool collidesWith(const QtCanvasSprite*,
|
||||
const QtCanvasPolygonalItem*,
|
||||
const QtCanvasRectangle*,
|
||||
const QtCanvasEllipse*,
|
||||
const QtCanvasText*) const = 0;
|
||||
// End of friend stuff
|
||||
|
||||
QtCanvas* cnv;
|
||||
static QtCanvas* current_canvas;
|
||||
double myx,myy,myz;
|
||||
QtCanvasItemExtra *ext;
|
||||
QtCanvasItemExtra& extra();
|
||||
uint ani:1;
|
||||
uint vis:1;
|
||||
uint val:1;
|
||||
uint sel:1;
|
||||
uint ena:1;
|
||||
uint act:1;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasData;
|
||||
|
||||
class QtCanvas : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QtCanvas(QObject* parent = 0);
|
||||
QtCanvas(int w, int h);
|
||||
QtCanvas(QPixmap p, int h, int v, int tilewidth, int tileheight);
|
||||
|
||||
virtual ~QtCanvas();
|
||||
|
||||
virtual void setTiles(QPixmap tiles, int h, int v,
|
||||
int tilewidth, int tileheight);
|
||||
virtual void setBackgroundPixmap(const QPixmap& p);
|
||||
QPixmap backgroundPixmap() const;
|
||||
|
||||
virtual void setBackgroundColor(const QColor& c);
|
||||
QColor backgroundColor() const;
|
||||
|
||||
virtual void setTile(int x, int y, int tilenum);
|
||||
int tile(int x, int y) const
|
||||
{ return grid[x+y*htiles]; }
|
||||
|
||||
int tilesHorizontally() const
|
||||
{ return htiles; }
|
||||
int tilesVertically() const
|
||||
{ return vtiles; }
|
||||
|
||||
int tileWidth() const
|
||||
{ return tilew; }
|
||||
int tileHeight() const
|
||||
{ return tileh; }
|
||||
|
||||
virtual void resize(int width, int height);
|
||||
int width() const
|
||||
{ return awidth; }
|
||||
int height() const
|
||||
{ return aheight; }
|
||||
QSize size() const
|
||||
{ return QSize(awidth,aheight); }
|
||||
QRect rect() const
|
||||
{ return QRect(0, 0, awidth, aheight); }
|
||||
bool onCanvas(int x, int y) const
|
||||
{ return x>=0 && y>=0 && x<awidth && y<aheight; }
|
||||
bool onCanvas(const QPoint& p) const
|
||||
{ return onCanvas(p.x(),p.y()); }
|
||||
bool validChunk(int x, int y) const
|
||||
{ return x>=0 && y>=0 && x<chwidth && y<chheight; }
|
||||
bool validChunk(const QPoint& p) const
|
||||
{ return validChunk(p.x(),p.y()); }
|
||||
|
||||
int chunkSize() const
|
||||
{ return chunksize; }
|
||||
virtual void retune(int chunksize, int maxclusters=100);
|
||||
|
||||
bool sameChunk(int x1, int y1, int x2, int y2) const
|
||||
{ return x1/chunksize==x2/chunksize && y1/chunksize==y2/chunksize; }
|
||||
virtual void setChangedChunk(int i, int j);
|
||||
virtual void setChangedChunkContaining(int x, int y);
|
||||
virtual void setAllChanged();
|
||||
virtual void setChanged(const QRect& area);
|
||||
virtual void setUnchanged(const QRect& area);
|
||||
|
||||
// These call setChangedChunk.
|
||||
void addItemToChunk(QtCanvasItem*, int i, int j);
|
||||
void removeItemFromChunk(QtCanvasItem*, int i, int j);
|
||||
void addItemToChunkContaining(QtCanvasItem*, int x, int y);
|
||||
void removeItemFromChunkContaining(QtCanvasItem*, int x, int y);
|
||||
|
||||
QtCanvasItemList allItems();
|
||||
QtCanvasItemList collisions(const QPoint&) const;
|
||||
QtCanvasItemList collisions(const QRect&) const;
|
||||
QtCanvasItemList collisions(const QPolygon& pa, const QtCanvasItem* item,
|
||||
bool exact) const;
|
||||
|
||||
void drawArea(const QRect&, QPainter* p, bool double_buffer=false);
|
||||
|
||||
// These are for QtCanvasView to call
|
||||
virtual void addView(QtCanvasView*);
|
||||
virtual void removeView(QtCanvasView*);
|
||||
|
||||
void drawCanvasArea(const QRect&, QPainter* p=0, bool double_buffer=true);
|
||||
void drawViewArea(QtCanvasView* view, QPainter* p, const QRect& r, bool dbuf);
|
||||
|
||||
// These are for QtCanvasItem to call
|
||||
virtual void addItem(QtCanvasItem*);
|
||||
virtual void addAnimation(QtCanvasItem*);
|
||||
virtual void removeItem(QtCanvasItem*);
|
||||
virtual void removeAnimation(QtCanvasItem*);
|
||||
|
||||
virtual void setAdvancePeriod(int ms);
|
||||
virtual void setUpdatePeriod(int ms);
|
||||
|
||||
signals:
|
||||
void resized();
|
||||
|
||||
public slots:
|
||||
virtual void advance();
|
||||
virtual void update();
|
||||
|
||||
protected:
|
||||
virtual void drawBackground(QPainter&, const QRect& area);
|
||||
virtual void drawForeground(QPainter&, const QRect& area);
|
||||
|
||||
private:
|
||||
void init(int w, int h, int chunksze=16, int maxclust=100);
|
||||
|
||||
QtCanvasChunk& chunk(int i, int j) const;
|
||||
QtCanvasChunk& chunkContaining(int x, int y) const;
|
||||
|
||||
QRect changeBounds();
|
||||
|
||||
int awidth,aheight;
|
||||
int chunksize;
|
||||
int maxclusters;
|
||||
int chwidth,chheight;
|
||||
QtCanvasChunk* chunks;
|
||||
|
||||
QtCanvasData* d;
|
||||
|
||||
void initTiles(QPixmap p, int h, int v, int tilewidth, int tileheight);
|
||||
ushort *grid;
|
||||
ushort htiles;
|
||||
ushort vtiles;
|
||||
ushort tilew;
|
||||
ushort tileh;
|
||||
bool oneone;
|
||||
QPixmap pm;
|
||||
QTimer* update_timer;
|
||||
QColor bgcolor;
|
||||
bool debug_redraw_areas;
|
||||
|
||||
friend void qt_unview(QtCanvas* c);
|
||||
|
||||
Q_DISABLE_COPY(QtCanvas)
|
||||
};
|
||||
|
||||
class QtCanvasViewData;
|
||||
|
||||
class QtCanvasView : public QScrollArea
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool highQualityRendering READ highQualityRendering WRITE setHighQualityRendering)
|
||||
public:
|
||||
|
||||
QtCanvasView(QWidget* parent=0);
|
||||
QtCanvasView(QtCanvas* viewing, QWidget* parent=0);
|
||||
~QtCanvasView();
|
||||
|
||||
QtCanvas* canvas() const
|
||||
{ return viewing; }
|
||||
void setCanvas(QtCanvas* v);
|
||||
|
||||
const QMatrix &worldMatrix() const;
|
||||
const QMatrix &inverseWorldMatrix() const;
|
||||
bool setWorldMatrix(const QMatrix &);
|
||||
|
||||
virtual QSize sizeHint() const;
|
||||
|
||||
bool highQualityRendering() const;
|
||||
public slots:
|
||||
void setHighQualityRendering(bool enable);
|
||||
|
||||
protected:
|
||||
friend class QtCanvasWidget;
|
||||
virtual void drawContents(QPainter *p, int cx, int cy, int cw, int ch);
|
||||
|
||||
virtual void contentsMousePressEvent( QMouseEvent* );
|
||||
virtual void contentsMouseReleaseEvent( QMouseEvent* );
|
||||
virtual void contentsMouseDoubleClickEvent( QMouseEvent* );
|
||||
virtual void contentsMouseMoveEvent( QMouseEvent* );
|
||||
virtual void contentsDragEnterEvent( QDragEnterEvent * );
|
||||
virtual void contentsDragMoveEvent( QDragMoveEvent * );
|
||||
virtual void contentsDragLeaveEvent( QDragLeaveEvent * );
|
||||
virtual void contentsDropEvent( QDropEvent * );
|
||||
virtual void contentsWheelEvent( QWheelEvent * );
|
||||
virtual void contentsContextMenuEvent( QContextMenuEvent * );
|
||||
|
||||
private:
|
||||
friend class QtCanvas;
|
||||
void drawContents(QPainter*);
|
||||
QtCanvas* viewing;
|
||||
QtCanvasViewData* d;
|
||||
|
||||
private slots:
|
||||
void updateContentsSize();
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QtCanvasView)
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasPixmap : public QPixmap
|
||||
{
|
||||
public:
|
||||
#ifndef QT_NO_IMAGEIO
|
||||
QtCanvasPixmap(const QString& datafilename);
|
||||
#endif
|
||||
QtCanvasPixmap(const QImage& image);
|
||||
QtCanvasPixmap(const QPixmap&, const QPoint& hotspot);
|
||||
~QtCanvasPixmap();
|
||||
|
||||
int offsetX() const
|
||||
{ return hotx; }
|
||||
int offsetY() const
|
||||
{ return hoty; }
|
||||
void setOffset(int x, int y) { hotx = x; hoty = y; }
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QtCanvasPixmap)
|
||||
|
||||
void init(const QImage&);
|
||||
void init(const QPixmap& pixmap, int hx, int hy);
|
||||
|
||||
friend class QtCanvasSprite;
|
||||
friend class QtCanvasPixmapArray;
|
||||
friend bool qt_testCollision(const QtCanvasSprite* s1, const QtCanvasSprite* s2);
|
||||
|
||||
int hotx,hoty;
|
||||
|
||||
QImage* collision_mask;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasPixmapArray
|
||||
{
|
||||
public:
|
||||
QtCanvasPixmapArray();
|
||||
#ifndef QT_NO_IMAGEIO
|
||||
QtCanvasPixmapArray(const QString& datafilenamepattern, int framecount=0);
|
||||
#endif
|
||||
QtCanvasPixmapArray(const QList<QPixmap> &pixmaps, const QPolygon &hotspots = QPolygon());
|
||||
~QtCanvasPixmapArray();
|
||||
|
||||
#ifndef QT_NO_IMAGEIO
|
||||
bool readPixmaps(const QString& datafilenamepattern, int framecount=0);
|
||||
bool readCollisionMasks(const QString& filenamepattern);
|
||||
#endif
|
||||
|
||||
// deprecated
|
||||
bool operator!(); // Failure check.
|
||||
bool isValid() const;
|
||||
|
||||
QtCanvasPixmap* image(int i) const
|
||||
{ return img ? img[i] : 0; }
|
||||
void setImage(int i, QtCanvasPixmap* p);
|
||||
uint count() const
|
||||
{ return (uint)framecount; }
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QtCanvasPixmapArray)
|
||||
|
||||
#ifndef QT_NO_IMAGEIO
|
||||
bool readPixmaps(const QString& datafilenamepattern, int framecount, bool maskonly);
|
||||
#endif
|
||||
|
||||
void reset();
|
||||
int framecount;
|
||||
QtCanvasPixmap** img;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasSprite : public QtCanvasItem
|
||||
{
|
||||
public:
|
||||
QtCanvasSprite(QtCanvasPixmapArray* array, QtCanvas* canvas);
|
||||
|
||||
void setSequence(QtCanvasPixmapArray* seq);
|
||||
|
||||
virtual ~QtCanvasSprite();
|
||||
|
||||
void move(double x, double y);
|
||||
virtual void move(double x, double y, int frame);
|
||||
void setFrame(int);
|
||||
enum FrameAnimationType { Cycle, Oscillate };
|
||||
virtual void setFrameAnimation(FrameAnimationType=Cycle, int step=1, int state=0);
|
||||
int frame() const
|
||||
{ return frm; }
|
||||
int frameCount() const
|
||||
{ return images->count(); }
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
bool collidesWith(const QtCanvasItem*) const;
|
||||
|
||||
QRect boundingRect() const;
|
||||
|
||||
// is there a reason for these to be protected? Lars
|
||||
//protected:
|
||||
|
||||
int width() const;
|
||||
int height() const;
|
||||
|
||||
int leftEdge() const;
|
||||
int topEdge() const;
|
||||
int rightEdge() const;
|
||||
int bottomEdge() const;
|
||||
|
||||
int leftEdge(int nx) const;
|
||||
int topEdge(int ny) const;
|
||||
int rightEdge(int nx) const;
|
||||
int bottomEdge(int ny) const;
|
||||
QtCanvasPixmap* image() const
|
||||
{ return images->image(frm); }
|
||||
virtual QtCanvasPixmap* imageAdvanced() const;
|
||||
QtCanvasPixmap* image(int f) const
|
||||
{ return images->image(f); }
|
||||
virtual void advance(int stage);
|
||||
|
||||
public:
|
||||
void draw(QPainter& painter);
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QtCanvasSprite)
|
||||
|
||||
void addToChunks();
|
||||
void removeFromChunks();
|
||||
void changeChunks();
|
||||
|
||||
int frm;
|
||||
ushort anim_val;
|
||||
uint anim_state:2;
|
||||
uint anim_type:14;
|
||||
bool collidesWith(const QtCanvasSprite*,
|
||||
const QtCanvasPolygonalItem*,
|
||||
const QtCanvasRectangle*,
|
||||
const QtCanvasEllipse*,
|
||||
const QtCanvasText*) const;
|
||||
|
||||
friend bool qt_testCollision(const QtCanvasSprite* s1,
|
||||
const QtCanvasSprite* s2);
|
||||
|
||||
QtCanvasPixmapArray* images;
|
||||
};
|
||||
|
||||
class QPolygonalProcessor;
|
||||
|
||||
class QtCanvasPolygonalItem : public QtCanvasItem
|
||||
{
|
||||
public:
|
||||
QtCanvasPolygonalItem(QtCanvas* canvas);
|
||||
virtual ~QtCanvasPolygonalItem();
|
||||
|
||||
bool collidesWith(const QtCanvasItem*) const;
|
||||
|
||||
virtual void setPen(QPen p);
|
||||
virtual void setBrush(QBrush b);
|
||||
|
||||
QPen pen() const
|
||||
{ return pn; }
|
||||
QBrush brush() const
|
||||
{ return br; }
|
||||
|
||||
virtual QPolygon areaPoints() const=0;
|
||||
virtual QPolygon areaPointsAdvanced() const;
|
||||
QRect boundingRect() const;
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
protected:
|
||||
void draw(QPainter &);
|
||||
virtual void drawShape(QPainter &) = 0;
|
||||
|
||||
bool winding() const;
|
||||
void setWinding(bool);
|
||||
|
||||
void invalidate();
|
||||
bool isValid() const
|
||||
{ return (bool)val; }
|
||||
|
||||
private:
|
||||
void scanPolygon(const QPolygon& pa, int winding,
|
||||
QPolygonalProcessor& process) const;
|
||||
QPolygon chunks() const;
|
||||
|
||||
bool collidesWith(const QtCanvasSprite*,
|
||||
const QtCanvasPolygonalItem*,
|
||||
const QtCanvasRectangle*,
|
||||
const QtCanvasEllipse*,
|
||||
const QtCanvasText*) const;
|
||||
|
||||
QBrush br;
|
||||
QPen pn;
|
||||
uint wind:1;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasRectangle : public QtCanvasPolygonalItem
|
||||
{
|
||||
public:
|
||||
QtCanvasRectangle(QtCanvas* canvas);
|
||||
QtCanvasRectangle(const QRect&, QtCanvas* canvas);
|
||||
QtCanvasRectangle(int x, int y, int width, int height, QtCanvas* canvas);
|
||||
|
||||
~QtCanvasRectangle();
|
||||
|
||||
int width() const;
|
||||
int height() const;
|
||||
void setSize(int w, int h);
|
||||
QSize size() const
|
||||
{ return QSize(w,h); }
|
||||
QPolygon areaPoints() const;
|
||||
QRect rect() const
|
||||
{ return QRect(int(x()),int(y()),w,h); }
|
||||
|
||||
bool collidesWith(const QtCanvasItem*) const;
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
protected:
|
||||
void drawShape(QPainter &);
|
||||
QPolygon chunks() const;
|
||||
|
||||
private:
|
||||
bool collidesWith( const QtCanvasSprite*,
|
||||
const QtCanvasPolygonalItem*,
|
||||
const QtCanvasRectangle*,
|
||||
const QtCanvasEllipse*,
|
||||
const QtCanvasText*) const;
|
||||
|
||||
int w, h;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasPolygon : public QtCanvasPolygonalItem
|
||||
{
|
||||
public:
|
||||
QtCanvasPolygon(QtCanvas* canvas);
|
||||
~QtCanvasPolygon();
|
||||
void setPoints(QPolygon);
|
||||
QPolygon points() const;
|
||||
void moveBy(double dx, double dy);
|
||||
|
||||
QPolygon areaPoints() const;
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
protected:
|
||||
void drawShape(QPainter &);
|
||||
QPolygon poly;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasSpline : public QtCanvasPolygon
|
||||
{
|
||||
public:
|
||||
QtCanvasSpline(QtCanvas* canvas);
|
||||
~QtCanvasSpline();
|
||||
|
||||
void setControlPoints(QPolygon, bool closed=true);
|
||||
QPolygon controlPoints() const;
|
||||
bool closed() const;
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
private:
|
||||
void recalcPoly();
|
||||
QPolygon bez;
|
||||
bool cl;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasLine : public QtCanvasPolygonalItem
|
||||
{
|
||||
public:
|
||||
QtCanvasLine(QtCanvas* canvas);
|
||||
~QtCanvasLine();
|
||||
void setPoints(int x1, int y1, int x2, int y2);
|
||||
|
||||
QPoint startPoint() const
|
||||
{ return QPoint(x1,y1); }
|
||||
QPoint endPoint() const
|
||||
{ return QPoint(x2,y2); }
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
void setPen(QPen p);
|
||||
void moveBy(double dx, double dy);
|
||||
|
||||
protected:
|
||||
void drawShape(QPainter &);
|
||||
QPolygon areaPoints() const;
|
||||
|
||||
private:
|
||||
int x1,y1,x2,y2;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasEllipse : public QtCanvasPolygonalItem
|
||||
{
|
||||
|
||||
public:
|
||||
QtCanvasEllipse(QtCanvas* canvas);
|
||||
QtCanvasEllipse(int width, int height, QtCanvas* canvas);
|
||||
QtCanvasEllipse(int width, int height, int startangle, int angle,
|
||||
QtCanvas* canvas);
|
||||
|
||||
~QtCanvasEllipse();
|
||||
|
||||
int width() const;
|
||||
int height() const;
|
||||
void setSize(int w, int h);
|
||||
void setAngles(int start, int length);
|
||||
int angleStart() const
|
||||
{ return a1; }
|
||||
int angleLength() const
|
||||
{ return a2; }
|
||||
QPolygon areaPoints() const;
|
||||
|
||||
bool collidesWith(const QtCanvasItem*) const;
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
protected:
|
||||
void drawShape(QPainter &);
|
||||
|
||||
private:
|
||||
bool collidesWith(const QtCanvasSprite*,
|
||||
const QtCanvasPolygonalItem*,
|
||||
const QtCanvasRectangle*,
|
||||
const QtCanvasEllipse*,
|
||||
const QtCanvasText*) const;
|
||||
int w, h;
|
||||
int a1, a2;
|
||||
};
|
||||
|
||||
|
||||
class QtCanvasTextExtra;
|
||||
|
||||
class QtCanvasText : public QtCanvasItem
|
||||
{
|
||||
public:
|
||||
QtCanvasText(QtCanvas* canvas);
|
||||
QtCanvasText(const QString&, QtCanvas* canvas);
|
||||
QtCanvasText(const QString&, QFont, QtCanvas* canvas);
|
||||
|
||||
virtual ~QtCanvasText();
|
||||
|
||||
void setText(const QString&);
|
||||
void setFont(const QFont&);
|
||||
void setColor(const QColor&);
|
||||
QString text() const;
|
||||
QFont font() const;
|
||||
QColor color() const;
|
||||
|
||||
void moveBy(double dx, double dy);
|
||||
|
||||
int textFlags() const
|
||||
{ return flags; }
|
||||
void setTextFlags(int);
|
||||
|
||||
QRect boundingRect() const;
|
||||
|
||||
bool collidesWith(const QtCanvasItem*) const;
|
||||
|
||||
int rtti() const;
|
||||
static int RTTI;
|
||||
|
||||
protected:
|
||||
virtual void draw(QPainter&);
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QtCanvasText)
|
||||
|
||||
void addToChunks();
|
||||
void removeFromChunks();
|
||||
void changeChunks();
|
||||
|
||||
void setRect();
|
||||
QRect brect;
|
||||
QString txt;
|
||||
int flags;
|
||||
QFont fnt;
|
||||
QColor col;
|
||||
QtCanvasTextExtra* extra;
|
||||
|
||||
bool collidesWith(const QtCanvasSprite*,
|
||||
const QtCanvasPolygonalItem*,
|
||||
const QtCanvasRectangle*,
|
||||
const QtCanvasEllipse*,
|
||||
const QtCanvasText*) const;
|
||||
};
|
||||
|
||||
#endif // QTCANVAS_H
|
||||
Reference in New Issue
Block a user