1 | /* |
2 | Copyright 2012 Viranch Mehta <viranch.mehta@gmail.com> |
3 | |
4 | This program is free software: you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation, either version 2 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "canvaswidget.h" |
19 | #include "globals.h" |
20 | #include "settings.h" |
21 | |
22 | #include <QGraphicsObject> |
23 | #include <QAction> |
24 | #include <QKeyEvent> |
25 | #include <QCursor> |
26 | |
27 | #include <KStandardDirs> |
28 | #include <KgThemeProvider> |
29 | |
30 | CanvasWidget::CanvasWidget(QWidget *parent) : |
31 | KgDeclarativeView(parent), |
32 | m_provider(new KgThemeProvider) |
33 | { |
34 | m_provider->discoverThemes("appdata" , QLatin1String("themes" )); |
35 | m_provider->setDeclarativeEngine("themeProvider" , engine()); |
36 | QString path = KStandardDirs::locate("appdata" , "qml/main.qml" ); |
37 | setSource(QUrl::fromLocalFile(path)); |
38 | |
39 | // forward signals from QML |
40 | connect(rootObject(), SIGNAL(levelComplete()), this, SIGNAL(levelComplete())); |
41 | connect(rootObject(), SIGNAL(gameEnded(int,int,int)), this, SIGNAL(gameEnded(int,int,int))); |
42 | connect(rootObject(), SIGNAL(mousePressed()), this, SIGNAL(mousePressed())); |
43 | |
44 | // for handling mouse cursor |
45 | connect(rootObject(), SIGNAL(pausedChanged()), this, SLOT(updateCursor())); |
46 | connect(this, SIGNAL(gameEnded(int,int,int)), this, SLOT(resetCursor())); |
47 | } |
48 | |
49 | CanvasWidget::~CanvasWidget() |
50 | { |
51 | delete m_provider; |
52 | } |
53 | |
54 | void CanvasWidget::updateFireShortcut() |
55 | { |
56 | QAction *fireAction = qobject_cast<QAction*>(sender()); |
57 | QString shortcut = fireAction->shortcut().toString(QKeySequence::NativeText); |
58 | rootObject()->setProperty("fireShortcut" , shortcut); |
59 | } |
60 | |
61 | void CanvasWidget::resizeEvent(QResizeEvent *event) |
62 | { |
63 | QDeclarativeView::resizeEvent(event); |
64 | QMetaObject::invokeMethod(rootObject(), "updateGeometry" ); |
65 | } |
66 | |
67 | void CanvasWidget::newGame() |
68 | { |
69 | QMetaObject::invokeMethod(rootObject(), "reset" ); |
70 | |
71 | setCursor(QCursor(Qt::BlankCursor)); |
72 | } |
73 | |
74 | void CanvasWidget::showLine(QString line, int lineNumber) |
75 | { |
76 | QMetaObject::invokeMethod(rootObject(), "loadLine" , |
77 | Q_ARG(QVariant, line), |
78 | Q_ARG(QVariant, lineNumber)); |
79 | } |
80 | |
81 | void CanvasWidget::putGift(QString gift, int times, QString pos) |
82 | { |
83 | QMetaObject::invokeMethod(rootObject(), "loadGift" , |
84 | Q_ARG(QVariant, gift), |
85 | Q_ARG(QVariant, times), |
86 | Q_ARG(QVariant, pos)); |
87 | } |
88 | |
89 | void CanvasWidget::startGame() |
90 | { |
91 | QMetaObject::invokeMethod(rootObject(), "startGame" ); |
92 | } |
93 | |
94 | void CanvasWidget::fire() |
95 | { |
96 | QMetaObject::invokeMethod(rootObject(), "fire" ); |
97 | } |
98 | |
99 | void CanvasWidget::setGamePaused(bool paused) |
100 | { |
101 | QMetaObject::invokeMethod(rootObject(), "setGamePaused" , |
102 | Q_ARG(QVariant, paused)); |
103 | } |
104 | |
105 | void CanvasWidget::updateBarDirection() |
106 | { |
107 | QMetaObject::invokeMethod(rootObject(), "updateBarDirection" , |
108 | Q_ARG(QVariant, m_barDirection)); |
109 | } |
110 | |
111 | void CanvasWidget::resetCursor() |
112 | { |
113 | setCursor(QCursor(Qt::ArrowCursor)); |
114 | } |
115 | |
116 | void CanvasWidget::updateCursor() |
117 | { |
118 | bool paused = rootObject()->property("paused" ).toBool(); |
119 | |
120 | if (paused) { |
121 | resetCursor(); |
122 | } else { |
123 | // FIXME: move the cursor to where the bar is |
124 | resetMousePosition(); |
125 | QCursor newCursor(Qt::BlankCursor); |
126 | newCursor.setPos(cursor().pos()); |
127 | setCursor(newCursor); |
128 | } |
129 | } |
130 | |
131 | void CanvasWidget::resetMousePosition() |
132 | { |
133 | // FIXME: the cursor's position is supposed to be reset, |
134 | // just doesn't work! |
135 | int barPosition = rootObject()->property("barCenter" ).toInt(); |
136 | QPoint p = mapToGlobal(QPoint(barPosition, 0)); |
137 | cursor().setPos(p.x(), cursor().pos().y()); |
138 | } |
139 | |
140 | void CanvasWidget::keyPressEvent(QKeyEvent *event) |
141 | { |
142 | if (event->isAutoRepeat()) { |
143 | QDeclarativeView::keyPressEvent(event); |
144 | return; |
145 | } |
146 | int key = event->key(); |
147 | switch (key) { |
148 | case Qt::Key_Right: |
149 | m_rightPressed = true; |
150 | m_barDirection = 1; |
151 | break; |
152 | case Qt::Key_Left: |
153 | m_leftPressed = true; |
154 | m_barDirection = -1; |
155 | break; |
156 | default: |
157 | QDeclarativeView::keyPressEvent(event); |
158 | return; |
159 | } |
160 | |
161 | updateBarDirection(); |
162 | } |
163 | |
164 | void CanvasWidget::keyReleaseEvent(QKeyEvent *event) |
165 | { |
166 | if (event->isAutoRepeat()) { |
167 | QDeclarativeView::keyReleaseEvent(event); |
168 | return; |
169 | } |
170 | int key = event->key(); |
171 | switch (key) { |
172 | case Qt::Key_Right: |
173 | m_rightPressed = false; |
174 | break; |
175 | case Qt::Key_Left: |
176 | m_leftPressed = false; |
177 | break; |
178 | default: |
179 | QDeclarativeView::keyReleaseEvent(event); |
180 | return; |
181 | } |
182 | |
183 | if (!m_rightPressed && !m_leftPressed) { |
184 | m_barDirection = 0; |
185 | } else if (m_rightPressed && !m_leftPressed) { |
186 | m_barDirection = 1; |
187 | } else if (!m_rightPressed && m_leftPressed) { |
188 | m_barDirection = -1; |
189 | } |
190 | |
191 | updateBarDirection(); |
192 | } |
193 | |
194 | void CanvasWidget::focusOutEvent(QFocusEvent *event) |
195 | { |
196 | emit focusLost(); |
197 | QDeclarativeView::focusOutEvent(event); |
198 | } |
199 | |