1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxPathEdit.cxx
23 // Author: Sergey TELKOV
25 #include "QtxPathEdit.h"
31 #include <QToolButton>
32 #include <QFileDialog>
33 #include <QRegExpValidator>
35 static const char* browse_icon[] = {
62 \brief The QtxPathEdit class represents a widget for file or directory
63 path preference items editing.
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.
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.
75 In addition, file/direcrory filters (wildcards) can be set with the
76 setPathFilter() method and retrieved with pathFilter() method.
81 \param type widget mode (Qtx::PathType)
82 \param parent parent widget
83 \sa pathType(), setPathType()
85 QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent )
95 Qtx::PT_OpenFile mode is used by default.
97 \param parent parent widget
98 \sa pathType(), setPathType()
100 QtxPathEdit::QtxPathEdit( QWidget* parent )
102 myType( Qtx::PT_OpenFile )
110 QtxPathEdit::~QtxPathEdit()
115 \brief Get widget mode.
116 \return currently used widget mode (Qtx::PathType)
119 Qtx::PathType QtxPathEdit::pathType() const
125 \brief Set widget mode.
126 \param type new widget mode (Qtx::PathType)
129 void QtxPathEdit::setPathType( const Qtx::PathType type )
131 if ( myType == type )
139 \brief Get currently selected path.
140 \return file or directory path entered by the user
143 QString QtxPathEdit::path() const
145 return myPath->text();
150 \param txt file or directory path
153 void QtxPathEdit::setPath( const QString& txt )
155 myPath->setText( txt );
159 \brief Get currently used path filters.
160 \return file or directory path filters
163 QString QtxPathEdit::pathFilter() const
169 \brief Set path filters.
170 \param f new file or directory path filters
173 void QtxPathEdit::setPathFilter( const QString& f )
183 \brief Called when user clicks "Browse" button.
185 Invokes standard browsng dialog box depending on the used widget mode.
188 \sa mode(), setMode()
190 void QtxPathEdit::onBrowse( bool /*on*/ )
193 QString initial = QFileInfo( myPath->text() ).path();
194 switch ( pathType() )
196 case Qtx::PT_OpenFile:
197 path = QFileDialog::getOpenFileName( myPath, QString(), initial, pathFilter() );
199 case Qtx::PT_SaveFile:
200 path = QFileDialog::getSaveFileName( myPath, QString(), initial, pathFilter() );
202 case Qtx::PT_Directory:
203 path = QFileDialog::getExistingDirectory( myPath, QString(), initial );
207 if ( !path.isEmpty() )
208 myPath->setText( QDir::convertSeparators( path ) );
214 \brief Get internal line edit widget.
215 \return line edit box widget
217 QLineEdit* QtxPathEdit::lineEdit() const
223 \brief Perform internal widget intialization.
225 void QtxPathEdit::initialize()
227 QHBoxLayout* base = new QHBoxLayout( this );
228 base->setMargin( 0 );
229 base->setSpacing( 5 );
231 base->addWidget( myPath = new QLineEdit( this ) );
232 myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
234 QToolButton* browse = new QToolButton( this );
235 browse->setIcon( QPixmap( browse_icon ) );
236 base->addWidget( browse );
238 connect( browse, SIGNAL( clicked( bool ) ), this, SLOT( onBrowse( bool ) ) );
240 setFocusProxy( myPath );
246 \brief Update widget state.
248 void QtxPathEdit::updateState()
250 myPath->setCompleter( Qtx::pathCompleter( pathType(), pathFilter() ) );