Salome HOME
0fd2e20e111ab04c7959cedfd04b8a94af4b6ee9
[modules/gui.git] / src / SUIT / SUIT_PreferenceMgr.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
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, or (at your option) any later version.
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
20 // File:      SUIT_PreferenceMgr.cxx
21 // Author:    Sergey TELKOV
22 //
23 #include "SUIT_PreferenceMgr.h"
24
25 SUIT_PreferenceMgr::SUIT_PreferenceMgr( QtxResourceMgr* resMgr, QWidget* parent )
26 : QtxPagePrefMgr( resMgr, parent ),
27 myRoot( 0 )
28 {
29 }
30
31 SUIT_PreferenceMgr::~SUIT_PreferenceMgr()
32 {
33 }
34
35 QIcon SUIT_PreferenceMgr::itemIcon( const int id ) const
36 {
37   const QtxPreferenceItem* item = 0;
38   if ( id == -1 )
39     item = this;
40   else
41     item = findItem( id, true );
42   return item ? item->icon() : QIcon();
43 }
44
45 void SUIT_PreferenceMgr::setItemIcon( const int id, const QIcon& ico )
46 {
47   QtxPreferenceItem* item = id == -1 ? this : findItem( id, true );
48   if ( item )
49     item->setIcon( ico );
50 }
51
52 QVariant SUIT_PreferenceMgr::itemProperty( const QString& prop, const int id ) const
53 {
54   const QtxPreferenceItem* item = 0;
55   if ( id == -1 )
56     item = this;
57   else
58     item = findItem( id, true );
59   return item ? item->option( prop ) : QVariant();
60 }
61
62 void SUIT_PreferenceMgr::setItemProperty( const QString& prop, const QVariant& val, const int id )
63 {
64   QtxPreferenceItem* item = id == -1 ? this : findItem( id, true );
65   if ( item )
66     item->setOption( prop, val );
67 }
68
69 int SUIT_PreferenceMgr::addItem( const QString& title, const int pId,
70                                  const SUIT_PreferenceMgr::PrefItemType type,
71                                  const QString& sect, const QString& param )
72 {
73   QtxPreferenceItem* parent = 0;
74   if ( pId == -1 )
75   {
76     if ( !myRoot )
77       myRoot = new QtxPagePrefListItem( QString( "root" ), this );
78     parent = myRoot;
79   }
80   else
81   {
82     parent = findItem( pId, true );
83   }
84
85   if ( !parent )
86     return -1;
87
88   QtxPreferenceItem* item = parent->findItem( title, false );
89
90   if ( item && item->depth() < 5 )
91     return item->id();
92
93   switch( type )
94   {
95   case Auto:
96     switch ( parent->depth() )
97     {
98     case 1:
99       item = new QtxPagePrefTabsItem( title, parent, sect, param );
100       break;
101     case 2:
102       item = new QtxPagePrefFrameItem( title, parent, sect, param, true );
103       break;
104     case 3:
105       item = new QtxPagePrefGroupItem( title, parent, sect, param );
106       break;
107     }
108     break;
109   case Space:
110     item = new QtxPagePrefSpaceItem( parent );
111     break;
112   case Bool:
113     item = new QtxPagePrefCheckItem( title, parent, sect, param );
114     break;
115   case Color:
116     item = new QtxPagePrefColorItem( title, parent, sect, param );
117     break;
118   case String:
119     item = new QtxPagePrefEditItem( QtxPagePrefEditItem::String, title, parent, sect, param );
120     break;
121   case Selector:
122     item = new QtxPagePrefSelectItem( title, parent, sect, param );
123     break;
124   case DblSpin:
125     item = new QtxPagePrefSpinItem( QtxPagePrefSpinItem::Double, title, parent, sect, param );
126     break;
127   case IntSpin:
128     item = new QtxPagePrefSpinItem( QtxPagePrefSpinItem::Integer, title, parent, sect, param );
129     break;
130   case Double:
131     item = new QtxPagePrefEditItem( QtxPagePrefEditItem::Double, title, parent, sect, param );
132     break;
133   case Integer:
134     item = new QtxPagePrefEditItem( QtxPagePrefEditItem::Integer, title, parent, sect, param );
135     break;
136   case Slider:
137     item = new QtxPagePrefSliderItem( title, parent, sect, param );
138     break;
139   case GroupBox:
140     item = new QtxPagePrefGroupItem( title, parent, sect, param );
141     break;
142   case Tab:
143     item = new QtxPagePrefTabsItem( title, parent, sect, param );
144     break;
145   case Frame:
146     item = new QtxPagePrefFrameItem( title, parent, sect, param );
147     break;
148   case Font:
149     item = new QtxPagePrefFontItem( title, parent, sect, param );
150     break;
151   case File:
152     item = new QtxPagePrefPathItem( Qtx::PT_OpenFile, title, parent, sect, param );
153     break;
154   case Directory:
155     item = new QtxPagePrefPathItem( Qtx::PT_Directory, title, parent, sect, param );
156     break;
157   case DirList:
158     item = new QtxPagePrefPathListItem( Qtx::PT_Directory, title, parent, sect, param );
159     break;
160   case Shortcut:
161     item = new QtxPagePrefShortcutBtnsItem( title, parent, sect, param );
162     break;
163   case ShortcutTree:
164     item = new QtxPagePrefShortcutTreeItem( title, parent, sect, param );
165     break;
166   case BiColor:
167     item = new QtxPagePrefBiColorItem( title, parent, sect, param );
168     break;
169   case Background:
170     item = new QtxPagePrefBackgroundItem( title, parent, sect, param );
171     break;
172   case UserDefined:
173     item = new QtxUserDefinedItem(parent);
174     break;
175   }
176
177   return item ? item->id() : -1;
178 }
179
180 void SUIT_PreferenceMgr::removeItem( const QString& title )
181 {
182   if ( myRoot )
183   {
184     QtxPreferenceItem* item = myRoot->findItem( title, false );
185     if ( item ) {
186       QtxPagePrefMgr::removeItem( item );
187       delete item;
188     }
189   }
190 }
191
192 QVariant SUIT_PreferenceMgr::optionValue( const QString& name ) const
193 {
194   QVariant val = QtxPagePrefMgr::optionValue( name );
195   if ( !val.isValid() && myRoot )
196     val = myRoot->option( name );
197   return val;
198 }
199
200 void SUIT_PreferenceMgr::setOptionValue( const QString& name, const QVariant& val )
201 {
202   QtxPagePrefMgr::setOptionValue( name, val );
203   if ( myRoot )
204     myRoot->setOption( name, val );
205 }
206
207 QtxPreferenceItem* SUIT_PreferenceMgr::root() const
208 {
209   return myRoot;
210 }