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