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