Mercurial > hg > orthanc-stone
comparison Deprecated/Applications/Qt/QCairoWidget.cpp @ 1399:ff8d2e46ac63
moved Applications into Deprecated
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 29 Apr 2020 20:44:31 +0200 |
parents | Applications/Qt/QCairoWidget.cpp@7ec8fea061b9 |
children |
comparison
equal
deleted
inserted
replaced
1398:c5403d52078c | 1399:ff8d2e46ac63 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
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/>. | |
19 **/ | |
20 | |
21 #include "QCairoWidget.h" | |
22 | |
23 #include <QPainter> | |
24 #include <QPaintEvent> | |
25 | |
26 #include <stdexcept> | |
27 | |
28 | |
29 QCairoWidget::StoneObserver::StoneObserver(QCairoWidget& that, | |
30 Deprecated::IViewport& viewport, | |
31 OrthancStone::MessageBroker& broker) : | |
32 OrthancStone::IObserver(broker), | |
33 that_(that) | |
34 { | |
35 // get notified each time the content of the central viewport changes | |
36 viewport.RegisterObserverCallback( | |
37 new OrthancStone::Callable<StoneObserver, Deprecated::IViewport::ViewportChangedMessage> | |
38 (*this, &StoneObserver::OnViewportChanged)); | |
39 } | |
40 | |
41 | |
42 QCairoWidget::QCairoWidget(QWidget *parent) : | |
43 QWidget(parent), | |
44 context_(NULL) | |
45 { | |
46 setFocusPolicy(Qt::StrongFocus); // catch keyPressEvents | |
47 } | |
48 | |
49 | |
50 void QCairoWidget::SetContext(OrthancStone::NativeStoneApplicationContext& context) | |
51 { | |
52 context_ = &context; | |
53 | |
54 { | |
55 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
56 observer_.reset(new StoneObserver(*this, | |
57 locker.GetCentralViewport(), | |
58 locker.GetMessageBroker())); | |
59 } | |
60 } | |
61 | |
62 | |
63 void QCairoWidget::paintEvent(QPaintEvent* /*event*/) | |
64 { | |
65 QPainter painter(this); | |
66 | |
67 if (image_.get() != NULL && | |
68 context_ != NULL) | |
69 { | |
70 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
71 Deprecated::IViewport& viewport = locker.GetCentralViewport(); | |
72 Orthanc::ImageAccessor a; | |
73 surface_.GetWriteableAccessor(a); | |
74 viewport.Render(a); | |
75 painter.drawImage(0, 0, *image_); | |
76 } | |
77 else | |
78 { | |
79 painter.fillRect(rect(), Qt::red); | |
80 } | |
81 } | |
82 | |
83 OrthancStone::KeyboardModifiers GetKeyboardModifiers(QInputEvent* event) | |
84 { | |
85 Qt::KeyboardModifiers qtModifiers = event->modifiers(); | |
86 int stoneModifiers = static_cast<int>(OrthancStone::KeyboardModifiers_None); | |
87 if ((qtModifiers & Qt::AltModifier) != 0) | |
88 { | |
89 stoneModifiers |= static_cast<int>(OrthancStone::KeyboardModifiers_Alt); | |
90 } | |
91 if ((qtModifiers & Qt::ControlModifier) != 0) | |
92 { | |
93 stoneModifiers |= static_cast<int>(OrthancStone::KeyboardModifiers_Control); | |
94 } | |
95 if ((qtModifiers & Qt::ShiftModifier) != 0) | |
96 { | |
97 stoneModifiers |= static_cast<int>(OrthancStone::KeyboardModifiers_Shift); | |
98 } | |
99 return static_cast<OrthancStone::KeyboardModifiers>(stoneModifiers); | |
100 } | |
101 | |
102 void QCairoWidget::mousePressEvent(QMouseEvent* event) | |
103 { | |
104 OrthancStone::KeyboardModifiers stoneModifiers = GetKeyboardModifiers(event); | |
105 | |
106 OrthancStone::MouseButton button; | |
107 | |
108 switch (event->button()) | |
109 { | |
110 case Qt::LeftButton: | |
111 button = OrthancStone::MouseButton_Left; | |
112 break; | |
113 | |
114 case Qt::RightButton: | |
115 button = OrthancStone::MouseButton_Right; | |
116 break; | |
117 | |
118 case Qt::MiddleButton: | |
119 button = OrthancStone::MouseButton_Middle; | |
120 break; | |
121 | |
122 default: | |
123 return; // Unsupported button | |
124 } | |
125 | |
126 { | |
127 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
128 locker.GetCentralViewport().MouseDown(button, event->pos().x(), event->pos().y(), stoneModifiers, std::vector<Deprecated::Touch>()); | |
129 } | |
130 } | |
131 | |
132 | |
133 void QCairoWidget::mouseReleaseEvent(QMouseEvent* /*eventNotUsed*/) | |
134 { | |
135 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
136 locker.GetCentralViewport().MouseLeave(); | |
137 } | |
138 | |
139 | |
140 void QCairoWidget::mouseMoveEvent(QMouseEvent* event) | |
141 { | |
142 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
143 locker.GetCentralViewport().MouseMove(event->pos().x(), event->pos().y(), std::vector<Deprecated::Touch>()); | |
144 } | |
145 | |
146 | |
147 void QCairoWidget::wheelEvent(QWheelEvent * event) | |
148 { | |
149 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
150 | |
151 OrthancStone::KeyboardModifiers stoneModifiers = GetKeyboardModifiers(event); | |
152 | |
153 if (event->orientation() == Qt::Vertical) | |
154 { | |
155 if (event->delta() < 0) // TODO: compare direction with SDL and make sure we send the same directions | |
156 { | |
157 locker.GetCentralViewport().MouseWheel(OrthancStone::MouseWheelDirection_Up, event->pos().x(), event->pos().y(), stoneModifiers); | |
158 } | |
159 else | |
160 { | |
161 locker.GetCentralViewport().MouseWheel(OrthancStone::MouseWheelDirection_Down, event->pos().x(), event->pos().y(), stoneModifiers); | |
162 } | |
163 } | |
164 } | |
165 | |
166 void QCairoWidget::keyPressEvent(QKeyEvent *event) | |
167 { | |
168 using namespace OrthancStone; | |
169 | |
170 OrthancStone::KeyboardModifiers stoneModifiers = GetKeyboardModifiers(event); | |
171 | |
172 OrthancStone::KeyboardKeys keyType = OrthancStone::KeyboardKeys_Generic; | |
173 char keyChar = event->text()[0].toLatin1(); | |
174 | |
175 #define CASE_QT_KEY_TO_ORTHANC(qt, o) case qt: keyType = o; break; | |
176 if (keyChar == 0) | |
177 { | |
178 switch (event->key()) | |
179 { | |
180 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Up, KeyboardKeys_Up); | |
181 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Down, KeyboardKeys_Down); | |
182 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Left, KeyboardKeys_Left); | |
183 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Right, KeyboardKeys_Right); | |
184 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F1, KeyboardKeys_F1); | |
185 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F2, KeyboardKeys_F2); | |
186 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F3, KeyboardKeys_F3); | |
187 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F4, KeyboardKeys_F4); | |
188 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F5, KeyboardKeys_F5); | |
189 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F6, KeyboardKeys_F6); | |
190 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F7, KeyboardKeys_F7); | |
191 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F8, KeyboardKeys_F8); | |
192 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F9, KeyboardKeys_F9); | |
193 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F10, KeyboardKeys_F10); | |
194 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F11, KeyboardKeys_F11); | |
195 CASE_QT_KEY_TO_ORTHANC(Qt::Key_F12, KeyboardKeys_F12); | |
196 default: | |
197 break; | |
198 } | |
199 } | |
200 else if (keyChar == 127) | |
201 { | |
202 switch (event->key()) | |
203 { | |
204 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Delete, KeyboardKeys_Delete); | |
205 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Backspace, KeyboardKeys_Backspace); | |
206 default: | |
207 break; | |
208 } | |
209 } | |
210 | |
211 { | |
212 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
213 locker.GetCentralViewport().KeyPressed(keyType, keyChar, stoneModifiers); | |
214 } | |
215 } | |
216 | |
217 | |
218 void QCairoWidget::resizeEvent(QResizeEvent* event) | |
219 { | |
220 grabGesture(Qt::PanGesture); | |
221 QWidget::resizeEvent(event); | |
222 | |
223 if (event) | |
224 { | |
225 surface_.SetSize(event->size().width(), event->size().height(), true); | |
226 | |
227 image_.reset(new QImage(reinterpret_cast<uchar*>(surface_.GetBuffer()), | |
228 event->size().width(), | |
229 event->size().height(), | |
230 surface_.GetPitch(), | |
231 QImage::Format_RGB32)); | |
232 | |
233 { | |
234 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); | |
235 locker.GetCentralViewport().SetSize(event->size().width(), event->size().height()); | |
236 } | |
237 } | |
238 } |