- implemented the swipe and touch logic in QML

- Added a SHA1 hash verification of the downloaded resource file
- Fixed the activation/deactivation of the QML UI to avoid burning the battery
This commit is contained in:
Xawotihs
2012-01-07 20:41:43 +00:00
parent 436f95dfec
commit 26738af904
5 changed files with 105 additions and 40 deletions

View File

@@ -49,6 +49,12 @@ public:
}
};
Q_INVOKABLE void pixelInput(int x, int y);
Q_INVOKABLE qint64 getTick() {
return g_startTimer.elapsed();
};
Q_INVOKABLE void doScroll(int x, int y) {
m_engine->Scroll(x, y);
};
int getNominalHeight(){ return SCREEN_HEIGHT;};
int getNominalWidth(){ return SCREEN_WIDTH;};
float getNominalRatio() { return ((float)SCREEN_WIDTH / (float)SCREEN_HEIGHT);};

View File

@@ -13,38 +13,38 @@
class FileDownloader : public QObject
{
Q_OBJECT
Q_PROPERTY(bool done READ isDone NOTIFY downloaded)
Q_PROPERTY(qint64 received READ received NOTIFY receivedChanged)
Q_PROPERTY(QUrl url READ getDownloadUrl WRITE setDownloadUrl NOTIFY downloadUrlChanged)
Q_PROPERTY(QString hash READ getHash NOTIFY hashChanged)
public:
explicit FileDownloader(QString localPath, QUrl url=QUrl(""), QObject *parent = 0);
explicit FileDownloader(QString localPath, QObject *parent = 0);
virtual ~FileDownloader();
qint64 received() const {return m_received;};
bool isDone() {return m_done;};
QUrl getDownloadUrl() {return m_downloadUrl;};
QString getHash() {return m_hash;};
signals:
void downloaded();
void receivedChanged();
void downloadUrlChanged();
void hashChanged();
private slots:
void fileDownloaded(QNetworkReply* pReply){
if(m_tmp.write(pReply->readAll()) == -1) return;
if(QFile(m_localPath).exists())
QFile::remove(m_localPath);
if(!m_tmp.rename(m_localPath)) return;
computeHash(m_tmp);
m_tmp.setAutoRemove(false);
m_done = true;
//emit a signal
emit downloaded();
};
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal){
m_received = bytesReceived*100/bytesTotal;
emit receivedChanged();
};
void setDownloadUrl(QUrl url);
void computeHash(QFile& file);
private:
@@ -53,8 +53,8 @@ private:
QTemporaryFile m_tmp;
QString m_localPath;
QUrl m_downloadUrl;
QString m_hash;
bool m_OK;
bool m_done;
};
QML_DECLARE_TYPEINFO(FileDownloader, QML_HAS_ATTACHED_PROPERTIES)

View File

@@ -1,3 +1,25 @@
#include <qplatformdefs.h>
#include <QtOpenGL>
#if (defined FORCE_GLES)
#undef GL_ES_VERSION_2_0
#undef GL_VERSION_2_0
#define GL_VERSION_ES_CM_1_1 1
#ifndef GL_OES_VERSION_1_1
#define glOrthof glOrtho
#define glClearDepthf glClearDepth
#endif
#endif
#if (defined FORCE_GLES)
#undef GL_ES_VERSION_2_0
#undef GL_VERSION_2_0
#define GL_VERSION_ES_CM_1_1 1
#define glOrthof glOrtho
#define glClearDepthf glClearDepth
#endif
#include "corewrapper.h"
#define ACTUAL_SCREEN_WIDTH (SCREEN_WIDTH)
@@ -38,10 +60,6 @@ WagicCore::WagicCore(QDeclarativeItem *parent) :
setWidth(480);
setHeight(272);
// BindKey is a static method, m_engine is not even initialized
for (signed int i = sizeof(gDefaultBindings)/sizeof(gDefaultBindings[0]) - 1; i >= 0; --i)
m_engine->BindKey(gDefaultBindings[i].keysym, gDefaultBindings[i].keycode);
g_startTimer.restart();
m_lastTickCount = g_startTimer.elapsed();
}
@@ -125,7 +143,7 @@ void WagicCore::setActive(bool active)
if(!m_active && active)
{
m_engine->Resume();
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
#if (defined Q_WS_MAEMO_5) || defined(MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
// 30 fps max on mobile
m_timerId = startTimer(33);
#else
@@ -138,8 +156,8 @@ void WagicCore::setActive(bool active)
else if(m_active && !active)
{
m_engine->Pause();
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
killTimer(timerId);
#if (defined Q_WS_MAEMO_5) || defined(MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
killTimer(m_timerId);
#endif
m_active = active;
emit activeChanged();
@@ -250,11 +268,11 @@ void WagicCore::keyPressEvent(QKeyEvent *event)
#if (defined Q_WS_MAEMO_5)
case Qt::Key_F7:
/* interrupt please */
g_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
m_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
break;
case Qt::Key_F8:
/* next phase please */
g_engine->HoldKey_NoRepeat(JGE_BTN_PREV);
m_engine->HoldKey_NoRepeat(JGE_BTN_PREV);
break;
#endif // Q_WS_MAEMO_5
case Qt::Key_F:

View File

@@ -1,8 +1,9 @@
#include "filedownloader.h"
#include <QDir>
#include <QCryptographicHash>
FileDownloader::FileDownloader(QString localPath, QUrl url, QObject *parent) :
QObject(parent), m_received(0), m_OK(false), m_done(false)
FileDownloader::FileDownloader(QString localPath, QObject *parent) :
QObject(parent), m_received(0), m_hash(""), m_OK(false)
{
QDir dir(QDir::homePath());
@@ -16,25 +17,28 @@ FileDownloader::FileDownloader(QString localPath, QUrl url, QObject *parent) :
QFile local(m_localPath);
if(local.exists()) {
m_done = true;
return;
computeHash(local);
}
if(!url.isEmpty())
setDownloadUrl(url);
}
void FileDownloader::setDownloadUrl(QUrl url)
{
connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
SLOT(fileDownloaded(QNetworkReply*)));
if((!url.isEmpty()) && url.toString() != m_downloadUrl.toString())
{
connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
SLOT(fileDownloaded(QNetworkReply*)));
QNetworkRequest request(url);
QNetworkReply* reply = m_WebCtrl.get(request);
QNetworkRequest request(url);
QNetworkReply* reply = m_WebCtrl.get(request);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
SLOT(downloadProgress(qint64, qint64)));
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
SLOT(downloadProgress(qint64, qint64)));
m_OK = m_tmp.open();
m_OK = m_tmp.open();
m_downloadUrl.setUrl(url.toString());
emit downloadUrlChanged();
}
}
@@ -42,3 +46,16 @@ FileDownloader::~FileDownloader()
{
}
void FileDownloader::computeHash(QFile& file)
{
QCryptographicHash crypto(QCryptographicHash::Sha1);
file.open(QFile::ReadOnly);
while(!file.atEnd()){
crypto.addData(file.read(8192));
}
QByteArray hash = crypto.result();
m_hash = hash.toHex();
emit hashChanged();
}

View File

@@ -7,10 +7,13 @@ Rectangle {
height: 272
state: "DOWNLOADING"
color: "black"
property url resource: "http://wagic.googlecode.com/files/core_017.zip"
property string hash: "cc6f9415f747acea500cdce190f0df6ee41db7cb"
states: [
State {
name: "DOWNLOADING"
when: (fileDownloader.hash != hash)
PropertyChanges {
target: column1; visible: true
}
@@ -18,12 +21,12 @@ Rectangle {
target: wagic; visible: false
}
PropertyChanges {
target:fileDownloader; url: "http://wagic.googlecode.com/files/core_017.zip"
target:fileDownloader; url: resource
}
},
State {
name: "NORMAL"
when: (fileDownloader.done == true)
when: (fileDownloader.hash == hash)
PropertyChanges {
target: column1; visible: false
}
@@ -72,7 +75,7 @@ Rectangle {
id: wagic
anchors.fill: parent
visible: false
active: Qt.WindowActive
active: Qt.application.active
}
/*
Rectangle {
@@ -87,6 +90,9 @@ Rectangle {
hoverEnabled: true
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton
property int lastTick: 0
property int lastX: 0
property int lastY: 0
onPositionChanged: {
wagic.pixelInput(
@@ -95,18 +101,36 @@ Rectangle {
}
onPressed: {
if(mouse.button == Qt.LeftButton)
wagic.doOK()
else if(mouse.button == Qt.MiddleButton)
wagic.pixelInput(
(mouse.x*wagic.nominalWidth)/width,
(mouse.y*wagic.nominalHeight)/height)
if(mouse.button == Qt.LeftButton) {
lastTick = wagic.getTick()
lastX = mouse.x
lastY = mouse.y
} else if(mouse.button == Qt.MiddleButton)
wagic.doCancel()
else if(mouse.button == Qt.RightButton)
wagic.doNext()
}
onReleased: {
if(mouse.button == Qt.LeftButton)
wagic.done()
else if(mouse.button == Qt.MiddleButton)
if(mouse.button == Qt.LeftButton) {
var currentTick = wagic.getTick()
if(currentTick - lastTick <= 250 )
{
if(Math.abs(lastX-mouse.x) < 50 && Math.abs(lastY-mouse.y) < 50 )
{
wagic.doOK()
// wagic.done()
}
}
else if(currentTick - lastTick >= 250)
{
wagic.doScroll(mouse.x - lastX, mouse.y - lastY)
}
} else if(mouse.button == Qt.MiddleButton)
wagic.done()
else if(mouse.button == Qt.RightButton)
wagic.done()