1 // Copyright (C) 2007-2022 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, or (at your option) any later version.
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"
25 #include <QApplication>
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 \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()
87 QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent, bool browse )
98 Qtx::PT_OpenFile mode is used by default.
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()
105 QtxPathEdit::QtxPathEdit( QWidget* parent, bool browse )
107 myType( Qtx::PT_OpenFile ),
116 QtxPathEdit::~QtxPathEdit()
121 \brief Get widget mode.
122 \return currently used widget mode (Qtx::PathType)
125 Qtx::PathType QtxPathEdit::pathType() const
131 \brief Set widget mode.
132 \param type new widget mode (Qtx::PathType)
135 void QtxPathEdit::setPathType( const Qtx::PathType type )
137 if ( myType == type )
145 \brief Get currently selected path.
146 \return file or directory path entered by the user
149 QString QtxPathEdit::path() const
151 return myPath->text();
156 \param txt file or directory path
159 void QtxPathEdit::setPath( const QString& txt )
161 myPath->setText( txt );
165 \brief Get currently used path filters.
166 \return file or directory path filters
169 QString QtxPathEdit::pathFilter() const
175 \brief Set path filters.
176 \param f new file or directory path filters
179 void QtxPathEdit::setPathFilter( const QString& f )
189 \brief Called when user clicks "Browse" button.
191 Invokes standard browsng dialog box depending on the used widget mode.
194 \sa mode(), setMode()
196 void QtxPathEdit::onBrowse( bool /*on*/ )
199 QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
200 switch ( pathType() )
202 case Qtx::PT_OpenFile:
203 path = QFileDialog::getOpenFileName( myPath, QString(), initial, pathFilter() );
205 case Qtx::PT_SaveFile:
206 path = QFileDialog::getSaveFileName( myPath, QString(), initial, pathFilter() );
208 case Qtx::PT_Directory:
209 path = QFileDialog::getExistingDirectory( myPath, QString(), initial );
213 if ( !path.isEmpty() )
214 myPath->setText( QDir::toNativeSeparators( path ) );
218 if ( !path.isEmpty() && myBrowse )
219 QApplication::postEvent( myPath, new QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier ) );
223 \brief Get internal line edit widget.
224 \return line edit box widget
226 QLineEdit* QtxPathEdit::lineEdit() const
232 \brief Perform internal widget intialization.
234 void QtxPathEdit::initialize()
236 QHBoxLayout* base = new QHBoxLayout( this );
237 base->setMargin( 0 );
238 base->setSpacing( 5 );
240 base->addWidget( myPath = new QLineEdit( this ) );
241 myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
243 QToolButton* browse = new QToolButton( this );
244 browse->setIcon( QPixmap( browse_icon ) );
245 base->addWidget( browse );
247 connect( browse, SIGNAL( clicked( bool ) ), this, SLOT( onBrowse( bool ) ) );
249 setFocusProxy( myPath );
255 \brief Update widget state.
257 void QtxPathEdit::updateState()
259 myPath->setCompleter( Qtx::pathCompleter( pathType(), pathFilter() ) );