24 #include <QInputDialog>
25 #include <QCloseEvent>
26 #include <QFileDialog>
27 #include <QMessageBox>
28 #include <QPushButton>
29 #include <QStandardItemModel>
41 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
45 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
49 listItems->setModel( model );
59 if ( mDialogMode ==
Import )
63 importTypeCombo->addItem(
tr(
"file specified below" ),
QVariant(
"file" ) );
65 importTypeCombo->addItem(
tr(
"URL specified below" ),
QVariant(
"url" ) );
69 groupCombo->addItem(
"imported",
QVariant(
"new" ) );
70 Q_FOREACH (
const QString& gName, groups )
72 groupCombo->addItem( gName );
75 btnBrowse->setText(
"Browse" );
76 connect( btnBrowse, SIGNAL( clicked() ),
this, SLOT(
browse() ) );
78 label->setText(
tr(
"Select symbols to import" ) );
79 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Import" ) );
85 btnBrowse->setHidden(
true );
86 fromLabel->setHidden(
true );
87 importTypeCombo->setHidden(
true );
88 locationLabel->setHidden(
true );
89 locationLineEdit->setHidden(
true );
90 groupLabel->setHidden(
true );
91 groupCombo->setHidden(
true );
93 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Export" ) );
94 if ( !populateStyles( mQgisStyle ) )
107 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
108 if ( selection.isEmpty() )
111 tr(
"You should select at least one symbol/color ramp." ) );
115 if ( mDialogMode ==
Export )
118 tr(
"XML files (*.xml *.XML)" ) );
130 mFileName = fileName;
132 moveStyles( &selection, mQgisStyle, mTempStyle );
133 if ( !mTempStyle->
exportXML( mFileName ) )
136 tr(
"Error when saving selected symbols to file:\n%1" )
143 moveStyles( &selection, mTempStyle, mQgisStyle );
155 bool QgsStyleV2ExportImportDialog::populateStyles(
QgsStyleV2* style )
158 if ( mDialogMode ==
Import )
164 tr(
"An error occured during import:\n%1" ).arg( style->
errorString() ) );
176 for (
int i = 0; i < styleNames.
count(); ++i )
178 name = styleNames[i];
190 for (
int i = 0; i < styleNames.
count(); ++i )
192 name = styleNames[i];
204 void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection,
QgsStyleV2* src,
QgsStyleV2* dst )
210 bool isSymbol =
true;
212 bool overwrite =
true;
216 if ( mDialogMode ==
Import )
218 int index = groupCombo->currentIndex();
219 QString name = groupCombo->itemText( index );
223 bool nameInvalid =
true;
224 while ( nameInvalid )
228 tr(
"Please enter a name for new group:" ),
235 tr(
"New group cannot be created without a name. Kindly enter a name." ) );
242 tr(
"Cannot create a group without name. Enter a name." ) );
254 groupid = dst->
groupId( name );
262 for (
int i = 0; i < selection->size(); ++i )
264 index = selection->at( i );
265 symbolName = index.model()->
data( index, 0 ).toString();
266 symbol = src->
symbol( symbolName );
267 if ( symbol == NULL )
278 tr(
"Symbol with name '%1' already exists.\nOverwrite?" )
280 QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
283 case QMessageBox::Cancel:
285 case QMessageBox::No:
287 case QMessageBox::Yes:
289 if ( mDialogMode ==
Import )
292 case QMessageBox::YesToAll:
296 case QMessageBox::NoToAll:
306 if ( mDialogMode ==
Import )
316 if ( mDialogMode ==
Import )
325 tr(
"Color ramp with name '%1' already exists.\nOverwrite?" )
327 QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
330 case QMessageBox::Cancel:
332 case QMessageBox::No:
334 case QMessageBox::Yes:
336 if ( mDialogMode ==
Import )
339 case QMessageBox::YesToAll:
343 case QMessageBox::NoToAll: prompt =
false;
352 if ( mDialogMode ==
Import )
362 if ( mDialogMode ==
Import )
377 listItems->selectAll();
382 listItems->clearSelection();
387 QString type = importTypeCombo->itemData( index ).toString();
389 locationLineEdit->setText(
"" );
391 if ( type ==
"file" )
393 locationLineEdit->setEnabled(
true );
394 btnBrowse->setText(
"Browse" );
396 else if ( type ==
"official" )
398 btnBrowse->setText(
"Fetch Symbols" );
399 locationLineEdit->setEnabled(
false );
403 btnBrowse->setText(
"Fetch Symbols" );
404 locationLineEdit->setEnabled(
true );
410 QString type = importTypeCombo->itemData( importTypeCombo->currentIndex() ).toString();
412 if ( type ==
"file" )
415 tr(
"XML files (*.xml *XML)" ) );
422 groupCombo->setItemText( 0, groupName );
423 locationLineEdit->setText( mFileName );
424 populateStyles( mTempStyle );
426 else if ( type ==
"official" )
433 downloadStyleXML(
QUrl( locationLineEdit->text() ) );
437 void QgsStyleV2ExportImportDialog::downloadStyleXML(
const QUrl& url )
443 if ( mTempFile->
open() )
457 connect( mProgressDlg, SIGNAL( canceled() ),
this, SLOT( downloadCanceled() ) );
468 connect( mNetReply, SIGNAL(
finished() ),
this, SLOT( httpFinished() ) );
469 connect( mNetReply, SIGNAL( readyRead() ),
this, SLOT( fileReadyRead() ) );
470 connect( mNetReply, SIGNAL( downloadProgress( qint64, qint64 ) ),
this, SLOT( updateProgress( qint64, qint64 ) ) );
474 void QgsStyleV2ExportImportDialog::httpFinished()
476 if ( mNetReply->
error() )
480 mProgressDlg->
hide();
482 tr(
"Download failed: %1." ).arg( mNetReply->
errorString() ) );
489 populateStyles( mTempStyle );
493 void QgsStyleV2ExportImportDialog::fileReadyRead()
498 void QgsStyleV2ExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
501 mProgressDlg->
setValue( bytesRead );
504 void QgsStyleV2ExportImportDialog::downloadCanceled()
QgsStyleV2ExportImportDialog(QgsStyleV2 *style, QWidget *parent=NULL, Mode mode=Export)
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)
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)
bool contains(const QString &str, Qt::CaseSensitivity cs) const
bool exportXML(const QString &filename)
Exports the style as a XML file.
bool addColorRamp(const QString &name, QgsVectorColorRampV2 *colorRamp, bool update=false)
add color ramp to style. takes ramp's ownership
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)
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 symbolNames()
return a list of names of symbols
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, QFlags< Qt::WindowType > flags, QFlags< Qt::InputMethodHint > inputMethodHints)
void importTypeChanged(int)
QgsSymbolV2 * symbol(const QString &name)
return a NEW copy of symbol
~QgsStyleV2ExportImportDialog()
QString & replace(int position, int n, QChar after)
void clear()
remove all contents of the style
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)
void postEvent(QObject *receiver, QEvent *event)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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