Commit 6584106c authored by Christoffer Ackelman's avatar Christoffer Ackelman

Fixed event handling when using QScrollArea

parent 5dcb0632
This diff is collapsed.
...@@ -42,16 +42,24 @@ ...@@ -42,16 +42,24 @@
#include <assert.h> #include <assert.h>
#include <QMainWindow>
#include <QPainter> #include <QPainter>
#include <QScrollArea>
#include "cow_qt_helpers.h"
QImage QtScrollWidgetFlow::createBuffer(QSize size)
{
QImage image = QImage(size, QImage::Format_RGB32);
QPainter imPainter(&image);
imPainter.fillRect(image.rect(), palette().color(QPalette::Background));
return image;
}
void QtScrollWidgetFlow::init( void QtScrollWidgetFlow::init(
unsigned int eCtxType, int (*init_proc)(FlowCtx *ctx, void *client_data), unsigned int eCtxType, int (*init_proc)(FlowCtx *ctx, void *client_data),
void *client_data, int (*init_proc2)(QWidget *w, FlowCtx *ctx, void *client_data, int (*init_proc2)(QWidget *w, FlowCtx *ctx,
void *client_data)) void *client_data))
{ {
this->image = QImage(size(), QImage::Format_RGB32); this->image = createBuffer(size());
this->ctxType = eCtxType; this->ctxType = eCtxType;
this->init_proc = init_proc; this->init_proc = init_proc;
this->init_widget_proc = init_proc2; this->init_widget_proc = init_proc2;
...@@ -63,15 +71,13 @@ QWidget *QtScrollWidgetFlow::initScroll( ...@@ -63,15 +71,13 @@ QWidget *QtScrollWidgetFlow::initScroll(
void *client_data, int (*init_proc2)(QWidget *w, FlowCtx *ctx, void *client_data, int (*init_proc2)(QWidget *w, FlowCtx *ctx,
void *client_data)) void *client_data))
{ {
this->image = QImage(size(), QImage::Format_RGB32); this->image = createBuffer(size());
this->ctxType = eCtxType; this->ctxType = eCtxType;
this->init_proc = init_proc; this->init_proc = init_proc;
this->init_widget_proc = init_proc2; this->init_widget_proc = init_proc2;
this->client_data = client_data; this->client_data = client_data;
QMainWindow *window = new QMainWindow(); QScrollAreaFlow *form = new QScrollAreaFlow();
window->setAttribute(Qt::WA_DeleteOnClose);
QScrollArea *form = new QScrollArea();
scroll_h = form->horizontalScrollBar(); scroll_h = form->horizontalScrollBar();
scroll_v = form->verticalScrollBar(); scroll_v = form->verticalScrollBar();
QObject::connect(scroll_h, SIGNAL(valueChanged(int)), this, QObject::connect(scroll_h, SIGNAL(valueChanged(int)), this,
...@@ -79,15 +85,21 @@ QWidget *QtScrollWidgetFlow::initScroll( ...@@ -79,15 +85,21 @@ QWidget *QtScrollWidgetFlow::initScroll(
QObject::connect(scroll_v, SIGNAL(valueChanged(int)), this, QObject::connect(scroll_v, SIGNAL(valueChanged(int)), this,
SLOT(scroll_v_action(int))); SLOT(scroll_v_action(int)));
form->setWidgetResizable(true);
form->setWidget(this); form->setWidget(this);
window->setCentralWidget(form); form->setWidgetResizable(true);
return window; return form;
}
bool QScrollAreaFlow::event(QEvent *event)
{
QtScrollWidgetFlow *child = ((QtScrollWidgetFlow *) widget());
child->handleEvent(event);
return QScrollArea::event(event);
} }
void QtScrollWidgetFlow::init(unsigned int eCtxType, QWidget *main) void QtScrollWidgetFlow::init(unsigned int eCtxType, QWidget *main)
{ {
this->image = QImage(size(), QImage::Format_RGB32); this->image = createBuffer(size());
this->ctxType = eCtxType; this->ctxType = eCtxType;
is_navigator = 1; is_navigator = 1;
main_widget = main; main_widget = main;
...@@ -200,37 +212,12 @@ void QtScrollWidgetFlow::scroll_v_action(int value) ...@@ -200,37 +212,12 @@ void QtScrollWidgetFlow::scroll_v_action(int value)
void QtScrollWidgetFlow::paintEvent(QPaintEvent *event) void QtScrollWidgetFlow::paintEvent(QPaintEvent *event)
{ {
handleEvent(event);
QWidget::paintEvent(event);
QPainter painter(this); QPainter painter(this);
QRect dirtyRect = event->rect(); QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect); painter.drawImage(dirtyRect, image, dirtyRect);
} QPainter imPainter(&image);
imPainter.fillRect(image.rect(), palette().color(QPalette::Background));
void QtScrollWidgetFlow::mouseMoveEvent(QMouseEvent *event) QWidget::paintEvent(event);
{
handleEvent(event, false, true);
QWidget::mouseMoveEvent(event);
}
void QtScrollWidgetFlow::moveEvent(QMoveEvent *event)
{
handleEvent(event, true);
QWidget::moveEvent(event);
}
void QtScrollWidgetFlow::resizeEvent(QResizeEvent *event)
{
this->image = QImage(event->size(), QImage::Format_RGB32);
realize();
handleEvent(event, true);
QWidget::resizeEvent(event);
}
void QtScrollWidgetFlow::showEvent(QShowEvent *event)
{
realize();
QWidget::showEvent(event);
} }
void QtScrollWidgetFlow::closeEvent(QCloseEvent *event) void QtScrollWidgetFlow::closeEvent(QCloseEvent *event)
...@@ -249,17 +236,16 @@ void QtScrollWidgetFlow::closeEvent(QCloseEvent *event) ...@@ -249,17 +236,16 @@ void QtScrollWidgetFlow::closeEvent(QCloseEvent *event)
QWidget::closeEvent(event); QWidget::closeEvent(event);
} }
void QtScrollWidgetFlow::handleEvent(QEvent *event, bool conf_scroll, void QtScrollWidgetFlow::handleEvent(QEvent *event)
bool update)
{ {
if (!destroyed) { if (!destroyed) {
if (conf_scroll) { if (event->type() == QEvent::Resize || event->type() == QEvent::Move) {
scroll_configure = 1; scroll_configure = 1;
} }
if (parent_ctx) { if (parent_ctx) {
FlowCtx * ctx = (FlowCtx *) parent_ctx; FlowCtx * ctx = (FlowCtx *) parent_ctx;
FlowDrawQt *drawer = ((FlowDrawQt *) ctx->fdraw); FlowDrawQt *drawer = ((FlowDrawQt *) ctx->fdraw);
if (update) { if (event->type() == QEvent::MouseMove) {
drawer->window->update(); drawer->window->update();
} }
drawer->event_handler(event); drawer->event_handler(event);
...@@ -267,11 +253,22 @@ void QtScrollWidgetFlow::handleEvent(QEvent *event, bool conf_scroll, ...@@ -267,11 +253,22 @@ void QtScrollWidgetFlow::handleEvent(QEvent *event, bool conf_scroll,
} }
} }
void QtScrollWidgetFlow::realize() bool QtScrollWidgetFlow::event(QEvent *event)
{ {
if (is_realized) { if (event->type() == QEvent::Resize) {
return; this->image = createBuffer(((QResizeEvent *) event)->size());
} }
if (!is_realized && (event->type() == QEvent::Show || event->type() == QEvent::Resize)) {
realize();
is_realized = true;
}
handleEvent(event);
return QWidget::event(event);
}
void QtScrollWidgetFlow::realize()
{
debug_print("realize\n");
if (!parent_ctx) { if (!parent_ctx) {
if (is_navigator) { if (is_navigator) {
...@@ -280,6 +277,7 @@ void QtScrollWidgetFlow::realize() ...@@ -280,6 +277,7 @@ void QtScrollWidgetFlow::realize()
if (main && !main->is_realized) { if (main && !main->is_realized) {
main->realize(); main->realize();
main->is_realized = true;
} }
assert(main->parent_ctx != NULL); assert(main->parent_ctx != NULL);
...@@ -294,6 +292,4 @@ void QtScrollWidgetFlow::realize() ...@@ -294,6 +292,4 @@ void QtScrollWidgetFlow::realize()
static_cast<flow_eCtxType>(ctxType)); static_cast<flow_eCtxType>(ctxType));
} }
} }
is_realized = true;
} }
\ No newline at end of file
...@@ -45,11 +45,9 @@ ...@@ -45,11 +45,9 @@
#include "flow_ctx.h" #include "flow_ctx.h"
#include "flow_draw_qt.h" #include "flow_draw_qt.h"
#include <QMouseEvent>
#include <QMoveEvent>
#include <QPaintEvent> #include <QPaintEvent>
#include <QResizeEvent>
#include <QScrollBar> #include <QScrollBar>
#include <QScrollArea>
#include <QWidget> #include <QWidget>
typedef struct { typedef struct {
...@@ -61,6 +59,13 @@ typedef struct { ...@@ -61,6 +59,13 @@ typedef struct {
int scroll_v_managed; int scroll_v_managed;
} widget_sScroll; } widget_sScroll;
// QScrollArea does not forward events to its child widget
// So we subclass QScrollArea and override the event() handler
class QScrollAreaFlow : public QScrollArea {
protected:
bool event(QEvent *event);
};
class QtScrollWidgetFlow : public QWidget { class QtScrollWidgetFlow : public QWidget {
Q_OBJECT Q_OBJECT
...@@ -100,22 +105,22 @@ public: ...@@ -100,22 +105,22 @@ public:
QImage image; QImage image;
virtual void handleEvent(QEvent *event);
protected: protected:
virtual void realize(); virtual void realize();
virtual void handleEvent(QEvent *event, bool conf_scroll = false,
bool update = false);
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void moveEvent(QMoveEvent *event);
void resizeEvent(QResizeEvent *event);
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
void showEvent(QShowEvent *event); bool event(QEvent *event);
bool is_realized; bool is_realized;
unsigned int ctxType; unsigned int ctxType;
private:
QImage createBuffer(QSize size);
public slots: public slots:
void scroll_h_action(int value); void scroll_h_action(int value);
void scroll_v_action(int value); void scroll_v_action(int value);
......
...@@ -340,8 +340,29 @@ int GlowDrawQt::event_handler(QEvent *event) ...@@ -340,8 +340,29 @@ int GlowDrawQt::event_handler(QEvent *event)
return 1; return 1;
} }
if (QApplication::focusWidget() == m_wind->window->window() || QObject *parent = m_wind->window;
event->type() == QEvent::KeyPress) { bool windowEvent = false;
while (parent != NULL) {
if (QApplication::focusWidget() == parent) {
windowEvent = true;
break;
}
parent = parent->parent();
}
bool navWindowEvent = false;
if (!windowEvent) {
parent = nav_wind->window;
while (parent != NULL) {
if (QApplication::focusWidget() == parent) {
navWindowEvent = true;
break;
}
parent = parent->parent();
}
}
if (windowEvent || event->type() == QEvent::KeyPress) {
if (GlowCtx::eventlog_enabled) { if (GlowCtx::eventlog_enabled) {
log_event(event); log_event(event);
} }
...@@ -852,7 +873,7 @@ int GlowDrawQt::event_handler(QEvent *event) ...@@ -852,7 +873,7 @@ int GlowDrawQt::event_handler(QEvent *event)
default: default:
break; break;
} }
} else if (QApplication::focusWidget() == nav_wind->window) { } else if (navWindowEvent) {
switch (event->type()) { switch (event->type()) {
case QEvent::MouseButtonPress: { case QEvent::MouseButtonPress: {
QMouseEvent *mouseEvent = ((QMouseEvent *) event); QMouseEvent *mouseEvent = ((QMouseEvent *) event);
...@@ -1834,10 +1855,8 @@ void GlowDrawQt::set_clip(DrawWind *wind, QPainter *painter) ...@@ -1834,10 +1855,8 @@ void GlowDrawQt::set_clip(DrawWind *wind, QPainter *painter)
void GlowDrawQt::set_image_clip_mask(QPainter *painter, glow_tPixmap pixmap, void GlowDrawQt::set_image_clip_mask(QPainter *painter, glow_tPixmap pixmap,
int x, int y) int x, int y)
{ {
QBitmap mask = QBitmap(*((QPixmap *) pixmap)); QRegion clipRegion = QRegion(((QPixmap *) pixmap)->createMaskFromColor(Qt::black));
QRegion clipRegion = QRegion(mask); painter->setClipRegion(clipRegion.translated(x, y));
clipRegion.translate(x, y);
painter->setClipRegion(clipRegion);
painter->setClipping(true); painter->setClipping(true);
} }
......
...@@ -40,16 +40,26 @@ ...@@ -40,16 +40,26 @@
*/ */
#include "glow_scroll_widget_qt.h" #include "glow_scroll_widget_qt.h"
#include <QMainWindow> #include <assert.h>
#include <QPainter> #include <QPainter>
#include <QScrollArea>
#include "cow_qt_helpers.h"
QImage QtScrollWidgetGlow::createBuffer(QSize size)
{
QImage image = QImage(size, QImage::Format_RGB32);
QPainter imPainter(&image);
imPainter.fillRect(image.rect(), palette().color(QPalette::Background));
return image;
}
void QtScrollWidgetGlow::init( void QtScrollWidgetGlow::init(
unsigned int eCtxType, int (*init_proc)(GlowCtx *ctx, void *client_data), unsigned int eCtxType, int (*init_proc)(GlowCtx *ctx, void *client_data),
void *client_data, int (*init_proc2)(QWidget *w, GlowCtx *ctx, void *client_data, int (*init_proc2)(QWidget *w, GlowCtx *ctx,
void *client_data)) void *client_data))
{ {
this->image = QImage(size(), QImage::Format_RGB32); this->image = createBuffer(size());
this->ctxType = eCtxType; this->ctxType = eCtxType;
this->init_proc = init_proc; this->init_proc = init_proc;
this->init_widget_proc = init_proc2; this->init_widget_proc = init_proc2;
...@@ -61,15 +71,13 @@ QWidget *QtScrollWidgetGlow::initScroll( ...@@ -61,15 +71,13 @@ QWidget *QtScrollWidgetGlow::initScroll(
void *client_data, int (*init_proc2)(QWidget *w, GlowCtx *ctx, void *client_data, int (*init_proc2)(QWidget *w, GlowCtx *ctx,
void *client_data)) void *client_data))
{ {
this->image = QImage(size(), QImage::Format_RGB32); this->image = createBuffer(size());
this->ctxType = eCtxType; this->ctxType = eCtxType;
this->init_proc = init_proc; this->init_proc = init_proc;
this->init_widget_proc = init_proc2; this->init_widget_proc = init_proc2;
this->client_data = client_data; this->client_data = client_data;
QMainWindow *window = new QMainWindow(); QScrollAreaGlow *form = new QScrollAreaGlow();
window->setAttribute(Qt::WA_DeleteOnClose);
QScrollArea *form = new QScrollArea();
scroll_h = form->horizontalScrollBar(); scroll_h = form->horizontalScrollBar();
scroll_v = form->verticalScrollBar(); scroll_v = form->verticalScrollBar();
QObject::connect(scroll_h, SIGNAL(valueChanged(int)), this, QObject::connect(scroll_h, SIGNAL(valueChanged(int)), this,
...@@ -77,15 +85,21 @@ QWidget *QtScrollWidgetGlow::initScroll( ...@@ -77,15 +85,21 @@ QWidget *QtScrollWidgetGlow::initScroll(
QObject::connect(scroll_v, SIGNAL(valueChanged(int)), this, QObject::connect(scroll_v, SIGNAL(valueChanged(int)), this,
SLOT(scroll_v_action(int))); SLOT(scroll_v_action(int)));
form->setWidgetResizable(true);
form->setWidget(this); form->setWidget(this);
window->setCentralWidget(form); form->setWidgetResizable(true);
return window; return form;
}
bool QScrollAreaGlow::event(QEvent *event)
{
QtScrollWidgetGlow *child = ((QtScrollWidgetGlow *) widget());
child->handleEvent(event);
return QScrollArea::event(event);
} }
void QtScrollWidgetGlow::init(unsigned int eCtxType, QWidget *main) void QtScrollWidgetGlow::init(unsigned int eCtxType, QWidget *main)
{ {
this->image = QImage(size(), QImage::Format_RGB32); this->image = createBuffer(size());
this->ctxType = eCtxType; this->ctxType = eCtxType;
is_navigator = 1; is_navigator = 1;
main_widget = main; main_widget = main;
...@@ -198,39 +212,12 @@ void QtScrollWidgetGlow::scroll_v_action(int value) ...@@ -198,39 +212,12 @@ void QtScrollWidgetGlow::scroll_v_action(int value)
void QtScrollWidgetGlow::paintEvent(QPaintEvent *event) void QtScrollWidgetGlow::paintEvent(QPaintEvent *event)
{ {
handleEvent(event);
QWidget::paintEvent(event);
QPainter painter(this); QPainter painter(this);
QRect dirtyRect = event->rect(); QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect); painter.drawImage(dirtyRect, image, dirtyRect);
} QPainter imPainter(&image);
imPainter.fillRect(image.rect(), palette().color(QPalette::Background));
void QtScrollWidgetGlow::mouseMoveEvent( QWidget::paintEvent(event);
QMouseEvent *event)
{
handleEvent(event, false, true);
QWidget::mouseMoveEvent(event);
}
void QtScrollWidgetGlow::moveEvent(QMoveEvent *event)
{
handleEvent(event, true);
QWidget::moveEvent(event);
}
void QtScrollWidgetGlow::resizeEvent(
QResizeEvent *event)
{
this->image = QImage(event->size(), QImage::Format_RGB32);
realize();
handleEvent(event, true);
QWidget::resizeEvent(event);
}
void QtScrollWidgetGlow::showEvent(QShowEvent *event)
{
realize();
QWidget::showEvent(event);
} }
void QtScrollWidgetGlow::closeEvent(QCloseEvent *event) void QtScrollWidgetGlow::closeEvent(QCloseEvent *event)
...@@ -249,17 +236,16 @@ void QtScrollWidgetGlow::closeEvent(QCloseEvent *event) ...@@ -249,17 +236,16 @@ void QtScrollWidgetGlow::closeEvent(QCloseEvent *event)
QWidget::closeEvent(event); QWidget::closeEvent(event);
} }
void QtScrollWidgetGlow::handleEvent(QEvent *event, bool conf_scroll, void QtScrollWidgetGlow::handleEvent(QEvent *event)
bool update)
{ {
if (!destroyed) { if (!destroyed) {
if (conf_scroll) { if (event->type() == QEvent::Resize || event->type() == QEvent::Move) {
scroll_configure = 1; scroll_configure = 1;
} }
if (parent_ctx) { if (parent_ctx) {
GlowCtx *ctx = (GlowCtx *) parent_ctx; GlowCtx *ctx = (GlowCtx *) parent_ctx;
GlowDrawQt *drawer = ((GlowDrawQt *) ctx->gdraw); GlowDrawQt *drawer = ((GlowDrawQt *) ctx->gdraw);
if (update) { if (event->type() == QEvent::MouseMove) {
drawer->m_wind->window->update(); drawer->m_wind->window->update();
} }
drawer->event_handler(event); drawer->event_handler(event);
...@@ -267,13 +253,22 @@ void QtScrollWidgetGlow::handleEvent(QEvent *event, bool conf_scroll, ...@@ -267,13 +253,22 @@ void QtScrollWidgetGlow::handleEvent(QEvent *event, bool conf_scroll,
} }
} }
#include <assert.h> bool QtScrollWidgetGlow::event(QEvent *event)
{
if (event->type() == QEvent::Resize) {
this->image = createBuffer(((QResizeEvent *) event)->size());
}
if (!is_realized && (event->type() == QEvent::Show || event->type() == QEvent::Resize)) {
realize();
is_realized = true;
}
handleEvent(event);
return QWidget::event(event);
}
void QtScrollWidgetGlow::realize() void QtScrollWidgetGlow::realize()
{ {
if (is_realized) { debug_print("realize\n");
return;
}
if (!parent_ctx) { if (!parent_ctx) {
if (is_navigator) { if (is_navigator) {
...@@ -282,6 +277,7 @@ void QtScrollWidgetGlow::realize() ...@@ -282,6 +277,7 @@ void QtScrollWidgetGlow::realize()
if (main && !main->is_realized) { if (main && !main->is_realized) {
main->realize(); main->realize();
main->is_realized = true;
} }
assert(main->parent_ctx != NULL); assert(main->parent_ctx != NULL);
...@@ -296,6 +292,4 @@ void QtScrollWidgetGlow::realize() ...@@ -296,6 +292,4 @@ void QtScrollWidgetGlow::realize()
static_cast<glow_eCtxType>(ctxType)); static_cast<glow_eCtxType>(ctxType));
} }
} }
is_realized = true;
} }
\ No newline at end of file
...@@ -45,11 +45,9 @@ ...@@ -45,11 +45,9 @@
#include "glow_ctx.h" #include "glow_ctx.h"
#include "glow_draw_qt.h" #include "glow_draw_qt.h"
#include <QMouseEvent>
#include <QMoveEvent>
#include <QPaintEvent> #include <QPaintEvent>
#include <QResizeEvent>
#include <QScrollBar> #include <QScrollBar>
#include <QScrollArea>
#include <QWidget> #include <QWidget>
typedef struct { typedef struct {
...@@ -61,6 +59,13 @@ typedef struct { ...@@ -61,6 +59,13 @@ typedef struct {
int scroll_v_managed; int scroll_v_managed;
} widget_sScroll; } widget_sScroll;
// QScrollArea does not forward events to its child widget
// So we subclass QScrollArea and override the event() handler
class QScrollAreaGlow : public QScrollArea {
protected:
bool event(QEvent *event);
};
class QtScrollWidgetGlow : public QWidget { class QtScrollWidgetGlow : public QWidget {
Q_OBJECT Q_OBJECT
...@@ -100,22 +105,22 @@ public: ...@@ -100,22 +105,22 @@ public:
QImage image; QImage image;
virtual void handleEvent(QEvent *event);
protected: protected:
virtual void realize(); virtual void realize();
virtual void handleEvent(QEvent *event, bool conf_scroll = false,
bool update = false);
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void moveEvent(QMoveEvent *event);
void resizeEvent(QResizeEvent *event);
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
void showEvent(QShowEvent *event); bool event(QEvent *event);
bool is_realized; bool is_realized;
unsigned int ctxType; unsigned int ctxType;
private:
QImage createBuffer(QSize size);
public slots: public slots:
void scroll_h_action(int value); void scroll_h_action(int value);
void scroll_v_action(int value); void scroll_v_action(int value);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment