1 /*
2  * abstractworldtool.h
3  * Copyright 2019, Nils Kuebler <nils-kuebler@web.de>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include "abstracttool.h"
24 
25 class QAction;
26 
27 namespace Tiled {
28 
29 struct World;
30 
31 class SelectionRectangle;
32 
33 /**
34  * A convenient base class for tools that work on object layers. Implements
35  * the standard context menu.
36  */
37 class AbstractWorldTool : public AbstractTool
38 {
39     Q_OBJECT
40     Q_INTERFACES(Tiled::AbstractTool)
41 
42 public:
43     /**
44      * Constructs an abstract object tool with the given \a name and \a icon.
45      */
46     AbstractWorldTool(Id id,
47                       const QString &name,
48                       const QIcon &icon,
49                       const QKeySequence &shortcut,
50                       QObject *parent = nullptr);
51     ~AbstractWorldTool() override;
52 
53     void activate(MapScene *scene) override;
54     void deactivate(MapScene *scene) override;
55 
56     void mouseLeft() override;
57     void mouseMoved(const QPointF &pos, Qt::KeyboardModifiers modifiers) override;
58     void mousePressed(QGraphicsSceneMouseEvent *event) override;
59 
60     void languageChanged() override;
61 
62     QUndoStack *undoStack() override;
63     void populateToolBar(QToolBar*) override;
64 
65 protected:
66     /**
67      * Overridden to only enable this tool when the currently has a world
68      * loaded.
69      */
70     void updateEnabledState() override;
71 
72     MapDocument *mapAt(const QPointF &pos) const;
73 
74     void addAnotherMapToWorldAtCenter();
75     void addAnotherMapToWorld(QPoint insertPos);
76     void removeCurrentMapFromWorld();
77     void removeFromWorld(const QString &mapFileName);
78     void addToWorld(const World *world);
79 
80     QPoint snapPoint(QPoint point, MapDocument *document) const;
81 
82     void setTargetMap(MapDocument *mapDocument);
targetMap()83     MapDocument *targetMap() const { return mTargetMap; }
84     void updateSelectionRectangle();
85 
86     bool mapCanBeMoved(MapDocument *mapDocument) const;
87     QRect mapRect(MapDocument *mapDocument) const;
88     const World *constWorld(MapDocument *mapDocument) const;
89 
90     void showContextMenu(QGraphicsSceneMouseEvent *);
91 
92 private:
93     MapDocument *mTargetMap = nullptr;
94 
95     QAction *mAddAnotherMapToWorldAction;
96     QAction *mAddMapToWorldAction;
97     QAction *mRemoveMapFromWorldAction;
98 
99     std::unique_ptr<SelectionRectangle> mSelectionRectangle;
100 };
101 
102 } // namespace Tiled
103