Salome HOME
Split Zones functionality (draft version).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_SplitZonesTool.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_SplitZonesTool.h"
24
25 #include <HYDROData_Zone.h>
26
27 HYDROGUI_SplitZonesTool::SplitDataList
28 HYDROGUI_SplitZonesTool::SplitZones( const HYDROData_SequenceOfObjects& theZoneList )
29 {
30   SplitDataList anOutputSplitDataList;
31
32   SplitDataList anInputSplitDataList;
33   for( int anIndex = 1, aLength = theZoneList.Length(); anIndex <= aLength; anIndex++ )
34   {
35     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( anIndex ) );
36     if( !aZone.IsNull() )
37     {
38       SplitData aSplitData( aZone->GetPainterPath(), aZone->GetName() );
39       anInputSplitDataList.append( aSplitData );
40     }
41   }
42
43   SplitDataListIterator anInputIter( anInputSplitDataList );
44   while( anInputIter.hasNext() )
45   {
46     const SplitData& anInputSplitData = anInputIter.next();
47     if( anOutputSplitDataList.isEmpty() )
48       anOutputSplitDataList.append( anInputSplitData );
49     else
50     {
51       SplitDataList aSplitDataList;
52       SplitDataListIterator anOutputIter( anOutputSplitDataList );
53       while( anOutputIter.hasNext() )
54       {
55         const SplitData& anOutputSplitData = anOutputIter.next();
56
57         SplitDataList aList = SplitTwoData( anOutputSplitData, anInputSplitData );
58         aSplitDataList.append( aList );
59       }
60       anOutputSplitDataList = aSplitDataList;
61     }
62   }
63
64   /*
65   if( theZoneList.Length() != 2 )
66     return aSplitDataList;
67
68   Handle(HYDROData_Zone) aZone1 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 1 ) );
69   Handle(HYDROData_Zone) aZone2 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 2 ) );
70
71   SplitData aSplitData1( aZone1->GetPainterPath(), aZone1->GetName() );
72   SplitData aSplitData2( aZone2->GetPainterPath(), aZone2->GetName() );
73
74   aSplitDataList = SplitTwoData( aSplitData1, aSplitData2 );
75   */
76
77   return anOutputSplitDataList;
78 }
79
80 HYDROGUI_SplitZonesTool::SplitDataList
81 HYDROGUI_SplitZonesTool::SplitTwoData( const SplitData& theData1,
82                                        const SplitData& theData2 )
83 {
84   SplitDataList aSplitDataList;
85
86   const QPainterPath& aPath1 = theData1.Path;
87   const QPainterPath& aPath2 = theData2.Path;
88
89   const QStringList& aZoneNames1 = theData1.ZoneNames;
90   const QStringList& aZoneNames2 = theData2.ZoneNames;
91
92   QPainterPath anIntersection = aPath1.intersected( aPath2 );
93   if( anIntersection.isEmpty() )
94   {
95     aSplitDataList.append( theData1 );
96     aSplitDataList.append( theData2 );
97   }
98   else
99   {
100     aSplitDataList.append( SplitData( anIntersection, aZoneNames1 + aZoneNames2 ) );
101
102     QPainterPath aPath1Sub = aPath1.subtracted( aPath2 );
103     if( !aPath1Sub.isEmpty() )
104       aSplitDataList.append( ExtractSeparateData( SplitData( aPath1Sub, aZoneNames1 ) ) );
105
106     QPainterPath aPath2Sub = aPath2.subtracted( aPath1 );
107     if( !aPath2Sub.isEmpty() )
108       aSplitDataList.append( ExtractSeparateData( SplitData( aPath2Sub, aZoneNames2 ) ) );
109   }
110   return aSplitDataList;
111 }
112
113 HYDROGUI_SplitZonesTool::SplitDataList
114 HYDROGUI_SplitZonesTool::ExtractSeparateData( const SplitData& theData )
115 {
116   SplitDataList aSplitDataList;
117
118   const QPainterPath& aBasePath = theData.Path;
119   const QStringList& aBaseZoneNames = theData.ZoneNames;
120
121   QList<QPainterPath> aPathList;
122   for( int i = 0, n = aBasePath.elementCount(); i < n; i++ )
123   {
124     const QPainterPath::Element anElement = aBasePath.elementAt( i );
125     switch( anElement.type )
126     {
127       case QPainterPath::MoveToElement:
128         aPathList.append( QPainterPath( QPointF( anElement.x, anElement.y ) ) );
129         break;
130       case QPainterPath::LineToElement:
131         if( !aPathList.isEmpty() )
132         {
133           QPainterPath& aPath = aPathList.last();
134           aPath.lineTo( anElement.x, anElement.y );
135         }
136         break;
137       case QPainterPath::CurveToElement: // currently not supported
138       default:
139         break;
140     }
141   }
142
143   if( aPathList.size() == 2 )
144   {
145     const QPainterPath& aPath1 = aPathList.first();
146     const QPainterPath& aPath2 = aPathList.last();
147     if( aPath1.contains( aPath2 ) || aPath2.contains( aPath1 ) )
148     {
149       aSplitDataList.append( theData );
150       return aSplitDataList;
151     }
152   }
153
154   QListIterator<QPainterPath> anIter( aPathList );
155   while( anIter.hasNext() )
156   {
157     const QPainterPath& aPath = anIter.next();
158     aSplitDataList.append( SplitData( aPath, aBaseZoneNames ) );
159   }
160   return aSplitDataList;
161 }