From 8b9678ac82c37d8d47e90ea97ef7b5310158a8d8 Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Sat, 7 Apr 2012 23:33:51 +0200 Subject: [PATCH] Qt frontend: make the Canvas class a real QObject. Not that it is required now, but it is at least cleaner. --- qt.cxx | 152 ++++++++++++++++++++++++++++++++--------------------------------- qt.h | 14 ++++++ 2 files changed, 89 insertions(+), 77 deletions(-) diff --git a/qt.cxx b/qt.cxx index 3e20c4f..62ebd92 100644 --- a/qt.cxx +++ b/qt.cxx @@ -113,86 +113,84 @@ static void repaint_rectangle(frontend *fe, QPaintDevice *widget, QRect(x - fe->ox, y - fe->oy, w, h)); } -class Canvas: public QWidget -{ - frontend* fe; -public: - Canvas(frontend* fe): QWidget() { - this->fe = fe; - } -protected: - void paintEvent (QPaintEvent *event) { - const QRect & rect = event->rect(); - repaint_rectangle(fe, fe->area, - rect.x(), rect.y(), - rect.width(), rect.height()); - } - void resizeEvent (QResizeEvent *event) { - int w, h; - - if (backing_store_ok(fe)) - teardown_backing_store(fe); - - w = fe->w = event->size().width(); - h = fe->h = event->size().height(); - midend_size(fe->me, &w, &h, TRUE); - fe->pw = w; - fe->ph = h; - fe->ox = (fe->w - fe->pw) / 2; - fe->oy = (fe->h - fe->ph) / 2; - - setup_backing_store(fe); - midend_force_redraw(fe->me); +Canvas::Canvas(frontend* fe): QWidget(), fe(fe) { } +void Canvas::paintEvent (QPaintEvent *event) +{ + const QRect & rect = event->rect(); + repaint_rectangle(fe, fe->area, + rect.x(), rect.y(), + rect.width(), rect.height()); +} +void Canvas::resizeEvent (QResizeEvent *event) +{ + int w, h; + + if (backing_store_ok(fe)) + teardown_backing_store(fe); + + w = fe->w = event->size().width(); + h = fe->h = event->size().height(); + midend_size(fe->me, &w, &h, TRUE); + fe->pw = w; + fe->ph = h; + fe->ox = (fe->w - fe->pw) / 2; + fe->oy = (fe->h - fe->ph) / 2; + + setup_backing_store(fe); + midend_force_redraw(fe->me); +} +void Canvas::mousePressEvent (QMouseEvent *event) +{ + int button; + if (!backing_store_ok(fe)) + event->ignore(); + switch(event->button()) { + case Qt::LeftButton: button = LEFT_BUTTON; break; + case Qt::RightButton: button = RIGHT_BUTTON; break; + case Qt::MidButton: button = MIDDLE_BUTTON; break; + default: event->ignore(); return; /* don't even know what button! */ } - void mousePressEvent (QMouseEvent *event) { - int button; - if (!backing_store_ok(fe)) - event->ignore(); - switch(event->button()) { - case Qt::LeftButton: button = LEFT_BUTTON; break; - case Qt::RightButton: button = RIGHT_BUTTON; break; - case Qt::MidButton: button = MIDDLE_BUTTON; break; - default: event->ignore(); return; /* don't even know what button! */ - } - event->accept(); - if (!midend_process_key(fe->me, event->x() - fe->ox, - event->y() - fe->oy, button)) - qApp->quit(); - } - void mouseReleaseEvent (QMouseEvent *event) { - int button; - if (!backing_store_ok(fe)) - event->ignore(); - switch(event->button()) { - case Qt::LeftButton: button = LEFT_RELEASE; break; - case Qt::RightButton: button = RIGHT_RELEASE; break; - case Qt::MidButton: button = MIDDLE_RELEASE; break; - default: event->ignore(); return; /* don't even know what button! */ - } - event->accept(); - if (!midend_process_key(fe->me, event->x() - fe->ox, - event->y() - fe->oy, button)) - qApp->quit(); + event->accept(); + if (!midend_process_key(fe->me, event->x() - fe->ox, + event->y() - fe->oy, button)) + qApp->quit(); +} +void Canvas::mouseReleaseEvent (QMouseEvent *event) +{ + int button; + if (!backing_store_ok(fe)) + event->ignore(); + switch(event->button()) { + case Qt::LeftButton: button = LEFT_RELEASE; break; + case Qt::RightButton: button = RIGHT_RELEASE; break; + case Qt::MidButton: button = MIDDLE_RELEASE; break; + default: event->ignore(); return; /* don't even know what button! */ } - void mouseMoveEvent (QMouseEvent *event) { - int button; - Qt::MouseButtons buttons = event->buttons(); - if (buttons.testFlag(Qt::MidButton)) - button = MIDDLE_DRAG; - else if (buttons.testFlag(Qt::LeftButton)) - button = LEFT_DRAG; - else if (buttons.testFlag(Qt::RightButton)) - button = RIGHT_DRAG; - else { - event->ignore(); /* don't even know what button! */ - return; - } - event->accept(); - if (!midend_process_key(fe->me, event->x() - fe->ox, - event->y() - fe->oy, button)) - qApp->quit(); + event->accept(); + if (!midend_process_key(fe->me, event->x() - fe->ox, + event->y() - fe->oy, button)) + qApp->quit(); +} +void Canvas::mouseMoveEvent (QMouseEvent *event) +{ + int button; + Qt::MouseButtons buttons = event->buttons(); + if (buttons.testFlag(Qt::MidButton)) + button = MIDDLE_DRAG; + else if (buttons.testFlag(Qt::LeftButton)) + button = LEFT_DRAG; + else if (buttons.testFlag(Qt::RightButton)) + button = RIGHT_DRAG; + else { + event->ignore(); /* don't even know what button! */ + return; } -}; + event->accept(); + if (!midend_process_key(fe->me, event->x() - fe->ox, + event->y() - fe->oy, button)) + qApp->quit(); +} + void get_random_seed(void **randseed, int *randseedsize) { diff --git a/qt.h b/qt.h index bf06dba..8d3af26 100644 --- a/qt.h +++ b/qt.h @@ -23,6 +23,20 @@ struct frontend { int ox, oy; /* offset of pixmap in drawing area */ }; +class Canvas: public QWidget +{ + Q_OBJECT; + frontend* fe; + public: + Canvas(frontend* fe); + protected: + void paintEvent (QPaintEvent *event); + void resizeEvent (QResizeEvent *event); + void mousePressEvent (QMouseEvent *event); + void mouseReleaseEvent (QMouseEvent *event); + void mouseMoveEvent (QMouseEvent *event); +}; + class PuzzleWindow: public QMainWindow { Q_OBJECT; frontend *fe; -- 2.11.4.GIT