Salome HOME
New Help for KERNEL & GUI.
[modules/gui.git] / src / SalomeApp / SalomeApp_StudyPropertiesDlg.cxx
1 //  SALOME SalomeApp
2 //
3 //  Copyright (C) 2005  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SalomeApp_StudyPropertiesDlg.cxx
8 //  Author : Sergey ANIKIN
9 //  Module : SALOME
10 //  $Header$
11
12 #include "SalomeApp_StudyPropertiesDlg.h"
13 #include "SalomeApp_ListView.h"
14 #include "SalomeApp_Study.h"
15
16 #include "SUIT_Session.h"
17 #include <SUIT_Desktop.h>
18 #include <SUIT_MessageBox.h>
19
20 // OCCT Includes
21 #include <OSD_Process.hxx>
22 #include <Quantity_Date.hxx>
23
24 // CORBA Headers
25 #include <SALOMEconfig.h>
26 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
27
28 // QT Includes
29 #include <qpushbutton.h>
30 #include <qlayout.h>
31
32 using namespace std;
33
34 #define  DEFAULT_MARGIN 11
35 #define DEFAULT_SPACING 6
36 #define     SPACER_SIZE 5
37 #define MIN_LIST_WIDTH  300
38 #define MIN_LIST_HEIGHT 150
39
40 class SalomeApp_PropItem : public SalomeApp_ListViewItem
41 {
42 public:
43   /*! constructor  */
44   SalomeApp_PropItem(SalomeApp_ListView* parent,
45                      const QString       theName,
46                      const bool          theEditable,
47                      const int           theUserType) :
48   SalomeApp_ListViewItem( parent, theName, theEditable )
49   {
50     setUserType(theUserType);
51   }
52   /*! constructor */
53   SalomeApp_PropItem(SalomeApp_ListView*     parent,
54                      SalomeApp_ListViewItem* after,
55                      const QString     theName,
56                      const bool        theEditable,
57                      const int         theUserType) :
58   SalomeApp_ListViewItem( parent, after, theName, theEditable )
59   {
60     setUserType(theUserType);
61   }
62   /*! fills widget with initial values (list or single value)*/
63   void fillWidgetWithValues( SalomeApp_EntityEdit* theWidget )
64   {
65     QStringList list;
66     switch(getUserType()) {
67     //case SalomeApp_StudyPropertiesDlg::prpModeId:
68     //  {
69     //    list << SalomeApp_StudyPropertiesDlg::tr("PRP_MODE_FROM_SCRATCH") <<
70     //        SalomeApp_StudyPropertiesDlg::tr("PRP_MODE_FROM_COPYFROM");
71     //    theWidget->insertList(list);
72     //    break;
73     //  }
74     case SalomeApp_StudyPropertiesDlg::prpLockedId:
75       {
76         list << SalomeApp_StudyPropertiesDlg::tr( "PRP_NO" ) << SalomeApp_StudyPropertiesDlg::tr( "PRP_YES" );
77         theWidget->insertList(list, getValue() == SalomeApp_StudyPropertiesDlg::tr( "PRP_NO" ) ? 0 : 1 );
78         break;
79       }
80     case SalomeApp_StudyPropertiesDlg::prpModificationsId:
81       {
82         SalomeApp_Study* study =
83           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
84         if (study) {
85           _PTR(Study) studyDoc = study->studyDS();
86           _PTR(AttributeStudyProperties) propAttr;
87           if ( studyDoc ) {
88             propAttr = studyDoc->GetProperties();
89             if ( propAttr ) {
90               std::vector<std::string> aUsers;
91               std::vector<int>  aMins, aHours, aDays, aMonths, aYears;
92               propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
93               int aCnt = aUsers.size();
94               for ( int i = 0; i < aCnt; i++ ) {
95                 QString val;
96                 val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
97                             aDays  [i],
98                             aMonths[i],
99                             aYears [i],
100                             aHours [i],
101                             aMins  [i]);
102                 val = val + " : " + QString( aUsers[i].c_str() );
103                 list.prepend(val);
104               }
105               theWidget->setDuplicatesEnabled(true);
106               theWidget->insertList(list);
107             }
108           }
109         }
110         break;
111       }
112     default:
113       {
114         SalomeApp_ListViewItem::fillWidgetWithValues(theWidget);
115         break;
116       }
117     }
118   }
119   /*! finishes editing of entity */
120   virtual UpdateType finishEditing( SalomeApp_EntityEdit* theWidget ) {
121     if ( getUserType() == SalomeApp_StudyPropertiesDlg::prpModificationsId )
122       return utCancel;
123     else
124       return SalomeApp_ListViewItem::finishEditing(theWidget);
125   }
126 };
127
128 /*!Constructor. Initialize study properties dialog.*/
129 SalomeApp_StudyPropertiesDlg::SalomeApp_StudyPropertiesDlg(QWidget* parent)
130      : QDialog(parent, "", TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
131        myChanged( false )
132 {
133   setCaption(tr("TLT_STUDY_PROPERTIES"));
134   setSizeGripEnabled( true );
135
136   clearWFlags(Qt::WStyle_ContextHelp);
137
138   QGridLayout* mainLayout = new QGridLayout(this);
139   mainLayout->setMargin(DEFAULT_MARGIN);
140   mainLayout->setSpacing(DEFAULT_SPACING);
141
142   myPropList = new SalomeApp_ListView(this);
143   myPropList->addColumn("");
144   myPropList->addColumn("");
145   myPropList->enableEditing(TRUE);
146   myPropList->setMinimumSize(MIN_LIST_WIDTH, MIN_LIST_HEIGHT);
147   mainLayout->addMultiCellWidget(myPropList, 0, 0, 0, 2);
148
149   myOKBtn = new QPushButton(tr("BUT_OK"), this);
150   mainLayout->addWidget(myOKBtn, 1, 0);
151
152   myCancelBtn = new QPushButton(tr("BUT_CANCEL"), this);
153   mainLayout->addWidget(myCancelBtn, 1, 2);
154
155   QSpacerItem* spacer1 =
156     new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum);
157   mainLayout->addItem(spacer1, 1, 1);
158
159   // Display study properties
160   SalomeApp_Study* study =
161     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
162   if (study)
163     myStudyDoc = study->studyDS();
164
165   initData();
166
167   connect(myOKBtn,     SIGNAL(clicked()), this, SLOT(onOK()));
168   connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
169 }
170
171 /*!
172   Destructor.
173 */
174 SalomeApp_StudyPropertiesDlg::~SalomeApp_StudyPropertiesDlg()
175 {
176 }
177
178 /*!
179   Data initializetion for dialog.(Study author's name, date of creation etc.)
180 */
181 void SalomeApp_StudyPropertiesDlg::initData()
182 {
183   bool hasData = myStudyDoc;
184   _PTR(AttributeStudyProperties) propAttr;
185   if (hasData)
186     propAttr = myStudyDoc->GetProperties();
187   hasData = hasData && propAttr;
188
189   // Study author's name
190   SalomeApp_PropItem* item = new SalomeApp_PropItem(myPropList, tr("PRP_AUTHOR")+":", true, prpAuthorId);
191   if (hasData)
192     item->setValue(propAttr->GetUserName().c_str());
193
194   // Date of creation
195   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_DATE")+":", false, prpDateId);
196   if (hasData) {
197     int minutes, hours, day, month, year;
198     if (propAttr->GetCreationDate(minutes, hours, day, month, year)) {
199       QString strDate;
200       strDate.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", day, month, year, hours, minutes);
201       item->setValue(strDate);
202     }
203   }
204
205   // Creation mode
206 //  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODE")+":", true, prpModeId);
207 //  item->setEditingType( SalomeApp_EntityEdit::etComboBox);
208 //  if (hasData) item->setValue(propAttr->GetCreationMode());
209
210   // Locked or not
211   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_LOCKED")+":", true, prpLockedId);
212   item->setEditingType( SalomeApp_EntityEdit::etComboBox);
213   if ( hasData )
214     item->setValue( tr( propAttr->IsLocked() ? "PRP_YES" : "PRP_NO" ) );
215
216   // Saved or not
217   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFIED")+":", false, prpSavedId);
218   bool isModified = false;
219   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>
220     (SUIT_Session::session()->activeApplication()->activeStudy());
221   if (study) {
222     isModified = study->isModified();
223     if (hasData) {
224       if (propAttr->IsModified() != isModified) {
225         propAttr->SetModified((int)isModified);
226       }
227     }
228   }
229   if (hasData) {
230     if (propAttr->IsModified())
231       item->setValue( tr( "PRP_YES" ) );
232     else
233       item->setValue( tr( "PRP_NO" ) );
234   }
235
236   // Modifications list
237   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFICATIONS")+":", true, prpModificationsId);
238   item->setEditingType( SalomeApp_EntityEdit::etComboBox);
239   if (hasData) {
240     std::vector<std::string> aUsers;
241     std::vector<int> aMins, aHours, aDays, aMonths, aYears;
242     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
243     int aLast = aUsers.size()-1;
244     if (aLast >= 0) {
245       QString val;
246       val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
247                   aDays  [aLast],
248                   aMonths[aLast],
249                   aYears [aLast],
250                   aHours [aLast],
251                   aMins  [aLast]);
252       val = val + " : " + QString(aUsers[aUsers.size()-1].c_str());
253       item->setValue(val);
254     }
255   }
256
257   myOKBtn->setEnabled(hasData);
258 }
259
260 /*!
261   accept data. Return true.
262 */
263 bool SalomeApp_StudyPropertiesDlg::acceptData()
264 {
265   return TRUE;
266 }
267
268 /*!
269   Fill properties attributes.
270 */
271 void SalomeApp_StudyPropertiesDlg::onOK()
272 {
273   myPropList->accept();
274
275   if (acceptData()) {
276     _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
277     //myChanged = propChanged();
278     if ( propAttr /*&& myChanged*/ ) {
279       QListViewItemIterator it( myPropList );
280       // iterate through all items of the listview
281       for ( ; it.current(); ++it ) {
282         SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
283         switch (item->getUserType()) {
284         case prpAuthorId:
285           if (QString(propAttr->GetUserName().c_str()) != item->getValue().stripWhiteSpace()) {
286             if (!propAttr->IsLocked()) {
287               propAttr->SetUserName(item->getValue().stripWhiteSpace().latin1());
288               myChanged = true;
289             } else {
290               SUIT_MessageBox::warn1(SUIT_Session::session()->activeApplication()->desktop(),
291                                      QObject::tr("WRN_WARNING"),
292                                      QObject::tr("WRN_STUDY_LOCKED"),
293                                      QObject::tr("BUT_OK"));
294             }
295           }
296           break;
297         //case prpModeId:
298         //  propAttr->SetCreationMode(item->getValue().stripWhiteSpace().latin1());
299         //  break;
300         case prpLockedId:
301           {
302             bool bLocked = item->getValue().compare(tr("PRP_YES")) == 0;
303             if (propAttr->IsLocked() != bLocked) {
304               propAttr->SetLocked(bLocked);
305               myChanged = true;
306             }
307           }
308           break;
309         default:
310           break;
311         }
312       }
313     }
314     accept();
315   }
316 }
317
318 /*!
319   Check is properties changed?
320 */
321 bool SalomeApp_StudyPropertiesDlg::propChanged()
322 {
323   _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
324   if (propAttr) {
325     QListViewItemIterator it (myPropList);
326     // iterate through all items of the listview
327     for (; it.current(); ++it) {
328       SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
329       switch (item->getUserType()) {
330       case prpAuthorId:
331         if ( QString( propAttr->GetUserName().c_str() ) != item->getValue().stripWhiteSpace() ) {
332           return true;
333         }
334         break;
335       //case prpModeId:
336       //  if ( QString( propAttr->GetCreationMode().c_str() ) != item->getValue().stripWhiteSpace() ) {
337       //    return true;
338       //  }
339       //  break;
340       case prpLockedId:
341         {
342           bool bLocked = item->getValue().compare( tr( "PRP_YES" ) ) == 0;
343           if ( propAttr->IsLocked() != bLocked ) {
344             return true;
345           }
346           break;
347         }
348       default:
349         break;
350       }
351     }
352   }
353   return false;
354 }