krita

kis_canvas.h

00001 /*
00002  *  Copyright (c) 1999 Matthias Elter  <me@kde.org>
00003  *  Copyright (c) 2004-2006 Adrian Page <adrian@pagenet.plus.com>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #ifndef KIS_CANVAS_H_
00021 #define KIS_CANVAS_H_
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <qwidget.h>
00028 #ifdef HAVE_GL
00029 #include <qgl.h>
00030 #endif
00031 #include <qpainter.h>
00032 
00033 #include "kis_global.h"
00034 #include "kis_point.h"
00035 #include "kis_vec.h"
00036 #include "kis_input_device.h"
00037 
00038 #ifdef Q_WS_X11
00039 
00040 // Irix has a different (and better) XInput tablet driver to
00041 // the XFree/xorg driver. Qt needs a separate code path for that
00042 // and so would we.
00043 #if defined(HAVE_XINPUTEXT) && !defined(Q_OS_IRIX)
00044 #define EXTENDED_X11_TABLET_SUPPORT
00045 #endif
00046 
00047 #include <map>
00048 #include <X11/Xlib.h>
00049 
00050 #if defined(EXTENDED_X11_TABLET_SUPPORT)
00051 #include <X11/extensions/XInput.h>
00052 #endif
00053 
00054 #endif // Q_WS_X11
00055 
00056 class KisEvent;
00057 class KisMoveEvent;
00058 class KisButtonPressEvent;
00059 class KisButtonReleaseEvent;
00060 class KisDoubleClickEvent;
00061 class KisCanvasWidgetPainter;
00062 
00063 class KisCanvasWidget : public QObject {
00064     Q_OBJECT
00065 
00066 public:
00067     KisCanvasWidget();
00068     virtual ~KisCanvasWidget();
00069     
00070     // When enabled, the canvas may throw away move events if the application
00071     // is unable to keep up with them, i.e. intermediate move events in the event
00072     // queue are skipped.
00073     void enableMoveEventCompressionHint(bool enableMoveCompression) { m_enableMoveEventCompressionHint = enableMoveCompression; }
00074 
00075     virtual KisCanvasWidgetPainter *createPainter() = 0;
00076 
00077 #ifdef EXTENDED_X11_TABLET_SUPPORT
00078     static KisInputDevice findActiveInputDevice();
00079     virtual void selectTabletDeviceEvents() = 0;
00080 
00081     static void selectTabletDeviceEvents(QWidget *widget);
00082 #endif
00083 
00084 #ifdef Q_WS_X11
00085     static void initX11Support();
00086 #endif
00087 
00088 signals:
00089     void sigGotPaintEvent(QPaintEvent*);
00090     void sigGotEnterEvent(QEvent*);
00091     void sigGotLeaveEvent(QEvent*);
00092     void sigGotMouseWheelEvent(QWheelEvent*);
00093     void sigGotKeyPressEvent(QKeyEvent*);
00094     void sigGotKeyReleaseEvent(QKeyEvent*);
00095     void sigGotDragEnterEvent(QDragEnterEvent*);
00096     void sigGotDropEvent(QDropEvent*);
00097     void sigGotMoveEvent(KisMoveEvent *);
00098     void sigGotButtonPressEvent(KisButtonPressEvent *);
00099     void sigGotButtonReleaseEvent(KisButtonReleaseEvent *);
00100     void sigGotDoubleClickEvent(KisDoubleClickEvent *);
00101 
00102 protected:
00103     void widgetGotPaintEvent(QPaintEvent *event);
00104     void widgetGotMousePressEvent(QMouseEvent *event);
00105     void widgetGotMouseReleaseEvent(QMouseEvent *event);
00106     void widgetGotMouseDoubleClickEvent(QMouseEvent *event);
00107     void widgetGotMouseMoveEvent(QMouseEvent *event);
00108     void widgetGotTabletEvent(QTabletEvent *event);
00109     void widgetGotEnterEvent(QEvent *event );
00110     void widgetGotLeaveEvent(QEvent *event);
00111     void widgetGotWheelEvent(QWheelEvent *event);
00112     void widgetGotKeyPressEvent(QKeyEvent *event);
00113     void widgetGotKeyReleaseEvent(QKeyEvent *event);
00114     void widgetGotDragEnterEvent(QDragEnterEvent *event);
00115     void widgetGotDropEvent(QDropEvent *event);
00116     void moveEvent(KisMoveEvent *event);
00117     void buttonPressEvent(KisButtonPressEvent *event);
00118     void buttonReleaseEvent(KisButtonReleaseEvent *event);
00119     void doubleClickEvent(KisDoubleClickEvent *event);
00120     void translateTabletEvent(KisEvent *event);
00121 
00122 protected:
00123 
00124     bool m_enableMoveEventCompressionHint;
00125     double m_lastPressure;
00126 
00127 #ifdef Q_WS_X11
00128     // On X11 systems, Qt throws away mouse move events if the application
00129     // is unable to keep up with them. We override this behaviour so that
00130     // we receive all move events, so that painting follows the mouse's motion
00131     // accurately.
00132     bool x11Event(XEvent *event, Display *x11Display, WId winId, QPoint widgetOriginPos);
00133     static Qt::ButtonState translateX11ButtonState(int state);
00134     static Qt::ButtonState translateX11Button(unsigned int button);
00135 
00136     static bool X11SupportInitialised;
00137 
00138     // Modifier masks for alt/meta - detected at run-time
00139     static long X11AltMask;
00140     static long X11MetaMask;
00141 
00142     int m_lastRootX;
00143     int m_lastRootY;
00144 
00145 #ifdef EXTENDED_X11_TABLET_SUPPORT
00146 
00147 public:
00148     class X11TabletDevice
00149     {
00150     public:
00151         X11TabletDevice();
00152         X11TabletDevice(const XDeviceInfo *deviceInfo);
00153 
00154         bool mightBeTabletDevice() const { return m_mightBeTabletDevice; }
00155 
00156         XID id() const { return m_deviceId; }
00157         XDevice *xDevice() const { return m_XDevice; }
00158         QString name() const { return m_name; }
00159 
00160         KisInputDevice inputDevice() const { return m_inputDevice; }
00161         void setInputDevice(KisInputDevice inputDevice) { m_inputDevice = inputDevice; }
00162 
00163         void setEnabled(bool enabled);
00164         bool enabled() const;
00165 
00166         Q_INT32 numAxes() const;
00167 
00168         void setXAxis(Q_INT32 axis);
00169         void setYAxis(Q_INT32 axis);
00170         void setPressureAxis(Q_INT32 axis);
00171         void setXTiltAxis(Q_INT32 axis);
00172         void setYTiltAxis(Q_INT32 axis);
00173         void setWheelAxis(Q_INT32 axis);
00174         void setToolIDAxis(Q_INT32 axis);
00175         void setSerialNumberAxis(Q_INT32 axis);
00176 
00177         static const Q_INT32 NoAxis = -1;
00178         static const Q_INT32 DefaultAxis = -2;
00179 
00180         Q_INT32 xAxis() const;
00181         Q_INT32 yAxis() const;
00182         Q_INT32 pressureAxis() const;
00183         Q_INT32 xTiltAxis() const;
00184         Q_INT32 yTiltAxis() const;
00185         Q_INT32 wheelAxis() const;
00186         Q_INT32 toolIDAxis() const;
00187         Q_INT32 serialNumberAxis() const;
00188 
00189         void readSettingsFromConfig();
00190         void writeSettingsToConfig();
00191 
00192         // These return -1 if the device does not support the event
00193         int buttonPressEvent() const { return m_buttonPressEvent; }
00194         int buttonReleaseEvent() const { return m_buttonReleaseEvent; }
00195         int motionNotifyEvent() const { return m_motionNotifyEvent; }
00196         int proximityInEvent() const { return m_proximityInEvent; }
00197         int proximityOutEvent() const { return m_proximityOutEvent; }
00198 
00199         void enableEvents(QWidget *widget) const;
00200 
00201         class State
00202         {
00203         public:
00204             State() {}
00205             State(const KisPoint& pos, double pressure, const KisVector2D& tilt, double wheel,
00206                   Q_UINT32 toolID, Q_UINT32 serialNumber);
00207 
00208             // Position, pressure and wheel are normalised to 0 - 1
00209             KisPoint pos() const { return m_pos; }
00210             double pressure() const { return m_pressure; }
00211             // Tilt is normalised to -1->+1
00212             KisVector2D tilt() const { return m_tilt; }
00213             double wheel() const { return m_wheel; }
00214             // Wacom tool id and serial number of device.
00215             Q_UINT32 toolID() const { return m_toolID; }
00216             Q_UINT32 serialNumber() const { return m_serialNumber; }
00217 
00218         private:
00219             KisPoint m_pos;
00220             double m_pressure;
00221             KisVector2D m_tilt;
00222             double m_wheel;
00223             Q_UINT32 m_toolID;
00224             Q_UINT32 m_serialNumber;
00225         };
00226 
00227         State translateAxisData(const int *axisData) const;
00228 
00229     private:
00230         double translateAxisValue(int value, const XAxisInfo& axisInfo) const;
00231 
00232         XID m_deviceId;
00233         XDevice *m_XDevice;
00234 
00235         QString m_name;
00236 
00237         bool m_mightBeTabletDevice;
00238         KisInputDevice m_inputDevice;
00239 
00240         bool m_enabled;
00241 
00242         Q_INT32 m_xAxis;
00243         Q_INT32 m_yAxis;
00244         Q_INT32 m_pressureAxis;
00245         Q_INT32 m_xTiltAxis;
00246         Q_INT32 m_yTiltAxis;
00247         Q_INT32 m_wheelAxis;
00248         Q_INT32 m_toolIDAxis;
00249         Q_INT32 m_serialNumberAxis;
00250 
00251         QValueVector<XAxisInfo> m_axisInfo;
00252 
00253         int m_motionNotifyEvent;
00254         int m_buttonPressEvent;
00255         int m_buttonReleaseEvent;
00256         int m_proximityInEvent;
00257         int m_proximityOutEvent;
00258 
00259         QValueVector<XEventClass> m_eventClassList;
00260     };
00261 
00262     typedef std::map<XID, X11TabletDevice> X11XIDTabletDeviceMap;
00263     static X11XIDTabletDeviceMap& tabletDeviceMap();
00264 
00265 protected:
00266     static int X11DeviceMotionNotifyEvent;
00267     static int X11DeviceButtonPressEvent;
00268     static int X11DeviceButtonReleaseEvent;
00269     static int X11ProximityInEvent;
00270     static int X11ProximityOutEvent;
00271 
00272     static X11XIDTabletDeviceMap X11TabletDeviceMap;
00273 
00274 #endif // EXTENDED_X11_TABLET_SUPPORT
00275 
00276 #endif // Q_WS_X11
00277 };
00278 
00279 class KisCanvas : public QObject {
00280     Q_OBJECT
00281 
00282 public:
00283     KisCanvas(QWidget *parent, const char *name);
00284     virtual ~KisCanvas();
00285     
00286     // When enabled, the canvas may throw away move events if the application
00287     // is unable to keep up with them, i.e. intermediate move events in the event
00288     // queue are skipped.
00289     void enableMoveEventCompressionHint(bool enableMoveCompression);
00290 
00291     bool isOpenGLCanvas() const;
00292 
00296     bool cursorIsOverCanvas() const;
00297 
00302     void handleKeyEvent(QEvent *e);
00303 
00304     int width() const;
00305     int height() const;
00306 
00307     void update();
00308     void update(const QRect& r);
00309     void update(int x, int y, int width, int height);
00310     void repaint();
00311     void repaint(bool erase);
00312     void repaint(int x, int y, int width, int height, bool erase = true);
00313     void repaint(const QRect& r, bool erase = true);
00314     void repaint(const QRegion& r, bool erase = true);
00315 
00316     void updateGeometry();
00317 
00318 #if defined(EXTENDED_X11_TABLET_SUPPORT)
00319     void selectTabletDeviceEvents();
00320 #endif
00321 
00322 signals:
00323     void sigGotPaintEvent(QPaintEvent*);
00324     void sigGotEnterEvent(QEvent*);
00325     void sigGotLeaveEvent(QEvent*);
00326     void sigGotMouseWheelEvent(QWheelEvent*);
00327     void sigGotKeyPressEvent(QKeyEvent*);
00328     void sigGotKeyReleaseEvent(QKeyEvent*);
00329     void sigGotDragEnterEvent(QDragEnterEvent*);
00330     void sigGotDropEvent(QDropEvent*);
00331     void sigGotMoveEvent(KisMoveEvent *);
00332     void sigGotButtonPressEvent(KisButtonPressEvent *);
00333     void sigGotButtonReleaseEvent(KisButtonReleaseEvent *);
00334     void sigGotDoubleClickEvent(KisDoubleClickEvent *);
00335 
00336 protected:
00337     // Allow KisView to render on the widget directly, but everything else
00338     // has restricted access.
00339     friend class KisView;
00340     friend class KisCanvasPainter;
00341 
00342     // One of these will be valid, the other null. In Qt3, using a QPainter on
00343     // a QGLWidget is not reliable.
00344     QWidget *QPaintDeviceWidget() const;
00345 #ifdef HAVE_GL
00346     QGLWidget *OpenGLWidget() const;
00347 #endif
00348     void createQPaintDeviceCanvas();
00349 #ifdef HAVE_GL
00350     void createOpenGLCanvas(QGLWidget *sharedContextWidget);
00351 #endif
00352     void show();
00353     void hide();
00354     void setGeometry(int x, int y, int width, int height);
00355 
00356     void setUpdatesEnabled(bool updatesEnabled);
00357     bool isUpdatesEnabled() const;
00358 
00359     void setFocusPolicy(QWidget::FocusPolicy focusPolicy);
00360 
00361     const QCursor& cursor() const;
00362     void setCursor(const QCursor& cursor);
00363 
00364     KisCanvasWidgetPainter *createPainter();
00365     KisCanvasWidget *canvasWidget() const;
00366 
00367 protected:
00368 #ifdef HAVE_GL
00369     void createCanvasWidget(bool useOpenGL, QGLWidget *sharedContextWidget = 0);
00370 #else
00371     void createCanvasWidget(bool useOpenGL);
00372 #endif
00373     QWidget *m_parent;
00374     QString m_name;
00375     KisCanvasWidget *m_canvasWidget;
00376     bool m_enableMoveEventCompressionHint;
00377     bool m_useOpenGL;
00378 };
00379 
00380 #endif // KIS_CANVAS_H_
00381 
KDE Home | KDE Accessibility Home | Description of Access Keys