Changeset View
Changeset View
Standalone View
Standalone View
src/canvaswidget.cpp
Show All 40 Lines | 34 | { | |||
---|---|---|---|---|---|
41 | setSource(QUrl::fromLocalFile(path)); | 41 | setSource(QUrl::fromLocalFile(path)); | ||
42 | 42 | | |||
43 | // forward signals from QML | 43 | // forward signals from QML | ||
44 | connect(rootObject(), SIGNAL(levelComplete()), this, SIGNAL(levelComplete())); | 44 | connect(rootObject(), SIGNAL(levelComplete()), this, SIGNAL(levelComplete())); | ||
45 | connect(rootObject(), SIGNAL(gameEnded(int,int,int)), this, SIGNAL(gameEnded(int,int,int))); | 45 | connect(rootObject(), SIGNAL(gameEnded(int,int,int)), this, SIGNAL(gameEnded(int,int,int))); | ||
46 | connect(rootObject(), SIGNAL(mousePressed()), this, SIGNAL(mousePressed())); | 46 | connect(rootObject(), SIGNAL(mousePressed()), this, SIGNAL(mousePressed())); | ||
47 | 47 | | |||
48 | // for handling mouse cursor | 48 | // for handling mouse cursor | ||
49 | connect(rootObject(), SIGNAL(pausedChanged()), this, SLOT(updateCursor())); | 49 | connect(rootObject(), SIGNAL(ballMovingChanged()), this, SLOT(updateCursor())); | ||
50 | connect(this, &CanvasWidget::gameEnded, this, &CanvasWidget::resetCursor); | | |||
51 | } | 50 | } | ||
52 | 51 | | |||
53 | CanvasWidget::~CanvasWidget() | 52 | CanvasWidget::~CanvasWidget() | ||
54 | { | 53 | { | ||
55 | delete m_provider; | 54 | delete m_provider; | ||
56 | } | 55 | } | ||
57 | 56 | | |||
58 | void CanvasWidget::updateFireShortcut() | 57 | void CanvasWidget::updateFireShortcut() | ||
59 | { | 58 | { | ||
60 | QAction *fireAction = qobject_cast<QAction *>(sender()); | 59 | QAction *fireAction = qobject_cast<QAction *>(sender()); | ||
61 | QString shortcut = fireAction->shortcut().toString(QKeySequence::NativeText); | 60 | QString shortcut = fireAction->shortcut().toString(QKeySequence::NativeText); | ||
62 | rootObject()->setProperty("fireShortcut", shortcut); | 61 | rootObject()->setProperty("fireShortcut", shortcut); | ||
63 | } | 62 | } | ||
64 | 63 | | |||
65 | void CanvasWidget::resizeEvent(QResizeEvent *event) | 64 | void CanvasWidget::resizeEvent(QResizeEvent *event) | ||
66 | { | 65 | { | ||
67 | QQuickWidget::resizeEvent(event); | 66 | QQuickWidget::resizeEvent(event); | ||
68 | QMetaObject::invokeMethod(rootObject(), "updateGeometry"); | 67 | QMetaObject::invokeMethod(rootObject(), "updateGeometry"); | ||
69 | } | 68 | } | ||
70 | 69 | | |||
70 | bool CanvasWidget::event(QEvent *event) | ||||
71 | { | ||||
72 | if (event->type() == QEvent::Leave) { | ||||
73 | updateCursor(); | ||||
74 | } | ||||
75 | return KgDeclarativeView::event(event); | ||||
76 | } | ||||
77 | | ||||
71 | void CanvasWidget::newGame() | 78 | void CanvasWidget::newGame() | ||
72 | { | 79 | { | ||
73 | QMetaObject::invokeMethod(rootObject(), "reset"); | 80 | QMetaObject::invokeMethod(rootObject(), "reset"); | ||
74 | | ||||
75 | setCursor(QCursor(Qt::BlankCursor)); | | |||
76 | } | 81 | } | ||
77 | 82 | | |||
78 | void CanvasWidget::showLine(const QString &line, int lineNumber) | 83 | void CanvasWidget::showLine(const QString &line, int lineNumber) | ||
79 | { | 84 | { | ||
80 | QMetaObject::invokeMethod(rootObject(), "loadLine", | 85 | QMetaObject::invokeMethod(rootObject(), "loadLine", | ||
81 | Q_ARG(QVariant, line), | 86 | Q_ARG(QVariant, line), | ||
82 | Q_ARG(QVariant, lineNumber)); | 87 | Q_ARG(QVariant, lineNumber)); | ||
83 | } | 88 | } | ||
Show All 33 Lines | |||||
117 | } | 122 | } | ||
118 | 123 | | |||
119 | void CanvasWidget::updateBarDirection() | 124 | void CanvasWidget::updateBarDirection() | ||
120 | { | 125 | { | ||
121 | QMetaObject::invokeMethod(rootObject(), "updateBarDirection", | 126 | QMetaObject::invokeMethod(rootObject(), "updateBarDirection", | ||
122 | Q_ARG(QVariant, m_barDirection)); | 127 | Q_ARG(QVariant, m_barDirection)); | ||
123 | } | 128 | } | ||
124 | 129 | | |||
125 | void CanvasWidget::resetCursor() | | |||
126 | { | | |||
127 | setCursor(QCursor(Qt::ArrowCursor)); | | |||
128 | } | | |||
129 | | ||||
130 | void CanvasWidget::updateCursor() | 130 | void CanvasWidget::updateCursor() | ||
131 | { | 131 | { | ||
132 | bool paused = rootObject()->property("paused").toBool(); | 132 | const bool ballMoving = rootObject()->property("ballMoving").toBool(); | ||
133 | 133 | | |||
134 | if (paused) { | 134 | if (ballMoving) { | ||
135 | resetCursor(); | | |||
136 | } else { | | |||
137 | // FIXME: move the cursor to where the bar is | | |||
138 | resetMousePosition(); | 135 | resetMousePosition(); | ||
139 | QCursor newCursor(Qt::BlankCursor); | 136 | setCursor(Qt::BlankCursor); | ||
140 | newCursor.setPos(cursor().pos()); | 137 | } else { | ||
141 | setCursor(newCursor); | 138 | setCursor(Qt::ArrowCursor); | ||
142 | } | 139 | } | ||
143 | } | 140 | } | ||
144 | 141 | | |||
145 | void CanvasWidget::resetMousePosition() | 142 | void CanvasWidget::resetMousePosition() | ||
146 | { | 143 | { | ||
147 | // FIXME: the cursor's position is supposed to be reset, | 144 | const QQuickItem *jailItem = rootObject()->property("jailItem").value<QQuickItem*>(); | ||
148 | // just doesn't work! | 145 | const QPoint jailItemTopLeft = mapToGlobal(QPoint(jailItem->x(), jailItem->y())); | ||
149 | int barPosition = rootObject()->property("barCenter").toInt(); | 146 | QPoint p = QCursor::pos(); | ||
150 | QPoint p = mapToGlobal(QPoint(barPosition, 0)); | 147 | if (p.x() < jailItemTopLeft.x()) { | ||
151 | cursor().setPos(p.x(), cursor().pos().y()); | 148 | QCursor::setPos(jailItemTopLeft.x(), p.y()); | ||
149 | } else if (p.x() > jailItemTopLeft.x() + jailItem->width()) { | ||||
150 | QCursor::setPos(jailItemTopLeft.x() + jailItem->width(), p.y()); | ||||
151 | } | ||||
152 | | ||||
153 | if (p.y() < jailItemTopLeft.y()) { | ||||
154 | QCursor::setPos(p.x(), jailItemTopLeft.y()); | ||||
155 | } else if (p.y() > jailItemTopLeft.y() + jailItem->height()) { | ||||
156 | QCursor::setPos(p.x(), jailItemTopLeft.y() + jailItem->height()); | ||||
157 | } | ||||
152 | } | 158 | } | ||
153 | 159 | | |||
154 | void CanvasWidget::focusOutEvent(QFocusEvent *event) | 160 | void CanvasWidget::focusOutEvent(QFocusEvent *event) | ||
155 | { | 161 | { | ||
156 | emit focusLost(); | 162 | emit focusLost(); | ||
157 | QQuickWidget::focusOutEvent(event); | 163 | QQuickWidget::focusOutEvent(event); | ||
158 | } | 164 | } |