tests/arthur/common/paintcommands.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 test suite 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 #ifndef PAINTCOMMANDS_H
       
    42 #define PAINTCOMMANDS_H
       
    43 
       
    44 #include <qcolor.h>
       
    45 #include <qmap.h>
       
    46 #include <qpainterpath.h>
       
    47 #include <qregion.h>
       
    48 #include <qstringlist.h>
       
    49 #include <qpixmap.h>
       
    50 #include <qbrush.h>
       
    51 
       
    52 QT_FORWARD_DECLARE_CLASS(QPainter)
       
    53 QT_FORWARD_DECLARE_CLASS(QRegExp)
       
    54 #ifndef QT_NO_OPENGL
       
    55 QT_FORWARD_DECLARE_CLASS(QGLPixelBuffer)
       
    56 #endif
       
    57 
       
    58 enum DeviceType {
       
    59         WidgetType,
       
    60         BitmapType,
       
    61         PixmapType,
       
    62         ImageType,
       
    63         ImageMonoType,
       
    64         OpenGLType,
       
    65         OpenGLPBufferType,
       
    66         PictureType,
       
    67         PrinterType,
       
    68         PdfType,
       
    69         PsType,
       
    70         GrabType,
       
    71         CustomDeviceType,
       
    72         CustomWidgetType,
       
    73         ImageWidgetType
       
    74 };
       
    75 
       
    76 /************************************************************************/
       
    77 class PaintCommands
       
    78 {
       
    79 public:
       
    80     // construction / initialization
       
    81     PaintCommands(const QStringList &cmds, int w, int h)
       
    82         : m_painter(0)
       
    83         , m_surface_painter(0)
       
    84         , m_commands(cmds)
       
    85         , m_gradientSpread(QGradient::PadSpread)
       
    86         , m_gradientCoordinate(QGradient::LogicalMode)
       
    87         , m_width(w)
       
    88         , m_height(h)
       
    89         , m_verboseMode(false)
       
    90         , m_type(WidgetType)
       
    91         , m_checkers_background(true)
       
    92     { staticInit(); }
       
    93 
       
    94 public:
       
    95     void setCheckersBackground(bool b) { staticInit(); m_checkers_background = b; }
       
    96     void setContents(const QStringList &cmds) {
       
    97         staticInit();
       
    98         m_blockMap.clear();
       
    99         m_pathMap.clear();
       
   100         m_pixmapMap.clear();
       
   101         m_imageMap.clear();
       
   102         m_regionMap.clear();
       
   103         m_gradientStops.clear();
       
   104         m_controlPoints.clear();
       
   105         m_gradientSpread = QGradient::PadSpread;
       
   106         m_gradientCoordinate = QGradient::LogicalMode;
       
   107         m_commands = cmds;
       
   108 
       
   109 
       
   110     }
       
   111     void setPainter(QPainter *pt) { staticInit(); m_painter = pt; }
       
   112     void setType(DeviceType t) { staticInit(); m_type = t; }
       
   113     void setFilePath(const QString &path) { staticInit(); m_filepath = path; }
       
   114     void setControlPoints(const QVector<QPointF> &points) { staticInit(); m_controlPoints = points; }
       
   115     void setVerboseMode(bool v) { staticInit(); m_verboseMode = v; }
       
   116     void insertAt(int commandIndex, const QStringList &newCommands);
       
   117 
       
   118     // run
       
   119     void runCommands();
       
   120 
       
   121 private:
       
   122     // run
       
   123     void runCommand(const QString &scriptLine);
       
   124 
       
   125     // conversion methods
       
   126     int convertToInt(const QString &str);
       
   127     double convertToDouble(const QString &str);
       
   128     float convertToFloat(const QString &str);
       
   129     QColor convertToColor(const QString &str);
       
   130 
       
   131     // commands: comments
       
   132     void command_comment(QRegExp re);
       
   133 
       
   134     // commands: importer
       
   135     void command_import(QRegExp re);
       
   136 
       
   137     // commands: blocks
       
   138     void command_begin_block(QRegExp re);
       
   139     void command_end_block(QRegExp re);
       
   140     void command_repeat_block(QRegExp re);
       
   141 
       
   142     // commands: misc
       
   143     void command_textlayout_draw(QRegExp re);
       
   144     void command_abort(QRegExp re);
       
   145 
       
   146     // commands: noops
       
   147     void command_noop(QRegExp re);
       
   148 
       
   149     // commands: setters
       
   150     void command_setBgMode(QRegExp re);
       
   151     void command_setBackground(QRegExp re);
       
   152     void command_setOpacity(QRegExp re);
       
   153     void command_path_setFillRule(QRegExp re);
       
   154     void command_setBrush(QRegExp re);
       
   155     void command_setBrushOrigin(QRegExp re);
       
   156     void command_brushTranslate(QRegExp re);
       
   157     void command_brushRotate(QRegExp re);
       
   158     void command_brushScale(QRegExp re);
       
   159     void command_brushShear(QRegExp re);
       
   160     void command_setClipPath(QRegExp re);
       
   161     void command_setClipRect(QRegExp re);
       
   162     void command_setClipRectangle(QRegExp re);
       
   163     void command_setClipRegion(QRegExp re);
       
   164     void command_setClipping(QRegExp re);
       
   165     void command_setCompositionMode(QRegExp re);
       
   166     void command_setFont(QRegExp re);
       
   167     void command_setPen(QRegExp re);
       
   168     void command_setPen2(QRegExp re);
       
   169     void command_pen_setDashOffset(QRegExp re);
       
   170     void command_pen_setDashPattern(QRegExp re);
       
   171     void command_pen_setCosmetic(QRegExp re);
       
   172     void command_setRenderHint(QRegExp re);
       
   173     void command_clearRenderHint(QRegExp re);
       
   174     void command_gradient_appendStop(QRegExp re);
       
   175     void command_gradient_clearStops(QRegExp re);
       
   176     void command_gradient_setConical(QRegExp re);
       
   177     void command_gradient_setLinear(QRegExp re);
       
   178     void command_gradient_setRadial(QRegExp re);
       
   179     void command_gradient_setLinearPen(QRegExp re);
       
   180     void command_gradient_setSpread(QRegExp re);
       
   181     void command_gradient_setCoordinateMode(QRegExp re);
       
   182 
       
   183     // commands: drawing ops
       
   184 #ifdef QT3_SUPPORT
       
   185     void command_qt3_drawArc(QRegExp re);
       
   186     void command_qt3_drawChord(QRegExp re);
       
   187     void command_qt3_drawEllipse(QRegExp re);
       
   188     void command_qt3_drawPie(QRegExp re);
       
   189     void command_qt3_drawRect(QRegExp re);
       
   190     void command_qt3_drawRoundRect(QRegExp re);
       
   191 #endif
       
   192     void command_drawArc(QRegExp re);
       
   193     void command_drawChord(QRegExp re);
       
   194     void command_drawConvexPolygon(QRegExp re);
       
   195     void command_drawEllipse(QRegExp re);
       
   196     void command_drawImage(QRegExp re);
       
   197     void command_drawLine(QRegExp re);
       
   198     void command_drawPath(QRegExp re);
       
   199     void command_drawPie(QRegExp re);
       
   200     void command_drawPixmap(QRegExp re);
       
   201     void command_drawPoint(QRegExp re);
       
   202     void command_drawPolygon(QRegExp re);
       
   203     void command_drawPolyline(QRegExp re);
       
   204     void command_drawRect(QRegExp re);
       
   205     void command_drawRoundedRect(QRegExp re);
       
   206     void command_drawRoundRect(QRegExp re);
       
   207     void command_drawText(QRegExp re);
       
   208     void command_drawTiledPixmap(QRegExp re);
       
   209     void command_path_addEllipse(QRegExp re);
       
   210     void command_path_addPolygon(QRegExp re);
       
   211     void command_path_addRect(QRegExp re);
       
   212     void command_path_addText(QRegExp re);
       
   213     void command_path_arcTo(QRegExp re);
       
   214     void command_path_closeSubpath(QRegExp re);
       
   215     void command_path_createOutline(QRegExp re);
       
   216     void command_path_cubicTo(QRegExp re);
       
   217     void command_path_debugPrint(QRegExp re);
       
   218     void command_path_lineTo(QRegExp re);
       
   219     void command_path_moveTo(QRegExp re);
       
   220     void command_region_addEllipse(QRegExp re);
       
   221     void command_region_addRect(QRegExp re);
       
   222 
       
   223     // getters
       
   224     void command_region_getClipRegion(QRegExp re);
       
   225     void command_path_getClipPath(QRegExp re);
       
   226 
       
   227     // commands: surface begin/end
       
   228     void command_surface_begin(QRegExp re);
       
   229     void command_surface_end(QRegExp re);
       
   230 
       
   231     // commands: save/restore painter state
       
   232     void command_restore(QRegExp re);
       
   233     void command_save(QRegExp re);
       
   234 
       
   235     // commands: pixmap/image
       
   236     void command_pixmap_load(QRegExp re);
       
   237     void command_pixmap_setMask(QRegExp re);
       
   238     void command_bitmap_load(QRegExp re);
       
   239     void command_image_convertToFormat(QRegExp re);
       
   240     void command_image_load(QRegExp re);
       
   241     void command_image_setColor(QRegExp re);
       
   242     void command_image_setNumColors(QRegExp re);
       
   243 
       
   244     // commands: transformation
       
   245     void command_resetMatrix(QRegExp re);
       
   246     void command_translate(QRegExp re);
       
   247     void command_rotate(QRegExp re);
       
   248     void command_rotate_x(QRegExp re);
       
   249     void command_rotate_y(QRegExp re);
       
   250     void command_scale(QRegExp re);
       
   251     void command_mapQuadToQuad(QRegExp re);
       
   252     void command_setMatrix(QRegExp re);
       
   253 
       
   254     // attributes
       
   255     QPainter *m_painter;
       
   256     QPainter *m_surface_painter;
       
   257     QImage m_surface_image;
       
   258     QPixmap m_surface_pixmap;
       
   259 #ifndef QT_NO_OPENGL
       
   260     QGLPixelBuffer *m_surface_pbuffer;
       
   261 #endif
       
   262     QRectF m_surface_rect;
       
   263     QStringList m_commands;
       
   264     QString m_currentCommand;
       
   265     int m_currentCommandIndex;
       
   266     QString m_filepath;
       
   267     QMap<QString, QStringList> m_blockMap;
       
   268     QMap<QString, QPainterPath> m_pathMap;
       
   269     QMap<QString, QPixmap> m_pixmapMap;
       
   270     QMap<QString, QImage> m_imageMap;
       
   271     QMap<QString, QRegion> m_regionMap;
       
   272     QGradientStops m_gradientStops;
       
   273     QGradient::Spread m_gradientSpread;
       
   274     QGradient::CoordinateMode m_gradientCoordinate;
       
   275     bool m_abort;
       
   276     int m_width;
       
   277     int m_height;
       
   278 
       
   279     bool m_verboseMode;
       
   280     DeviceType m_type;
       
   281     bool m_checkers_background;
       
   282 
       
   283     QVector<QPointF> m_controlPoints;
       
   284 
       
   285     // painter functionalities string tables
       
   286     static const char *brushStyleTable[];
       
   287     static const char *penStyleTable[];
       
   288     static const char *fontWeightTable[];
       
   289     static const char *clipOperationTable[];
       
   290     static const char *spreadMethodTable[];
       
   291     static const char *coordinateMethodTable[];
       
   292     static const char *compositionModeTable[];
       
   293     static const char *imageFormatTable[];
       
   294     static const char *sizeModeTable[];
       
   295     static int translateEnum(const char *table[], const QString &pattern, int limit);
       
   296 
       
   297     // utility
       
   298     template <typename T> T image_load(const QString &filepath);
       
   299 
       
   300     // commands dictionary management
       
   301     static void staticInit();
       
   302 
       
   303 public:
       
   304     struct PaintCommandInfos
       
   305     {
       
   306         PaintCommandInfos(QString id, void (PaintCommands::*p)(QRegExp), QRegExp r, QString sy, QString sa)
       
   307             : identifier(id)
       
   308             , regExp(r)
       
   309             , syntax(sy)
       
   310             , sample(sa)
       
   311             , paintMethod(p)
       
   312             {}
       
   313         PaintCommandInfos(QString title)
       
   314             : identifier(title), paintMethod(0) {}
       
   315         bool isSectionHeader() const { return paintMethod == 0; }
       
   316         QString identifier;
       
   317         QRegExp regExp;
       
   318         QString syntax;
       
   319         QString sample;
       
   320         void (PaintCommands::*paintMethod)(QRegExp);
       
   321     };
       
   322 
       
   323     static PaintCommandInfos *findCommandById(const QString &identifier) {
       
   324         for (int i=0; i<s_commandInfoTable.size(); i++)
       
   325             if (s_commandInfoTable[i].identifier == identifier)
       
   326                 return &s_commandInfoTable[i];
       
   327         return 0;
       
   328     }
       
   329 
       
   330     static QList<PaintCommandInfos> s_commandInfoTable;
       
   331     static QList<QPair<QString,QStringList> > s_enumsTable;
       
   332 };
       
   333 
       
   334 #endif // PAINTCOMMANDS_H