QtMainWindowWidget.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // Filename: QtMainWindowWidget.cpp
3 // Author: Florian Hecht
4 // Date: 2008
5 // ****************************************************************************
6 
7 
8 #include "QtMainWindowWidget.h"
9 #include "QtMainWindow.h"
10 
11 #include <qpainter.h>
12 #include <qimage.h>
13 #include <qevent.h>
14 
16 #include "Image/ByteImage.h"
17 
18 #include <stdio.h>
19 
20 
22 : m_main_window(main_window), m_type(type), m_widget(NULL)
23 {
24 }
25 
26 
28 {
30 
31  if (ec != NULL && m_type == eButton)
32  {
33  ec->ButtonPushed(this);
34  }
35 }
37 {
39 
40  if (ec != NULL && (m_type == eSlider || m_type == eComboBox))
41  {
42  ec->ValueChanged(this, value);
43  }
44 }
46 {
48 
49  if (ec != NULL && m_type == eCheckBox)
50  {
51  ec->ValueChanged(this, (flag ? 1 : 0));
52  }
53 }
54 void CQtMainWindowWidget::TextChanged(const QString &str)
55 {
57 
58  if (ec != NULL && m_type == eTextEdit)
59  {
60  ec->ValueChanged(this, -1);
61  }
62 }
63 
64 
65 
66 CQtImageWidget::CQtImageWidget(CQtMainWindow *main_window, QWidget *pParent)
67 : QWidget(pParent), CQtMainWindowWidget(main_window, eImage), m_pBuffer(NULL), m_nWidth(0), m_nHeight(0), m_mouse_down(false)
68 {
69  m_widget = this;
70 
71  show();
72  #if QT_VERSION >= 0x040000
73  setBackgroundRole(QPalette::NoRole);
74  #else
75  setBackgroundMode(Qt::NoBackground);
76  #endif
77 
78  setMouseTracking(true);
79 
80  #if QT_VERSION >= 0x040000
81  setFocusPolicy(Qt::ClickFocus);
82  #else
83  setFocusPolicy(QWidget::ClickFocus);
84  #endif
85 }
86 
88 {
89  if (m_pBuffer != NULL)
90  delete [] m_pBuffer;
91 }
92 
94 {
95  if (m_nWidth != pImage->width || m_nHeight != pImage->height)
96  {
97  m_nWidth = pImage->width;
98  m_nHeight = pImage->height;
99 
100  if (m_pBuffer != NULL)
101  delete [] m_pBuffer;
102 
103  m_pBuffer = new unsigned char[m_nWidth * m_nHeight * 4];
104  }
105 
106  if (pImage->type == CByteImage::eGrayScale)
107  {
108  const int nPixels = m_nWidth * m_nHeight;
109  const unsigned char *pixels = pImage->pixels;
110  int *output = (int *) m_pBuffer;
111 
112  for (int i = 0; i < nPixels; i++)
113  output[i] = 255 << 24 | pixels[i] << 16 | pixels[i] << 8 | pixels[i];
114  }
115  else if (pImage->type == CByteImage::eRGB24)
116  {
117  const int nPixels = m_nWidth * m_nHeight;
118  const unsigned char *pixels = pImage->pixels;
119  int *output = (int *) m_pBuffer;
120 
121  for (int offset = 0, i = 0; i < nPixels; i++)
122  {
123  output[i] = 255 << 24 | pixels[offset] << 16 | pixels[offset + 1] << 8 | pixels[offset + 2];
124  offset += 3;
125  }
126  }
127  else if (pImage->type == CByteImage::eRGB24Split)
128  {
129  const int nPixels = m_nWidth * m_nHeight;
130  const unsigned char *pixels_r = pImage->pixels;
131  const unsigned char *pixels_g = pixels_r + nPixels;
132  const unsigned char *pixels_b = pixels_g + nPixels;
133  int *output = (int *) m_pBuffer;
134 
135  for (int i = 0; i < nPixels; i++)
136  output[i] = 255 << 24 | pixels_r[i] << 16 | pixels_g[i] << 8 | pixels_b[i];
137  }
138 
139  #if QT_VERSION >= 0x040000
140  repaint(0, 0, m_nWidth, m_nHeight);
141  #else
142  repaint(0, 0, m_nWidth, m_nHeight, false);
143  #endif
144 }
145 
146 void CQtImageWidget::paintEvent(QPaintEvent *pPaintEvent)
147 {
148  if (m_pBuffer != NULL)
149  {
150  QPainter painter(this);
151 
152  #if QT_VERSION >= 0x040000
153  QImage image(m_pBuffer, m_nWidth, m_nHeight, QImage::Format_RGB32);
154  #else
155  QImage image(m_pBuffer, m_nWidth, m_nHeight, 32, 0, 0, QImage::BigEndian);
156  #endif
157 
158  painter.drawImage(0, 0, image);
159 
160  if (m_mouse_down)
161  {
162  unsigned int x0 = m_mouse_start_x;
163  unsigned int y0 = m_mouse_start_y;
164  unsigned int x1 = m_mouse_current_x;
165  unsigned int y1 = m_mouse_current_y;
166 
167  if (x0 > x1)
168  {
169  unsigned int swap = x0;
170  x0 = x1;
171  x1 = swap;
172  }
173  if (y0 > y1)
174  {
175  unsigned int swap = y0;
176  y0 = y1;
177  y1 = swap;
178  }
179 
180  QPen pen(Qt::white, 1);
181  painter.setPen(pen);
182  painter.drawRect(x0, y0, x1 - x0, y1 - y0);
183  }
184  }
185 }
186 
187 void CQtImageWidget::mousePressEvent(QMouseEvent * e)
188 {
189  if (e->button() == Qt::LeftButton)
190  {
191  if (m_mouse_down)
192  {
193  m_mouse_down = false;
194  }
195  else
196  {
197  m_mouse_down = true;
198 
199  m_mouse_start_x = e->x();
200  m_mouse_start_y = e->y();
201 
204  }
205  }
206 
208 
209  if (ec != NULL)
210  {
211  int btn = -1;
212 
213  switch (e->button())
214  {
215  case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
216  case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
217  case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
218  default: printf("unknown mouse button pressed\n"); break;
219  }
220 
222  ec->MouseDown(w, btn, e->x(), e->y());
223  }
224 }
225 
227 {
228  if (e->button() == Qt::LeftButton && m_mouse_down)
229  {
231 
233  {
234  if (ec != NULL)
235  {
238  }
239  }
240  else
241  {
242  unsigned int x0 = m_mouse_start_x;
243  unsigned int y0 = m_mouse_start_y;
244  unsigned int x1 = m_mouse_current_x;
245  unsigned int y1 = m_mouse_current_y;
246 
247  if (x0 > x1)
248  {
249  unsigned int swap = x0;
250  x0 = x1;
251  x1 = swap;
252  }
253  if (y0 > y1)
254  {
255  unsigned int swap = y0;
256  y0 = y1;
257  y1 = swap;
258  }
259 
260  if (ec != NULL)
261  {
263  ec->RectSelected(w, x0, y0, x1, y1);
264  }
265 
266  update(x0, y0, x1 - x0, y1 - y0);
267  }
268 
269  m_mouse_down = false;
270  }
271 
273 
274  if (ec != NULL)
275  {
276  int btn = -1;
277 
278  switch (e->button())
279  {
280  case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
281  case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
282  case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
283  default: printf("unknown mouse button released\n"); break;
284  }
285 
287  ec->MouseUp(w, btn, e->x(), e->y());
288  }
289 }
290 
291 void CQtImageWidget::mouseMoveEvent(QMouseEvent * e)
292 {
293  if (m_mouse_down)
294  {
295  unsigned int x0 = m_mouse_start_x;
296  unsigned int y0 = m_mouse_start_y;
297  unsigned int x1 = m_mouse_current_x;
298  unsigned int y1 = m_mouse_current_y;
299 
300  if (x0 > x1)
301  {
302  unsigned int swap = x0;
303  x0 = x1;
304  x1 = swap;
305  }
306  if (y0 > y1)
307  {
308  unsigned int swap = y0;
309  y0 = y1;
310  y1 = swap;
311  }
312 
313  update(x0, y0, x1 - x0, y1 - y0);
314 
315  m_mouse_current_x = e->x();
316  m_mouse_current_y = e->y();
317 
318  if (m_mouse_current_x < 0)
319  m_mouse_current_x = 0;
322 
323  if (m_mouse_current_y < 0)
324  m_mouse_current_y = 0;
327 
328  x0 = m_mouse_start_x;
329  y0 = m_mouse_start_y;
330  x1 = m_mouse_current_x;
331  y1 = m_mouse_current_y;
332 
333  if (x0 > x1)
334  {
335  unsigned int swap = x0;
336  x0 = x1;
337  x1 = swap;
338  }
339  if (y0 > y1)
340  {
341  unsigned int swap = y0;
342  y0 = y1;
343  y1 = swap;
344  }
345 
346  update(x0, y0, x1 - x0, y1 - y0);
347  }
348 
350 
351  if (ec != NULL)
352  {
354  ec->MouseMove(w, e->x(), e->y());
355  }
356 }
357 
358 void CQtImageWidget::keyPressEvent(QKeyEvent * e)
359 {
361 
362  if (ec != NULL)
363  {
365 
366  #if QT_VERSION >= 0x040000
367  ec->KeyDown(w, e->text().toInt());
368  #else
369  ec->KeyDown(w, e->ascii());
370  #endif
371  }
372 }
373 
375 {
377 
378  if (ec != NULL)
379  {
381 
382  #if QT_VERSION >= 0x040000
383  ec->KeyUp(w, e->text().toInt());
384  #else
385  ec->KeyUp(w, e->ascii());
386  #endif
387  }
388 }
389 
390 
391 
392 #ifdef USE_OPENGL
393 
394 CQtGLWidget::CQtGLWidget(CQtMainWindow *main_window, QWidget *pParent)
395 : QGLWidget(pParent), CQtMainWindowWidget(main_window, eGLWidget)
396 {
397  m_widget = this;
398 
399  show();
400 
401  setMouseTracking(true);
402 
403  #if QT_VERSION >= 0x040000
404  setFocusPolicy(Qt::ClickFocus);
405  #else
406  setFocusPolicy(QWidget::ClickFocus);
407  #endif
408 }
409 
410 CQtGLWidget::~CQtGLWidget()
411 {
412 }
413 
414 void CQtGLWidget::mousePressEvent(QMouseEvent * e)
415 {
417 
418  if (ec != NULL)
419  {
420  int btn = -1;
421 
422  switch (e->button())
423  {
424  case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
425  case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
426  case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
427  default: printf("unknown mouse button pressed\n"); break;
428  }
429 
431  ec->MouseDown(w, btn, e->x(), e->y());
432  }
433 }
434 
435 void CQtGLWidget::mouseReleaseEvent(QMouseEvent * e)
436 {
438 
439  if (ec != NULL)
440  {
441  int btn = -1;
442 
443  switch (e->button())
444  {
445  case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
446  case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
447  case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
448  default: printf("unknown mouse button released\n"); break;
449  }
450 
452  ec->MouseUp(w, btn, e->x(), e->y());
453  }
454 }
455 
456 void CQtGLWidget::mouseMoveEvent(QMouseEvent * e)
457 {
459 
460  if (ec != NULL)
461  {
463  ec->MouseMove(w, e->x(), e->y());
464  }
465 }
466 
467 void CQtGLWidget::keyPressEvent(QKeyEvent * e)
468 {
470 
471  if (ec != NULL)
472  {
474 
475  #if QT_VERSION >= 0x040000
476  ec->KeyDown(w, e->text().toInt());
477  #else
478  ec->KeyDown(w, e->ascii());
479  #endif
480  }
481 }
482 
483 void CQtGLWidget::keyReleaseEvent(QKeyEvent * e)
484 {
486 
487  if (ec != NULL)
488  {
490 
491  #if QT_VERSION >= 0x040000
492  ec->KeyUp(w, e->text().toInt());
493  #else
494  ec->KeyUp(w, e->ascii());
495  #endif
496  }
497 }
498 
499 #endif
virtual void KeyUp(WIDGET_HANDLE widget, int key)
virtual void ButtonPushed(WIDGET_HANDLE widget)
virtual void ValueChanged(WIDGET_HANDLE widget, int value)
virtual void KeyDown(WIDGET_HANDLE widget, int key)
void TextChanged(const QString &str)
void keyReleaseEvent(QKeyEvent *e)
int width
The width of the image in pixels.
Definition: ByteImage.h:257
CMainWindowEventInterface * GetEventCallback()
Definition: QtMainWindow.h:71
CQtMainWindowWidget(CQtMainWindow *main_window, QtWidgetType type)
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3131
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
#define IVT_LEFT_BUTTON
void mousePressEvent(QMouseEvent *e)
virtual void MouseDown(WIDGET_HANDLE widget, int button, int x, int y)
GLintptr offset
Definition: glext.h:3389
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3121
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
void mouseMoveEvent(QMouseEvent *e)
QtWidgetType
GLsizei const GLfloat * value
Definition: glext.h:3538
virtual void MouseMove(WIDGET_HANDLE widget, int x, int y)
virtual void PointClicked(WIDGET_HANDLE widget, int x, int y)
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glext.h:3154
void keyPressEvent(QKeyEvent *e)
#define IVT_RIGHT_BUTTON
int height
The height of the image in pixels.
Definition: ByteImage.h:264
unsigned char * m_pBuffer
void mouseReleaseEvent(QMouseEvent *e)
CQtImageWidget(CQtMainWindow *main_window, QWidget *pParent=0)
void paintEvent(QPaintEvent *pPaintEvent)
#define IVT_MIDDLE_BUTTON
ImageType type
The type of the image.
Definition: ByteImage.h:292
CQtMainWindow * m_main_window
void SetImage(const CByteImage *pImage)
void ValueChanged(int value)
Interface for the event mechanism of GUIs using the GUI toolkit of the IVT.
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3571
virtual void RectSelected(WIDGET_HANDLE widget, int x0, int y0, int x1, int y1)
virtual void MouseUp(WIDGET_HANDLE widget, int button, int x, int y)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28