Salome HOME
updated copyright message
[modules/gui.git] / src / Qtx / QtxPagePrefMgr.cxx
index 33d101bb1085e3379e8e4d3b621d7bb07152c9e8..5ac0728763158f5f3f90e756d8f1200b6d524d81 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // 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.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include <stdio.h>
 
+namespace
+{
+  bool toStringList(const QVariant& value, QStringList& result)
+  {
+    bool ok = false;
+    if ( value.type() == QVariant::StringList )
+    {
+      result = value.toStringList();
+      ok = true;
+    }
+    else if ( value.type() == QVariant::List )
+    {
+      QList<QVariant> valueList = value.toList();
+      for ( QList<QVariant>::const_iterator it = valueList.begin(); it != valueList.end(); ++it )
+      {
+        if ( (*it).canConvert( QVariant::String ) )
+          result.append( (*it).toString() );
+      }
+      ok = true;
+    }
+    return ok;
+  }
+}
+
 /*!
   \class QtxPagePrefMgr
   \brief GUI implementation of the QtxPreferenceMgr class: preferences manager.
@@ -1343,7 +1367,10 @@ bool QtxPagePrefFrameItem::stretch() const
 void QtxPagePrefFrameItem::setStretch( const bool on )
 {
   QSpacerItem* s = 0;
-  QLayout* l = widget() ? widget()->layout() : 0;
+  QWidget* w = widget();
+  if ( qobject_cast<QScrollArea*>( w ) )
+    w = qobject_cast<QScrollArea*>( w )->widget();
+  QLayout* l = w ? w->layout() : 0;
   for ( int i = 0; l && i < l->count() && !s; i++ )
     s = l->itemAt( i )->spacerItem();
 
@@ -2698,7 +2725,7 @@ void QtxPagePrefSelectItem::setInputType( const int type )
 QStringList QtxPagePrefSelectItem::strings() const
 {
   QStringList res;
-  for ( uint i = 0; i < mySelector->count(); i++ )
+  for ( int i = 0; i < mySelector->count(); i++ )
     res.append( mySelector->itemText( i ) );
   return res;
 }
@@ -2708,10 +2735,10 @@ QStringList QtxPagePrefSelectItem::strings() const
   \return list of values IDs
   \sa strings(), icons(), setNumbers()
 */
-QList<int> QtxPagePrefSelectItem::numbers() const
+QList<QVariant> QtxPagePrefSelectItem::numbers() const
 {
-  QList<int> res;
-  for ( uint i = 0; i < mySelector->count(); i++ )
+  QList<QVariant> res;
+  for ( int i = 0; i < mySelector->count(); i++ )
   {
     if ( mySelector->hasId( i ) )
       res.append( mySelector->id( i ) );
@@ -2727,7 +2754,7 @@ QList<int> QtxPagePrefSelectItem::numbers() const
 QList<QIcon> QtxPagePrefSelectItem::icons() const
 {
   QList<QIcon> res;
-  for ( uint i = 0; i < mySelector->count(); i++ )
+  for ( int i = 0; i < mySelector->count(); i++ )
     res.append( mySelector->itemIcon( i ) );
   return res;
 }
@@ -2748,10 +2775,10 @@ void QtxPagePrefSelectItem::setStrings( const QStringList& lst )
   \param ids new list of values IDs
   \sa numbers(), setStrings(), setIcons()
 */
-void QtxPagePrefSelectItem::setNumbers( const QList<int>& ids )
+void QtxPagePrefSelectItem::setNumbers( const QList<QVariant>& ids )
 {
-  uint i = 0;
-  for ( QList<int>::const_iterator it = ids.begin(); it != ids.end(); ++it, i++ ) {
+  int i = 0;
+  for ( QList<QVariant>::const_iterator it = ids.begin(); it != ids.end(); ++it, i++ ) {
     if ( i >= mySelector->count() )
       mySelector->addItem(QString("") );
     
@@ -2769,7 +2796,7 @@ void QtxPagePrefSelectItem::setNumbers( const QList<int>& ids )
 */
 void QtxPagePrefSelectItem::setIcons( const QList<QIcon>& icns )
 {
-  uint i = 0;
+  int i = 0;
   for ( QList<QIcon>::const_iterator it = icns.begin(); it != icns.end() && i < mySelector->count(); ++it, i++ )
     mySelector->setItemIcon( i, *it );
 }
@@ -2785,10 +2812,16 @@ void QtxPagePrefSelectItem::store()
 
   int idx = mySelector->currentIndex();
 
-  if ( mySelector->hasId( idx ) )
-    setInteger( mySelector->id( idx ) );
-  else if ( idx >= 0 )
+  if ( mySelector->hasId( idx ) ) {
+    QVariant id = mySelector->id( idx );
+    if ( id.type() == QVariant::Int )
+      setInteger( id.toInt() );
+    else if ( id.type() == QVariant::String )
+      setString( id.toString() );
+  }
+  else if ( idx >= 0 ) {
     setString( mySelector->itemText( idx ) );
+  }
 }
 
 /*!
@@ -2798,16 +2831,12 @@ void QtxPagePrefSelectItem::store()
 void QtxPagePrefSelectItem::retrieve()
 {
   QString txt = getString();
-
-  int idx = -1;
-
-  bool ok = false;
-  int num = txt.toInt( &ok );
-  if ( ok )
-    idx = mySelector->index( num );
-  else
+  
+  // try to find via the id
+  int idx = mySelector->index( txt );
+  if ( idx < 0 )
   {
-    for ( uint i = 0; i < mySelector->count() && idx == -1; i++ )
+    for ( int i = 0; i < mySelector->count() && idx == -1; i++ )
     {
       if ( mySelector->itemText( i ) == txt )
         idx = i;
@@ -2845,11 +2874,7 @@ QVariant QtxPagePrefSelectItem::optionValue( const QString& name ) const
     return strings();
   else if ( name == "numbers" || name == "ids" || name == "indexes" )
   {
-    QList<QVariant> lst;
-    QList<int> nums = numbers();
-    for ( QList<int>::const_iterator it = nums.begin(); it != nums.end(); ++it )
-      lst.append( *it );
-    return lst;
+    return numbers();
   }
   else if ( name == "icons" || name == "pixmaps" )
   {
@@ -2893,10 +2918,9 @@ void QtxPagePrefSelectItem::setOptionValue( const QString& name, const QVariant&
 */
 void QtxPagePrefSelectItem::setStrings( const QVariant& var )
 {
-  if ( var.type() != QVariant::StringList )
-    return;
-
-  setStrings( var.toStringList() );
+  QStringList values;
+  if ( toStringList( var, values ) )
+    setStrings( values );
 }
 
 /*!
@@ -2909,14 +2933,7 @@ void QtxPagePrefSelectItem::setNumbers( const QVariant& var )
   if ( var.type() != QVariant::List )
     return;
 
-  QList<int> lst;
-  QList<QVariant> varList = var.toList();
-  for ( QList<QVariant>::const_iterator it = varList.begin(); it != varList.end(); ++it )
-  {
-    if ( (*it).canConvert( QVariant::Int ) )
-      lst.append( (*it).toInt() );
-  }
-  setNumbers( lst );
+  setNumbers( var.toList() );
 }
 
 /*!
@@ -3812,8 +3829,9 @@ void QtxPagePrefFontItem::setOptionValue( const QString& name, const QVariant& v
   }
   else if ( name == "fonts" || name == "families" )
   {
-    if ( val.canConvert( QVariant::StringList ) )
-      setFonts( val.toStringList() );
+    QStringList values;
+    if ( toStringList( val, values ) )
+      setFonts( values );
   }
   else if ( name == "sizes" )
   {
@@ -4457,7 +4475,7 @@ void QtxPagePrefShortcutBtnsItem::retrieve()
   \param param resource file parameter associated with the preference item
 */
 QtxPagePrefShortcutTreeItem::QtxPagePrefShortcutTreeItem( const QString& title, QtxPreferenceItem* parent, const QString& sect, 
-                                                          const QString& param ): QtxPageNamedPrefItem( title, parent, sect, "" )
+                                                          const QString& /*param*/ ): QtxPageNamedPrefItem( title, parent, sect, "" )
 {
   mySection = sect;
 
@@ -4792,8 +4810,9 @@ void QtxPagePrefBackgroundItem::setOptionValue( const QString& name, const QVari
       setImageFormats( val.toString() );
   }
   else if ( name == "gradient_names" ) {
-    if ( val.canConvert( QVariant::StringList ) )
-      setGradients( val.toStringList() );
+    QStringList values;
+    if ( toStringList( val, values ) )
+      setGradients( values );
   }
   else if ( name == "gradient_ids" ) {
     if ( val.canConvert( QVariant::List ) ) {