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