|
| 1 | +/*************************************************************************** |
| 2 | + qgsfilenameconfigdlg.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : 2015-11-26 |
| 5 | + Copyright : (C) 2015 Médéric Ribreux |
| 6 | + Email : mederic.ribreux at medspx dot fr |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgsfilenameconfigdlg.h" |
| 17 | +#include "qgsproject.h" |
| 18 | + |
| 19 | +#include <QFileDialog> |
| 20 | +#include <QSettings> |
| 21 | + |
| 22 | +class QgsFileNameWidgetWrapper; |
| 23 | + |
| 24 | +QgsFileNameConfigDlg::QgsFileNameConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) |
| 25 | + : QgsEditorConfigWidget( vl, fieldIdx, parent ) |
| 26 | +{ |
| 27 | + setupUi( this ); |
| 28 | + |
| 29 | + // By default, uncheck some options |
| 30 | + mUseLink->setChecked( false ); |
| 31 | + mFullUrl->setChecked( false ); |
| 32 | + mRootPath->setPlaceholderText( QSettings().value( "/UI/lastFileNameWidgetDir", QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) ) ).toString() ); |
| 33 | + |
| 34 | + // Add connection to button for choosing default path |
| 35 | + connect( mRootPathButton, SIGNAL( clicked() ), this, SLOT( chooseDefaultPath() ) ); |
| 36 | + |
| 37 | + // Activate Relative Default Path option only if Default Path is set |
| 38 | + connect( mRootPath, SIGNAL( textChanged( const QString & ) ), this, SLOT( enableRelativeDefault() ) ); |
| 39 | + |
| 40 | + // Dynamic GroupBox for relative paths option |
| 41 | + connect( mRelativeGroupBox, SIGNAL( toggled( bool ) ), this, SLOT( enableRelative( bool ) ) ); |
| 42 | + |
| 43 | + // set ids for StorageTypeButtons |
| 44 | + mStorageButtonGroup->setId( mStoreFilesButton, 0 ); |
| 45 | + mStorageButtonGroup->setId( mStoreDirsButton, 1 ); |
| 46 | + |
| 47 | + // First button is toggled by default |
| 48 | + mStorageButtonGroup->button( 0 )->toggle(); |
| 49 | + |
| 50 | + // set ids for RelativeButtons |
| 51 | + mRelativeButtonGroup->setId( mRelativeProj, 0 ); |
| 52 | + mRelativeButtonGroup->setId( mRelativeDefault, 1 ); |
| 53 | + |
| 54 | + mRelativeButtonGroup->button( 0 )->toggle(); |
| 55 | +} |
| 56 | + |
| 57 | +// Choose a base directory for rootPath |
| 58 | +void QgsFileNameConfigDlg::chooseDefaultPath() |
| 59 | +{ |
| 60 | + QString dir; |
| 61 | + if ( !mRootPath->text().isEmpty() ) |
| 62 | + dir = mRootPath->text(); |
| 63 | + else |
| 64 | + dir = QSettings().value( "/UI/lastFileNameWidgetDir", QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) ) ).toString(); |
| 65 | + |
| 66 | + QString rootName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), dir, QFileDialog::ShowDirsOnly ); |
| 67 | + |
| 68 | + if ( rootName.isNull() ) |
| 69 | + return; |
| 70 | + |
| 71 | + mRootPath->setText( rootName ); |
| 72 | +} |
| 73 | + |
| 74 | +// Modify RelativeDefault according to mRootPath content |
| 75 | +void QgsFileNameConfigDlg::enableRelativeDefault() |
| 76 | +{ |
| 77 | + // Activate (or not) the RelativeDefault button if default path |
| 78 | + if ( mRelativeGroupBox->isChecked() ) |
| 79 | + mRelativeDefault->setEnabled( !mRootPath->text().isEmpty() ); |
| 80 | + |
| 81 | + // If no default path, RelativeProj button enabled by default |
| 82 | + if ( mRootPath->text().isEmpty() ) |
| 83 | + mRelativeProj->toggle(); |
| 84 | +} |
| 85 | + |
| 86 | +// Dynamic activation of RelativeGroupBox |
| 87 | +void QgsFileNameConfigDlg::enableRelative( bool state ) |
| 88 | +{ |
| 89 | + if ( state ) |
| 90 | + { |
| 91 | + mRelativeProj->setEnabled( true ); |
| 92 | + if ( mRootPath->text().isEmpty() ) |
| 93 | + mRelativeDefault->setEnabled( false ); |
| 94 | + else |
| 95 | + mRelativeDefault->setEnabled( true ); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + mRelativeProj->setEnabled( false ); |
| 100 | + mRelativeDefault->setEnabled( false ); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// All non mandatory options are not stored to reduce |
| 105 | +// the file size of the project file ! |
| 106 | +QgsEditorWidgetConfig QgsFileNameConfigDlg::config() |
| 107 | +{ |
| 108 | + QgsEditorWidgetConfig cfg; |
| 109 | + |
| 110 | + if ( mUseLink->isChecked() ) |
| 111 | + { |
| 112 | + cfg.insert( "UseLink", mUseLink->isChecked() ); |
| 113 | + if ( mFullUrl->isChecked() ) |
| 114 | + cfg.insert( "FullUrl", mFullUrl->isChecked() ); |
| 115 | + } |
| 116 | + |
| 117 | + if ( !mRootPath->text().isEmpty() ) |
| 118 | + cfg.insert( "DefaultRoot", mRootPath->text() ); |
| 119 | + |
| 120 | + // Save Storage Mode |
| 121 | + int but = mStorageButtonGroup->checkedId(); |
| 122 | + if ( but != -1 ) |
| 123 | + { |
| 124 | + if ( but == 0 ) |
| 125 | + cfg.insert( "StorageMode", "Files" ); |
| 126 | + else if ( but == 1 ) |
| 127 | + cfg.insert( "StorageMode", "Dirs" ); |
| 128 | + } |
| 129 | + |
| 130 | + // Save Relative Paths option |
| 131 | + if ( mRelativeGroupBox->isChecked() ) |
| 132 | + { |
| 133 | + but = mRelativeButtonGroup->checkedId(); |
| 134 | + if ( but != -1 ) |
| 135 | + { |
| 136 | + if ( but == 0 ) |
| 137 | + cfg.insert( "RelativeStorage", "Project" ); |
| 138 | + else if ( but == 1 ) |
| 139 | + cfg.insert( "RelativeStorage", "Default" ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return cfg; |
| 144 | +} |
| 145 | + |
| 146 | +// Set the configuration on the widget |
| 147 | +void QgsFileNameConfigDlg::setConfig( const QgsEditorWidgetConfig& config ) |
| 148 | +{ |
| 149 | + if ( config.contains( "UseLink" ) ) |
| 150 | + { |
| 151 | + mUseLink->setChecked( true ); |
| 152 | + if ( config.contains( "FullUrl" ) ) |
| 153 | + mFullUrl->setChecked( true ); |
| 154 | + } |
| 155 | + |
| 156 | + if ( config.contains( "DefaultRoot" ) ) |
| 157 | + mRootPath->setText( config.value( "DefaultRoot" ).toString() ); |
| 158 | + |
| 159 | + if ( config.contains( "RelativeStorage" ) ) |
| 160 | + { |
| 161 | + mRelativeGroupBox->setChecked( true ); |
| 162 | + if ( config.value( "RelativeStorage" ) == "Default" ) |
| 163 | + mRelativeDefault->toggle(); |
| 164 | + else if ( config.value( "RelativeStorage" ) == "Project" ) |
| 165 | + mRelativeProj->toggle(); |
| 166 | + } |
| 167 | + |
| 168 | + // set storage mode |
| 169 | + if ( config.contains( "StorageMode" ) ) |
| 170 | + { |
| 171 | + if ( config.value( "StorageMode" ) == "Files" ) |
| 172 | + mStorageButtonGroup->button( 0 )->toggle(); |
| 173 | + else if ( config.value( "StorageMode" ) == "Dirs" ) |
| 174 | + mStorageButtonGroup->button( 1 )->toggle(); |
| 175 | + } |
| 176 | +} |
0 commit comments