Salome HOME
2fd7dcb8af63f47517d6ec1143c9eaad2b4b180b
[modules/gui.git] / src / SalomeApp / SalomeApp_StudyPropertiesDlg.cxx
1 // Copyright (C) 2007-2012  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
23 //  SALOME SalomeApp
24 //  File   : SalomeApp_StudyPropertiesDlg.cxx
25 //  Author : Roman NIKOLAEV
26
27 #include "SalomeApp_StudyPropertiesDlg.h"
28 #include "SalomeApp_Study.h"
29
30 #include "SUIT_Session.h"
31 #include <SUIT_Desktop.h>
32 #include <SUIT_MessageBox.h>
33
34 // CORBA Headers
35 #include <SALOMEconfig.h>
36 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
37
38 // QT Includes
39 #include <QPushButton>
40 #include <QGridLayout>
41 #include <QHBoxLayout>
42 #include <QFrame>
43 #include <QLineEdit>
44 #include <QLabel>
45 #include <QCheckBox>
46 #include <QComboBox>
47 #include <QTextEdit>
48 #include <QTreeWidget>
49 #include <QPushButton>
50
51 #include <iostream>
52  
53 #define  DEFAULT_MARGIN 11
54 #define  DEFAULT_SPACING 6
55 #define     SPACER_SIZE 5
56 #define MIN_LIST_WIDTH  300
57 #define MIN_LIST_HEIGHT 150
58
59
60 /*!Constructor. Initialize study properties dialog.*/
61 SalomeApp_StudyPropertiesDlg::SalomeApp_StudyPropertiesDlg(QWidget* parent)
62   : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
63     myIsChanged(false)
64 {
65   setModal( TRUE );
66
67   setWindowTitle(tr("TLT_STUDY_PROPERTIES"));
68   setSizeGripEnabled( true );
69
70   setWindowFlags( windowFlags() & ~Qt::WindowContextHelpButtonHint );
71
72   // Display study properties
73   SalomeApp_Study* study =
74     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
75   if (study)
76     myStudyDoc = study->studyDS();
77
78   //Author
79   QLabel* authorLbl = new QLabel(tr("PRP_AUTHOR"),this);
80   myAuthor = new QLineEdit(this);
81
82   //Creation date
83   QLabel* dateLbl = new QLabel(tr("PRP_DATE"),this);
84   myDate = new QLabel(this);
85
86   //Locked flag
87   myLocked = new QCheckBox(tr("PRP_LOCKED"), this);
88
89   //Modification flag
90   myModification = new QLabel(this);
91
92   //Units
93   QLabel* unitsLbl = new QLabel(tr("PRP_UNITS"),this);
94   myUnits = new QComboBox(this);
95
96   //Comment
97   QLabel* commentLabel = new QLabel(tr("PRP_COMMENT"),this);
98   myComment = new QTextEdit(this);
99   myComment->setMaximumHeight(80);
100
101   //Modifications
102   QLabel* modificationsLabel = new QLabel(tr("PRP_MODIFICATIONS"),this);  
103   myModifications = new QTreeWidget(this);
104   myModifications->setRootIsDecorated(false);
105   myModifications->setUniformRowHeights(true);
106   myModifications->setAllColumnsShowFocus(true);
107   myModifications->setColumnCount(2);
108   myModifications->setMaximumHeight(80);
109   QStringList columnNames;
110   columnNames.append(tr("PRP_AUTHOR"));
111   columnNames.append(tr("PRP_DATE_MODIF"));
112
113   QTreeWidgetItem * headerItem = new QTreeWidgetItem(columnNames);
114   myModifications->setHeaderItem ( headerItem );
115
116   QFrame* buttonFrame = new QFrame(this);
117
118   QHBoxLayout* horizontalLayout = new QHBoxLayout(buttonFrame);
119   
120   myOkButton = new QPushButton(tr("BUT_OK"), buttonFrame);
121   myCancelButton = new QPushButton(tr("BUT_CANCEL"), buttonFrame);
122
123   horizontalLayout->addWidget(myOkButton);
124   horizontalLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum) );
125   horizontalLayout->addWidget(myCancelButton);
126   
127   QGridLayout* gridLayout = new QGridLayout(this);
128   gridLayout->setMargin(DEFAULT_MARGIN);
129   gridLayout->setSpacing(DEFAULT_SPACING);
130   gridLayout->addWidget(authorLbl, 0, 0, 1, 1);
131   gridLayout->addWidget(myAuthor, 0, 1, 1, 1);
132   gridLayout->addWidget(dateLbl, 1, 0, 1, 1);
133   gridLayout->addWidget(myDate, 1, 1, 1, 1);
134   gridLayout->addWidget(myLocked, 2, 0, 1, 1);
135   gridLayout->addWidget(myModification, 2, 1, 1, 1);
136   gridLayout->addWidget(unitsLbl, 3, 0, 1, 1);
137   gridLayout->addWidget(myUnits, 3, 1, 1, 1);
138   gridLayout->addItem(new QSpacerItem(/*72*/0, 0/*20*/, QSizePolicy::Expanding, QSizePolicy::Minimum), 3, 2, 1, 1);
139   gridLayout->addWidget(commentLabel, 4, 0, 1, 1);
140   gridLayout->addWidget(myComment, 4, 1, 1, 2);
141   gridLayout->addWidget(modificationsLabel, 5, 0, 1, 1);
142   gridLayout->addWidget(myModifications, 5, 1, 1, 2);
143   gridLayout->addWidget(buttonFrame, 6, 0, 1, 3);
144
145   connect(myOkButton,     SIGNAL(clicked()), this, SLOT(clickOnOk()));
146   connect(myCancelButton, SIGNAL(clicked()), this, SLOT(reject()));
147   
148   initData();
149 }
150
151 /*!
152   Destructor.
153 */
154 SalomeApp_StudyPropertiesDlg::~SalomeApp_StudyPropertiesDlg()
155 {
156 }
157
158 /*!
159   Data initializetion for dialog.(Study author's name, date of creation etc.)
160 */
161 void SalomeApp_StudyPropertiesDlg::initData()
162 {
163   bool hasData = myStudyDoc;
164   _PTR(AttributeStudyProperties) propAttr;
165   if (hasData)
166     propAttr = myStudyDoc->GetProperties();
167   hasData = hasData && propAttr;
168   
169   if (hasData) {
170     //Creator and creation date
171     myAuthor->setText(propAttr->GetUserName().c_str());
172     int minutes, hours, day, month, year;
173     if (propAttr->GetCreationDate(minutes, hours, day, month, year)) {
174       QString strDate;
175       strDate.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", day, month, year, hours, minutes);
176       myDate->setText(strDate);
177     }
178     
179     //Is Locked?
180     myLocked->setChecked(propAttr->IsLocked());
181
182     //Is Modified?
183     bool isModified = false;
184     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>
185       (SUIT_Session::session()->activeApplication()->activeStudy());
186     if (study) {
187       isModified = study->isModified();
188       if (hasData) {
189         if (propAttr->IsModified() != isModified) {
190           propAttr->SetModified((int)isModified);
191         }
192       }
193     }
194
195     QString modif = propAttr->IsModified() ? tr( "PRP_MODIFIED" ) : tr( "PRP_NOT_MODIFIED" );
196     myModification->setText(modif);
197     
198     //Units
199     QStringList theList;
200     theList <<""<<"mm"<<"cm"<<"m"<<"km"<<"inch";
201     myUnits->insertItems(0,theList); 
202
203     QString anUnit = propAttr->GetUnits().c_str();
204
205     int aFound = -1;
206     for (int i = 0; i < myUnits->count(); i++) {
207       if ( myUnits->itemText(i) == anUnit ) {
208         aFound = i;
209         break;
210       }
211     }
212     
213     if (aFound >= 0) {
214       myUnits->setCurrentIndex(aFound);
215     }
216     
217     //Comments
218     myComment->setPlainText(propAttr->GetComment().c_str());
219     
220     //Modifications
221     std::vector<std::string> aUsers;
222     std::vector<int>  aMins, aHours, aDays, aMonths, aYears;
223     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
224     int aCnt = aUsers.size();
225     for ( int i = 0; i < aCnt; i++ ) {
226       QString date;
227       date.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
228                   aDays  [i],
229                   aMonths[i],
230                   aYears [i],
231                   aHours [i],
232                   aMins  [i]);
233       QStringList aList;
234       aList.append(aUsers[i].c_str());
235       aList.append(date);
236       QTreeWidgetItem* item = new  QTreeWidgetItem(aList);
237       myModifications->addTopLevelItem(item);
238     }        
239   }
240   adjustSize();
241 }
242
243 /*!
244   Fill properties attributes.
245 */
246 void SalomeApp_StudyPropertiesDlg::clickOnOk()
247 {
248   _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
249   //Firstly, store locked flag
250   if(propAttr) {
251     bool bLocked = myLocked->isChecked();
252     if (propAttr->IsLocked() != bLocked) {
253       propAttr->SetLocked(bLocked);
254       myIsChanged = true;
255     }
256     
257     bool needWarning = false;
258     
259     //Author
260     if (QString(propAttr->GetUserName().c_str()) != myAuthor->text().trimmed()) {
261       if(!propAttr->IsLocked()) {
262         propAttr->SetUserName(myAuthor->text().trimmed().toStdString());
263         myIsChanged = true;
264       } else {
265         needWarning = true;
266       }
267     }
268
269     //Unit
270     if (QString(propAttr->GetUnits().c_str()) != myUnits->currentText()) {
271       if(!propAttr->IsLocked()) {
272         propAttr->SetUnits(myUnits->currentText().toStdString());
273         myIsChanged = true;
274       } else {
275         needWarning = true;
276       }
277     }
278       
279     //Comment      
280     if (QString(propAttr->GetComment().c_str()) != myComment->toPlainText()) {
281       if(!propAttr->IsLocked()) {
282         propAttr->SetComment(myComment->toPlainText().toStdString());
283         myIsChanged = true;
284       } else {
285         needWarning = true;
286       }
287     }
288     
289     if(needWarning) {
290       SUIT_MessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(),
291                                QObject::tr("WRN_WARNING"),
292                                QObject::tr("WRN_STUDY_LOCKED") );
293     }
294     
295     accept();
296   }
297 }