Main Page   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

/cygdrive/d/Eigene Dateien/!DProcs/code_gen/QWidget.cpp

Go to the documentation of this file.
00001 // Class QWidget 
00002 // Common super class for all widgets. Here is also the basic implementation of event passing and dragging
00003 // Filename QWidget.cpp
00004 // FaPra 2003-2004, A5,  Alexander Kramer
00005 
00006 
00007 #include "QWidget.h"
00008 #include <iostream>
00009 using namespace std;
00010 
00011 // ----------------------------------------------------------
00012 
00013 //! adds given widget to the children
00014 void QWidget::add( QWidget*  widget )
00015 {
00016   Children.push_front(widget);
00017   childrenChanged();
00018 } // end of add()
00019 
00020 //! removes given widget from the children
00021 void QWidget::remove( QWidget*  widget )
00022 {
00023   if (FocusPath == widget) FocusPath = 0;
00024   Children.remove(widget);
00025   childrenChanged();
00026 } // end of remove()
00027 
00028 //! sends request for freeing a widget to parent
00029 void QWidget::freeWidget( QWidget*  widget )
00030 {
00031   if (Owner) Owner->freeWidget(widget);
00032 } // end of freeWidget()
00033 
00034 //! clears the focus path by setting it to 0 recursively
00035 void QWidget::clearFocusPath()
00036 {
00037   if (FocusPath != 0)
00038    {
00039       FocusPath->clearFocusPath();
00040       FocusPath = 0;
00041    }
00042 } // end of clearFocusPath()
00043 
00044 //! returns true if widget has keyboard focus
00045 bool QWidget::getFocused()
00046 {
00047   return (Owner->getFocusPath() == this);
00048 } // end of getFocused()
00049 
00050 //! frees the widget and all resources. This method must be used insted of explicit delete
00051 void QWidget::free()
00052 {
00053   if (Owner) Owner->freeWidget(this);
00054 } // end of free()
00055 
00056 //! primary event receiver method. Returns true, if event was handled. In this case the event won't be passed to next child
00057 bool QWidget::event( QEvent*  e )
00058 {
00059 
00060   // if widget is not visible there is no event handling
00061   if (!Visible) return false;
00062 
00063   // check for draw event first
00064   if (e->getType() == etDraw)
00065    {
00066      changeContext();
00067      draw(e);
00068      if (onCustomDraw!=0) (*onCustomDraw)(this,e);
00069 
00070      /*
00071   glColorMask(0,0,0,0);
00072   glEnable(GL_STENCIL_TEST);
00073   glStencilFunc(GL_ALWAYS,1,1);
00074   glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
00075   glBegin(GL_QUADS);
00076   LookAndFeel->drawRect(0,0, Width, Height);
00077   glEnd();
00078   glColorMask(1,1,1,1);
00079   glStencilFunc(GL_EQUAL,1,1);
00080   glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
00081   glDisable(GL_STENCIL_TEST);
00082   */
00083 
00084   eventToChildrenReversed(e);
00085 
00086 
00087      restoreContext();
00088      return false;
00089   }
00090 
00091   // if we are here, we have mouse or keyboard event
00092 
00093   // if widget is disabled, no mouse or keyboard events are handled
00094   if (!Enabled) return false;
00095 
00096   // check for keyboard events
00097   if  ((e->getType() == etKeyboard) ||
00098        (e->getType() == etSpecialKey )  ||
00099        (e->getType() == etKeyboardUp ))
00100   {
00101      return handleKeyboardEvent(e);
00102   }
00103 
00104    // if we are here we have mouse event
00105 
00106    return handleMouseEvent(e);
00107 
00108 } // end of event()
00109 
00110 //! post request for redisplay
00111 void QWidget::redraw()
00112 {
00113   Owner->redraw();
00114 } // end of redraw()
00115 
00116 //! sends the event to each child. Returns true if event was handled
00117 bool QWidget::eventToChildren( QEvent*  e )
00118 {
00119   // Create constant iterator for list.
00120   list<QWidget*>::const_iterator iter;
00121   // Iterate through list
00122   for (iter=Children.begin(); iter != Children.end(); iter++)
00123   {
00124     if ((*iter)->event(e)) return true;
00125   }
00126   return false;
00127 } // end of eventToChildren()
00128 
00129 //! sends the event to each child in reversed order. Returns true if event was handled
00130 bool QWidget::eventToChildrenReversed( QEvent*  e )
00131 {
00132  // Create constant iterator for list.
00133   list<QWidget*>::reverse_iterator iter;
00134   // Iterate through list
00135   for (iter=Children.rbegin(); iter != Children.rend(); iter++)
00136   {
00137     if ((*iter)->event(e)) return true;
00138   }
00139   return false;
00140 } // end of eventToChildrenReversed()
00141 
00142 //! brings the given widget to the front. Must be a child widget
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 } // end of bringToFront()
00149 
00150 //! Returns true if a given widget is in front
00151 bool QWidget::isInFront( QWidget*  widget )
00152 {
00153   return (Children.front() == widget);
00154 } // end of isInFront()
00155 
00156 //! handles the keyboard event
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 } // end of handleKeyboardEvent()
00164 
00165 //! handles the mouse event for this widget
00166 bool QWidget::handleMouseEvent( QEvent*  e )
00167 {
00168 
00169     // handle dragging
00170     if ((Dragging) && (e->getType() == etMouseMotion))
00171     {
00172       handleDragging(e);
00173       return true;
00174     }
00175 
00176       // save mouse coordinates for later restore
00177     int saved_x = e->getX();
00178     int saved_y = e->getY();
00179 
00180     // x and y are mouse coordinates relative to this widget
00181     int x = saved_x - Left;
00182     int y = saved_y - Top;
00183 
00184 
00185     //defines whether mouse coordinates are in this widget
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     // adjust mouse coordinates to be relative to this widget
00194     e->setX(x);
00195     e->setY(y);
00196 
00197 
00198     // pass mouse event to the children
00199     // handledByChildren is true if one of the
00200     bool handledByChildren = eventToChildren(e);
00201 
00202     // set mouse coordinates back to the saved values
00203     e->setX(saved_x);
00204     e->setY(saved_y);
00205 
00206     // return true if mouse event was handled in children widgets
00207     if (handledByChildren) return true;
00208 
00209     // So, the event was not handled by any child, see
00210     // what we can do with it.
00211 
00212     if ((Dragging) && (e->getType() == etMouseUp)) Dragging = false;
00213 
00214     // process mouse events in derived classes with owners coordinates
00215     processMouseOwner(e);
00216 
00217     // so we are not in dragging mode right now
00218     // check for entering dragging mode and other mouse events
00219 
00220     if  (ourRegion)
00221     {
00222        // processEvent in derived classes if we are in widgets region
00223        processEvent(e);
00224 
00225        // check for dragging and call custom event handlers
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        //the event was handled in this widget, so other children should not
00255        //get it -> return true
00256        return true;
00257     }
00258 
00259 
00260   // mouse event was not in our region -> return false
00261   return false;
00262 
00263 } // end of handleMouseEvent()
00264 
00265 //! handles the dragging
00266 void QWidget::handleDragging( QEvent*  e )
00267 {
00268    // defines snap width
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 } // end of handleDragging()
00291 
00292 //! tries to set focus to the current widget
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 } // end of doFocus()
00301 
00302 //! called if Sender wants to announce new focused control
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 } // end of requestFocus()
00312 
00313 //! changes the global drawing context
00314 void QWidget::changeContext()
00315 {
00316   glPushMatrix();
00317   glTranslatef(Left,Top,0.0);
00318 } // end of changeContext()
00319 
00320 //! restores the global drawing context
00321 void QWidget::restoreContext()
00322 {
00323   glPopMatrix();
00324 } // end of restoreContext()
00325 
00326 //! calls delete for every child widget
00327 void QWidget::freeChildren()
00328 {
00329   while (Children.size() > 0)
00330   {
00331     delete Children.front();
00332     Children.pop_front();
00333   }
00334 } // end of freeChildren()
00335 
00336 //! draws itself with OpenGL. Here is the OpenGL drawing code
00337 void QWidget::draw( QEvent*  e )
00338 {
00339 
00340 } // end of draw()
00341 
00342 //! layout the children widgets. This method is called every time the size is changed. Have to be overwritten for respond on size changes
00343 void QWidget::layout()
00344 {
00345 
00346 } // end of layout()
00347 
00348 //! have to return true if widget should be dragged with mouse. This method is overwritten in sub classes. If returns true, widget will be dragged
00349 bool QWidget::canDrag( QEvent*  e )
00350 {
00351  return false;
00352 } // end of canDrag()
00353 
00354 //! processing mouse/keyboard events which are in widgets region. Mouse coordinates are relative to Owner
00355 bool QWidget::processEvent( QEvent*  e )
00356 {
00357  return false;
00358 } // end of processEvent()
00359 
00360 //! processing mouse events which are in Owner widget region. Mouse coordinates are relative to Owner
00361 bool QWidget::processMouseOwner( QEvent*  e )
00362 {
00363  return false;
00364 } // end of processMouseOwner()
00365 
00366 //! called if a child is added/removed
00367 void QWidget::childrenChanged()
00368 {
00369 } // end of childrenChanged()
00370 
00371 //! defines whether control can have keyboard focus. Should return true if Widget wants to get keyboard focus
00372 bool QWidget::canFocus( QEvent*  e )
00373 {
00374  return false;
00375 } // end of canFocus()
00376 
00377 //! initialization code
00378 void QWidget::Init()
00379 {
00380 
00381 } // end of Init()
00382 
00383 //! clean up code
00384 void QWidget::CleanUp()
00385 {
00386 
00387 } // end of CleanUp()
00388 
00389 //! getter method for Width
00390 int QWidget::getWidth()
00391 {
00392 return Width;
00393 } // end of getWidth()
00394 
00395 //! setter method for Width
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 } // end of setWidth()
00406 
00407 //! getter method for Height
00408 int QWidget::getHeight()
00409 {
00410 return Height;
00411 } // end of getHeight()
00412 
00413 //! setter method for Height
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 } // end of setHeight()
00424 
00425 //! getter method for Top
00426 int QWidget::getTop()
00427 {
00428 return Top;
00429 } // end of getTop()
00430 
00431 //! setter method for Top
00432 void QWidget::setTop( int  newValue )
00433 {
00434  if (Top != newValue)
00435  {
00436     Top  = newValue;
00437  }
00438 } // end of setTop()
00439 
00440 //! getter method for Left
00441 int QWidget::getLeft()
00442 {
00443 return Left;
00444 } // end of getLeft()
00445 
00446 //! setter method for Left
00447 void QWidget::setLeft( int  newValue )
00448 {
00449   if (Left != newValue)
00450   {
00451     Left = newValue;
00452   }
00453 } // end of setLeft()
00454 
00455 //! getter method for MinWidth
00456 int QWidget::getMinWidth()
00457 {
00458 return MinWidth;
00459 } // end of getMinWidth()
00460 
00461 //! setter method for MinWidth
00462 void QWidget::setMinWidth( int  newValue )
00463 {
00464 MinWidth = newValue;
00465 } // end of setMinWidth()
00466 
00467 //! getter method for MinHeight
00468 int QWidget::getMinHeight()
00469 {
00470 return MinHeight;
00471 } // end of getMinHeight()
00472 
00473 //! setter method for MinHeight
00474 void QWidget::setMinHeight( int  newValue )
00475 {
00476 MinHeight = newValue;
00477 } // end of setMinHeight()
00478 
00479 //! getter method for ClientBorderLeft
00480 int QWidget::getClientBorderLeft()
00481 {
00482 return ClientBorderLeft;
00483 } // end of getClientBorderLeft()
00484 
00485 //! setter method for ClientBorderLeft
00486 void QWidget::setClientBorderLeft( int  newValue )
00487 {
00488 ClientBorderLeft = newValue;
00489 } // end of setClientBorderLeft()
00490 
00491 //! getter method for ClientBorderRight
00492 int QWidget::getClientBorderRight()
00493 {
00494 return ClientBorderRight;
00495 } // end of getClientBorderRight()
00496 
00497 //! setter method for ClientBorderRight
00498 void QWidget::setClientBorderRight( int  newValue )
00499 {
00500 ClientBorderRight = newValue;
00501 } // end of setClientBorderRight()
00502 
00503 //! getter method for ClientBorderTop
00504 int QWidget::getClientBorderTop()
00505 {
00506 return ClientBorderTop;
00507 } // end of getClientBorderTop()
00508 
00509 //! setter method for ClientBorderTop
00510 void QWidget::setClientBorderTop( int  newValue )
00511 {
00512 ClientBorderTop = newValue;
00513 } // end of setClientBorderTop()
00514 
00515 //! getter method for ClientBorderBottom
00516 int QWidget::getClientBorderBottom()
00517 {
00518 return ClientBorderBottom;
00519 } // end of getClientBorderBottom()
00520 
00521 //! setter method for ClientBorderBottom
00522 void QWidget::setClientBorderBottom( int  newValue )
00523 {
00524 ClientBorderBottom = newValue;
00525 } // end of setClientBorderBottom()
00526 
00527 //! getter method for Enabled
00528 bool QWidget::getEnabled()
00529 {
00530 return Enabled;
00531 } // end of getEnabled()
00532 
00533 //! setter method for Enabled
00534 void QWidget::setEnabled( bool  newValue )
00535 {
00536 Enabled = newValue;
00537 } // end of setEnabled()
00538 
00539 //! getter method for Visible
00540 bool QWidget::getVisible()
00541 {
00542 return Visible;
00543 } // end of getVisible()
00544 
00545 //! setter method for Visible
00546 void QWidget::setVisible( bool  newValue )
00547 {
00548 Visible = newValue;
00549 } // end of setVisible()
00550 
00551 //! getter method for LookAndFeel
00552 QLookAndFeel* QWidget::getLookAndFeel()
00553 {
00554 return LookAndFeel;
00555 } // end of getLookAndFeel()
00556 
00557 //! setter method for LookAndFeel
00558 void QWidget::setLookAndFeel( QLookAndFeel*  newValue )
00559 {
00560 LookAndFeel = newValue;
00561 } // end of setLookAndFeel()
00562 
00563 //! getter method for Owner
00564 QWidget* QWidget::getOwner()
00565 {
00566 return Owner;
00567 } // end of getOwner()
00568 
00569 //! setter method for Owner
00570 void QWidget::setOwner( QWidget*  newValue )
00571 {
00572 Owner = newValue;
00573 } // end of setOwner()
00574 
00575 //! getter method for FocusPath
00576 QWidget* QWidget::getFocusPath()
00577 {
00578 return FocusPath;
00579 } // end of getFocusPath()
00580 
00581 //! setter method for FocusPath
00582 void QWidget::setFocusPath( QWidget*  newValue )
00583 {
00584   FocusPath = newValue;
00585 } // end of setFocusPath()
00586 
00587 // WARNING : The implementation of this method will be automatically generated with code generator.
00588 // To prevent this add '//KEEP' at the first line of the implementation.(first line after '{')
00589 //! prints itself to the cout
00590 void QWidget::print()
00591 {
00592 //KEEP
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 } // end of print()
00603 
00604 // WARNING : The implementation of this method will be automatically generated with code generator.
00605 // To prevent this add '//KEEP' at the first line of the implementation.(first line after '{')
00606 //! Constructor
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 } // end of QWidget()
00637 
00638 // WARNING : The implementation of this method will be automatically generated with code generator.
00639 // To prevent this add '//KEEP' at the first line of the implementation.(first line after '{')
00640 //! Destructor
00641  QWidget::~QWidget()
00642 {
00643 freeChildren();
00644 CleanUp();
00645 } // end of ~QWidget()
00646 
00647 // end of implementation of QWidget
00648 
00649 

Generated on Thu Mar 18 18:33:48 2004 for miniQT by doxygen1.2.18