25 #include <QAbstractListModel> 28 #include <QFileDialog> 29 #include <QModelIndex> 30 #include <QPixmapCache> 39 : QAbstractListModel( parent )
46 : QAbstractListModel( parent )
59 QString entry =
mSvgFiles.at( index.row() );
61 if ( role == Qt::DecorationRole )
64 if ( !QPixmapCache::find( entry, pixmap ) )
69 bool fillParam, outlineParam, outlineWidthParam;
74 pixmap = QPixmap::fromImage( img );
75 QPixmapCache::insert( entry, pixmap );
80 else if ( role == Qt::UserRole || role == Qt::ToolTipRole )
92 : QStandardItemModel( parent )
95 QStandardItem *parentItem = invisibleRootItem();
97 for (
int i = 0; i < svgPaths.size(); i++ )
99 QDir dir( svgPaths[i] );
100 QStandardItem *baseGroup;
104 baseGroup =
new QStandardItem(
tr(
"App Symbols" ) );
108 baseGroup =
new QStandardItem(
tr(
"User Symbols" ) );
112 baseGroup =
new QStandardItem( dir.dirName() );
114 baseGroup->setData( QVariant( svgPaths[i] ) );
115 baseGroup->setEditable(
false );
116 baseGroup->setCheckable(
false );
117 baseGroup->setIcon( QgsApplication::style()->standardIcon( QStyle::SP_DirIcon ) );
118 baseGroup->setToolTip( dir.path() );
119 parentItem->appendRow( baseGroup );
120 createTree( baseGroup );
121 QgsDebugMsg( QString(
"SVG base path %1: %2" ).arg( i ).arg( baseGroup->data().toString() ) );
125 void QgsSvgSelectorGroupsModel::createTree( QStandardItem* &parentGroup )
127 QDir parentDir( parentGroup->data().toString() );
128 foreach ( QString item, parentDir.entryList( QDir::Dirs | QDir::NoDotAndDotDot ) )
130 QStandardItem* group =
new QStandardItem( item );
131 group->setData( QVariant( parentDir.path() +
"/" + item ) );
132 group->setEditable(
false );
133 group->setCheckable(
false );
134 group->setToolTip( parentDir.path() +
"/" + item );
135 group->setIcon( QgsApplication::style()->standardIcon( QStyle::SP_DirIcon ) );
136 parentGroup->appendRow( group );
150 mGroupsTreeView->setHeaderHidden(
true );
153 connect( mImagesListView->selectionModel(), SIGNAL( currentChanged(
const QModelIndex&,
const QModelIndex& ) ),
154 this, SLOT( svgSelectionChanged(
const QModelIndex& ) ) );
155 connect( mGroupsTreeView->selectionModel(), SIGNAL( currentChanged(
const QModelIndex&,
const QModelIndex& ) ),
156 this, SLOT( populateIcons(
const QModelIndex& ) ) );
160 || settings.value(
"/Windows/SvgSelectorWidget/RelativePath" ).toBool() );
161 mRelativePathChkBx->setChecked( useRelativePath );
167 settings.setValue(
"/Windows/SvgSelectorWidget/RelativePath", mRelativePathChkBx->isChecked() );
172 QString updatedPath(
"" );
175 if ( !svgPath.contains(
"://" ) || ( svgPath.contains(
"file://", Qt::CaseInsensitive ) ) )
178 if ( !resolvedPath.isNull() )
180 updatedPath = resolvedPath;
184 mCurrentSvgPath = updatedPath;
186 mFileLineEdit->blockSignals(
true );
187 mFileLineEdit->setText( updatedPath );
188 mFileLineEdit->blockSignals(
false );
190 mImagesListView->selectionModel()->blockSignals(
true );
191 QAbstractItemModel* m = mImagesListView->model();
192 QItemSelectionModel* selModel = mImagesListView->selectionModel();
193 for (
int i = 0; i < m->rowCount(); i++ )
195 QModelIndex idx( m->index( i, 0 ) );
196 if ( m->data( idx ).toString() == svgPath )
198 selModel->select( idx, QItemSelectionModel::SelectCurrent );
199 selModel->setCurrentIndex( idx, QItemSelectionModel::SelectCurrent );
200 mImagesListView->scrollTo( idx );
204 mImagesListView->selectionModel()->blockSignals(
false );
209 if ( mRelativePathChkBx->isChecked() )
212 return mCurrentSvgPath;
220 void QgsSvgSelectorWidget::updateCurrentSvgPath(
const QString& svgPath )
222 mCurrentSvgPath = svgPath;
226 void QgsSvgSelectorWidget::svgSelectionChanged(
const QModelIndex& idx )
228 QString filePath = idx.data( Qt::UserRole ).toString();
229 mFileLineEdit->setText( filePath );
230 updateCurrentSvgPath( filePath );
233 void QgsSvgSelectorWidget::populateIcons(
const QModelIndex& idx )
235 QString path = idx.data( Qt::UserRole + 1 ).toString();
238 mImagesListView->setModel( m );
240 connect( mImagesListView->selectionModel(), SIGNAL( currentChanged(
const QModelIndex&,
const QModelIndex& ) ),
241 this, SLOT( svgSelectionChanged(
const QModelIndex& ) ) );
244 void QgsSvgSelectorWidget::on_mFilePushButton_clicked()
247 QString openDir = settings.value(
"/UI/lastSVGMarkerDir",
"." ).toString();
249 QString lineEditText = mFileLineEdit->text();
250 if ( !lineEditText.isEmpty() )
252 QFileInfo openDirFileInfo( lineEditText );
253 openDir = openDirFileInfo.path();
256 QString file = QFileDialog::getOpenFileName( 0,
257 tr(
"Select SVG file" ),
259 tr(
"SVG files" ) +
" (*.svg *.SVG)" );
266 QFileInfo fi( file );
267 if ( !fi.exists() || !fi.isReadable() )
269 updateCurrentSvgPath( QString(
"" ) );
270 updateLineEditFeedback(
false );
273 settings.setValue(
"/UI/lastSVGMarkerDir", fi.absolutePath() );
274 mFileLineEdit->setText( file );
275 updateCurrentSvgPath( file );
278 void QgsSvgSelectorWidget::updateLineEditFeedback(
bool ok, QString tip )
281 mFileLineEdit->setStyleSheet( QString( !ok ?
"QLineEdit{ color: rgb(225, 0, 0); }" :
"" ) );
282 mFileLineEdit->setToolTip( !ok ?
tr(
"File not found" ) : tip );
285 void QgsSvgSelectorWidget::on_mFileLineEdit_textChanged(
const QString& text )
288 bool validSVG = !resolvedPath.isNull();
290 updateLineEditFeedback( validSVG, resolvedPath );
291 updateCurrentSvgPath( validSVG ? resolvedPath : QString(
"" ) );
297 mGroupsTreeView->setModel( g );
299 int rows = g->rowCount( g->indexFromItem( g->invisibleRootItem() ) );
300 for (
int i = 0; i < rows; i++ )
302 mGroupsTreeView->setExpanded( g->indexFromItem( g->item( i ) ),
true );
307 mImagesListView->setModel( m );
313 QDialogButtonBox::StandardButtons buttons,
314 Qt::Orientation orientation )
315 : QDialog( parent, fl )
318 Q_UNUSED( orientation );
321 mButtonBox =
new QDialogButtonBox( buttons, orientation,
this );
322 connect(
mButtonBox, SIGNAL( accepted() ),
this, SLOT( accept() ) );
323 connect(
mButtonBox, SIGNAL( rejected() ),
this, SLOT( reject() ) );
325 setMinimumSize( 480, 320 );
336 restoreGeometry( settings.value(
"/Windows/SvgSelectorDialog/geometry" ).toByteArray() );
342 settings.setValue(
"/Windows/SvgSelectorDialog/geometry", saveGeometry() );
QgsSvgSelectorWidget * mSvgSelector
static const QString pkgDataPath()
Returns the common root path of all application data directories.
static const QStringList svgPaths()
Returns the pathes to svg directories.
QgsSvgSelectorGroupsModel(QObject *parent)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
bool readBoolEntry(const QString &scope, const QString &key, bool def=false, bool *ok=0) const
static const QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
QgsSvgSelectorDialog(QWidget *parent=0, Qt::WindowFlags fl=QgisGui::ModalDialogFlags, QDialogButtonBox::StandardButtons buttons=QDialogButtonBox::Close|QDialogButtonBox::Ok, Qt::Orientation orientation=Qt::Horizontal)
void containsParams(const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasOutlineParam, QColor &defaultOutlineColor, bool &hasOutlineWidthParam, double &defaultOutlineWidth) const
Tests if an svg file contains parameters for fill, outline color, outline width.
static QgsSvgCache * instance()
int rowCount(const QModelIndex &parent=QModelIndex()) const override
const QImage & svgAsImage(const QString &file, double size, const QColor &fill, const QColor &outline, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, bool &fitsInCache)
Get SVG as QImage.
static QString symbolPathToName(QString path)
Get symbols's name from its path.
QDialogButtonBox * mButtonBox
static QStringList listSvgFiles()
Return a list of all available svg files.
static QString symbolNameToPath(QString name)
Get symbol's path from its name.
static QStringList listSvgFilesAt(QString directory)
Return a list of svg files at the specified directory.
QgsSvgSelectorListModel(QObject *parent)
static QgsProject * instance()
access to canonical QgsProject instance