diff --git a/src/canvaswidget.cpp b/src/canvaswidget.cpp index 644c3fa..343fece 100644 --- a/src/canvaswidget.cpp +++ b/src/canvaswidget.cpp @@ -1,158 +1,158 @@ /* Copyright 2012 Viranch Mehta This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "canvaswidget.h" #include "globals.h" #include "kbreakout_debug.h" #include "settings.h" #include #include #include #include #include #include CanvasWidget::CanvasWidget(QWidget *parent) : KgDeclarativeView(parent), m_provider(new KgThemeProvider) { m_provider->discoverThemes("appdata", QStringLiteral("themes")); m_provider->setDeclarativeEngine(QStringLiteral("themeProvider"), engine()); QString path = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("qml/main.qml")); qCDebug(KBREAKOUT_General) << "QtQuick QML file: " << path; setSource(QUrl::fromLocalFile(path)); // forward signals from QML connect(rootObject(), SIGNAL(levelComplete()), this, SIGNAL(levelComplete())); connect(rootObject(), SIGNAL(gameEnded(int,int,int)), this, SIGNAL(gameEnded(int,int,int))); connect(rootObject(), SIGNAL(mousePressed()), this, SIGNAL(mousePressed())); // for handling mouse cursor connect(rootObject(), SIGNAL(pausedChanged()), this, SLOT(updateCursor())); connect(this, &CanvasWidget::gameEnded, this, &CanvasWidget::resetCursor); } CanvasWidget::~CanvasWidget() { delete m_provider; } void CanvasWidget::updateFireShortcut() { QAction *fireAction = qobject_cast(sender()); QString shortcut = fireAction->shortcut().toString(QKeySequence::NativeText); rootObject()->setProperty("fireShortcut", shortcut); } void CanvasWidget::resizeEvent(QResizeEvent *event) { QQuickWidget::resizeEvent(event); QMetaObject::invokeMethod(rootObject(), "updateGeometry"); } void CanvasWidget::newGame() { QMetaObject::invokeMethod(rootObject(), "reset"); setCursor(QCursor(Qt::BlankCursor)); } -void CanvasWidget::showLine(QString line, int lineNumber) +void CanvasWidget::showLine(const QString &line, int lineNumber) { QMetaObject::invokeMethod(rootObject(), "loadLine", Q_ARG(QVariant, line), Q_ARG(QVariant, lineNumber)); } -void CanvasWidget::putGift(QString gift, int times, QString pos) +void CanvasWidget::putGift(const QString &gift, int times, const QString &pos) { QMetaObject::invokeMethod(rootObject(), "loadGift", Q_ARG(QVariant, gift), Q_ARG(QVariant, times), Q_ARG(QVariant, pos)); } void CanvasWidget::startGame() { QMetaObject::invokeMethod(rootObject(), "startGame"); } void CanvasWidget::fire() { QMetaObject::invokeMethod(rootObject(), "fire"); } void CanvasWidget::cheatSkipLevel() { QMetaObject::invokeMethod(rootObject(), "cheatSkipLevel"); } void CanvasWidget::cheatAddLife() { QMetaObject::invokeMethod(rootObject(), "cheatAddLife"); } void CanvasWidget::setGamePaused(bool paused) { QMetaObject::invokeMethod(rootObject(), "setGamePaused", Q_ARG(QVariant, paused)); } void CanvasWidget::updateBarDirection() { QMetaObject::invokeMethod(rootObject(), "updateBarDirection", Q_ARG(QVariant, m_barDirection)); } void CanvasWidget::resetCursor() { setCursor(QCursor(Qt::ArrowCursor)); } void CanvasWidget::updateCursor() { bool paused = rootObject()->property("paused").toBool(); if (paused) { resetCursor(); } else { // FIXME: move the cursor to where the bar is resetMousePosition(); QCursor newCursor(Qt::BlankCursor); newCursor.setPos(cursor().pos()); setCursor(newCursor); } } void CanvasWidget::resetMousePosition() { // FIXME: the cursor's position is supposed to be reset, // just doesn't work! int barPosition = rootObject()->property("barCenter").toInt(); QPoint p = mapToGlobal(QPoint(barPosition, 0)); cursor().setPos(p.x(), cursor().pos().y()); } void CanvasWidget::focusOutEvent(QFocusEvent *event) { emit focusLost(); QQuickWidget::focusOutEvent(event); } diff --git a/src/canvaswidget.h b/src/canvaswidget.h index 9808551..b3a9f69 100644 --- a/src/canvaswidget.h +++ b/src/canvaswidget.h @@ -1,71 +1,71 @@ /* Copyright 2012 Viranch Mehta This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef CANVASWIDGET_H #define CANVASWIDGET_H #include class KgThemeProvider; class CanvasWidget : public KgDeclarativeView { Q_OBJECT public: explicit CanvasWidget(QWidget *parent = nullptr); ~CanvasWidget() override; KgThemeProvider *getProvider() { return m_provider; } Q_SIGNALS: void levelComplete(); void gameEnded(int score, int level, int elapsedTime); void focusLost(); void mousePressed(); public Q_SLOTS: void fire(); void cheatSkipLevel(); void cheatAddLife(); void setGamePaused(bool paused); void updateFireShortcut(); public Q_SLOTS: void newGame(); - void showLine(QString line, int lineNumber); - void putGift(QString gift, int times, QString pos); + void showLine(const QString &line, int lineNumber); + void putGift(const QString &gift, int times, const QString &pos); void updateBarDirection(); void startGame(); void updateCursor(); void resetCursor(); void resetMousePosition(); protected: void focusOutEvent(QFocusEvent *event) override; void resizeEvent(QResizeEvent *event) override; private: //used when moving the bar with the keys int m_barDirection; // used to track which direction keys are pressed between key events bool m_rightPressed; bool m_leftPressed; KgThemeProvider *m_provider; }; #endif //CANVASWIDGET_H