[QGIS Commit] r15236 - in trunk/qgis/src: gui gui/symbology-ng ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Feb 21 17:20:23 EST 2011


Author: wonder
Date: 2011-02-21 14:20:23 -0800 (Mon, 21 Feb 2011)
New Revision: 15236

Added:
   trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp
   trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.h
   trunk/qgis/src/ui/qgsstylev2exportimportdialogbase.ui
Modified:
   trunk/qgis/src/gui/CMakeLists.txt
   trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.cpp
   trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.h
   trunk/qgis/src/ui/qgsstylev2managerdialogbase.ui
Log:
[FEATURE] Export and import of styles (symbology-ng). Contributed by Alex Bruy (#3425)

(finally we have an easy way for sharing of symbols and color ramps!)


Modified: trunk/qgis/src/gui/CMakeLists.txt
===================================================================
--- trunk/qgis/src/gui/CMakeLists.txt	2011-02-21 18:15:04 UTC (rev 15235)
+++ trunk/qgis/src/gui/CMakeLists.txt	2011-02-21 22:20:23 UTC (rev 15236)
@@ -20,6 +20,7 @@
 symbology-ng/qgsvectorrandomcolorrampv2dialog.cpp
 symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.cpp
 symbology-ng/characterwidget.cpp
+symbology-ng/qgsstylev2exportimportdialog.cpp
 
 qgisgui.cpp
 qgisinterface.cpp
@@ -79,6 +80,7 @@
 symbology-ng/characterwidget.h
 symbology-ng/qgspenstylecombobox.h
 symbology-ng/qgsbrushstylecombobox.h
+symbology-ng/qgsstylev2exportimportdialog.h
 
 qgsattributeeditor.h
 qgscomposerview.h

Added: trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp
===================================================================
--- trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp	                        (rev 0)
+++ trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.cpp	2011-02-21 22:20:23 UTC (rev 15236)
@@ -0,0 +1,286 @@
+/***************************************************************************
+    qgsstylev2exportimportdialog.cpp
+    ---------------------
+    begin                : Jan 2011
+    copyright            : (C) 2011 by Alexander Bruy
+    email                : alexander dot bruy at gmail dot com
+
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+/* $Id: qgsstylev2exportimportdialog.cpp 13187 2010-03-28 22:14:44Z jef $ */
+
+#include <QCloseEvent>
+#include <QFileDialog>
+#include <QMessageBox>
+#include <QPushButton>
+#include <QStandardItemModel>
+
+#include "qgsstylev2exportimportdialog.h"
+
+#include "qgsstylev2.h"
+#include "qgssymbolv2.h"
+#include "qgssymbollayerv2utils.h"
+#include "qgsvectorcolorrampv2.h"
+
+QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style,
+    QWidget *parent, Mode mode, QString fileName )
+    : QDialog( parent ), mDialogMode( mode ), mQgisStyle( style ), mFileName( fileName )
+{
+  setupUi( this );
+
+  // additional buttons
+  QPushButton *pb;
+  pb = new QPushButton( tr( "Select all" ) );
+  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
+  connect( pb, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
+
+  pb = new QPushButton( tr( "Clear selection" ) );
+  buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
+  connect( pb, SIGNAL( clicked() ), this, SLOT( clearSelection() ) );
+
+  QStandardItemModel* model = new QStandardItemModel( listItems );
+  listItems->setModel( model );
+
+  mTempStyle = new QgsStyleV2();
+
+  if ( mDialogMode == Import )
+  {
+    label->setText( tr( "Select symbols to import" ) );
+    buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
+    if ( !populateStyles( mTempStyle ) )
+    {
+      QApplication::postEvent( this, new QCloseEvent() );
+    }
+  }
+  else
+  {
+    buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
+    if ( !populateStyles( mQgisStyle ) )
+    {
+      QApplication::postEvent( this, new QCloseEvent() );
+    }
+  }
+
+  // use Ok button for starting import and export operations
+  disconnect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
+  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( doExportImport() ) );
+}
+
+void QgsStyleV2ExportImportDialog::doExportImport()
+{
+  QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
+  if ( selection.isEmpty() )
+  {
+    QMessageBox::warning( this, tr( "Export/import error" ),
+                          tr( "You should select at least one symbol/color ramp." ) );
+    return;
+  }
+
+  if ( mDialogMode == Export )
+  {
+    QString fileName = QFileDialog::getSaveFileName( this, tr( "Save styles" ), ".",
+                                                     tr( "XML files (*.xml *.XML)" ) );
+    if ( fileName.isEmpty() )
+    {
+      return;
+    }
+
+    // ensure the user never ommited the extension from the file name
+    if ( !fileName.toLower().endsWith( ".xml" ) )
+    {
+      fileName += ".xml";
+    }
+
+    mFileName = fileName;
+
+    moveStyles( &selection, mQgisStyle, mTempStyle );
+    if ( !mTempStyle->save( mFileName ) )
+    {
+      QMessageBox::warning( this, tr( "Export/import error" ),
+                            tr( "Error when saving selected symbols to file:\n%1" )
+                            .arg( mTempStyle->errorString() ) );
+      return;
+    }
+  }
+  else // import
+  {
+    moveStyles( &selection, mTempStyle, mQgisStyle );
+    mQgisStyle->save();
+
+    // clear model
+    QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
+    model->clear();
+    accept();
+  }
+
+  mFileName = "";
+  mTempStyle->clear();
+
+  return;
+}
+
+bool QgsStyleV2ExportImportDialog::populateStyles( QgsStyleV2* style )
+{
+  // load symbols and color ramps from file
+  if ( mDialogMode == Import )
+  {
+    if ( !mTempStyle->load( mFileName ) )
+    {
+      QMessageBox::warning( this, tr( "Import error" ),
+                            tr( "An error was occured during import:\n%1" ).arg( mTempStyle->errorString() ) );
+      return false;
+    }
+  }
+
+  QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
+  model->clear();
+
+  // populate symbols
+  QStringList styleNames = style->symbolNames();
+  QString name;
+
+  for ( int i = 0; i < styleNames.count(); ++i )
+  {
+    name = styleNames[i];
+    QgsSymbolV2* symbol = style->symbol( name );
+    QStandardItem* item = new QStandardItem( name );
+    QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, listItems->iconSize() );
+    item->setIcon( icon );
+    model->appendRow( item );
+    delete symbol;
+  }
+
+  // and color ramps
+  styleNames = style->colorRampNames();
+
+  for ( int i = 0; i < styleNames.count(); ++i )
+  {
+    name = styleNames[i];
+    QgsVectorColorRampV2* ramp = style->colorRamp( name );
+
+    QStandardItem* item = new QStandardItem( name );
+    QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, listItems->iconSize() );
+    item->setIcon( icon );
+    model->appendRow( item );
+    delete ramp;
+  }
+  return true;
+}
+
+void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsStyleV2* src, QgsStyleV2* dst )
+{
+  QString symbolName;
+  QgsSymbolV2* symbol;
+  QgsVectorColorRampV2* ramp;
+  QModelIndex index;
+  bool isSymbol = true;
+  bool prompt = true;
+  bool overwrite = true;
+
+  for( int i = 0; i < selection->size(); ++i )
+  {
+    index = selection->at( i );
+    symbolName = index.model()->data( index, 0 ).toString();
+    symbol = src->symbol( symbolName );
+    if ( symbol == NULL )
+    {
+      isSymbol = false;
+      ramp = src->colorRamp( symbolName );
+    }
+
+    if ( isSymbol )
+    {
+      if ( dst->symbolNames().contains(  symbolName ) && prompt )
+      {
+        int res = QMessageBox::warning( this, tr( "Duplicate names" ),
+                                        tr( "Symbol with name '%1' already exists.\nOverwrite?" )
+                                        .arg( symbolName ),
+                                        QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
+        switch ( res )
+        {
+          case QMessageBox::Cancel:   return;
+          case QMessageBox::No:       continue;
+          case QMessageBox::Yes:      dst->addSymbol( symbolName, symbol );
+                                      continue;
+          case QMessageBox::YesToAll: prompt = false;
+                                      overwrite = true;
+                                      break;
+          case QMessageBox::NoToAll:  prompt = false;
+                                      overwrite = false;
+                                      break;
+        }
+      }
+
+      if ( dst->symbolNames().contains(  symbolName ) && overwrite )
+      {
+        dst->addSymbol( symbolName, symbol );
+      }
+      else if ( dst->symbolNames().contains(  symbolName ) && !overwrite )
+      {
+        continue;
+      }
+      else
+      {
+        dst->addSymbol( symbolName, symbol );
+      }
+    }
+    else
+    {
+      if ( dst->colorRampNames().contains(  symbolName ) && prompt )
+      {
+        int res = QMessageBox::warning( this, tr( "Duplicate names" ),
+                                        tr( "Color ramp with name '%1' already exists.\nOverwrite?" )
+                                        .arg( symbolName ),
+                                        QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
+        switch ( res )
+        {
+          case QMessageBox::Cancel:   return;
+          case QMessageBox::No:       continue;
+          case QMessageBox::Yes:      dst->addColorRamp( symbolName, ramp );
+                                      continue;
+          case QMessageBox::YesToAll: prompt = false;
+                                      overwrite = true;
+                                      break;
+          case QMessageBox::NoToAll:  prompt = false;
+                                      overwrite = false;
+                                      break;
+        }
+      }
+
+      if ( dst->colorRampNames().contains(  symbolName ) && overwrite )
+      {
+        dst->addColorRamp( symbolName, ramp );
+      }
+      else if ( dst->colorRampNames().contains(  symbolName ) && !overwrite )
+      {
+        continue;
+      }
+      else
+      {
+        dst->addColorRamp( symbolName, ramp );
+      }
+    }
+  }
+}
+
+QgsStyleV2ExportImportDialog::~QgsStyleV2ExportImportDialog()
+{
+  delete mTempStyle;
+}
+
+void QgsStyleV2ExportImportDialog::selectAll()
+{
+  listItems->selectAll();
+}
+
+void QgsStyleV2ExportImportDialog::clearSelection()
+{
+  listItems->clearSelection();
+}

Added: trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.h
===================================================================
--- trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.h	                        (rev 0)
+++ trunk/qgis/src/gui/symbology-ng/qgsstylev2exportimportdialog.h	2011-02-21 22:20:23 UTC (rev 15236)
@@ -0,0 +1,60 @@
+/***************************************************************************
+    qgsstylev2exportimportdialog.h
+    ---------------------
+    begin                : Jan 2011
+    copyright            : (C) 2011 by Alexander Bruy
+    email                : alexander dot bruy at gmail dot com
+
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+/* $Id: qgsstylev2exportimportdialog.h 13187 2010-03-28 22:14:44Z jef $ */
+
+#ifndef QGSSTYLEV2EXPORTIMPORTDIALOG_H
+#define QGSSTYLEV2EXPORTIMPORTDIALOG_H
+
+#include <QDialog>
+
+#include "ui_qgsstylev2exportimportdialogbase.h"
+
+class QgsStyleV2;
+
+class QgsStyleV2ExportImportDialog : public QDialog, private Ui::QgsStyleV2ExportImportDialogBase
+{
+    Q_OBJECT
+
+  public:
+    enum Mode
+    {
+      Export,
+      Import
+    };
+
+    // constructor
+    // mode argument must be 0 for saving and 1 for loading
+    QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent = NULL, Mode mode = Export, QString fileName = "" );
+    ~QgsStyleV2ExportImportDialog();
+
+  public slots:
+    void doExportImport();
+    void selectAll();
+    void clearSelection();
+
+  private:
+    bool populateStyles( QgsStyleV2* style );
+    void moveStyles( QModelIndexList* selection, QgsStyleV2* src, QgsStyleV2* dst );
+
+    QString mFileName;
+    Mode mDialogMode;
+
+    QgsStyleV2* mQgisStyle;
+    QgsStyleV2* mTempStyle;
+};
+
+#endif // QGSSTYLEV2EXPORTIMPORTDIALOG_H

Modified: trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.cpp
===================================================================
--- trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.cpp	2011-02-21 18:15:04 UTC (rev 15235)
+++ trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.cpp	2011-02-21 22:20:23 UTC (rev 15236)
@@ -10,8 +10,10 @@
 #include "qgsvectorgradientcolorrampv2dialog.h"
 #include "qgsvectorrandomcolorrampv2dialog.h"
 #include "qgsvectorcolorbrewercolorrampv2dialog.h"
+#include "qgsstylev2exportimportdialog.h"
 
 #include <QFile>
+#include <QFileDialog>
 #include <QInputDialog>
 #include <QStandardItemModel>
 
@@ -42,6 +44,8 @@
   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() ) );
 
   QStandardItemModel* model = new QStandardItemModel( listItems );
   listItems->setModel( model );
@@ -479,3 +483,23 @@
     mModified = true;
   }
 }
+
+void QgsStyleV2ManagerDialog::exportItems()
+{
+  QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Export );
+  dlg.exec();
+}
+
+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 );
+  dlg.exec();
+  populateList();
+}

Modified: trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.h
===================================================================
--- trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.h	2011-02-21 18:15:04 UTC (rev 15235)
+++ trunk/qgis/src/gui/symbology-ng/qgsstylev2managerdialog.h	2011-02-21 22:20:23 UTC (rev 15236)
@@ -24,6 +24,8 @@
     void addItem();
     void editItem();
     void removeItem();
+    void exportItems();
+    void importItems();
     //! adds symbols of some type to list
     void populateList();
 

Added: trunk/qgis/src/ui/qgsstylev2exportimportdialogbase.ui
===================================================================
--- trunk/qgis/src/ui/qgsstylev2exportimportdialogbase.ui	                        (rev 0)
+++ trunk/qgis/src/ui/qgsstylev2exportimportdialogbase.ui	2011-02-21 22:20:23 UTC (rev 15236)
@@ -0,0 +1,105 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>QgsStyleV2ExportImportDialogBase</class>
+ <widget class="QDialog" name="QgsStyleV2ExportImportDialogBase">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>300</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Styles import/export</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QLabel" name="label">
+     <property name="text">
+      <string>Select symbols to export</string>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QListView" name="listItems">
+     <property name="sizePolicy">
+      <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+       <horstretch>0</horstretch>
+       <verstretch>3</verstretch>
+      </sizepolicy>
+     </property>
+     <property name="editTriggers">
+      <set>QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
+     </property>
+     <property name="selectionMode">
+      <enum>QAbstractItemView::ExtendedSelection</enum>
+     </property>
+     <property name="iconSize">
+      <size>
+       <width>48</width>
+       <height>48</height>
+      </size>
+     </property>
+     <property name="movement">
+      <enum>QListView::Static</enum>
+     </property>
+     <property name="resizeMode">
+      <enum>QListView::Adjust</enum>
+     </property>
+     <property name="spacing">
+      <number>5</number>
+     </property>
+     <property name="viewMode">
+      <enum>QListView::IconMode</enum>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>QgsStyleV2ExportImportDialogBase</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>QgsStyleV2ExportImportDialogBase</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>

Modified: trunk/qgis/src/ui/qgsstylev2managerdialogbase.ui
===================================================================
--- trunk/qgis/src/ui/qgsstylev2managerdialogbase.ui	2011-02-21 18:15:04 UTC (rev 15235)
+++ trunk/qgis/src/ui/qgsstylev2managerdialogbase.ui	2011-02-21 22:20:23 UTC (rev 15236)
@@ -169,6 +169,20 @@
        </property>
       </spacer>
      </item>
+     <item>
+      <widget class="QPushButton" name="btnExportItems">
+       <property name="text">
+        <string>Export...</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="btnImportItems">
+       <property name="text">
+        <string>Import...</string>
+       </property>
+      </widget>
+     </item>
     </layout>
    </item>
    <item>
@@ -187,7 +201,6 @@
   <tabstop>listItems</tabstop>
   <tabstop>btnAddItem</tabstop>
   <tabstop>btnEditItem</tabstop>
-  <tabstop>btnRemoveItem</tabstop>
   <tabstop>buttonBox</tabstop>
  </tabstops>
  <resources>



More information about the QGIS-commit mailing list