diff options
Diffstat (limited to 'examples/widgets/symbianvibration')
-rw-r--r-- | examples/widgets/symbianvibration/main.cpp | 14 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/mainwindow.cpp | 23 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/mainwindow.h | 23 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/symbianvibration.pro | 39 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/vibrationsurface.cpp | 117 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/vibrationsurface.h | 31 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/xqvibra.cpp | 170 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/xqvibra.h | 61 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/xqvibra_p.cpp | 131 | ||||
-rw-r--r-- | examples/widgets/symbianvibration/xqvibra_p.h | 39 |
10 files changed, 648 insertions, 0 deletions
diff --git a/examples/widgets/symbianvibration/main.cpp b/examples/widgets/symbianvibration/main.cpp new file mode 100644 index 0000000..015ed54 --- /dev/null +++ b/examples/widgets/symbianvibration/main.cpp @@ -0,0 +1,14 @@ +#include <QtGui/QApplication> +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; +#if defined(Q_WS_S60) + w.showMaximized(); +#else + w.show(); +#endif + return a.exec(); +} diff --git a/examples/widgets/symbianvibration/mainwindow.cpp b/examples/widgets/symbianvibration/mainwindow.cpp new file mode 100644 index 0000000..67cf220 --- /dev/null +++ b/examples/widgets/symbianvibration/mainwindow.cpp @@ -0,0 +1,23 @@ +#include <QtGui/QMenuBar> +#include "mainwindow.h" +#include "vibrationsurface.h" +#include "XQVibra.h" + +//! [0] +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + vibra = new XQVibra(this); + setCentralWidget(new VibrationSurface(vibra, this)); + menuBar()->addAction(tr("Vibrate"), this, SLOT(vibrate())); +} +//! [0] + +//! [1] +void MainWindow::vibrate() +{ + vibra->setIntensity(75); + vibra->start(2500); +} +//! [1] + diff --git a/examples/widgets/symbianvibration/mainwindow.h b/examples/widgets/symbianvibration/mainwindow.h new file mode 100644 index 0000000..cc77f7b --- /dev/null +++ b/examples/widgets/symbianvibration/mainwindow.h @@ -0,0 +1,23 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QtGui/QMainWindow> +class XQVibra; + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +private slots: + void vibrate(); + +private: + XQVibra *vibra; +}; +//! [0] + +#endif // MAINWINDOW_H diff --git a/examples/widgets/symbianvibration/symbianvibration.pro b/examples/widgets/symbianvibration/symbianvibration.pro new file mode 100644 index 0000000..d99b76d --- /dev/null +++ b/examples/widgets/symbianvibration/symbianvibration.pro @@ -0,0 +1,39 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2010-06-14T10:09:27 +# +#------------------------------------------------- + +QT += core gui + +TARGET = symbianvibration +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + vibrationsurface.cpp \ + xqvibra.cpp \ + xqvibra_p.cpp + +HEADERS += mainwindow.h \ + vibrationsurface.h \ + xqvibra.h \ + xqvibra_p.h + +CONFIG += mobility +MOBILITY = + +symbian { + TARGET.UID3 = 0xecf47018 + # TARGET.CAPABILITY += + TARGET.EPOCSTACKSIZE = 0x14000 + TARGET.EPOCHEAPSIZE = 0x020000 0x800000 + LIBS += -lhwrmvibraclient + include($$PWD/../../symbianpkgrules.pri) +} + +!symbian { + error(The Symbian Vibration Example only works for the Symbian target!) +} + diff --git a/examples/widgets/symbianvibration/vibrationsurface.cpp b/examples/widgets/symbianvibration/vibrationsurface.cpp new file mode 100644 index 0000000..5e2e962 --- /dev/null +++ b/examples/widgets/symbianvibration/vibrationsurface.cpp @@ -0,0 +1,117 @@ +#include "vibrationsurface.h" +#include <QtGui/QPainter> +#include <QtCore/QLine> +#include <QtGui/QMouseEvent> +#include <QtGui/QScreen> +#include <QtCore/QRect> +#include <QtGui/QColor> + +#include "xqvibra.h" + +//! [4] +const int NumberOfLevels = 10; +const double IntensityFactor = XQVibra::MaxIntensity / NumberOfLevels; +//! [4] + +VibrationSurface::VibrationSurface(XQVibra *vibra, QWidget *parent) : + QWidget(parent), + vibra(vibra), + lastIntensity(0) +{ +} + +//! [0] +void VibrationSurface::mousePressEvent(QMouseEvent *event) +{ + applyIntensity(event->x(), event->y()); + vibra->start(); +} +//! [0] + +//! [1] +void VibrationSurface::mouseMoveEvent(QMouseEvent *event) +{ + applyIntensity(event->x(), event->y()); +} +//! [1] + +//! [2] +void VibrationSurface::mouseReleaseEvent(QMouseEvent *) +{ + vibra->stop(); +} +//! [2] + +//! [5] +void VibrationSurface::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + QRect rect = geometry(); + int dx = 0, dy = 0; + + if (height() > width()) { + dy = height() / NumberOfLevels; + rect.setHeight(dy); + } else { + dx = width() / NumberOfLevels; + rect.setWidth(dx); + } +//! [5] +//! [6] + for (int i = 0; i < NumberOfLevels; i++) { + int x = i * dx; + int y = i * dy; + int intensity = getIntensity(x, y); + QColor color = QColor(40, 80, 10).lighter(100 + intensity); + + rect.moveTo(x, y); + painter.fillRect(rect, color); + painter.setPen(color.darker()); + painter.drawText(rect, Qt::AlignCenter, QString::number(intensity)); + } +} +//! [6] + +//! [7] +int VibrationSurface::getIntensity(int x, int y) +{ + int level; + int coord; + + if (height() > width()) { + level = height() / NumberOfLevels; + coord = y; + } else { + level = width() / NumberOfLevels; + coord = x; + } + + if (level == 0) { + return 0; + } +//! [7] +//! [8] + int intensity = (coord / level + 1) * IntensityFactor; + + if (intensity < 0) { + intensity = 0; + } else if (intensity > XQVibra::MaxIntensity) { + intensity = XQVibra::MaxIntensity; + } + + return intensity; +} +//! [8] + +//! [3] +void VibrationSurface::applyIntensity(int x, int y) +{ + int intensity = getIntensity(x, y); + + if (intensity != lastIntensity) { + vibra->setIntensity(intensity); + lastIntensity = intensity; + } +} +//! [3] + diff --git a/examples/widgets/symbianvibration/vibrationsurface.h b/examples/widgets/symbianvibration/vibrationsurface.h new file mode 100644 index 0000000..eee6291 --- /dev/null +++ b/examples/widgets/symbianvibration/vibrationsurface.h @@ -0,0 +1,31 @@ +#ifndef TOUCHAREA_H +#define TOUCHAREA_H + +#include <QWidget> + +class XQVibra; + +//! [0] +class VibrationSurface : public QWidget +{ + Q_OBJECT +public: + explicit VibrationSurface(XQVibra *vibra, QWidget *parent = 0); + +protected: + virtual void mousePressEvent(QMouseEvent *); + virtual void mouseMoveEvent(QMouseEvent *); + virtual void mouseReleaseEvent(QMouseEvent *); + virtual void paintEvent(QPaintEvent *); + +private: + + int getIntensity(int x, int y); + void applyIntensity(int x, int y); + + XQVibra *vibra; + int lastIntensity; +}; +//! [0] + +#endif // TOUCHAREA_H diff --git a/examples/widgets/symbianvibration/xqvibra.cpp b/examples/widgets/symbianvibration/xqvibra.cpp new file mode 100644 index 0000000..1263c3e --- /dev/null +++ b/examples/widgets/symbianvibration/xqvibra.cpp @@ -0,0 +1,170 @@ +#include "xqvibra.h" +#include "xqvibra_p.h" + +/*! + \class XQVibra + + \brief The XQVibra class is used to control the device's vibra. The XQVibra + class provides also information of vibration setting in the user profile. + + Example: + \code + XQVibra *vibra = new XQVibra(this); + QPushButton *startButton = new QPushButton(this); + QPushButton *stopButton = new QPushButton(this); + connect(startButton, SIGNAL(clicked()), vibra, SLOT(start())); + connect(stopButton, SIGNAL(clicked()), vibra, SLOT(stop())); + \endcode +*/ + +/*! \var XQVibra::InfiniteDuration + With this constant vibration can be set to work indefinitely (Note! Depends on the HW) +*/ +/*! \var XQVibra::MaxIntensity + Maximum intensity as percentages +*/ +/*! \var XQVibra::MinIntensity + Minumum intensity as percentages +*/ + +/*! + Constructs a XQVibra object with the given parent. + Call error() to get a value of XQVibra::Error that indicates which error occurred during initialisation if any. + \sa start(), setIntensity(), error() +*/ +XQVibra::XQVibra(QObject *parent) + : QObject(parent), d(new XQVibraPrivate(this)) +{ +} + +/*! + Destroys the XQVibra object. +*/ +XQVibra::~XQVibra() +{ + delete d; +} + +/*! + \enum XQVibra::Error + + This enum defines the possible errors for a XQVibra object. +*/ +/*! \var XQVibra::Error XQVibra::NoError + No error occured. +*/ +/*! \var XQVibra::Error XQVibra::OutOfMemoryError + Not enough memory. +*/ +/*! \var XQVibra::Error XQVibra::ArgumentError + Duration is invalid. +*/ +/*! \var XQVibra::Error XQVibra::VibraInUseError + Vibra is already in used by other client. +*/ +/*! \var XQVibra::Error XQVibra::HardwareError + There is a hardware error. +*/ +/*! \var XQVibra::Error XQVibra::TimeOutError + Timeout occurred in controlling vibra. +*/ +/*! \var XQVibra::Error XQVibra::VibraLockedError + Vibra is locked down because too much continuous use or explicitly blocked by + for example some vibration sensitive accessory. +*/ +/*! \var XQVibra::Error XQVibra::AccessDeniedError + Vibration setting in the user profile is not set. +*/ +/*! \var XQVibra::Error XQVibra::UnknownError + Unknown error. +*/ + +/*! + \enum XQVibra::Status + + This enum defines the possible statuses of the vibration +*/ +/*! \var XQVibra::Status XQVibra::StatusNotAllowed + Vibra is set off in the user profile or status is unknow +*/ +/*! \var XQVibra::Status XQVibra::StatusOff + Vibration is non-active +*/ +/*! \var XQVibra::Status XQVibra::StatusOn + Vibration is active +*/ + +/*! + Starts vibrating. If duration hasn't been set the vibration continues + indefinitely unless stopped with stop() function. Calling the start while vibration + is active the active vibration is interrupted and the new vibration starts immediately. + + \param duration Specifies duration how long vibration should last + \return If false is returned, an error has occurred. Call error() to get a value of + XQVibra::Error that indicates which error occurred + \sa stop(), setIntensity(), error() +*/ +bool XQVibra::start(int duration) +{ + return d->start(duration); +} + +/*! + Interrupts the device vibration immediately. + + \return If false is returned, an error has occurred. Call error() to get a value of + XQVibra::Error that indicates which error occurred + \sa start(), setIntensity(), error() +*/ +bool XQVibra::stop() +{ + return d->stop(); +} + +/*! + Sets the intensity of the vibration. Allowed values for the intensity are + between -100 and 100. 0 means no vibrating. NOTE: The device might have + hardware-imposed limits on supported vibra intensity values, so actual + effect might vary between different hardware. + + \param intensity Intensity of the vibra in decimals + \return If false is returned, an error has occurred. Call error() to get a value of + XQVibra::Error that indicates which error occurred + \sa error() +*/ +bool XQVibra::setIntensity(int intensity) +{ + return d->setIntensity(intensity); +} + +/*! + Returns the current status of the vibration. This function can be used to check has vibration + allowed in the user profile. + + \return current status + \sa statusChanged() +*/ +XQVibra::Status XQVibra::currentStatus() const +{ + return d->currentStatus(); +} + +/*! + Returns the type of error that occurred if the latest function call failed; otherwise returns NoError + \return Error code +*/ +XQVibra::Error XQVibra::error() const +{ + return d->error(); +} + +/*! + \fn void XQVibra::statusChanged(Status status) + + This signal is emitted when the there is a change of the vibration status. + + \param status a vibration status + \sa currentStatus() +*/ + +// End of file diff --git a/examples/widgets/symbianvibration/xqvibra.h b/examples/widgets/symbianvibration/xqvibra.h new file mode 100644 index 0000000..5520d08 --- /dev/null +++ b/examples/widgets/symbianvibration/xqvibra.h @@ -0,0 +1,61 @@ +#ifndef XQVIBRA_H +#define XQVIBRA_H + +// INCLUDES +#include <QObject> + +// FORWARD DECLARATIONS +class XQVibraPrivate; + +// CLASS DECLARATION +//! [0] +class XQVibra : public QObject +{ + Q_OBJECT + +public: + static const int InfiniteDuration = 0; + static const int MaxIntensity = 100; + static const int MinIntensity = -100; + + enum Error { + NoError = 0, + OutOfMemoryError, + ArgumentError, + VibraInUseError, + HardwareError, + TimeOutError, + VibraLockedError, + AccessDeniedError, + UnknownError = -1 + }; + + enum Status { + StatusNotAllowed = 0, + StatusOff, + StatusOn + }; + + XQVibra(QObject *parent = 0); + ~XQVibra(); + + XQVibra::Status currentStatus() const; + XQVibra::Error error() const; + +Q_SIGNALS: + void statusChanged(XQVibra::Status status); + +public Q_SLOTS: + bool start(int duration = InfiniteDuration); + bool stop(); + bool setIntensity(int intensity); + +private: + friend class XQVibraPrivate; + XQVibraPrivate *d; +}; +//! [0] + +#endif // XQVIBRA_H + +// End of file diff --git a/examples/widgets/symbianvibration/xqvibra_p.cpp b/examples/widgets/symbianvibration/xqvibra_p.cpp new file mode 100644 index 0000000..9f2b5f9 --- /dev/null +++ b/examples/widgets/symbianvibration/xqvibra_p.cpp @@ -0,0 +1,131 @@ +#include "xqvibra_p.h" + +const int KDefaultIntensity = 0xFF; + +XQVibraPrivate::XQVibraPrivate(XQVibra *vibra) + : q(vibra), iStatus(XQVibra::StatusOff), iDuration(XQVibra::InfiniteDuration), iIntensity(KDefaultIntensity) + +{ + TRAP(iError, iVibra = CHWRMVibra::NewL();) + QObject::connect(&iTimer, SIGNAL(timeout()), q, SLOT(stop())); +} + +XQVibraPrivate::~XQVibraPrivate() +{ + delete iVibra; +} + +bool XQVibraPrivate::start(int aDuration) +{ + iDuration = aDuration; + TRAP(iError, + if (iIntensity == KDefaultIntensity) { + iVibra->StartVibraL(XQVibra::InfiniteDuration); + } else { + iVibra->StopVibraL(); + iVibra->StartVibraL(XQVibra::InfiniteDuration, iIntensity); + } + + if (aDuration != XQVibra::InfiniteDuration) { + iTimer.start(aDuration); + } else { + iTimer.stop(); + } + + if (iStatus != XQVibra::StatusOn) { + iStatus = XQVibra::StatusOn; + emit q->statusChanged(iStatus); + } + ) + return (iError == KErrNone); +} + +bool XQVibraPrivate::stop() +{ + TRAP(iError, + if (iVibra->VibraStatus() == CHWRMVibra::EVibraStatusOn) { + iVibra->StopVibraL(); + if (iTimer.isActive()) { + iTimer.stop(); + } + } + + iStatus = XQVibra::StatusOff; + emit q->statusChanged(iStatus); + ) + return (iError == KErrNone); +} + +void XQVibraPrivate::VibraModeChanged(CHWRMVibra::TVibraModeState /*aStatus*/) +{ + // Implementation isn't needed here because this information isn't used in the public side of the extension +} + +void XQVibraPrivate::VibraStatusChanged(CHWRMVibra::TVibraStatus aStatus) +{ + if (aStatus == CHWRMVibra::EVibraStatusUnknown || + aStatus == CHWRMVibra::EVibraStatusNotAllowed) { + iStatus = XQVibra::StatusNotAllowed; + emit q->statusChanged(iStatus); + } + + if (iDuration == XQVibra::InfiniteDuration) { + if (iStatus != XQVibra::StatusOff) { + iStatus = XQVibra::StatusOff; + emit q->statusChanged(iStatus); + } + } +} + +bool XQVibraPrivate::setIntensity(int aIntensity) +{ + TRAP(iError, + if (aIntensity >= KHWRMVibraMinIntensity && aIntensity <= KHWRMVibraMaxIntensity) { + iIntensity = aIntensity; + if (iIntensity == 0 && iStatus == XQVibra::StatusOn) { + iVibra->StopVibraL(); + } else if (iStatus == XQVibra::StatusOn) { + iVibra->StopVibraL(); + iVibra->StartVibraL(XQVibra::InfiniteDuration, iIntensity); + } + } else { + User::Leave(KErrArgument); + } + ) + return (iError == KErrNone); +} + +XQVibra::Status XQVibraPrivate::currentStatus() const +{ + if (iVibra->VibraStatus() == CHWRMVibra::EVibraStatusUnknown || + iVibra->VibraStatus() == CHWRMVibra::EVibraStatusNotAllowed) { + return XQVibra::StatusNotAllowed; + } + return iStatus; +} + +XQVibra::Error XQVibraPrivate::error() const +{ + switch (iError) { + case KErrNone: + return XQVibra::NoError; + case KErrNoMemory: + return XQVibra::OutOfMemoryError; + case KErrArgument: + return XQVibra::ArgumentError; + case KErrInUse: + return XQVibra::VibraInUseError; + case KErrGeneral: + return XQVibra::HardwareError; + case KErrTimedOut: + return XQVibra::TimeOutError; + case KErrLocked: + return XQVibra::VibraLockedError; + case KErrAccessDenied: + return XQVibra::AccessDeniedError; + default: + return XQVibra::UnknownError; + } +} + +// End of file diff --git a/examples/widgets/symbianvibration/xqvibra_p.h b/examples/widgets/symbianvibration/xqvibra_p.h new file mode 100644 index 0000000..7b4e9d8 --- /dev/null +++ b/examples/widgets/symbianvibration/xqvibra_p.h @@ -0,0 +1,39 @@ +#ifndef XQVIBRA_P_H +#define XQVIBRA_P_H + +// INCLUDES +#include "xqvibra.h" +#include <HWRMVibra.h> +#include <QTimer> + +// CLASS DECLARATION +class XQVibraPrivate: public CBase, public MHWRMVibraObserver +{ + +public: + XQVibraPrivate(XQVibra *vibra); + ~XQVibraPrivate(); + + bool start(int aDuration = XQVibra::InfiniteDuration); + bool stop(); + bool setIntensity(int aIntensity); + XQVibra::Status currentStatus() const; + XQVibra::Error error() const; + +private: // From MHWRMVibraObserver + void VibraModeChanged(CHWRMVibra::TVibraModeState aStatus); + void VibraStatusChanged(CHWRMVibra::TVibraStatus aStatus); + +private: + XQVibra *q; + XQVibra::Status iStatus; + CHWRMVibra *iVibra; + QTimer iTimer; + int iDuration; + int iIntensity; + int iError; +}; + +#endif /*XQVIBRA_P_H*/ + +// End of file |