Mercurial > hg > orthanc-stone
comparison Framework/Scene2DViewport/ViewportController.cpp @ 860:238693c3bc51 am-dev
merge default -> am-dev
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Mon, 24 Jun 2019 14:35:00 +0200 |
parents | e3c56d4f863f |
children | a29c13497557 |
comparison
equal
deleted
inserted
replaced
856:a6e17a5a39e7 | 860:238693c3bc51 |
---|---|
17 * You should have received a copy of the GNU Affero General Public License | 17 * You should have received a copy of the GNU Affero General Public License |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | 19 **/ |
20 | 20 |
21 #include "ViewportController.h" | 21 #include "ViewportController.h" |
22 | |
23 #include "UndoStack.h" | |
22 #include "MeasureCommands.h" | 24 #include "MeasureCommands.h" |
23 | 25 |
24 #include <Framework/StoneException.h> | 26 #include "../StoneException.h" |
25 | 27 |
26 #include <boost/make_shared.hpp> | 28 #include <boost/make_shared.hpp> |
27 | 29 |
28 using namespace Orthanc; | |
29 | |
30 namespace OrthancStone | 30 namespace OrthancStone |
31 { | 31 { |
32 ViewportController::ViewportController(MessageBroker& broker) | 32 ViewportController::ViewportController(boost::weak_ptr<UndoStack> undoStackW, MessageBroker& broker) |
33 : IObservable(broker) | 33 : IObservable(broker) |
34 , numAppliedCommands_(0) | 34 , undoStackW_(undoStackW) |
35 , canvasToSceneFactor_(0.0) | |
35 { | 36 { |
36 scene_ = boost::make_shared<Scene2D>(); | 37 scene_ = boost::make_shared<Scene2D>(); |
37 } | 38 } |
38 | 39 |
39 Scene2DPtr ViewportController::GetScene() | 40 boost::shared_ptr<UndoStack> ViewportController::GetUndoStack() |
41 { | |
42 return undoStackW_.lock(); | |
43 } | |
44 | |
45 boost::shared_ptr<const UndoStack> ViewportController::GetUndoStack() const | |
46 { | |
47 return undoStackW_.lock(); | |
48 } | |
49 | |
50 void ViewportController::PushCommand(boost::shared_ptr<TrackerCommand> command) | |
51 { | |
52 GetUndoStack()->PushCommand(command); | |
53 } | |
54 | |
55 void ViewportController::Undo() | |
56 { | |
57 GetUndoStack()->Undo(); | |
58 } | |
59 | |
60 void ViewportController::Redo() | |
61 { | |
62 GetUndoStack()->Redo(); | |
63 } | |
64 | |
65 bool ViewportController::CanUndo() const | |
66 { | |
67 return GetUndoStack()->CanUndo(); | |
68 } | |
69 | |
70 bool ViewportController::CanRedo() const | |
71 { | |
72 return GetUndoStack()->CanRedo(); | |
73 } | |
74 | |
75 boost::shared_ptr<const Scene2D> ViewportController::GetScene() const | |
76 { | |
77 return scene_; | |
78 } | |
79 | |
80 boost::shared_ptr<Scene2D> ViewportController::GetScene() | |
40 { | 81 { |
41 return scene_; | 82 return scene_; |
42 } | 83 } |
43 | 84 |
44 bool ViewportController::HandlePointerEvent(PointerEvent e) | 85 bool ViewportController::HandlePointerEvent(PointerEvent e) |
45 { | 86 { |
46 throw StoneException(ErrorCode_NotImplemented); | 87 throw StoneException(ErrorCode_NotImplemented); |
47 } | 88 } |
48 | 89 |
49 std::vector<MeasureToolPtr> ViewportController::HitTestMeasureTools( | 90 std::vector<boost::shared_ptr<MeasureTool> > ViewportController::HitTestMeasureTools( |
50 ScenePoint2D p) | 91 ScenePoint2D p) |
51 { | 92 { |
52 std::vector<MeasureToolPtr> ret; | 93 std::vector<boost::shared_ptr<MeasureTool> > ret; |
53 | 94 |
54 | 95 for (size_t i = 0; i < measureTools_.size(); ++i) |
55 //for (size_t i = 0; i < measureTools_.size(); ++i) | 96 { |
56 //{ | 97 if (measureTools_[i]->HitTest(p)) |
57 | 98 ret.push_back(measureTools_[i]); |
58 //} | 99 } |
59 return ret; | 100 return ret; |
60 } | 101 } |
61 | 102 |
62 const OrthancStone::AffineTransform2D& ViewportController::GetCanvasToSceneTransform() const | 103 const OrthancStone::AffineTransform2D& ViewportController::GetCanvasToSceneTransform() const |
63 { | 104 { |
72 void ViewportController::SetSceneToCanvasTransform( | 113 void ViewportController::SetSceneToCanvasTransform( |
73 const AffineTransform2D& transform) | 114 const AffineTransform2D& transform) |
74 { | 115 { |
75 scene_->SetSceneToCanvasTransform(transform); | 116 scene_->SetSceneToCanvasTransform(transform); |
76 BroadcastMessage(SceneTransformChanged(*this)); | 117 BroadcastMessage(SceneTransformChanged(*this)); |
118 | |
119 // update the canvas to scene factor | |
120 canvasToSceneFactor_ = 0.0; | |
121 canvasToSceneFactor_ = GetCanvasToSceneFactor(); | |
77 } | 122 } |
78 | 123 |
79 void ViewportController::FitContent( | 124 void ViewportController::FitContent( |
80 unsigned int canvasWidth, unsigned int canvasHeight) | 125 unsigned int canvasWidth, unsigned int canvasHeight) |
81 { | 126 { |
82 scene_->FitContent(canvasWidth, canvasHeight); | 127 scene_->FitContent(canvasWidth, canvasHeight); |
83 BroadcastMessage(SceneTransformChanged(*this)); | 128 BroadcastMessage(SceneTransformChanged(*this)); |
84 } | 129 } |
85 | 130 |
86 void ViewportController::PushCommand(TrackerCommandPtr command) | 131 void ViewportController::AddMeasureTool(boost::shared_ptr<MeasureTool> measureTool) |
87 { | |
88 commandStack_.erase( | |
89 commandStack_.begin() + numAppliedCommands_, | |
90 commandStack_.end()); | |
91 | |
92 ORTHANC_ASSERT(std::find(commandStack_.begin(), commandStack_.end(), command) | |
93 == commandStack_.end(), "Duplicate command"); | |
94 commandStack_.push_back(command); | |
95 numAppliedCommands_++; | |
96 } | |
97 | |
98 void ViewportController::Undo() | |
99 { | |
100 ORTHANC_ASSERT(CanUndo(), ""); | |
101 commandStack_[numAppliedCommands_-1]->Undo(); | |
102 numAppliedCommands_--; | |
103 } | |
104 | |
105 void ViewportController::Redo() | |
106 { | |
107 ORTHANC_ASSERT(CanRedo(), ""); | |
108 commandStack_[numAppliedCommands_]->Redo(); | |
109 numAppliedCommands_++; | |
110 } | |
111 | |
112 bool ViewportController::CanUndo() const | |
113 { | |
114 return numAppliedCommands_ > 0; | |
115 } | |
116 | |
117 bool ViewportController::CanRedo() const | |
118 { | |
119 return numAppliedCommands_ < commandStack_.size(); | |
120 } | |
121 | |
122 void ViewportController::AddMeasureTool(MeasureToolPtr measureTool) | |
123 { | 132 { |
124 ORTHANC_ASSERT(std::find(measureTools_.begin(), measureTools_.end(), measureTool) | 133 ORTHANC_ASSERT(std::find(measureTools_.begin(), measureTools_.end(), measureTool) |
125 == measureTools_.end(), "Duplicate measure tool"); | 134 == measureTools_.end(), "Duplicate measure tool"); |
126 measureTools_.push_back(measureTool); | 135 measureTools_.push_back(measureTool); |
127 } | 136 } |
128 | 137 |
129 void ViewportController::RemoveMeasureTool(MeasureToolPtr measureTool) | 138 void ViewportController::RemoveMeasureTool(boost::shared_ptr<MeasureTool> measureTool) |
130 { | 139 { |
131 ORTHANC_ASSERT(std::find(measureTools_.begin(), measureTools_.end(), measureTool) | 140 ORTHANC_ASSERT(std::find(measureTools_.begin(), measureTools_.end(), measureTool) |
132 != measureTools_.end(), "Measure tool not found"); | 141 != measureTools_.end(), "Measure tool not found"); |
133 measureTools_.push_back(measureTool); | 142 measureTools_.erase( |
143 std::remove(measureTools_.begin(), measureTools_.end(), measureTool), | |
144 measureTools_.end()); | |
134 } | 145 } |
135 | 146 |
147 | |
148 double ViewportController::GetCanvasToSceneFactor() const | |
149 { | |
150 if (canvasToSceneFactor_ == 0) | |
151 { | |
152 canvasToSceneFactor_ = | |
153 GetScene()->GetCanvasToSceneTransform().ComputeZoom(); | |
154 } | |
155 return canvasToSceneFactor_; | |
156 } | |
157 | |
158 double ViewportController::GetHandleSideLengthS() const | |
159 { | |
160 return HANDLE_SIDE_LENGTH_CANVAS_COORD * GetCanvasToSceneFactor(); | |
161 } | |
162 | |
163 double ViewportController::GetAngleToolArcRadiusS() const | |
164 { | |
165 return ARC_RADIUS_CANVAS_COORD * GetCanvasToSceneFactor(); | |
166 } | |
167 | |
168 double ViewportController::GetHitTestMaximumDistanceS() const | |
169 { | |
170 return HIT_TEST_MAX_DISTANCE_CANVAS_COORD * GetCanvasToSceneFactor(); | |
171 } | |
172 | |
173 double ViewportController::GetAngleTopTextLabelDistanceS() const | |
174 { | |
175 return TEXT_CENTER_DISTANCE_CANVAS_COORD * GetCanvasToSceneFactor(); | |
176 } | |
136 } | 177 } |
137 | 178 |