src/qt3support/sql/q3editorfactory.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 "q3cleanuphandler.h"
       
    43 #include "qlabel.h"
       
    44 #include "qlineedit.h"
       
    45 #include "qspinbox.h"
       
    46 #include "qcombobox.h"
       
    47 
       
    48 #include "q3editorfactory.h"
       
    49 #include "qdatetimeedit.h"
       
    50 
       
    51 #ifndef QT_NO_SQL_EDIT_WIDGETS
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 /*!
       
    56     \class Q3EditorFactory
       
    57     \brief The Q3EditorFactory class is used to create editor widgets
       
    58     for QVariant data types.
       
    59 
       
    60     \compat
       
    61 
       
    62     Each editor factory provides the createEditor() function which
       
    63     given a QVariant will create and return a QWidget that can edit
       
    64     that QVariant. For example if you have a QVariant::String type, a
       
    65     QLineEdit would be the default editor returned, whereas a
       
    66     QVariant::Int's default editor would be a QSpinBox.
       
    67 
       
    68     If you want to create different editors for fields with the same
       
    69     data type, subclass Q3EditorFactory and reimplement the
       
    70     createEditor() function.
       
    71 */
       
    72 
       
    73 /*!
       
    74     Constructs an editor factory with parent \a parent.
       
    75 */
       
    76 
       
    77 Q3EditorFactory::Q3EditorFactory (QObject * parent)
       
    78     : QObject(parent)
       
    79 {
       
    80 
       
    81 }
       
    82 
       
    83 /*!
       
    84     Destroys the object and frees any allocated resources.
       
    85 */
       
    86 
       
    87 Q3EditorFactory::~Q3EditorFactory()
       
    88 {
       
    89 
       
    90 }
       
    91 
       
    92 static Q3EditorFactory * defaultfactory = 0;
       
    93 static Q3CleanupHandler< Q3EditorFactory > q_cleanup_editor_factory;
       
    94 
       
    95 /*!
       
    96     Returns an instance of a default editor factory.
       
    97 */
       
    98 
       
    99 Q3EditorFactory * Q3EditorFactory::defaultFactory()
       
   100 {
       
   101     if(defaultfactory == 0){
       
   102         defaultfactory = new Q3EditorFactory();
       
   103         q_cleanup_editor_factory.add(&defaultfactory);
       
   104     }
       
   105 
       
   106     return defaultfactory;
       
   107 }
       
   108 
       
   109 /*!
       
   110     Replaces the default editor factory with \a factory.
       
   111     \e{Q3EditorFactory takes ownership of factory, and destroys it
       
   112     when it is no longer needed.}
       
   113 */
       
   114 
       
   115 void Q3EditorFactory::installDefaultFactory(Q3EditorFactory * factory)
       
   116 {
       
   117     if(factory == 0 || factory == defaultfactory) return;
       
   118 
       
   119     if(defaultfactory != 0){
       
   120         q_cleanup_editor_factory.remove(&defaultfactory);
       
   121         delete defaultfactory;
       
   122     }
       
   123     defaultfactory = factory;
       
   124     q_cleanup_editor_factory.add(&defaultfactory);
       
   125 }
       
   126 
       
   127 /*!
       
   128     Creates and returns the appropriate editor for the QVariant \a v.
       
   129     If the QVariant is invalid, 0 is returned. The \a parent is passed
       
   130     to the appropriate editor's constructor.
       
   131 */
       
   132 
       
   133 QWidget * Q3EditorFactory::createEditor(QWidget * parent, const QVariant & v)
       
   134 {
       
   135     QWidget * w = 0;
       
   136     switch(v.type()){
       
   137         case QVariant::Invalid:
       
   138             w = 0;
       
   139             break;
       
   140         case QVariant::Bool:
       
   141             w = new QComboBox(parent, "qt_editor_bool");
       
   142             ((QComboBox *) w)->insertItem(QLatin1String("False"));
       
   143             ((QComboBox *) w)->insertItem(QLatin1String("True"));
       
   144             break;
       
   145         case QVariant::UInt:
       
   146             w = new QSpinBox(0, 999999, 1, parent, "qt_editor_spinbox");
       
   147             break;
       
   148         case QVariant::Int:
       
   149             w = new QSpinBox(-999999, 999999, 1, parent, "qt_editor_int");
       
   150             break;
       
   151         case QVariant::String:
       
   152         case QVariant::Double:
       
   153             w = new QLineEdit(parent, "qt_editor_double");
       
   154             ((QLineEdit*)w)->setFrame(false);
       
   155             break;
       
   156         case QVariant::Date: {
       
   157             QDateTimeEdit *edit = new QDateTimeEdit(parent);
       
   158             edit->setDisplayFormat(QLatin1String("yyyy/MM/dd"));
       
   159             edit->setObjectName(QLatin1String("qt_editor_date"));
       
   160             w = edit; }
       
   161             break;
       
   162         case QVariant::Time: {
       
   163             QDateTimeEdit *edit = new QDateTimeEdit(parent);
       
   164             edit->setDisplayFormat(QLatin1String("hh:mm"));
       
   165             edit->setObjectName(QLatin1String("qt_editor_time"));
       
   166             w = edit; }
       
   167             break;
       
   168         case QVariant::DateTime:
       
   169             w = new QDateTimeEdit(parent);
       
   170             w->setObjectName(QLatin1String("qt_editor_datetime"));
       
   171             break;
       
   172 #ifndef QT_NO_LABEL
       
   173         case QVariant::Pixmap:
       
   174             w = new QLabel(parent, QLatin1String("qt_editor_pixmap"));
       
   175             break;
       
   176 #endif
       
   177         case QVariant::Palette:
       
   178         case QVariant::Color:
       
   179         case QVariant::Font:
       
   180         case QVariant::Brush:
       
   181         case QVariant::Bitmap:
       
   182         case QVariant::Cursor:
       
   183         case QVariant::Map:
       
   184         case QVariant::StringList:
       
   185         case QVariant::Rect:
       
   186         case QVariant::Size:
       
   187         case QVariant::IconSet:
       
   188         case QVariant::Point:
       
   189         case QVariant::PointArray:
       
   190         case QVariant::Region:
       
   191         case QVariant::SizePolicy:
       
   192         case QVariant::ByteArray:
       
   193         default:
       
   194             w = new QWidget(parent, "qt_editor_default");
       
   195             break;
       
   196     }
       
   197     return w;
       
   198 }
       
   199 
       
   200 QT_END_NAMESPACE
       
   201 
       
   202 #endif // QT_NO_SQL