]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNodesEditor.cxx
9669eea49704045325fb3dddb2dcdfa9f49b86fb
[cpPlugins.git] / appli / cpPipelineEditor / QNodesEditor.cxx
1 /* Copyright (c) 2012, STANISLAW ADASZEWSKI\r
2    All rights reserved.\r
3 \r
4    Redistribution and use in source and binary forms, with or without\r
5    modification, are permitted provided that the following conditions are met:\r
6    * Redistributions of source code must retain the above copyright\r
7    notice, this list of conditions and the following disclaimer.\r
8    * Redistributions in binary form must reproduce the above copyright\r
9    notice, this list of conditions and the following disclaimer in the\r
10    documentation and/or other materials provided with the distribution.\r
11    * Neither the name of STANISLAW ADASZEWSKI nor the\r
12    names of its contributors may be used to endorse or promote products\r
13    derived from this software without specific prior written permission.\r
14 \r
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
16    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
17    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
18    DISCLAIMED. IN NO EVENT SHALL STANISLAW ADASZEWSKI BE LIABLE FOR ANY\r
19    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
20    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
21    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
22    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
23    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
24    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
25 */\r
26 \r
27 #include "QNodesEditor.h"\r
28 \r
29 #include <QGraphicsScene>\r
30 #include <QEvent>\r
31 #include <QGraphicsSceneMouseEvent>\r
32 \r
33 #include "QNEPort.h"\r
34 #include "QNEConnection.h"\r
35 #include "QNEBlock.h"\r
36 \r
37 // -------------------------------------------------------------------------\r
38 PipelineEditor::QNodesEditor::\r
39 QNodesEditor( QObject* parent )\r
40  : Superclass( parent )\r
41 {\r
42   this->m_Conn = NULL;\r
43 }\r
44 \r
45 // -------------------------------------------------------------------------\r
46 PipelineEditor::QNodesEditor::\r
47 ~QNodesEditor( )\r
48 {\r
49 }\r
50 \r
51 // -------------------------------------------------------------------------\r
52 void PipelineEditor::QNodesEditor::\r
53 install( QGraphicsScene* s )\r
54 {\r
55   s->installEventFilter( this );\r
56   this->m_Scene = s;\r
57 }\r
58 \r
59 // -------------------------------------------------------------------------\r
60 QGraphicsItem* PipelineEditor::QNodesEditor::\r
61 itemAt( const QPointF& pos )\r
62 {\r
63   QList< QGraphicsItem* > items =\r
64     this->m_Scene->items( QRectF( pos - QPointF( 1, 1 ), QSize( 3, 3 ) ) );\r
65 \r
66   foreach( QGraphicsItem* item, items )\r
67     if( item->type( ) > QGraphicsItem::UserType )\r
68       return( item );\r
69   return( NULL );\r
70 }\r
71 \r
72 // -------------------------------------------------------------------------\r
73 bool PipelineEditor::QNodesEditor::\r
74 eventFilter( QObject* o, QEvent* e )\r
75 {\r
76   QGraphicsSceneMouseEvent* me = ( QGraphicsSceneMouseEvent* ) e;\r
77 \r
78   switch ( ( int ) e->type( ) )\r
79   {\r
80   case QEvent::GraphicsThis->M_SceneMousePress:\r
81   {\r
82     switch ( ( int ) me->button( ) )\r
83     {\r
84     case Qt::LeftButton:\r
85     {\r
86       QGraphicsItem* item = this->itemAt( me->this->m_ScenePos( ) );\r
87       if( item && item->type( ) == QNEPort::Type )\r
88       {\r
89         this->m_Conn = new QNEConnection( 0, this->m_Scene );\r
90         this->m_Conn->setPort1( ( QNEPort* ) item );\r
91         this->m_Conn->setPos1( item->this->m_ScenePos( ) );\r
92         this->m_Conn->setPos2( me->this->m_ScenePos( ) );\r
93         this->m_Conn->updatePath( );\r
94 \r
95         return( true );\r
96       }\r
97       else if( item && item->type( ) == QNEBlock::Type )\r
98       {\r
99         /* if( selBlock )\r
100            selBlock->setSelected( ); */\r
101         // selBlock = ( QNEBlock* ) item;\r
102 \r
103       } // fi\r
104       break;\r
105     }\r
106     case Qt::RightButton:\r
107     {\r
108       QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) );\r
109       if( item && ( item->type( ) == QNEConnection::Type || item->type( ) == QNEBlock::Type ) )\r
110         delete item;\r
111       // if( selBlock == ( QNEBlock* ) item )\r
112       // selBlock = 0;\r
113       break;\r
114     }\r
115     }\r
116   }\r
117   case QEvent::GraphicsThis->M_SceneMouseMove:\r
118   {\r
119     if( this->m_Conn )\r
120     {\r
121       this->m_Conn->setPos2( me->this->m_ScenePos( ) );\r
122       this->m_Conn->updatePath( );\r
123       return( true );\r
124     }\r
125     break;\r
126   }\r
127   case QEvent::GraphicsThis->M_SceneMouseRelease:\r
128   {\r
129     if( this->m_Conn && me->button( ) == Qt::LeftButton )\r
130     {\r
131       QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) );\r
132       if( item && item->type( ) == QNEPort::Type )\r
133       {\r
134         QNEPort* port1 = this->m_Conn->port1( );\r
135         QNEPort* port2 = ( QNEPort* ) item;\r
136 \r
137         if( port1->block( ) != port2->block( ) && port1->isOutput( ) != port2->isOutput( ) && !port1->isThis->M_Connected( port2 ) )\r
138         {\r
139           this->m_Conn->setPos2( port2->this->m_ScenePos( ) );\r
140           this->m_Conn->setPort2( port2 );\r
141           this->m_Conn->updatePath( );\r
142           this->m_Conn = NULL;\r
143           return( true );\r
144         }\r
145       }\r
146 \r
147       delete this->m_Conn;\r
148       this->m_Conn = NULL;\r
149       return( true );\r
150     }\r
151     break;\r
152   }\r
153   }\r
154   return( this->Superclass::eventFilter( o, e ) );\r
155 }\r
156 \r
157 // -------------------------------------------------------------------------\r
158 void PipelineEditor::QNodesEditor::\r
159 save( QDataStream& ds )\r
160 {\r
161   foreach( QGraphicsItem* item, this->m_Scene->items( ) )\r
162     if( item->type( ) == QNEBlock::Type )\r
163     {\r
164       ds << item->type( );\r
165       ( ( QNEBlock* ) item )->save( ds );\r
166     }\r
167 \r
168   foreach( QGraphicsItem* item, this->m_Scene->items( ) )\r
169     if( item->type( ) == QNEConnection::Type )\r
170     {\r
171       ds << item->type( );\r
172       ( ( QNEConnection* ) item )->save( ds );\r
173     }\r
174 }\r
175 \r
176 // -------------------------------------------------------------------------\r
177 void PipelineEditor::QNodesEditor::\r
178 load( QDataStream& ds )\r
179 {\r
180   this->m_Scene->clear( );\r
181 \r
182   QMap<quint64, QNEPort*> portMap;\r
183 \r
184   while ( !ds.atEnd( ) )\r
185   {\r
186     int type;\r
187     ds >> type;\r
188     if( type == QNEBlock::Type )\r
189     {\r
190       QNEBlock* block = new QNEBlock( 0, this->m_Scene );\r
191       block->load( ds, portMap );\r
192     } else if( type == QNEConnection::Type )\r
193     {\r
194       QNEConnection* this->m_Conn = new QNEConnection( 0, this->m_Scene );\r
195       this->m_Conn->load( ds, portMap );\r
196     }\r
197   }\r
198 }\r
199 \r
200 // eof - $RCSfile$\r