[QGIS Commit] r15566 - in trunk/qgis: python/gui src/gui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue Mar 22 19:18:06 EDT 2011


Author: jef
Date: 2011-03-22 16:18:06 -0700 (Tue, 22 Mar 2011)
New Revision: 15566

Added:
   trunk/qgis/python/gui/qgsattributeeditor.sip
   trunk/qgis/python/gui/qgsfieldvalidator.sip
   trunk/qgis/src/gui/qgsfieldvalidator.cpp
   trunk/qgis/src/gui/qgsfieldvalidator.h
Modified:
   trunk/qgis/python/gui/gui.sip
   trunk/qgis/src/gui/CMakeLists.txt
   trunk/qgis/src/gui/qgsattributeeditor.cpp
Log:
fix #2554:
- apply heavy modified patch from sunil.
- introduces QgsFieldValidator including sip bindings
- add missing sip bindings to QgsAttributeEditor


Modified: trunk/qgis/python/gui/gui.sip
===================================================================
--- trunk/qgis/python/gui/gui.sip	2011-03-22 20:35:44 UTC (rev 15565)
+++ trunk/qgis/python/gui/gui.sip	2011-03-22 23:18:06 UTC (rev 15566)
@@ -33,5 +33,7 @@
 %Include qgstextannotationitem.sip
 %Include qgsvertexmarker.sip
 %Include qgssearchquerybuilder.sip
+%Include qgsattributeeditor.sip
+%Include qgsfieldvalidator.sip
 
 %Include symbology-ng-gui.sip

Added: trunk/qgis/python/gui/qgsattributeeditor.sip
===================================================================
--- trunk/qgis/python/gui/qgsattributeeditor.sip	                        (rev 0)
+++ trunk/qgis/python/gui/qgsattributeeditor.sip	2011-03-22 23:18:06 UTC (rev 15566)
@@ -0,0 +1,16 @@
+/* \brief create attribute widget for editing */
+class QgsAttributeEditor : public QObject
+{
+%TypeHeaderCode
+#incude <qgsattributeeditor.h>
+%End
+  public:
+    QgsAttributeEditor( QObject *parent );
+    static QWidget *createAttributeEditor( QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value );
+    static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value );
+    static bool setValue( QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value );
+
+  public slots:
+    void selectFileName( void );
+    void selectDate( void );
+};

Added: trunk/qgis/python/gui/qgsfieldvalidator.sip
===================================================================
--- trunk/qgis/python/gui/qgsfieldvalidator.sip	                        (rev 0)
+++ trunk/qgis/python/gui/qgsfieldvalidator.sip	2011-03-22 23:18:06 UTC (rev 15566)
@@ -0,0 +1,11 @@
+class QgsFieldValidator : public QValidator
+{
+%TypeHeaderCode
+#include <qgsfieldvalidator.h>
+%End
+    QgsFieldValidator( QObject *parent, const QgsField &field );
+    ~QgsFieldValidator();
+
+    virtual State validate( QString &, int & ) const;
+    virtual void fixup( QString & ) const;
+};

Modified: trunk/qgis/src/gui/CMakeLists.txt
===================================================================
--- trunk/qgis/src/gui/CMakeLists.txt	2011-03-22 20:35:44 UTC (rev 15565)
+++ trunk/qgis/src/gui/CMakeLists.txt	2011-03-22 23:18:06 UTC (rev 15566)
@@ -35,6 +35,7 @@
 qgsdetaileditemdata.cpp
 qgsencodingfiledialog.cpp
 qgsfiledropedit.cpp
+qgsfieldvalidator.cpp
 qgsformannotationitem.cpp
 qgsgenericprojectionselector.cpp
 qgsmapcanvas.cpp
@@ -89,6 +90,7 @@
 qgslegendinterface.h
 qgisinterface.h
 qgsencodingfiledialog.h
+qgsfieldvalidator.h
 qgsformannotationitem.h
 qgsgenericprojectionselector.h
 qgsmapcanvas.h
@@ -206,6 +208,8 @@
 qgsvertexmarker.h
 qgsmaptip.h
 qgssearchquerybuilder.h
+qgsattributeeditor.h
+qgsfieldvalidator.h
 
 ${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsdetaileditemwidgetbase.h
 ${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsgenericprojectionselectorbase.h

Modified: trunk/qgis/src/gui/qgsattributeeditor.cpp
===================================================================
--- trunk/qgis/src/gui/qgsattributeeditor.cpp	2011-03-22 20:35:44 UTC (rev 15565)
+++ trunk/qgis/src/gui/qgsattributeeditor.cpp	2011-03-22 23:18:06 UTC (rev 15566)
@@ -23,6 +23,7 @@
 #include <qgscategorizedsymbolrendererv2.h>
 #include <qgssymbol.h>
 #include <qgslonglongvalidator.h>
+#include <qgsfieldvalidator.h>
 
 #include <QPushButton>
 #include <QLineEdit>
@@ -357,19 +358,7 @@
           le->setCompleter( c );
         }
 
-        if ( myFieldType == QVariant::Int )
-        {
-          le->setValidator( new QIntValidator( le ) );
-        }
-        else if ( myFieldType == QVariant::LongLong )
-        {
-          le->setValidator( new QgsLongLongValidator( le ) );
-        }
-        else if ( myFieldType == QVariant::Double )
-        {
-          le->setValidator( new QDoubleValidator( le ) );
-        }
-
+        le->setValidator( new QgsFieldValidator( le, field ) );
         myWidget = le;
       }
 

Added: trunk/qgis/src/gui/qgsfieldvalidator.cpp
===================================================================
--- trunk/qgis/src/gui/qgsfieldvalidator.cpp	                        (rev 0)
+++ trunk/qgis/src/gui/qgsfieldvalidator.cpp	2011-03-22 23:18:06 UTC (rev 15566)
@@ -0,0 +1,154 @@
+/***************************************************************************
+                         qgsfieldvalidator.cpp  -  description
+                             -------------------
+    begin                : March 2011
+    copyright            : (C) 2011 by SunilRajKiran-kCube
+    email                : sunilraj.kiran at kcubeconsulting.com
+
+  adapted version of QValidator for QgsField
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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$ */
+
+#include "qgsfieldvalidator.h"
+
+#include <QValidator>
+#include <QRegExpValidator>
+#include <QDate>
+#include <QVariant>
+#include <QSettings>
+
+#include "qgslogger.h"
+#include "qgslonglongvalidator.h"
+#include "qgsfield.h"
+
+QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field )
+    : QValidator( parent )
+    , mField( field )
+{
+  switch ( mField.type() )
+  {
+    case QVariant::Int:
+    {
+      if ( mField.length() > 0 )
+      {
+        QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() );
+        mValidator = new QRegExpValidator( QRegExp( re ), parent );
+      }
+      else
+      {
+        mValidator = new QIntValidator( parent );
+      }
+    }
+    break;
+
+    case QVariant::Double:
+    {
+      if ( mField.length() > 0 && mField.precision() > 0 )
+      {
+        QString re = QString( "-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.length() - mField.precision() ).arg( mField.precision() );
+        mValidator = new QRegExpValidator( QRegExp( re ), parent );
+      }
+      else if ( mField.precision() > 0 )
+      {
+        QString re = QString( "-?\\d*(\\.\\d{0,%1))?" ).arg( mField.precision() );
+        mValidator = new QRegExpValidator( QRegExp( re ), parent );
+      }
+      else
+      {
+        mValidator = new QDoubleValidator( parent );
+      }
+    }
+    break;
+
+    case QVariant::LongLong :
+      mValidator = new QgsLongLongValidator( parent );
+      break;
+
+    default:
+      mValidator = 0;
+  }
+
+  QSettings settings;
+  mNullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
+}
+
+QgsFieldValidator::~QgsFieldValidator()
+{
+  delete mValidator;
+}
+
+QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
+{
+  // empty values are considered NULL for numbers and dates and are acceptable
+  if ( s.isEmpty() &&
+       ( mField.type() == QVariant::Double
+         || mField.type() == QVariant::Int
+         || mField.type() == QVariant::LongLong
+         || mField.type() == QVariant::Date
+       )
+     )
+  {
+    return Acceptable;
+  }
+
+  // delegate to the child validator if any
+  if ( mValidator )
+  {
+    QValidator::State result = mValidator->validate( s, i );
+    return result;
+  }
+  else if ( mField.type() == QVariant::String )
+  {
+    // allow to enter the NULL representation, which might be
+    // longer than the actual field
+    if ( mNullValue.size() > 0 &&
+         s.size() > 0 &&
+         s.size() < mNullValue.size() &&
+         s == mNullValue.left( s.size() ) )
+      return Intermediate;
+
+    if ( s == mNullValue )
+      return Acceptable;
+
+    if ( mField.length() > 0 && s.size() > mField.length() )
+      return Invalid;
+  }
+  else if ( mField.type() == QVariant::Date )
+  {
+    return QDate::fromString( s ).isValid() ? Acceptable : Intermediate;
+  }
+  else
+  {
+    QgsDebugMsg( "unsupported type for validation" );
+    return Invalid;
+  }
+
+  return Acceptable;
+}
+
+void QgsFieldValidator::fixup( QString &s ) const
+{
+  if ( mValidator )
+  {
+    mValidator->fixup( s );
+  }
+  else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() )
+  {
+    // if the value is longer, this must be a partial NULL representation
+    s = mNullValue;
+  }
+  else if ( mField.type() == QVariant::Date )
+  {
+    // invalid dates will also translate to NULL
+    s = "";
+  }
+}

Added: trunk/qgis/src/gui/qgsfieldvalidator.h
===================================================================
--- trunk/qgis/src/gui/qgsfieldvalidator.h	                        (rev 0)
+++ trunk/qgis/src/gui/qgsfieldvalidator.h	2011-03-22 23:18:06 UTC (rev 15566)
@@ -0,0 +1,50 @@
+/***************************************************************************
+                         qgsfieldvalidator.h  -  description
+                             -------------------
+    begin                : March 2011
+    copyright            : (C) 2011 by SunilRajKiran-kCube
+    email                : sunilraj.kiran at kcubeconsulting.com
+
+  adapted version of QValidator for QgsField
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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$ */
+
+#ifndef QGSFIELDVALIDATOR_H
+#define QGSFIELDVALIDATOR_H
+
+#include <QValidator>
+#include <QVariant>
+#include <QSettings>
+#include "qgsfield.h"
+
+
+class GUI_EXPORT QgsFieldValidator : public QValidator
+{
+    Q_OBJECT
+
+  public:
+    QgsFieldValidator( QObject *parent, const QgsField &field );
+    ~QgsFieldValidator();
+
+    virtual State validate( QString &, int & ) const;
+    virtual void fixup( QString & ) const;
+
+  private:
+    // Disables copy constructing
+    Q_DISABLE_COPY( QgsFieldValidator )
+
+    QValidator *mValidator;
+    QgsField mField;
+    QString mNullValue;
+};
+
+#endif // QGSFIELDVALIDATOR_H



More information about the QGIS-commit mailing list