Salome HOME
Clear info panel when module is deactivated
[modules/gui.git] / src / Qtx / QtxPathEdit.cxx
index 1504448d6fa95cf970bbad99b853210ad996cdda..a35ad5671e3f0ac6a537df2e748759eeeab384d6 100644 (file)
@@ -1,33 +1,33 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-//
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // File:      QtxPathEdit.cxx
 // Author:    Sergey TELKOV
 //
 #include "QtxPathEdit.h"
 
+#include <QApplication>
 #include <QLayout>
 #include <QDirModel>
 #include <QLineEdit>
 #include <QCompleter>
+#include <QKeyEvent>
 #include <QToolButton>
 #include <QFileDialog>
 #include <QRegExpValidator>
@@ -80,11 +80,14 @@ static const char* browse_icon[] = {
   \brief Constructor
   \param type widget mode (Qtx::PathType)
   \param parent parent widget
+  \param browse if \c true, automatically finish editing of file path when
+                user presses OK in "Select File/Directory" dialog box
   \sa pathType(), setPathType()
 */
-QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent )
+QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent, bool browse )
 : QFrame( parent ),
-  myType( type )
+  myType( type ),
+  myBrowse ( browse )
 {
   initialize();
 }
@@ -95,11 +98,14 @@ QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent )
   Qtx::PT_OpenFile mode is used by default.
 
   \param parent parent widget
+  \param browse if \c true, automatically finish editing of file path when
+                user presses OK in "Select File/Directory" dialog box
   \sa pathType(), setPathType()
 */
-QtxPathEdit::QtxPathEdit( QWidget* parent )
+QtxPathEdit::QtxPathEdit( QWidget* parent, bool browse )
 : QFrame( parent ),
-  myType( Qtx::PT_OpenFile )
+  myType( Qtx::PT_OpenFile ),
+  myBrowse ( browse )
 {
   initialize();
 }
@@ -190,7 +196,7 @@ void QtxPathEdit::setPathFilter( const QString& f )
 void QtxPathEdit::onBrowse( bool /*on*/ )
 {
   QString path;
-  QString initial = QFileInfo( myPath->text() ).path();
+  QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
   switch ( pathType() )
   {
   case Qtx::PT_OpenFile:
@@ -205,9 +211,12 @@ void QtxPathEdit::onBrowse( bool /*on*/ )
   }
 
   if ( !path.isEmpty() )
-    myPath->setText( QDir::convertSeparators( path ) ); 
+    myPath->setText( QDir::toNativeSeparators( path ) ); 
 
   myPath->setFocus();
+
+  if ( !path.isEmpty() && myBrowse )
+    QApplication::postEvent( myPath, new QKeyEvent( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier ) );
 }
 
 /*!
@@ -229,7 +238,7 @@ void QtxPathEdit::initialize()
   base->setSpacing( 5 );
 
   base->addWidget( myPath = new QLineEdit( this ) );
-  myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
+  myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
 
   QToolButton* browse = new QToolButton( this );
   browse->setIcon( QPixmap( browse_icon ) );