Salome HOME
Revert "Synchronize adm files"
[modules/gui.git] / src / SalomeApp / SalomeApp_StudyPropertiesDlg.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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   myModifications->setHeaderLabels( columnNames );
113
114   //Component versions
115   QLabel* versionsLabel = new QLabel(tr("PRP_VERSIONS"),this);  
116   myVersions = new QTreeWidget(this);
117   myVersions->setRootIsDecorated(false);
118   myVersions->setUniformRowHeights(true);
119   myVersions->setAllColumnsShowFocus(true);
120   myVersions->setColumnCount(2);
121   myVersions->setMaximumHeight(80);
122   columnNames.clear();
123   columnNames.append(tr("PRP_COMPONENT"));
124   columnNames.append(tr("PRP_VERSION"));
125   myVersions->setHeaderLabels( columnNames );
126
127   QFrame* buttonFrame = new QFrame(this);
128
129   QHBoxLayout* horizontalLayout = new QHBoxLayout(buttonFrame);
130   
131   myOkButton = new QPushButton(tr("BUT_OK"), buttonFrame);
132   myCancelButton = new QPushButton(tr("BUT_CANCEL"), buttonFrame);
133
134   horizontalLayout->addWidget(myOkButton);
135   horizontalLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum) );
136   horizontalLayout->addWidget(myCancelButton);
137   
138   QGridLayout* gridLayout = new QGridLayout(this);
139   gridLayout->setMargin(DEFAULT_MARGIN);
140   gridLayout->setSpacing(DEFAULT_SPACING);
141   gridLayout->addWidget(authorLbl, 0, 0, 1, 1);
142   gridLayout->addWidget(myAuthor, 0, 1, 1, 1);
143   gridLayout->addWidget(dateLbl, 1, 0, 1, 1);
144   gridLayout->addWidget(myDate, 1, 1, 1, 1);
145   gridLayout->addWidget(myLocked, 2, 0, 1, 1);
146   gridLayout->addWidget(myModification, 2, 1, 1, 1);
147   gridLayout->addWidget(unitsLbl, 3, 0, 1, 1);
148   gridLayout->addWidget(myUnits, 3, 1, 1, 1);
149   gridLayout->addItem(new QSpacerItem(/*72*/0, 0/*20*/, QSizePolicy::Expanding, QSizePolicy::Minimum), 3, 2, 1, 1);
150   gridLayout->addWidget(commentLabel, 4, 0, 1, 1);
151   gridLayout->addWidget(myComment, 4, 1, 1, 2);
152   gridLayout->addWidget(modificationsLabel, 5, 0, 1, 1);
153   gridLayout->addWidget(myModifications, 5, 1, 1, 2);
154   gridLayout->addWidget(versionsLabel, 6, 0, 1, 1);
155   gridLayout->addWidget(myVersions, 6, 1, 1, 2);
156   gridLayout->addWidget(buttonFrame, 7, 0, 1, 3);
157
158   connect(myOkButton,     SIGNAL(clicked()), this, SLOT(clickOnOk()));
159   connect(myCancelButton, SIGNAL(clicked()), this, SLOT(reject()));
160   
161   initData();
162 }
163
164 /*!
165   Destructor.
166 */
167 SalomeApp_StudyPropertiesDlg::~SalomeApp_StudyPropertiesDlg()
168 {
169 }
170
171 /*!
172   Data initializetion for dialog.(Study author's name, date of creation etc.)
173 */
174 void SalomeApp_StudyPropertiesDlg::initData()
175 {
176   bool hasData = myStudyDoc;
177   _PTR(AttributeStudyProperties) propAttr;
178   if (hasData)
179     propAttr = myStudyDoc->GetProperties();
180   hasData = hasData && propAttr;
181   
182   if (hasData) {
183     //Creator and creation date
184     myAuthor->setText(propAttr->GetUserName().c_str());
185     int minutes, hours, day, month, year;
186     if (propAttr->GetCreationDate(minutes, hours, day, month, year)) {
187       QString strDate;
188       strDate.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", day, month, year, hours, minutes);
189       myDate->setText(strDate);
190     }
191     
192     //Is Locked?
193     myLocked->setChecked(propAttr->IsLocked());
194
195     //Is Modified?
196     bool isModified = false;
197     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>
198       (SUIT_Session::session()->activeApplication()->activeStudy());
199     isModified = study->isModified();
200     if (propAttr->IsModified() != isModified) {
201       propAttr->SetModified((int)isModified);
202     }
203     
204     QString modif = propAttr->IsModified() ? tr( "PRP_MODIFIED" ) : tr( "PRP_NOT_MODIFIED" );
205     myModification->setText(modif);
206     
207     //Units
208     QStringList theList;
209     theList <<""<<"mm"<<"cm"<<"m"<<"km"<<"inch";
210     myUnits->insertItems(0,theList); 
211
212     QString anUnit = propAttr->GetUnits().c_str();
213
214     int aFound = -1;
215     for (int i = 0; i < myUnits->count(); i++) {
216       if ( myUnits->itemText(i) == anUnit ) {
217         aFound = i;
218         break;
219       }
220     }
221     
222     if (aFound >= 0) {
223       myUnits->setCurrentIndex(aFound);
224     }
225     
226     //Comments
227     myComment->setPlainText(propAttr->GetComment().c_str());
228     
229     //Modifications
230     std::vector<std::string> aUsers;
231     std::vector<int>  aMins, aHours, aDays, aMonths, aYears;
232     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
233     int aCnt = aUsers.size();
234     for ( int i = 0; i < aCnt; i++ ) {
235       QString date;
236       date.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
237                   aDays  [i],
238                   aMonths[i],
239                   aYears [i],
240                   aHours [i],
241                   aMins  [i]);
242       QStringList aList;
243       aList.append(aUsers[i].c_str());
244       aList.append(date);
245       QTreeWidgetItem* item = new  QTreeWidgetItem(aList);
246       myModifications->addTopLevelItem(item);
247     }        
248   
249     //Component versions
250     std::vector<std::string> aComponents = propAttr->GetStoredComponents();
251     for ( int i = 0; i < aComponents.size(); i++ ) {
252       std::vector<int>  aMins, aHours, aDays, aMonths, aYears;
253       std::vector<std::string> aVersions = propAttr->GetComponentVersions(aComponents[i]);
254       QStringList aData;
255       aData.append( aComponents[i].c_str() );
256       aData.append( aVersions.empty() || aVersions[0] == "" ? "unknown" : aVersions[0].c_str() );
257       QTreeWidgetItem* item = new QTreeWidgetItem( aData );
258       if ( aVersions.size() > 1 )
259         item->setForeground( 1, Qt::red );
260       myVersions->addTopLevelItem(item);
261     }
262   }
263   adjustSize();
264 }
265
266 /*!
267   Fill properties attributes.
268 */
269 void SalomeApp_StudyPropertiesDlg::clickOnOk()
270 {
271   _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
272   //Firstly, store locked flag
273   if(propAttr) {
274     bool bLocked = myLocked->isChecked();
275     if (propAttr->IsLocked() != bLocked) {
276       propAttr->SetLocked(bLocked);
277       myIsChanged = true;
278     }
279     
280     bool needWarning = false;
281     
282     //Author
283     if (QString(propAttr->GetUserName().c_str()) != myAuthor->text().trimmed()) {
284       if(!propAttr->IsLocked()) {
285         propAttr->SetUserName(myAuthor->text().trimmed().toStdString());
286         myIsChanged = true;
287       } else {
288         needWarning = true;
289       }
290     }
291
292     //Unit
293     if (QString(propAttr->GetUnits().c_str()) != myUnits->currentText()) {
294       if(!propAttr->IsLocked()) {
295         propAttr->SetUnits(myUnits->currentText().toStdString());
296         myIsChanged = true;
297       } else {
298         needWarning = true;
299       }
300     }
301       
302     //Comment      
303     if (QString(propAttr->GetComment().c_str()) != myComment->toPlainText()) {
304       if(!propAttr->IsLocked()) {
305         propAttr->SetComment(myComment->toPlainText().toStdString());
306         myIsChanged = true;
307       } else {
308         needWarning = true;
309       }
310     }
311     
312     if(needWarning) {
313       SUIT_MessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(),
314                                QObject::tr("WRN_WARNING"),
315                                QObject::tr("WRN_STUDY_LOCKED") );
316     }
317     
318     accept();
319   }
320 }