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