Salome HOME
sources v1.2
[modules/kernel.git] / src / SALOMEGUI / SALOMEGUI_StudyPropertiesDlg.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOMEGUI_StudyPropertiesDlg.cxx
8 //  Author : Sergey ANIKIN
9 //  Module : SALOME
10 //  $Header$
11
12 using namespace std;
13 #include "SALOMEGUI_StudyPropertiesDlg.h"
14
15 #include "QAD_Desktop.h"
16 #include "QAD_ListView.h"
17
18 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
19
20 #include <qpushbutton.h>
21 #include <qlayout.h>
22
23 #define  DEFAULT_MARGIN 11
24 #define DEFAULT_SPACING 6
25 #define     SPACER_SIZE 5
26 #define MIN_LIST_WIDTH  300
27 #define MIN_LIST_HEIGHT 150
28
29 class SALOMEGUI_PropItem : public QAD_ListViewItem
30 {
31 public:
32 // constructor  
33   SALOMEGUI_PropItem(QAD_ListView*     parent,
34                      const QString     theName,
35                      const bool        theEditable,
36                      const int         theUserType) :
37   QAD_ListViewItem(parent, theName, theEditable)
38   {
39     setUserType(theUserType);
40   }
41 // constructor
42   SALOMEGUI_PropItem(QAD_ListView*     parent,
43                      QAD_ListViewItem* after,
44                      const QString     theName,
45                      const bool        theEditable,
46                      const int         theUserType) :
47   QAD_ListViewItem(parent, after, theName, theEditable)
48   {
49     setUserType(theUserType);
50   }
51 // fills widget with initial values (list or single value)
52   void fillWidgetWithValues(QAD_EntityEdit* theWidget)
53   {
54     QStringList list;
55     switch(getUserType()) {
56     case SALOMEGUI_StudyPropertiesDlg::prpModeId:
57       {
58         list << QObject::tr("PRP_MODE_FROM_SCRATCH")<< QObject::tr("PRP_MODE_FROM_COPYFROM");
59         theWidget->insertList(list);
60         break;
61       }
62     case SALOMEGUI_StudyPropertiesDlg::prpLockedId:
63       {
64         list << QObject::tr("PRP_NO") << QObject::tr("PRP_YES");
65         theWidget->insertList(list, getValue() == QObject::tr("PRP_NO") ? 0 : 1);    
66         break;
67       }
68     case SALOMEGUI_StudyPropertiesDlg::prpModificationsId:
69       {
70         QAD_Study* study = QAD_Application::getDesktop()->getActiveStudy();
71         if (study) { 
72           SALOMEDS::Study_var studyDoc = study->getStudyDocument();
73           SALOMEDS::AttributeStudyProperties_var propAttr;
74           if (!studyDoc->_is_nil()) {
75             propAttr = studyDoc->GetProperties();
76             if (!propAttr->_is_nil()) {
77               SALOMEDS::StringSeq_var aUsers;
78               SALOMEDS::LongSeq_var  aMins, aHours, aDays, aMonths, aYears;
79               propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
80               int aCnt = aUsers->length();
81               for ( int i = 0; i < aCnt; i++ ) {
82                 QString val;
83                 val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", 
84                             aDays  [i], 
85                             aMonths[i], 
86                             aYears [i], 
87                             aHours [i], 
88                             aMins  [i]);
89                 val = val + " : " + QString(aUsers[i]);
90                 list.prepend(val);
91               }
92               theWidget->setDuplicatesEnabled(true);
93               theWidget->insertList(list);    
94             }
95           }
96         }
97         break;
98       }
99     default:
100       {
101         QAD_ListViewItem::fillWidgetWithValues(theWidget);
102         break;
103       }
104     }
105   }
106 // finishes editing of entity
107   virtual UpdateType finishEditing(QAD_EntityEdit* theWidget) {
108     if ( getUserType() == SALOMEGUI_StudyPropertiesDlg::prpModificationsId )
109       return utCancel;
110     else
111       return QAD_ListViewItem::finishEditing(theWidget);
112   }
113 };
114
115 SALOMEGUI_StudyPropertiesDlg::SALOMEGUI_StudyPropertiesDlg(QWidget* parent)
116      : QDialog(parent, "", TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
117        myChanged( false )
118 {
119   setCaption(tr("TLT_STUDY_PROPERTIES"));
120   setSizeGripEnabled( true );
121
122   clearWFlags(Qt::WStyle_ContextHelp);
123
124   QGridLayout* mainLayout = new QGridLayout(this);
125   mainLayout->setMargin(DEFAULT_MARGIN);
126   mainLayout->setSpacing(DEFAULT_SPACING);
127   
128   myPropList = new QAD_ListView(this);
129   myPropList->addColumn("");
130   myPropList->addColumn("");
131   myPropList->enableEditing(TRUE);
132   myPropList->setMinimumSize(MIN_LIST_WIDTH, MIN_LIST_HEIGHT);
133   mainLayout->addMultiCellWidget(myPropList, 0, 0, 0, 2);
134
135   myOKBtn = new QPushButton(tr("BUT_OK"), this);
136   mainLayout->addWidget(myOKBtn, 1, 0);
137
138   myCancelBtn = new QPushButton(tr("BUT_CANCEL"), this);
139   mainLayout->addWidget(myCancelBtn, 1, 2);
140
141   QSpacerItem* spacer1 = new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum);
142   mainLayout->addItem(spacer1, 1, 1);
143
144   // Display study properties
145   QAD_Study* study = ((QAD_Desktop*)parent)->getActiveStudy();
146   if (study) myStudyDoc = study->getStudyDocument();
147
148   initData();
149
150   connect(myOKBtn,     SIGNAL(clicked()), this, SLOT(onOK()));
151   connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
152 }
153
154 SALOMEGUI_StudyPropertiesDlg::~SALOMEGUI_StudyPropertiesDlg()
155 {
156 }
157
158 void SALOMEGUI_StudyPropertiesDlg::initData()
159 {
160   bool hasData = !myStudyDoc->_is_nil();
161   SALOMEDS::AttributeStudyProperties_var propAttr;
162   if (hasData) propAttr = myStudyDoc->GetProperties();
163   hasData = hasData && !propAttr->_is_nil();
164
165   // Study author's name
166   SALOMEGUI_PropItem* item = new SALOMEGUI_PropItem(myPropList, tr("PRP_AUTHOR")+":", true, prpAuthorId);
167   if (hasData) item->setValue(propAttr->GetUserName());
168
169   // Date of creation
170   item = new SALOMEGUI_PropItem(myPropList, item, tr("PRP_DATE")+":", false, prpDateId);
171   if (hasData) {
172     CORBA::Long 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       item->setValue(strDate);
177     }
178   }
179   
180   // Creation mode
181 //  item = new SALOMEGUI_PropItem(myPropList, item, tr("PRP_MODE")+":", true, prpModeId);
182 //  item->setEditingType(QAD_EntityEdit::etComboBox);
183 //  if (hasData) item->setValue(propAttr->GetCreationMode());
184
185   // Locked or not
186   item = new SALOMEGUI_PropItem(myPropList, item, tr("PRP_LOCKED")+":", true, prpLockedId);
187   item->setEditingType(QAD_EntityEdit::etComboBox);  
188   if (hasData) (propAttr->IsLocked()) ? item->setValue(tr("PRP_YES")) : item->setValue(tr("PRP_NO"));
189
190   // Saved or not
191   item = new SALOMEGUI_PropItem(myPropList, item, tr("PRP_MODIFIED")+":", false, prpSavedId);
192   if (hasData) {
193     if (propAttr->IsModified())
194       item->setValue(tr("PRP_YES"));
195     else
196       item->setValue(tr("PRP_NO"));
197   }
198
199   // Modifications list
200   item = new SALOMEGUI_PropItem(myPropList, item, tr("PRP_MODIFICATIONS")+":", true, prpModificationsId); 
201   item->setEditingType(QAD_EntityEdit::etComboBox);  
202   if (hasData) { 
203     SALOMEDS::StringSeq_var aUsers;
204     SALOMEDS::LongSeq_var   aMins, aHours, aDays, aMonths, aYears;
205     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
206     int aLast = aUsers->length()-1;
207     if (aLast >= 0) {
208       QString val;
209       val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", 
210                   aDays  [aLast], 
211                   aMonths[aLast], 
212                   aYears [aLast], 
213                   aHours [aLast], 
214                   aMins  [aLast]);
215       val = val + " : " + QString(aUsers[aUsers->length()-1]);
216       item->setValue(val);
217     }
218   }
219
220   myOKBtn->setEnabled(hasData);
221 }
222
223 bool SALOMEGUI_StudyPropertiesDlg::acceptData()
224 {
225   return TRUE;
226 }
227
228 void SALOMEGUI_StudyPropertiesDlg::onOK()
229 {
230   myPropList->accept();
231
232   if (acceptData()) {
233     SALOMEDS::AttributeStudyProperties_var propAttr = myStudyDoc->GetProperties();
234     myChanged = propChanged();
235     if ( !propAttr->_is_nil() && myChanged ) {
236       QListViewItemIterator it( myPropList );
237       // iterate through all items of the listview
238       for ( ; it.current(); ++it ) {
239         SALOMEGUI_PropItem* item = (SALOMEGUI_PropItem*)(it.current());
240         switch (item->getUserType()) {
241         case prpAuthorId:
242           propAttr->SetUserName(item->getValue().stripWhiteSpace().latin1());
243           break;
244         case prpModeId:
245           propAttr->SetCreationMode(item->getValue().stripWhiteSpace().latin1());
246           break;
247         case prpLockedId:
248           propAttr->SetLocked(item->getValue().compare(tr("PRP_YES")) == 0);
249           break;
250         default:
251           break;
252         }
253       }
254     }
255     accept();
256   }
257 }
258
259 bool SALOMEGUI_StudyPropertiesDlg::propChanged() {
260   SALOMEDS::AttributeStudyProperties_var propAttr = myStudyDoc->GetProperties();
261   if ( !propAttr->_is_nil() ) {
262     QListViewItemIterator it( myPropList );
263     // iterate through all items of the listview
264     for ( ; it.current(); ++it ) {
265       SALOMEGUI_PropItem* item = (SALOMEGUI_PropItem*)(it.current());
266       switch (item->getUserType()) {
267       case prpAuthorId:
268         if ( QString( propAttr->GetUserName() ) != item->getValue().stripWhiteSpace() ) {
269           return true;
270         }
271         break;
272       case prpModeId:
273         if ( QString( propAttr->GetCreationMode() ) != item->getValue().stripWhiteSpace() ) {
274           return true;
275         }
276         break;
277       case prpLockedId:
278         {
279           bool bLocked = item->getValue().compare( tr( "PRP_YES" ) ) == 0;
280           if ( propAttr->IsLocked() != bLocked ) {
281             return true;
282           }
283           break;
284         }
285       default:
286         break;
287       }
288     }
289   }
290   return false;
291 }