Skip to content
Permalink
Browse files

Refactored the MiniMapRenderer to no longer be a singleton

It was made a singleton to be able to set the relevant map document to
be used by the MiniMapRenderer when used by the ResizeHelper. Instead,
the ResizeHelper now has a callback that can be set to customize how it
will draw the mini-map, so that it doesn't need to explicitly access the
MiniMapRenderer anymore.
  • Loading branch information...
bjorn committed Apr 14, 2017
1 parent 245c013 commit 62a89e1c099d95e769bf1859eacaa624b51ba484
@@ -48,6 +48,7 @@
#include "maprenderer.h"
#include "mapscene.h"
#include "mapview.h"
#include "minimaprenderer.h"
#include "newmapdialog.h"
#include "newtilesetdialog.h"
#include "objectgroup.h"
@@ -1287,6 +1288,14 @@ void MainWindow::resizeMap()

ResizeDialog resizeDialog(this);
resizeDialog.setOldSize(map->size());
resizeDialog.setMiniMapRenderer([mapDocument](QSize size){
QImage image(size, QImage::Format_ARGB32_Premultiplied);
MiniMapRenderer(mapDocument).renderToImage(image, MiniMapRenderer::DrawObjects
| MiniMapRenderer::DrawImages
| MiniMapRenderer::DrawTiles
| MiniMapRenderer::IgnoreInvisibleLayer);
return image;
});

if (resizeDialog.exec()) {
const QSize &newSize = resizeDialog.newSize();
@@ -23,6 +23,7 @@

#include "documentmanager.h"
#include "map.h"
#include "mapdocument.h"
#include "maprenderer.h"
#include "mapview.h"
#include "utils.h"
@@ -46,7 +47,6 @@ MiniMap::MiniMap(QWidget *parent)
| MiniMapRenderer::DrawObjects
| MiniMapRenderer::DrawImages
| MiniMapRenderer::IgnoreInvisibleLayer)
, mMiniMapRenderer(MiniMapRenderer::instance())
{
setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
setMinimumSize(50, 50);
@@ -74,7 +74,6 @@ void MiniMap::setMapDocument(MapDocument *map)
}

mMapDocument = map;
mMiniMapRenderer.setMapDocument(mMapDocument);

if (mMapDocument) {
connect(mMapDocument->undoStack(), SIGNAL(indexChanged(int)),
@@ -195,7 +194,8 @@ void MiniMap::renderMapToImage()
if (imageSize.isEmpty())
return;

mMiniMapRenderer.renderToImage(mMapImage, mRenderFlags);
MiniMapRenderer miniMapRenderer(mMapDocument);
miniMapRenderer.renderToImage(mMapImage, mRenderFlags);
}

void MiniMap::centerViewOnLocalPixel(QPoint centerPos, int delta)
@@ -71,7 +71,6 @@ private slots:
bool mMouseMoveCursorState;
bool mRedrawMapImage;
MiniMapRenderer::RenderFlags mRenderFlags;
MiniMapRenderer& mMiniMapRenderer;

QRect viewportRect() const;
QPointF mapToScene(QPoint p) const;
@@ -23,6 +23,7 @@
#include "minimaprenderer.h"

#include "imagelayer.h"
#include "mapdocument.h"
#include "mapobject.h"
#include "mapobjectitem.h"
#include "maprenderer.h"
@@ -35,10 +36,9 @@
using namespace Tiled;
using namespace Tiled::Internal;

MiniMapRenderer& MiniMapRenderer::instance()
MiniMapRenderer::MiniMapRenderer(MapDocument *mapDocument)
: mMapDocument(mapDocument)
{
static MiniMapRenderer singletonRenderer;
return singletonRenderer;
}

static bool objectLessThan(const MapObject *a, const MapObject *b)
@@ -50,7 +50,7 @@ void MiniMapRenderer::renderToImage(QImage& image, RenderFlags renderFlags) cons
{
if (!mMapDocument)
return;

MapRenderer *renderer = mMapDocument->renderer();

bool drawObjects = renderFlags.testFlag(RenderFlag::DrawObjects);
@@ -133,16 +133,3 @@ void MiniMapRenderer::renderToImage(QImage& image, RenderFlags renderFlags) cons

renderer->setFlags(rendererFlags);
}

void MiniMapRenderer::setMapDocument(MapDocument* map)
{
mMapDocument = map;
}

MiniMapRenderer::MiniMapRenderer()
{
}

MiniMapRenderer::~MiniMapRenderer()
{
}
@@ -22,13 +22,13 @@

#pragma once

#include "mapdocument.h"

#include <QImage>

namespace Tiled {
namespace Internal {

class MapDocument;

class MiniMapRenderer
{
public:
@@ -42,16 +42,12 @@ class MiniMapRenderer

Q_DECLARE_FLAGS(RenderFlags, RenderFlag)

static MiniMapRenderer& instance();
void renderToImage(QImage& image, RenderFlags renderFlags) const;
void setMapDocument(MapDocument* map);
private:
MiniMapRenderer();
~MiniMapRenderer();
MiniMapRenderer(MiniMapRenderer const&) = delete;
MiniMapRenderer& operator= (MiniMapRenderer const&) = delete;
MiniMapRenderer(MapDocument *mapDocument);

MapDocument* mMapDocument;
void renderToImage(QImage &image, RenderFlags renderFlags) const;

private:
MapDocument *mMapDocument;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(Tiled::Internal::MiniMapRenderer::RenderFlags)
@@ -74,12 +74,12 @@ void ResizeDialog::setOldSize(const QSize &size)
mUi->heightSpinBox->setValue(size.height());
}

const QSize &ResizeDialog::newSize() const
QSize ResizeDialog::newSize() const
{
return mUi->resizeHelper->newSize();
}

const QPoint &ResizeDialog::offset() const
QPoint ResizeDialog::offset() const
{
return mUi->resizeHelper->offset();
}
@@ -89,6 +89,11 @@ bool ResizeDialog::removeObjects() const
return mUi->removeObjectsCheckBox->isChecked();
}

void ResizeDialog::setMiniMapRenderer(std::function<QImage (QSize)> renderer)
{
mUi->resizeHelper->setMiniMapRenderer(renderer);
}

void ResizeDialog::removeObjectsToggled(bool removeObjects)
{
Preferences *prefs = Preferences::instance();
@@ -22,6 +22,8 @@

#include <QDialog>

#include <functional>

namespace Ui {
class ResizeDialog;
}
@@ -40,11 +42,13 @@ class ResizeDialog : public QDialog

void setOldSize(const QSize &size);

const QSize &newSize() const;
const QPoint &offset() const;
QSize newSize() const;
QPoint offset() const;

bool removeObjects() const;

void setMiniMapRenderer(std::function<QImage (QSize)> renderer);

private slots:
void removeObjectsToggled(bool removeObjects);
void updateOffsetBounds(const QRect &bounds);
@@ -17,6 +17,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "resizehelper.h"

#include <QMouseEvent>
@@ -27,7 +28,6 @@ using namespace Tiled::Internal;

ResizeHelper::ResizeHelper(QWidget *parent)
: QWidget(parent)
, mMiniMapRenderer(MiniMapRenderer::instance())
, mZoom(0)
{
setMinimumSize(20, 20);
@@ -99,6 +99,11 @@ void ResizeHelper::setNewHeight(int height)
recalculateScale();
}

void ResizeHelper::setMiniMapRenderer(std::function<QImage (QSize)> renderer)
{
mMiniMapRenderer = renderer;
}

void ResizeHelper::paintEvent(QPaintEvent *)
{
const QSize _size = size() - QSize(2, 2);
@@ -124,7 +129,14 @@ void ResizeHelper::paintEvent(QPaintEvent *)
pen.setColor(Qt::white);

painter.setOpacity(0.5);
painter.drawImage(oldRect, mMinimap);

if (mMiniMap.isNull()) {
painter.setPen(pen);
painter.setBrush(Qt::white);
painter.drawRect(oldRect);
} else {
painter.drawImage(oldRect, mMiniMap);
}

pen.setColor(Qt::black);
pen.setStyle(Qt::DashLine);
@@ -198,15 +210,18 @@ void ResizeHelper::recalculateScale()

newScale += mZoom;

if (newScale != mScale) {
mScale = newScale;
mMinimap = QImage(mOldSize * mScale, QImage::Format_ARGB32_Premultiplied);
mMiniMapRenderer.renderToImage(mMinimap, MiniMapRenderer::DrawObjects
| MiniMapRenderer::DrawImages
| MiniMapRenderer::DrawTiles
| MiniMapRenderer::IgnoreInvisibleLayer);
if (newScale != mScale && mMiniMapRenderer) {
const qreal ratio =
#if QT_VERSION >= 0x050600
devicePixelRatioF();
#else
devicePixelRatio();
#endif
const QSize size = mOldSize * (newScale * ratio);
mMiniMap = mMiniMapRenderer(size);
}

mScale = newScale;
update();
}

@@ -20,12 +20,12 @@

#pragma once

#include "minimaprenderer.h"

#include <QPoint>
#include <QSize>
#include <QWidget>

#include <functional>

class QMouseEvent;
class QResizeEvent;

@@ -43,32 +43,22 @@ class ResizeHelper : public QWidget
public:
ResizeHelper(QWidget *parent = nullptr);

const QSize &oldSize() const
{ return mOldSize; }

const QSize &newSize() const
{ return mNewSize; }

const QPoint &offset() const
{ return mOffset; }
QSize oldSize() const { return mOldSize; }
QSize newSize() const { return mNewSize; }
QPoint offset() const { return mOffset; }
QRect offsetBounds() const { return mOffsetBounds; }

const QRect &offsetBounds() const
{ return mOffsetBounds; }
void setMiniMapRenderer(std::function<QImage (QSize)> renderer);

signals:
void offsetChanged(const QPoint &offset);

void offsetXChanged(int value);

void offsetYChanged(int value);

void offsetBoundsChanged(const QRect &bounds);

public slots:
void setOldSize(const QSize &size);

void setNewSize(const QSize &size);

void setOffset(const QPoint &offset);

/** Method to set only the X offset, provided for convenience. */
@@ -85,17 +75,12 @@ public slots:

protected:
void paintEvent(QPaintEvent *event) override;

void mousePressEvent(QMouseEvent *event) override;

void mouseMoveEvent(QMouseEvent *event) override;

void wheelEvent(QWheelEvent *event) override;

void resizeEvent(QResizeEvent *event) override;

void recalculateScale();

void recalculateMinMaxOffset();

private:
@@ -108,9 +93,9 @@ public slots:
bool mDragging;
double mScale;

MiniMapRenderer& mMiniMapRenderer;
QImage mMinimap;
QImage mMiniMap;
double mZoom;
std::function<QImage(QSize)> mMiniMapRenderer;
};

} // namespace Internal

0 comments on commit 62a89e1

Please sign in to comment.
You can’t perform that action at this time.