]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_StudyPropertiesDlg.cxx
Salome HOME
830536676c97436dbfd420951f187806d9ce55c4
[modules/gui.git] / src / SalomeApp / SalomeApp_StudyPropertiesDlg.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  SALOME SalomeApp
20 //
21 //  File   : SalomeApp_StudyPropertiesDlg.cxx
22 //  Author : Sergey ANIKIN
23 //  Module : SALOME
24 //  $Header$
25
26 #include "SalomeApp_StudyPropertiesDlg.h"
27 #include "SalomeApp_ListView.h"
28 #include "SalomeApp_Study.h"
29
30 #include "SUIT_Session.h"
31 #include <SUIT_Desktop.h>
32 #include <SUIT_MessageBox.h>
33
34 // OCCT Includes
35 #include <OSD_Process.hxx>
36 #include <Quantity_Date.hxx>
37
38 // CORBA Headers
39 #include <SALOMEconfig.h>
40 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
41
42 // QT Includes
43 #include <qpushbutton.h>
44 #include <qlayout.h>
45
46 using namespace std;
47
48 #define  DEFAULT_MARGIN 11
49 #define DEFAULT_SPACING 6
50 #define     SPACER_SIZE 5
51 #define MIN_LIST_WIDTH  300
52 #define MIN_LIST_HEIGHT 150
53
54 class SalomeApp_PropItem : public SalomeApp_ListViewItem
55 {
56 public:
57   /*! constructor  */
58   SalomeApp_PropItem(SalomeApp_ListView* parent,
59                      const QString       theName,
60                      const bool          theEditable,
61                      const int           theUserType) :
62   SalomeApp_ListViewItem( parent, theName, theEditable )
63   {
64     setUserType(theUserType);
65   }
66   /*! constructor */
67   SalomeApp_PropItem(SalomeApp_ListView*     parent,
68                      SalomeApp_ListViewItem* after,
69                      const QString     theName,
70                      const bool        theEditable,
71                      const int         theUserType) :
72   SalomeApp_ListViewItem( parent, after, theName, theEditable )
73   {
74     setUserType(theUserType);
75   }
76   /*! fills widget with initial values (list or single value)*/
77   void fillWidgetWithValues( SalomeApp_EntityEdit* theWidget )
78   {
79     QStringList list;
80     switch(getUserType()) {
81     //case SalomeApp_StudyPropertiesDlg::prpModeId:
82     //  {
83     //    list << SalomeApp_StudyPropertiesDlg::tr("PRP_MODE_FROM_SCRATCH") <<
84     //        SalomeApp_StudyPropertiesDlg::tr("PRP_MODE_FROM_COPYFROM");
85     //    theWidget->insertList(list);
86     //    break;
87     //  }
88     case SalomeApp_StudyPropertiesDlg::prpLockedId:
89       {
90         list << SalomeApp_StudyPropertiesDlg::tr( "PRP_NO" ) << SalomeApp_StudyPropertiesDlg::tr( "PRP_YES" );
91         theWidget->insertList(list, getValue() == SalomeApp_StudyPropertiesDlg::tr( "PRP_NO" ) ? 0 : 1 );
92         break;
93       }
94     case SalomeApp_StudyPropertiesDlg::prpModificationsId:
95       {
96         SalomeApp_Study* study =
97           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
98         if (study) {
99           _PTR(Study) studyDoc = study->studyDS();
100           _PTR(AttributeStudyProperties) propAttr;
101           if ( studyDoc ) {
102             propAttr = studyDoc->GetProperties();
103             if ( propAttr ) {
104               std::vector<std::string> aUsers;
105               std::vector<int>  aMins, aHours, aDays, aMonths, aYears;
106               propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
107               int aCnt = aUsers.size();
108               for ( int i = 0; i < aCnt; i++ ) {
109                 QString val;
110                 val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
111                             aDays  [i],
112                             aMonths[i],
113                             aYears [i],
114                             aHours [i],
115                             aMins  [i]);
116                 val = val + " : " + QString( aUsers[i].c_str() );
117                 list.prepend(val);
118               }
119               theWidget->setDuplicatesEnabled(true);
120               theWidget->insertList(list);
121             }
122           }
123         }
124         break;
125       }
126     default:
127       {
128         SalomeApp_ListViewItem::fillWidgetWithValues(theWidget);
129         break;
130       }
131     }
132   }
133   /*! finishes editing of entity */
134   virtual UpdateType finishEditing( SalomeApp_EntityEdit* theWidget ) {
135     if ( getUserType() == SalomeApp_StudyPropertiesDlg::prpModificationsId )
136       return utCancel;
137     else
138       return SalomeApp_ListViewItem::finishEditing(theWidget);
139   }
140 };
141
142 /*!Constructor. Initialize study properties dialog.*/
143 SalomeApp_StudyPropertiesDlg::SalomeApp_StudyPropertiesDlg(QWidget* parent)
144      : QDialog(parent, "", TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
145        myChanged( false )
146 {
147   setCaption(tr("TLT_STUDY_PROPERTIES"));
148   setSizeGripEnabled( true );
149
150   clearWFlags(Qt::WStyle_ContextHelp);
151
152   QGridLayout* mainLayout = new QGridLayout(this);
153   mainLayout->setMargin(DEFAULT_MARGIN);
154   mainLayout->setSpacing(DEFAULT_SPACING);
155
156   myPropList = new SalomeApp_ListView(this);
157   myPropList->addColumn("");
158   myPropList->addColumn("");
159   myPropList->enableEditing(TRUE);
160   myPropList->setMinimumSize(MIN_LIST_WIDTH, MIN_LIST_HEIGHT);
161   mainLayout->addMultiCellWidget(myPropList, 0, 0, 0, 2);
162
163   myOKBtn = new QPushButton(tr("BUT_OK"), this);
164   mainLayout->addWidget(myOKBtn, 1, 0);
165
166   myCancelBtn = new QPushButton(tr("BUT_CANCEL"), this);
167   mainLayout->addWidget(myCancelBtn, 1, 2);
168
169   QSpacerItem* spacer1 =
170     new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum);
171   mainLayout->addItem(spacer1, 1, 1);
172
173   // Display study properties
174   SalomeApp_Study* study =
175     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
176   if (study)
177     myStudyDoc = study->studyDS();
178
179   initData();
180
181   connect(myOKBtn,     SIGNAL(clicked()), this, SLOT(onOK()));
182   connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
183 }
184
185 /*!
186   Destructor.
187 */
188 SalomeApp_StudyPropertiesDlg::~SalomeApp_StudyPropertiesDlg()
189 {
190 }
191
192 /*!
193   Data initializetion for dialog.(Study author's name, date of creation etc.)
194 */
195 void SalomeApp_StudyPropertiesDlg::initData()
196 {
197   bool hasData = myStudyDoc;
198   _PTR(AttributeStudyProperties) propAttr;
199   if (hasData)
200     propAttr = myStudyDoc->GetProperties();
201   hasData = hasData && propAttr;
202
203   // Study author's name
204   SalomeApp_PropItem* item = new SalomeApp_PropItem(myPropList, tr("PRP_AUTHOR")+":", true, prpAuthorId);
205   if (hasData)
206     item->setValue(propAttr->GetUserName().c_str());
207
208   // Date of creation
209   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_DATE")+":", false, prpDateId);
210   if (hasData) {
211     int minutes, hours, day, month, year;
212     if (propAttr->GetCreationDate(minutes, hours, day, month, year)) {
213       QString strDate;
214       strDate.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", day, month, year, hours, minutes);
215       item->setValue(strDate);
216     }
217   }
218
219   // Creation mode
220 //  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODE")+":", true, prpModeId);
221 //  item->setEditingType( SalomeApp_EntityEdit::etComboBox);
222 //  if (hasData) item->setValue(propAttr->GetCreationMode());
223
224   // Locked or not
225   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_LOCKED")+":", true, prpLockedId);
226   item->setEditingType( SalomeApp_EntityEdit::etComboBox);
227   if ( hasData )
228     item->setValue( tr( propAttr->IsLocked() ? "PRP_YES" : "PRP_NO" ) );
229
230   // Saved or not
231   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFIED")+":", false, prpSavedId);
232   bool isModified = false;
233   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>
234     (SUIT_Session::session()->activeApplication()->activeStudy());
235   if (study) {
236     isModified = study->isModified();
237     if (hasData) {
238       if (propAttr->IsModified() != isModified) {
239         propAttr->SetModified((int)isModified);
240       }
241     }
242   }
243   if (hasData) {
244     if (propAttr->IsModified())
245       item->setValue( tr( "PRP_YES" ) );
246     else
247       item->setValue( tr( "PRP_NO" ) );
248   }
249
250   // Modifications list
251   item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFICATIONS")+":", true, prpModificationsId);
252   item->setEditingType( SalomeApp_EntityEdit::etComboBox);
253   if (hasData) {
254     std::vector<std::string> aUsers;
255     std::vector<int> aMins, aHours, aDays, aMonths, aYears;
256     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
257     int aLast = aUsers.size()-1;
258     if (aLast >= 0) {
259       QString val;
260       val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
261                   aDays  [aLast],
262                   aMonths[aLast],
263                   aYears [aLast],
264                   aHours [aLast],
265                   aMins  [aLast]);
266       val = val + " : " + QString(aUsers[aUsers.size()-1].c_str());
267       item->setValue(val);
268     }
269   }
270
271   myOKBtn->setEnabled(hasData);
272 }
273
274 /*!
275   accept data. Return true.
276 */
277 bool SalomeApp_StudyPropertiesDlg::acceptData()
278 {
279   return TRUE;
280 }
281
282 /*!
283   Fill properties attributes.
284 */
285 void SalomeApp_StudyPropertiesDlg::onOK()
286 {
287   myPropList->accept();
288
289   if (acceptData()) {
290     _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
291     //myChanged = propChanged();
292     if ( propAttr /*&& myChanged*/ ) {
293       QListViewItemIterator it( myPropList );
294       // iterate through all items of the listview
295       for ( ; it.current(); ++it ) {
296         SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
297         switch (item->getUserType()) {
298         case prpAuthorId:
299           if (QString(propAttr->GetUserName().c_str()) != item->getValue().stripWhiteSpace()) {
300             if (!propAttr->IsLocked()) {
301               propAttr->SetUserName(item->getValue().stripWhiteSpace().latin1());
302               myChanged = true;
303             } else {
304               SUIT_MessageBox::warn1(SUIT_Session::session()->activeApplication()->desktop(),
305                                      QObject::tr("WRN_WARNING"),
306                                      QObject::tr("WRN_STUDY_LOCKED"),
307                                      QObject::tr("BUT_OK"));
308             }
309           }
310           break;
311         //case prpModeId:
312         //  propAttr->SetCreationMode(item->getValue().stripWhiteSpace().latin1());
313         //  break;
314         case prpLockedId:
315           {
316             bool bLocked = item->getValue().compare(tr("PRP_YES")) == 0;
317             if (propAttr->IsLocked() != bLocked) {
318               propAttr->SetLocked(bLocked);
319               myChanged = true;
320             }
321           }
322           break;
323         default:
324           break;
325         }
326       }
327     }
328     accept();
329   }
330 }
331
332 /*!
333   Check is properties changed?
334 */
335 bool SalomeApp_StudyPropertiesDlg::propChanged()
336 {
337   _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
338   if (propAttr) {
339     QListViewItemIterator it (myPropList);
340     // iterate through all items of the listview
341     for (; it.current(); ++it) {
342       SalomeApp_PropItem* item = (SalomeApp_PropItem*)(it.current());
343       switch (item->getUserType()) {
344       case prpAuthorId:
345         if ( QString( propAttr->GetUserName().c_str() ) != item->getValue().stripWhiteSpace() ) {
346           return true;
347         }
348         break;
349       //case prpModeId:
350       //  if ( QString( propAttr->GetCreationMode().c_str() ) != item->getValue().stripWhiteSpace() ) {
351       //    return true;
352       //  }
353       //  break;
354       case prpLockedId:
355         {
356           bool bLocked = item->getValue().compare( tr( "PRP_YES" ) ) == 0;
357           if ( propAttr->IsLocked() != bLocked ) {
358             return true;
359           }
360           break;
361         }
362       default:
363         break;
364       }
365     }
366   }
367   return false;
368 }