00001
00002
00003
00004
00005
00006
00007 #include "QWidget.h"
00008 #include <iostream>
00009 using namespace std;
00010
00011
00012
00013
00014 void QWidget::add( QWidget* widget )
00015 {
00016 Children.push_front(widget);
00017 childrenChanged();
00018 }
00019
00020
00021 void QWidget::remove( QWidget* widget )
00022 {
00023 if (FocusPath == widget) FocusPath = 0;
00024 Children.remove(widget);
00025 childrenChanged();
00026 }
00027
00028
00029 void QWidget::freeWidget( QWidget* widget )
00030 {
00031 if (Owner) Owner->freeWidget(widget);
00032 }
00033
00034
00035 void QWidget::clearFocusPath()
00036 {
00037 if (FocusPath != 0)
00038 {
00039 FocusPath->clearFocusPath();
00040 FocusPath = 0;
00041 }
00042 }
00043
00044
00045 bool QWidget::getFocused()
00046 {
00047 return (Owner->getFocusPath() == this);
00048 }
00049
00050
00051 void QWidget::free()
00052 {
00053 if (Owner) Owner->freeWidget(this);
00054 }
00055
00056
00057 bool QWidget::event( QEvent* e )
00058 {
00059
00060
00061 if (!Visible) return false;
00062
00063
00064 if (e->getType() == etDraw)
00065 {
00066 changeContext();
00067 draw(e);
00068 if (onCustomDraw!=0) (*onCustomDraw)(this,e);
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 eventToChildrenReversed(e);
00085
00086
00087 restoreContext();
00088 return false;
00089 }
00090
00091
00092
00093
00094 if (!Enabled) return false;
00095
00096
00097 if ((e->getType() == etKeyboard) ||
00098 (e->getType() == etSpecialKey ) ||
00099 (e->getType() == etKeyboardUp ))
00100 {
00101 return handleKeyboardEvent(e);
00102 }
00103
00104
00105
00106 return handleMouseEvent(e);
00107
00108 }
00109
00110
00111 void QWidget::redraw()
00112 {
00113 Owner->redraw();
00114 }
00115
00116
00117 bool QWidget::eventToChildren( QEvent* e )
00118 {
00119
00120 list<QWidget*>::const_iterator iter;
00121
00122 for (iter=Children.begin(); iter != Children.end(); iter++)
00123 {
00124 if ((*iter)->event(e)) return true;
00125 }
00126 return false;
00127 }
00128
00129
00130 bool QWidget::eventToChildrenReversed( QEvent* e )
00131 {
00132
00133 list<QWidget*>::reverse_iterator iter;
00134
00135 for (iter=Children.rbegin(); iter != Children.rend(); iter++)
00136 {
00137 if ((*iter)->event(e)) return true;
00138 }
00139 return false;
00140 }
00141
00142
00143 void QWidget::bringToFront( QWidget* widget )
00144 {
00145 unsigned int size = Children.size();
00146 Children.remove(widget);
00147 if ((Children.size()+1)== size) Children.push_front(widget);
00148 }
00149
00150
00151 bool QWidget::isInFront( QWidget* widget )
00152 {
00153 return (Children.front() == widget);
00154 }
00155
00156
00157 bool QWidget::handleKeyboardEvent( QEvent* e )
00158 {
00159 processEvent(e);
00160 if (onKey!=0) (*onKey)(this,e);
00161 if (FocusPath) FocusPath->event(e);
00162 return true;
00163 }
00164
00165
00166 bool QWidget::handleMouseEvent( QEvent* e )
00167 {
00168
00169
00170 if ((Dragging) && (e->getType() == etMouseMotion))
00171 {
00172 handleDragging(e);
00173 return true;
00174 }
00175
00176
00177 int saved_x = e->getX();
00178 int saved_y = e->getY();
00179
00180
00181 int x = saved_x - Left;
00182 int y = saved_y - Top;
00183
00184
00185
00186 bool ourRegion = ((x>=0) && (y>=0) && (x<=Width) && (y<=Height));
00187
00188 if (ourRegion && (e->getType() == etMouseDown))
00189 {
00190 doFocus(e);
00191 }
00192
00193
00194 e->setX(x);
00195 e->setY(y);
00196
00197
00198
00199
00200 bool handledByChildren = eventToChildren(e);
00201
00202
00203 e->setX(saved_x);
00204 e->setY(saved_y);
00205
00206
00207 if (handledByChildren) return true;
00208
00209
00210
00211
00212 if ((Dragging) && (e->getType() == etMouseUp)) Dragging = false;
00213
00214
00215 processMouseOwner(e);
00216
00217
00218
00219
00220 if (ourRegion)
00221 {
00222
00223 processEvent(e);
00224
00225
00226 switch (e->getType())
00227 {
00228 case etMouseClick:
00229 if (onClick!=0) (*onClick)(this,e);
00230 break;
00231
00232 case etMouseDown:
00233 if (canDrag(e))
00234 {
00235 Dragging = true;
00236 DragStartX = x;
00237 DragStartY = y;
00238 }
00239 if (onMouseDown!=0) (*onMouseDown)(this,e);
00240 break;
00241
00242 case etMouseUp:
00243
00244 if (onMouseUp!=0) (*onMouseUp)(this,e);
00245 break;
00246
00247 case etMouseMotion:
00248 if (onMouseMove!=0) (*onMouseMove)(this,e);
00249 break;
00250
00251 default: ;
00252 }
00253
00254
00255
00256 return true;
00257 }
00258
00259
00260
00261 return false;
00262
00263 }
00264
00265
00266 void QWidget::handleDragging( QEvent* e )
00267 {
00268
00269 const int snap_width = 5;
00270
00271 int new_x = e->getX()-DragStartX;
00272 int new_y = e->getY()-DragStartY;
00273
00274 int minx = Owner->getClientBorderLeft();
00275 int maxx = Owner->getWidth()-Width-Owner->getClientBorderRight();
00276 int miny = Owner->getClientBorderTop();
00277 int maxy = Owner->getHeight()-Height-Owner->getClientBorderBottom();
00278
00279 if (new_x < snap_width + minx ) new_x = minx;
00280 else
00281 if (new_x > maxx - snap_width) new_x = maxx;
00282
00283 if (new_y < snap_width + miny) new_y = miny;
00284 else
00285 if (new_y > maxy - snap_width) new_y = maxy;
00286
00287 setLeft(new_x);
00288 setTop (new_y);
00289
00290 }
00291
00292
00293 void QWidget::doFocus( QEvent* e )
00294 {
00295 if (Owner) Owner->bringToFront(this);
00296 if ((canFocus(e)) && (Owner!=0))
00297 {
00298 Owner->requestFocus(this,this);
00299 }
00300 }
00301
00302
00303 void QWidget::requestFocus( QWidget* Sender , QWidget* FocusedWidget )
00304 {
00305 if ((FocusPath != 0) && (FocusPath != Sender))
00306 {
00307 FocusPath->clearFocusPath();
00308 }
00309 FocusPath = Sender;
00310 Owner->requestFocus(this,FocusedWidget);
00311 }
00312
00313
00314 void QWidget::changeContext()
00315 {
00316 glPushMatrix();
00317 glTranslatef(Left,Top,0.0);
00318 }
00319
00320
00321 void QWidget::restoreContext()
00322 {
00323 glPopMatrix();
00324 }
00325
00326
00327 void QWidget::freeChildren()
00328 {
00329 while (Children.size() > 0)
00330 {
00331 delete Children.front();
00332 Children.pop_front();
00333 }
00334 }
00335
00336
00337 void QWidget::draw( QEvent* e )
00338 {
00339
00340 }
00341
00342
00343 void QWidget::layout()
00344 {
00345
00346 }
00347
00348
00349 bool QWidget::canDrag( QEvent* e )
00350 {
00351 return false;
00352 }
00353
00354
00355 bool QWidget::processEvent( QEvent* e )
00356 {
00357 return false;
00358 }
00359
00360
00361 bool QWidget::processMouseOwner( QEvent* e )
00362 {
00363 return false;
00364 }
00365
00366
00367 void QWidget::childrenChanged()
00368 {
00369 }
00370
00371
00372 bool QWidget::canFocus( QEvent* e )
00373 {
00374 return false;
00375 }
00376
00377
00378 void QWidget::Init()
00379 {
00380
00381 }
00382
00383
00384 void QWidget::CleanUp()
00385 {
00386
00387 }
00388
00389
00390 int QWidget::getWidth()
00391 {
00392 return Width;
00393 }
00394
00395
00396 void QWidget::setWidth( int newValue )
00397 {
00398 if (newValue < MinWidth) newValue = MinWidth;
00399 if (Width != newValue)
00400 {
00401 Width = newValue;
00402 layout();
00403 if (onResize!=0) (*onResize)(this);
00404 }
00405 }
00406
00407
00408 int QWidget::getHeight()
00409 {
00410 return Height;
00411 }
00412
00413
00414 void QWidget::setHeight( int newValue )
00415 {
00416 if (newValue < MinHeight) newValue = MinHeight;
00417 if (Height != newValue)
00418 {
00419 Height = newValue;
00420 layout();
00421 if (onResize!=0) (*onResize)(this);
00422 }
00423 }
00424
00425
00426 int QWidget::getTop()
00427 {
00428 return Top;
00429 }
00430
00431
00432 void QWidget::setTop( int newValue )
00433 {
00434 if (Top != newValue)
00435 {
00436 Top = newValue;
00437 }
00438 }
00439
00440
00441 int QWidget::getLeft()
00442 {
00443 return Left;
00444 }
00445
00446
00447 void QWidget::setLeft( int newValue )
00448 {
00449 if (Left != newValue)
00450 {
00451 Left = newValue;
00452 }
00453 }
00454
00455
00456 int QWidget::getMinWidth()
00457 {
00458 return MinWidth;
00459 }
00460
00461
00462 void QWidget::setMinWidth( int newValue )
00463 {
00464 MinWidth = newValue;
00465 }
00466
00467
00468 int QWidget::getMinHeight()
00469 {
00470 return MinHeight;
00471 }
00472
00473
00474 void QWidget::setMinHeight( int newValue )
00475 {
00476 MinHeight = newValue;
00477 }
00478
00479
00480 int QWidget::getClientBorderLeft()
00481 {
00482 return ClientBorderLeft;
00483 }
00484
00485
00486 void QWidget::setClientBorderLeft( int newValue )
00487 {
00488 ClientBorderLeft = newValue;
00489 }
00490
00491
00492 int QWidget::getClientBorderRight()
00493 {
00494 return ClientBorderRight;
00495 }
00496
00497
00498 void QWidget::setClientBorderRight( int newValue )
00499 {
00500 ClientBorderRight = newValue;
00501 }
00502
00503
00504 int QWidget::getClientBorderTop()
00505 {
00506 return ClientBorderTop;
00507 }
00508
00509
00510 void QWidget::setClientBorderTop( int newValue )
00511 {
00512 ClientBorderTop = newValue;
00513 }
00514
00515
00516 int QWidget::getClientBorderBottom()
00517 {
00518 return ClientBorderBottom;
00519 }
00520
00521
00522 void QWidget::setClientBorderBottom( int newValue )
00523 {
00524 ClientBorderBottom = newValue;
00525 }
00526
00527
00528 bool QWidget::getEnabled()
00529 {
00530 return Enabled;
00531 }
00532
00533
00534 void QWidget::setEnabled( bool newValue )
00535 {
00536 Enabled = newValue;
00537 }
00538
00539
00540 bool QWidget::getVisible()
00541 {
00542 return Visible;
00543 }
00544
00545
00546 void QWidget::setVisible( bool newValue )
00547 {
00548 Visible = newValue;
00549 }
00550
00551
00552 QLookAndFeel* QWidget::getLookAndFeel()
00553 {
00554 return LookAndFeel;
00555 }
00556
00557
00558 void QWidget::setLookAndFeel( QLookAndFeel* newValue )
00559 {
00560 LookAndFeel = newValue;
00561 }
00562
00563
00564 QWidget* QWidget::getOwner()
00565 {
00566 return Owner;
00567 }
00568
00569
00570 void QWidget::setOwner( QWidget* newValue )
00571 {
00572 Owner = newValue;
00573 }
00574
00575
00576 QWidget* QWidget::getFocusPath()
00577 {
00578 return FocusPath;
00579 }
00580
00581
00582 void QWidget::setFocusPath( QWidget* newValue )
00583 {
00584 FocusPath = newValue;
00585 }
00586
00587
00588
00589
00590 void QWidget::print()
00591 {
00592
00593 cout << "Class : QWidget" << endl;
00594 cout << "Width = " << Width << endl;
00595 cout << "Height = " << Height << endl;
00596 cout << "Top = " << Top << endl;
00597 cout << "Left = " << Left << endl;
00598 cout << "Enabled = " << Enabled << endl;
00599 cout << "Visible = " << Visible << endl;
00600 cout << "Owner = " << Owner << endl;
00601 cout << "Children = " << Children.size() << endl;
00602 }
00603
00604
00605
00606
00607 QWidget::QWidget( QWidget* aOwner )
00608 {
00609 onResize = 0;
00610 onCustomDraw = 0;
00611 onKey = 0;
00612 onMouseMove = 0;
00613 onMouseUp = 0;
00614 onMouseDown = 0;
00615 onClick = 0;
00616 DragStartX = 0;
00617 DragStartY = 0;
00618 Width = 20;
00619 Height = 10;
00620 Top = 0;if (aOwner) Top = aOwner->getClientBorderTop();
00621 Left = 0;if (aOwner) Left = aOwner->getClientBorderLeft();
00622 MinWidth = 0;
00623 MinHeight = 0;
00624 ClientBorderLeft = 0;
00625 ClientBorderRight = 0;
00626 ClientBorderTop = 0;
00627 ClientBorderBottom = 0;
00628 Enabled = true;
00629 Visible = true;
00630 Dragging = false;
00631 LookAndFeel = 0;if (aOwner) LookAndFeel = aOwner->getLookAndFeel();
00632 Owner = aOwner;if (aOwner) aOwner->add(this);
00633 FocusPath = 0;
00634 Init();
00635 layout();
00636 }
00637
00638
00639
00640
00641 QWidget::~QWidget()
00642 {
00643 freeChildren();
00644 CleanUp();
00645 }
00646
00647
00648
00649