1 // Copyright (C) 2007-2024 CEA, EDF, 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 <QRegExpValidator>
34 static const char* browse_icon[] = {
61 \brief The QtxPathEdit class represents a widget for file or directory
62 path preference items editing.
64 The path preference item is represented as the line edit box for the
65 direct path editing and small button clicking on which invokes browse
66 dialog box. The widget can be used in different modes: "Open File",
67 "Save File", "Select Directory". The mode defines the type of the
68 standard browse dialog box which is invoked on the button clicking.
70 Initial path value can be set with setPath() method. Chosen path
71 can be retrieved with the path() method. The widget mode can be set
72 with setPathType() and retrieved with pathType() method.
74 In addition, file/direcrory filters (wildcards) can be set with the
75 setPathFilter() method and retrieved with pathFilter() method.
80 \param type widget mode (Qtx::PathType)
81 \param parent parent widget
82 \param browse if \c true, automatically finish editing of file path when
83 user presses OK in "Select File/Directory" dialog box
84 \sa pathType(), setPathType()
86 QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent, bool browse )
97 Qtx::PT_OpenFile mode is used by default.
99 \param parent parent widget
100 \param browse if \c true, automatically finish editing of file path when
101 user presses OK in "Select File/Directory" dialog box
102 \sa pathType(), setPathType()
104 QtxPathEdit::QtxPathEdit( QWidget* parent, bool browse )
106 myType( Qtx::PT_OpenFile ),
115 QtxPathEdit::~QtxPathEdit()
120 \brief Get widget mode.
121 \return currently used widget mode (Qtx::PathType)
124 Qtx::PathType QtxPathEdit::pathType() const
130 \brief Set widget mode.
131 \param type new widget mode (Qtx::PathType)
134 void QtxPathEdit::setPathType( const Qtx::PathType type )
136 if ( myType == type )
144 \brief Get currently selected path.
145 \return file or directory path entered by the user
148 QString QtxPathEdit::path() const
150 return myPath->text();
155 \param txt file or directory path
158 void QtxPathEdit::setPath( const QString& txt )
160 myPath->setText( txt );
164 \brief Get currently used path filters.
165 \return file or directory path filters
168 QString QtxPathEdit::pathFilter() const
174 \brief Set path filters.
175 \param f new file or directory path filters
178 void QtxPathEdit::setPathFilter( const QString& f )
188 \brief Set path file dialog options.
189 \param f new file or directory path options
192 void QtxPathEdit::setPathOptions(const QFileDialog::Options options)
194 if ( myOptions == options )
201 \brief Get currently used path options.
202 \return file or directory path options
205 QFileDialog::Options QtxPathEdit::pathOptions() const
212 \brief Called when user clicks "Browse" button.
214 Invokes standard browsng dialog box depending on the used widget mode.
217 \sa mode(), setMode()
219 void QtxPathEdit::onBrowse( bool /*on*/ )
222 QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
223 switch ( pathType() )
225 case Qtx::PT_OpenFile:
226 path = QFileDialog::getOpenFileName( myPath, QString(), initial, pathFilter(), nullptr, pathOptions() );
228 case Qtx::PT_SaveFile:
229 path = QFileDialog::getSaveFileName( myPath, QString(), initial, pathFilter(), nullptr, pathOptions() );
231 case Qtx::PT_Directory:
232 path = QFileDialog::getExistingDirectory( myPath, QString(), initial, pathOptions() );
236 if ( !path.isEmpty() )
237 myPath->setText( QDir::toNativeSeparators( path ) );
241 if ( !path.isEmpty() && myBrowse )
242 QApplication::postEvent( myPath, new QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier ) );
246 \brief Get internal line edit widget.
247 \return line edit box widget
249 QLineEdit* QtxPathEdit::lineEdit() const
255 \brief Perform internal widget intialization.
257 void QtxPathEdit::initialize()
259 QHBoxLayout* base = new QHBoxLayout( this );
260 base->setMargin( 0 );
261 base->setSpacing( 5 );
263 base->addWidget( myPath = new QLineEdit( this ) );
264 myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
266 QToolButton* browse = new QToolButton( this );
267 browse->setIcon( QPixmap( browse_icon ) );
268 base->addWidget( browse );
270 connect( browse, SIGNAL( clicked( bool ) ), this, SLOT( onBrowse( bool ) ) );
272 setFocusProxy( myPath );
278 \brief Update widget state.
280 void QtxPathEdit::updateState()
282 myPath->setCompleter( Qtx::pathCompleter( pathType(), pathFilter() ) );