0023821: Improve qmake project files for Qt samples
[occt.git] / samples / mfc / standard / 03_Viewer2d / src / Properties / TypePropertyPage.cpp
1 // TypePropertyPage.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5
6 #include "TypePropertyPage.h"
7
8 #include "Aspect_TypeMap.hxx"
9 #include "Aspect_TypeMapEntry.hxx"
10 #include "TColQuantity_Array1OfLength.hxx"
11 #include "TColStd_SequenceOfReal.hxx"
12
13 /////////////////////////////////////////////////////////////////////////////
14 // CTypePropertyPage property page
15
16 IMPLEMENT_DYNCREATE(CTypePropertyPage, CPropertyPage)
17
18 CTypePropertyPage::CTypePropertyPage() : CPropertyPage(CTypePropertyPage::IDD)
19 {
20         //{{AFX_DATA_INIT(CTypePropertyPage)
21         m_TypeMapSize       = _T("Computing...");
22         m_CurrentEntryStyle = _T("Computing...");
23         m_CurrentEntryValue = _T("Computing...");
24     m_NewEntryValue     = _T("");
25         //}}AFX_DATA_INIT
26
27
28 }
29 BOOL CTypePropertyPage::OnInitDialog() 
30 {
31         CPropertyPage::OnInitDialog();
32         
33         // TODO: Add extra initialization here
34
35     m_NewEntryControl.AddString("SOLID");
36     m_NewEntryControl.AddString("DASH");
37     m_NewEntryControl.AddString("DOT");
38     m_NewEntryControl.AddString("DOTDASH");
39     m_NewEntryControl.AddString("USERDEFINED");
40     m_NewEntryControl.SetCurSel(1);
41
42     UpdateDisplay(1);
43
44     UpdateData(false);
45
46         return TRUE;  // return TRUE unless you set the focus to a control
47                       // EXCEPTION: OCX Property Pages should return FALSE
48 }
49
50 CTypePropertyPage::~CTypePropertyPage()
51 {
52 }
53
54 void CTypePropertyPage::DoDataExchange(CDataExchange* pDX)
55 {
56         CPropertyPage::DoDataExchange(pDX);
57         //{{AFX_DATA_MAP(CTypePropertyPage)
58         DDX_Control(pDX, IDC_TypeMap_TAB, m_TabCtrl);
59         DDX_Control(pDX, IDC_TypeMap_COMBO_NewEntryStyle, m_NewEntryControl);
60         DDX_Control(pDX, IDC_TypeMap_EDIT_NewEntryValue, m_NewEntryValueControl);
61         DDX_Text(pDX, IDC_TypeMap_STATIC_Size, m_TypeMapSize);
62         DDX_Text(pDX, IDC_TypeMap_STATIC_CurrentEntryStyle, m_CurrentEntryStyle);
63         DDX_Text(pDX, IDC_TypeMap_STATIC_CurrentEntryValue, m_CurrentEntryValue);
64         DDX_Text(pDX, IDC_TypeMap_EDIT_NewEntryValue, m_NewEntryValue);
65
66         //}}AFX_DATA_MAP
67 }
68
69
70 BEGIN_MESSAGE_MAP(CTypePropertyPage, CPropertyPage)
71         //{{AFX_MSG_MAP(CTypePropertyPage)
72         ON_BN_CLICKED(IDC_FontMap_BUTTON_NewEntry, OnFontMapBUTTONNewEntry)
73         ON_BN_CLICKED(IDC_FontMap_BUTTON_UpdateCurrent, OnFontMapBUTTONUpdateCurrent)
74         ON_EN_CHANGE(IDC_TypeMap_EDIT_NewEntryValue, OnChangeTypeMapEDITNewEntryValue)
75         ON_NOTIFY(TCN_SELCHANGE, IDC_TypeMap_TAB, OnSelchangeTypeMapTAB)
76         ON_CBN_SELCHANGE(IDC_TypeMap_COMBO_NewEntryStyle, OnSelchangeTypeMapCOMBONewEntryStyle)
77         //}}AFX_MSG_MAP
78 END_MESSAGE_MAP()
79
80 /////////////////////////////////////////////////////////////////////////////
81 // CTypePropertyPage message handlers
82 void CTypePropertyPage::UpdateDisplay(int CurrentSelectionIndex)
83 {
84     TCollection_AsciiString SizeMessage(myTypeMap->Size());
85     m_TypeMapSize = _T(SizeMessage.ToCString());
86         
87     m_TabCtrl.DeleteAllItems();
88
89         TC_ITEM TabCtrlItem;
90         TabCtrlItem.mask = TCIF_TEXT;
91
92     for(int i =1;i<=myTypeMap->Size();i++) 
93     {
94       Aspect_TypeMapEntry aTypeMapEntry = myTypeMap->Entry(i);
95       TCollection_AsciiString EntryNumerMessage(aTypeMapEntry.Index());
96       TabCtrlItem.pszText = (LPSTR) EntryNumerMessage.ToCString();
97           m_TabCtrl.InsertItem( aTypeMapEntry.Index(), &TabCtrlItem );
98     }
99     m_TabCtrl.SetCurSel(CurrentSelectionIndex-1);
100
101     // update The Current Selected entry
102     Aspect_TypeMapEntry aTypeMapEntry = myTypeMap->Entry(CurrentSelectionIndex);
103     if (aTypeMapEntry. IsAllocated () ) 
104       {
105         Aspect_LineStyle TypeMapEntryType = aTypeMapEntry.Type() ;
106
107         switch (TypeMapEntryType.Style()) {
108           case Aspect_TOL_SOLID       :  m_CurrentEntryStyle = _T("SOLID");       break;
109           case Aspect_TOL_DASH        :  m_CurrentEntryStyle = _T("DASH");        break;
110           case Aspect_TOL_DOT         :  m_CurrentEntryStyle = _T("DOT");         break;
111           case Aspect_TOL_DOTDASH     :  m_CurrentEntryStyle = _T("DOTDASH");     break;
112           case Aspect_TOL_USERDEFINED :  m_CurrentEntryStyle = _T("USERDEFINED"); break;
113           default : m_CurrentEntryStyle = _T("Underknown");
114         }
115         int NbValue = TypeMapEntryType.Length();
116         TCollection_AsciiString Message = BuildValuesAscii(TypeMapEntryType.Values());
117
118             m_CurrentEntryValue = _T(Message.ToCString());
119
120         m_NewEntryControl.SetCurSel(TypeMapEntryType.Style());
121         m_NewEntryValue = _T(Message.ToCString());  
122         if (TypeMapEntryType.Style() == Aspect_TOL_USERDEFINED)
123           m_NewEntryValueControl.SetReadOnly( false );
124         else
125           m_NewEntryValueControl.SetReadOnly( true );
126       }
127       else 
128       {
129             m_CurrentEntryStyle = _T("Not Allocated");
130             m_CurrentEntryValue = _T("");
131         m_NewEntryValue     = _T("No value");  
132         GetDlgItem(IDC_FontMap_BUTTON_UpdateCurrent)->ShowWindow(SW_HIDE);
133       }
134 }
135
136 void CTypePropertyPage::OnSelchangeTypeMapTAB(NMHDR* pNMHDR, LRESULT* pResult) 
137 {
138     UpdateDisplay(m_TabCtrl.GetCurSel()+1);     
139     UpdateData(false);
140         *pResult = 0;
141 }
142 void CTypePropertyPage::OnSelchangeTypeMapCOMBONewEntryStyle() 
143 {
144    UpdateData(true);
145    // the Type Map entry change in the edit part
146    if (m_NewEntryControl.GetCurSel() == Aspect_TOL_USERDEFINED)
147      m_NewEntryValueControl.SetReadOnly( false );
148    else
149    {
150       m_NewEntryValueControl.SetReadOnly( true );
151     
152     // create a dummy map to extract the default values
153       Aspect_TypeMapEntry aTypeMapEntry(99,(Aspect_TypeOfLine)m_NewEntryControl.GetCurSel());
154       Aspect_LineStyle TypeMapEntryType = aTypeMapEntry.Type() ;
155       TCollection_AsciiString Message = BuildValuesAscii(TypeMapEntryType.Values());
156
157       m_NewEntryValue = _T(Message.ToCString());
158    }
159    UpdateData(false);
160 }
161
162 void CTypePropertyPage::OnChangeTypeMapEDITNewEntryValue() 
163 {
164    UpdateData(true);
165    Handle(TColQuantity_HArray1OfLength) anArray;
166    Standard_Boolean IsDone = ExtractValues(TCollection_AsciiString((char *)(LPCSTR)m_NewEntryValue),anArray);
167    if (IsDone)
168    {
169         GetDlgItem(IDC_FontMap_BUTTON_UpdateCurrent)->ShowWindow(SW_SHOW);
170         GetDlgItem(IDC_FontMap_BUTTON_NewEntry)->ShowWindow(SW_SHOW);
171    }
172    else
173    {
174         GetDlgItem(IDC_FontMap_BUTTON_UpdateCurrent)->ShowWindow(SW_HIDE);
175         GetDlgItem(IDC_FontMap_BUTTON_NewEntry)->ShowWindow(SW_HIDE);
176     }
177 }
178
179 void CTypePropertyPage::OnFontMapBUTTONNewEntry() 
180 {
181    UpdateData(true);
182    int NewEntry;
183
184    if (m_NewEntryControl.GetCurSel() == Aspect_TOL_USERDEFINED)
185    {
186      Handle(TColQuantity_HArray1OfLength) anArray;
187      if (ExtractValues(TCollection_AsciiString((char *)(LPCSTR)m_NewEntryValue),anArray))
188      {
189        Aspect_LineStyle aLineStyle(anArray->Array1());
190       NewEntry = myTypeMap->AddEntry( aLineStyle );
191      }
192      else Standard_Failure::Raise(" The String is not Valid ");
193    }
194    else
195    {
196       Aspect_LineStyle aLineStyle((Aspect_TypeOfLine)m_NewEntryControl.GetCurSel());
197       NewEntry = myTypeMap->AddEntry( aLineStyle );
198    }
199
200    SetModified(true);
201    UpdateDisplay(NewEntry);
202    UpdateData(false);
203 }
204
205 void CTypePropertyPage::OnFontMapBUTTONUpdateCurrent() 
206 {
207    UpdateData(true);
208    Aspect_TypeMapEntry aTypeMapEntry = myTypeMap->Entry( m_TabCtrl.GetCurSel()+1);
209
210    if (m_NewEntryControl.GetCurSel() == Aspect_TOL_USERDEFINED)
211    {
212      Handle(TColQuantity_HArray1OfLength) anArray;
213      if (ExtractValues(TCollection_AsciiString((char *)(LPCSTR)m_NewEntryValue),anArray))
214      {
215        Aspect_LineStyle aLineStyle(anArray->Array1());
216        aTypeMapEntry.SetType( aLineStyle );
217      }
218      else Standard_Failure::Raise(" The String is not Valid ");
219    }
220    else
221    {
222      Aspect_LineStyle aLineStyle((Aspect_TypeOfLine)m_NewEntryControl.GetCurSel());
223      aTypeMapEntry.SetType( aLineStyle );
224    }
225
226    myTypeMap->AddEntry(aTypeMapEntry); // in fact just update
227    
228    SetModified(true);
229    UpdateDisplay(m_TabCtrl.GetCurSel()+1);      
230    UpdateData(false);
231 }
232
233 BOOL CTypePropertyPage::OnApply() 
234 {
235     myViewer->SetTypeMap(myTypeMap);
236     myViewer->Update();
237         return CPropertyPage::OnApply();
238 }
239
240 Standard_Boolean  CTypePropertyPage::ExtractValues(TCollection_AsciiString aMessage,
241                                                    Handle(TColQuantity_HArray1OfLength)& anArray) // out 
242 {
243   TColStd_SequenceOfReal aSequenceOfReal;
244   Standard_Integer CurrentStartValue=1;
245   bool NotFinish = true;
246   while (NotFinish)
247   {
248     CurrentStartValue = aMessage.SearchFromEnd(";");
249     if ( CurrentStartValue == aMessage.Length()) return Standard_False;
250     if (CurrentStartValue != -1)
251     { 
252       TCollection_AsciiString aNewMessage = aMessage.Split(CurrentStartValue);
253       aMessage.Remove(aMessage.Length());
254       if (aNewMessage.IsRealValue())
255         aSequenceOfReal.Append(aNewMessage.RealValue());
256       else  return Standard_False;
257     }
258     else
259     {
260       if (aMessage.IsRealValue())
261         aSequenceOfReal.Append(aMessage.RealValue());
262       else  return Standard_False;
263       NotFinish = false;
264     }
265   }
266
267   anArray = new TColQuantity_HArray1OfLength(1,aSequenceOfReal.Length());
268   for (int i=1;i<=aSequenceOfReal.Length();i++)
269     anArray->SetValue(i,aSequenceOfReal(aSequenceOfReal.Length()-i+1));
270
271   return Standard_True;
272 }
273
274 TCollection_AsciiString CTypePropertyPage::BuildValuesAscii(const TColQuantity_Array1OfLength& anArray)
275 {
276   TCollection_AsciiString Message;
277   int NbValue = anArray.Length();
278   for (int j=1;j<NbValue;j++)
279    {
280      Message += anArray(j);
281      Message += " ; ";
282    }
283   Quantity_Length Length =  anArray(NbValue);
284   if (Length > Precision::Confusion()) Message += Length;
285   else Message += "No value";
286   return Message;
287 }