Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SVTK / SVTK_CubeAxesDlg.cxx
1 //  VISU VISUGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : SVTK_CubeAxesDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : VISU
27
28 #include "SVTK_CubeAxesDlg.h"
29
30 #include "SVTK_MainWindow.h"
31 #include "SVTK_FontWidget.h"
32
33 #include "SVTK_CubeAxesActor2D.h"
34
35 #include "QtxAction.h"
36 #include "QtxIntSpinBox.h"
37
38 #include <qlayout.h>
39 #include <qframe.h>
40 #include <qpushbutton.h>
41 #include <qtabwidget.h>
42 #include <qcheckbox.h>
43 #include <qgroupbox.h>
44 #include <qlineedit.h>
45 #include <qlabel.h>
46 #include <qobjectlist.h>
47 #include <qvalidator.h>
48
49 #include <vtkAxisActor2D.h>
50 #include <vtkTextProperty.h>
51
52 /*!
53  * Class       : AxisWg
54  * Description : Tab of dialog
55  */
56
57 /*!
58   Constructor
59 */
60 SVTK_AxisWidget::SVTK_AxisWidget (QWidget* theParent)
61 :  QFrame(theParent)
62 {
63   QValueList< QLabel* > aLabels;
64
65   // "Name" grp
66
67   myNameGrp = new QGroupBox(3, Qt::Vertical, tr("AXIS_NAME"), this);
68   myIsNameVisible = new QCheckBox(tr("IS_VISIBLE"), myNameGrp);
69
70   QHBox* aHBox = new QHBox(myNameGrp);
71   aHBox->setSpacing(5);
72   QLabel* aLabel = new QLabel(tr("NAME"), aHBox);
73   myAxisName = new QLineEdit(aHBox);
74   aLabels.append(aLabel);
75
76   aHBox = new QHBox(myNameGrp);
77   aHBox->setSpacing(5);
78   aLabel = new QLabel(tr("FONT"), aHBox);
79   myNameFont = new SVTK_FontWidget(aHBox);
80   aLabels.append(aLabel);
81
82
83   // "Labels" grp
84
85   myLabelsGrp = new QGroupBox(4, Qt::Vertical, tr("LABELS"), this);
86   myIsLabelsVisible = new QCheckBox(tr("IS_VISIBLE"), myLabelsGrp);
87
88   aHBox = new QHBox(myLabelsGrp);
89   aHBox->setSpacing(5);
90   aLabel = new QLabel(tr("NUMBER"), aHBox);
91   myLabelNumber = new QtxIntSpinBox(0,25,1,aHBox,"SpinBoxLabelNumber");
92   aLabels.append(aLabel);
93
94   aHBox = new QHBox(myLabelsGrp);
95   aHBox->setSpacing(5);
96   aLabel = new QLabel(tr("OFFSET"), aHBox);
97   myLabelOffset = new QtxIntSpinBox(0,100,1,aHBox,"SpinBoxLabellOffset");
98   aLabels.append(aLabel);
99
100   aHBox = new QHBox(myLabelsGrp);
101   aHBox->setSpacing(5);
102   aLabel = new QLabel(tr("FONT"), aHBox);
103   myLabelsFont = new SVTK_FontWidget(aHBox);
104   aLabels.append(aLabel);
105
106   // "Tick marks" grp
107
108   myTicksGrp = new QGroupBox(2, Qt::Vertical, tr("TICK_MARKS"), this);
109   myIsTicksVisible = new QCheckBox(tr("IS_VISIBLE"), myTicksGrp);
110
111   aHBox = new QHBox(myTicksGrp);
112   aHBox->setSpacing(5);
113   aLabel = new QLabel(tr("LENGTH"), aHBox);
114   myTickLength = new QtxIntSpinBox(0,100,1,aHBox,"SpinBoxTickLength");
115   
116   aLabels.append(aLabel);
117
118   // Layout
119
120   QVBoxLayout* aLay = new QVBoxLayout(this, 0, 5);
121   aLay->addWidget(myNameGrp);
122   aLay->addWidget(myLabelsGrp);
123   aLay->addWidget(myTicksGrp);
124
125   // init
126   myIsNameVisible->setChecked(true);
127   myIsLabelsVisible->setChecked(true);
128   myIsTicksVisible->setChecked(true);
129   updateControlState();
130
131   // Adjust label widths
132   QValueList< QLabel* >::iterator anIter;
133   int aMaxWidth = 0;
134   for (anIter = aLabels.begin(); anIter != aLabels.end(); anIter++)
135     aMaxWidth = QMAX(aMaxWidth, (*anIter)->sizeHint().width());
136   for (anIter = aLabels.begin(); anIter != aLabels.end(); anIter++)
137     (*anIter)->setFixedWidth(aMaxWidth);
138
139   // connect signals and slots
140   connect(myIsNameVisible, SIGNAL(stateChanged(int)), SLOT(onNameChecked()));
141   connect(myIsLabelsVisible, SIGNAL(stateChanged(int)), SLOT(onLabelsChecked()));
142   connect(myIsTicksVisible, SIGNAL(stateChanged(int)), SLOT(onTicksChecked()));
143 }
144
145 /*!
146   Destructor
147 */
148 SVTK_AxisWidget::~SVTK_AxisWidget()
149 {
150 }
151
152 void SVTK_AxisWidget::updateControlState()
153 {
154   onNameChecked();
155   onLabelsChecked();
156   onTicksChecked();
157 }
158
159 void SVTK_AxisWidget::setEnabled(QGroupBox* theGrp, const bool theState)
160 {
161   QObjectList aChildren(*theGrp->children());
162   QObject* anObj;
163   for(anObj = aChildren.first(); anObj !=0; anObj = aChildren.next())
164     if (anObj !=0 && anObj->inherits("QHBox"))
165       ((QHBox*)anObj)->setEnabled(theState);
166 }
167
168 void SVTK_AxisWidget::onLabelsChecked()
169 {
170   setEnabled(myLabelsGrp, myIsLabelsVisible->isChecked());
171 }
172
173 void SVTK_AxisWidget::onTicksChecked()
174 {
175   setEnabled(myTicksGrp, myIsTicksVisible->isChecked());
176 }
177
178 void SVTK_AxisWidget::onNameChecked()
179 {
180   setEnabled(myNameGrp, myIsNameVisible->isChecked());
181 }
182
183 void SVTK_AxisWidget::UseName(const bool toUse)
184 {
185   myIsNameVisible->setChecked(toUse);
186 }
187
188 void SVTK_AxisWidget::SetName(const QString& theName)
189 {
190   myAxisName->setText(theName);
191 }
192
193 void SVTK_AxisWidget::SetNameFont(const QColor& theColor,
194                                   const int theFont,
195                                   const bool theIsBold,
196                                   const bool theIsItalic,
197                                   const bool theIsShadow)
198 {
199   myNameFont->SetData(theColor, theFont, theIsBold, theIsItalic, theIsShadow);
200 }
201
202 bool SVTK_AxisWidget::ReadData(vtkAxisActor2D* theActor)
203 {
204   if (theActor == 0)
205     return false;
206
207   // Name
208
209   bool useName = theActor->GetTitleVisibility();
210   QString aTitle(theActor->GetTitle());
211
212   QColor aTitleColor(255, 255, 255);
213   int aTitleFontFamily = VTK_ARIAL;
214   bool isTitleBold = false;
215   bool isTitleItalic = false;
216   bool isTitleShadow = false;
217
218   vtkTextProperty* aTitleProp = theActor->GetTitleTextProperty();
219   if (aTitleProp !=0)
220   {
221     vtkFloatingPointType c[ 3 ];
222     aTitleProp->GetColor(c);
223     aTitleColor.setRgb((int)(c[ 0 ] * 255), (int)(c[ 1 ] * 255), (int)(c[ 2 ] * 255));
224     aTitleFontFamily = aTitleProp->GetFontFamily();
225     isTitleBold = aTitleProp->GetBold() ? true : false;
226     isTitleItalic = aTitleProp->GetItalic() ? true : false;
227     isTitleShadow = aTitleProp->GetShadow() ? true : false;
228   }
229
230   myIsNameVisible->setChecked(useName);
231   myAxisName->setText(aTitle);
232   myNameFont->SetData(aTitleColor, aTitleFontFamily, isTitleBold, isTitleItalic, isTitleShadow);
233
234   // Labels
235
236   bool useLabels = theActor->GetLabelVisibility();
237   int nbLabels = theActor->GetNumberOfLabels();
238   int anOffset = theActor->GetTickOffset();
239
240   QColor aLabelsColor(255, 255, 255);
241   int aLabelsFontFamily = VTK_ARIAL;
242   bool isLabelsBold = false;
243   bool isLabelsItalic = false;
244   bool isLabelsShadow = false;
245
246   vtkTextProperty* aLabelsProp = theActor->GetLabelTextProperty();
247   if (aLabelsProp !=0)
248   {
249     vtkFloatingPointType c[ 3 ];
250     aLabelsProp->GetColor(c);
251     aLabelsColor.setRgb((int)(c[ 0 ] * 255), (int)(c[ 1 ] * 255), (int)(c[ 2 ] * 255));
252     aLabelsFontFamily = aLabelsProp->GetFontFamily();
253     isLabelsBold = aLabelsProp->GetBold() ? true : false;
254     isLabelsItalic = aLabelsProp->GetItalic() ? true : false;
255     isLabelsShadow = aLabelsProp->GetShadow() ? true : false;
256   }
257
258   myIsLabelsVisible->setChecked(useLabels);
259   myLabelNumber->setValue(nbLabels);
260   myLabelOffset->setValue(anOffset);
261   myLabelsFont->SetData(aLabelsColor, aLabelsFontFamily, isLabelsBold, isLabelsItalic, isLabelsShadow);
262
263   // Tick marks
264   bool useTickMarks = theActor->GetTickVisibility();
265   int aTickLength = theActor->GetTickLength();
266
267   myIsTicksVisible->setChecked(useTickMarks);
268   myTickLength->setValue(aTickLength);
269
270   return true;
271 }
272
273 bool SVTK_AxisWidget::Apply(vtkAxisActor2D* theActor)
274 {
275    if (theActor == 0)
276     return false;
277
278   // Name
279
280   theActor->SetTitleVisibility(myIsNameVisible->isChecked() ? 1 : 0);
281   theActor->SetTitle(myAxisName->text().latin1());
282
283   QColor aTitleColor(255, 255, 255);
284   int aTitleFontFamily = VTK_ARIAL;
285   bool isTitleBold = false;
286   bool isTitleItalic = false;
287   bool isTitleShadow = false;
288
289   myNameFont->GetData(aTitleColor, aTitleFontFamily, isTitleBold, isTitleItalic, isTitleShadow);
290   vtkTextProperty* aTitleProp = theActor->GetTitleTextProperty();
291   if (aTitleProp)
292   {
293     aTitleProp->SetColor(aTitleColor.red() / 255.,
294                           aTitleColor.green() / 255.,
295                           aTitleColor.blue() / 255.);
296     aTitleProp->SetFontFamily(aTitleFontFamily);
297
298     aTitleProp->SetBold(isTitleBold ? 1 : 0);
299     aTitleProp->SetItalic(isTitleItalic ? 1 : 0);
300     aTitleProp->SetShadow(isTitleShadow ? 1 : 0);
301
302     theActor->SetTitleTextProperty(aTitleProp);
303   }
304
305   // Labels
306
307   theActor->SetLabelVisibility(myIsLabelsVisible->isChecked() ? 1 : 0);
308
309   int nbLabels = myLabelNumber->value();
310   theActor->SetNumberOfLabels(nbLabels);
311
312   int anOffset = myLabelOffset->value();
313   theActor->SetTickOffset(anOffset);
314
315   QColor aLabelsColor(255, 255, 255);
316   int aLabelsFontFamily = VTK_ARIAL;
317   bool isLabelsBold = false;
318   bool isLabelsItalic = false;
319   bool isLabelsShadow = false;
320
321   myLabelsFont->GetData(aLabelsColor, aLabelsFontFamily, isLabelsBold, isLabelsItalic, isLabelsShadow);
322   vtkTextProperty* aLabelsProp = theActor->GetLabelTextProperty();
323   if (aLabelsProp)
324   {
325     aLabelsProp->SetColor(aLabelsColor.red() / 255.,
326                           aLabelsColor.green() / 255.,
327                           aLabelsColor.blue() / 255.);
328     aLabelsProp->SetFontFamily(aLabelsFontFamily);
329
330     aLabelsProp->SetBold(isLabelsBold ? 1 : 0);
331     aLabelsProp->SetItalic(isLabelsItalic ? 1 : 0);
332     aLabelsProp->SetShadow(isLabelsShadow ? 1 : 0);
333
334     aLabelsProp->Modified();
335     theActor->SetLabelTextProperty(aLabelsProp);
336   }
337
338
339   // Tick marks
340   theActor->SetTickVisibility(myIsTicksVisible->isChecked());
341   int aTickLength = myTickLength->value();
342   theActor->SetTickLength(aTickLength);
343
344   return true;
345 }
346
347 /*
348   Class       : SVTK_CubeAxesDlg
349   Description : Dialog for specifynig cube axes properties
350 */
351
352 /*!
353   Constructor
354 */
355 SVTK_CubeAxesDlg::SVTK_CubeAxesDlg(QtxAction* theAction,
356                                    SVTK_MainWindow* theParent,
357                                    const char* theName):
358   SVTK_DialogBase(theAction,
359                   theParent, 
360                   theName),
361   myMainWindow(theParent)
362 {
363   setCaption(tr("CAPTION"));
364
365   QVBoxLayout* aLay = new QVBoxLayout(this, 5, 5);
366   aLay->addWidget(createMainFrame(this));
367   aLay->addWidget(createButtonFrame(this));
368
369   connect(theParent, SIGNAL(Show( QShowEvent * )), this, SLOT(onParentShow()));
370   connect(theParent, SIGNAL(Hide( QHideEvent * )), this, SLOT(onParentHide()));
371 }
372
373 /*!
374   Create frame containing dialog's input fields
375 */
376 QWidget* SVTK_CubeAxesDlg::createMainFrame(QWidget* theParent)
377 {
378   QFrame* aFrame = new QFrame(theParent);
379
380   myTabWg = new QTabWidget(aFrame);
381
382   myAxes[ 0 ] = new SVTK_AxisWidget(myTabWg);
383   myAxes[ 1 ] = new SVTK_AxisWidget(myTabWg);
384   myAxes[ 2 ] = new SVTK_AxisWidget(myTabWg);
385
386   myTabWg->addTab(myAxes[ 0 ], tr("X_AXIS"));
387   myTabWg->addTab(myAxes[ 1 ], tr("Y_AXIS"));
388   myTabWg->addTab(myAxes[ 2 ], tr("Z_AXIS"));
389
390   myTabWg->setMargin(5);
391
392   myIsVisible = new QCheckBox(tr("IS_VISIBLE"), aFrame);
393
394   QVBoxLayout* aLay = new QVBoxLayout(aFrame, 0, 5);
395   aLay->addWidget(myTabWg);
396   aLay->addWidget(myIsVisible);
397
398   return aFrame;
399 }
400
401 /*!
402   Create frame containing buttons
403 */
404 QWidget* SVTK_CubeAxesDlg::createButtonFrame(QWidget* theParent)
405 {
406   QFrame* aFrame = new QFrame(theParent);
407   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
408
409   myOkBtn    = new QPushButton(tr("BUT_OK"), aFrame);
410   myApplyBtn = new QPushButton(tr("BUT_APPLY"), aFrame);
411   myCloseBtn = new QPushButton(tr("BUT_CLOSE"), aFrame);
412
413   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
414
415   QHBoxLayout* aLay = new QHBoxLayout(aFrame, 5, 5);
416
417   aLay->addWidget(myOkBtn);
418   aLay->addWidget(myApplyBtn);
419   aLay->addItem(aSpacer);
420   aLay->addWidget(myCloseBtn);
421
422   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
423   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
424   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
425
426   return aFrame;
427 }
428
429 /*!
430   Destructor
431 */
432 SVTK_CubeAxesDlg::~SVTK_CubeAxesDlg()
433 {
434 }
435
436 /*!
437   Update dialog fields, connect signals and slots, show dialog
438 */
439 void SVTK_CubeAxesDlg::Update()
440 {
441   myActor = myMainWindow->GetCubeAxes();
442
443   myAxes[ 0 ]->ReadData(myActor->GetXAxisActor2D());
444   myAxes[ 1 ]->ReadData(myActor->GetYAxisActor2D());
445   myAxes[ 2 ]->ReadData(myActor->GetZAxisActor2D());
446
447   myIsVisible->setChecked(myActor->GetVisibility() ? true : false);
448 }
449
450 /*!
451   Verify validity of entry data
452 */
453 bool SVTK_CubeAxesDlg::isValid() const
454 {
455   return true;
456 }
457
458 /*!
459   Verify validity of entry data
460 */
461 bool SVTK_CubeAxesDlg::onApply()
462 {
463   bool isOk = true;
464
465   try
466   {
467     QWidget *aCurrWid = this->focusWidget();
468     aCurrWid->clearFocus();
469     aCurrWid->setFocus();
470
471     isOk = isOk && myAxes[ 0 ]->Apply(myActor->GetXAxisActor2D());
472     isOk = isOk && myAxes[ 1 ]->Apply(myActor->GetYAxisActor2D());
473     isOk = isOk && myAxes[ 2 ]->Apply(myActor->GetZAxisActor2D());
474
475
476     //myActor->SetXLabel(myAxes[ 0 ]->myAxisName->text());
477     //myActor->SetYLabel(myAxes[ 1 ]->myAxisName->text());
478     //myActor->SetZLabel(myAxes[ 2 ]->myAxisName->text());
479
480     //myActor->SetNumberOfLabels(myActor->GetXAxisActor2D()->GetNumberOfLabels());
481     if (myIsVisible->isChecked())
482       myActor->VisibilityOn();
483     else
484       myActor->VisibilityOff();
485
486     if (isOk)
487       myMainWindow->Repaint();
488   }
489   catch(...)
490   {
491     isOk = false;
492   }
493
494   return isOk;
495 }
496
497 /*!
498   SLOT called when "Ok" button pressed.
499 */
500 void SVTK_CubeAxesDlg::onOk()
501 {
502   if (onApply())
503     onClose();
504 }
505
506 /*!
507   SLOT: called when "Close" button pressed. Close dialog
508 */
509 void SVTK_CubeAxesDlg::onClose()
510 {
511   reject();
512 }