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

/cygdrive/d/Eigene Dateien/!DProcs/code_gen/src/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 
00009 
00010 #include <iostream>
00011 using namespace std;
00012 
00013 
00014 // implementation of QWidget
00015 
00016 //! adds given widget to the children
00017 void QWidget::add( QWidget*  widget )
00018 {
00019   Children.push_front(widget);
00020   childrenChanged();
00021 } // end of add()
00022 
00023 //! removes given widget from the children
00024 void QWidget::remove( QWidget*  widget )
00025 {
00026   if (FocusPath == widget) FocusPath = 0;
00027   Children.remove(widget);
00028   childrenChanged();
00029 } // end of remove()
00030 
00031 //! sends request for freeing a widget to parent
00032 void QWidget::freeWidget( QWidget*  widget )
00033 {
00034   if (Owner) Owner->freeWidget(widget);
00035 } // end of freeWidget()
00036 
00037 //! clears the focus path by setting it to 0 recursively
00038 void QWidget::clearFocusPath()
00039 {
00040   if (FocusPath != 0)
00041    {
00042       FocusPath->clearFocusPath();
00043       FocusPath = 0;
00044    }
00045 } // end of clearFocusPath()
00046 
00047 //! returns true if widget has keyboard focus
00048 bool QWidget::getFocused()
00049 {
00050   return (Owner->getFocusPath() == this);
00051 } // end of getFocused()
00052 
00053 //! frees the widget and all resources. This method must be used insted of explicit delete
00054 void QWidget::free()
00055 {
00056   if (Owner) Owner->freeWidget(this);
00057 } // end of free()
00058 
00059 //! primary event receiver method. Returns true, if event was handled. In this case the event won't be passed to next child
00060 bool QWidget::event( QEvent*  e )
00061 {
00062 
00063   // if widget is not visible there is no event handling
00064   if (!Visible) return false;
00065 
00066   // check for draw event first
00067   if (e->getType() == etDraw)
00068    {
00069      changeContext();
00070      draw(e);
00071      if (onCustomDraw!=0) (*onCustomDraw)(this,e);
00072 
00073      /*
00074   glColorMask(0,0,0,0);
00075   glEnable(GL_STENCIL_TEST);
00076   glStencilFunc(GL_ALWAYS,1,1);
00077   glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
00078   glBegin(GL_QUADS);
00079   LookAndFeel->drawRect(0,0, Width, Height);
00080   glEnd();
00081   glColorMask(1,1,1,1);
00082   glStencilFunc(GL_EQUAL,1,1);
00083   glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
00084   glDisable(GL_STENCIL_TEST);
00085   */
00086 
00087   eventToChildrenReversed(e);
00088 
00089 
00090      restoreContext();
00091      return false;
00092   }
00093 
00094   // if we are here, we have mouse or keyboard event
00095 
00096   // if widget is disabled, no mouse or keyboard events are handled
00097   if (!Enabled) return false;
00098 
00099   // check for keyboard events
00100   if  ((e->getType() == etKeyboard) ||
00101        (e->getType() == etSpecialKey )  ||
00102        (e->getType() == etKeyboardUp ))
00103   {
00104      return handleKeyboardEvent(e);
00105   }
00106 
00107    // if we are here we have mouse event
00108 
00109    return handleMouseEvent(e);
00110 
00111 } // end of event()
00112 
00113 //! post request for redisplay
00114 void QWidget::redraw()
00115 {
00116   Owner->redraw();
00117 } // end of redraw()
00118 
00119 //! sends the event to each child. Returns true if event was handled
00120 bool QWidget::eventToChildren( QEvent*  e )
00121 {
00122   // Create constant iterator for list.
00123   list<QWidget*>::const_iterator iter;
00124   // Iterate through list
00125   for (iter=Children.begin(); iter != Children.end(); iter++)
00126   {
00127     if ((*iter)->event(e)) return true;
00128   }
00129   return false;
00130 } // end of eventToChildren()
00131 
00132 //! sends the event to each child in reversed order. Returns true if event was handled
00133 bool QWidget::eventToChildrenReversed( QEvent*  e )
00134 {
00135  // Create constant iterator for list.
00136   list<QWidget*>::reverse_iterator iter;
00137   // Iterate through list
00138   for (iter=Children.rbegin(); iter != Children.rend(); iter++)
00139   {
00140     if ((*iter)->event(e)) return true;
00141   }
00142   return false;
00143 } // end of eventToChildrenReversed()
00144 
00145 //! brings the given widget to the front. Must be a child widget
00146 void QWidget::bringToFront( QWidget*  widget )
00147 {
00148   unsigned int size = Children.size();
00149   Children.remove(widget);
00150   if ((Children.size()+1)== size) Children.push_front(widget);
00151 } // end of bringToFront()
00152 
00153 //! Returns true if a given widget is in front
00154 bool QWidget::isInFront( QWidget*  widget )
00155 {
00156   return (Children.front() == widget);
00157 } // end of isInFront()
00158 
00159 //! handles the keyboard event
00160 bool QWidget::handleKeyboardEvent( QEvent*  e )
00161 {
00162  processEvent(e);
00163  if (onKey!=0)  (*onKey)(this,e);
00164  if (FocusPath) FocusPath->event(e);
00165  return true;
00166 } // end of handleKeyboardEvent()
00167 
00168 //! handles the mouse event for this widget
00169 bool QWidget::handleMouseEvent( QEvent*  e )
00170 {
00171 
00172     // handle dragging
00173     if ((Dragging) && (e->getType() == etMouseMotion))
00174     {
00175       handleDragging(e);
00176       return true;
00177     }
00178 
00179       // save mouse coordinates for later restore
00180     int saved_x = e->getX();
00181     int saved_y = e->getY();
00182 
00183     // x and y are mouse coordinates relative to this widget
00184     int x = saved_x - Left;
00185     int y = saved_y - Top;
00186 
00187 
00188     //defines whether mouse coordinates are in this widget
00189     bool ourRegion = ((x>=0) && (y>=0) && (x<=Width) && (y<=Height));
00190 
00191     if (ourRegion && (e->getType() == etMouseDown))
00192     {
00193       doFocus(e);
00194     }
00195 
00196     // adjust mouse coordinates to be relative to this widget
00197     e->setX(x);
00198     e->setY(y);
00199 
00200 
00201     // pass mouse event to the children
00202     // handledByChildren is true if one of the
00203     bool handledByChildren = eventToChildren(e);
00204 
00205     // set mouse coordinates back to the saved values
00206     e->setX(saved_x);
00207     e->setY(saved_y);
00208 
00209     // return true if mouse event was handled in children widgets
00210     if (handledByChildren) return true;
00211 
00212     // So, the event was not handled by any child, see
00213     // what we can do with it.
00214 
00215     if ((Dragging) && (e->getType() == etMouseUp)) Dragging = false;
00216 
00217     // process mouse events in derived classes with owners coordinates
00218     processMouseOwner(e);
00219 
00220     // so we are not in dragging mode right now
00221     // check for entering dragging mode and other mouse events
00222 
00223     if  (ourRegion)
00224     {
00225        // processEvent in derived classes if we are in widgets region
00226        processEvent(e);
00227 
00228        // check for dragging and call custom event handlers
00229         switch (e->getType())
00230         {
00231           case etMouseClick:
00232             if (onClick!=0)     (*onClick)(this,e);
00233           break;
00234 
00235           case etMouseDown:
00236             if (canDrag(e))
00237             {
00238               Dragging = true;
00239               DragStartX = x;
00240               DragStartY = y;
00241             }
00242             if (onMouseDown!=0) (*onMouseDown)(this,e);
00243           break;
00244 
00245           case etMouseUp:
00246 
00247             if (onMouseUp!=0)   (*onMouseUp)(this,e);
00248           break;
00249 
00250           case etMouseMotion:
00251              if (onMouseMove!=0)   (*onMouseMove)(this,e);
00252           break;
00253 
00254           default: ;
00255         }
00256 
00257        //the event was handled in this widget, so other children should not
00258        //get it -> return true
00259        return true;
00260     }
00261 
00262 
00263   // mouse event was not in our region -> return false
00264   return false;
00265 
00266 } // end of handleMouseEvent()
00267 
00268 //! handles the dragging
00269 void QWidget::handleDragging( QEvent*  e )
00270 {
00271    // defines snap width
00272    const int snap_width = 5;
00273 
00274    int new_x = e->getX()-DragStartX;
00275    int new_y = e->getY()-DragStartY;
00276 
00277    int minx = Owner->getClientBorderLeft();
00278    int maxx = Owner->getWidth()-Width-Owner->getClientBorderRight();
00279    int miny = Owner->getClientBorderTop();
00280    int maxy = Owner->getHeight()-Height-Owner->getClientBorderBottom();
00281 
00282    if (new_x < snap_width + minx ) new_x = minx;
00283    else
00284    if (new_x > maxx - snap_width) new_x = maxx;
00285 
00286    if (new_y < snap_width + miny) new_y = miny;
00287    else
00288    if (new_y > maxy - snap_width) new_y = maxy;
00289 
00290    setLeft(new_x);
00291    setTop (new_y);
00292 
00293 } // end of handleDragging()
00294 
00295 //! tries to set focus to the current widget
00296 void QWidget::doFocus( QEvent*  e )
00297 {
00298   if (Owner) Owner->bringToFront(this);
00299   if ((canFocus(e)) && (Owner!=0))
00300    {
00301      Owner->requestFocus(this,this);
00302    }
00303 } // end of doFocus()
00304 
00305 //! called if Sender wants to announce new focused control
00306 void QWidget::requestFocus( QWidget*  Sender , QWidget*  FocusedWidget )
00307 {
00308   if ((FocusPath != 0) && (FocusPath != Sender))
00309    {
00310       FocusPath->clearFocusPath();
00311    }
00312   FocusPath = Sender;
00313   Owner->requestFocus(this,FocusedWidget);
00314 } // end of requestFocus()
00315 
00316 //! changes the global drawing context
00317 void QWidget::changeContext()
00318 {
00319   glPushMatrix();
00320   glTranslatef(Left,Top,0.0);
00321 } // end of changeContext()
00322 
00323 //! restores the global drawing context
00324 void QWidget::restoreContext()
00325 {
00326   glPopMatrix();
00327 } // end of restoreContext()
00328 
00329 //! calls delete for every child widget
00330 void QWidget::freeChildren()
00331 {
00332   while (Children.size() > 0)
00333   {
00334     delete Children.front();
00335     Children.pop_front();
00336   }
00337 } // end of freeChildren()
00338 
00339 //! draws itself with OpenGL. Here is the OpenGL drawing code
00340 void QWidget::draw( QEvent*  e )
00341 {
00342 
00343 } // end of draw()
00344 
00345 //! layout the children widgets. This method is called every time the size is changed. Have to be overwritten for respond on size changes
00346 void QWidget::layout()
00347 {
00348 
00349 } // end of layout()
00350 
00351 //! 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
00352 bool QWidget::canDrag( QEvent*  e )
00353 {
00354  return false;
00355 } // end of canDrag()
00356 
00357 //! processing mouse/keyboard events which are in widgets region. Mouse coordinates are relative to Owner
00358 bool QWidget::processEvent( QEvent*  e )
00359 {
00360  return false;
00361 } // end of processEvent()
00362 
00363 //! processing mouse events which are in Owner widget region. Mouse coordinates are relative to Owner
00364 bool QWidget::processMouseOwner( QEvent*  e )
00365 {
00366  return false;
00367 } // end of processMouseOwner()
00368 
00369 //! called if a child is added/removed
00370 void QWidget::childrenChanged()
00371 {
00372 } // end of childrenChanged()
00373 
00374 //! defines whether control can have keyboard focus. Should return true if Widget wants to get keyboard focus
00375 bool QWidget::canFocus( QEvent*  e )
00376 {
00377  return false;
00378 } // end of canFocus()
00379 
00380 //! initialization code
00381 void QWidget::Init()
00382 {
00383 
00384 } // end of Init()
00385 
00386 //! clean up code
00387 void QWidget::CleanUp()
00388 {
00389 
00390 } // end of CleanUp()
00391 
00392 //! getter method for Width
00393 int QWidget::getWidth()
00394 {
00395 return Width;
00396 } // end of getWidth()
00397 
00398 //! setter method for Width
00399 void QWidget::setWidth( int  newValue )
00400 {
00401  if (newValue < MinWidth) newValue = MinWidth;
00402  if (Width != newValue)
00403   {
00404     Width = newValue;
00405     layout();
00406     if (onResize!=0)   (*onResize)(this);
00407   }
00408 } // end of setWidth()
00409 
00410 //! getter method for Height
00411 int QWidget::getHeight()
00412 {
00413 return Height;
00414 } // end of getHeight()
00415 
00416 //! setter method for Height
00417 void QWidget::setHeight( int  newValue )
00418 {
00419   if (newValue < MinHeight) newValue = MinHeight;
00420   if (Height != newValue)
00421   {
00422     Height = newValue;
00423     layout();
00424     if (onResize!=0)   (*onResize)(this);
00425   }
00426 } // end of setHeight()
00427 
00428 //! getter method for Top
00429 int QWidget::getTop()
00430 {
00431 return Top;
00432 } // end of getTop()
00433 
00434 //! setter method for Top
00435 void QWidget::setTop( int  newValue )
00436 {
00437  if (Top != newValue)
00438  {
00439     Top  = newValue;
00440  }
00441 } // end of setTop()
00442 
00443 //! getter method for Left
00444 int QWidget::getLeft()
00445 {
00446 return Left;
00447 } // end of getLeft()
00448 
00449 //! setter method for Left
00450 void QWidget::setLeft( int  newValue )
00451 {
00452   if (Left != newValue)
00453   {
00454     Left = newValue;
00455   }
00456 } // end of setLeft()
00457 
00458 //! getter method for MinWidth
00459 int QWidget::getMinWidth()
00460 {
00461 return MinWidth;
00462 } // end of getMinWidth()
00463 
00464 //! setter method for MinWidth
00465 void QWidget::setMinWidth( int  newValue )
00466 {
00467 MinWidth = newValue;
00468 } // end of setMinWidth()
00469 
00470 //! getter method for MinHeight
00471 int QWidget::getMinHeight()
00472 {
00473 return MinHeight;
00474 } // end of getMinHeight()
00475 
00476 //! setter method for MinHeight
00477 void QWidget::setMinHeight( int  newValue )
00478 {
00479 MinHeight = newValue;
00480 } // end of setMinHeight()
00481 
00482 //! getter method for ClientBorderLeft
00483 int QWidget::getClientBorderLeft()
00484 {
00485 return ClientBorderLeft;
00486 } // end of getClientBorderLeft()
00487 
00488 //! setter method for ClientBorderLeft
00489 void QWidget::setClientBorderLeft( int  newValue )
00490 {
00491 ClientBorderLeft = newValue;
00492 } // end of setClientBorderLeft()
00493 
00494 //! getter method for ClientBorderRight
00495 int QWidget::getClientBorderRight()
00496 {
00497 return ClientBorderRight;
00498 } // end of getClientBorderRight()
00499 
00500 //! setter method for ClientBorderRight
00501 void QWidget::setClientBorderRight( int  newValue )
00502 {
00503 ClientBorderRight = newValue;
00504 } // end of setClientBorderRight()
00505 
00506 //! getter method for ClientBorderTop
00507 int QWidget::getClientBorderTop()
00508 {
00509 return ClientBorderTop;
00510 } // end of getClientBorderTop()
00511 
00512 //! setter method for ClientBorderTop
00513 void QWidget::setClientBorderTop( int  newValue )
00514 {
00515 ClientBorderTop = newValue;
00516 } // end of setClientBorderTop()
00517 
00518 //! getter method for ClientBorderBottom
00519 int QWidget::getClientBorderBottom()
00520 {
00521 return ClientBorderBottom;
00522 } // end of getClientBorderBottom()
00523 
00524 //! setter method for ClientBorderBottom
00525 void QWidget::setClientBorderBottom( int  newValue )
00526 {
00527 ClientBorderBottom = newValue;
00528 } // end of setClientBorderBottom()
00529 
00530 //! getter method for Enabled
00531 bool QWidget::getEnabled()
00532 {
00533 return Enabled;
00534 } // end of getEnabled()
00535 
00536 //! setter method for Enabled
00537 void QWidget::setEnabled( bool  newValue )
00538 {
00539 Enabled = newValue;
00540 } // end of setEnabled()
00541 
00542 //! getter method for Visible
00543 bool QWidget::getVisible()
00544 {
00545 return Visible;
00546 } // end of getVisible()
00547 
00548 //! setter method for Visible
00549 void QWidget::setVisible( bool  newValue )
00550 {
00551 Visible = newValue;
00552 } // end of setVisible()
00553 
00554 //! getter method for LookAndFeel
00555 QLookAndFeel* QWidget::getLookAndFeel()
00556 {
00557 return LookAndFeel;
00558 } // end of getLookAndFeel()
00559 
00560 //! setter method for LookAndFeel
00561 void QWidget::setLookAndFeel( QLookAndFeel*  newValue )
00562 {
00563 LookAndFeel = newValue;
00564 } // end of setLookAndFeel()
00565 
00566 //! getter method for Owner
00567 QWidget* QWidget::getOwner()
00568 {
00569 return Owner;
00570 } // end of getOwner()
00571 
00572 //! setter method for Owner
00573 void QWidget::setOwner( QWidget*  newValue )
00574 {
00575 Owner = newValue;
00576 } // end of setOwner()
00577 
00578 //! getter method for FocusPath
00579 QWidget* QWidget::getFocusPath()
00580 {
00581 return FocusPath;
00582 } // end of getFocusPath()
00583 
00584 //! setter method for FocusPath
00585 void QWidget::setFocusPath( QWidget*  newValue )
00586 {
00587   FocusPath = newValue;
00588 } // end of setFocusPath()
00589 
00590 // WARNING : The implementation of this method will be automatically generated with code generator.
00591 // To prevent this add '//KEEP' at the first line of the implementation.(first line after '{')
00592 //! prints itself to the cout
00593 void QWidget::print()
00594 {
00595 //KEEP
00596 cout << "Class : QWidget" << endl;
00597 cout << "Width = " << Width << endl;
00598 cout << "Height = " << Height << endl;
00599 cout << "Top = " << Top << endl;
00600 cout << "Left = " << Left << endl;
00601 cout << "Enabled = " << Enabled << endl;
00602 cout << "Visible = " << Visible << endl;
00603 cout << "Owner = " << Owner << endl;
00604 cout << "Children = " << Children.size() << endl;
00605 } // end of print()
00606 
00607 // WARNING : The implementation of this method will be automatically generated with code generator.
00608 // To prevent this add '//KEEP' at the first line of the implementation.(first line after '{')
00609 //! Constructor
00610  QWidget::QWidget( QWidget*  aOwner )
00611 {
00612 onResize = 0;
00613 onCustomDraw = 0;
00614 onKey = 0;
00615 onMouseMove = 0;
00616 onMouseUp = 0;
00617 onMouseDown = 0;
00618 onClick = 0;
00619 DragStartX = 0;
00620 DragStartY = 0;
00621 Width = 20;
00622 Height = 10;
00623 Top = 0;if (aOwner) Top = aOwner->getClientBorderTop();
00624 Left = 0;if (aOwner) Left = aOwner->getClientBorderLeft();
00625 MinWidth = 0;
00626 MinHeight = 0;
00627 ClientBorderLeft = 0;
00628 ClientBorderRight = 0;
00629 ClientBorderTop = 0;
00630 ClientBorderBottom = 0;
00631 Enabled = true;
00632 Visible = true;
00633 Dragging = false;
00634 LookAndFeel = 0;if (aOwner) LookAndFeel = aOwner->getLookAndFeel();
00635 Owner = aOwner;if (aOwner) aOwner->add(this);
00636 FocusPath = 0;
00637 Init();
00638 layout();
00639 } // end of QWidget()
00640 
00641 // WARNING : The implementation of this method will be automatically generated with code generator.
00642 // To prevent this add '//KEEP' at the first line of the implementation.(first line after '{')
00643 //! Destructor
00644  QWidget::~QWidget()
00645 {
00646 //KEEP
00647 freeChildren();
00648 CleanUp();
00649 } // end of ~QWidget()
00650 
00651 // end of implementation of QWidget
00652 
00653 

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