Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / Qtx / QtxPathEdit.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, 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.
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:      QtxPathEdit.cxx
21 // Author:    Sergey TELKOV
22 //
23 #include "QtxPathEdit.h"
24
25 #include <QLayout>
26 #include <QDirModel>
27 #include <QLineEdit>
28 #include <QCompleter>
29 #include <QToolButton>
30 #include <QFileDialog>
31 #include <QRegExpValidator>
32
33 static const char* browse_icon[] = {
34 "16 16 5 1",
35 "  c none",
36 ". c #ffff00",
37 "# c #848200",
38 "a c #ffffff",
39 "b c #000000",
40 "                ",
41 "          bbb   ",
42 "         b   b b",
43 "              bb",
44 "  bbb        bbb",
45 " ba.abbbbbbb    ",
46 " b.a.a.a.a.b    ",
47 " ba.a.a.a.ab    ",
48 " b.a.abbbbbbbbbb",
49 " ba.ab#########b",
50 " b.ab#########b ",
51 " bab#########b  ",
52 " bb#########b   ",
53 " bbbbbbbbbbb    ",
54 "                ",
55 "                "
56 };
57
58 /*!
59   \class QtxPathEdit
60   \brief The QtxPathEdit class represents a widget for file or directory
61   path preference items editing.
62
63   The path preference item is represented as the line edit box for the 
64   direct path editing and small button clicking on which invokes browse
65   dialog box. The widget can be used in different modes: "Open File", 
66   "Save File", "Select Directory". The mode defines the type of the
67   standard browse dialog box which is invoked on the button clicking.
68
69   Initial path value can be set with setPath() method. Chosen path
70   can be retrieved with the path() method. The widget mode can be set 
71   with setPathType() and retrieved with pathType() method.
72
73   In addition, file/direcrory filters (wildcards) can be set with the
74   setPathFilter() method and retrieved with pathFilter() method.
75 */
76
77 /*!
78   \brief Constructor
79   \param type widget mode (Qtx::PathType)
80   \param parent parent widget
81   \sa pathType(), setPathType()
82 */
83 QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent )
84 : QFrame( parent ),
85   myType( type )
86 {
87   initialize();
88 }
89
90 /*!
91   \brief Constructor
92
93   Qtx::PT_OpenFile mode is used by default.
94
95   \param parent parent widget
96   \sa pathType(), setPathType()
97 */
98 QtxPathEdit::QtxPathEdit( QWidget* parent )
99 : QFrame( parent ),
100   myType( Qtx::PT_OpenFile )
101 {
102   initialize();
103 }
104
105 /*!
106   \brief Destructor
107 */
108 QtxPathEdit::~QtxPathEdit()
109 {
110 }
111
112 /*!
113   \brief Get widget mode.
114   \return currently used widget mode (Qtx::PathType)
115   \sa setPathType()
116 */
117 Qtx::PathType QtxPathEdit::pathType() const
118 {
119   return myType;
120 }
121
122 /*!
123   \brief Set widget mode.
124   \param type new widget mode (Qtx::PathType)
125   \sa pathType()
126 */
127 void QtxPathEdit::setPathType( const Qtx::PathType type )
128 {
129   if ( myType == type )
130     return;
131
132   myType = type;
133   updateState();
134 }
135
136 /*!
137   \brief Get currently selected path.
138   \return file or directory path entered by the user
139   \sa setPath()
140 */
141 QString QtxPathEdit::path() const
142 {
143   return myPath->text();
144 }
145
146 /*!
147   \brief Set path.
148   \param txt file or directory path 
149   \sa path()
150 */
151 void QtxPathEdit::setPath( const QString& txt )
152 {
153   myPath->setText( txt );
154 }
155
156 /*!
157   \brief Get currently used path filters.
158   \return file or directory path filters
159   \sa setPathFilter()
160 */
161 QString QtxPathEdit::pathFilter() const
162 {
163   return myFilter;
164 }
165
166 /*!
167   \brief Set path filters.
168   \param f new file or directory path filters
169   \sa pathFilter()
170 */
171 void QtxPathEdit::setPathFilter( const QString& f )
172 {
173   if ( myFilter == f )
174     return;
175
176   myFilter = f;
177   updateState();
178 }
179
180 /*!
181   \brief Called when user clicks "Browse" button. 
182
183   Invokes standard browsng dialog box depending on the used widget mode.
184
185   \param on (not used)
186   \sa mode(), setMode()
187 */
188 void QtxPathEdit::onBrowse( bool /*on*/ )
189 {
190   QString path;
191   QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
192   switch ( pathType() )
193   {
194   case Qtx::PT_OpenFile:
195     path = QFileDialog::getOpenFileName( myPath, QString(), initial, pathFilter() );
196     break;
197   case Qtx::PT_SaveFile:
198     path = QFileDialog::getSaveFileName( myPath, QString(), initial, pathFilter() );
199     break;
200   case Qtx::PT_Directory:
201     path = QFileDialog::getExistingDirectory( myPath, QString(), initial );
202     break;
203   }
204
205   if ( !path.isEmpty() )
206     myPath->setText( QDir::convertSeparators( path ) ); 
207
208   myPath->setFocus();
209 }
210
211 /*!
212   \brief Get internal line edit widget.
213   \return line edit box widget
214 */
215 QLineEdit* QtxPathEdit::lineEdit() const
216 {
217   return myPath;
218 }
219
220 /*!
221   \brief Perform internal widget intialization.
222 */
223 void QtxPathEdit::initialize()
224 {
225   QHBoxLayout* base = new QHBoxLayout( this );
226   base->setMargin( 0 );
227   base->setSpacing( 5 );
228
229   base->addWidget( myPath = new QLineEdit( this ) );
230   myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
231
232   QToolButton* browse = new QToolButton( this );
233   browse->setIcon( QPixmap( browse_icon ) );
234   base->addWidget( browse );
235
236   connect( browse, SIGNAL( clicked( bool ) ), this, SLOT( onBrowse( bool ) ) );
237
238   setFocusProxy( myPath );
239
240   updateState();
241 }
242
243 /*!
244   \brief Update widget state.
245 */
246 void QtxPathEdit::updateState()
247 {
248   myPath->setCompleter( Qtx::pathCompleter( pathType(), pathFilter() ) );
249 }