0031067: Visualization - Aspect_Window::DoResize() should be a non-const method
[occt.git] / samples / qt / Common / src / OcctWindow.cxx
1 #include <OcctWindow.h>
2
3 IMPLEMENT_STANDARD_RTTIEXT(OcctWindow,Aspect_Window)
4
5 // =======================================================================
6 // function : OcctWindow
7 // purpose  :
8 // =======================================================================
9 OcctWindow::OcctWindow ( QWidget* theWidget, const Quantity_NameOfColor theBackColor )
10 : Aspect_Window(), 
11   myWidget( theWidget )
12 {
13   SetBackground (theBackColor);
14   myXLeft   = myWidget->rect().left();
15   myYTop    = myWidget->rect().top();
16   myXRight  = myWidget->rect().right();
17   myYBottom = myWidget->rect().bottom();
18 }
19
20 // =======================================================================
21 // function : Destroy
22 // purpose  :
23 // =======================================================================
24 void OcctWindow::Destroy()
25 {
26   myWidget = NULL;
27 }
28
29 // =======================================================================
30 // function : NativeParentHandle
31 // purpose  :
32 // =======================================================================
33 Aspect_Drawable OcctWindow::NativeParentHandle() const
34 {
35   QWidget* aParentWidget = myWidget->parentWidget();
36   if ( aParentWidget != NULL )
37     return (Aspect_Drawable)aParentWidget->winId();
38   else
39     return 0;
40 }
41
42 // =======================================================================
43 // function : NativeHandle
44 // purpose  :
45 // =======================================================================
46 Aspect_Drawable OcctWindow::NativeHandle() const
47 {
48   return (Aspect_Drawable)myWidget->winId();
49 }
50
51 // =======================================================================
52 // function : IsMapped
53 // purpose  :
54 // =======================================================================
55 Standard_Boolean OcctWindow::IsMapped() const
56 {
57   return !( myWidget->isMinimized() || myWidget->isHidden() );
58 }
59
60 // =======================================================================
61 // function : Map
62 // purpose  :
63 // =======================================================================
64 void OcctWindow::Map() const
65 {
66   myWidget->show();
67   myWidget->update();
68 }
69
70 // =======================================================================
71 // function : Unmap
72 // purpose  :
73 // =======================================================================
74 void OcctWindow::Unmap() const
75 {
76   myWidget->hide();
77   myWidget->update();
78 }
79
80 // =======================================================================
81 // function : DoResize
82 // purpose  :
83 // =======================================================================
84 Aspect_TypeOfResize OcctWindow::DoResize()
85 {
86   int                 aMask = 0;
87   Aspect_TypeOfResize aMode = Aspect_TOR_UNKNOWN;
88
89   if ( !myWidget->isMinimized() )
90   {
91     if ( Abs ( myWidget->rect().left()   - myXLeft   ) > 2 ) aMask |= 1;
92     if ( Abs ( myWidget->rect().right()  - myXRight  ) > 2 ) aMask |= 2;
93     if ( Abs ( myWidget->rect().top()    - myYTop    ) > 2 ) aMask |= 4;
94     if ( Abs ( myWidget->rect().bottom() - myYBottom ) > 2 ) aMask |= 8;
95
96     switch ( aMask )
97     {
98       case 0:
99         aMode = Aspect_TOR_NO_BORDER;
100         break;
101       case 1:
102         aMode = Aspect_TOR_LEFT_BORDER;
103         break;
104       case 2:
105         aMode = Aspect_TOR_RIGHT_BORDER;
106         break;
107       case 4:
108         aMode = Aspect_TOR_TOP_BORDER;
109         break;
110       case 5:
111         aMode = Aspect_TOR_LEFT_AND_TOP_BORDER;
112         break;
113       case 6:
114         aMode = Aspect_TOR_TOP_AND_RIGHT_BORDER;
115         break;
116       case 8:
117         aMode = Aspect_TOR_BOTTOM_BORDER;
118         break;
119       case 9:
120         aMode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER;
121         break;
122       case 10:
123         aMode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER;
124         break;
125       default:
126         break;
127     }  // end switch
128
129     myXLeft   = myWidget->rect().left();
130     myXRight  = myWidget->rect().right();
131     myYTop    = myWidget->rect().top();
132     myYBottom = myWidget->rect().bottom();
133   }
134
135   return aMode;
136 }
137
138 // =======================================================================
139 // function : Ratio
140 // purpose  :
141 // =======================================================================
142 Standard_Real OcctWindow::Ratio() const
143 {
144   QRect aRect = myWidget->rect();
145   return Standard_Real( aRect.right() - aRect.left() ) / Standard_Real( aRect.bottom() - aRect.top() );
146 }
147
148 // =======================================================================
149 // function : Size
150 // purpose  :
151 // =======================================================================
152 void OcctWindow::Size ( Standard_Integer& theWidth, Standard_Integer& theHeight ) const
153 {
154   QRect aRect = myWidget->rect();
155   theWidth  = aRect.right();
156   theHeight = aRect.bottom();
157 }
158
159 // =======================================================================
160 // function : Position
161 // purpose  :
162 // =======================================================================
163 void OcctWindow::Position ( Standard_Integer& theX1, Standard_Integer& theY1,
164                             Standard_Integer& theX2, Standard_Integer& theY2 ) const
165 {
166   theX1 = myWidget->rect().left();
167   theX2 = myWidget->rect().right();
168   theY1 = myWidget->rect().top();
169   theY2 = myWidget->rect().bottom();
170 }