Salome HOME
a3db1329dbdaea283077f2a8a804fc1897512968
[modules/gui.git] / src / GLViewer / GLViewer_AspectLine.cxx
1 //  Copyright (C) 2007-2008  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 //  Author : OPEN CASCADE
23 // File:      GLViewer_AspectLine.cxx
24 // Created:   26/05/2005 3:17:00 PM
25 //
26 #include "GLViewer_AspectLine.h"
27
28 /*!
29   Default constructor
30 */
31 GLViewer_AspectLine::GLViewer_AspectLine()
32 {
33     myNColor = QColor( 255, 255, 255 );
34     myHColor = QColor( 0, 255, 255 );
35     mySColor = QColor( 255, 0, 0 );
36
37     myLineWidth = 1.0;
38     myLineType = 0;
39 }
40
41 /*!
42   Constructor
43   \param type: 0 for normal line and 1 for strip line
44   \param width - width of line
45 */
46 GLViewer_AspectLine::GLViewer_AspectLine( int type, float width )
47 {
48     myNColor = QColor( 255, 255, 255 );
49     myHColor = QColor( 0, 255, 255 );
50     mySColor = QColor( 255, 0, 0 );
51
52     myLineWidth = width;
53     if( type == 1 || type == 0 )
54         myLineType = type;
55     else
56         myLineType = 0;
57 }
58
59 /*!
60   Destructor
61 */
62 GLViewer_AspectLine::~GLViewer_AspectLine()
63 {
64 }
65
66 /*!
67   Sets line color
68   \param nc - normal color
69   \param hc - hilighting color
70   \param sc - selection color
71 */
72 void GLViewer_AspectLine::setLineColors( QColor nc, QColor hc, QColor sc )
73 {
74     myNColor = nc;
75     myHColor = hc;
76     mySColor = sc;
77 }
78
79 /*!
80   Sets type of line
81   \param type: 0 for normal line and 1 for strip line
82 */
83 int GLViewer_AspectLine::setLineType( const int type )
84 {
85     if( type == 1 || type == 0 )
86     {
87         myLineType = type;
88         return 0;
89     }
90     return 1;
91 }
92
93 /*!
94   Sets width of line
95   \param width - new width of line
96 */
97 int GLViewer_AspectLine::setLineWidth( const float width )
98 {
99     if( width > 0 )
100     {
101         myLineWidth = width;
102         return 0;
103     }
104     return 1;
105 }
106
107 /*!
108   \return colors of line
109   \param nc - variable for normal color
110   \param hc - variable for hilighting color
111   \param sc - variable for selection color
112 */
113 void GLViewer_AspectLine::getLineColors( QColor& nc, QColor& hc, QColor& sc ) const
114 {
115     nc = myNColor;
116     hc = myHColor;
117     sc = mySColor;
118 }
119
120 /*!
121   \return binary representation of line aspect
122 */
123 QByteArray GLViewer_AspectLine::getByteCopy() const
124 {
125     int anISize = sizeof( int );
126     int aFSize = sizeof( float );
127     int aNR = myNColor.red(), aNG = myNColor.green(), aNB = myNColor.blue();
128     int aHR = myHColor.red(), aHG = myHColor.green(), aHB = myHColor.blue();
129     int aSR = mySColor.red(), aSG = mySColor.green(), aSB = mySColor.blue();
130
131     QByteArray aResult;
132     aResult.resize( anISize * 10 + aFSize );
133
134     int i = 0;
135     
136     char* aPointer = (char*)&aNR;
137     for( i = 0; i < anISize; i++, aPointer++ )
138         aResult[i] = *aPointer;
139     aPointer = (char*)&aNG;
140     for( ; i < 2*anISize; i++, aPointer++ )
141         aResult[i] = *aPointer;
142     aPointer = (char*)&aNB;
143     for( ; i < 3*anISize; i++, aPointer++ )
144         aResult[i] = *aPointer;
145
146     aPointer = (char*)&aHR;
147     for( ; i < 4*anISize; i++, aPointer++ )
148         aResult[i] = *aPointer;
149     aPointer = (char*)&aHG;
150     for( ; i < 5*anISize; i++, aPointer++ )
151         aResult[i] = *aPointer;
152     aPointer = (char*)&aHB;
153     for( ; i < 6*anISize; i++, aPointer++ )
154         aResult[i] = *aPointer;
155
156     aPointer = (char*)&aSR;
157     for( ; i < 7*anISize; i++, aPointer++ )
158         aResult[i] = *aPointer;
159     aPointer = (char*)&aSG;
160     for( ; i < 8*anISize; i++, aPointer++ )
161         aResult[i] = *aPointer;
162     aPointer = (char*)&aSB;
163     for( ; i < 9*anISize; i++, aPointer++ )
164         aResult[i] = *aPointer;
165     
166     aPointer = (char*)&myLineWidth;
167     for( ; i < 9*anISize + aFSize; i++, aPointer++ )
168         aResult[i] = *aPointer;
169
170     aPointer = (char*)&myLineType;
171     for( ; i < 10*anISize + aFSize; i++, aPointer++ )
172         aResult[i] = *aPointer;    
173
174     return aResult;
175 }
176
177 /*!
178   Sets line aspect from binary representation
179 */
180 GLViewer_AspectLine* GLViewer_AspectLine::fromByteCopy( QByteArray theBytes )
181 {
182
183     int anISize = sizeof( int );
184     int aFSize = sizeof( float );
185     int aNR = 0, aNG = 0, aNB = 0;
186     int aHR = 0, aHG = 0, aHB = 0;
187     int aSR = 0, aSG = 0, aSB = 0;
188     int aLineType = 0;
189     float aLineWidth = 0;
190
191     int i = 0;
192
193     char* aPointer = (char*)&aNR;
194     for( i = 0; i < anISize; i++, aPointer++ )
195         *aPointer = theBytes[i];
196     aPointer = (char*)&aNG;
197     for( ; i < 2*anISize; i++, aPointer++ )
198         *aPointer = theBytes[i];
199     aPointer = (char*)&aNB;
200     for( ; i < 3*anISize; i++, aPointer++ )
201         *aPointer = theBytes[i];
202
203     aPointer = (char*)&aHR;
204     for( ; i < 4*anISize; i++, aPointer++ )
205         *aPointer = theBytes[i];
206     aPointer = (char*)&aHG;
207     for( ; i < 5*anISize; i++, aPointer++ )
208         *aPointer = theBytes[i];
209     aPointer = (char*)&aHB;
210     for( ; i < 6*anISize; i++, aPointer++ )
211         *aPointer = theBytes[i];
212
213     aPointer = (char*)&aSR;
214     for( ; i < 7*anISize; i++, aPointer++ )
215         *aPointer = theBytes[i];
216     aPointer = (char*)&aSG;
217     for( ; i < 8*anISize; i++, aPointer++ )
218         *aPointer = theBytes[i];
219     aPointer = (char*)&aSB;
220     for( ; i < 9*anISize; i++, aPointer++ )
221         *aPointer = theBytes[i];
222
223     aPointer = (char*)&aLineWidth;
224     for( ; i < 9*anISize + aFSize; i++, aPointer++ )
225         *aPointer = theBytes[i];
226
227     aPointer = (char*)&aLineType;
228     for( ; i < 10*anISize + aFSize; i++, aPointer++ )
229         *aPointer = theBytes[i];
230
231     GLViewer_AspectLine* anAspect = new GLViewer_AspectLine( aLineType, aLineWidth );
232     anAspect->setLineColors( QColor( aNR, aNG, aNB ), 
233                              QColor( aHR, aHG, aHB ), 
234                              QColor( aSR, aSG, aSB ) );
235     return anAspect;
236 }