Salome HOME
Copyrights update 2015.
[modules/gui.git] / src / Qtx / QtxPathEdit.cxx
1 // Copyright (C) 2007-2015  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, 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:      QtxPathEdit.cxx
21 // Author:    Sergey TELKOV
22 //
23 #include "QtxPathEdit.h"
24
25 #include <QApplication>
26 #include <QLayout>
27 #include <QDirModel>
28 #include <QLineEdit>
29 #include <QCompleter>
30 #include <QKeyEvent>
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   \param browse if \c true, automatically finish editing of file path when
84                 user presses OK in "Select File/Directory" dialog box
85   \sa pathType(), setPathType()
86 */
87 QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent, bool browse )
88 : QFrame( parent ),
89   myType( type ),
90   myBrowse ( browse )
91 {
92   initialize();
93 }
94
95 /*!
96   \brief Constructor
97
98   Qtx::PT_OpenFile mode is used by default.
99
100   \param parent parent widget
101   \param browse if \c true, automatically finish editing of file path when
102                 user presses OK in "Select File/Directory" dialog box
103   \sa pathType(), setPathType()
104 */
105 QtxPathEdit::QtxPathEdit( QWidget* parent, bool browse )
106 : QFrame( parent ),
107   myType( Qtx::PT_OpenFile ),
108   myBrowse ( browse )
109 {
110   initialize();
111 }
112
113 /*!
114   \brief Destructor
115 */
116 QtxPathEdit::~QtxPathEdit()
117 {
118 }
119
120 /*!
121   \brief Get widget mode.
122   \return currently used widget mode (Qtx::PathType)
123   \sa setPathType()
124 */
125 Qtx::PathType QtxPathEdit::pathType() const
126 {
127   return myType;
128 }
129
130 /*!
131   \brief Set widget mode.
132   \param type new widget mode (Qtx::PathType)
133   \sa pathType()
134 */
135 void QtxPathEdit::setPathType( const Qtx::PathType type )
136 {
137   if ( myType == type )
138     return;
139
140   myType = type;
141   updateState();
142 }
143
144 /*!
145   \brief Get currently selected path.
146   \return file or directory path entered by the user
147   \sa setPath()
148 */
149 QString QtxPathEdit::path() const
150 {
151   return myPath->text();
152 }
153
154 /*!
155   \brief Set path.
156   \param txt file or directory path 
157   \sa path()
158 */
159 void QtxPathEdit::setPath( const QString& txt )
160 {
161   myPath->setText( txt );
162 }
163
164 /*!
165   \brief Get currently used path filters.
166   \return file or directory path filters
167   \sa setPathFilter()
168 */
169 QString QtxPathEdit::pathFilter() const
170 {
171   return myFilter;
172 }
173
174 /*!
175   \brief Set path filters.
176   \param f new file or directory path filters
177   \sa pathFilter()
178 */
179 void QtxPathEdit::setPathFilter( const QString& f )
180 {
181   if ( myFilter == f )
182     return;
183
184   myFilter = f;
185   updateState();
186 }
187
188 /*!
189   \brief Called when user clicks "Browse" button. 
190
191   Invokes standard browsng dialog box depending on the used widget mode.
192
193   \param on (not used)
194   \sa mode(), setMode()
195 */
196 void QtxPathEdit::onBrowse( bool /*on*/ )
197 {
198   QString path;
199   QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
200   switch ( pathType() )
201   {
202   case Qtx::PT_OpenFile:
203     path = QFileDialog::getOpenFileName( myPath, QString(), initial, pathFilter() );
204     break;
205   case Qtx::PT_SaveFile:
206     path = QFileDialog::getSaveFileName( myPath, QString(), initial, pathFilter() );
207     break;
208   case Qtx::PT_Directory:
209     path = QFileDialog::getExistingDirectory( myPath, QString(), initial );
210     break;
211   }
212
213   if ( !path.isEmpty() )
214     myPath->setText( QDir::convertSeparators( path ) ); 
215
216   myPath->setFocus();
217
218   if ( !path.isEmpty() && myBrowse )
219     QApplication::postEvent( myPath, new QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier ) );
220 }
221
222 /*!
223   \brief Get internal line edit widget.
224   \return line edit box widget
225 */
226 QLineEdit* QtxPathEdit::lineEdit() const
227 {
228   return myPath;
229 }
230
231 /*!
232   \brief Perform internal widget intialization.
233 */
234 void QtxPathEdit::initialize()
235 {
236   QHBoxLayout* base = new QHBoxLayout( this );
237   base->setMargin( 0 );
238   base->setSpacing( 5 );
239
240   base->addWidget( myPath = new QLineEdit( this ) );
241   myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
242
243   QToolButton* browse = new QToolButton( this );
244   browse->setIcon( QPixmap( browse_icon ) );
245   base->addWidget( browse );
246
247   connect( browse, SIGNAL( clicked( bool ) ), this, SLOT( onBrowse( bool ) ) );
248
249   setFocusProxy( myPath );
250
251   updateState();
252 }
253
254 /*!
255   \brief Update widget state.
256 */
257 void QtxPathEdit::updateState()
258 {
259   myPath->setCompleter( Qtx::pathCompleter( pathType(), pathFilter() ) );
260 }