00001 00002 #include <QDebug> 00003 00004 #include "engines_area.h" 00005 00006 EnginesArea::EnginesArea( QWidget *parent ) : QScrollArea( parent ) 00007 { 00008 setAcceptDrops( TRUE ) ; 00009 setWidgetResizable( TRUE ) ; 00010 00011 _name = "" ; 00012 _base = new QWidget( ) ; 00013 _layout = new QVBoxLayout() ; 00014 _layout->setAlignment( Qt::AlignHCenter ) ; 00015 00016 _base->setLayout( _layout ) ; 00017 setWidget( _base ) ; 00018 } 00019 00020 EnginesArea::~EnginesArea() 00021 { 00022 } 00023 00024 void EnginesArea::setName( const QString &name ) 00025 { 00026 _name = name ; 00027 } 00028 00029 QString EnginesArea::name() 00030 { 00031 return _name ; 00032 } 00033 00034 void EnginesArea::insertWidget( QWidget* engine, int index ) 00035 { 00036 engine->setAttribute( Qt::WA_DeleteOnClose ); // For drag and drop 00037 engine->show(); // ensure that the engine is visible after a drag and drop 00038 00039 _layout->insertWidget( index , engine ); 00040 } 00041 00042 // Drag and drop 00043 00044 void EnginesArea::dragEnterEvent( QDragEnterEvent *event ) 00045 { 00046 if ( event->mimeData()->hasFormat( OrKEngine::MimeType ) && event->source() != 0 ) 00047 // event source is 0 if the source is not in this application 00048 { 00049 event->setDropAction( Qt::MoveAction ); 00050 event->accept(); 00051 } 00052 else 00053 event->ignore(); 00054 } 00055 00056 void EnginesArea::dropEvent( QDropEvent *event ) 00057 { 00058 if ( event->mimeData()->hasFormat( OrKEngine::MimeType ) && event->source() != 0 ) 00059 { 00060 int index = engineIndexAt( event->pos() ) ; // we get -1 if there is no engine at this point 00061 00062 if ( _layout->count() != 0 && index == -1 ) 00063 { 00064 event->ignore(); 00065 return ; 00066 } 00067 event->setDropAction(Qt::MoveAction); 00068 event->accept(); 00069 00070 insertWidget( event->source() , index ) ; 00071 emit engineDropped() ; // FIXME should no be called if the engine return to its initial positon 00072 } 00073 else 00074 event->ignore(); 00075 } 00076 00077 void EnginesArea::dragMoveEvent( QDragMoveEvent *event ) 00078 { 00079 if ( event->mimeData()->hasFormat( OrKEngine::MimeType ) && event->source() != 0 ) 00080 { 00081 event->setDropAction( Qt::MoveAction ); 00082 event->accept(); 00083 } 00084 else 00085 event->ignore(); 00086 } 00087 00088 00089 QWidget* EnginesArea::engineAt( const QPoint &p ) 00090 { 00091 // return a 0 pointer if there is no engine at point *p 00092 QWidget *engine = 0 ; 00093 int index = engineIndexAt( p ) ; 00094 if ( index != -1 ) 00095 engine = _layout->itemAt(index)->widget() ; 00096 00097 return engine ; 00098 } 00099 00100 int EnginesArea::engineIndexAt( const QPoint &p ) 00101 { 00102 // return -1 if there is no engine at point *p 00103 int index = -1 ; 00104 00105 QList<Engine *> children = _base->findChildren<Engine *>(); 00106 Engine* engine ; 00107 00108 for (int i = 0; i < children.size(); ++i) 00109 { 00110 engine = children.at(i) ; 00111 if ( engine->frameGeometry().contains( p ) ) 00112 break ; 00113 } 00114 00115 if ( engine != 0 ) 00116 index = _layout->indexOf( engine ) ; 00117 00118 return index ; 00119 } 00120 00121 int EnginesArea::count() const 00122 { 00123 return _layout->count() ; 00124 } 00125 00126 QLayoutItem* EnginesArea::itemAt( int index ) const 00127 { 00128 return _layout->itemAt( index ) ; 00129 } 00130 00131 void EnginesArea::hideEvent ( QHideEvent * event ) 00132 { 00133 emit areaClosed() ; 00134 emit areaSetVisible( FALSE ) ; 00135 event->accept() ; 00136 } 00137 00138 void EnginesArea::showEvent( QShowEvent *event ) 00139 { 00140 emit areaSetVisible( TRUE ) ; 00141 event->accept() ; 00142 } 00143 00144 void EnginesArea::closeEvent( QCloseEvent *event ) 00145 { 00146 emit areaClosed() ; 00147 hide() ; 00148 event->ignore() ; 00149 }