]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_StudyPropertiesDlg.cxx
Salome HOME
Prefernces and properties.
[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 SalomeApp_StudyPropertiesDlg::SalomeApp_StudyPropertiesDlg(QWidget* parent)
118      : QDialog(parent, "", TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
119        myChanged( false )
120 {
121   setCaption(tr("TLT_STUDY_PROPERTIES"));
122   setSizeGripEnabled( true );
123
124   clearWFlags(Qt::WStyle_ContextHelp);
125
126   QGridLayout* mainLayout = new QGridLayout(this);
127   mainLayout->setMargin(DEFAULT_MARGIN);
128   mainLayout->setSpacing(DEFAULT_SPACING);
129   
130   myPropList = new SalomeApp_ListView(this);
131   myPropList->addColumn("");
132   myPropList->addColumn("");
133   myPropList->enableEditing(TRUE);
134   myPropList->setMinimumSize(MIN_LIST_WIDTH, MIN_LIST_HEIGHT);
135   mainLayout->addMultiCellWidget(myPropList, 0, 0, 0, 2);
136
137   myOKBtn = new QPushButton(tr("BUT_OK"), this);
138   mainLayout->addWidget(myOKBtn, 1, 0);
139
140   myCancelBtn = new QPushButton(tr("BUT_CANCEL"), this);
141   mainLayout->addWidget(myCancelBtn, 1, 2);
142
143   QSpacerItem* spacer1 = new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum);
144   mainLayout->addItem(spacer1, 1, 1);
145
146   // Display study properties
147   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
148   if (study)
149     myStudyDoc = study->studyDS();
150
151   initData();
152
153   connect(myOKBtn,     SIGNAL(clicked()), this, SLOT(onOK()));
154   connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
155 }
156
157 SalomeApp_StudyPropertiesDlg::~SalomeApp_StudyPropertiesDlg()
158 {
159 }
160
161 void SalomeApp_StudyPropertiesDlg::initData()
162 {
163   bool hasData = myStudyDoc;
164   _PTR(AttributeStudyProperties) propAttr;
165   if (hasData) 
166     propAttr = myStudyDoc->GetProperties();
167   hasData = hasData && propAttr;
168
169   // Study author's name
170   SalomeApp_PropItem* item = new SalomeApp_PropItem(myPropList, tr("PRP_AUTHOR")+":", true, prpAuthorId);
171   if (hasData)
172     item->setValue(propAttr->GetUserName().c_str());
173
174   // Date of creation
175   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_DATE")+":", false, prpDateId);
176   if (hasData) {
177     int minutes, hours, day, month, year;
178     if (propAttr->GetCreationDate(minutes, hours, day, month, year)) {
179       QString strDate;
180       strDate.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", day, month, year, hours, minutes);
181       item->setValue(strDate);
182     }
183   }
184   
185   // Creation mode
186 //  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODE")+":", true, prpModeId);
187 //  item->setEditingType( SalomeApp_EntityEdit::etComboBox);
188 //  if (hasData) item->setValue(propAttr->GetCreationMode());
189
190   // Locked or not
191   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_LOCKED")+":", true, prpLockedId);
192   item->setEditingType( SalomeApp_EntityEdit::etComboBox);  
193   if ( hasData )
194     item->setValue( tr( propAttr->IsLocked() ? "PRP_YES" : "PRP_NO" ) );
195
196   // Saved or not
197   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFIED")+":", false, prpSavedId);
198   if (hasData) {
199     if (propAttr->IsModified())
200       item->setValue( tr( "PRP_YES" ) );
201     else
202       item->setValue( tr( "PRP_NO" ) );
203   }
204
205   // Modifications list
206   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFICATIONS")+":", true, prpModificationsId); 
207   item->setEditingType( SalomeApp_EntityEdit::etComboBox);  
208   if (hasData) { 
209     std::vector<std::string> aUsers;
210     std::vector<int> aMins, aHours, aDays, aMonths, aYears;
211     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
212     int aLast = aUsers.size()-1;
213     if (aLast >= 0) {
214       QString val;
215       val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", 
216                   aDays  [aLast], 
217                   aMonths[aLast], 
218                   aYears [aLast], 
219                   aHours [aLast], 
220                   aMins  [aLast]);
221       val = val + " : " + QString(aUsers[aUsers.size()-1].c_str());
222       item->setValue(val);
223     }
224   }
225
226   myOKBtn->setEnabled(hasData);
227 }
228
229 bool SalomeApp_StudyPropertiesDlg::acceptData()
230 {
231   return TRUE;
232 }
233
234 void SalomeApp_StudyPropertiesDlg::onOK()
235 {
236   myPropList->accept();
237
238   if (acceptData()) {
239     _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
240     myChanged = propChanged();
241     if ( propAttr && myChanged ) {
242       QListViewItemIterator it( myPropList );
243       // iterate through all items of the listview
244       for ( ; it.current(); ++it ) {
245         SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
246         switch (item->getUserType()) {
247         case prpAuthorId:
248           propAttr->SetUserName(item->getValue().stripWhiteSpace().latin1());
249           break;
250         case prpModeId:
251           propAttr->SetCreationMode(item->getValue().stripWhiteSpace().latin1());
252           break;
253         case prpLockedId:
254           propAttr->SetLocked(item->getValue().compare(tr("PRP_YES")) == 0);
255           break;
256         default:
257           break;
258         }
259       }
260     }
261     accept();
262   }
263 }
264
265 bool SalomeApp_StudyPropertiesDlg::propChanged() {
266   _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
267   if ( propAttr ) {
268     QListViewItemIterator it( myPropList );
269     // iterate through all items of the listview
270     for ( ; it.current(); ++it ) {
271       SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
272       switch (item->getUserType()) {
273       case prpAuthorId:
274         if ( QString( propAttr->GetUserName().c_str() ) != item->getValue().stripWhiteSpace() ) {
275           return true;
276         }
277         break;
278       case prpModeId:
279         if ( QString( propAttr->GetCreationMode().c_str() ) != item->getValue().stripWhiteSpace() ) {
280           return true;
281         }
282         break;
283       case prpLockedId:
284         {
285           bool bLocked = item->getValue().compare( tr( "PRP_YES" ) ) == 0;
286           if ( propAttr->IsLocked() != bLocked ) {
287             return true;
288           }
289           break;
290         }
291       default:
292         break;
293       }
294     }
295   }
296   return false;
297 }