00001
00002
00003
00004
00005
00006
00007
00008 #include "QtMainWindow.h"
00009 #include "QtMainWindowWidget.h"
00010 #include "QtApplicationHandler.h"
00011 #include "Interfaces/MainWindowEventInterface.h"
00012
00013 #include "Image/ByteImage.h"
00014
00015 #include <qstring.h>
00016 #include <qpushbutton.h>
00017 #include <qlabel.h>
00018 #include <qcheckbox.h>
00019 #include <qlineedit.h>
00020 #include <qslider.h>
00021 #include <qcombobox.h>
00022 #include <qapplication.h>
00023 #include <qevent.h>
00024
00025 #ifdef USE_OPENGL
00026 #include <qgl.h>
00027 #endif
00028
00029 #include <stdio.h>
00030
00031
00032
00033
00034 int CQtMainWindow::m_ref_count = 0;
00035
00036 CQtMainWindow::CQtMainWindow(int x, int y, int width, int height, const char *title)
00037 {
00038 setFixedSize(width, height);
00039 #ifdef OSX_QTGUI // make sure the Qt window is competely visible under OS X
00040 if (y < 22)
00041 y = 22;
00042 #endif
00043 move(x, y);
00044
00045 #if QT_VERSION >= 0x040000
00046 setWindowTitle(title);
00047 setAttribute(Qt::WA_QuitOnClose, true);
00048 #else
00049 setCaption(title);
00050 #endif
00051
00052 m_event_callback = NULL;
00053
00054 m_ref_count++;
00055 }
00056
00057 CQtMainWindow::CQtMainWindow(int x, int y, int width, int height, QWidget *pParent) : QWidget(pParent)
00058 {
00059 setFixedSize(width, height);
00060 move(x, y);
00061
00062 m_event_callback = NULL;
00063
00064 m_ref_count++;
00065 }
00066
00067 CQtMainWindow::~CQtMainWindow()
00068 {
00069 int c = m_widgets.size();
00070
00071 for (int i = c-1; i >= 0; i--)
00072 {
00073 delete m_widgets[i];
00074 }
00075
00076 m_widgets.clear();
00077 }
00078
00079
00080 WIDGET_HANDLE CQtMainWindow::AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent)
00081 {
00082 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00083
00084 CQtImageWidget *iw = new CQtImageWidget(this, (p != NULL ? p->m_widget : this));
00085 iw->setFixedSize(width, height);
00086 iw->move(x, y);
00087
00088 CQtMainWindowWidget *w = iw;
00089
00090 m_widgets.push_back(w);
00091
00092 return (WIDGET_HANDLE)w;
00093 }
00094
00095 WIDGET_HANDLE CQtMainWindow::AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00096 {
00097 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00098
00099 CQtMainWindowWidget *w = new CQtMainWindowWidget(this, eButton);
00100
00101 QPushButton *pBtn = new QPushButton((p != NULL ? p->m_widget : this));
00102 pBtn->setText(text);
00103 pBtn->setFixedSize(width, height);
00104 pBtn->move(x, y);
00105
00106 w->m_widget = pBtn;
00107
00108 connect(pBtn, SIGNAL(clicked()), w, SLOT(Clicked()));
00109
00110 m_widgets.push_back(w);
00111
00112 return (WIDGET_HANDLE)w;
00113 }
00114
00115 WIDGET_HANDLE CQtMainWindow::AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00116 {
00117 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00118
00119 CQtMainWindowWidget *w = new CQtMainWindowWidget(this, eLabel);
00120
00121 QLabel *pLabel = new QLabel((p != NULL ? p->m_widget : this));
00122 pLabel->setText(text);
00123 pLabel->setFixedSize(width, height);
00124 pLabel->move(x, y);
00125
00126 w->m_widget = pLabel;
00127
00128 m_widgets.push_back(w);
00129
00130 return (WIDGET_HANDLE)w;
00131 }
00132
00133 WIDGET_HANDLE CQtMainWindow::AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent)
00134 {
00135 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00136
00137 CQtMainWindowWidget *w = new CQtMainWindowWidget(this, eCheckBox);
00138
00139 QCheckBox *pCheckBox = new QCheckBox((p != NULL ? p->m_widget : this));
00140 pCheckBox->setText(text);
00141 pCheckBox->setFixedSize(width, height);
00142 pCheckBox->move(x, y);
00143 pCheckBox->setChecked(checked);
00144
00145 w->m_widget = pCheckBox;
00146
00147 connect(pCheckBox, SIGNAL(toggled(bool)), w, SLOT(Toggled(bool)));
00148
00149 m_widgets.push_back(w);
00150
00151 return (WIDGET_HANDLE)w;
00152 }
00153
00154 WIDGET_HANDLE CQtMainWindow::AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00155 {
00156 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00157
00158 CQtMainWindowWidget *w = new CQtMainWindowWidget(this, eTextEdit);
00159
00160 QLineEdit *pLineEdit = new QLineEdit((p != NULL ? p->m_widget : this));
00161 pLineEdit->setText(text);
00162 pLineEdit->setFixedSize(width, height);
00163 pLineEdit->move(x, y);
00164
00165 w->m_widget = pLineEdit;
00166
00167 connect(pLineEdit, SIGNAL(textChanged(const QString &)), w, SLOT(TextChanged(const QString &)));
00168
00169 m_widgets.push_back(w);
00170
00171 return (WIDGET_HANDLE)w;
00172 }
00173
00174 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)
00175 {
00176 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00177
00178 CQtMainWindowWidget *w = new CQtMainWindowWidget(this, eSlider);
00179
00180 QSlider *pSlider = new QSlider(Qt::Horizontal, (p != NULL ? p->m_widget : this));
00181 pSlider->setFixedSize(width, height);
00182 pSlider->move(x, y);
00183 #if QT_VERSION >= 0x040000
00184 pSlider->setMinimum(min_value);
00185 pSlider->setMaximum(max_value);
00186 pSlider->setTickPosition(QSlider::TicksBelow);
00187 pSlider->setFocusPolicy(Qt::StrongFocus);
00188 #else
00189 pSlider->setMinValue(min_value);
00190 pSlider->setMaxValue(max_value);
00191 pSlider->setTickmarks(QSlider::Below);
00192 pSlider->setFocusPolicy(QWidget::StrongFocus);
00193 #endif
00194 pSlider->setValue(value);
00195 pSlider->setPageStep(1);
00196 pSlider->setTickInterval(step);
00197
00198
00199 w->m_widget = pSlider;
00200
00201 connect(pSlider, SIGNAL(valueChanged(int)), w, SLOT(ValueChanged(int)));
00202
00203 m_widgets.push_back(w);
00204
00205 return (WIDGET_HANDLE)w;
00206 }
00207
00208 WIDGET_HANDLE CQtMainWindow::AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent)
00209 {
00210 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00211
00212 CQtMainWindowWidget *w = new CQtMainWindowWidget(this, eComboBox);
00213
00214 QComboBox *pComboBox = new QComboBox((p != NULL ? p->m_widget : this));
00215 pComboBox->setFixedSize(width, height);
00216 pComboBox->move(x, y);
00217
00218 #if QT_VERSION >= 0x040000
00219 for (int i = 0; i < num_entries; i++)
00220 {
00221 pComboBox->addItem(entries[i]);
00222 }
00223 pComboBox->setCurrentIndex(current_entry);
00224 #else
00225 for (int i = 0; i < num_entries; i++)
00226 {
00227 pComboBox->insertItem(entries[i]);
00228 }
00229 pComboBox->setCurrentItem(current_entry);
00230 #endif
00231
00232 w->m_widget = pComboBox;
00233
00234 connect(pComboBox, SIGNAL(activated(int)), w, SLOT(ValueChanged(int)));
00235
00236 m_widgets.push_back(w);
00237
00238 return (WIDGET_HANDLE)w;
00239 }
00240
00241 WIDGET_HANDLE CQtMainWindow::AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent)
00242 {
00243 #ifdef USE_OPENGL
00244 CQtMainWindowWidget *p = (CQtMainWindowWidget*)parent;
00245
00246 CQtGLWidget *pGLWidget = new CQtGLWidget(this, (p != NULL ? p->m_widget : this));
00247 pGLWidget->setFixedSize(width, height);
00248 pGLWidget->move(x, y);
00249
00250 CQtMainWindowWidget *w = pGLWidget;
00251
00252 m_widgets.push_back(w);
00253
00254 return (WIDGET_HANDLE)w;
00255 #else
00256 printf("WARNING: CQtMainWindow::AddGLWidget has been called, but USE_OPENGL = 0 in Makefile.base\n");
00257 return (WIDGET_HANDLE)NULL;
00258
00259 #endif
00260 }
00261
00262
00263
00264 #if QT_VERSION >= 0x040000
00265 #define GET_ASCII toAscii
00266 #else
00267 #define GET_ASCII ascii
00268 #endif
00269
00270 bool CQtMainWindow::GetText(WIDGET_HANDLE widget, char *text, int len)
00271 {
00272 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00273
00274 if (w->m_type == eButton)
00275 {
00276 QPushButton *btn = (QPushButton*)w->m_widget;
00277 QString str = btn->text();
00278 strncpy(text, str.GET_ASCII(), len);
00279
00280 return true;
00281 }
00282 else if(w->m_type == eCheckBox)
00283 {
00284 QCheckBox *btn = (QCheckBox*)w->m_widget;
00285 QString str = btn->text();
00286 strncpy(text, str.GET_ASCII(), len);
00287
00288 return true;
00289 }
00290 else if (w->m_type == eTextEdit)
00291 {
00292 QLineEdit *le = (QLineEdit*)w->m_widget;
00293 QString str = le->text();
00294 strncpy(text, str.GET_ASCII(), len);
00295
00296 return true;
00297 }
00298 else if (w->m_type == eLabel)
00299 {
00300 QLabel *label = (QLabel*)w->m_widget;
00301 QString str = label->text();
00302 strncpy(text, str.GET_ASCII(), len);
00303
00304 return true;
00305 }
00306
00307 return false;
00308 }
00309 bool CQtMainWindow::SetText(WIDGET_HANDLE widget, const char *text)
00310 {
00311 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00312
00313 if (w->m_type == eButton)
00314 {
00315 QPushButton *btn = (QPushButton*)w->m_widget;
00316 btn->setText(text);
00317
00318 return true;
00319 }
00320 else if (w->m_type == eCheckBox)
00321 {
00322 QCheckBox *btn = (QCheckBox*)w->m_widget;
00323 btn->setText(text);
00324
00325 return true;
00326 }
00327 else if (w->m_type == eTextEdit)
00328 {
00329 QLineEdit *le = (QLineEdit*)w->m_widget;
00330 le->setText(text);
00331
00332 return true;
00333 }
00334 else if (w->m_type == eLabel)
00335 {
00336 QLabel *label = (QLabel*)w->m_widget;
00337 label->setText(text);
00338
00339 return true;
00340 }
00341
00342 return false;
00343 }
00344
00345 bool CQtMainWindow::SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
00346 {
00347 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00348
00349 if (w->m_type == eImage)
00350 {
00351 CQtImageWidget *iw = (CQtImageWidget*)w;
00352
00353 iw->SetImage(pImage);
00354
00355 return true;
00356 }
00357
00358 return false;
00359 }
00360
00361 bool CQtMainWindow::GetValue(WIDGET_HANDLE widget, int &value)
00362 {
00363 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00364
00365 if (w->m_type == eCheckBox)
00366 {
00367 QCheckBox *cb = (QCheckBox*)w->m_widget;
00368
00369 value = cb->isChecked();
00370
00371 return true;
00372 }
00373 else if (w->m_type == eSlider)
00374 {
00375 QSlider *sl = (QSlider*)w->m_widget;
00376
00377 value = sl->value();
00378
00379 return true;
00380 }
00381 else if (w->m_type == eComboBox)
00382 {
00383 QComboBox *cb = (QComboBox*)w->m_widget;
00384
00385 #if QT_VERSION >= 0x040000
00386 value = cb->currentIndex();
00387 #else
00388 value = cb->currentItem();
00389 #endif
00390
00391 return true;
00392 }
00393
00394 return false;
00395 }
00396
00397 bool CQtMainWindow::SetValue(WIDGET_HANDLE widget, int value)
00398 {
00399 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00400
00401 if (w->m_type == eCheckBox)
00402 {
00403 QCheckBox *cb = (QCheckBox*)w->m_widget;
00404
00405 cb->setChecked(value != 0);
00406
00407 return true;
00408 }
00409 else if (w->m_type == eSlider)
00410 {
00411 QSlider *sl = (QSlider*)w->m_widget;
00412
00413 sl->setValue(value);
00414
00415 return true;
00416 }
00417 else if (w->m_type == eComboBox)
00418 {
00419 QComboBox *cb = (QComboBox*)w->m_widget;
00420
00421 #if QT_VERSION >= 0x040000
00422 cb->setCurrentIndex(value);
00423 #else
00424 cb->setCurrentItem(value);
00425 #endif
00426
00427 return true;
00428 }
00429
00430 return false;
00431 }
00432
00433 bool CQtMainWindow::SwapBuffersGLWidget(WIDGET_HANDLE widget)
00434 {
00435 #ifdef USE_OPENGL
00436
00437 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00438
00439 if (w->m_type == eGLWidget)
00440 {
00441 QGLWidget *glw = (QGLWidget*)w->m_widget;
00442
00443
00444 glw->updateGL();
00445
00446 return true;
00447 }
00448
00449 #endif
00450
00451 return false;
00452 }
00453
00454 bool CQtMainWindow::MakeCurrentGLWidget(WIDGET_HANDLE widget)
00455 {
00456 #ifdef USE_OPENGL
00457
00458 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00459
00460 if (w->m_type == eGLWidget)
00461 {
00462 QGLWidget *glw = (QGLWidget*)w->m_widget;
00463
00464 glw->makeCurrent();
00465
00466 return true;
00467 }
00468 #endif
00469
00470 return false;
00471 }
00472
00473
00474
00475
00476 void CQtMainWindow::Show(WIDGET_HANDLE widget)
00477 {
00478 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00479
00480 if (w == NULL)
00481 {
00482 show();
00483 }
00484 else
00485 {
00486 w->m_widget->show();
00487 }
00488 }
00489 void CQtMainWindow::Hide(WIDGET_HANDLE widget)
00490 {
00491 CQtMainWindowWidget *w = (CQtMainWindowWidget*)widget;
00492
00493 if (w == NULL)
00494 {
00495 hide();
00496 }
00497 else
00498 {
00499 w->m_widget->hide();
00500 }
00501 }
00502
00503 #if QT_VERSION < 0x040000
00504
00505 #ifdef Q_WS_WIN
00506 # include <windows.h>
00507 # include <shellapi.h>
00508 #elif defined Q_WS_X11
00509 # include <X11/Xlib.h>
00510 #else
00511 # include <Carbon/Carbon.h>
00512
00513
00514
00515
00516
00517
00518
00519
00520 #endif
00521 #include <qpaintdevice.h>
00522
00523
00524
00525 static Qt::ButtonState get_modifiers()
00526 {
00527 int result = Qt::NoButton;
00528
00529 #if defined(Q_WS_WIN)
00530
00531 result |= GetAsyncKeyState(VK_SHIFT) < 0 ? Qt::ShiftButton : 0;
00532 result |= GetAsyncKeyState(VK_MENU) < 0 ? Qt::AltButton : 0;
00533 result |= GetAsyncKeyState(VK_CONTROL) < 0 ? Qt::ControlButton : 0;
00534
00535 #elif defined(Q_WS_X11)
00536
00537 Window root, child;
00538 int root_x, root_y;
00539 int win_x, win_y;
00540 uint keys_buttons;
00541 bool status = XQueryPointer(QPaintDevice::x11AppDisplay(),
00542 QPaintDevice::x11AppRootWindow(),
00543 &root, &child,
00544 &root_x, &root_y,
00545 &win_x, &win_y,
00546 &keys_buttons);
00547
00548 if (status)
00549 {
00550 result |= keys_buttons & ShiftMask ? Qt::ShiftButton : 0;
00551 result |= keys_buttons & Mod1Mask ? Qt::AltButton : 0;
00552 result |= keys_buttons & ControlMask ? Qt::ControlButton : 0;
00553 }
00554
00555 #else
00556
00557 UInt32 modifiers = GetCurrentKeyModifiers();
00558 if (modifiers & (1 << shiftKeyBit) modifiers |= Qt::ShiftButton;
00559 if (modifiers & (1 << rightShiftKeyBit) modifiers |= Qt::ShiftButton;
00560 if (modifiers & (1 << optionKeyBit) modifiers |= Qt::AltButton;
00561 if (modifiers & (1 << rightOptionKeyBit) modifiers |= Qt::AltButton;
00562 if (modifiers & (1 << controlKeyBit) modifiers |= Qt::ControlButton;
00563 if (modifiers & (1 << rightControlKeyBit) modifiers |= Qt::ControlButton;
00564
00565 #endif
00566
00567 return Qt::ButtonState(result);
00568 }
00569
00570 #endif
00571
00572
00573
00574 int CQtMainWindow::GetModifierKeyState()
00575 {
00576 int state = 0;
00577 int mod = 0;
00578
00579 #if QT_VERSION >= 0x040000
00580 mod = qApp->keyboardModifiers();
00581 if (mod & Qt::ShiftModifier)
00582 state |= IVT_SHIFT_KEY;
00583 if (mod & Qt::ControlModifier)
00584 state |= IVT_CONTROL_KEY;
00585 if (mod & Qt::AltModifier)
00586 state |= IVT_ALT_KEY;
00587 #else
00588 mod = get_modifiers();
00589 if (mod & Qt::ShiftButton)
00590 state |= IVT_SHIFT_KEY;
00591 if (mod & Qt::ControlButton)
00592 state |= IVT_CONTROL_KEY;
00593 if (mod & Qt::AltButton)
00594 state |= IVT_ALT_KEY;
00595 #endif
00596
00597 return state;
00598 }
00599
00600
00601 void CQtMainWindow::closeEvent(QCloseEvent *e)
00602 {
00603 m_ref_count--;
00604 if (m_ref_count == 0)
00605 {
00606 CQtApplicationHandler *app_handler = CQtApplicationHandler::GetApplicationHandler();
00607
00608 if (app_handler)
00609 app_handler->Exit();
00610 }
00611 }