1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: QtxPathEdit.cxx
21 // Author: Sergey TELKOV
23 #include "QtxPathEdit.h"
29 #include <QToolButton>
30 #include <QFileDialog>
31 #include <QRegExpValidator>
33 static const char* browse_icon[] = {
60 \brief The QtxPathEdit class represents a widget for file or directory
61 path preference items editing.
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.
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.
73 In addition, file/direcrory filters (wildcards) can be set with the
74 setPathFilter() method and retrieved with pathFilter() method.
79 \param type widget mode (Qtx::PathType)
80 \param parent parent widget
81 \sa pathType(), setPathType()
83 QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent )
93 Qtx::PT_OpenFile mode is used by default.
95 \param parent parent widget
96 \sa pathType(), setPathType()
98 QtxPathEdit::QtxPathEdit( QWidget* parent )
100 myType( Qtx::PT_OpenFile )
108 QtxPathEdit::~QtxPathEdit()
113 \brief Get widget mode.
114 \return currently used widget mode (Qtx::PathType)
117 Qtx::PathType QtxPathEdit::pathType() const
123 \brief Set widget mode.
124 \param type new widget mode (Qtx::PathType)
127 void QtxPathEdit::setPathType( const Qtx::PathType type )
129 if ( myType == type )
137 \brief Get currently selected path.
138 \return file or directory path entered by the user
141 QString QtxPathEdit::path() const
143 return myPath->text();
148 \param txt file or directory path
151 void QtxPathEdit::setPath( const QString& txt )
153 myPath->setText( txt );
157 \brief Get currently used path filters.
158 \return file or directory path filters
161 QString QtxPathEdit::pathFilter() const
167 \brief Set path filters.
168 \param f new file or directory path filters
171 void QtxPathEdit::setPathFilter( const QString& f )
181 \brief Called when user clicks "Browse" button.
183 Invokes standard browsng dialog box depending on the used widget mode.
186 \sa mode(), setMode()
188 void QtxPathEdit::onBrowse( bool /*on*/ )
191 QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
192 switch ( pathType() )
194 case Qtx::PT_OpenFile:
195 path = QFileDialog::getOpenFileName( myPath, QString(), initial, pathFilter() );
197 case Qtx::PT_SaveFile:
198 path = QFileDialog::getSaveFileName( myPath, QString(), initial, pathFilter() );
200 case Qtx::PT_Directory:
201 path = QFileDialog::getExistingDirectory( myPath, QString(), initial );
205 if ( !path.isEmpty() )
206 myPath->setText( QDir::convertSeparators( path ) );
212 \brief Get internal line edit widget.
213 \return line edit box widget
215 QLineEdit* QtxPathEdit::lineEdit() const
221 \brief Perform internal widget intialization.
223 void QtxPathEdit::initialize()
225 QHBoxLayout* base = new QHBoxLayout( this );
226 base->setMargin( 0 );
227 base->setSpacing( 5 );
229 base->addWidget( myPath = new QLineEdit( this ) );
230 myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
232 QToolButton* browse = new QToolButton( this );
233 browse->setIcon( QPixmap( browse_icon ) );
234 base->addWidget( browse );
236 connect( browse, SIGNAL( clicked( bool ) ), this, SLOT( onBrowse( bool ) ) );
238 setFocusProxy( myPath );
244 \brief Update widget state.
246 void QtxPathEdit::updateState()
248 myPath->setCompleter( Qtx::pathCompleter( pathType(), pathFilter() ) );