Skip to content

Commit

Permalink
ScalableImage initializes image to given QRect
Browse files Browse the repository at this point in the history
  • Loading branch information
hrobeers committed Nov 4, 2013
1 parent 86e1d99 commit 6452223
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/patheditor/patheditor.pri
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ HEADERS += \
../patheditor/controlpoint.h \
../patheditor/patheditorview.h \
../patheditor/pathfunctors.h \
../patheditor/path.h
../patheditor/path.h \
../patheditor/scalepoint.h \
../patheditor/scalableimage.h

SOURCES += \
../patheditor/editablepath.cpp \
Expand All @@ -31,7 +33,9 @@ SOURCES += \
../patheditor/quadrantrestrictor.cpp \
../patheditor/controlpoint.cpp \
../patheditor/patheditorview.cpp \
../patheditor/path.cpp
../patheditor/path.cpp \
../patheditor/scalepoint.cpp \
../patheditor/scalableimage.cpp

OTHER_FILES += \
../patheditor/patheditorfwd/README
2 changes: 2 additions & 0 deletions src/patheditor/patheditorfwd/patheditorfwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace patheditor
class Restrictor;
class QuadrantRestrictor;
class PathEditorView;
class ScalePoint;
class ScalableImage;
}

#endif // PATHEDITORFWD_H
12 changes: 5 additions & 7 deletions src/patheditor/patheditorview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "patheditorview.h"
#include <QDragMoveEvent>
#include <QUrl>
#include <QGraphicsPixmapItem>
#include "scalableimage.h"

#define MIN_UNIT_SIZE 5

Expand Down Expand Up @@ -58,16 +58,14 @@ void PathEditorView::setImage(const QUrl &url)
QPixmap image(url.path());
if (!image.isNull())
{
_imageItem = new QGraphicsPixmapItem(image);
QRect position = this->rect();
position.setHeight(position.height() * 0.9);
position.translate(0, -position.height());
_imageItem = new ScalableImage(image, position);
_imageItem->setFlag(QGraphicsItem::ItemIsMovable);
_imageItem->setZValue(-1);
_imageItem->setOpacity(0.7);

// transform image
qreal scaleFactor = _viewRect.height() / _imageItem->boundingRect().height();
_imageItem->scale(scaleFactor, scaleFactor);
_imageItem->moveBy(0, -_imageItem->boundingRect().height() * scaleFactor);

scene()->addItem(_imageItem);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/patheditor/patheditorview.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifndef PATHEDITORVIEW_H
#define PATHEDITORVIEW_H

#include "patheditorfwd/patheditorfwd.h"

#include <QGraphicsView>

namespace patheditor
Expand Down Expand Up @@ -50,7 +52,7 @@ namespace patheditor

private:
qreal _pxPerUnit;
QGraphicsPixmapItem* _imageItem;
ScalableImage* _imageItem;
QRectF _viewRect;

void drawLinesWithInterval(qreal px, QPainter *painter, const QRectF &rect);
Expand Down
11 changes: 11 additions & 0 deletions src/patheditor/pathsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ PathSettings::PathSettings()
QColor red(Qt::red);
red.setAlpha(100);
_controlPointBrush.setColor(red);

// scalePointBrush
_scalePointBrush.setStyle(Qt::SolidPattern);
QColor green(Qt::green);
green.setAlpha(100);
_scalePointBrush.setColor(green);
}

PathSettings PathSettings::Default()
Expand Down Expand Up @@ -78,3 +84,8 @@ QBrush &PathSettings::controlPointBrush()
{
return _controlPointBrush;
}

QBrush &PathSettings::scalePointBrush()
{
return _scalePointBrush;
}
2 changes: 2 additions & 0 deletions src/patheditor/pathsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace patheditor

QBrush& pointBrush();
QBrush& controlPointBrush();
QBrush& scalePointBrush();

private:
int _lineWidth;
Expand All @@ -54,6 +55,7 @@ namespace patheditor

QBrush _pointBrush;
QBrush _controlPointBrush;
QBrush _scalePointBrush;
};
}

Expand Down
62 changes: 62 additions & 0 deletions src/patheditor/scalableimage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/****************************************************************************
Copyright (c) 2013, Hans Robeers
All rights reserved.
BSD 2-Clause License
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.
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 HOLDER 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 "scalableimage.h"
#include "pathsettings.h"
#include <QPainter>
#include "qmath.h"

using namespace patheditor;

ScalableImage::ScalableImage(const QPixmap &pixmap, const QRect &initialRect, QGraphicsItem *parent) :
QGraphicsObject(parent)
{
_pixmap = pixmap;
_rect = initialRect;

qreal imageAR = (qreal)pixmap.height() / (qreal)pixmap.width();
qreal rectAR = (qreal)_rect.height() / (qreal)_rect.width();
if (imageAR > rectAR)
_rect.setWidth(_rect.height() / imageAR);
else
_rect.setHeight(_rect.width() * imageAR);


_scalePoint.reset(new ScalePoint(_rect.topRight().x(), _rect.topRight().y()));

PathSettings settings = PathSettings::Default();
_scalePoint->createPointHandle(settings, this);
}

void ScalableImage::paint(QPainter *painter, const QStyleOptionGraphicsItem */*unused*/, QWidget */*unused*/)
{
painter->drawPixmap(_rect, _pixmap);
}

QRectF ScalableImage::boundingRect() const
{
return QRectF(_rect);
}

ScalableImage::~ScalableImage()
{
}
53 changes: 53 additions & 0 deletions src/patheditor/scalableimage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/****************************************************************************
Copyright (c) 2013, Hans Robeers
All rights reserved.
BSD 2-Clause License
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.
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 HOLDER 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 SCALABLEIMAGE_H
#define SCALABLEIMAGE_H

#include <QGraphicsObject>
#include "scalepoint.h"

namespace patheditor
{
class ScalableImage : public QGraphicsObject
{
Q_OBJECT
public:
explicit ScalableImage(const QPixmap &pixmap, const QRect &initialRect, QGraphicsItem *parent = 0);

virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
virtual QRectF boundingRect() const;

virtual ~ScalableImage();

signals:

public slots:

private:
QPixmap _pixmap;
QRect _rect;
QScopedPointer<ScalePoint> _scalePoint;
};
}

#endif // SCALABLEIMAGE_H
38 changes: 38 additions & 0 deletions src/patheditor/scalepoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/****************************************************************************
Copyright (c) 2013, Hans Robeers
All rights reserved.
BSD 2-Clause License
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.
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 HOLDER 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 "scalepoint.h"
#include "pathsettings.h"

using namespace patheditor;

ScalePoint::ScalePoint(qreal xpos, qreal ypos) :
PathPoint(xpos, ypos)
{
}

void ScalePoint::createPointHandle(PathSettings &settings, QGraphicsItem *parent)
{
PointHandle *newPointHandle = new PointHandle(this, settings.handleSize(), settings.scalePointBrush(), parent);
newPointHandle->setZValue(1);
replaceCurrentPointHandle(newPointHandle);
}
41 changes: 41 additions & 0 deletions src/patheditor/scalepoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/****************************************************************************
Copyright (c) 2013, Hans Robeers
All rights reserved.
BSD 2-Clause License
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.
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 HOLDER 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 SCALEPOINT_H
#define SCALEPOINT_H

#include "pathpoint.h"

namespace patheditor
{
class ScalePoint : public PathPoint
{
public:
explicit ScalePoint(qreal xpos, qreal ypos);

virtual void createPointHandle(PathSettings &settings, QGraphicsItem *parent);

virtual ~ScalePoint() {}
};
}

#endif // SCALEPOINT_H
4 changes: 2 additions & 2 deletions src/version_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef VERSION_AUTOGEN_H
#define VERSION_AUTOGEN_H

#define BUILD_NUMBER 228
#define COMMIT_HASH "f0c9930e1646a086b544560a058e9dd7b2756ce7"
#define BUILD_NUMBER 229
#define COMMIT_HASH "86e1d99c2a11ea74a05499951e9512cceb302dac"

#endif // VERSION_AUTOGEN_H

0 comments on commit 6452223

Please sign in to comment.