From c09b0f4e07c593b793b7e06a56f3a8ece7abf60f Mon Sep 17 00:00:00 2001 From: sbh Date: Wed, 12 Sep 2012 11:11:30 +0000 Subject: [PATCH] MTN: Realtive mode was added to QtxPathEdit --- src/Qtx/Qtx.h | 2 ++ src/Qtx/QtxPathEdit.cxx | 46 +++++++++++++++++++++++++++++++++++++++-- src/Qtx/QtxPathEdit.h | 5 +++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Qtx/Qtx.h b/src/Qtx/Qtx.h index 907cb5afa..3291b8178 100755 --- a/src/Qtx/Qtx.h +++ b/src/Qtx/Qtx.h @@ -156,6 +156,8 @@ public: static QString dir( const QString&, const bool = true ); static QString file( const QString&, const bool = true ); static QString extension( const QString&, const bool = false ); + static QString relativePath( const QString&, const QString& ); + static QString absolutePath( const QString&, const QString& ); static QString library( const QString& ); diff --git a/src/Qtx/QtxPathEdit.cxx b/src/Qtx/QtxPathEdit.cxx index c524cbbe1..8e06601f8 100644 --- a/src/Qtx/QtxPathEdit.cxx +++ b/src/Qtx/QtxPathEdit.cxx @@ -196,6 +196,41 @@ void QtxPathEdit::setValidator( QValidator* v ) lineEdit()->setValidator( v ); } +/*! + \brief Set base path. If thePath is not empty the editor + becomes relative path editor. + \param thePath the base path. If thePath is not empty the editor become + relative path editor. If thePath is empty the editor become absolute path + editor +*/ +void QtxPathEdit::setBasePath( const QString& theBasePath ) +{ + if( myBasePath.isEmpty() && !theBasePath.isEmpty() ){ +//Switch to relative path editor + QString aNewPath = Qtx::relativePath( theBasePath, myPath->text() ); + myPath->setText( aNewPath ); + } + if( !myBasePath.isEmpty() && theBasePath.isEmpty() ){ +//Switch to absolute path editor + QString aNewPath = Qtx::relativePath( theBasePath, myPath->text() ); + myPath->setText( aNewPath ); + } + if( !myBasePath.isEmpty() && !theBasePath.isEmpty() ){ + QString anAbsPath = Qtx::absolutePath( myBasePath, myPath->text() ); + QString aNewPath = Qtx::relativePath( theBasePath, anAbsPath ); + myPath->setText( aNewPath ); + } + myBasePath = theBasePath; +} + +/*! + \brief Return base path. + */ +QString QtxPathEdit::basePath() const +{ + return myBasePath; +} + /*! \brief Called when user clicks "Browse" button. @@ -206,8 +241,12 @@ void QtxPathEdit::setValidator( QValidator* v ) */ void QtxPathEdit::onBrowse( bool /*on*/ ) { + emit beforeBrowse(); QString path; - QString initial = QFileInfo( myPath->text() ).path(); + QString initial = myPath->text(); + if( !myBasePath.isEmpty() ) + initial = Qtx::absolutePath( myBasePath, initial ); + initial = QFileInfo( initial ).path(); switch ( pathType() ) { case Qtx::PT_OpenFile: @@ -222,7 +261,10 @@ void QtxPathEdit::onBrowse( bool /*on*/ ) } if ( !path.isEmpty() ) { - QString txt = QDir::convertSeparators( path ); + QString txt = path; + if( !myBasePath.isEmpty() ) + txt = Qtx::relativePath(myBasePath, path ); + txt = QDir::convertSeparators( txt ); bool block = myPath->signalsBlocked(); myPath->blockSignals( true ); myPath->setText( txt ); diff --git a/src/Qtx/QtxPathEdit.h b/src/Qtx/QtxPathEdit.h index d85312ce7..4e084573b 100644 --- a/src/Qtx/QtxPathEdit.h +++ b/src/Qtx/QtxPathEdit.h @@ -52,10 +52,14 @@ public: const QValidator* validator() const; void setValidator( QValidator* ); + void setBasePath( const QString& theBasePath ); + QString basePath() const; + signals: void returnPressed(); void pathChanged( const QString& ); void pathSelected( const QString& ); + void beforeBrowse(); private slots: void onBrowse( bool = false ); @@ -68,6 +72,7 @@ private: QLineEdit* myPath; Qtx::PathType myType; QString myFilter; + QString myBasePath; }; #endif -- 2.39.2