ciwidgetmgr.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "cnitem.h"
00012 #include "canvasitemparts.h"
00013 #include "eventinfo.h"
00014 
00015 #include <kdebug.h>
00016 
00017 CIWidgetMgr::CIWidgetMgr( QCanvas *canvas, CNItem *item )
00018 {
00019         p_cnItem = item;
00020         p_canvas = canvas;
00021 }
00022 
00023 CIWidgetMgr::~CIWidgetMgr()
00024 {
00025         // QCanvas deletes our items for us. Actually, it pretty much insists on deleting them,
00026         // despite me telling it not to, so if I delete them then it gets confused and crashes.
00027         // Naughty QCanvas!
00028         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00029         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00030         {
00031                 delete it.data();
00032         }
00033         m_widgetMap.clear();
00034 }
00035 
00036 void CIWidgetMgr::setWidgetsPos( const QPoint &pos )
00037 {
00038         m_pos = pos;
00039 }
00040 
00041 void CIWidgetMgr::setDrawWidgets( bool draw )
00042 {
00043         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00044         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00045         {
00046                 draw ? it.data()->show() : it.data()->hide();
00047         }
00048 }
00049 
00050 Widget *CIWidgetMgr::widgetWithID( const QString &id ) const
00051 {
00052         WidgetMap::const_iterator it = m_widgetMap.find(id);
00053         if(it == m_widgetMap.end() ) return 0;
00054         else return it.data();
00055 }
00056 
00057 Button *CIWidgetMgr::button( const QString &id ) const
00058 {
00059         return dynamic_cast<Button*>(widgetWithID(id));
00060 }
00061 
00062 Slider *CIWidgetMgr::slider( const QString &id ) const
00063 {
00064         return dynamic_cast<Slider*>(widgetWithID(id));
00065 }
00066 
00067 void CIWidgetMgr::setButtonState( const QString &id, int state )
00068 {
00069         Button *b = button(id);
00070         if(!b) return;
00071         
00072         // Actually, we don't want to check to see if we are already down; this way,
00073         // we get toggle events when loading from file
00074 //      bool oldState = b->isDown();
00075 //      if ( oldState == state )
00076 //              return;
00077         
00078         b->setState(state);
00079 }
00080 
00081 void CIWidgetMgr::drawWidgets( QPainter &p )
00082 {
00083         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00084         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00085         {
00086                 it.data()->drawShape(p);
00087         }
00088 }
00089 
00090 void CIWidgetMgr::removeWidget( const QString & id )
00091 {
00092         if ( !m_widgetMap.contains(id) )
00093                 return;
00094         
00095         delete m_widgetMap[id];
00096         m_widgetMap.remove(id);
00097 }
00098 
00099 Button* CIWidgetMgr::addButton( const QString &id, const QRect & pos, const QString &display, bool toggle )
00100 {
00101         WidgetMap::iterator it;
00102         
00103         Button *button = new Button( id, p_cnItem, toggle, pos, p_canvas );
00104         (dynamic_cast<QButton*>(button->widget()))->setText(display);
00105         
00106         it = m_widgetMap.find(id);
00107 
00108         if ( it == m_widgetMap.end() )
00109         {
00110                 m_widgetMap[id] = button;
00111         } else {
00112                 kdWarning() << "CIWidgetMgr::addButton: Attempting to re-add button with same id as previous"<<endl;
00113                 delete it.data();
00114                 it.data() = button;
00115         }
00116         
00117         p_cnItem->updateAttachedPositioning();
00118         return button;
00119 }
00120 
00121 Button* CIWidgetMgr::addButton( const QString &id, const QRect & pos, QPixmap pixmap, bool toggle )
00122 {
00123         WidgetMap::iterator it;
00124         
00125         Button *button = new Button( id, p_cnItem, toggle, pos, p_canvas );
00126         button->setPixmap(pixmap);
00127         
00128         it = m_widgetMap.find(id);
00129 
00130         if ( it == m_widgetMap.end() )
00131         {
00132                 m_widgetMap[id] = button;
00133         } else {
00134                 kdWarning() << "CIWidgetMgr::addButton: Attempting to re-add button with same id as previous"<<endl;
00135                 delete it.data();
00136                 it.data() = button;
00137         }
00138         
00139         p_cnItem->updateAttachedPositioning();
00140         return button;
00141 }
00142 
00143 
00144 Slider* CIWidgetMgr::addSlider( const QString &id, int minValue, int maxValue, int pageStep, int value, Qt::Orientation orientation, const QRect & pos )
00145 {
00146         Slider *slider = new Slider( id, p_cnItem, pos, p_canvas );
00147         QSlider *qslider = dynamic_cast<QSlider*>(slider->widget());
00148         
00149         qslider->setMinValue(minValue);
00150         qslider->setMaxValue(maxValue);
00151         qslider->setPageStep(pageStep);
00152         qslider->setValue(value);
00153         slider->setOrientation(orientation);
00154         
00155         WidgetMap::iterator it = m_widgetMap.find(id);
00156 
00157         if ( it == m_widgetMap.end() ) {
00158                 m_widgetMap[id] = slider;
00159         } else {
00160                 kdWarning() << "CIWidgetMgr::addSlider: Attempting to re-add slider with same id as previous"<<endl;
00161                 delete slider;
00162                 return 0;
00163         }
00164         
00165         p_cnItem->updateAttachedPositioning();
00166         return slider;
00167 }
00168 
00169 bool CIWidgetMgr::mousePressEvent( const EventInfo &info )
00170 {
00171         QMouseEvent *e = info.mousePressEvent( 0, 0 );
00172         
00173         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00174         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00175         {
00176                 if ( it.data()->rect().contains(info.pos) ) {
00177                         it.data()->mousePressEvent(e);
00178 
00179                         if (e->isAccepted()) {
00180                                 delete e;
00181                                 return true;
00182                         }
00183                 }
00184         }
00185         delete e;
00186         return false;
00187 }
00188 
00189 bool CIWidgetMgr::mouseReleaseEvent( const EventInfo &info )
00190 {
00191         QMouseEvent *e = info.mouseReleaseEvent( 0, 0 );
00192         
00193         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00194         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00195         {
00196                 it.data()->mouseReleaseEvent(e);
00197         }
00198 
00199         bool accepted = e->isAccepted();
00200         delete e;
00201         return accepted;
00202 }
00203 
00204 bool CIWidgetMgr::mouseDoubleClickEvent( const EventInfo &info )
00205 {
00206         QMouseEvent *e = info.mouseDoubleClickEvent( 0, 0 );
00207 
00208         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00209         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00210         {
00211                 if ( it.data()->rect().contains(info.pos) )
00212                 {
00213                         it.data()->mouseDoubleClickEvent(e);
00214                         if (e->isAccepted())
00215                         {
00216                                 delete e;
00217                                 return true;
00218                         }
00219                 }
00220         }
00221         delete e;
00222         return false;
00223 }
00224 
00225 bool CIWidgetMgr::mouseMoveEvent( const EventInfo &info )
00226 {
00227         QMouseEvent *e = info.mouseMoveEvent( 0, 0 );
00228         
00229         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00230         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it ) {
00231                 it.data()->mouseMoveEvent(e);
00232                 if (e->isAccepted()) {
00233                         delete e;
00234                         return true;
00235                 }
00236         }
00237         delete e;
00238         return false;
00239 }
00240 
00241 
00242 bool CIWidgetMgr::wheelEvent(const EventInfo &info )
00243 {
00244         QWheelEvent *e = info.wheelEvent( 0, 0 );
00245         
00246         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00247         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00248         {
00249                 if ( it.data()->rect().contains(info.pos) ) {
00250                         it.data()->wheelEvent(e);
00251                         if (e->isAccepted()) {
00252                                 delete e;
00253                                 return true;
00254                         }
00255                 }
00256         }
00257 
00258         delete e;
00259         return false;
00260 }
00261 
00262 void CIWidgetMgr::enterEvent()
00263 {
00264         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00265         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00266         {
00267                 it.data()->enterEvent();
00268         }
00269 }
00270 
00271 void CIWidgetMgr::leaveEvent()
00272 {
00273         const WidgetMap::iterator widgetMapEnd = m_widgetMap.end();
00274         for ( WidgetMap::iterator it = m_widgetMap.begin(); it != widgetMapEnd; ++it )
00275         {
00276                 it.data()->leaveEvent();
00277         }
00278 }
00279 

Generated on Tue May 8 17:05:28 2007 for KTechLab by  doxygen 1.5.1