QtMainWindow.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // Filename: QtMainWindow.cpp
3 // Author: Florian Hecht
4 // Date: 2008
5 // ****************************************************************************
6 
7 
8 #include "QtMainWindow.h"
9 #include "QtMainWindowWidget.h"
10 #include "QtApplicationHandler.h"
12 
13 #include "Image/ByteImage.h"
14 
15 #include <qstring.h>
16 #include <qpushbutton.h>
17 #include <qlabel.h>
18 #include <qcheckbox.h>
19 #include <qlineedit.h>
20 #include <qslider.h>
21 #include <qcombobox.h>
22 #include <qapplication.h>
23 #include <qevent.h>
24 
25 #ifdef USE_OPENGL
26 #include <qgl.h>
27 #endif
28 
29 #include <stdio.h>
30 
31 
32 
33 
35 
36 CQtMainWindow::CQtMainWindow(int x, int y, int width, int height, const char *title)
37 {
38  setFixedSize(width, height);
39  #ifdef OSX_QTGUI // make sure the Qt window is competely visible under OS X
40  if (y < 22)
41  y = 22;
42  #endif
43  move(x, y);
44 
45  #if QT_VERSION >= 0x040000
46  setWindowTitle(title);
47  setAttribute(Qt::WA_QuitOnClose, true);
48  #else
49  setCaption(title);
50  #endif
51 
52  m_event_callback = NULL;
53 
54  m_ref_count++;
55 }
56 
57 CQtMainWindow::CQtMainWindow(int x, int y, int width, int height, QWidget *pParent) : QWidget(pParent)
58 {
59  setFixedSize(width, height);
60  move(x, y);
61 
62  m_event_callback = NULL;
63 
64  m_ref_count++;
65 }
66 
68 {
69  int c = m_widgets.size();
70 
71  for (int i = c-1; i >= 0; i--)
72  {
73  delete m_widgets[i];
74  }
75 
76  m_widgets.clear();
77 }
78 
79 // create widgets
81 {
83 
84  CQtImageWidget *iw = new CQtImageWidget(this, (p != NULL ? p->m_widget : this));
85  iw->setFixedSize(width, height);
86  iw->move(x, y);
87 
88  CQtMainWindowWidget *w = iw;
89 
90  m_widgets.push_back(w);
91 
92  return (WIDGET_HANDLE)w;
93 }
94 
95 WIDGET_HANDLE CQtMainWindow::AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
96 {
98 
100 
101  QPushButton *pBtn = new QPushButton((p != NULL ? p->m_widget : this));
102  pBtn->setText(text);
103  pBtn->setFixedSize(width, height);
104  pBtn->move(x, y);
105 
106  w->m_widget = pBtn;
107 
108  connect(pBtn, SIGNAL(clicked()), w, SLOT(Clicked()));
109 
110  m_widgets.push_back(w);
111 
112  return (WIDGET_HANDLE)w;
113 }
114 
115 WIDGET_HANDLE CQtMainWindow::AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
116 {
118 
120 
121  QLabel *pLabel = new QLabel((p != NULL ? p->m_widget : this));
122  pLabel->setText(text);
123  pLabel->setFixedSize(width, height);
124  pLabel->move(x, y);
125 
126  w->m_widget = pLabel;
127 
128  m_widgets.push_back(w);
129 
130  return (WIDGET_HANDLE)w;
131 }
132 
133 WIDGET_HANDLE CQtMainWindow::AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent)
134 {
136 
138 
139  QCheckBox *pCheckBox = new QCheckBox((p != NULL ? p->m_widget : this));
140  pCheckBox->setText(text);
141  pCheckBox->setFixedSize(width, height);
142  pCheckBox->move(x, y);
143  pCheckBox->setChecked(checked);
144 
145  w->m_widget = pCheckBox;
146 
147  connect(pCheckBox, SIGNAL(toggled(bool)), w, SLOT(Toggled(bool)));
148 
149  m_widgets.push_back(w);
150 
151  return (WIDGET_HANDLE)w;
152 }
153 
154 WIDGET_HANDLE CQtMainWindow::AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
155 {
157 
159 
160  QLineEdit *pLineEdit = new QLineEdit((p != NULL ? p->m_widget : this));
161  pLineEdit->setText(text);
162  pLineEdit->setFixedSize(width, height);
163  pLineEdit->move(x, y);
164 
165  w->m_widget = pLineEdit;
166 
167  connect(pLineEdit, SIGNAL(textChanged(const QString &)), w, SLOT(TextChanged(const QString &)));
168 
169  m_widgets.push_back(w);
170 
171  return (WIDGET_HANDLE)w;
172 }
173 
174 WIDGET_HANDLE CQtMainWindow::AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent)
175 {
177 
179 
180  QSlider *pSlider = new QSlider(Qt::Horizontal, (p != NULL ? p->m_widget : this));
181  pSlider->setFixedSize(width, height);
182  pSlider->move(x, y);
183  #if QT_VERSION >= 0x040000
184  pSlider->setMinimum(min_value);
185  pSlider->setMaximum(max_value);
186  pSlider->setTickPosition(QSlider::TicksBelow);
187  pSlider->setFocusPolicy(Qt::StrongFocus);
188  #else
189  pSlider->setMinValue(min_value);
190  pSlider->setMaxValue(max_value);
191  pSlider->setTickmarks(QSlider::Below);
192  pSlider->setFocusPolicy(QWidget::StrongFocus);
193  #endif
194  pSlider->setValue(value);
195  pSlider->setPageStep(1);
196  pSlider->setTickInterval(step);
197 
198 
199  w->m_widget = pSlider;
200 
201  connect(pSlider, SIGNAL(valueChanged(int)), w, SLOT(ValueChanged(int)));
202 
203  m_widgets.push_back(w);
204 
205  return (WIDGET_HANDLE)w;
206 }
207 
208 WIDGET_HANDLE CQtMainWindow::AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent)
209 {
211 
213 
214  QComboBox *pComboBox = new QComboBox((p != NULL ? p->m_widget : this));
215  pComboBox->setFixedSize(width, height);
216  pComboBox->move(x, y);
217 
218  #if QT_VERSION >= 0x040000
219  for (int i = 0; i < num_entries; i++)
220  {
221  pComboBox->addItem(entries[i]);
222  }
223  pComboBox->setCurrentIndex(current_entry);
224  #else
225  for (int i = 0; i < num_entries; i++)
226  {
227  pComboBox->insertItem(entries[i]);
228  }
229  pComboBox->setCurrentItem(current_entry);
230  #endif
231 
232  w->m_widget = pComboBox;
233 
234  connect(pComboBox, SIGNAL(activated(int)), w, SLOT(ValueChanged(int)));
235 
236  m_widgets.push_back(w);
237 
238  return (WIDGET_HANDLE)w;
239 }
240 
242 {
243 #ifdef USE_OPENGL
245 
246  CQtGLWidget *pGLWidget = new CQtGLWidget(this, (p != NULL ? p->m_widget : this));
247  pGLWidget->setFixedSize(width, height);
248  pGLWidget->move(x, y);
249 
250  CQtMainWindowWidget *w = pGLWidget;
251 
252  m_widgets.push_back(w);
253 
254  return (WIDGET_HANDLE)w;
255 #else
256  printf("WARNING: CQtMainWindow::AddGLWidget has been called, but USE_OPENGL = 0 in Makefile.base\n");
257  return (WIDGET_HANDLE)NULL;
258 
259 #endif
260 }
261 
262 
263 // access to widget attributes
264 #if QT_VERSION >= 0x040000
265 #define GET_ASCII toAscii
266 #else
267 #define GET_ASCII ascii
268 #endif
269 
270 bool CQtMainWindow::GetText(WIDGET_HANDLE widget, char *text, int len)
271 {
273 
274  if (w->m_type == eButton)
275  {
276  QPushButton *btn = (QPushButton*)w->m_widget;
277  QString str = btn->text();
278  strncpy(text, str.GET_ASCII(), len);
279 
280  return true;
281  }
282  else if(w->m_type == eCheckBox)
283  {
284  QCheckBox *btn = (QCheckBox*)w->m_widget;
285  QString str = btn->text();
286  strncpy(text, str.GET_ASCII(), len);
287 
288  return true;
289  }
290  else if (w->m_type == eTextEdit)
291  {
292  QLineEdit *le = (QLineEdit*)w->m_widget;
293  QString str = le->text();
294  strncpy(text, str.GET_ASCII(), len);
295 
296  return true;
297  }
298  else if (w->m_type == eLabel)
299  {
300  QLabel *label = (QLabel*)w->m_widget;
301  QString str = label->text();
302  strncpy(text, str.GET_ASCII(), len);
303 
304  return true;
305  }
306 
307  return false;
308 }
309 bool CQtMainWindow::SetText(WIDGET_HANDLE widget, const char *text)
310 {
312 
313  if (w->m_type == eButton)
314  {
315  QPushButton *btn = (QPushButton*)w->m_widget;
316  btn->setText(text);
317 
318  return true;
319  }
320  else if (w->m_type == eCheckBox)
321  {
322  QCheckBox *btn = (QCheckBox*)w->m_widget;
323  btn->setText(text);
324 
325  return true;
326  }
327  else if (w->m_type == eTextEdit)
328  {
329  QLineEdit *le = (QLineEdit*)w->m_widget;
330  le->setText(text);
331 
332  return true;
333  }
334  else if (w->m_type == eLabel)
335  {
336  QLabel *label = (QLabel*)w->m_widget;
337  label->setText(text);
338 
339  return true;
340  }
341 
342  return false;
343 }
344 
346 {
348 
349  if (w->m_type == eImage)
350  {
351  CQtImageWidget *iw = (CQtImageWidget*)w;
352 
353  iw->SetImage(pImage);
354 
355  return true;
356  }
357 
358  return false;
359 }
360 
362 {
364 
365  if (w->m_type == eCheckBox)
366  {
367  QCheckBox *cb = (QCheckBox*)w->m_widget;
368 
369  value = cb->isChecked();
370 
371  return true;
372  }
373  else if (w->m_type == eSlider)
374  {
375  QSlider *sl = (QSlider*)w->m_widget;
376 
377  value = sl->value();
378 
379  return true;
380  }
381  else if (w->m_type == eComboBox)
382  {
383  QComboBox *cb = (QComboBox*)w->m_widget;
384 
385  #if QT_VERSION >= 0x040000
386  value = cb->currentIndex();
387  #else
388  value = cb->currentItem();
389  #endif
390 
391  return true;
392  }
393 
394  return false;
395 }
396 
398 {
400 
401  if (w->m_type == eCheckBox)
402  {
403  QCheckBox *cb = (QCheckBox*)w->m_widget;
404 
405  cb->setChecked(value != 0);
406 
407  return true;
408  }
409  else if (w->m_type == eSlider)
410  {
411  QSlider *sl = (QSlider*)w->m_widget;
412 
413  sl->setValue(value);
414 
415  return true;
416  }
417  else if (w->m_type == eComboBox)
418  {
419  QComboBox *cb = (QComboBox*)w->m_widget;
420 
421  #if QT_VERSION >= 0x040000
422  cb->setCurrentIndex(value);
423  #else
424  cb->setCurrentItem(value);
425  #endif
426 
427  return true;
428  }
429 
430  return false;
431 }
432 
434 {
435 #ifdef USE_OPENGL
436 
438 
439  if (w->m_type == eGLWidget)
440  {
441  QGLWidget *glw = (QGLWidget*)w->m_widget;
442 
443  //glw->swapBuffers();
444  glw->updateGL();
445 
446  return true;
447  }
448 
449 #endif
450 
451  return false;
452 }
453 
455 {
456 #ifdef USE_OPENGL
457 
459 
460  if (w->m_type == eGLWidget)
461  {
462  QGLWidget *glw = (QGLWidget*)w->m_widget;
463 
464  glw->makeCurrent();
465 
466  return true;
467  }
468 #endif
469 
470  return false;
471 }
472 
473 
474 
475 // window control
477 {
479 
480  if (w == NULL)
481  {
482  show();
483  }
484  else
485  {
486  w->m_widget->show();
487  }
488 }
490 {
492 
493  if (w == NULL)
494  {
495  hide();
496  }
497  else
498  {
499  w->m_widget->hide();
500  }
501 }
502 
503 #if QT_VERSION < 0x040000
504 
505 #ifdef Q_WS_WIN
506 # include <windows.h>
507 # include <shellapi.h>
508 #elif defined Q_WS_X11
509 # include <X11/Xlib.h>
510 #else /* Q_WS_MAC */
511 # include <Carbon/Carbon.h>
512 /* //probably don't need these
513 #define shiftKeyBit 9
514 #define optionKeyBit 11
515 #define controlKeyBit 12
516 #define rightShiftKeyBit 13
517 #define rightOptionKeyBit 14
518 #define rightControlKeyBit 15
519 */
520 #endif
521 #include <qpaintdevice.h>
522 
523 // Implement a way to easily deduce which mousebuttons and modifier
524 // keys are held down
525 static Qt::ButtonState get_modifiers()
526 {
527  int result = Qt::NoButton;
528 
529 #if defined(Q_WS_WIN)
530 
531  result |= GetAsyncKeyState(VK_SHIFT) < 0 ? Qt::ShiftButton : 0;
532  result |= GetAsyncKeyState(VK_MENU) < 0 ? Qt::AltButton : 0;
533  result |= GetAsyncKeyState(VK_CONTROL) < 0 ? Qt::ControlButton : 0;
534 
535 #elif defined(Q_WS_X11)
536 
537  Window root, child;
538  int root_x, root_y;
539  int win_x, win_y;
540  uint keys_buttons;
541  bool status = XQueryPointer(QPaintDevice::x11AppDisplay(),
542  QPaintDevice::x11AppRootWindow(),
543  &root, &child,
544  &root_x, &root_y,
545  &win_x, &win_y,
546  &keys_buttons);
547 
548  if (status)
549  {
550  result |= keys_buttons & ShiftMask ? Qt::ShiftButton : 0;
551  result |= keys_buttons & Mod1Mask ? Qt::AltButton : 0;
552  result |= keys_buttons & ControlMask ? Qt::ControlButton : 0;
553  }
554 
555 #else /* Q_WS_MAC */
556 
557  UInt32 modifiers = GetCurrentKeyModifiers();
558  if (modifiers & (1 << shiftKeyBit) modifiers |= Qt::ShiftButton;
559  if (modifiers & (1 << rightShiftKeyBit) modifiers |= Qt::ShiftButton;
560  if (modifiers & (1 << optionKeyBit) modifiers |= Qt::AltButton;
561  if (modifiers & (1 << rightOptionKeyBit) modifiers |= Qt::AltButton;
562  if (modifiers & (1 << controlKeyBit) modifiers |= Qt::ControlButton;
563  if (modifiers & (1 << rightControlKeyBit) modifiers |= Qt::ControlButton;
564 
565 #endif
566 
567  return Qt::ButtonState(result);
568 }
569 
570 #endif /* end QT_VERSION < 0x040000 */
571 
572 
573 
575 {
576  int state = 0;
577  int mod = 0;
578 
579  #if QT_VERSION >= 0x040000
580  mod = qApp->keyboardModifiers();
581  if (mod & Qt::ShiftModifier)
582  state |= IVT_SHIFT_KEY;
583  if (mod & Qt::ControlModifier)
584  state |= IVT_CONTROL_KEY;
585  if (mod & Qt::AltModifier)
586  state |= IVT_ALT_KEY;
587  #else
588  mod = get_modifiers();
589  if (mod & Qt::ShiftButton)
590  state |= IVT_SHIFT_KEY;
591  if (mod & Qt::ControlButton)
592  state |= IVT_CONTROL_KEY;
593  if (mod & Qt::AltButton)
594  state |= IVT_ALT_KEY;
595  #endif
596 
597  return state;
598 }
599 
600 
601 void CQtMainWindow::closeEvent(QCloseEvent *e)
602 {
603  m_ref_count--;
604  if (m_ref_count == 0)
605  {
607 
608  if (app_handler)
609  app_handler->Exit();
610  }
611 }
void Show(WIDGET_HANDLE widget=0)
WIDGET_HANDLE AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent=0)
static int m_ref_count
Definition: QtMainWindow.h:82
CMainWindowEventInterface * m_event_callback
Definition: QtMainWindow.h:78
static CQtApplicationHandler * GetApplicationHandler()
WIDGET_HANDLE AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
WIDGET_HANDLE AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
bool SwapBuffersGLWidget(WIDGET_HANDLE widget)
#define IVT_SHIFT_KEY
void Hide(WIDGET_HANDLE widget=0)
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
CQtMainWindow(int x, int y, int width, int height, const char *title)
GLenum GLsizei len
Definition: glext.h:3940
WIDGET_HANDLE AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent=0)
bool SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
WIDGET_HANDLE AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
#define IVT_CONTROL_KEY
std::vector< CQtMainWindowWidget * > m_widgets
Definition: QtMainWindow.h:80
static Qt::ButtonState get_modifiers()
WIDGET_HANDLE AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
GLenum GLint x
Definition: glext.h:3125
int GetModifierKeyState()
const GLubyte * c
Definition: glext.h:5181
void * WIDGET_HANDLE
GLsizei const GLfloat * value
Definition: glext.h:3538
bool SetValue(WIDGET_HANDLE widget, int value)
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
bool GetText(WIDGET_HANDLE widget, char *text, int len)
GLenum GLint GLint y
Definition: glext.h:3125
void closeEvent(QCloseEvent *e)
WIDGET_HANDLE AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent=0)
bool SetText(WIDGET_HANDLE widget, const char *text)
void SetImage(const CByteImage *pImage)
GLfloat GLfloat p
Definition: glext.h:5178
bool MakeCurrentGLWidget(WIDGET_HANDLE widget)
bool GetValue(WIDGET_HANDLE widget, int &value)
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3571
WIDGET_HANDLE AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
#define IVT_ALT_KEY


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