src/tools/uic3/subclassing.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 tools applications 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 "ui3reader.h"
       
    43 #include "parser.h"
       
    44 #include "domtool.h"
       
    45 #include "globaldefs.h"
       
    46 #include <QFile>
       
    47 #include <QStringList>
       
    48 #include <QDateTime>
       
    49 #include <QRegExp>
       
    50 #include <stdio.h>
       
    51 #include <stdlib.h>
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 /*!
       
    56   Creates a declaration ( headerfile ) for a subclass \a subClass
       
    57   of the form given in \a e
       
    58 
       
    59   \sa createSubImpl()
       
    60  */
       
    61 void Ui3Reader::createSubDecl( const QDomElement &e, const QString& subClass )
       
    62 {
       
    63     QDomElement n;
       
    64     QDomNodeList nl;
       
    65     int i;
       
    66 
       
    67     QString objClass = getClassName( e );
       
    68     if ( objClass.isEmpty() )
       
    69         return;
       
    70 
       
    71     out << "class " << subClass << " : public " << nameOfClass << endl;
       
    72     out << '{' << endl;
       
    73 
       
    74 /* tmake ignore Q_OBJECT */
       
    75     out << "    Q_OBJECT" << endl;
       
    76     out << endl;
       
    77     out << "public:" << endl;
       
    78 
       
    79     // constructor
       
    80     if ( objClass == QLatin1String("QDialog") || objClass == QLatin1String("QWizard") ) {
       
    81         out << "    " << subClass << "( QWidget* parent = 0, const char* name = 0, bool modal = false, Qt::WindowFlags fl = 0 );" << endl;
       
    82     } else { // standard QWidget
       
    83         out << "    " << subClass << "( QWidget* parent = 0, const char* name = 0, Qt::WindowFlags fl = 0 );" << endl;
       
    84     }
       
    85 
       
    86     // destructor
       
    87     out << "    ~" << subClass << "();" << endl;
       
    88     out << endl;
       
    89 
       
    90     // find additional functions
       
    91     QStringList publicSlots, protectedSlots, privateSlots;
       
    92     QStringList publicSlotTypes, protectedSlotTypes, privateSlotTypes;
       
    93     QStringList publicSlotSpecifier, protectedSlotSpecifier, privateSlotSpecifier;
       
    94     QStringList publicFuncts, protectedFuncts, privateFuncts;
       
    95     QStringList publicFunctRetTyp, protectedFunctRetTyp, privateFunctRetTyp;
       
    96     QStringList publicFunctSpec, protectedFunctSpec, privateFunctSpec;
       
    97 
       
    98     nl = e.parentNode().toElement().elementsByTagName(QLatin1String("slot"));
       
    99     for ( i = 0; i < (int) nl.length(); i++ ) {
       
   100         n = nl.item(i).toElement();
       
   101         if ( n.parentNode().toElement().tagName() != QLatin1String("slots")
       
   102              && n.parentNode().toElement().tagName() != QLatin1String("connections") )
       
   103             continue;
       
   104         if ( n.attribute(QLatin1String("language"), QLatin1String("C++")) != QLatin1String("C++") )
       
   105             continue;
       
   106         QString returnType = n.attribute(QLatin1String("returnType"), QLatin1String("void"));
       
   107         QString functionName = n.firstChild().toText().data().trimmed();
       
   108         if ( functionName.endsWith(QLatin1Char(';')))
       
   109             functionName.chop(1);
       
   110         QString specifier = n.attribute(QLatin1String("specifier"));
       
   111         QString access = n.attribute(QLatin1String("access"));
       
   112         if ( access == QLatin1String("protected") ) {
       
   113             protectedSlots += functionName;
       
   114             protectedSlotTypes += returnType;
       
   115             protectedSlotSpecifier += specifier;
       
   116         } else if ( access == QLatin1String("private") ) {
       
   117             privateSlots += functionName;
       
   118             privateSlotTypes += returnType;
       
   119             privateSlotSpecifier += specifier;
       
   120         } else {
       
   121             publicSlots += functionName;
       
   122             publicSlotTypes += returnType;
       
   123             publicSlotSpecifier += specifier;
       
   124         }
       
   125     }
       
   126 
       
   127     nl = e.parentNode().toElement().elementsByTagName(QLatin1String("function"));
       
   128     for ( i = 0; i < (int) nl.length(); i++ ) {
       
   129         n = nl.item(i).toElement();
       
   130         if ( n.parentNode().toElement().tagName() != QLatin1String("functions") )
       
   131             continue;
       
   132         if ( n.attribute(QLatin1String("language"), QLatin1String("C++")) != QLatin1String("C++") )
       
   133             continue;
       
   134         QString returnType = n.attribute(QLatin1String("returnType"), QLatin1String("void"));
       
   135         QString functionName = n.firstChild().toText().data().trimmed();
       
   136         if ( functionName.endsWith(QLatin1Char(';')) )
       
   137             functionName.chop(1);
       
   138         QString specifier = n.attribute(QLatin1String("specifier"));
       
   139         QString access = n.attribute(QLatin1String("access"));
       
   140         if ( access == QLatin1String("protected") ) {
       
   141             protectedFuncts += functionName;
       
   142             protectedFunctRetTyp += returnType;
       
   143             protectedFunctSpec += specifier;
       
   144         } else if ( access == QLatin1String("private") ) {
       
   145             privateFuncts += functionName;
       
   146             privateFunctRetTyp += returnType;
       
   147             privateFunctSpec += specifier;
       
   148         } else {
       
   149             publicFuncts += functionName;
       
   150             publicFunctRetTyp += returnType;
       
   151             publicFunctSpec += specifier;
       
   152         }
       
   153     }
       
   154 
       
   155     if ( !publicFuncts.isEmpty() )
       
   156         writeFunctionsSubDecl( publicFuncts, publicFunctRetTyp, publicFunctSpec );
       
   157 
       
   158     // create public additional slots
       
   159     if ( !publicSlots.isEmpty() ) {
       
   160         out << "public slots:" << endl;
       
   161         writeFunctionsSubDecl( publicSlots, publicSlotTypes, publicSlotSpecifier );
       
   162     }
       
   163 
       
   164     if ( !protectedFuncts.isEmpty() ) {
       
   165         out << "protected:" << endl;
       
   166         writeFunctionsSubDecl( protectedFuncts, protectedFunctRetTyp, protectedFunctSpec );
       
   167     }
       
   168 
       
   169     // create protected additional slots
       
   170     if ( !protectedSlots.isEmpty() ) {
       
   171         out << "protected slots:" << endl;
       
   172         writeFunctionsSubDecl( protectedSlots, protectedSlotTypes, protectedSlotSpecifier );
       
   173     }
       
   174 
       
   175     if ( !privateFuncts.isEmpty() ) {
       
   176         out << "private:" << endl;
       
   177         writeFunctionsSubDecl( privateFuncts, privateFunctRetTyp, privateFunctSpec );
       
   178     }
       
   179 
       
   180     // create private additional slots
       
   181     if ( !privateSlots.isEmpty() ) {
       
   182         out << "private slots:" << endl;
       
   183         writeFunctionsSubDecl( privateSlots, privateSlotTypes, privateSlotSpecifier );
       
   184     }
       
   185     out << "};" << endl;
       
   186 }
       
   187 
       
   188 void Ui3Reader::writeFunctionsSubDecl( const QStringList &fuLst, const QStringList &typLst, const QStringList &specLst )
       
   189 {
       
   190     QStringList::ConstIterator it, it2, it3;
       
   191     for ( it = fuLst.begin(), it2 = typLst.begin(), it3 = specLst.begin();
       
   192           it != fuLst.end(); ++it, ++it2, ++it3 ) {
       
   193         QString type = fixDeclaration(*it2);
       
   194         if ( type.isEmpty() )
       
   195             type = QLatin1String("void");
       
   196         if ( *it3 == QLatin1String("non virtual") )
       
   197             continue;
       
   198         out << "    " << type << ' ' << fixDeclaration(*it) << ';' << endl;
       
   199     }
       
   200     out << endl;
       
   201 }
       
   202 
       
   203 /*!
       
   204   Creates an implementation for a subclass \a subClass of the form
       
   205   given in \a e
       
   206 
       
   207   \sa createSubDecl()
       
   208  */
       
   209 void Ui3Reader::createSubImpl( const QDomElement &e, const QString& subClass )
       
   210 {
       
   211     QDomElement n;
       
   212     QDomNodeList nl;
       
   213     int i;
       
   214 
       
   215     QString objClass = getClassName( e );
       
   216     if ( objClass.isEmpty() )
       
   217         return;
       
   218 
       
   219     // constructor
       
   220     if ( objClass == QLatin1String("QDialog") || objClass == QLatin1String("QWizard") ) {
       
   221         out << "/* " << endl;
       
   222         out << " *  Constructs a " << subClass << " which is a child of 'parent', with the " << endl;
       
   223         out << " *  name 'name' and widget flags set to 'f' " << endl;
       
   224         out << " *" << endl;
       
   225         out << " *  The " << objClass.mid(1).toLower() << " will by default be modeless, unless you set 'modal' to" << endl;
       
   226         out << " *  true to construct a modal " << objClass.mid(1).toLower() << '.' << endl;
       
   227         out << " */" << endl;
       
   228         out << subClass << "::" << subClass << "( QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl )" << endl;
       
   229         out << "    : " << nameOfClass << "( parent, name, modal, fl )" << endl;
       
   230     } else { // standard QWidget
       
   231         out << "/* " << endl;
       
   232         out << " *  Constructs a " << subClass << " which is a child of 'parent', with the " << endl;
       
   233         out << " *  name 'name' and widget flags set to 'f' " << endl;
       
   234         out << " */" << endl;
       
   235         out << subClass << "::" << subClass << "( QWidget* parent, const char* name, Qt::WindowFlags fl )" << endl;
       
   236         out << "    : " << nameOfClass << "( parent, name, fl )" << endl;
       
   237     }
       
   238     out << '{' << endl;
       
   239     out << '}' << endl;
       
   240     out << endl;
       
   241 
       
   242     // destructor
       
   243     out << "/*  " << endl;
       
   244     out << " *  Destroys the object and frees any allocated resources" << endl;
       
   245     out << " */" << endl;
       
   246     out << subClass << "::~" << subClass << "()" << endl;
       
   247     out << '{' << endl;
       
   248     out << "    // no need to delete child widgets, Qt does it all for us" << endl;
       
   249     out << '}' << endl;
       
   250     out << endl;
       
   251 
       
   252 
       
   253     // find additional functions
       
   254     QStringList publicSlots, protectedSlots, privateSlots;
       
   255     QStringList publicSlotTypes, protectedSlotTypes, privateSlotTypes;
       
   256     QStringList publicSlotSpecifier, protectedSlotSpecifier, privateSlotSpecifier;
       
   257     QStringList publicFuncts, protectedFuncts, privateFuncts;
       
   258     QStringList publicFunctRetTyp, protectedFunctRetTyp, privateFunctRetTyp;
       
   259     QStringList publicFunctSpec, protectedFunctSpec, privateFunctSpec;
       
   260 
       
   261     nl = e.parentNode().toElement().elementsByTagName(QLatin1String("slot"));
       
   262     for ( i = 0; i < (int) nl.length(); i++ ) {
       
   263         n = nl.item(i).toElement();
       
   264         if ( n.parentNode().toElement().tagName() != QLatin1String("slots")
       
   265              && n.parentNode().toElement().tagName() != QLatin1String("connections") )
       
   266             continue;
       
   267         if ( n.attribute(QLatin1String("language"), QLatin1String("C++")) != QLatin1String("C++") )
       
   268             continue;
       
   269         QString returnType = n.attribute(QLatin1String("returnType"), QLatin1String("void"));
       
   270         QString functionName = n.firstChild().toText().data().trimmed();
       
   271         if ( functionName.endsWith(QLatin1Char(';')) )
       
   272             functionName.chop(1);
       
   273         QString specifier = n.attribute(QLatin1String("specifier"));
       
   274         QString access = n.attribute(QLatin1String("access"));
       
   275         if ( access == QLatin1String("protected") ) {
       
   276             protectedSlots += functionName;
       
   277             protectedSlotTypes += returnType;
       
   278             protectedSlotSpecifier += specifier;
       
   279         } else if ( access == QLatin1String("private") ) {
       
   280             privateSlots += functionName;
       
   281             privateSlotTypes += returnType;
       
   282             privateSlotSpecifier += specifier;
       
   283         } else {
       
   284             publicSlots += functionName;
       
   285             publicSlotTypes += returnType;
       
   286             publicSlotSpecifier += specifier;
       
   287         }
       
   288     }
       
   289 
       
   290     nl = e.parentNode().toElement().elementsByTagName(QLatin1String("function"));
       
   291     for ( i = 0; i < (int) nl.length(); i++ ) {
       
   292         n = nl.item(i).toElement();
       
   293         if ( n.parentNode().toElement().tagName() != QLatin1String("functions") )
       
   294             continue;
       
   295         if ( n.attribute(QLatin1String("language"), QLatin1String("C++")) != QLatin1String("C++") )
       
   296             continue;
       
   297         QString returnType = n.attribute(QLatin1String("returnType"), QLatin1String("void"));
       
   298         QString functionName = n.firstChild().toText().data().trimmed();
       
   299         if ( functionName.endsWith(QLatin1Char(';')) )
       
   300             functionName.chop(1);
       
   301         QString specifier = n.attribute(QLatin1String("specifier"));
       
   302         QString access = n.attribute(QLatin1String("access"));
       
   303         if ( access == QLatin1String("protected") ) {
       
   304             protectedFuncts += functionName;
       
   305             protectedFunctRetTyp += returnType;
       
   306             protectedFunctSpec += specifier;
       
   307         } else if ( access == QLatin1String("private") ) {
       
   308             privateFuncts += functionName;
       
   309             privateFunctRetTyp += returnType;
       
   310             privateFunctSpec += specifier;
       
   311         } else {
       
   312             publicFuncts += functionName;
       
   313             publicFunctRetTyp += returnType;
       
   314             publicFunctSpec += specifier;
       
   315         }
       
   316     }
       
   317 
       
   318     if ( !publicFuncts.isEmpty() )
       
   319         writeFunctionsSubImpl( publicFuncts, publicFunctRetTyp, publicFunctSpec, subClass, QLatin1String("public function"));
       
   320 
       
   321     // create stubs for public additional slots
       
   322     if ( !publicSlots.isEmpty() )
       
   323         writeFunctionsSubImpl( publicSlots, publicSlotTypes, publicSlotSpecifier, subClass, QLatin1String("public slot"));
       
   324 
       
   325     if ( !protectedFuncts.isEmpty() )
       
   326         writeFunctionsSubImpl( protectedFuncts, protectedFunctRetTyp, protectedFunctSpec, subClass, QLatin1String("protected function"));
       
   327 
       
   328     // create stubs for protected additional slots
       
   329     if ( !protectedSlots.isEmpty() )
       
   330         writeFunctionsSubImpl( protectedSlots, protectedSlotTypes, protectedSlotSpecifier, subClass, QLatin1String("protected slot"));
       
   331 
       
   332     if ( !privateFuncts.isEmpty() )
       
   333         writeFunctionsSubImpl( privateFuncts, privateFunctRetTyp, privateFunctSpec, subClass, QLatin1String("private function"));
       
   334 
       
   335     // create stubs for private additional slots
       
   336     if ( !privateSlots.isEmpty() )
       
   337         writeFunctionsSubImpl( privateSlots, privateSlotTypes, privateSlotSpecifier, subClass, QLatin1String("private slot"));
       
   338 }
       
   339 
       
   340 void Ui3Reader::writeFunctionsSubImpl( const QStringList &fuLst, const QStringList &typLst, const QStringList &specLst,
       
   341                                  const QString &subClass, const QString &descr )
       
   342 {
       
   343     QStringList::ConstIterator it, it2, it3;
       
   344     for ( it = fuLst.begin(), it2 = typLst.begin(), it3 = specLst.begin();
       
   345           it != fuLst.end(); ++it, ++it2, ++it3 ) {
       
   346         QString type = fixDeclaration(*it2);
       
   347         if ( type.isEmpty() )
       
   348             type = QLatin1String("void");
       
   349         if ( *it3 == QLatin1String("non virtual") )
       
   350             continue;
       
   351         out << "/*" << endl;
       
   352         out << " * " << descr << endl;
       
   353         out << " */" << endl;
       
   354         out << type << ' ' << subClass << "::" << fixDeclaration(*it) << endl;
       
   355         out << '{' << endl;
       
   356         out << "    qWarning( \"" << subClass << "::" << fixDeclaration(*it) << " not yet implemented!\" );" << endl;
       
   357         out << '}' << endl << endl;
       
   358     }
       
   359     out << endl;
       
   360 }
       
   361 
       
   362 QT_END_NAMESPACE