Skip to content

Commit 24a39fd

Browse files
author
Arunmozhi
committed
support for online import of style XML files added
1 parent adddc02 commit 24a39fd

6 files changed

+219
-35
lines changed

src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp

+149-8
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
#include <QPushButton>
2828
#include <QStandardItemModel>
2929

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

5150
mTempStyle = new QgsStyleV2();
51+
// TODO validate
52+
mFileName = "";
53+
mProgressDlg = NULL;
54+
mTempFile = NULL;
55+
mNetManager = new QNetworkAccessManager( this );
56+
mNetReply = NULL;
5257

5358
if ( mDialogMode == Import )
5459
{
60+
// populate the import types
61+
importTypeCombo->addItem( "file specified below", QVariant( "file" ) );
62+
importTypeCombo->addItem( "official QGIS repo online", QVariant( "official" ) );
63+
importTypeCombo->addItem( "URL specified below", QVariant( "url" ) );
64+
connect( importTypeCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( importTypeChanged( int ) ) );
65+
66+
btnBrowse->setText( "Browse" );
67+
connect( btnBrowse, SIGNAL( clicked() ), this, SLOT( browse() ) );
68+
5569
label->setText( tr( "Select symbols to import" ) );
5670
buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
57-
if ( !populateStyles( mTempStyle ) )
58-
{
59-
QApplication::postEvent( this, new QCloseEvent() );
60-
}
71+
6172
}
6273
else
6374
{
75+
// hide import specific controls when exporting
76+
btnBrowse->setHidden( true );
77+
fromLabel->setHidden( true );
78+
importTypeCombo->setHidden( true );
79+
locationLabel->setHidden( true );
80+
locationLineEdit->setHidden( true );
81+
6482
buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
6583
if ( !populateStyles( mQgisStyle ) )
6684
{
@@ -112,8 +130,6 @@ void QgsStyleV2ExportImportDialog::doExportImport()
112130
else // import
113131
{
114132
moveStyles( &selection, mTempStyle, mQgisStyle );
115-
// TODO save in the move function itself using saveSymbol and saveColorRamp
116-
mQgisStyle->save();
117133

118134
// clear model
119135
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
@@ -305,3 +321,128 @@ void QgsStyleV2ExportImportDialog::clearSelection()
305321
{
306322
listItems->clearSelection();
307323
}
324+
325+
void QgsStyleV2ExportImportDialog::importTypeChanged( int index )
326+
{
327+
QString type = importTypeCombo->itemData( index ).toString();
328+
329+
locationLineEdit->setText( "" );
330+
331+
if ( type == "file" )
332+
{
333+
locationLineEdit->setEnabled( true );
334+
btnBrowse->setText( "Browse" );
335+
}
336+
else if ( type == "official" )
337+
{
338+
btnBrowse->setText( "Fetch Symbols" );
339+
locationLineEdit->setEnabled( false );
340+
}
341+
else
342+
{
343+
btnBrowse->setText( "Fetch Symbols" );
344+
locationLineEdit->setEnabled( true );
345+
}
346+
}
347+
348+
void QgsStyleV2ExportImportDialog::browse()
349+
{
350+
QString type = importTypeCombo->itemData( importTypeCombo->currentIndex() ).toString();
351+
352+
if ( type == "file" )
353+
{
354+
mFileName = QFileDialog::getOpenFileName( this, tr( "Load styles" ), ".",
355+
tr( "XML files (*.xml *XML)" ) );
356+
if ( mFileName.isEmpty() )
357+
{
358+
return;
359+
}
360+
locationLineEdit->setText( mFileName );
361+
}
362+
else if ( type == "official" )
363+
{
364+
// TODO set URL
365+
downloadStyleXML( QUrl( "http://...." ) );
366+
}
367+
else
368+
{
369+
downloadStyleXML( QUrl( locationLineEdit->text() ) );
370+
}
371+
populateStyles( mTempStyle );
372+
}
373+
374+
void QgsStyleV2ExportImportDialog::downloadStyleXML( QUrl url )
375+
{
376+
// TODO Try to move this code to some core Network interface,
377+
// HTTP downloading is a generic functionality that might be used elsewhere
378+
379+
mTempFile = new QTemporaryFile();
380+
if ( mTempFile->open() )
381+
{
382+
mFileName = mTempFile->fileName();
383+
384+
if ( mProgressDlg )
385+
{
386+
QProgressDialog *dummy = mProgressDlg;
387+
mProgressDlg = NULL;
388+
delete dummy;
389+
}
390+
mProgressDlg = new QProgressDialog();
391+
mProgressDlg->setLabelText( tr( "Downloading style ... " ) );
392+
mProgressDlg->setAutoClose( true );
393+
394+
connect( mProgressDlg, SIGNAL( canceled() ), this, SLOT( downloadCanceled() ) );
395+
396+
// open the network connection and connect the respective slots
397+
if ( mNetReply )
398+
{
399+
QNetworkReply *dummyReply = mNetReply;
400+
mNetReply = NULL;
401+
delete dummyReply;
402+
}
403+
mNetReply = mNetManager->get( QNetworkRequest( url ) );
404+
405+
connect( mNetReply, SIGNAL( finished() ), this, SLOT( httpFinished() ) );
406+
connect( mNetReply, SIGNAL( readyRead() ), this, SLOT( fileReadyRead() ) );
407+
connect( mNetReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( updateProgress( qint64, qint64 ) ) );
408+
}
409+
}
410+
411+
void QgsStyleV2ExportImportDialog::httpFinished()
412+
{
413+
if ( mNetReply->error() )
414+
{
415+
mTempFile->remove();
416+
mFileName = "";
417+
mProgressDlg->hide();
418+
QMessageBox::information( this, tr( "HTTP Error!" ),
419+
tr( "Download failed: %1." ).arg( mNetReply->errorString() ) );
420+
return;
421+
}
422+
else
423+
{
424+
mTempFile->flush();
425+
mTempFile->close();
426+
populateStyles( mTempStyle );
427+
delete mTempFile;
428+
mTempFile = NULL;
429+
}
430+
}
431+
432+
void QgsStyleV2ExportImportDialog::fileReadyRead()
433+
{
434+
mTempFile->write( mNetReply->readAll() );
435+
}
436+
437+
void QgsStyleV2ExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
438+
{
439+
mProgressDlg->setMaximum( bytesTotal );
440+
mProgressDlg->setValue( bytesRead );
441+
}
442+
443+
void QgsStyleV2ExportImportDialog::downloadCanceled()
444+
{
445+
mNetReply->abort();
446+
mTempFile->remove();
447+
mFileName = "";
448+
}

src/gui/symbology-ng/qgsstylev2exportimportdialog.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#define QGSSTYLEV2EXPORTIMPORTDIALOG_H
2020

2121
#include <QDialog>
22+
#include <QUrl>
23+
#include <QProgressDialog>
24+
#include <QTemporaryFile>
25+
#include <QNetworkAccessManager>
26+
#include <QNetworkReply>
2227

2328
#include "ui_qgsstylev2exportimportdialogbase.h"
2429

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

3843
// constructor
3944
// mode argument must be 0 for saving and 1 for loading
40-
QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent = NULL, Mode mode = Export, QString fileName = "" );
45+
QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent = NULL, Mode mode = Export );
4146
~QgsStyleV2ExportImportDialog();
4247

4348
public slots:
4449
void doExportImport();
4550
void selectAll();
4651
void clearSelection();
4752

53+
void importTypeChanged( int );
54+
void browse();
55+
56+
private slots:
57+
void httpFinished();
58+
void fileReadyRead();
59+
void updateProgress( qint64, qint64 );
60+
void downloadCanceled();
61+
4862
private:
63+
void downloadStyleXML( QUrl url );
4964
bool populateStyles( QgsStyleV2* style );
5065
void moveStyles( QModelIndexList* selection, QgsStyleV2* src, QgsStyleV2* dst );
5166

67+
QProgressDialog *mProgressDlg;
68+
QTemporaryFile *mTempFile;
69+
QNetworkAccessManager *mNetManager;
70+
QNetworkReply *mNetReply;
71+
5272
QString mFileName;
5373
Mode mDialogMode;
5474

src/gui/symbology-ng/qgsstylev2managerdialog.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <QAction>
3737
#include <QMenu>
3838

39-
4039
#include "qgsapplication.h"
4140
#include "qgslogger.h"
4241

@@ -66,8 +65,13 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
6665
connect( btnAddItem, SIGNAL( clicked() ), this, SLOT( addItem() ) );
6766
connect( btnEditItem, SIGNAL( clicked() ), this, SLOT( editItem() ) );
6867
connect( btnRemoveItem, SIGNAL( clicked() ), this, SLOT( removeItem() ) );
69-
connect( btnExportItems, SIGNAL( clicked() ), this, SLOT( exportItems() ) );
70-
connect( btnImportItems, SIGNAL( clicked() ), this, SLOT( importItems() ) );
68+
69+
QMenu *shareMenu = new QMenu( "Share Menu", this );
70+
QAction *exportAction = shareMenu->addAction( "Export" );
71+
QAction *importAction = shareMenu->addAction( "Import" );
72+
connect( exportAction, SIGNAL( triggered() ), this, SLOT( exportItems() ) );
73+
connect( importAction, SIGNAL( triggered() ), this, SLOT( importItems() ) );
74+
btnShare->setMenu( shareMenu );
7175

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

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

585589
void QgsStyleV2ManagerDialog::importItems()
586590
{
587-
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load styles" ), ".",
588-
tr( "XML files (*.xml *XML)" ) );
589-
if ( fileName.isEmpty() )
590-
{
591-
return;
592-
}
593-
594-
QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Import, fileName );
591+
QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Import );
595592
dlg.exec();
596593
populateList();
597594
}
@@ -1219,3 +1216,4 @@ bool QgsStyleV2ManagerDialog::eventFilter( QObject *obj, QEvent *event )
12191216
}
12201217
return false;
12211218
}
1219+

src/gui/symbology-ng/qgsstylev2managerdialog.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class GUI_EXPORT QgsStyleV2ManagerDialog : public QDialog, private Ui::QgsStyleV
4242
void removeItem();
4343
void exportItems();
4444
void importItems();
45+
4546
//! adds symbols of some type to list
4647
void populateList();
4748

src/ui/qgsstylev2exportimportdialogbase.ui

+36-5
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,52 @@
77
<x>0</x>
88
<y>0</y>
99
<width>400</width>
10-
<height>300</height>
10+
<height>379</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Styles import/export</string>
1515
</property>
16-
<layout class="QVBoxLayout" name="verticalLayout">
17-
<item>
16+
<layout class="QGridLayout" name="gridLayout_2">
17+
<item row="0" column="0">
18+
<layout class="QGridLayout" name="gridLayout">
19+
<item row="0" column="0">
20+
<widget class="QLabel" name="fromLabel">
21+
<property name="text">
22+
<string>Import from</string>
23+
</property>
24+
</widget>
25+
</item>
26+
<item row="0" column="1" colspan="2">
27+
<widget class="QComboBox" name="importTypeCombo"/>
28+
</item>
29+
<item row="1" column="0">
30+
<widget class="QLabel" name="locationLabel">
31+
<property name="text">
32+
<string>Location</string>
33+
</property>
34+
</widget>
35+
</item>
36+
<item row="1" column="1">
37+
<widget class="QLineEdit" name="locationLineEdit"/>
38+
</item>
39+
<item row="1" column="2">
40+
<widget class="QPushButton" name="btnBrowse">
41+
<property name="text">
42+
<string/>
43+
</property>
44+
</widget>
45+
</item>
46+
</layout>
47+
</item>
48+
<item row="1" column="0">
1849
<widget class="QLabel" name="label">
1950
<property name="text">
2051
<string>Select symbols to export</string>
2152
</property>
2253
</widget>
2354
</item>
24-
<item>
55+
<item row="2" column="0">
2556
<widget class="QListView" name="listItems">
2657
<property name="sizePolicy">
2758
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
@@ -55,7 +86,7 @@
5586
</property>
5687
</widget>
5788
</item>
58-
<item>
89+
<item row="3" column="0">
5990
<widget class="QDialogButtonBox" name="buttonBox">
6091
<property name="orientation">
6192
<enum>Qt::Horizontal</enum>

src/ui/qgsstylev2managerdialogbase.ui

+2-9
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,9 @@
247247
</widget>
248248
</item>
249249
<item>
250-
<widget class="QPushButton" name="btnExportItems">
250+
<widget class="QPushButton" name="btnShare">
251251
<property name="text">
252-
<string>Export</string>
253-
</property>
254-
</widget>
255-
</item>
256-
<item>
257-
<widget class="QPushButton" name="btnImportItems">
258-
<property name="text">
259-
<string>Import</string>
252+
<string>Share</string>
260253
</property>
261254
</widget>
262255
</item>

0 commit comments

Comments
 (0)