Back to index

salome-gui  6.5.0
QtxPathEdit.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File:      QtxPathEdit.cxx
00021 // Author:    Sergey TELKOV
00022 //
00023 #include "QtxPathEdit.h"
00024 
00025 #include <QLayout>
00026 #include <QDirModel>
00027 #include <QLineEdit>
00028 #include <QCompleter>
00029 #include <QToolButton>
00030 #include <QFileDialog>
00031 #include <QRegExpValidator>
00032 
00033 static const char* browse_icon[] = {
00034 "16 16 5 1",
00035 "  c none",
00036 ". c #ffff00",
00037 "# c #848200",
00038 "a c #ffffff",
00039 "b c #000000",
00040 "                ",
00041 "          bbb   ",
00042 "         b   b b",
00043 "              bb",
00044 "  bbb        bbb",
00045 " ba.abbbbbbb    ",
00046 " b.a.a.a.a.b    ",
00047 " ba.a.a.a.ab    ",
00048 " b.a.abbbbbbbbbb",
00049 " ba.ab#########b",
00050 " b.ab#########b ",
00051 " bab#########b  ",
00052 " bb#########b   ",
00053 " bbbbbbbbbbb    ",
00054 "                ",
00055 "                "
00056 };
00057 
00083 QtxPathEdit::QtxPathEdit( const Qtx::PathType type, QWidget* parent )
00084 : QFrame( parent ),
00085   myType( type )
00086 {
00087   initialize();
00088 }
00089 
00098 QtxPathEdit::QtxPathEdit( QWidget* parent )
00099 : QFrame( parent ),
00100   myType( Qtx::PT_OpenFile )
00101 {
00102   initialize();
00103 }
00104 
00108 QtxPathEdit::~QtxPathEdit()
00109 {
00110 }
00111 
00117 Qtx::PathType QtxPathEdit::pathType() const
00118 {
00119   return myType;
00120 }
00121 
00127 void QtxPathEdit::setPathType( const Qtx::PathType type )
00128 {
00129   if ( myType == type )
00130     return;
00131 
00132   myType = type;
00133   updateState();
00134 }
00135 
00141 QString QtxPathEdit::path() const
00142 {
00143   return myPath->text();
00144 }
00145 
00151 void QtxPathEdit::setPath( const QString& txt )
00152 {
00153   myPath->setText( txt );
00154 }
00155 
00161 QString QtxPathEdit::pathFilter() const
00162 {
00163   return myFilter;
00164 }
00165 
00171 void QtxPathEdit::setPathFilter( const QString& f )
00172 {
00173   if ( myFilter == f )
00174     return;
00175 
00176   myFilter = f;
00177   updateState();
00178 }
00179 
00188 void QtxPathEdit::onBrowse( bool /*on*/ )
00189 {
00190   QString path;
00191   QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
00192   switch ( pathType() )
00193   {
00194   case Qtx::PT_OpenFile:
00195     path = QFileDialog::getOpenFileName( myPath, QString(), initial, pathFilter() );
00196     break;
00197   case Qtx::PT_SaveFile:
00198     path = QFileDialog::getSaveFileName( myPath, QString(), initial, pathFilter() );
00199     break;
00200   case Qtx::PT_Directory:
00201     path = QFileDialog::getExistingDirectory( myPath, QString(), initial );
00202     break;
00203   }
00204 
00205   if ( !path.isEmpty() )
00206     myPath->setText( QDir::convertSeparators( path ) ); 
00207 
00208   myPath->setFocus();
00209 }
00210 
00215 QLineEdit* QtxPathEdit::lineEdit() const
00216 {
00217   return myPath;
00218 }
00219 
00223 void QtxPathEdit::initialize()
00224 {
00225   QHBoxLayout* base = new QHBoxLayout( this );
00226   base->setMargin( 0 );
00227   base->setSpacing( 5 );
00228 
00229   base->addWidget( myPath = new QLineEdit( this ) );
00230   myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
00231 
00232   QToolButton* browse = new QToolButton( this );
00233   browse->setIcon( QPixmap( browse_icon ) );
00234   base->addWidget( browse );
00235 
00236   connect( browse, SIGNAL( clicked( bool ) ), this, SLOT( onBrowse( bool ) ) );
00237 
00238   setFocusProxy( myPath );
00239 
00240   updateState();
00241 }
00242 
00246 void QtxPathEdit::updateState()
00247 {
00248   myPath->setCompleter( Qtx::pathCompleter( pathType(), pathFilter() ) );
00249 }