[QGIS Commit] r10887 - in branches/symbology-ng-branch/src/plugins: . labeling

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Jun 5 16:08:15 EDT 2009


Author: wonder
Date: 2009-06-05 16:08:15 -0400 (Fri, 05 Jun 2009)
New Revision: 10887

Added:
   branches/symbology-ng-branch/src/plugins/labeling/
   branches/symbology-ng-branch/src/plugins/labeling/CMakeLists.txt
   branches/symbology-ng-branch/src/plugins/labeling/labeling.cpp
   branches/symbology-ng-branch/src/plugins/labeling/labeling.h
   branches/symbology-ng-branch/src/plugins/labeling/labeling.png
   branches/symbology-ng-branch/src/plugins/labeling/labeling.qrc
   branches/symbology-ng-branch/src/plugins/labeling/labelinggui.cpp
   branches/symbology-ng-branch/src/plugins/labeling/labelinggui.h
   branches/symbology-ng-branch/src/plugins/labeling/labelingguibase.ui
   branches/symbology-ng-branch/src/plugins/labeling/pallabeling.cpp
   branches/symbology-ng-branch/src/plugins/labeling/pallabeling.h
Modified:
   branches/symbology-ng-branch/src/plugins/CMakeLists.txt
Log:
First prototype of labeling plugin based on PAL (usable already)


Modified: branches/symbology-ng-branch/src/plugins/CMakeLists.txt
===================================================================
--- branches/symbology-ng-branch/src/plugins/CMakeLists.txt	2009-06-05 14:47:10 UTC (rev 10886)
+++ branches/symbology-ng-branch/src/plugins/CMakeLists.txt	2009-06-05 20:08:15 UTC (rev 10887)
@@ -32,3 +32,5 @@
 SUBDIRS (ogr_converter)
 
 SUBDIRS (diagram_overlay)
+
+SUBDIRS (labeling)

Added: branches/symbology-ng-branch/src/plugins/labeling/CMakeLists.txt
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/CMakeLists.txt	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/CMakeLists.txt	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,50 @@
+
+########################################################
+# Files
+
+SET (labeling_SRCS
+     labeling.cpp
+     labelinggui.cpp
+     pallabeling.cpp
+)
+
+SET (labeling_UIS labelingguibase.ui)
+
+SET (labeling_MOC_HDRS
+     labeling.h
+     labelinggui.h
+)
+
+SET (labeling_RCCS  labeling.qrc)
+
+########################################################
+# Build
+
+QT4_WRAP_UI (labeling_UIS_H  ${labeling_UIS})
+
+QT4_WRAP_CPP (labeling_MOC_SRCS  ${labeling_MOC_HDRS})
+
+QT4_ADD_RESOURCES(labeling_RCC_SRCS ${labeling_RCCS})
+
+ADD_LIBRARY (labelingplugin MODULE ${labeling_SRCS} ${labeling_MOC_SRCS} ${labeling_RCC_SRCS} ${labeling_UIS_H})
+
+INCLUDE_DIRECTORIES(
+     ${CMAKE_CURRENT_BINARY_DIR}
+     ../../core ../../core/raster ../../core/renderer ../../core/symbology
+     ../../gui
+     ..
+)
+
+TARGET_LINK_LIBRARIES(labelingplugin
+  qgis_core
+  qgis_gui
+)
+
+
+########################################################
+# Install
+
+INSTALL(TARGETS labelingplugin
+  RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
+  LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})
+

Added: branches/symbology-ng-branch/src/plugins/labeling/labeling.cpp
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/labeling.cpp	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/labeling.cpp	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,195 @@
+/***************************************************************************
+  labeling.cpp
+  Smart labeling for vector layers
+  -------------------
+         begin                : June 2009
+         copyright            : (C) Martin Dobias
+         email                : wonder.sk at gmail.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.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+//
+// QGIS Specific includes
+//
+
+#include <qgisinterface.h>
+#include <qgisgui.h>
+#include <qgsmapcanvas.h>
+#include <qgsvectorlayer.h>
+
+#include "labeling.h"
+#include "labelinggui.h"
+#include "pallabeling.h"
+
+//
+// Qt4 Related Includes
+//
+
+#include <QAction>
+#include <QMessageBox>
+#include <QPainter>
+#include <QToolBar>
+
+
+static const char * const sIdent = "$Id: plugin.cpp 9327 2008-09-14 11:18:44Z jef $";
+static const QString sName = QObject::tr( "Labeling" );
+static const QString sDescription = QObject::tr( "Smart labeling for vector layers" );
+static const QString sPluginVersion = QObject::tr( "Version 0.1" );
+static const QgisPlugin::PLUGINTYPE sPluginType = QgisPlugin::UI;
+
+//////////////////////////////////////////////////////////////////////
+//
+// THE FOLLOWING METHODS ARE MANDATORY FOR ALL PLUGINS
+//
+//////////////////////////////////////////////////////////////////////
+
+/**
+ * Constructor for the plugin. The plugin is passed a pointer
+ * an interface object that provides access to exposed functions in QGIS.
+ * @param theQGisInterface - Pointer to the QGIS interface object
+ */
+Labeling::Labeling( QgisInterface * theQgisInterface ):
+    QgisPlugin( sName, sDescription, sPluginVersion, sPluginType ),
+    mQGisIface( theQgisInterface )
+{
+}
+
+Labeling::~Labeling()
+{
+}
+
+/*
+ * Initialize the GUI interface for the plugin - this is only called once when the plugin is
+ * added to the plugin registry in the QGIS application.
+ */
+void Labeling::initGui()
+{
+  mLBL = new PalLabeling(mQGisIface->mapCanvas());
+
+  // Create the action for tool
+  mQActionPointer = new QAction( QIcon( ":/labeling/labeling.png" ), tr( "Labeling" ), this );
+  // Set the what's this text
+  mQActionPointer->setWhatsThis( tr( "Replace this with a short description of what the plugin does" ) );
+  // Connect the action to the run
+  connect( mQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
+  // Add the icon to the toolbar
+  mQGisIface->addToolBarIcon( mQActionPointer );
+  mQGisIface->addPluginToMenu( tr( "&Labeling" ), mQActionPointer );
+
+  connect( mQGisIface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ), this, SLOT( doLabeling( QPainter * ) ) );
+
+}
+
+void Labeling::doLabeling( QPainter * painter )
+{
+  int w = painter->device()->width();
+  int h = painter->device()->height();
+
+
+  QgsMapLayer* layer = mQGisIface->activeLayer();
+  if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
+  {
+    painter->drawLine(0,0,w,h);
+    return;
+  }
+
+  mLBL->doLabeling(painter);
+}
+
+// Slot called when the menu item is triggered
+// If you created more menu items / toolbar buttons in initiGui, you should
+// create a separate handler for each action - this single run() method will
+// not be enough
+void Labeling::run()
+{
+  QgsMapLayer* layer = mQGisIface->activeLayer();
+  if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
+  {
+    QMessageBox::warning(mQGisIface->mainWindow(), "Labeling", "Please select a vector layer first.");
+    return;
+  }
+  //QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>(layer);
+
+  LabelingGui myPluginGui( mLBL, layer->getLayerID(), mQGisIface->mainWindow() );
+
+  if (myPluginGui.exec())
+  {
+    // alter labeling
+    mLBL->removeLayer(layer->getLayerID());
+    mLBL->addLayer( myPluginGui.layerSettings() );
+
+    // trigger refresh
+    mQGisIface->mapCanvas()->refresh();
+  }
+}
+
+// Unload the plugin by cleaning up the GUI
+void Labeling::unload()
+{
+  // remove the GUI
+  mQGisIface->removePluginMenu( "&Labeling", mQActionPointer );
+  mQGisIface->removeToolBarIcon( mQActionPointer );
+  delete mQActionPointer;
+
+  delete mLBL;
+}
+
+
+//////////////////////////////////////////////////////////////////////////
+//
+//
+//  THE FOLLOWING CODE IS AUTOGENERATED BY THE PLUGIN BUILDER SCRIPT
+//    YOU WOULD NORMALLY NOT NEED TO MODIFY THIS, AND YOUR PLUGIN
+//      MAY NOT WORK PROPERLY IF YOU MODIFY THIS INCORRECTLY
+//
+//
+//////////////////////////////////////////////////////////////////////////
+
+
+/**
+ * Required extern functions needed  for every plugin
+ * These functions can be called prior to creating an instance
+ * of the plugin class
+ */
+// Class factory to return a new instance of the plugin class
+QGISEXTERN QgisPlugin * classFactory( QgisInterface * theQgisInterfacePointer )
+{
+  return new Labeling( theQgisInterfacePointer );
+}
+// Return the name of the plugin - note that we do not user class members as
+// the class may not yet be insantiated when this method is called.
+QGISEXTERN QString name()
+{
+  return sName;
+}
+
+// Return the description
+QGISEXTERN QString description()
+{
+  return sDescription;
+}
+
+// Return the type (either UI or MapLayer plugin)
+QGISEXTERN int type()
+{
+  return sPluginType;
+}
+
+// Return the version number for the plugin
+QGISEXTERN QString version()
+{
+  return sPluginVersion;
+}
+
+// Delete ourself
+QGISEXTERN void unload( QgisPlugin * thePluginPointer )
+{
+  delete thePluginPointer;
+}

Added: branches/symbology-ng-branch/src/plugins/labeling/labeling.h
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/labeling.h	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/labeling.h	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,71 @@
+/***************************************************************************
+  labeling.h
+  Smart labeling for vector layers
+  -------------------
+         begin                : June 2009
+         copyright            : (C) Martin Dobias
+         email                : wonder.sk at gmail.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.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef Labeling_H
+#define Labeling_H
+
+//QT4 includes
+#include <QObject>
+
+//QGIS includes
+#include "../qgisplugin.h"
+
+//forward declarations
+class QAction;
+class QPainter;
+class QToolBar;
+
+class QgisInterface;
+
+class PalLabeling;
+
+class Labeling: public QObject, public QgisPlugin
+{
+    Q_OBJECT
+  public:
+
+    /**
+    * Constructor for a plugin. The QgisInterface pointer is passed by
+    * QGIS when it attempts to instantiate the plugin.
+    * @param theInterface Pointer to the QgisInterface object.
+     */
+    Labeling( QgisInterface * theInterface );
+    //! Destructor
+    virtual ~Labeling();
+
+  public slots:
+    //! init the gui
+    virtual void initGui();
+    //! Show the dialog box
+    void run();
+    //! unload the plugin
+    void unload();
+
+    //! hook to renderComplete signal
+    void doLabeling(QPainter* painter);
+
+  private:
+
+    //! Pointer to the QGIS interface object
+    QgisInterface *mQGisIface;
+    //! Pointer to the qaction for this plugin
+    QAction * mQActionPointer;
+
+    PalLabeling* mLBL;
+};
+
+#endif //Labeling_H

Added: branches/symbology-ng-branch/src/plugins/labeling/labeling.png
===================================================================
(Binary files differ)


Property changes on: branches/symbology-ng-branch/src/plugins/labeling/labeling.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: branches/symbology-ng-branch/src/plugins/labeling/labeling.qrc
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/labeling.qrc	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/labeling.qrc	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/labeling/" >
+        <file>labeling.png</file>
+    </qresource>
+</RCC>

Added: branches/symbology-ng-branch/src/plugins/labeling/labelinggui.cpp
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/labelinggui.cpp	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/labelinggui.cpp	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,144 @@
+/***************************************************************************
+  labelinggui.cpp
+  Smart labeling for vector layers
+  -------------------
+         begin                : June 2009
+         copyright            : (C) Martin Dobias
+         email                : wonder.sk at gmail.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.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "labelinggui.h"
+
+#include <qgsvectorlayer.h>
+#include <qgsvectordataprovider.h>
+#include <qgsmaplayerregistry.h>
+
+#include "pallabeling.h"
+
+#include <QColorDialog>
+#include <QFontDialog>
+
+#include <iostream>
+#include <QApplication>
+
+LabelingGui::LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent )
+    : QDialog( parent ), mLBL( lbl ), mLayerId( layerId )
+{
+  setupUi( this );
+
+  connect(btnTextColor, SIGNAL(clicked()), this, SLOT(changeTextColor()) );
+  connect(btnChangeFont, SIGNAL(clicked()), this, SLOT(changeTextFont()) );
+
+  populatePlacementMethods();
+  populateFieldNames();
+
+  PalLabeling::LayerSettings lyr = lbl->layer(layerId);
+  if (!lyr.layerId.isEmpty())
+  {
+    // load the labeling settings
+    cboPlacement->setCurrentIndex( cboPlacement->findData( QVariant( (int)lyr.placement ) ) );
+    cboFieldName->setCurrentIndex( cboFieldName->findText(lyr.fieldName) );
+    chkEnableLabeling->setChecked( lyr.enabled );
+  }
+  else
+  {
+    // set enabled by default
+    chkEnableLabeling->setChecked( true );
+  }
+
+  btnTextColor->setColor( lyr.textColor );
+  updateFontPreview( lyr.textFont );
+}
+
+LabelingGui::~LabelingGui()
+{
+}
+
+QgsVectorLayer* LabelingGui::layer()
+{
+  QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer(mLayerId);
+  if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
+    return NULL;
+  return static_cast<QgsVectorLayer*>(layer);
+}
+
+PalLabeling::LayerSettings LabelingGui::layerSettings()
+{
+  PalLabeling::LayerSettings lyr;
+  lyr.layerId = mLayerId;
+  lyr.fieldName = cboFieldName->currentText();
+  lyr.placement = (PalLabeling::Placement) cboPlacement->itemData(cboPlacement->currentIndex()).toInt();
+  lyr.textColor = btnTextColor->color();
+  lyr.textFont = lblFontPreview->font();
+  lyr.enabled = chkEnableLabeling->isChecked();
+
+  return lyr;
+}
+
+void LabelingGui::populatePlacementMethods()
+{
+  switch (layer()->geometryType())
+  {
+    case QGis::Point:
+      cboPlacement->addItem(tr("Around the point"), QVariant(PalLabeling::AroundPoint));
+      break;
+    case QGis::Line:
+      cboPlacement->addItem(tr("On the line"), QVariant(PalLabeling::OnLine));
+      cboPlacement->addItem(tr("Around the line"), QVariant(PalLabeling::AroundLine));
+      break;
+    case QGis::Polygon:
+      cboPlacement->addItem(tr("Horizontal"), QVariant(PalLabeling::Horizontal));
+      cboPlacement->addItem(tr("Free"), QVariant(PalLabeling::Free));
+      cboPlacement->addItem(tr("Around the centroid"), QVariant(PalLabeling::AroundPoint));
+      cboPlacement->addItem(tr("On the perimeter"), QVariant(PalLabeling::OnLine));
+      cboPlacement->addItem(tr("Around the perimeter"), QVariant(PalLabeling::AroundLine));
+      break;
+  }
+}
+
+void LabelingGui::populateFieldNames()
+{
+  QgsFieldMap fields = layer()->dataProvider()->fields();
+  for (QgsFieldMap::iterator it = fields.begin(); it != fields.end(); it++)
+  {
+    cboFieldName->addItem(it->name());
+  }
+}
+
+void LabelingGui::changeTextColor()
+{
+  QColor color = QColorDialog::getColor( btnTextColor->color(), this);
+  if (!color.isValid())
+    return;
+
+  btnTextColor->setColor(color);
+  updateFontPreview( lblFontPreview->font() );
+}
+
+void LabelingGui::changeTextFont()
+{
+  bool ok;
+  QFont font = QFontDialog::getFont(&ok, lblFontPreview->font(), this);
+  if (ok)
+    updateFontPreview( font );
+}
+
+void LabelingGui::updateFontPreview(QFont font)
+{
+  lblFontName->setText( QString("%1, %2").arg(font.family()).arg(font.pointSize()) );
+  lblFontPreview->setFont(font);
+
+  QPalette palette = lblFontPreview->palette();
+  QBrush brush(btnTextColor->color());
+  palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
+  palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);
+  lblFontPreview->setPalette(palette);
+}

Added: branches/symbology-ng-branch/src/plugins/labeling/labelinggui.h
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/labelinggui.h	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/labelinggui.h	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,56 @@
+/***************************************************************************
+  labelinggui.h
+  Smart labeling for vector layers
+  -------------------
+         begin                : June 2009
+         copyright            : (C) Martin Dobias
+         email                : wonder.sk at gmail.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.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef LabelingGUI_H
+#define LabelingGUI_H
+
+#include <QDialog>
+#include <ui_labelingguibase.h>
+
+//class PalLabeling;
+class QgsVectorLayer;
+//struct PalLabeling::LayerSettings;
+
+#include "pallabeling.h"
+
+class LabelingGui : public QDialog, private Ui::LabelingGuiBase
+{
+  Q_OBJECT
+
+  public:
+    LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent );
+    ~LabelingGui();
+
+    PalLabeling::LayerSettings layerSettings();
+
+  public slots:
+    void changeTextColor();
+    void changeTextFont();
+
+  protected:
+    void populatePlacementMethods();
+    void populateFieldNames();
+    void updateFontPreview(QFont font);
+
+    QgsVectorLayer* layer();
+
+  private:
+    PalLabeling* mLBL;
+    QString mLayerId;
+};
+
+#endif

Added: branches/symbology-ng-branch/src/plugins/labeling/labelingguibase.ui
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/labelingguibase.ui	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/labelingguibase.ui	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,340 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>LabelingGuiBase</class>
+ <widget class="QDialog" name="LabelingGuiBase">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>354</width>
+    <height>435</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Layer labeling settings</string>
+  </property>
+  <property name="windowIcon">
+   <iconset>
+    <normaloff/>
+   </iconset>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QCheckBox" name="chkEnableLabeling">
+     <property name="text">
+      <string>Label this layer</string>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <layout class="QGridLayout" name="gridLayout_2">
+     <item row="0" column="0">
+      <widget class="QLabel" name="label">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+         <horstretch>1</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="text">
+        <string>Placement</string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="1">
+      <widget class="QComboBox" name="cboPlacement">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+         <horstretch>2</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="0">
+      <widget class="QLabel" name="label_6">
+       <property name="text">
+        <string>Field with labels</string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="1">
+      <widget class="QComboBox" name="cboFieldName"/>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <widget class="QGroupBox" name="groupBox">
+     <property name="title">
+      <string>Text style</string>
+     </property>
+     <layout class="QGridLayout" name="gridLayout">
+      <item row="0" column="0">
+       <widget class="QLabel" name="label_4">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="text">
+         <string>Font</string>
+        </property>
+        <property name="alignment">
+         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+        </property>
+       </widget>
+      </item>
+      <item row="0" column="1" colspan="2">
+       <widget class="QLabel" name="lblFontName">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+          <horstretch>1</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="text">
+         <string>TextLabel</string>
+        </property>
+       </widget>
+      </item>
+      <item row="0" column="3">
+       <widget class="QToolButton" name="btnChangeFont">
+        <property name="text">
+         <string>...</string>
+        </property>
+       </widget>
+      </item>
+      <item row="1" column="0">
+       <widget class="QLabel" name="label_3">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="text">
+         <string>Color</string>
+        </property>
+        <property name="alignment">
+         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+        </property>
+       </widget>
+      </item>
+      <item row="1" column="1">
+       <widget class="QgsColorButton" name="btnTextColor">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="text">
+         <string/>
+        </property>
+       </widget>
+      </item>
+      <item row="1" column="2" colspan="2">
+       <spacer name="horizontalSpacer">
+        <property name="orientation">
+         <enum>Qt::Horizontal</enum>
+        </property>
+        <property name="sizeType">
+         <enum>QSizePolicy::Preferred</enum>
+        </property>
+        <property name="sizeHint" stdset="0">
+         <size>
+          <width>142</width>
+          <height>20</height>
+         </size>
+        </property>
+       </spacer>
+      </item>
+      <item row="2" column="0">
+       <widget class="QLabel" name="label_7">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="text">
+         <string>Sample</string>
+        </property>
+        <property name="alignment">
+         <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+        </property>
+       </widget>
+      </item>
+      <item row="2" column="1" colspan="3">
+       <widget class="QLabel" name="lblFontPreview">
+        <property name="text">
+         <string>Lorem Ipsum</string>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </widget>
+   </item>
+   <item>
+    <spacer name="verticalSpacer_2">
+     <property name="orientation">
+      <enum>Qt::Vertical</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>20</width>
+       <height>40</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout">
+     <item>
+      <widget class="QLabel" name="label_2">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
+       <property name="text">
+        <string>Priority</string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QDoubleSpinBox" name="spinPriority">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <spacer name="horizontalSpacer_2">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <widget class="QCheckBox" name="chkNoObstacle">
+     <property name="enabled">
+      <bool>false</bool>
+     </property>
+     <property name="text">
+      <string>not an obstacle</string>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout_2">
+     <item>
+      <widget class="QPushButton" name="btnEngineSettings">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
+       <property name="text">
+        <string>Engine settings</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <spacer name="horizontalSpacer_3">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <spacer name="verticalSpacer">
+     <property name="orientation">
+      <enum>Qt::Vertical</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>20</width>
+       <height>25</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <customwidgets>
+  <customwidget>
+   <class>QgsColorButton</class>
+   <extends>QToolButton</extends>
+   <header>qgscolorbutton.h</header>
+  </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>LabelingGuiBase</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>360</x>
+     <y>415</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>309</x>
+     <y>430</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>LabelingGuiBase</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>526</x>
+     <y>414</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>544</x>
+     <y>430</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>

Added: branches/symbology-ng-branch/src/plugins/labeling/pallabeling.cpp
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/pallabeling.cpp	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/pallabeling.cpp	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,236 @@
+#include "pallabeling.h"
+
+#include <iostream>
+#include <list>
+
+#include <pal/pal.h>
+#include <pal/layer.h>
+#include <pal/palgeometry.h>
+#include <pal/palexception.h>
+
+#include <geos_c.h>
+
+#include <cmath>
+
+#include <QByteArray>
+#include <QString>
+#include <QFontMetrics>
+
+#include <qgsvectorlayer.h>
+#include <qgsmaplayerregistry.h>
+#include <qgsvectordataprovider.h>
+#include <qgsgeometry.h>
+#include <qgsmapcanvas.h>
+
+using namespace pal;
+
+
+class MyLabel : public PalGeometry
+{
+public:
+  MyLabel(int id, QString text, GEOSGeometry* g): mG(g), mText(text), mId(id)
+  {
+    mStrId = QByteArray::number(id);
+  }
+
+  ~MyLabel()
+  {
+    if (mG) GEOSGeom_destroy(mG);
+  }
+
+  // getGeosGeometry + releaseGeosGeometry is called twice: once when adding, second time when labeling
+
+  GEOSGeometry* getGeosGeometry()
+  {
+    return mG;
+  }
+  void releaseGeosGeometry(GEOSGeometry* /*geom*/)
+  {
+    // nothing here - we'll delete the geometry in destructor
+  }
+
+  const char* strId() { return mStrId.data(); }
+  QString text() { return mText; }
+
+protected:
+  GEOSGeometry* mG;
+  QString mText;
+  QByteArray mStrId;
+  int mId;
+};
+
+
+
+// -------------
+
+PalLabeling::PalLabeling(QgsMapCanvas* mapCanvas)
+  : mMapCanvas(mapCanvas)
+{
+}
+
+void PalLabeling::addLayer(LayerSettings layerSettings)
+{
+  mLayers.append(layerSettings);
+}
+
+void PalLabeling::removeLayer(QString layerId)
+{
+  for (int i = 0; i < mLayers.count(); i++)
+  {
+    if (mLayers.at(i).layerId == layerId)
+    {
+      mLayers.removeAt(i);
+      return;
+    }
+  }
+}
+
+PalLabeling::LayerSettings PalLabeling::layer(QString layerId)
+{
+  for (int i = 0; i < mLayers.count(); i++)
+  {
+    if (mLayers.at(i).layerId == layerId)
+    {
+      return mLayers.at(i);
+    }
+  }
+  return LayerSettings();
+}
+
+
+int PalLabeling::prepareLayer(Pal& pal, const LayerSettings& lyr)
+{
+  if (!lyr.enabled)
+    return 0;
+
+  QgsVectorLayer* vlayer = (QgsVectorLayer*) QgsMapLayerRegistry::instance()->mapLayer(lyr.layerId);
+  if (vlayer == NULL)
+    return 0;
+
+  QgsAttributeList attrs;
+
+  int fldName = vlayer->dataProvider()->fieldNameIndex(lyr.fieldName);
+  if (fldName == -1)
+    return 0;
+  attrs << fldName;
+  vlayer->select(attrs, mMapCanvas->extent());
+
+
+  // how to place the labels
+  Arrangement arrangement;
+  switch (lyr.placement)
+  {
+    case AroundPoint: arrangement = P_POINT; break;
+    case OnLine:      arrangement = P_LINE; break;
+    case AroundLine:  arrangement = P_LINE_AROUND; break;
+    case Horizontal:  arrangement = P_HORIZ; break;
+    case Free:        arrangement = P_FREE; break;
+  }
+
+  // create the pal layer
+  Layer* l = pal.addLayer(lyr.layerId.toLocal8Bit().data(), -1, -1, arrangement, METER, 0, true, true, true);
+
+  QFontMetrics fm(lyr.textFont);
+
+  QgsFeature f;
+  int feats = 0;
+  const QgsMapToPixel* xform = mMapCanvas->mapRenderer()->coordinateTransform();
+  QgsPoint ptZero = xform->toMapCoordinates( 0,0 );
+
+  while (vlayer->nextFeature(f))
+  {
+    QString labelText = f.attributeMap()[fldName].toString();
+    QRect labelRect = fm.boundingRect(labelText);
+    //std::cout << "bound: " << labelRect.width() << "x" << labelRect.height() << std::endl;
+    // 2px border...
+    QgsPoint ptSize = xform->toMapCoordinates( labelRect.width()+2,labelRect.height()+2 );
+    double labelX = fabs(ptSize.x()-ptZero.x());
+    double labelY = fabs(ptSize.y()-ptZero.y());
+    //std::cout << "L " << labelX << " " << labelY << std::endl;
+
+    MyLabel* lbl = new MyLabel(f.id(), labelText, GEOSGeom_clone( f.geometry()->asGeos() ) );
+
+    // TODO: owner of the id?
+    l->registerFeature(lbl->strId(), lbl, labelX, labelY);
+    feats++;
+  }
+
+  return feats;
+}
+
+
+void PalLabeling::doLabeling(QPainter* painter)
+{
+  Pal p;
+  //p.setSearch(POPMUSIC_TABU_CHAIN);// this is really slow! // default is CHAIN (worst, fastest)
+  // TODO: API 0.2 - no mention about changing map units!
+  // pal map units = METER by default ... change setMapUnit
+  //p.setMapUnit(METER);
+  // pal label units ... to be chosen
+  // pal dist label - pixels?
+
+  QTime t;
+  t.start();
+
+  int feats = 0;
+  for (int i = 0; i < mLayers.count(); i++)
+  {
+    feats += prepareLayer(p, mLayers.at(i));
+  }
+
+  std::cout << "LABELING prepare: " << t.elapsed() << "ms" << std::endl;
+  t.restart();
+
+  // do the labeling itself
+  double scale = 1; // scale denominator
+  QgsRectangle r = mMapCanvas->extent();
+  double bbox[] = { r.xMinimum(), r.yMinimum(), r.xMaximum(), r.yMaximum() };
+
+  std::list<Label*>* labels;
+  try
+  {
+     labels = p.labeller(scale, bbox, NULL, false);
+  }
+  catch ( std::exception e )
+  {
+    std::cerr << "PAL EXCEPTION :-( " << e.what() << std::endl;
+    return;
+  }
+
+  std::cout << "LABELING work:   " << t.elapsed() << "ms" << std::endl;
+  std::cout << "-->> " << labels->size() << "/" << feats << std::endl;
+  t.restart();
+
+  QFontMetrics fm = painter->fontMetrics();
+  QRect labelRect = fm.boundingRect("X"); // dummy text to find out height
+  int baseline = labelRect.bottom(); // how many pixels of the text are below the baseline
+
+  // draw the labels
+  const QgsMapToPixel* xform = mMapCanvas->mapRenderer()->coordinateTransform();
+  std::list<Label*>::iterator it = labels->begin();
+  for ( ; it != labels->end(); ++it)
+  {
+    Label* label = *it;
+
+    QgsPoint outPt = xform->transform(label->getOrigX(), label->getOrigY());
+
+    // TODO: optimize access :)
+    const LayerSettings& lyr = layer(label->getLayerName());
+
+    // shift by one as we have 2px border
+    painter->save();
+    painter->setPen( lyr.textColor );
+    painter->setFont( lyr.textFont );
+    painter->translate( QPointF(outPt.x()+1, outPt.y()-1-baseline) );
+    painter->rotate(-label->getRotation() * 180 / M_PI );
+    painter->drawText(0,0, ((MyLabel*)label->getGeometry())->text());
+    painter->restore();
+
+    delete label->getGeometry();
+    delete label;
+  }
+
+  std::cout << "LABELING draw:   " << t.elapsed() << "ms" << std::endl;
+
+  delete labels;
+}

Added: branches/symbology-ng-branch/src/plugins/labeling/pallabeling.h
===================================================================
--- branches/symbology-ng-branch/src/plugins/labeling/pallabeling.h	                        (rev 0)
+++ branches/symbology-ng-branch/src/plugins/labeling/pallabeling.h	2009-06-05 20:08:15 UTC (rev 10887)
@@ -0,0 +1,57 @@
+#ifndef PALLABELING_H
+#define PALLABELING_H
+
+class QPainter;
+class QgsMapCanvas;
+
+#include <QString>
+#include <QFont>
+#include <QColor>
+
+namespace pal
+{
+  class Pal;
+}
+
+class PalLabeling
+{
+public:
+    PalLabeling(QgsMapCanvas* mapCanvas);
+
+    enum Placement
+    {
+      AroundPoint, // Point / Polygon
+      OnLine, // Line / Polygon
+      AroundLine, // Line / Polygon
+      Horizontal, // Polygon
+      Free // Polygon
+    };
+
+    struct LayerSettings
+    {
+      //LayerSettings()
+      QString layerId;
+      QString fieldName;
+      Placement placement;
+      QFont textFont;
+      QColor textColor;
+      bool enabled;
+    };
+
+    void doLabeling(QPainter* painter);
+
+    void addLayer(LayerSettings layerSettings);
+
+    void removeLayer(QString layerId);
+
+    LayerSettings layer(QString layerId);
+
+protected:
+    int prepareLayer(pal::Pal& pal, const LayerSettings& lyr);
+
+protected:
+    QList<LayerSettings> mLayers;
+    QgsMapCanvas* mMapCanvas;
+};
+
+#endif // PALLABELING_H



More information about the QGIS-commit mailing list