Salome HOME
Update Help for VISU module.
[modules/visu.git] / src / VISUGUI / VisuGUI_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   : VisuGUI_CubeAxesDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : VISU
27
28 #include "VisuGUI_CubeAxesDlg.h"
29
30 #include "VisuGUI.h"
31 #include "VisuGUI_Tools.h"
32 #include "VisuGUI_FontWg.h"
33
34 #include "SVTK_ViewWindow.h"
35 #include "SVTK_CubeAxesActor2D.h"
36
37 #include <qlayout.h>
38 #include <qframe.h>
39 #include <qpushbutton.h>
40 #include <qtabwidget.h>
41 #include <qcheckbox.h>
42 #include <qgroupbox.h>
43 #include <qlineedit.h>
44 #include <qlabel.h>
45 #include <qobjectlist.h>
46 #include <qvalidator.h>
47
48 #include <vtkAxisActor2D.h>
49 #include <vtkTextProperty.h>
50
51 /*!
52  * Class       : AxisWg
53  * Description : Tab of dialog
54  */
55
56 //=======================================================================
57 // name    : VisuGUI_AxisWg::AxisWg
58 // Purpose : Constructor
59 //=======================================================================
60 VisuGUI_AxisWg::VisuGUI_AxisWg (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 VisuGUI_FontWg(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 QLineEdit(aHBox);
92   myLabelNumber->setValidator(new QIntValidator(0, 25, this));
93   myLabelNumber->installEventFilter(this);
94   aLabels.append(aLabel);
95
96   aHBox = new QHBox(myLabelsGrp);
97   aHBox->setSpacing(5);
98   aLabel = new QLabel(tr("OFFSET"), aHBox);
99   myLabelOffset = new QLineEdit(aHBox);
100   aLabels.append(aLabel);
101
102   aHBox = new QHBox(myLabelsGrp);
103   aHBox->setSpacing(5);
104   aLabel = new QLabel(tr("FONT"), aHBox);
105   myLabelsFont = new VisuGUI_FontWg(aHBox);
106   aLabels.append(aLabel);
107
108   // "Tick marks" grp
109
110   myTicksGrp = new QGroupBox(2, Qt::Vertical, tr("TICK_MARKS"), this);
111   myIsTicksVisible = new QCheckBox(tr("IS_VISIBLE"), myTicksGrp);
112
113   aHBox = new QHBox(myTicksGrp);
114   aHBox->setSpacing(5);
115   aLabel = new QLabel(tr("LENGTH"), aHBox);
116   myTickLength = new QLineEdit(aHBox);
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 VisuGUI_AxisWg::~VisuGUI_AxisWg()
147 {
148 }
149
150 void VisuGUI_AxisWg::updateControlState()
151 {
152   onNameChecked();
153   onLabelsChecked();
154   onTicksChecked();
155 }
156
157 bool VisuGUI_AxisWg::eventFilter(QObject* o, QEvent* e)
158 {
159   if (e->type() == QEvent::FocusOut) {
160     bool isOK = false;
161     int k = myLabelNumber->text().toInt(&isOK);
162     if (isOK && k > 25) myLabelNumber->setText("25");
163   }
164   return false;
165 }
166
167 //=======================================================================
168 // name    : VisuGUI_AxisWg::onNameChecked
169 // Purpose :
170 //=======================================================================
171 void VisuGUI_AxisWg::setEnabled(QGroupBox* theGrp, const bool theState)
172 {
173   QObjectList aChildren(*theGrp->children());
174   QObject* anObj;
175   for(anObj = aChildren.first(); anObj !=0; anObj = aChildren.next())
176     if (anObj !=0 && anObj->inherits("QHBox"))
177       ((QHBox*)anObj)->setEnabled(theState);
178 }
179
180 //=======================================================================
181 // Labels    : VisuGUI_AxisWg::onLabelsChecked
182 // Purpose :
183 //=======================================================================
184 void VisuGUI_AxisWg::onLabelsChecked()
185 {
186   setEnabled(myLabelsGrp, myIsLabelsVisible->isChecked());
187 }
188
189 //=======================================================================
190 // Labels    : VisuGUI_AxisWg::onTicksChecked
191 // Purpose :
192 //=======================================================================
193 void VisuGUI_AxisWg::onTicksChecked()
194 {
195   setEnabled(myTicksGrp, myIsTicksVisible->isChecked());
196 }
197
198 //=======================================================================
199 // name    : VisuGUI_AxisWg::onNameChecked
200 // Purpose :
201 //=======================================================================
202 void VisuGUI_AxisWg::onNameChecked()
203 {
204   setEnabled(myNameGrp, myIsNameVisible->isChecked());
205 }
206
207 //=======================================================================
208 // name    : VisuGUI_AxisWg::UseName
209 // Purpose :
210 //=======================================================================
211 void VisuGUI_AxisWg::UseName(const bool toUse)
212 {
213   myIsNameVisible->setChecked(toUse);
214 }
215
216 //=======================================================================
217 // name    : VisuGUI_AxisWg::SetName
218 // Purpose :
219 //================================================== =====================
220 void VisuGUI_AxisWg::SetName(const QString& theName)
221 {
222   myAxisName->setText(theName);
223 }
224
225 //=======================================================================
226 // name    : VisuGUI_AxisWg::SetNameFont
227 // Purpose :
228 //=======================================================================
229 void VisuGUI_AxisWg::SetNameFont(const QColor& theColor,
230                                   const int theFont,
231                                   const bool theIsBold,
232                                   const bool theIsItalic,
233                                   const bool theIsShadow)
234 {
235   myNameFont->SetData(theColor, theFont, theIsBold, theIsItalic, theIsShadow);
236 }
237
238 //=======================================================================
239 // name    : VisuGUI_AxisWg::SetNameFont
240 // Purpose :
241 //=======================================================================
242 bool VisuGUI_AxisWg::ReadData(vtkAxisActor2D* theActor)
243 {
244   if (theActor == 0)
245     return false;
246
247   // Name
248
249   bool useName = theActor->GetTitleVisibility();
250   QString aTitle(theActor->GetTitle());
251
252   QColor aTitleColor(255, 255, 255);
253   int aTitleFontFamily = VTK_ARIAL;
254   bool isTitleBold = false;
255   bool isTitleItalic = false;
256   bool isTitleShadow = false;
257
258   vtkTextProperty* aTitleProp = theActor->GetTitleTextProperty();
259   if (aTitleProp !=0)
260   {
261     float c[ 3 ];
262     aTitleProp->GetColor(c);
263     aTitleColor.setRgb((int)(c[ 0 ] * 255), (int)(c[ 1 ] * 255), (int)(c[ 2 ] * 255));
264     aTitleFontFamily = aTitleProp->GetFontFamily();
265     isTitleBold = aTitleProp->GetBold() ? true : false;
266     isTitleItalic = aTitleProp->GetItalic() ? true : false;
267     isTitleShadow = aTitleProp->GetShadow() ? true : false;
268   }
269
270   myIsNameVisible->setChecked(useName);
271   myAxisName->setText(aTitle);
272   myNameFont->SetData(aTitleColor, aTitleFontFamily, isTitleBold, isTitleItalic, isTitleShadow);
273
274   // Labels
275
276   bool useLabels = theActor->GetLabelVisibility();
277   int nbLabels = theActor->GetNumberOfLabels();
278   int anOffset = theActor->GetTickOffset();
279
280   QColor aLabelsColor(255, 255, 255);
281   int aLabelsFontFamily = VTK_ARIAL;
282   bool isLabelsBold = false;
283   bool isLabelsItalic = false;
284   bool isLabelsShadow = false;
285
286   vtkTextProperty* aLabelsProp = theActor->GetLabelTextProperty();
287   if (aLabelsProp !=0)
288   {
289     float c[ 3 ];
290     aLabelsProp->GetColor(c);
291     aLabelsColor.setRgb((int)(c[ 0 ] * 255), (int)(c[ 1 ] * 255), (int)(c[ 2 ] * 255));
292     aLabelsFontFamily = aLabelsProp->GetFontFamily();
293     isLabelsBold = aLabelsProp->GetBold() ? true : false;
294     isLabelsItalic = aLabelsProp->GetItalic() ? true : false;
295     isLabelsShadow = aLabelsProp->GetShadow() ? true : false;
296   }
297
298   myIsLabelsVisible->setChecked(useLabels);
299   myLabelNumber->setText(QString("%1").arg(nbLabels));
300   myLabelOffset->setText(QString("%1").arg(anOffset));
301   myLabelsFont->SetData(aLabelsColor, aLabelsFontFamily, isLabelsBold, isLabelsItalic, isLabelsShadow);
302
303   // Tick marks
304   bool useTickMarks = theActor->GetTickVisibility();
305   int aTickLength = theActor->GetTickLength();
306
307   myIsTicksVisible->setChecked(useTickMarks);
308   myTickLength->setText(QString("%1").arg(aTickLength));
309
310   return true;
311 }
312
313 //=======================================================================
314 // name    : VisuGUI_CubeAxesDlg::Apply
315 // Purpose :
316 //=======================================================================
317 bool VisuGUI_AxisWg::Apply(vtkAxisActor2D* theActor)
318 {
319    if (theActor == 0)
320     return false;
321
322   // Name
323
324   theActor->SetTitleVisibility(myIsNameVisible->isChecked() ? 1 : 0);
325   theActor->SetTitle(myAxisName->text().latin1());
326
327   QColor aTitleColor(255, 255, 255);
328   int aTitleFontFamily = VTK_ARIAL;
329   bool isTitleBold = false;
330   bool isTitleItalic = false;
331   bool isTitleShadow = false;
332
333   myNameFont->GetData(aTitleColor, aTitleFontFamily, isTitleBold, isTitleItalic, isTitleShadow);
334   vtkTextProperty* aTitleProp = theActor->GetTitleTextProperty();
335   if (aTitleProp)
336   {
337     aTitleProp->SetColor(aTitleColor.red() / 255.,
338                           aTitleColor.green() / 255.,
339                           aTitleColor.blue() / 255.);
340     aTitleProp->SetFontFamily(aTitleFontFamily);
341
342     aTitleProp->SetBold(isTitleBold ? 1 : 0);
343     aTitleProp->SetItalic(isTitleItalic ? 1 : 0);
344     aTitleProp->SetShadow(isTitleShadow ? 1 : 0);
345
346     theActor->SetTitleTextProperty(aTitleProp);
347   }
348
349   // Labels
350
351   theActor->SetLabelVisibility(myIsLabelsVisible->isChecked() ? 1 : 0);
352
353   bool isOk = false;
354   int nbLabels = myLabelNumber->text().toInt(&isOk);
355   if (isOk)
356     theActor->SetNumberOfLabels(nbLabels);
357
358   int anOffset = myLabelOffset->text().toInt(&isOk);
359   if (isOk)
360     theActor->SetTickOffset(anOffset);
361
362   QColor aLabelsColor(255, 255, 255);
363   int aLabelsFontFamily = VTK_ARIAL;
364   bool isLabelsBold = false;
365   bool isLabelsItalic = false;
366   bool isLabelsShadow = false;
367
368   myLabelsFont->GetData(aLabelsColor, aLabelsFontFamily, isLabelsBold, isLabelsItalic, isLabelsShadow);
369   vtkTextProperty* aLabelsProp = theActor->GetLabelTextProperty();
370   if (aLabelsProp)
371   {
372     aLabelsProp->SetColor(aLabelsColor.red() / 255.,
373                           aLabelsColor.green() / 255.,
374                           aLabelsColor.blue() / 255.);
375     aLabelsProp->SetFontFamily(aLabelsFontFamily);
376
377     aLabelsProp->SetBold(isLabelsBold ? 1 : 0);
378     aLabelsProp->SetItalic(isLabelsItalic ? 1 : 0);
379     aLabelsProp->SetShadow(isLabelsShadow ? 1 : 0);
380
381     aLabelsProp->Modified();
382     theActor->SetLabelTextProperty(aLabelsProp);
383   }
384
385
386   // Tick marks
387   theActor->SetTickVisibility(myIsTicksVisible->isChecked());
388   int aTickLength = myTickLength->text().toInt(&isOk);
389   if (isOk)
390     theActor->SetTickLength(aTickLength);
391
392   return true;
393 }
394
395 /*
396   Class       : VisuGUI_CubeAxesDlg
397   Description : Dialog for specifynig cube axes properties
398 */
399
400 //=======================================================================
401 // name    : VisuGUI_CubeAxesDlg::VisuGUI_CubeAxesDlg
402 // Purpose : Constructor
403 //=======================================================================
404 VisuGUI_CubeAxesDlg::VisuGUI_CubeAxesDlg(QWidget* theParent)
405 : QDialog(theParent, "VisuGUI_CubeAxesDlg", false,
406     WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose)
407 {
408   setCaption(tr("CAPTION"));
409
410   QVBoxLayout* aLay = new QVBoxLayout(this, 5, 5);
411   aLay->addWidget(createMainFrame(this));
412   aLay->addWidget(createButtonFrame(this));
413
414   Init();
415
416 //if (VISU::GetDesktop()->getMainFrame())
417 //  connect(VISU::GetDesktop()->getMainFrame(), SIGNAL(windowActivated(QWidget*)),
418 //          SLOT(onWindowActivated(QWidget*)));
419 }
420
421 //=======================================================================
422 // name    : VisuGUI_CubeAxesDlg::createMainFrame
423 // Purpose : Create frame containing dialog's input fields
424 //=======================================================================
425 QWidget* VisuGUI_CubeAxesDlg::createMainFrame(QWidget* theParent)
426 {
427   QFrame* aFrame = new QFrame(theParent);
428
429   myTabWg = new QTabWidget(aFrame);
430
431   myAxes[ 0 ] = new VisuGUI_AxisWg(myTabWg);
432   myAxes[ 1 ] = new VisuGUI_AxisWg(myTabWg);
433   myAxes[ 2 ] = new VisuGUI_AxisWg(myTabWg);
434
435   myTabWg->addTab(myAxes[ 0 ], tr("X_AXIS"));
436   myTabWg->addTab(myAxes[ 1 ], tr("Y_AXIS"));
437   myTabWg->addTab(myAxes[ 2 ], tr("Z_AXIS"));
438
439   myTabWg->setMargin(5);
440
441   myIsVisible = new QCheckBox(tr("IS_VISIBLE"), aFrame);
442
443   QVBoxLayout* aLay = new QVBoxLayout(aFrame, 0, 5);
444   aLay->addWidget(myTabWg);
445   aLay->addWidget(myIsVisible);
446
447   return aFrame;
448 }
449
450 //=======================================================================
451 // name    : VisuGUI_CubeAxesDlg::createButtonFrame
452 // Purpose : Create frame containing buttons
453 //=======================================================================
454 QWidget* VisuGUI_CubeAxesDlg::createButtonFrame(QWidget* theParent)
455 {
456   QFrame* aFrame = new QFrame(theParent);
457   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
458
459   myOkBtn    = new QPushButton(tr("BUT_OK"), aFrame);
460   myApplyBtn = new QPushButton(tr("BUT_APPLY"), aFrame);
461   myCloseBtn = new QPushButton(tr("BUT_CLOSE"), aFrame);
462
463   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
464
465   QHBoxLayout* aLay = new QHBoxLayout(aFrame, 5, 5);
466
467   aLay->addWidget(myOkBtn);
468   aLay->addWidget(myApplyBtn);
469   aLay->addItem(aSpacer);
470   aLay->addWidget(myCloseBtn);
471
472   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
473   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
474   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
475
476   return aFrame;
477 }
478
479 //=======================================================================
480 // name    : VisuGUI_CubeAxesDlg::~VisuGUI_CubeAxesDlg
481 // Purpose : Destructor
482 //=======================================================================
483 VisuGUI_CubeAxesDlg::~VisuGUI_CubeAxesDlg()
484 {
485 }
486
487 //=======================================================================
488 // name    : VisuGUI_CubeAxesDlg::Init
489 // Purpose : Init dialog fields, connect signals and slots, show dialog
490 //=======================================================================
491 bool VisuGUI_CubeAxesDlg::Init()
492 {
493   SVTK_ViewWindow* aFrame = VISU::GetViewWindow();
494   if (aFrame == 0)
495     return false;
496
497   SVTK_CubeAxesActor2D* anActor = aFrame->GetCubeAxes();
498   if (anActor == 0)
499     return false;
500
501   myAxes[ 0 ]->ReadData(anActor->GetXAxisActor2D());
502   myAxes[ 1 ]->ReadData(anActor->GetYAxisActor2D());
503   myAxes[ 2 ]->ReadData(anActor->GetZAxisActor2D());
504
505   myIsVisible->setChecked(anActor->GetVisibility() ? true : false);
506
507   return true;
508
509 }
510
511 //=======================================================================
512 // name    : VisuGUI_CubeAxesDlg::isValid
513 // Purpose : Verify validity of entry data
514 //=======================================================================
515 bool VisuGUI_CubeAxesDlg::isValid() const
516 {
517   return true;
518 }
519
520 //=======================================================================
521 // name    : VisuGUI_CubeAxesDlg::onApply
522 // Purpose : Verify validity of entry data
523 //=======================================================================
524 bool VisuGUI_CubeAxesDlg::onApply()
525 {
526   bool isOk = true;
527
528   try
529   {
530     SVTK_ViewWindow* aFrame = VISU::GetViewWindow();
531     if (aFrame == 0)
532       return false;
533
534     SVTK_CubeAxesActor2D* anActor = aFrame->GetCubeAxes();
535     if (anActor == 0)
536       return false;
537
538     isOk = isOk && myAxes[ 0 ]->Apply(anActor->GetXAxisActor2D());
539     isOk = isOk && myAxes[ 1 ]->Apply(anActor->GetYAxisActor2D());
540     isOk = isOk && myAxes[ 2 ]->Apply(anActor->GetZAxisActor2D());
541
542
543     //anActor->SetXLabel(myAxes[ 0 ]->myAxisName->text());
544     //anActor->SetYLabel(myAxes[ 1 ]->myAxisName->text());
545     //anActor->SetZLabel(myAxes[ 2 ]->myAxisName->text());
546
547     //anActor->SetNumberOfLabels(anActor->GetXAxisActor2D()->GetNumberOfLabels());
548     if (myIsVisible->isChecked())
549       anActor->VisibilityOn();
550     else
551       anActor->VisibilityOff();
552
553     if (isOk)
554       aFrame->Repaint();
555   }
556   catch(...)
557   {
558     isOk = false;
559   }
560
561   return isOk;
562 }
563
564 //=======================================================================
565 // name    : VisuGUI_CubeAxesDlg::onOk
566 // Purpose : SLOT called when "Ok" button pressed.
567 //=======================================================================
568 void VisuGUI_CubeAxesDlg::onOk()
569 {
570   if (onApply())
571     onClose();
572 }
573
574 //=======================================================================
575 // name    : VisuGUI_CubeAxesDlg::onClose
576 // Purpose : SLOT called when "Close" button pressed. Close dialog
577 //=======================================================================
578 void VisuGUI_CubeAxesDlg::onClose()
579 {
580   reject();
581 }
582
583 //=======================================================================
584 // name    : VisuGUI_CubeAxesDlg::onClose
585 // Purpose : SLOT called when "Close" button pressed. Close dialog
586 //=======================================================================
587 void VisuGUI_CubeAxesDlg::onWindowActivated (QWidget*)
588 {
589   SVTK_ViewWindow* aFrame = VISU::GetViewWindow();
590   if (aFrame)
591     Init();
592   else
593     onClose();
594 }