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