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