src/qt3support/sql/q3sqleditorfactory.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt3Support module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "q3sqleditorfactory.h"
       
    43 
       
    44 #ifndef QT_NO_SQL_EDIT_WIDGETS
       
    45 
       
    46 #include "qsqlfield.h"
       
    47 #include "q3cleanuphandler.h"
       
    48 #include "qlabel.h"
       
    49 #include "qlineedit.h"
       
    50 #include "qspinbox.h"
       
    51 #include "qcombobox.h"
       
    52 #include "qdatetimeedit.h"
       
    53 
       
    54 QT_BEGIN_NAMESPACE
       
    55 
       
    56 /*!
       
    57     \class Q3SqlEditorFactory
       
    58     \brief The Q3SqlEditorFactory class is used to create the editors
       
    59     used by Q3DataTable and Q3SqlForm.
       
    60 
       
    61     \compat
       
    62 
       
    63     Q3SqlEditorFactory is used by Q3DataTable and Q3SqlForm to
       
    64     automatically create appropriate editors for a given QSqlField.
       
    65     For example if the field is a QVariant::String a QLineEdit would
       
    66     be the default editor, whereas a QVariant::Int's default editor
       
    67     would be a QSpinBox.
       
    68 
       
    69     If you want to create different editors for fields with the same
       
    70     data type, subclass Q3SqlEditorFactory and reimplement the
       
    71     createEditor() function.
       
    72 
       
    73     \sa Q3DataTable, Q3SqlForm
       
    74 */
       
    75 
       
    76 
       
    77 /*!
       
    78     Constructs a SQL editor factory with parent \a parent.
       
    79 */
       
    80 
       
    81 Q3SqlEditorFactory::Q3SqlEditorFactory (QObject * parent)
       
    82     : Q3EditorFactory(parent)
       
    83 {
       
    84 
       
    85 }
       
    86 
       
    87 /*!
       
    88     Destroys the object and frees any allocated resources.
       
    89 */
       
    90 
       
    91 Q3SqlEditorFactory::~Q3SqlEditorFactory()
       
    92 {
       
    93 
       
    94 }
       
    95 
       
    96 static Q3SqlEditorFactory * defaultfactory = 0;
       
    97 static Q3CleanupHandler< Q3SqlEditorFactory > qsql_cleanup_editor_factory;
       
    98 
       
    99 /*!
       
   100     Returns an instance of a default editor factory.
       
   101 */
       
   102 
       
   103 Q3SqlEditorFactory * Q3SqlEditorFactory::defaultFactory()
       
   104 {
       
   105     if(defaultfactory == 0){
       
   106         defaultfactory = new Q3SqlEditorFactory();
       
   107         qsql_cleanup_editor_factory.add(&defaultfactory);
       
   108     }
       
   109 
       
   110     return defaultfactory;
       
   111 }
       
   112 
       
   113 /*!
       
   114     Replaces the default editor factory with \a factory. All
       
   115     Q3DataTable and Q3SqlForm instantiations will use this new factory
       
   116     for creating field editors. \e{Q3SqlEditorFactory takes ownership
       
   117     of \a factory, and destroys it when it is no longer needed.}
       
   118 */
       
   119 
       
   120 void Q3SqlEditorFactory::installDefaultFactory(Q3SqlEditorFactory * factory)
       
   121 {
       
   122     if(factory == 0) return;
       
   123 
       
   124     if(defaultfactory != 0){
       
   125         qsql_cleanup_editor_factory.remove(&defaultfactory);
       
   126         delete defaultfactory;
       
   127     }
       
   128     defaultfactory = factory;
       
   129     qsql_cleanup_editor_factory.add(&defaultfactory);
       
   130 }
       
   131 
       
   132 /*!
       
   133     Creates and returns the appropriate editor widget for the QVariant
       
   134     \a variant.
       
   135 
       
   136     The widget that is returned has the parent \a parent (which may be
       
   137     zero). If \a variant is invalid, 0 is returned.
       
   138 */
       
   139 
       
   140 QWidget * Q3SqlEditorFactory::createEditor(QWidget * parent,
       
   141                                            const QVariant & variant)
       
   142 {
       
   143     return Q3EditorFactory::createEditor(parent, variant);
       
   144 }
       
   145 
       
   146 /*!
       
   147     \overload
       
   148 
       
   149     Creates and returns the appropriate editor for the QSqlField \a
       
   150     field.
       
   151 */
       
   152 
       
   153 QWidget * Q3SqlEditorFactory::createEditor(QWidget * parent,
       
   154                                            const QSqlField * field)
       
   155 {
       
   156     if (!field) {
       
   157         return 0;
       
   158     }
       
   159 
       
   160     QWidget * w = 0;
       
   161     switch(field->type()){
       
   162         case QVariant::Invalid:
       
   163             w = 0;
       
   164             break;
       
   165         case QVariant::Bool:
       
   166             w = new QComboBox(parent, "qt_editor_bool");
       
   167             ((QComboBox *) w)->insertItem(QLatin1String("False"));
       
   168             ((QComboBox *) w)->insertItem(QLatin1String("True"));
       
   169             break;
       
   170         case QVariant::UInt:
       
   171             w = new QSpinBox(0, 2147483647, 1, parent, "qt_editor_spinbox");
       
   172             break;
       
   173         case QVariant::Int:
       
   174             w = new QSpinBox(-2147483647, 2147483647, 1, parent, "qt_editor_int");
       
   175             break;
       
   176         case QVariant::LongLong:
       
   177         case QVariant::ULongLong:
       
   178         case QVariant::String:
       
   179         case QVariant::Double:
       
   180             w = new QLineEdit(parent, "qt_editor_double");
       
   181             ((QLineEdit*)w)->setFrame(false);
       
   182             break;
       
   183         case QVariant::Date: {
       
   184             QDateTimeEdit *edit = new QDateTimeEdit(parent);
       
   185             edit->setDisplayFormat(QLatin1String("yyyy/MM/dd"));
       
   186             edit->setObjectName(QLatin1String("qt_editor_date"));
       
   187             w = edit; }
       
   188             break;
       
   189         case QVariant::Time: {
       
   190             QDateTimeEdit *edit = new QDateTimeEdit(parent);
       
   191             edit->setDisplayFormat(QLatin1String("hh:mm"));
       
   192             edit->setObjectName(QLatin1String("qt_editor_time"));
       
   193             w = edit; }
       
   194             break;
       
   195         case QVariant::DateTime:
       
   196             w = new QDateTimeEdit(parent);
       
   197             w->setObjectName(QLatin1String("qt_editor_datetime"));
       
   198             break;
       
   199 #ifndef QT_NO_LABEL
       
   200         case QVariant::Pixmap:
       
   201             w = new QLabel(parent, "qt_editor_pixmap");
       
   202             break;
       
   203 #endif
       
   204         case QVariant::Palette:
       
   205         case QVariant::Color:
       
   206         case QVariant::Font:
       
   207         case QVariant::Brush:
       
   208         case QVariant::Bitmap:
       
   209         case QVariant::Cursor:
       
   210         case QVariant::Map:
       
   211         case QVariant::StringList:
       
   212         case QVariant::Rect:
       
   213         case QVariant::Size:
       
   214         case QVariant::IconSet:
       
   215         case QVariant::Point:
       
   216         case QVariant::PointArray:
       
   217         case QVariant::Region:
       
   218         case QVariant::SizePolicy:
       
   219         case QVariant::ByteArray:
       
   220         default:
       
   221             w = new QWidget(parent, "qt_editor_default");
       
   222             break;
       
   223     }
       
   224     return w;
       
   225 }
       
   226 
       
   227 QT_END_NAMESPACE
       
   228 
       
   229 #endif // QT_NO_SQL