Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / SalomeApp / SalomeApp_StudyPropertiesDlg.cxx
index ee81e8ee159e8088995c5ab94eafd337ba46f72c..2fd7dcb8af63f47517d6ec1143c9eaad2b4b180b 100644 (file)
@@ -1,32 +1,30 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  SALOME SalomeApp
 //  File   : SalomeApp_StudyPropertiesDlg.cxx
-//  Author : Sergey ANIKIN
-//  Module : SALOME
-//  $Header$
-//
+//  Author : Roman NIKOLAEV
+
 #include "SalomeApp_StudyPropertiesDlg.h"
-#include "SalomeApp_ListView.h"
 #include "SalomeApp_Study.h"
 
 #include "SUIT_Session.h"
 // QT Includes
 #include <QPushButton>
 #include <QGridLayout>
+#include <QHBoxLayout>
+#include <QFrame>
+#include <QLineEdit>
+#include <QLabel>
+#include <QCheckBox>
+#include <QComboBox>
+#include <QTextEdit>
+#include <QTreeWidget>
+#include <QPushButton>
 
-using namespace std;
-
+#include <iostream>
 #define  DEFAULT_MARGIN 11
-#define DEFAULT_SPACING 6
+#define  DEFAULT_SPACING 6
 #define     SPACER_SIZE 5
 #define MIN_LIST_WIDTH  300
 #define MIN_LIST_HEIGHT 150
 
-class SalomeApp_PropItem : public SalomeApp_ListViewItem
-{
-public:
-  /*! constructor  */
-  SalomeApp_PropItem(SalomeApp_ListView* parent,
-                    const QString       theName,
-                    const bool          theEditable,
-                    const int           theUserType) :
-  SalomeApp_ListViewItem( parent, QStringList(theName), theEditable )
-  {
-    setUserType(theUserType);
-  }
-  /*! constructor */
-  SalomeApp_PropItem(SalomeApp_ListView*     parent,
-                    SalomeApp_ListViewItem* after,
-                    const QString     theName,
-                    const bool        theEditable,
-                    const int         theUserType) :
-  SalomeApp_ListViewItem( parent, after, theName, theEditable )
-  {
-    setUserType(theUserType);
-  }
-  /*! fills widget with initial values (list or single value)*/
-  void fillWidgetWithValues( SalomeApp_EntityEdit* theWidget )
-  {
-    QStringList list;
-    switch(getUserType()) {
-    //case SalomeApp_StudyPropertiesDlg::prpModeId:
-    //  {
-    //    list << SalomeApp_StudyPropertiesDlg::tr("PRP_MODE_FROM_SCRATCH") <<
-    //        SalomeApp_StudyPropertiesDlg::tr("PRP_MODE_FROM_COPYFROM");
-    //    theWidget->insertList(list);
-    //    break;
-    //  }
-    case SalomeApp_StudyPropertiesDlg::prpLockedId:
-      {
-       list << SalomeApp_StudyPropertiesDlg::tr( "PRP_NO" ) << SalomeApp_StudyPropertiesDlg::tr( "PRP_YES" );
-       theWidget->insertList(list, getValue() == SalomeApp_StudyPropertiesDlg::tr( "PRP_NO" ) ? 0 : 1 );
-       break;
-      }
-    case SalomeApp_StudyPropertiesDlg::prpModificationsId:
-      {
-       SalomeApp_Study* study =
-          dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
-       if (study) {
-         _PTR(Study) studyDoc = study->studyDS();
-         _PTR(AttributeStudyProperties) propAttr;
-         if ( studyDoc ) {
-           propAttr = studyDoc->GetProperties();
-           if ( propAttr ) {
-             std::vector<std::string> aUsers;
-             std::vector<int>  aMins, aHours, aDays, aMonths, aYears;
-             propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
-             int aCnt = aUsers.size();
-             for ( int i = 0; i < aCnt; i++ ) {
-               QString val;
-               val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
-                           aDays  [i],
-                           aMonths[i],
-                           aYears [i],
-                           aHours [i],
-                           aMins  [i]);
-               val = val + " : " + QString( aUsers[i].c_str() );
-               list.prepend(val);
-             }
-             theWidget->setDuplicatesEnabled(true);
-             theWidget->insertList(list);
-           }
-         }
-       }
-       break;
-      }
-    default:
-      {
-       SalomeApp_ListViewItem::fillWidgetWithValues(theWidget);
-       break;
-      }
-    }
-  }
-  /*! finishes editing of entity */
-  virtual UpdateType finishEditing( SalomeApp_EntityEdit* theWidget ) {
-    if ( getUserType() == SalomeApp_StudyPropertiesDlg::prpModificationsId )
-      return utCancel;
-    else
-      return SalomeApp_ListViewItem::finishEditing(theWidget);
-  }
-};
 
 /*!Constructor. Initialize study properties dialog.*/
 SalomeApp_StudyPropertiesDlg::SalomeApp_StudyPropertiesDlg(QWidget* parent)
-     : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
-       myChanged( false )
+  : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
+    myIsChanged(false)
 {
-  setObjectName( "" );
   setModal( TRUE );
 
   setWindowTitle(tr("TLT_STUDY_PROPERTIES"));
   setSizeGripEnabled( true );
 
-  setWindowFlags( windowFlags() ^ QFlags<Qt::WindowType>(!Qt::WindowContextHelpButtonHint) );
-
-  QGridLayout* mainLayout = new QGridLayout(this);
-  mainLayout->setMargin(DEFAULT_MARGIN);
-  mainLayout->setSpacing(DEFAULT_SPACING);
-
-  myPropList = new SalomeApp_ListView(this);
-  myPropList->setColumnCount(2);
-  QStringList aLabels;
-  aLabels << "" << "";
-  myPropList->setHeaderLabels( aLabels );
-  myPropList->enableEditing(TRUE);
-  myPropList->setMinimumSize(MIN_LIST_WIDTH, MIN_LIST_HEIGHT);
-  mainLayout->addWidget(myPropList, 0, 0, 1, 3);
-
-  myOKBtn = new QPushButton(tr("BUT_OK"), this);
-  mainLayout->addWidget(myOKBtn, 1, 0);
-
-  myCancelBtn = new QPushButton(tr("BUT_CANCEL"), this);
-  mainLayout->addWidget(myCancelBtn, 1, 2);
-
-  QSpacerItem* spacer1 =
-    new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum);
-  mainLayout->addItem(spacer1, 1, 1);
+  setWindowFlags( windowFlags() & ~Qt::WindowContextHelpButtonHint );
 
   // Display study properties
   SalomeApp_Study* study =
@@ -179,10 +75,77 @@ SalomeApp_StudyPropertiesDlg::SalomeApp_StudyPropertiesDlg(QWidget* parent)
   if (study)
     myStudyDoc = study->studyDS();
 
-  initData();
+  //Author
+  QLabel* authorLbl = new QLabel(tr("PRP_AUTHOR"),this);
+  myAuthor = new QLineEdit(this);
+
+  //Creation date
+  QLabel* dateLbl = new QLabel(tr("PRP_DATE"),this);
+  myDate = new QLabel(this);
+
+  //Locked flag
+  myLocked = new QCheckBox(tr("PRP_LOCKED"), this);
+
+  //Modification flag
+  myModification = new QLabel(this);
+
+  //Units
+  QLabel* unitsLbl = new QLabel(tr("PRP_UNITS"),this);
+  myUnits = new QComboBox(this);
+
+  //Comment
+  QLabel* commentLabel = new QLabel(tr("PRP_COMMENT"),this);
+  myComment = new QTextEdit(this);
+  myComment->setMaximumHeight(80);
 
-  connect(myOKBtn,     SIGNAL(clicked()), this, SLOT(onOK()));
-  connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
+  //Modifications
+  QLabel* modificationsLabel = new QLabel(tr("PRP_MODIFICATIONS"),this);  
+  myModifications = new QTreeWidget(this);
+  myModifications->setRootIsDecorated(false);
+  myModifications->setUniformRowHeights(true);
+  myModifications->setAllColumnsShowFocus(true);
+  myModifications->setColumnCount(2);
+  myModifications->setMaximumHeight(80);
+  QStringList columnNames;
+  columnNames.append(tr("PRP_AUTHOR"));
+  columnNames.append(tr("PRP_DATE_MODIF"));
+
+  QTreeWidgetItem * headerItem = new QTreeWidgetItem(columnNames);
+  myModifications->setHeaderItem ( headerItem );
+
+  QFrame* buttonFrame = new QFrame(this);
+
+  QHBoxLayout* horizontalLayout = new QHBoxLayout(buttonFrame);
+  
+  myOkButton = new QPushButton(tr("BUT_OK"), buttonFrame);
+  myCancelButton = new QPushButton(tr("BUT_CANCEL"), buttonFrame);
+
+  horizontalLayout->addWidget(myOkButton);
+  horizontalLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum) );
+  horizontalLayout->addWidget(myCancelButton);
+  
+  QGridLayout* gridLayout = new QGridLayout(this);
+  gridLayout->setMargin(DEFAULT_MARGIN);
+  gridLayout->setSpacing(DEFAULT_SPACING);
+  gridLayout->addWidget(authorLbl, 0, 0, 1, 1);
+  gridLayout->addWidget(myAuthor, 0, 1, 1, 1);
+  gridLayout->addWidget(dateLbl, 1, 0, 1, 1);
+  gridLayout->addWidget(myDate, 1, 1, 1, 1);
+  gridLayout->addWidget(myLocked, 2, 0, 1, 1);
+  gridLayout->addWidget(myModification, 2, 1, 1, 1);
+  gridLayout->addWidget(unitsLbl, 3, 0, 1, 1);
+  gridLayout->addWidget(myUnits, 3, 1, 1, 1);
+  gridLayout->addItem(new QSpacerItem(/*72*/0, 0/*20*/, QSizePolicy::Expanding, QSizePolicy::Minimum), 3, 2, 1, 1);
+  gridLayout->addWidget(commentLabel, 4, 0, 1, 1);
+  gridLayout->addWidget(myComment, 4, 1, 1, 2);
+  gridLayout->addWidget(modificationsLabel, 5, 0, 1, 1);
+  gridLayout->addWidget(myModifications, 5, 1, 1, 2);
+  gridLayout->addWidget(buttonFrame, 6, 0, 1, 3);
+
+  connect(myOkButton,     SIGNAL(clicked()), this, SLOT(clickOnOk()));
+  connect(myCancelButton, SIGNAL(clicked()), this, SLOT(reject()));
+  
+  initData();
 }
 
 /*!
@@ -202,172 +165,133 @@ void SalomeApp_StudyPropertiesDlg::initData()
   if (hasData)
     propAttr = myStudyDoc->GetProperties();
   hasData = hasData && propAttr;
-
-  // Study author's name
-  SalomeApp_PropItem* item = new SalomeApp_PropItem(myPropList, tr("PRP_AUTHOR")+":", true, prpAuthorId);
-  if (hasData)
-    item->setValue(propAttr->GetUserName().c_str());
-
-  // Date of creation
-  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_DATE")+":", false, prpDateId);
+  
   if (hasData) {
+    //Creator and creation date
+    myAuthor->setText(propAttr->GetUserName().c_str());
     int minutes, hours, day, month, year;
     if (propAttr->GetCreationDate(minutes, hours, day, month, year)) {
       QString strDate;
       strDate.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d", day, month, year, hours, minutes);
-      item->setValue(strDate);
+      myDate->setText(strDate);
+    }
+    
+    //Is Locked?
+    myLocked->setChecked(propAttr->IsLocked());
+
+    //Is Modified?
+    bool isModified = false;
+    SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>
+      (SUIT_Session::session()->activeApplication()->activeStudy());
+    if (study) {
+      isModified = study->isModified();
+      if (hasData) {
+        if (propAttr->IsModified() != isModified) {
+          propAttr->SetModified((int)isModified);
+        }
+      }
     }
-  }
 
-  // Creation mode
-//  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODE")+":", true, prpModeId);
-//  item->setEditingType( SalomeApp_EntityEdit::etComboBox);
-//  if (hasData) item->setValue(propAttr->GetCreationMode());
+    QString modif = propAttr->IsModified() ? tr( "PRP_MODIFIED" ) : tr( "PRP_NOT_MODIFIED" );
+    myModification->setText(modif);
+    
+    //Units
+    QStringList theList;
+    theList <<""<<"mm"<<"cm"<<"m"<<"km"<<"inch";
+    myUnits->insertItems(0,theList); 
 
-  // Locked or not
-  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_LOCKED")+":", true, prpLockedId);
-  item->setEditingType( SalomeApp_EntityEdit::etComboBox);
-  if ( hasData )
-    item->setValue( tr( propAttr->IsLocked() ? "PRP_YES" : "PRP_NO" ) );
+    QString anUnit = propAttr->GetUnits().c_str();
 
-  // Saved or not
-  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFIED")+":", false, prpSavedId);
-  bool isModified = false;
-  SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>
-    (SUIT_Session::session()->activeApplication()->activeStudy());
-  if (study) {
-    isModified = study->isModified();
-    if (hasData) {
-      if (propAttr->IsModified() != isModified) {
-        propAttr->SetModified((int)isModified);
+    int aFound = -1;
+    for (int i = 0; i < myUnits->count(); i++) {
+      if ( myUnits->itemText(i) == anUnit ) {
+        aFound = i;
+        break;
       }
     }
-  }
-  if (hasData) {
-    if (propAttr->IsModified())
-      item->setValue( tr( "PRP_YES" ) );
-    else
-      item->setValue( tr( "PRP_NO" ) );
-  }
-
-  // Modifications list
-  item = new SalomeApp_PropItem(myPropList, item, tr("PRP_MODIFICATIONS")+":", true, prpModificationsId);
-  item->setEditingType( SalomeApp_EntityEdit::etComboBox);
-  if (hasData) {
+    
+    if (aFound >= 0) {
+      myUnits->setCurrentIndex(aFound);
+    }
+    
+    //Comments
+    myComment->setPlainText(propAttr->GetComment().c_str());
+    
+    //Modifications
     std::vector<std::string> aUsers;
-    std::vector<int> aMins, aHours, aDays, aMonths, aYears;
+    std::vector<int>  aMins, aHours, aDays, aMonths, aYears;
     propAttr->GetModificationsList(aUsers, aMins, aHours, aDays, aMonths, aYears, false);
-    int aLast = aUsers.size()-1;
-    if (aLast >= 0) {
-      QString val;
-      val.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
-                 aDays  [aLast],
-                 aMonths[aLast],
-                 aYears [aLast],
-                 aHours [aLast],
-                 aMins  [aLast]);
-      val = val + " : " + QString(aUsers[aUsers.size()-1].c_str());
-      item->setValue(val);
-    }
+    int aCnt = aUsers.size();
+    for ( int i = 0; i < aCnt; i++ ) {
+      QString date;
+      date.sprintf("%2.2d/%2.2d/%2d %2.2d:%2.2d",
+                  aDays  [i],
+                  aMonths[i],
+                  aYears [i],
+                  aHours [i],
+                  aMins  [i]);
+      QStringList aList;
+      aList.append(aUsers[i].c_str());
+      aList.append(date);
+      QTreeWidgetItem* item = new  QTreeWidgetItem(aList);
+      myModifications->addTopLevelItem(item);
+    }        
   }
-
-  myOKBtn->setEnabled(hasData);
-}
-
-/*!
-  accept data. Return true.
-*/
-bool SalomeApp_StudyPropertiesDlg::acceptData()
-{
-  return TRUE;
+  adjustSize();
 }
 
 /*!
   Fill properties attributes.
 */
-void SalomeApp_StudyPropertiesDlg::onOK()
+void SalomeApp_StudyPropertiesDlg::clickOnOk()
 {
-  myPropList->accept();
-
-  if (acceptData()) {
-    _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
-    //myChanged = propChanged();
-    if ( propAttr /*&& myChanged*/ ) {
-      QTreeWidgetItemIterator it( myPropList );
-      // iterate through all items of the listview
-      while (*it) {
-       SalomeApp_PropItem* item = (SalomeApp_PropItem*)(*it);
-       switch (item->getUserType()) {
-       case prpAuthorId:
-          if (QString(propAttr->GetUserName().c_str()) != item->getValue().trimmed()) {
-            if (!propAttr->IsLocked()) {
-              propAttr->SetUserName(item->getValue().trimmed().toStdString());
-              myChanged = true;
-            } else {
-              SUIT_MessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(),
-                                      QObject::tr("WRN_WARNING"),
-                                      QObject::tr("WRN_STUDY_LOCKED") );
-            }
-          }
-         break;
-        //case prpModeId:
-       //  propAttr->SetCreationMode(item->getValue().trimmed().latin1());
-       //  break;
-       case prpLockedId:
-          {
-            bool bLocked = item->getValue().compare(tr("PRP_YES")) == 0;
-            if (propAttr->IsLocked() != bLocked) {
-              propAttr->SetLocked(bLocked);
-              myChanged = true;
-            }
-          }
-         break;
-       default:
-         break;
-       }
-       ++it;
+  _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
+  //Firstly, store locked flag
+  if(propAttr) {
+    bool bLocked = myLocked->isChecked();
+    if (propAttr->IsLocked() != bLocked) {
+      propAttr->SetLocked(bLocked);
+      myIsChanged = true;
+    }
+    
+    bool needWarning = false;
+    
+    //Author
+    if (QString(propAttr->GetUserName().c_str()) != myAuthor->text().trimmed()) {
+      if(!propAttr->IsLocked()) {
+        propAttr->SetUserName(myAuthor->text().trimmed().toStdString());
+        myIsChanged = true;
+      } else {
+        needWarning = true;
       }
     }
-    accept();
-  }
-}
-
-/*!
-  Check is properties changed?
-*/
-bool SalomeApp_StudyPropertiesDlg::propChanged()
-{
-  _PTR(AttributeStudyProperties) propAttr = myStudyDoc->GetProperties();
-  if (propAttr) {
-    QTreeWidgetItemIterator it (myPropList);
 
-    // iterate through all items of the listview
-    while (*it) {
-      SalomeApp_PropItem* item = (SalomeApp_PropItem*)(*it);
-      switch (item->getUserType()) {
-      case prpAuthorId:
-       if ( QString( propAttr->GetUserName().c_str() ) != item->getValue().trimmed() ) {
-         return true;
-       }
-       break;
-      //case prpModeId:
-      //  if ( QString( propAttr->GetCreationMode().c_str() ) != item->getValue().trimmed() ) {
-      //    return true;
-      //  }
-      //  break;
-      case prpLockedId:
-       {
-         bool bLocked = item->getValue().compare( tr( "PRP_YES" ) ) == 0;
-         if ( propAttr->IsLocked() != bLocked ) {
-           return true;
-         }
-         break;
-       }
-      default:
-       break;
+    //Unit
+    if (QString(propAttr->GetUnits().c_str()) != myUnits->currentText()) {
+      if(!propAttr->IsLocked()) {
+        propAttr->SetUnits(myUnits->currentText().toStdString());
+        myIsChanged = true;
+      } else {
+        needWarning = true;
       }
-      ++it;
     }
+      
+    //Comment      
+    if (QString(propAttr->GetComment().c_str()) != myComment->toPlainText()) {
+      if(!propAttr->IsLocked()) {
+        propAttr->SetComment(myComment->toPlainText().toStdString());
+        myIsChanged = true;
+      } else {
+        needWarning = true;
+      }
+    }
+    
+    if(needWarning) {
+      SUIT_MessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(),
+                               QObject::tr("WRN_WARNING"),
+                               QObject::tr("WRN_STUDY_LOCKED") );
+    }
+    
+    accept();
   }
-  return false;
 }