00001
00002
00003
00004
00005
00006
00007
00008 #include "QtMainWindowWidget.h"
00009 #include "QtMainWindow.h"
00010
00011 #include <qpainter.h>
00012 #include <qimage.h>
00013 #include <qevent.h>
00014
00015 #include "Interfaces/MainWindowEventInterface.h"
00016 #include "Image/ByteImage.h"
00017
00018 #include <stdio.h>
00019
00020
00021 CQtMainWindowWidget::CQtMainWindowWidget(CQtMainWindow *main_window, QtWidgetType type)
00022 : m_main_window(main_window), m_type(type), m_widget(NULL)
00023 {
00024 }
00025
00026
00027 void CQtMainWindowWidget::Clicked()
00028 {
00029 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00030
00031 if (ec != NULL && m_type == eButton)
00032 {
00033 ec->ButtonPushed(this);
00034 }
00035 }
00036 void CQtMainWindowWidget::ValueChanged(int value)
00037 {
00038 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00039
00040 if (ec != NULL && (m_type == eSlider || m_type == eComboBox))
00041 {
00042 ec->ValueChanged(this, value);
00043 }
00044 }
00045 void CQtMainWindowWidget::Toggled(bool flag)
00046 {
00047 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00048
00049 if (ec != NULL && m_type == eCheckBox)
00050 {
00051 ec->ValueChanged(this, (flag ? 1 : 0));
00052 }
00053 }
00054 void CQtMainWindowWidget::TextChanged(const QString &str)
00055 {
00056 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00057
00058 if (ec != NULL && m_type == eTextEdit)
00059 {
00060 ec->ValueChanged(this, -1);
00061 }
00062 }
00063
00064
00065
00066 CQtImageWidget::CQtImageWidget(CQtMainWindow *main_window, QWidget *pParent)
00067 : QWidget(pParent), CQtMainWindowWidget(main_window, eImage), m_pBuffer(NULL), m_nWidth(0), m_nHeight(0), m_mouse_down(false)
00068 {
00069 m_widget = this;
00070
00071 show();
00072 #if QT_VERSION >= 0x040000
00073 setBackgroundRole(QPalette::NoRole);
00074 #else
00075 setBackgroundMode(Qt::NoBackground);
00076 #endif
00077
00078 setMouseTracking(true);
00079
00080 #if QT_VERSION >= 0x040000
00081 setFocusPolicy(Qt::ClickFocus);
00082 #else
00083 setFocusPolicy(QWidget::ClickFocus);
00084 #endif
00085 }
00086
00087 CQtImageWidget::~CQtImageWidget()
00088 {
00089 if (m_pBuffer != NULL)
00090 delete [] m_pBuffer;
00091 }
00092
00093 void CQtImageWidget::SetImage(const CByteImage *pImage)
00094 {
00095 if (m_nWidth != pImage->width || m_nHeight != pImage->height)
00096 {
00097 m_nWidth = pImage->width;
00098 m_nHeight = pImage->height;
00099
00100 if (m_pBuffer != NULL)
00101 delete [] m_pBuffer;
00102
00103 m_pBuffer = new unsigned char[m_nWidth * m_nHeight * 4];
00104 }
00105
00106 if (pImage->type == CByteImage::eGrayScale)
00107 {
00108 const int nPixels = m_nWidth * m_nHeight;
00109 const unsigned char *pixels = pImage->pixels;
00110 int *output = (int *) m_pBuffer;
00111
00112 for (int i = 0; i < nPixels; i++)
00113 output[i] = 255 << 24 | pixels[i] << 16 | pixels[i] << 8 | pixels[i];
00114 }
00115 else if (pImage->type == CByteImage::eRGB24)
00116 {
00117 const int nPixels = m_nWidth * m_nHeight;
00118 const unsigned char *pixels = pImage->pixels;
00119 int *output = (int *) m_pBuffer;
00120
00121 for (int offset = 0, i = 0; i < nPixels; i++)
00122 {
00123 output[i] = 255 << 24 | pixels[offset] << 16 | pixels[offset + 1] << 8 | pixels[offset + 2];
00124 offset += 3;
00125 }
00126 }
00127 else if (pImage->type == CByteImage::eRGB24Split)
00128 {
00129 const int nPixels = m_nWidth * m_nHeight;
00130 const unsigned char *pixels_r = pImage->pixels;
00131 const unsigned char *pixels_g = pixels_r + nPixels;
00132 const unsigned char *pixels_b = pixels_g + nPixels;
00133 int *output = (int *) m_pBuffer;
00134
00135 for (int i = 0; i < nPixels; i++)
00136 output[i] = 255 << 24 | pixels_r[i] << 16 | pixels_g[i] << 8 | pixels_b[i];
00137 }
00138
00139 #if QT_VERSION >= 0x040000
00140 repaint(0, 0, m_nWidth, m_nHeight);
00141 #else
00142 repaint(0, 0, m_nWidth, m_nHeight, false);
00143 #endif
00144 }
00145
00146 void CQtImageWidget::paintEvent(QPaintEvent *pPaintEvent)
00147 {
00148 if (m_pBuffer != NULL)
00149 {
00150 QPainter painter(this);
00151
00152 #if QT_VERSION >= 0x040000
00153 QImage image(m_pBuffer, m_nWidth, m_nHeight, QImage::Format_RGB32);
00154 #else
00155 QImage image(m_pBuffer, m_nWidth, m_nHeight, 32, 0, 0, QImage::BigEndian);
00156 #endif
00157
00158 painter.drawImage(0, 0, image);
00159
00160 if (m_mouse_down)
00161 {
00162 unsigned int x0 = m_mouse_start_x;
00163 unsigned int y0 = m_mouse_start_y;
00164 unsigned int x1 = m_mouse_current_x;
00165 unsigned int y1 = m_mouse_current_y;
00166
00167 if (x0 > x1)
00168 {
00169 unsigned int swap = x0;
00170 x0 = x1;
00171 x1 = swap;
00172 }
00173 if (y0 > y1)
00174 {
00175 unsigned int swap = y0;
00176 y0 = y1;
00177 y1 = swap;
00178 }
00179
00180 QPen pen(Qt::white, 1);
00181 painter.setPen(pen);
00182 painter.drawRect(x0, y0, x1 - x0, y1 - y0);
00183 }
00184 }
00185 }
00186
00187 void CQtImageWidget::mousePressEvent(QMouseEvent * e)
00188 {
00189 if (e->button() == Qt::LeftButton)
00190 {
00191 if (m_mouse_down)
00192 {
00193 m_mouse_down = false;
00194 }
00195 else
00196 {
00197 m_mouse_down = true;
00198
00199 m_mouse_start_x = e->x();
00200 m_mouse_start_y = e->y();
00201
00202 m_mouse_current_x = m_mouse_start_x;
00203 m_mouse_current_y = m_mouse_start_y;
00204 }
00205 }
00206
00207 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00208
00209 if (ec != NULL)
00210 {
00211 int btn = -1;
00212
00213 switch (e->button())
00214 {
00215 case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
00216 case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
00217 case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
00218 default: printf("unknown mouse button pressed\n"); break;
00219 }
00220
00221 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00222 ec->MouseDown(w, btn, e->x(), e->y());
00223 }
00224 }
00225
00226 void CQtImageWidget::mouseReleaseEvent(QMouseEvent * e)
00227 {
00228 if (e->button() == Qt::LeftButton && m_mouse_down)
00229 {
00230 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00231
00232 if (m_mouse_start_x == m_mouse_current_x && m_mouse_start_y == m_mouse_current_y)
00233 {
00234 if (ec != NULL)
00235 {
00236 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00237 ec->PointClicked(w, m_mouse_start_x, m_mouse_start_y);
00238 }
00239 }
00240 else
00241 {
00242 unsigned int x0 = m_mouse_start_x;
00243 unsigned int y0 = m_mouse_start_y;
00244 unsigned int x1 = m_mouse_current_x;
00245 unsigned int y1 = m_mouse_current_y;
00246
00247 if (x0 > x1)
00248 {
00249 unsigned int swap = x0;
00250 x0 = x1;
00251 x1 = swap;
00252 }
00253 if (y0 > y1)
00254 {
00255 unsigned int swap = y0;
00256 y0 = y1;
00257 y1 = swap;
00258 }
00259
00260 if (ec != NULL)
00261 {
00262 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00263 ec->RectSelected(w, x0, y0, x1, y1);
00264 }
00265
00266 update(x0, y0, x1 - x0, y1 - y0);
00267 }
00268
00269 m_mouse_down = false;
00270 }
00271
00272 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00273
00274 if (ec != NULL)
00275 {
00276 int btn = -1;
00277
00278 switch (e->button())
00279 {
00280 case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
00281 case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
00282 case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
00283 default: printf("unknown mouse button released\n"); break;
00284 }
00285
00286 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00287 ec->MouseUp(w, btn, e->x(), e->y());
00288 }
00289 }
00290
00291 void CQtImageWidget::mouseMoveEvent(QMouseEvent * e)
00292 {
00293 if (m_mouse_down)
00294 {
00295 unsigned int x0 = m_mouse_start_x;
00296 unsigned int y0 = m_mouse_start_y;
00297 unsigned int x1 = m_mouse_current_x;
00298 unsigned int y1 = m_mouse_current_y;
00299
00300 if (x0 > x1)
00301 {
00302 unsigned int swap = x0;
00303 x0 = x1;
00304 x1 = swap;
00305 }
00306 if (y0 > y1)
00307 {
00308 unsigned int swap = y0;
00309 y0 = y1;
00310 y1 = swap;
00311 }
00312
00313 update(x0, y0, x1 - x0, y1 - y0);
00314
00315 m_mouse_current_x = e->x();
00316 m_mouse_current_y = e->y();
00317
00318 if (m_mouse_current_x < 0)
00319 m_mouse_current_x = 0;
00320 if (m_mouse_current_x >= m_nWidth)
00321 m_mouse_current_x = m_nWidth-1;
00322
00323 if (m_mouse_current_y < 0)
00324 m_mouse_current_y = 0;
00325 if (m_mouse_current_y >= m_nHeight)
00326 m_mouse_current_y = m_nHeight-1;
00327
00328 x0 = m_mouse_start_x;
00329 y0 = m_mouse_start_y;
00330 x1 = m_mouse_current_x;
00331 y1 = m_mouse_current_y;
00332
00333 if (x0 > x1)
00334 {
00335 unsigned int swap = x0;
00336 x0 = x1;
00337 x1 = swap;
00338 }
00339 if (y0 > y1)
00340 {
00341 unsigned int swap = y0;
00342 y0 = y1;
00343 y1 = swap;
00344 }
00345
00346 update(x0, y0, x1 - x0, y1 - y0);
00347 }
00348
00349 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00350
00351 if (ec != NULL)
00352 {
00353 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00354 ec->MouseMove(w, e->x(), e->y());
00355 }
00356 }
00357
00358 void CQtImageWidget::keyPressEvent(QKeyEvent * e)
00359 {
00360 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00361
00362 if (ec != NULL)
00363 {
00364 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00365
00366 #if QT_VERSION >= 0x040000
00367 ec->KeyDown(w, e->text().toInt());
00368 #else
00369 ec->KeyDown(w, e->ascii());
00370 #endif
00371 }
00372 }
00373
00374 void CQtImageWidget::keyReleaseEvent(QKeyEvent * e)
00375 {
00376 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00377
00378 if (ec != NULL)
00379 {
00380 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00381
00382 #if QT_VERSION >= 0x040000
00383 ec->KeyUp(w, e->text().toInt());
00384 #else
00385 ec->KeyUp(w, e->ascii());
00386 #endif
00387 }
00388 }
00389
00390
00391
00392 #ifdef USE_OPENGL
00393
00394 CQtGLWidget::CQtGLWidget(CQtMainWindow *main_window, QWidget *pParent)
00395 : QGLWidget(pParent), CQtMainWindowWidget(main_window, eGLWidget)
00396 {
00397 m_widget = this;
00398
00399 show();
00400
00401 setMouseTracking(true);
00402
00403 #if QT_VERSION >= 0x040000
00404 setFocusPolicy(Qt::ClickFocus);
00405 #else
00406 setFocusPolicy(QWidget::ClickFocus);
00407 #endif
00408 }
00409
00410 CQtGLWidget::~CQtGLWidget()
00411 {
00412 }
00413
00414 void CQtGLWidget::mousePressEvent(QMouseEvent * e)
00415 {
00416 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00417
00418 if (ec != NULL)
00419 {
00420 int btn = -1;
00421
00422 switch (e->button())
00423 {
00424 case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
00425 case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
00426 case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
00427 default: printf("unknown mouse button pressed\n"); break;
00428 }
00429
00430 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00431 ec->MouseDown(w, btn, e->x(), e->y());
00432 }
00433 }
00434
00435 void CQtGLWidget::mouseReleaseEvent(QMouseEvent * e)
00436 {
00437 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00438
00439 if (ec != NULL)
00440 {
00441 int btn = -1;
00442
00443 switch (e->button())
00444 {
00445 case Qt::LeftButton: btn = IVT_LEFT_BUTTON; break;
00446 case Qt::RightButton: btn = IVT_RIGHT_BUTTON; break;
00447 case Qt::MidButton: btn = IVT_MIDDLE_BUTTON; break;
00448 default: printf("unknown mouse button released\n"); break;
00449 }
00450
00451 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00452 ec->MouseUp(w, btn, e->x(), e->y());
00453 }
00454 }
00455
00456 void CQtGLWidget::mouseMoveEvent(QMouseEvent * e)
00457 {
00458 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00459
00460 if (ec != NULL)
00461 {
00462 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00463 ec->MouseMove(w, e->x(), e->y());
00464 }
00465 }
00466
00467 void CQtGLWidget::keyPressEvent(QKeyEvent * e)
00468 {
00469 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00470
00471 if (ec != NULL)
00472 {
00473 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00474
00475 #if QT_VERSION >= 0x040000
00476 ec->KeyDown(w, e->text().toInt());
00477 #else
00478 ec->KeyDown(w, e->ascii());
00479 #endif
00480 }
00481 }
00482
00483 void CQtGLWidget::keyReleaseEvent(QKeyEvent * e)
00484 {
00485 CMainWindowEventInterface *ec = m_main_window->GetEventCallback();
00486
00487 if (ec != NULL)
00488 {
00489 CQtMainWindowWidget *w = (CQtMainWindowWidget*)this;
00490
00491 #if QT_VERSION >= 0x040000
00492 ec->KeyUp(w, e->text().toInt());
00493 #else
00494 ec->KeyUp(w, e->ascii());
00495 #endif
00496 }
00497 }
00498
00499 #endif