Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / GLViewer / GLViewer_AspectLine.cxx
1 //  Copyright (C) 2005 OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 // File:      GLViewer_AspectLine.cxx
23 // Created:   26/05/2005 3:17:00 PM
24
25 #include "GLViewer_AspectLine.h"
26
27 /*!
28   Default constructor
29 */
30 GLViewer_AspectLine::GLViewer_AspectLine()
31 {
32     myNColor = QColor( 255, 255, 255 );
33     myHColor = QColor( 0, 255, 255 );
34     mySColor = QColor( 255, 0, 0 );
35
36     myLineWidth = 1.0;
37     myLineType = 0;
38 }
39
40 /*!
41   Constructor
42   \param type: 0 for normal line and 1 for strip line
43   \param width - width of line
44 */
45 GLViewer_AspectLine::GLViewer_AspectLine( int type, float width )
46 {
47     myNColor = QColor( 255, 255, 255 );
48     myHColor = QColor( 0, 255, 255 );
49     mySColor = QColor( 255, 0, 0 );
50
51     myLineWidth = width;
52     if( type == 1 || type == 0 )
53         myLineType = type;
54     else
55         myLineType = 0;
56 }
57
58 /*!
59   Destructor
60 */
61 GLViewer_AspectLine::~GLViewer_AspectLine()
62 {
63 }
64
65 /*!
66   Sets line color
67   \param nc - normal color
68   \param hc - hilighting color
69   \param sc - selection color
70 */
71 void GLViewer_AspectLine::setLineColors( QColor nc, QColor hc, QColor sc )
72 {
73     myNColor = nc;
74     myHColor = hc;
75     mySColor = sc;
76 }
77
78 /*!
79   Sets type of line
80   \param type: 0 for normal line and 1 for strip line
81 */
82 int GLViewer_AspectLine::setLineType( const int type )
83 {
84     if( type == 1 || type == 0 )
85     {
86         myLineType = type;
87         return 0;
88     }
89     return 1;
90 }
91
92 /*!
93   Sets width of line
94   \param width - new width of line
95 */
96 int GLViewer_AspectLine::setLineWidth( const float width )
97 {
98     if( width > 0 )
99     {
100         myLineWidth = width;
101         return 0;
102     }
103     return 1;
104 }
105
106 /*!
107   \return colors of line
108   \param nc - variable for normal color
109   \param hc - variable for hilighting color
110   \param sc - variable for selection color
111 */
112 void GLViewer_AspectLine::getLineColors( QColor& nc, QColor& hc, QColor& sc ) const
113 {
114     nc = myNColor;
115     hc = myHColor;
116     sc = mySColor;
117 }
118
119 /*!
120   \return binary representation of line aspect
121 */
122 QByteArray GLViewer_AspectLine::getByteCopy() const
123 {
124     int anISize = sizeof( int );
125     int aFSize = sizeof( float );
126     int aNR = myNColor.red(), aNG = myNColor.green(), aNB = myNColor.blue();
127     int aHR = myHColor.red(), aHG = myHColor.green(), aHB = myHColor.blue();
128     int aSR = mySColor.red(), aSG = mySColor.green(), aSB = mySColor.blue();
129
130     QByteArray aResult( anISize * 10 + aFSize );
131
132     int i = 0;
133     
134     char* aPointer = (char*)&aNR;
135     for( i = 0; i < anISize; i++, aPointer++ )
136         aResult[i] = *aPointer;
137     aPointer = (char*)&aNG;
138     for( ; i < 2*anISize; i++, aPointer++ )
139         aResult[i] = *aPointer;
140     aPointer = (char*)&aNB;
141     for( ; i < 3*anISize; i++, aPointer++ )
142         aResult[i] = *aPointer;
143
144     aPointer = (char*)&aHR;
145     for( ; i < 4*anISize; i++, aPointer++ )
146         aResult[i] = *aPointer;
147     aPointer = (char*)&aHG;
148     for( ; i < 5*anISize; i++, aPointer++ )
149         aResult[i] = *aPointer;
150     aPointer = (char*)&aHB;
151     for( ; i < 6*anISize; i++, aPointer++ )
152         aResult[i] = *aPointer;
153
154     aPointer = (char*)&aSR;
155     for( ; i < 7*anISize; i++, aPointer++ )
156         aResult[i] = *aPointer;
157     aPointer = (char*)&aSG;
158     for( ; i < 8*anISize; i++, aPointer++ )
159         aResult[i] = *aPointer;
160     aPointer = (char*)&aSB;
161     for( ; i < 9*anISize; i++, aPointer++ )
162         aResult[i] = *aPointer;
163     
164     aPointer = (char*)&myLineWidth;
165     for( ; i < 9*anISize + aFSize; i++, aPointer++ )
166         aResult[i] = *aPointer;
167
168     aPointer = (char*)&myLineType;
169     for( ; i < 10*anISize + aFSize; i++, aPointer++ )
170         aResult[i] = *aPointer;    
171
172     return aResult;
173 }
174
175 /*!
176   Sets line aspect from binary representation
177 */
178 GLViewer_AspectLine* GLViewer_AspectLine::fromByteCopy( QByteArray theBytes )
179 {
180
181     int anISize = sizeof( int );
182     int aFSize = sizeof( float );
183     int aNR = 0, aNG = 0, aNB = 0;
184     int aHR = 0, aHG = 0, aHB = 0;
185     int aSR = 0, aSG = 0, aSB = 0;
186     int aLineType = 0;
187     float aLineWidth = 0;
188
189     int i = 0;
190
191     char* aPointer = (char*)&aNR;
192     for( i = 0; i < anISize; i++, aPointer++ )
193         *aPointer = theBytes[i];
194     aPointer = (char*)&aNG;
195     for( ; i < 2*anISize; i++, aPointer++ )
196         *aPointer = theBytes[i];
197     aPointer = (char*)&aNB;
198     for( ; i < 3*anISize; i++, aPointer++ )
199         *aPointer = theBytes[i];
200
201     aPointer = (char*)&aHR;
202     for( ; i < 4*anISize; i++, aPointer++ )
203         *aPointer = theBytes[i];
204     aPointer = (char*)&aHG;
205     for( ; i < 5*anISize; i++, aPointer++ )
206         *aPointer = theBytes[i];
207     aPointer = (char*)&aHB;
208     for( ; i < 6*anISize; i++, aPointer++ )
209         *aPointer = theBytes[i];
210
211     aPointer = (char*)&aSR;
212     for( ; i < 7*anISize; i++, aPointer++ )
213         *aPointer = theBytes[i];
214     aPointer = (char*)&aSG;
215     for( ; i < 8*anISize; i++, aPointer++ )
216         *aPointer = theBytes[i];
217     aPointer = (char*)&aSB;
218     for( ; i < 9*anISize; i++, aPointer++ )
219         *aPointer = theBytes[i];
220
221     aPointer = (char*)&aLineWidth;
222     for( ; i < 9*anISize + aFSize; i++, aPointer++ )
223         *aPointer = theBytes[i];
224
225     aPointer = (char*)&aLineType;
226     for( ; i < 10*anISize + aFSize; i++, aPointer++ )
227         *aPointer = theBytes[i];
228
229     GLViewer_AspectLine* anAspect = new GLViewer_AspectLine( aLineType, aLineWidth );
230     anAspect->setLineColors( QColor( aNR, aNG, aNB ), 
231                              QColor( aHR, aHG, aHB ), 
232                              QColor( aSR, aSG, aSB ) );
233     return anAspect;
234 }