Salome HOME
Merge from OCC_development_generic_2006
[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
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    : SVTK_AxisWidget::AxisWg
58 // Purpose : 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 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 SVTK_FontWidget(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 SVTK_AxisWidget::~SVTK_AxisWidget()
147 {
148 }
149
150 void SVTK_AxisWidget::updateControlState()
151 {
152   onNameChecked();
153   onLabelsChecked();
154   onTicksChecked();
155 }
156
157 bool SVTK_AxisWidget::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    : SVTK_AxisWidget::onNameChecked
169 // Purpose :
170 //=======================================================================
171 void SVTK_AxisWidget::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    : SVTK_AxisWidget::onLabelsChecked
182 // Purpose :
183 //=======================================================================
184 void SVTK_AxisWidget::onLabelsChecked()
185 {
186   setEnabled(myLabelsGrp, myIsLabelsVisible->isChecked());
187 }
188
189 //=======================================================================
190 // Labels    : SVTK_AxisWidget::onTicksChecked
191 // Purpose :
192 //=======================================================================
193 void SVTK_AxisWidget::onTicksChecked()
194 {
195   setEnabled(myTicksGrp, myIsTicksVisible->isChecked());
196 }
197
198 //=======================================================================
199 // name    : SVTK_AxisWidget::onNameChecked
200 // Purpose :
201 //=======================================================================
202 void SVTK_AxisWidget::onNameChecked()
203 {
204   setEnabled(myNameGrp, myIsNameVisible->isChecked());
205 }
206
207 //=======================================================================
208 // name    : SVTK_AxisWidget::UseName
209 // Purpose :
210 //=======================================================================
211 void SVTK_AxisWidget::UseName(const bool toUse)
212 {
213   myIsNameVisible->setChecked(toUse);
214 }
215
216 //=======================================================================
217 // name    : SVTK_AxisWidget::SetName
218 // Purpose :
219 //================================================== =====================
220 void SVTK_AxisWidget::SetName(const QString& theName)
221 {
222   myAxisName->setText(theName);
223 }
224
225 //=======================================================================
226 // name    : SVTK_AxisWidget::SetNameFont
227 // Purpose :
228 //=======================================================================
229 void SVTK_AxisWidget::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    : SVTK_AxisWidget::SetNameFont
240 // Purpose :
241 //=======================================================================
242 bool SVTK_AxisWidget::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    : SVTK_CubeAxesDlg::Apply
315 // Purpose :
316 //=======================================================================
317 bool SVTK_AxisWidget::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       : SVTK_CubeAxesDlg
397   Description : Dialog for specifynig cube axes properties
398 */
399
400 //=======================================================================
401 // name    : SVTK_CubeAxesDlg::SVTK_CubeAxesDlg
402 // Purpose : Constructor
403 //=======================================================================
404 SVTK_CubeAxesDlg::SVTK_CubeAxesDlg(SVTK_MainWindow* theParent,
405                                    const char* theName,
406                                    QtxAction* theAction):
407   QDialog(theParent, 
408           theName, 
409           false,
410           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
411   myMainWindow(theParent),
412   myAction(theAction)
413 {
414   setCaption(tr("CAPTION"));
415
416   QVBoxLayout* aLay = new QVBoxLayout(this, 5, 5);
417   aLay->addWidget(createMainFrame(this));
418   aLay->addWidget(createButtonFrame(this));
419 }
420
421 //=======================================================================
422 // name    : SVTK_CubeAxesDlg::createMainFrame
423 // Purpose : Create frame containing dialog's input fields
424 //=======================================================================
425 QWidget* SVTK_CubeAxesDlg::createMainFrame(QWidget* theParent)
426 {
427   QFrame* aFrame = new QFrame(theParent);
428
429   myTabWg = new QTabWidget(aFrame);
430
431   myAxes[ 0 ] = new SVTK_AxisWidget(myTabWg);
432   myAxes[ 1 ] = new SVTK_AxisWidget(myTabWg);
433   myAxes[ 2 ] = new SVTK_AxisWidget(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    : SVTK_CubeAxesDlg::createButtonFrame
452 // Purpose : Create frame containing buttons
453 //=======================================================================
454 QWidget* SVTK_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    : SVTK_CubeAxesDlg::~SVTK_CubeAxesDlg
481 // Purpose : Destructor
482 //=======================================================================
483 SVTK_CubeAxesDlg::~SVTK_CubeAxesDlg()
484 {
485 }
486
487 //=======================================================================
488 // name    : SVTK_CubeAxesDlg::Update
489 // Purpose : Update dialog fields, connect signals and slots, show dialog
490 //=======================================================================
491 void SVTK_CubeAxesDlg::Update()
492 {
493   myActor = myMainWindow->GetCubeAxes();
494
495   myAxes[ 0 ]->ReadData(myActor->GetXAxisActor2D());
496   myAxes[ 1 ]->ReadData(myActor->GetYAxisActor2D());
497   myAxes[ 2 ]->ReadData(myActor->GetZAxisActor2D());
498
499   myIsVisible->setChecked(myActor->GetVisibility() ? true : false);
500 }
501
502 //=======================================================================
503 // name    : SVTK_CubeAxesDlg::isValid
504 // Purpose : Verify validity of entry data
505 //=======================================================================
506 bool SVTK_CubeAxesDlg::isValid() const
507 {
508   return true;
509 }
510
511 //=======================================================================
512 // name    : SVTK_CubeAxesDlg::onApply
513 // Purpose : Verify validity of entry data
514 //=======================================================================
515 bool SVTK_CubeAxesDlg::onApply()
516 {
517   bool isOk = true;
518
519   try
520   {
521     isOk = isOk && myAxes[ 0 ]->Apply(myActor->GetXAxisActor2D());
522     isOk = isOk && myAxes[ 1 ]->Apply(myActor->GetYAxisActor2D());
523     isOk = isOk && myAxes[ 2 ]->Apply(myActor->GetZAxisActor2D());
524
525
526     //myActor->SetXLabel(myAxes[ 0 ]->myAxisName->text());
527     //myActor->SetYLabel(myAxes[ 1 ]->myAxisName->text());
528     //myActor->SetZLabel(myAxes[ 2 ]->myAxisName->text());
529
530     //myActor->SetNumberOfLabels(myActor->GetXAxisActor2D()->GetNumberOfLabels());
531     if (myIsVisible->isChecked())
532       myActor->VisibilityOn();
533     else
534       myActor->VisibilityOff();
535
536     if (isOk)
537       myMainWindow->Repaint();
538   }
539   catch(...)
540   {
541     isOk = false;
542   }
543
544   return isOk;
545 }
546
547 //=======================================================================
548 // name    : SVTK_CubeAxesDlg::onOk
549 // Purpose : SLOT called when "Ok" button pressed.
550 //=======================================================================
551 void SVTK_CubeAxesDlg::onOk()
552 {
553   if (onApply())
554     onClose();
555 }
556
557 //=======================================================================
558 // name    : SVTK_CubeAxesDlg::onClose
559 // Purpose : SLOT called when "Close" button pressed. Close dialog
560 //=======================================================================
561 void SVTK_CubeAxesDlg::onClose()
562 {
563   reject();
564
565   myAction->setOn( false );
566 }
567
568 void SVTK_CubeAxesDlg::done( int r )
569 {
570   myAction->setOn( false );
571   QDialog::done( r );
572 }