comparison Applications/Qt/QCairoWidget.cpp @ 385:6cc3ce74dc05

using message broker in widgets
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 07 Nov 2018 20:49:41 +0100
parents 557c8ff1db5c
children b70e9be013e4
comparison
equal deleted inserted replaced
384:d20d75f20c5d 385:6cc3ce74dc05
23 #include <QPainter> 23 #include <QPainter>
24 #include <QPaintEvent> 24 #include <QPaintEvent>
25 25
26 #include <stdexcept> 26 #include <stdexcept>
27 27
28
29 QCairoWidget::StoneObserver::StoneObserver(QCairoWidget& that,
30 OrthancStone::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, OrthancStone::IViewport::ViewportChangedMessage>
38 (*this, &StoneObserver::OnViewportChanged));
39 }
40
41
28 QCairoWidget::QCairoWidget(QWidget *parent) : 42 QCairoWidget::QCairoWidget(QWidget *parent) :
29 QWidget(parent), 43 QWidget(parent),
30 context_(NULL) 44 context_(NULL)
31 { 45 {
32 setFocusPolicy(Qt::StrongFocus); // catch keyPressEvents 46 setFocusPolicy(Qt::StrongFocus); // catch keyPressEvents
33 } 47 }
34 48
35 QCairoWidget::~QCairoWidget()
36 {
37 }
38 49
39 void QCairoWidget::SetContext(OrthancStone::NativeStoneApplicationContext& context) 50 void QCairoWidget::SetContext(OrthancStone::NativeStoneApplicationContext& context)
40 { 51 {
41 context_ = &context; 52 context_ = &context;
42 context_->GetCentralViewport().Register(*this); // get notified each time the content of the central viewport changes 53
43 } 54 {
55 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
56 observer_.reset(new StoneObserver(*this,
57 locker.GetCentralViewport(),
58 locker.GetMessageBroker()));
59 }
60 }
61
44 62
45 void QCairoWidget::paintEvent(QPaintEvent* /*event*/) 63 void QCairoWidget::paintEvent(QPaintEvent* /*event*/)
46 { 64 {
47 QPainter painter(this); 65 QPainter painter(this);
48 66
49 if (image_.get() != NULL && context_ != NULL) 67 if (image_.get() != NULL &&
68 context_ != NULL)
50 { 69 {
51 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_); 70 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
52 OrthancStone::IViewport& viewport = context_->GetCentralViewport(); 71 OrthancStone::IViewport& viewport = locker.GetCentralViewport();
53 Orthanc::ImageAccessor a; 72 Orthanc::ImageAccessor a;
54 surface_.GetWriteableAccessor(a); 73 surface_.GetWriteableAccessor(a);
55 viewport.Render(a); 74 viewport.Render(a);
56 painter.drawImage(0, 0, *image_); 75 painter.drawImage(0, 0, *image_);
57 } 76 }
101 break; 120 break;
102 121
103 default: 122 default:
104 return; // Unsupported button 123 return; // Unsupported button
105 } 124 }
106 context_->GetCentralViewport().MouseDown(button, event->pos().x(), event->pos().y(), stoneModifiers); 125
126 {
127 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
128 locker.GetCentralViewport().MouseDown(button, event->pos().x(), event->pos().y(), stoneModifiers);
129 }
107 } 130 }
108 131
109 132
110 void QCairoWidget::mouseReleaseEvent(QMouseEvent* /*eventNotUsed*/) 133 void QCairoWidget::mouseReleaseEvent(QMouseEvent* /*eventNotUsed*/)
111 { 134 {
112 context_->GetCentralViewport().MouseLeave(); 135 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
136 locker.GetCentralViewport().MouseLeave();
113 } 137 }
114 138
115 139
116 void QCairoWidget::mouseMoveEvent(QMouseEvent* event) 140 void QCairoWidget::mouseMoveEvent(QMouseEvent* event)
117 { 141 {
118 context_->GetCentralViewport().MouseMove(event->pos().x(), event->pos().y()); 142 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
143 locker.GetCentralViewport().MouseMove(event->pos().x(), event->pos().y());
119 } 144 }
120 145
121 146
122 void QCairoWidget::wheelEvent(QWheelEvent * event) 147 void QCairoWidget::wheelEvent(QWheelEvent * event)
123 { 148 {
149 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
150
124 OrthancStone::KeyboardModifiers stoneModifiers = GetKeyboardModifiers(event); 151 OrthancStone::KeyboardModifiers stoneModifiers = GetKeyboardModifiers(event);
125 152
126 if (event->orientation() == Qt::Vertical) 153 if (event->orientation() == Qt::Vertical)
127 { 154 {
128 if (event->delta() < 0) // TODO: compare direction with SDL and make sure we send the same directions 155 if (event->delta() < 0) // TODO: compare direction with SDL and make sure we send the same directions
129 { 156 {
130 context_->GetCentralViewport().MouseWheel(OrthancStone::MouseWheelDirection_Up, event->pos().x(), event->pos().y(), stoneModifiers); 157 locker.GetCentralViewport().MouseWheel(OrthancStone::MouseWheelDirection_Up, event->pos().x(), event->pos().y(), stoneModifiers);
131 } 158 }
132 else 159 else
133 { 160 {
134 context_->GetCentralViewport().MouseWheel(OrthancStone::MouseWheelDirection_Down, event->pos().x(), event->pos().y(), stoneModifiers); 161 locker.GetCentralViewport().MouseWheel(OrthancStone::MouseWheelDirection_Down, event->pos().x(), event->pos().y(), stoneModifiers);
135 } 162 }
136 } 163 }
137 } 164 }
138 165
139 void QCairoWidget::keyPressEvent(QKeyEvent *event) 166 void QCairoWidget::keyPressEvent(QKeyEvent *event)
156 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Right, KeyboardKeys_Right); 183 CASE_QT_KEY_TO_ORTHANC(Qt::Key_Right, KeyboardKeys_Right);
157 default: 184 default:
158 break; 185 break;
159 } 186 }
160 } 187 }
161 context_->GetCentralViewport().KeyPressed(keyType, keyChar, stoneModifiers); 188
189 {
190 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
191 locker.GetCentralViewport().KeyPressed(keyType, keyChar, stoneModifiers);
192 }
162 } 193 }
163 194
164 195
165 void QCairoWidget::resizeEvent(QResizeEvent* event) 196 void QCairoWidget::resizeEvent(QResizeEvent* event)
166 { 197 {
175 event->size().width(), 206 event->size().width(),
176 event->size().height(), 207 event->size().height(),
177 surface_.GetPitch(), 208 surface_.GetPitch(),
178 QImage::Format_RGB32)); 209 QImage::Format_RGB32));
179 210
180 context_->GetCentralViewport().SetSize(event->size().width(), event->size().height()); 211 {
181 212 OrthancStone::NativeStoneApplicationContext::GlobalMutexLocker locker(*context_);
182 } 213 locker.GetCentralViewport().SetSize(event->size().width(), event->size().height());
183 } 214 }
215 }
216 }