Salome HOME
Added slots for Copy and Paste operations
[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 << QObject::tr("PRP_MODE_FROM_SCRATCH") << 
60                 QObject::tr("PRP_MODE_FROM_COPYFROM");
61         theWidget->insertList(list);
62         break;
63       }
64     case SalomeApp_StudyPropertiesDlg::prpLockedId:
65       {
66         list << QObject::tr("PRP_NO") << QObject::tr("PRP_YES");
67         theWidget->insertList(list, getValue() == QObject::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) (propAttr->IsLocked()) ? item->setValue(tr("PRP_YES")) : item->setValue(tr("PRP_NO"));
194
195   // Saved or not
196   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFIED")+":", false, prpSavedId);
197   if (hasData) {
198     if (propAttr->IsModified())
199       item->setValue(tr("PRP_YES"));
200     else
201       item->setValue(tr("PRP_NO"));
202   }
203
204   // Modifications list
205   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFICATIONS")+":", true, prpModificationsId); 
206   item->setEditingType( SalomeApp_EntityEdit::etComboBox);  
207   if (hasData) { 
208     std::vector<std::string> aUsers;
209     std::vector<int> aMins, aHours, aDays, aMonths, aYears;
210     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
211     int aLast = aUsers.size()-1;
212     if (aLast >= 0) {
213       QString val;
214       val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", 
215                   aDays  [aLast], 
216                   aMonths[aLast], 
217                   aYears [aLast], 
218                   aHours [aLast], 
219                   aMins  [aLast]);
220       val = val + " : " + QString(aUsers[aUsers.size()-1].c_str());
221       item->setValue(val);
222     }
223   }
224
225   myOKBtn->setEnabled(hasData);
226 }
227
228 bool SalomeApp_StudyPropertiesDlg::acceptData()
229 {
230   return TRUE;
231 }
232
233 void SalomeApp_StudyPropertiesDlg::onOK()
234 {
235   myPropList->accept();
236
237   if (acceptData()) {
238     _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
239     myChanged = propChanged();
240     if ( propAttr && myChanged ) {
241       QListViewItemIterator it( myPropList );
242       // iterate through all items of the listview
243       for ( ; it.current(); ++it ) {
244         SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
245         switch (item->getUserType()) {
246         case prpAuthorId:
247           propAttr->SetUserName(item->getValue().stripWhiteSpace().latin1());
248           break;
249         case prpModeId:
250           propAttr->SetCreationMode(item->getValue().stripWhiteSpace().latin1());
251           break;
252         case prpLockedId:
253           propAttr->SetLocked(item->getValue().compare(tr("PRP_YES")) == 0);
254           break;
255         default:
256           break;
257         }
258       }
259     }
260     accept();
261   }
262 }
263
264 bool SalomeApp_StudyPropertiesDlg::propChanged() {
265   _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
266   if ( propAttr ) {
267     QListViewItemIterator it( myPropList );
268     // iterate through all items of the listview
269     for ( ; it.current(); ++it ) {
270       SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
271       switch (item->getUserType()) {
272       case prpAuthorId:
273         if ( QString( propAttr->GetUserName().c_str() ) != item->getValue().stripWhiteSpace() ) {
274           return true;
275         }
276         break;
277       case prpModeId:
278         if ( QString( propAttr->GetCreationMode().c_str() ) != item->getValue().stripWhiteSpace() ) {
279           return true;
280         }
281         break;
282       case prpLockedId:
283         {
284           bool bLocked = item->getValue().compare( tr( "PRP_YES" ) ) == 0;
285           if ( propAttr->IsLocked() != bLocked ) {
286             return true;
287           }
288           break;
289         }
290       default:
291         break;
292       }
293     }
294   }
295   return false;
296 }