18 #include "ui_qgsstylev2exportimportdialogbase.h" 26 #include <QInputDialog> 27 #include <QCloseEvent> 28 #include <QFileDialog> 29 #include <QMessageBox> 30 #include <QPushButton> 31 #include <QStandardItemModel> 44 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
48 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
53 listItems->setModel( model );
60 mProgressDlg =
nullptr;
61 mGroupSelectionDlg =
nullptr;
66 if ( mDialogMode ==
Import )
70 importTypeCombo->addItem(
tr(
"file specified below" ),
QVariant(
"file" ) );
72 importTypeCombo->addItem(
tr(
"URL specified below" ),
QVariant(
"url" ) );
76 groupCombo->addItem(
"imported",
QVariant(
"new" ) );
77 Q_FOREACH (
const QString& gName, groups )
79 groupCombo->addItem( gName );
82 btnBrowse->setText(
"Browse" );
83 connect( btnBrowse, SIGNAL( clicked() ),
this, SLOT(
browse() ) );
85 label->setText(
tr(
"Select symbols to import" ) );
86 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Import" ) );
92 btnBrowse->setHidden(
true );
93 fromLabel->setHidden(
true );
94 importTypeCombo->setHidden(
true );
95 locationLabel->setHidden(
true );
96 locationLineEdit->setHidden(
true );
99 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
101 groupLabel->setHidden(
true );
102 groupCombo->setHidden(
true );
104 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Export" ) );
105 if ( !populateStyles( mQgisStyle ) )
114 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
119 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
120 if ( selection.isEmpty() )
123 tr(
"You should select at least one symbol/color ramp." ) );
127 if ( mDialogMode ==
Export )
130 tr(
"XML files (*.xml *.XML)" ) );
137 if ( !fileName.
endsWith(
".xml", Qt::CaseInsensitive ) )
142 mFileName = fileName;
144 moveStyles( &selection, mQgisStyle, mTempStyle );
145 if ( !mTempStyle->
exportXML( mFileName ) )
148 tr(
"Error when saving selected symbols to file:\n%1" )
155 moveStyles( &selection, mTempStyle, mQgisStyle );
170 if ( mDialogMode ==
Import )
176 tr(
"An error occurred during import:\n%1" ).arg( style->
errorString() ) );
188 for (
int i = 0; i < styleNames.
count(); ++i )
190 name = styleNames[i];
202 for (
int i = 0; i < styleNames.
count(); ++i )
204 name = styleNames[i];
216 void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection,
QgsStyleV2* src,
QgsStyleV2* dst )
222 bool isSymbol =
true;
224 bool overwrite =
true;
228 if ( mDialogMode ==
Import )
239 groupid = dst->
groupId( name );
247 for (
int i = 0; i < selection->size(); ++i )
249 index = selection->at( i );
251 symbol = src->
symbol( symbolName );
263 tr(
"Symbol with name '%1' already exists.\nOverwrite?" )
265 QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
268 case QMessageBox::Cancel:
270 case QMessageBox::No:
272 case QMessageBox::Yes:
274 if ( mDialogMode ==
Import )
277 case QMessageBox::YesToAll:
281 case QMessageBox::NoToAll:
291 if ( mDialogMode ==
Import )
301 if ( mDialogMode ==
Import )
310 tr(
"Color ramp with name '%1' already exists.\nOverwrite?" )
312 QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
315 case QMessageBox::Cancel:
317 case QMessageBox::No:
319 case QMessageBox::Yes:
321 if ( mDialogMode ==
Import )
324 case QMessageBox::YesToAll:
328 case QMessageBox::NoToAll:
338 if ( mDialogMode ==
Import )
348 if ( mDialogMode ==
Import )
359 delete mGroupSelectionDlg;
364 listItems->selectAll();
369 listItems->clearSelection();
374 Q_FOREACH (
const QString &symbolName, symbolNames )
376 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0, 0 ), Qt::DisplayRole, symbolName , 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
379 listItems->selectionModel()->select( index, QItemSelectionModel::Select );
386 Q_FOREACH (
const QString &symbolName, symbolNames )
388 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0, 0 ), Qt::DisplayRole, symbolName , 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
392 listItems->selectionModel()->select( deselection, QItemSelectionModel::Deselect );
432 if ( ! mGroupSelectionDlg )
438 connect( mGroupSelectionDlg, SIGNAL( allSelected() ),
this, SLOT(
selectAll() ) );
443 mGroupSelectionDlg->
show();
444 mGroupSelectionDlg->
raise();
450 QString type = importTypeCombo->itemData( index ).toString();
452 locationLineEdit->setText(
"" );
454 if ( type ==
"file" )
456 locationLineEdit->setEnabled(
true );
457 btnBrowse->setText(
"Browse" );
459 else if ( type ==
"official" )
461 btnBrowse->setText(
"Fetch Symbols" );
462 locationLineEdit->setEnabled(
false );
466 btnBrowse->setText(
"Fetch Symbols" );
467 locationLineEdit->setEnabled(
true );
473 QString type = importTypeCombo->itemData( importTypeCombo->currentIndex() ).toString();
475 if ( type ==
"file" )
478 tr(
"XML files (*.xml *XML)" ) );
485 groupCombo->setItemText( 0, groupName );
486 locationLineEdit->setText( mFileName );
487 populateStyles( mTempStyle );
489 else if ( type ==
"official" )
496 downloadStyleXML(
QUrl( locationLineEdit->text() ) );
500 void QgsStyleV2ExportImportDialog::downloadStyleXML(
const QUrl& url )
506 if ( mTempFile->
open() )
513 mProgressDlg =
nullptr;
520 connect( mProgressDlg, SIGNAL( canceled() ),
this, SLOT( downloadCanceled() ) );
531 connect( mNetReply, SIGNAL(
finished() ),
this, SLOT( httpFinished() ) );
532 connect( mNetReply, SIGNAL( readyRead() ),
this, SLOT( fileReadyRead() ) );
533 connect( mNetReply, SIGNAL( downloadProgress( qint64, qint64 ) ),
this, SLOT( updateProgress( qint64, qint64 ) ) );
537 void QgsStyleV2ExportImportDialog::httpFinished()
539 if ( mNetReply->
error() )
543 mProgressDlg->
hide();
545 tr(
"Download failed: %1." ).arg( mNetReply->
errorString() ) );
552 populateStyles( mTempStyle );
556 void QgsStyleV2ExportImportDialog::fileReadyRead()
561 void QgsStyleV2ExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
564 mProgressDlg->
setValue( bytesRead );
567 void QgsStyleV2ExportImportDialog::downloadCanceled()
576 Q_UNUSED( selected );
577 Q_UNUSED( deselected );
578 bool nothingSelected = listItems->selectionModel()->selectedIndexes().empty();
579 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( nothingSelected );
bool saveColorRamp(const QString &name, QgsVectorColorRampV2 *ramp, int groupid, const QStringList &tags)
add the colorramp to the DB
QgsVectorColorRampV2 * colorRamp(const QString &name)
return a NEW copy of color ramp
void setIcon(const QIcon &icon)
void selectByGroup()
selectByGroup open select by group dialog
void deselectSymbols(const QStringList &symbolNames)
deselectSymbols deselect symbols by name
static QIcon colorRampPreviewIcon(QgsVectorColorRampV2 *ramp, QSize size)
bool importXML(const QString &filename)
Imports the symbols and colorramps into the default style database from the given XML file...
void setMaximum(int maximum)
bool addSymbol(const QString &name, QgsSymbolV2 *symbol, bool update=false)
add symbol to style. takes symbol's ownership
QString errorString() const
int groupId(const QString &group)
return the DB id for the given group name
void setLabelText(const QString &text)
QStringList symbolsOfGroup(StyleEntity type, int groupid)
returns the symbolnames of a given groupid
void deselectGroup(const QString &groupName)
Deselect the symbols belonging to the given group.
bool contains(const QString &str, Qt::CaseSensitivity cs) const
void selectSymbols(const QStringList &symbolNames)
selectSymbols select symbols by name
bool exportXML(const QString &filename)
Exports the style as a XML file.
void selectAll()
selectAll selects all symbols
bool addColorRamp(const QString &name, QgsVectorColorRampV2 *colorRamp, bool update=false)
add color ramp to style. takes ramp's ownership
QString & remove(int position, int n)
void clearSelection()
clearSelection deselects all symbols
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QStringList colorRampNames()
return a list of names of color ramps
QString tr(const char *sourceText, const char *disambiguation, int n)
void finished(int result)
const char * name() const
void setValue(int progress)
QgsStyleV2ExportImportDialog(QgsStyleV2 *style, QWidget *parent=nullptr, Mode mode=Export)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
int count(const T &value) const
bool saveSymbol(const QString &name, QgsSymbolV2 *symbol, int groupid, const QStringList &tags)
add the symbol to the DB with the tags
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void setAutoClose(bool close)
QStringList symbolsOfSmartgroup(StyleEntity type, int id)
returns the symbols for the smartgroup
void deselectSmartgroup(const QString &groupName)
deselectSmartgroup deselects all symbols from a smart group
virtual QVariant data(const QModelIndex &index, int role) const=0
void selectSmartgroup(const QString &groupName)
selectSmartgroup selects all symbols from a smart group
QStringList symbolNames()
return a list of names of symbols
void importTypeChanged(int)
QgsSymbolV2 * symbol(const QString &name)
return a NEW copy of symbol
~QgsStyleV2ExportImportDialog()
void clear()
remove all contents of the style
const QAbstractItemModel * model() const
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
qint64 write(const char *data, qint64 maxSize)
NetworkError error() const
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
QNetworkReply * get(const QNetworkRequest &request)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
int smartgroupId(const QString &smartgroup)
return the DB id for the given smartgroup name
void postEvent(QObject *receiver, QEvent *event)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void selectGroup(const QString &groupName)
Select the symbols belonging to the given group.
Abstract base class for color ramps.
QString errorString()
return last error from load/save operation
void appendRow(const QList< QStandardItem * > &items)
QStringList groupNames()
return the all the groups in the style
int addGroup(const QString &groupName, int parent=0)
adds a new group and returns the group's id