symbology-ng_export_import.diff

Proposed patch - Alexander Bruy, 2011-02-21 12:51 PM

Download (18.4 KB)

View differences:

src/gui/symbology-ng/qgsstylev2managerdialog.cpp (working copy)
10 10
#include "qgsvectorgradientcolorrampv2dialog.h"
11 11
#include "qgsvectorrandomcolorrampv2dialog.h"
12 12
#include "qgsvectorcolorbrewercolorrampv2dialog.h"
13
#include "qgsstylev2exportimportdialog.h"
13 14

  
14 15
#include <QFile>
16
#include <QFileDialog>
15 17
#include <QInputDialog>
16 18
#include <QStandardItemModel>
17 19

  
......
42 44
  connect( btnAddItem, SIGNAL( clicked() ), this, SLOT( addItem() ) );
43 45
  connect( btnEditItem, SIGNAL( clicked() ), this, SLOT( editItem() ) );
44 46
  connect( btnRemoveItem, SIGNAL( clicked() ), this, SLOT( removeItem() ) );
47
  connect( btnExportItems, SIGNAL( clicked() ), this, SLOT( exportItems() ) );
48
  connect( btnImportItems, SIGNAL( clicked() ), this, SLOT( importItems() ) );
45 49

  
46 50
  QStandardItemModel* model = new QStandardItemModel( listItems );
47 51
  listItems->setModel( model );
......
479 483
    mModified = true;
480 484
  }
481 485
}
486

  
487
void QgsStyleV2ManagerDialog::exportItems()
488
{
489
  QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Export);
490
  dlg.exec();
491
}
492

  
493
void QgsStyleV2ManagerDialog::importItems()
494
{
495
  QString fileName = QFileDialog::getOpenFileName( this, tr( "Load styles" ), ".",
496
                                                   tr( "XML files (*.xml *XML)" ) );
497
  if ( fileName.isEmpty() )
498
  {
499
    return;
500
  }
501

  
502
  QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Import, fileName );
503
  dlg.exec();
504
  populateList();
505
}
src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp (revision 0)
1
/***************************************************************************
2
    qgsstylev2exportimportdialog.cpp
3
    ---------------------
4
    begin                : Dec 2011
5
    copyright            : (C) 2011 by Alexander Bruy
6
    email                : alexander dot bruy at gmail dot com
7

  
8
 ***************************************************************************
9
 *                                                                         *
10
 *   This program is free software; you can redistribute it and/or modify  *
11
 *   it under the terms of the GNU General Public License as published by  *
12
 *   the Free Software Foundation; either version 2 of the License, or     *
13
 *   (at your option) any later version.                                   *
14
 *                                                                         *
15
 ***************************************************************************/
16

  
17
/* $Id: qgsstylev2exportimportdialog.cpp 13187 2010-03-28 22:14:44Z jef $ */
18

  
19
#include <QCloseEvent>
20
#include <QFileDialog>
21
#include <QMessageBox>
22
#include <QPushButton>
23
#include <QStandardItemModel>
24

  
25
#include "qgsstylev2exportimportdialog.h"
26

  
27
#include "qgsstylev2.h"
28
#include "qgssymbolv2.h"
29
#include "qgssymbollayerv2utils.h"
30
#include "qgsvectorcolorrampv2.h"
31

  
32
QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style,
33
    QWidget *parent, Mode mode, QString fileName )
34
    : QDialog( parent ), mDialogMode( mode ), mQgisStyle( style ), mFileName( fileName )
35
{
36
  setupUi( this );
37

  
38
  // additional buttons
39
  QPushButton *pb;
40
  pb = new QPushButton( tr( "Select all" ) );
41
  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
42
  connect( pb, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
43

  
44
  pb = new QPushButton( tr( "Clear selection" ) );
45
  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
46
  connect( pb, SIGNAL( clicked() ), this, SLOT( clearSelection() ) );
47

  
48
  QStandardItemModel* model = new QStandardItemModel( listItems );
49
  listItems->setModel( model );
50

  
51
  mTempStyle = new QgsStyleV2();
52

  
53
  if ( mDialogMode == Import )
54
  {
55
    label->setText( tr( "Select symbols to import" ) );
56
    buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
57
    if ( !populateStyles( mTempStyle ) )
58
    {
59
      QApplication::postEvent( this, new QCloseEvent() );
60
    }
61
  }
62
  else
63
  {
64
    buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
65
    if ( !populateStyles( mQgisStyle ) )
66
    {
67
      QApplication::postEvent( this, new QCloseEvent() );
68
    }
69
  }
70

  
71
  // use Ok button for starting import and export operations
72
  disconnect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
73
  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( doExportImport() ) );
74
}
75

  
76
void QgsStyleV2ExportImportDialog::doExportImport()
77
{
78
  QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
79
  if ( selection.isEmpty() )
80
  {
81
    QMessageBox::warning( this, tr( "Export/import error" ),
82
                          tr( "You should select at least one symbol/color ramp." ) );
83
    return;
84
  }
85

  
86
  if ( mDialogMode == Export )
87
  {
88
    QString fileName = QFileDialog::getSaveFileName( this, tr( "Save styles" ), ".",
89
                                                     tr( "XML files (*.xml *.XML)" ) );
90
    if ( fileName.isEmpty() )
91
    {
92
      return;
93
    }
94

  
95
    // ensure the user never ommited the extension from the file name
96
    if ( !fileName.toLower().endsWith( ".xml" ) )
97
    {
98
      fileName += ".xml";
99
    }
100

  
101
    mFileName = fileName;
102

  
103
    moveStyles( &selection, mQgisStyle, mTempStyle );
104
    if ( !mTempStyle->save( mFileName ) )
105
    {
106
      QMessageBox::warning( this, tr( "Export/import error" ),
107
                            tr( "Error when saving selected symbols to file:\n%1" )
108
                            .arg( mTempStyle->errorString() ) );
109
      return;
110
    }
111
  }
112
  else // import
113
  {
114
    moveStyles( &selection, mTempStyle, mQgisStyle );
115
    mQgisStyle->save();
116

  
117
    // clear model
118
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
119
    model->clear();
120
    accept();
121
  }
122

  
123
  mFileName = "";
124
  mTempStyle->clear();
125

  
126
  return;
127
}
128

  
129
bool QgsStyleV2ExportImportDialog::populateStyles( QgsStyleV2* style )
130
{
131
  // load symbols and color ramps from file
132
  if ( mDialogMode == Import )
133
  {
134
    if ( !mTempStyle->load( mFileName ) )
135
    {
136
      QMessageBox::warning( this, tr( "Import error" ),
137
                            tr( "An error was occured during import:\n%1" ).arg( mTempStyle->errorString() ) );
138
      return false;
139
    }
140
  }
141

  
142
  QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
143
  model->clear();
144

  
145
  // populate symbols
146
  QStringList styleNames = style->symbolNames();
147
  QString name;
148

  
149
  for ( int i = 0; i < styleNames.count(); ++i )
150
  {
151
    name = styleNames[i];
152
    QgsSymbolV2* symbol = style->symbol( name );
153
    QStandardItem* item = new QStandardItem( name );
154
    QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, listItems->iconSize() );
155
    item->setIcon( icon );
156
    model->appendRow( item );
157
    delete symbol;
158
  }
159

  
160
  // and color ramps
161
  styleNames = style->colorRampNames();
162

  
163
  for ( int i = 0; i < styleNames.count(); ++i )
164
  {
165
    name = styleNames[i];
166
    QgsVectorColorRampV2* ramp = style->colorRamp( name );
167

  
168
    QStandardItem* item = new QStandardItem( name );
169
    QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, listItems->iconSize() );
170
    item->setIcon( icon );
171
    model->appendRow( item );
172
    delete ramp;
173
  }
174
  return true;
175
}
176

  
177
void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsStyleV2* src, QgsStyleV2* dst )
178
{
179
  QString symbolName;
180
  QgsSymbolV2* symbol;
181
  QgsVectorColorRampV2* ramp;
182
  QModelIndex index;
183
  bool isSymbol = true;
184
  bool prompt = true;
185
  bool overwrite = true;
186

  
187
  for( int i = 0; i < selection->size(); ++i )
188
  {
189
    index = selection->at( i );
190
    symbolName = index.model()->data( index, 0 ).toString();
191
    symbol = src->symbol( symbolName );
192
    if ( symbol == NULL )
193
    {
194
      isSymbol = false;
195
      ramp = src->colorRamp( symbolName );
196
    }
197

  
198
    if ( isSymbol )
199
    {
200
      if ( dst->symbolNames().contains(  symbolName ) && prompt )
201
      {
202
        int res = QMessageBox::warning( this, tr( "Duplicate symbol names" ),
203
                                        tr( "Symbol with name '%1' already exists.\nOverwrite?" )
204
                                        .arg( symbolName ),
205
                                        QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
206
        switch ( res )
207
        {
208
          case QMessageBox::Cancel:   return;
209
          case QMessageBox::No:       continue;
210
          case QMessageBox::Yes:      dst->addSymbol( symbolName, symbol );
211
                                      continue;
212
          case QMessageBox::YesToAll: prompt = false;
213
                                      overwrite = true;
214
                                      break;
215
          case QMessageBox::NoToAll:  prompt = false;
216
                                      overwrite = false;
217
                                      break;
218
        }
219
      }
220

  
221
      if ( dst->symbolNames().contains(  symbolName ) && overwrite )
222
      {
223
        dst->addSymbol( symbolName, symbol );
224
      }
225
      else if ( dst->symbolNames().contains(  symbolName ) && !overwrite )
226
      {
227
        continue;
228
      }
229
      else
230
      {
231
        dst->addSymbol( symbolName, symbol );
232
      }
233
    }
234
    else
235
    {
236
      if ( dst->colorRampNames().contains(  symbolName ) && prompt )
237
      {
238
        int res = QMessageBox::warning( this, tr( "Duplicate symbol names" ),
239
                                        tr( "Symbol with name '%1' already exists.\nOverwrite?" )
240
                                        .arg( symbolName ),
241
                                        QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
242
        switch ( res )
243
        {
244
          case QMessageBox::Cancel:   return;
245
          case QMessageBox::No:       continue;
246
          case QMessageBox::Yes:      dst->addColorRamp( symbolName, ramp );
247
                                      continue;
248
          case QMessageBox::YesToAll: prompt = false;
249
                                      overwrite = true;
250
                                      break;
251
          case QMessageBox::NoToAll:  prompt = false;
252
                                      overwrite = false;
253
                                      break;
254
        }
255
      }
256

  
257
      if ( dst->colorRampNames().contains(  symbolName ) && overwrite )
258
      {
259
        dst->addColorRamp( symbolName, ramp );
260
      }
261
      else if ( dst->colorRampNames().contains(  symbolName ) && !overwrite )
262
      {
263
        continue;
264
      }
265
      else
266
      {
267
        dst->addColorRamp( symbolName, ramp );
268
      }
269
    }
270
  }
271
}
272

  
273
QgsStyleV2ExportImportDialog::~QgsStyleV2ExportImportDialog()
274
{
275
  delete mTempStyle;
276
}
277

  
278
void QgsStyleV2ExportImportDialog::selectAll()
279
{
280
  listItems->selectAll();
281
}
282

  
283
void QgsStyleV2ExportImportDialog::clearSelection()
284
{
285
  listItems->clearSelection();
286
}
src/gui/symbology-ng/qgsstylev2managerdialog.h (working copy)
24 24
    void addItem();
25 25
    void editItem();
26 26
    void removeItem();
27
    void exportItems();
28
    void importItems();
27 29
    //! adds symbols of some type to list
28 30
    void populateList();
29 31

  
src/gui/symbology-ng/qgsstylev2exportimportdialog.h (revision 0)
1
/***************************************************************************
2
    qgsstylev2exportimportdialog.h
3
    ---------------------
4
    begin                : Dec 2011
5
    copyright            : (C) 2011 by Alexander Bruy
6
    email                : alexander dot bruy at gmail dot com
7

  
8
 ***************************************************************************
9
 *                                                                         *
10
 *   This program is free software; you can redistribute it and/or modify  *
11
 *   it under the terms of the GNU General Public License as published by  *
12
 *   the Free Software Foundation; either version 2 of the License, or     *
13
 *   (at your option) any later version.                                   *
14
 *                                                                         *
15
 ***************************************************************************/
16

  
17
/* $Id: qgsstylev2exportimportdialog.h 13187 2010-03-28 22:14:44Z jef $ */
18

  
19
#ifndef QGSSTYLEV2EXPORTIMPORTDIALOG_H
20
#define QGSSTYLEV2EXPORTIMPORTDIALOG_H
21

  
22
#include <QDialog>
23

  
24
#include "ui_qgsstylev2exportimportdialogbase.h"
25

  
26
class QgsStyleV2;
27

  
28
class QgsStyleV2ExportImportDialog : public QDialog, private Ui::QgsStyleV2ExportImportDialogBase
29
{
30
    Q_OBJECT
31

  
32
  public:
33
    enum Mode
34
    {
35
      Export,
36
      Import
37
    };
38

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

  
44
  public slots:
45
    void doExportImport();
46
    void selectAll();
47
    void clearSelection();
48

  
49
  private:
50
    bool populateStyles( QgsStyleV2* style );
51
    void moveStyles( QModelIndexList* selection, QgsStyleV2* src, QgsStyleV2* dst );
52

  
53
    QString mFileName;
54
    Mode mDialogMode;
55

  
56
    QgsStyleV2* mQgisStyle;
57
    QgsStyleV2* mTempStyle;
58
};
59

  
60
#endif // QGSSTYLEV2EXPORTIMPORTDIALOG_H
src/gui/CMakeLists.txt (working copy)
20 20
symbology-ng/qgsvectorrandomcolorrampv2dialog.cpp
21 21
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.cpp
22 22
symbology-ng/characterwidget.cpp
23
symbology-ng/qgsstylev2exportimportdialog.cpp
23 24

  
24 25
qgisgui.cpp
25 26
qgisinterface.cpp
......
79 80
symbology-ng/characterwidget.h
80 81
symbology-ng/qgspenstylecombobox.h
81 82
symbology-ng/qgsbrushstylecombobox.h
83
symbology-ng/qgsstylev2exportimportdialog.h
82 84

  
83 85
qgsattributeeditor.h
84 86
qgscomposerview.h
src/ui/qgsstylev2exportimportdialogbase.ui (revision 0)
1
<?xml version="1.0" encoding="UTF-8"?>
2
<ui version="4.0">
3
 <class>QgsStyleV2ExportImportDialogBase</class>
4
 <widget class="QDialog" name="QgsStyleV2ExportImportDialogBase">
5
  <property name="geometry">
6
   <rect>
7
    <x>0</x>
8
    <y>0</y>
9
    <width>400</width>
10
    <height>300</height>
11
   </rect>
12
  </property>
13
  <property name="windowTitle">
14
   <string>Styles import/export</string>
15
  </property>
16
  <layout class="QVBoxLayout" name="verticalLayout">
17
   <item>
18
    <widget class="QLabel" name="label">
19
     <property name="text">
20
      <string>Select symbols to export</string>
21
     </property>
22
    </widget>
23
   </item>
24
   <item>
25
    <widget class="QListView" name="listItems">
26
     <property name="sizePolicy">
27
      <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
28
       <horstretch>0</horstretch>
29
       <verstretch>3</verstretch>
30
      </sizepolicy>
31
     </property>
32
     <property name="editTriggers">
33
      <set>QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
34
     </property>
35
     <property name="selectionMode">
36
      <enum>QAbstractItemView::ExtendedSelection</enum>
37
     </property>
38
     <property name="iconSize">
39
      <size>
40
       <width>48</width>
41
       <height>48</height>
42
      </size>
43
     </property>
44
     <property name="movement">
45
      <enum>QListView::Static</enum>
46
     </property>
47
     <property name="resizeMode">
48
      <enum>QListView::Adjust</enum>
49
     </property>
50
     <property name="spacing">
51
      <number>5</number>
52
     </property>
53
     <property name="viewMode">
54
      <enum>QListView::IconMode</enum>
55
     </property>
56
    </widget>
57
   </item>
58
   <item>
59
    <widget class="QDialogButtonBox" name="buttonBox">
60
     <property name="orientation">
61
      <enum>Qt::Horizontal</enum>
62
     </property>
63
     <property name="standardButtons">
64
      <set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
65
     </property>
66
    </widget>
67
   </item>
68
  </layout>
69
 </widget>
70
 <resources/>
71
 <connections>
72
  <connection>
73
   <sender>buttonBox</sender>
74
   <signal>accepted()</signal>
75
   <receiver>QgsStyleV2ExportImportDialogBase</receiver>
76
   <slot>accept()</slot>
77
   <hints>
78
    <hint type="sourcelabel">
79
     <x>248</x>
80
     <y>254</y>
81
    </hint>
82
    <hint type="destinationlabel">
83
     <x>157</x>
84
     <y>274</y>
85
    </hint>
86
   </hints>
87
  </connection>
88
  <connection>
89
   <sender>buttonBox</sender>
90
   <signal>rejected()</signal>
91
   <receiver>QgsStyleV2ExportImportDialogBase</receiver>
92
   <slot>reject()</slot>
93
   <hints>
94
    <hint type="sourcelabel">
95
     <x>316</x>
96
     <y>260</y>
97
    </hint>
98
    <hint type="destinationlabel">
99
     <x>286</x>
100
     <y>274</y>
101
    </hint>
102
   </hints>
103
  </connection>
104
 </connections>
105
</ui>
src/ui/qgsstylev2managerdialogbase.ui (working copy)
169 169
       </property>
170 170
      </spacer>
171 171
     </item>
172
     <item>
173
      <widget class="QPushButton" name="btnExportItems">
174
       <property name="text">
175
        <string>Export</string>
176
       </property>
177
      </widget>
178
     </item>
179
     <item>
180
      <widget class="QPushButton" name="btnImportItems">
181
       <property name="text">
182
        <string>Import</string>
183
       </property>
184
      </widget>
185
     </item>
172 186
    </layout>
173 187
   </item>
174 188
   <item>
......
187 201
  <tabstop>listItems</tabstop>
188 202
  <tabstop>btnAddItem</tabstop>
189 203
  <tabstop>btnEditItem</tabstop>
190
  <tabstop>btnRemoveItem</tabstop>
191 204
  <tabstop>buttonBox</tabstop>
192 205
 </tabstops>
193 206
 <resources>