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