Skip to content
Permalink
Browse files

support for online import of style XML files added

  • Loading branch information
tecoholic committed Aug 2, 2012
1 parent adddc02 commit 24a39fdcc6433a018172171788105c1eadb4dfbc
@@ -27,9 +27,8 @@
#include <QPushButton>
#include <QStandardItemModel>

QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent, Mode mode, QString fileName )
QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent, Mode mode )
: QDialog( parent )
, mFileName( fileName )
, mDialogMode( mode )
, mQgisStyle( style )
{
@@ -49,18 +48,37 @@ QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style, Q
listItems->setModel( model );

mTempStyle = new QgsStyleV2();
// TODO validate
mFileName = "";
mProgressDlg = NULL;
mTempFile = NULL;
mNetManager = new QNetworkAccessManager( this );
mNetReply = NULL;

if ( mDialogMode == Import )
{
// populate the import types
importTypeCombo->addItem( "file specified below", QVariant( "file" ) );
importTypeCombo->addItem( "official QGIS repo online", QVariant( "official" ) );
importTypeCombo->addItem( "URL specified below", QVariant( "url" ) );
connect( importTypeCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( importTypeChanged( int ) ) );

btnBrowse->setText( "Browse" );
connect( btnBrowse, SIGNAL( clicked() ), this, SLOT( browse() ) );

label->setText( tr( "Select symbols to import" ) );
buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
if ( !populateStyles( mTempStyle ) )
{
QApplication::postEvent( this, new QCloseEvent() );
}

}
else
{
// hide import specific controls when exporting
btnBrowse->setHidden( true );
fromLabel->setHidden( true );
importTypeCombo->setHidden( true );
locationLabel->setHidden( true );
locationLineEdit->setHidden( true );

buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
if ( !populateStyles( mQgisStyle ) )
{
@@ -112,8 +130,6 @@ void QgsStyleV2ExportImportDialog::doExportImport()
else // import
{
moveStyles( &selection, mTempStyle, mQgisStyle );
// TODO save in the move function itself using saveSymbol and saveColorRamp
mQgisStyle->save();

// clear model
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
@@ -305,3 +321,128 @@ void QgsStyleV2ExportImportDialog::clearSelection()
{
listItems->clearSelection();
}

void QgsStyleV2ExportImportDialog::importTypeChanged( int index )
{
QString type = importTypeCombo->itemData( index ).toString();

locationLineEdit->setText( "" );

if ( type == "file" )
{
locationLineEdit->setEnabled( true );
btnBrowse->setText( "Browse" );
}
else if ( type == "official" )
{
btnBrowse->setText( "Fetch Symbols" );
locationLineEdit->setEnabled( false );
}
else
{
btnBrowse->setText( "Fetch Symbols" );
locationLineEdit->setEnabled( true );
}
}

void QgsStyleV2ExportImportDialog::browse()
{
QString type = importTypeCombo->itemData( importTypeCombo->currentIndex() ).toString();

if ( type == "file" )
{
mFileName = QFileDialog::getOpenFileName( this, tr( "Load styles" ), ".",
tr( "XML files (*.xml *XML)" ) );
if ( mFileName.isEmpty() )
{
return;
}
locationLineEdit->setText( mFileName );
}
else if ( type == "official" )
{
// TODO set URL
downloadStyleXML( QUrl( "http://...." ) );
}
else
{
downloadStyleXML( QUrl( locationLineEdit->text() ) );
}
populateStyles( mTempStyle );
}

void QgsStyleV2ExportImportDialog::downloadStyleXML( QUrl url )
{
// TODO Try to move this code to some core Network interface,
// HTTP downloading is a generic functionality that might be used elsewhere

mTempFile = new QTemporaryFile();
if ( mTempFile->open() )
{
mFileName = mTempFile->fileName();

if ( mProgressDlg )
{
QProgressDialog *dummy = mProgressDlg;
mProgressDlg = NULL;
delete dummy;
}
mProgressDlg = new QProgressDialog();
mProgressDlg->setLabelText( tr( "Downloading style ... " ) );
mProgressDlg->setAutoClose( true );

connect( mProgressDlg, SIGNAL( canceled() ), this, SLOT( downloadCanceled() ) );

// open the network connection and connect the respective slots
if ( mNetReply )
{
QNetworkReply *dummyReply = mNetReply;
mNetReply = NULL;
delete dummyReply;
}
mNetReply = mNetManager->get( QNetworkRequest( url ) );

connect( mNetReply, SIGNAL( finished() ), this, SLOT( httpFinished() ) );
connect( mNetReply, SIGNAL( readyRead() ), this, SLOT( fileReadyRead() ) );
connect( mNetReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( updateProgress( qint64, qint64 ) ) );
}
}

void QgsStyleV2ExportImportDialog::httpFinished()
{
if ( mNetReply->error() )
{
mTempFile->remove();
mFileName = "";
mProgressDlg->hide();
QMessageBox::information( this, tr( "HTTP Error!" ),
tr( "Download failed: %1." ).arg( mNetReply->errorString() ) );
return;
}
else
{
mTempFile->flush();
mTempFile->close();
populateStyles( mTempStyle );
delete mTempFile;
mTempFile = NULL;
}
}

void QgsStyleV2ExportImportDialog::fileReadyRead()
{
mTempFile->write( mNetReply->readAll() );
}

void QgsStyleV2ExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
{
mProgressDlg->setMaximum( bytesTotal );
mProgressDlg->setValue( bytesRead );
}

void QgsStyleV2ExportImportDialog::downloadCanceled()
{
mNetReply->abort();
mTempFile->remove();
mFileName = "";
}
@@ -19,6 +19,11 @@
#define QGSSTYLEV2EXPORTIMPORTDIALOG_H

#include <QDialog>
#include <QUrl>
#include <QProgressDialog>
#include <QTemporaryFile>
#include <QNetworkAccessManager>
#include <QNetworkReply>

#include "ui_qgsstylev2exportimportdialogbase.h"

@@ -37,18 +42,33 @@ class QgsStyleV2ExportImportDialog : public QDialog, private Ui::QgsStyleV2Expor

// constructor
// mode argument must be 0 for saving and 1 for loading
QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent = NULL, Mode mode = Export, QString fileName = "" );
QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent = NULL, Mode mode = Export );
~QgsStyleV2ExportImportDialog();

public slots:
void doExportImport();
void selectAll();
void clearSelection();

void importTypeChanged( int );
void browse();

private slots:
void httpFinished();
void fileReadyRead();
void updateProgress( qint64, qint64 );
void downloadCanceled();

private:
void downloadStyleXML( QUrl url );
bool populateStyles( QgsStyleV2* style );
void moveStyles( QModelIndexList* selection, QgsStyleV2* src, QgsStyleV2* dst );

QProgressDialog *mProgressDlg;
QTemporaryFile *mTempFile;
QNetworkAccessManager *mNetManager;
QNetworkReply *mNetReply;

QString mFileName;
Mode mDialogMode;

@@ -36,7 +36,6 @@
#include <QAction>
#include <QMenu>


#include "qgsapplication.h"
#include "qgslogger.h"

@@ -66,8 +65,13 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
connect( btnAddItem, SIGNAL( clicked() ), this, SLOT( addItem() ) );
connect( btnEditItem, SIGNAL( clicked() ), this, SLOT( editItem() ) );
connect( btnRemoveItem, SIGNAL( clicked() ), this, SLOT( removeItem() ) );
connect( btnExportItems, SIGNAL( clicked() ), this, SLOT( exportItems() ) );
connect( btnImportItems, SIGNAL( clicked() ), this, SLOT( importItems() ) );

QMenu *shareMenu = new QMenu( "Share Menu", this );
QAction *exportAction = shareMenu->addAction( "Export" );
QAction *importAction = shareMenu->addAction( "Import" );
connect( exportAction, SIGNAL( triggered() ), this, SLOT( exportItems() ) );
connect( importAction, SIGNAL( triggered() ), this, SLOT( importItems() ) );
btnShare->setMenu( shareMenu );

// Set editing mode off by default
mGrouppingMode = false;
@@ -96,7 +100,7 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
connect( groupModel, SIGNAL( itemChanged( QStandardItem* ) ),
this, SLOT( groupRenamed( QStandardItem* ) ) );

QMenu *groupMenu = new QMenu( "Group Actions" );
QMenu *groupMenu = new QMenu( "Group Actions", this );
QAction *groupSymbols = groupMenu->addAction( "Group Symbols" );
QAction *editSmartgroup = groupMenu->addAction( "Edit Smart Group" );
btnManageGroups->setMenu( groupMenu );
@@ -584,14 +588,7 @@ void QgsStyleV2ManagerDialog::exportItems()

void QgsStyleV2ManagerDialog::importItems()
{
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load styles" ), ".",
tr( "XML files (*.xml *XML)" ) );
if ( fileName.isEmpty() )
{
return;
}

QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Import, fileName );
QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Import );
dlg.exec();
populateList();
}
@@ -1219,3 +1216,4 @@ bool QgsStyleV2ManagerDialog::eventFilter( QObject *obj, QEvent *event )
}
return false;
}

@@ -42,6 +42,7 @@ class GUI_EXPORT QgsStyleV2ManagerDialog : public QDialog, private Ui::QgsStyleV
void removeItem();
void exportItems();
void importItems();

//! adds symbols of some type to list
void populateList();

@@ -7,21 +7,52 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<height>379</height>
</rect>
</property>
<property name="windowTitle">
<string>Styles import/export</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="fromLabel">
<property name="text">
<string>Import from</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="importTypeCombo"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="locationLabel">
<property name="text">
<string>Location</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="locationLineEdit"/>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="btnBrowse">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Select symbols to export</string>
</property>
</widget>
</item>
<item>
<item row="2" column="0">
<widget class="QListView" name="listItems">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
@@ -55,7 +86,7 @@
</property>
</widget>
</item>
<item>
<item row="3" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -247,16 +247,9 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="btnExportItems">
<widget class="QPushButton" name="btnShare">
<property name="text">
<string>Export</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnImportItems">
<property name="text">
<string>Import</string>
<string>Share</string>
</property>
</widget>
</item>

0 comments on commit 24a39fd

Please sign in to comment.
You can’t perform that action at this time.