Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / CurveCreator / CurveCreator_UndoOptsDlg.cxx
1 // Copyright (C) 2013-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        CurveCreator_UndoOptsDlg.cxx
21 // Author:      Sergey KHROMOV
22
23 #include "CurveCreator_UndoOptsDlg.h"
24
25 #include <QButtonGroup>
26 #include <QGridLayout>
27 #include <QGroupBox>
28 #include <QIntValidator>
29 #include <QLineEdit>
30 #include <QPushButton>
31 #include <QRadioButton>
32
33 #define UNDO_DEPTH_UNLIMITED  0
34 #define UNDO_DEPTH_DISABLED   1
35 #define UNDO_DEPTH_FIX_SIZE   2
36
37 //=======================================================================
38 // function: Constructor
39 // purpose:
40 //=======================================================================
41 CurveCreator_UndoOptsDlg::CurveCreator_UndoOptsDlg(QWidget* parent)
42   : QDialog          (parent),
43     myUndoDepth      (UNDO_DEPTH_UNLIMITED),
44     myOptsBtnGrp     (NULL),
45     myBufferSizeEdit (NULL),
46     myOkBtn          (NULL),
47     myCancelBtn      (NULL)
48 {
49   setWindowTitle(tr("CC_UNDO_OPTIONS_TITLE"));
50
51   // Set Undo/Redo options group
52   QGroupBox    *anUndoOptsGrp  =
53             new QGroupBox(tr("CC_UNDO_OPTIONS_MODIFY"));
54   QGridLayout  *anUndoOptsLO   = new QGridLayout(anUndoOptsGrp);
55   QRadioButton *aDisabledRdBtn =
56             new QRadioButton(tr("CC_UNDO_OPTIONS_DISABLED"), anUndoOptsGrp);
57   QRadioButton *aFixSizeRdBtn  =
58             new QRadioButton(tr("CC_UNDO_OPTIONS_FIXED_SIZE"), anUndoOptsGrp);
59   QRadioButton *anUnlimRdBtn   =
60             new QRadioButton(tr("CC_UNDO_OPTIONS_UNLIMITED"), anUndoOptsGrp);
61
62   myOptsBtnGrp     = new QButtonGroup(anUndoOptsGrp);
63   myBufferSizeEdit = new QLineEdit(anUndoOptsGrp);
64   anUndoOptsLO->setMargin(9);
65   anUndoOptsLO->setSpacing(6);
66   anUndoOptsLO->addWidget(aDisabledRdBtn,   0, 0);
67   anUndoOptsLO->addWidget(aFixSizeRdBtn,    1, 0);
68   anUndoOptsLO->addWidget(anUnlimRdBtn,     2, 0);
69   anUndoOptsLO->addWidget(myBufferSizeEdit, 1, 1);
70   myOptsBtnGrp->addButton(anUnlimRdBtn,   UNDO_DEPTH_UNLIMITED);
71   myOptsBtnGrp->addButton(aDisabledRdBtn, UNDO_DEPTH_DISABLED);
72   myOptsBtnGrp->addButton(aFixSizeRdBtn,  UNDO_DEPTH_FIX_SIZE);
73
74   // Set OK/Cancel buttons group
75   QGroupBox   *anOkCancelGrp  = new QGroupBox;
76   QGridLayout *anOkCancelLO   = new QGridLayout(anOkCancelGrp);
77
78   myOkBtn     = new QPushButton(tr("GEOM_BUT_OK"), anOkCancelGrp);
79   myCancelBtn = new QPushButton(tr("GEOM_BUT_CANCEL"), anOkCancelGrp);
80   anOkCancelLO->setMargin(9);
81   anOkCancelLO->setSpacing(6);
82   anOkCancelLO->addWidget(myOkBtn,     0, 0);
83   anOkCancelLO->addWidget(myCancelBtn, 0, 1);
84
85   // Set main group
86   QGroupBox   *aMainGrp = new QGroupBox;
87   QVBoxLayout *aMainLO = new QVBoxLayout(aMainGrp);
88
89   aMainLO->addWidget(anUndoOptsGrp);
90   aMainLO->addWidget(anOkCancelGrp);
91
92   setLayout(aMainLO);
93
94   init();
95 }
96
97 //=======================================================================
98 // function: Destructor
99 // purpose:
100 //=======================================================================
101 CurveCreator_UndoOptsDlg::~CurveCreator_UndoOptsDlg()
102 {
103 }
104
105 //=======================================================================
106 // function: setUndoDepth
107 // purpose:
108 //=======================================================================
109 void CurveCreator_UndoOptsDlg::setUndoDepth(const int theDepth)
110 {
111   myUndoDepth = theDepth;
112
113   const int aDepthId = myUndoDepth + 1;
114   int       anId     = UNDO_DEPTH_FIX_SIZE;
115
116   if (aDepthId == UNDO_DEPTH_UNLIMITED ||
117       aDepthId == UNDO_DEPTH_DISABLED) {
118     anId = aDepthId;
119   } else if (myUndoDepth > 0) {
120     myBufferSizeEdit->setText(QString::number(myUndoDepth));
121   }
122
123   myOptsBtnGrp->button(anId)->setChecked(true);
124   optionChanged(anId);
125 }
126
127 //=======================================================================
128 // function: getUndoDepth
129 // purpose:
130 //=======================================================================
131 int CurveCreator_UndoOptsDlg::getUndoDepth() const
132 {
133   return myUndoDepth;
134 }
135
136 //=======================================================================
137 // function: isEnabled
138 // purpose:
139 //=======================================================================
140 bool CurveCreator_UndoOptsDlg::isEnabled() const
141 {
142   return (myUndoDepth + 1 != UNDO_DEPTH_DISABLED);
143 }
144
145 //=======================================================================
146 // function: isUnlimited
147 // purpose:
148 //=======================================================================
149 bool CurveCreator_UndoOptsDlg::isUnlimited() const
150 {
151   return (myUndoDepth + 1 == UNDO_DEPTH_UNLIMITED);
152 }
153
154 //=======================================================================
155 // function: init
156 // purpose:
157 //=======================================================================
158 void CurveCreator_UndoOptsDlg::init()
159 {
160   // Initialize sections group.
161   myOptsBtnGrp->setExclusive(true);
162   myOptsBtnGrp->button(UNDO_DEPTH_UNLIMITED)->setChecked(true);
163   connect(myOptsBtnGrp, SIGNAL(buttonClicked(int)),
164           this, SLOT(optionChanged(int)));
165
166   // Initialize line edit.
167   QIntValidator *aValidator = new QIntValidator(myBufferSizeEdit);
168
169   aValidator->setBottom(1);
170   myBufferSizeEdit->setValidator(aValidator);
171   optionChanged(UNDO_DEPTH_UNLIMITED);
172
173   // Init buttons.
174   myOkBtn->setDefault(true);
175
176   connect(myOkBtn,     SIGNAL(clicked()), this, SLOT(accept()));
177   connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
178
179   setTabOrder();
180 }
181
182 //=======================================================================
183 // function: setTabOrder
184 // purpose:
185 //=======================================================================
186 void CurveCreator_UndoOptsDlg::setTabOrder()
187 {
188   QWidget::setTabOrder(myOptsBtnGrp->button(UNDO_DEPTH_DISABLED),
189                        myOptsBtnGrp->button(UNDO_DEPTH_FIX_SIZE));
190   QWidget::setTabOrder(myOptsBtnGrp->button(UNDO_DEPTH_FIX_SIZE),
191                        myBufferSizeEdit);
192   QWidget::setTabOrder(myBufferSizeEdit,
193                        myOptsBtnGrp->button(UNDO_DEPTH_UNLIMITED));
194   QWidget::setTabOrder(myOptsBtnGrp->button(UNDO_DEPTH_UNLIMITED), myOkBtn);
195   QWidget::setTabOrder(myOkBtn, myCancelBtn);
196 }
197
198 //=======================================================================
199 // function: optionChanged
200 // purpose:
201 //=======================================================================
202 void CurveCreator_UndoOptsDlg::optionChanged(int theId)
203 {
204   switch (theId) {
205     case UNDO_DEPTH_FIX_SIZE:
206       myBufferSizeEdit->setEnabled(true);
207       break;
208     case UNDO_DEPTH_UNLIMITED:
209     case UNDO_DEPTH_DISABLED:
210     default:
211       myBufferSizeEdit->setEnabled(false);
212       break;
213   }
214 }
215
216 //=======================================================================
217 // function: accept
218 // purpose:
219 //=======================================================================
220 void CurveCreator_UndoOptsDlg::accept()
221 {
222   const int anId = myOptsBtnGrp->checkedId();
223
224   switch (anId) {
225     case UNDO_DEPTH_FIX_SIZE:
226       myUndoDepth = myBufferSizeEdit->text().toInt();
227       break;
228     case UNDO_DEPTH_UNLIMITED:
229     case UNDO_DEPTH_DISABLED:
230       myUndoDepth = anId - 1;
231       break;
232     default:
233       break;
234   }
235
236   QDialog::accept();
237 }