File indexing completed on 2024-05-05 17:04:28

0001 /* This file is part of the KDE project
0002  * Copyright (C) Boudewijn Rempt <boud@valdyas.org>, (C) 2013
0003  *
0004  *  This program is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation; either version 2 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program; if not, write to the Free Software
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 #include "TouchDeclarativeView.h"
0019 
0020 #include <QWidget>
0021 #include <QSurfaceFormat>
0022 #include <QResizeEvent>
0023 #include <QApplication>
0024 #include <QQuickItem>
0025 
0026 #include <gemini/ViewModeSwitchEvent.h>
0027 
0028 TouchDeclarativeView::TouchDeclarativeView(QWindow *parent)
0029     : QQuickView(parent)
0030     , m_drawCanvas(false)
0031     , m_canvasWidget(0)
0032     , m_GLInitialized(false)
0033 {
0034 //     setCacheMode(QQuickView::CacheNone);
0035 //     setViewportUpdateMode(QQuickView::FullViewportUpdate);
0036 
0037 //     setFlag(Qt::WA_AcceptTouchEvents);
0038 //     setAttribute(Qt::WA_OpaquePaintEvent);
0039 //     setAttribute(Qt::WA_NoSystemBackground);
0040 //     viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
0041 //     viewport()->setAttribute(Qt::WA_NoSystemBackground);
0042 //     installEventFilter(this);
0043 }
0044 
0045 TouchDeclarativeView::TouchDeclarativeView(const QUrl &url, QWindow *parent)
0046     : QQuickView(url, parent)
0047     , m_drawCanvas(false)
0048     , m_canvasWidget(0)
0049     , m_GLInitialized(false)
0050 {
0051 //     setCacheMode(QQuickView::CacheNone);
0052 //     setViewportUpdateMode(QQuickView::FullViewportUpdate);
0053 
0054 //     QSurfaceFormat format;
0055 //     format.setSampleBuffers(true);
0056 //     setFormat(format);
0057 
0058 //     setAttribute(Qt::WA_AcceptTouchEvents);
0059 //     setAttribute(Qt::WA_OpaquePaintEvent);
0060 //     setAttribute(Qt::WA_NoSystemBackground);
0061 //     viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
0062 //     viewport()->setAttribute(Qt::WA_NoSystemBackground);
0063 //     installEventFilter(this);
0064 }
0065 
0066 TouchDeclarativeView::~TouchDeclarativeView()
0067 {
0068     m_canvasWidget = 0;
0069 }
0070 
0071 QWidget* TouchDeclarativeView::canvasWidget() const
0072 {
0073     return m_canvasWidget.data();
0074 }
0075 
0076 void TouchDeclarativeView::setCanvasWidget(QWidget *canvasWidget)
0077 {
0078     m_canvasWidget = canvasWidget;// qobject_cast<KisOpenGLCanvas2*>(canvasWidget);
0079     connect(m_canvasWidget, SIGNAL(destroyed(QObject*)), this, SLOT(resetInitialized()));
0080     emit canvasWidgetChanged();
0081 }
0082 
0083 void TouchDeclarativeView::resetInitialized()
0084 {
0085     m_GLInitialized = false;
0086 }
0087 
0088 bool TouchDeclarativeView::drawCanvas() const
0089 {
0090     return m_drawCanvas;
0091 }
0092 
0093 void TouchDeclarativeView::setDrawCanvas(bool drawCanvas)
0094 {
0095     if (m_drawCanvas != drawCanvas) {
0096         m_drawCanvas = drawCanvas;
0097         emit drawCanvasChanged();
0098     }
0099 }
0100 
0101 // void TouchDeclarativeView::drawBackground(QPainter *painter, const QRectF &rect)
0102 // {
0103 // 
0104 //     if (painter->paintEngine()->type() != QPaintEngine::OpenGL2) {
0105 //         qWarning("OpenGLScene: drawBackground needs a "
0106 //                  "QGLWidget to be set as viewport on the "
0107 //                  "graphics view");
0108 //         return;
0109 //     }
0110 // 
0111 //     if (m_drawCanvas && m_canvasWidget) {
0112 //         if (!m_GLInitialized) {
0113 //             //m_canvasWidget->initializeCheckerShader();
0114 //             //m_canvasWidget->initializeDisplayShader();
0115 //             m_GLInitialized = true;
0116 //         }
0117 //         //m_canvasWidget->renderCanvasGL();
0118 //         //m_canvasWidget->renderDecorations(painter);
0119 //     }
0120 //     else {
0121 //         QQuickView::drawBackground(painter, rect);
0122 //     }
0123 // 
0124 // }
0125 
0126 
0127 // void TouchDeclarativeView::resizeEvent(QResizeEvent *event)
0128 // {
0129 //     if (m_canvasWidget) {
0130 //         //m_canvasWidget->coordinatesConverter()->setCanvasWidgetSize(event->size());
0131 //     }
0132 // 
0133 //     QQuickView::resizeEvent(event);
0134 // }
0135 // 
0136 // bool TouchDeclarativeView::event( QEvent* event )
0137 // {
0138 //     switch(static_cast<int>(event->type())) {
0139 //         case QEvent::TabletPress:
0140 //         case QEvent::TabletMove:
0141 //         case QEvent::TabletRelease:
0142 //             break;
0143 //         case ViewModeSwitchEvent::AboutToSwitchViewModeEvent:
0144 //         case ViewModeSwitchEvent::SwitchedToTouchModeEvent: {
0145 //             // If we don't have a canvas widget yet, we don't really have anywhere to send those events
0146 //             // so... let's just not
0147 //             //if (m_canvasWidget.data())
0148 // //             {
0149 //                 //QGraphicsScene is silly and will not forward unknown events to its items, so emulate that
0150 //                 //functionality.
0151 // //                 QList<QGraphicsItem*> items = scene()->items();
0152 // //                 Q_FOREACH(QGraphicsItem* item, items) {
0153 // //                    if (item == m_sketchView || qobject_cast<TouchView*>((item))) {
0154 // //                        if (item != m_sketchView)
0155 // //                            m_sketchView = item;
0156 // //                         scene()->sendEvent(item, event);
0157 // //                        break;
0158 // //                    }
0159 // //                 }
0160 // //             }
0161 //             sendEvent(contentItem(), event);
0162 //             break;
0163 //         }
0164 //         default:
0165 //             break;
0166 //     }
0167 //     return QQuickView::event( event );
0168 // }
0169 // 
0170 // bool TouchDeclarativeView::eventFilter(QObject* watched, QEvent* e)
0171 // {
0172 //     switch(static_cast<int>(e->type())) {
0173 // //         case KisTabletEvent::TabletMoveEx:
0174 // //         case KisTabletEvent::TabletPressEx:
0175 // //         case KisTabletEvent::TabletReleaseEx: {
0176 // //             if (m_canvasWidget.data())
0177 // //             {
0178 // //                 //QGraphicsScene is silly and will not forward unknown events to its items, so emulate that
0179 // //                 //functionality.s
0180 // //                 KisTabletEvent* ev = static_cast<KisTabletEvent*>(e);
0181 // //                 QList<QGraphicsItem*> items = scene()->items(ev->pos());
0182 // //                 Q_FOREACH(QGraphicsItem* item, items)
0183 // //                 {
0184 // //                     if(scene()->sendEvent(item, e))
0185 // //                         return true;
0186 // //                 }
0187 // //             }
0188 // //             break;
0189 // //         }
0190 //         default:
0191 //             break;
0192 //     }
0193 // 
0194 // 
0195 //     return QQuickView::eventFilter(watched, e);
0196 // }