Files:
The Symbian Vibrator example shows how to get fine-grained vibration control on Symbian devices.
Native Symbian APIs have to be used to enable vibration, since QtMobility doesn't provide an interface for it yet. It is, however, planned to be included in a future release. In anticipation for that, we make use of the XQVibra class that was a part of the Mobile Extensions Technology Preview API for Qt for Symbian. The pre-compiled libraries are no longer compatible with Qt 4.6, but we can include the source code itself with the project.
The example application divides the window into rectangles, which can be pressed to make the device vibrate. Pressing different rectangles make the device vibrate with different intensities. Each rectangle has a different color and its intensity number is drawn on top of it. Moving the cursor from one rectangle to another changes the vibration intensity to that of the new one. Vibration stops when the mouse button has been released. It is also possible to launch a short burst of vibration through the menu.
The example consists of four classes:
The XQVibra class uses the pimpl-idiom to hide the platform specific implementation behind a common interface. Technically it would be possible to support more target platforms, with only the addition of a private implementation. The rest of the code would work the same, since only the common interface is used.
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; };
XQVibra provides a very simple interface for us to use. The interesting part are the three slots start(), stop() and setIntensity(). Calling the start method initiates vibration for the specified duration. Calling it while the device is already vibrating causes it to stop the current one and start the new one, even if the intensities are the same. The setIntensity() method should be called before starting vibration.
VibrationSurface inherits from QWidget and acts like a controller for a XQVibra object. It responds to mouse events and performs custom painting.
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; };
The virtual event methods are reimplemented from QWidget. As can be seen, there is no public programmable interface beyond what QWidget provides.
Mouse events control the intensity of the vibration.
void VibrationSurface::mousePressEvent(QMouseEvent *event) { applyIntensity(event->x(), event->y()); vibra->start(); } void VibrationSurface::mouseMoveEvent(QMouseEvent *event) { applyIntensity(event->x(), event->y()); } void VibrationSurface::mouseReleaseEvent(QMouseEvent *) { vibra->stop(); }
Presses starts the vibration, movement changes the intensity and releases stops the vibration. To set the right amount of vibration, the private method applyIntensity() is used. It sets the vibration intensity according to which rectangle the mouse currently resides in.
void VibrationSurface::applyIntensity(int x, int y) { int intensity = getIntensity(x, y); if (intensity != lastIntensity) { vibra->setIntensity(intensity); lastIntensity = intensity; } }
We make sure only to change the intensity if it is different than last time, so that the vibrator isn't stopped and restarted unnecessarily.
The range of vibration intensity ranges from 0 to XQVibra::MaxIntensity. We divide this range into a set of levels. The number of levels and the intensity increase for each level are stored in two constants.
const int NumberOfLevels = 10; const double IntensityFactor = XQVibra::MaxIntensity / NumberOfLevels;
Each rectangle has an intensity of one IntensityPerLevel more than the previous one.
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); }
The rectangles are either put in a row, if the widget's width is greater than its height (landscape), otherwise they are put in a column (portrait). Each rectangle's size is thus dependent on the length of the width or the height of the widget, whichever is longer. The length is then divided by the number of levels, which gets us either the height or the width of each rectangle. The dx and dy specify the distance from one rectangle to the next, which is the same as either the width or height of the rectangle.
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)); } }
For each level of intensity, we draw a rectangle with increasing brightness. On top of the rectangle a text label is drawn, specifying the intesity of this level. We use the rectangle rect as a template for drawing, and move it down or right at each iteration.
The intensity is calculated by dividing the greater of the width and height into NumberOfLevels slices.
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; }
In case the widget's geometry is too small to fit all the levels, the user interface will not work. For simplicity, we just return 0.
When we know the axis along which the rectangles lie, we can find the one in which the mouse cursor lie.
int intensity = (coord / level + 1) * IntensityFactor; if (intensity < 0) { intensity = 0; } else if (intensity > XQVibra::MaxIntensity) { intensity = XQVibra::MaxIntensity; } return intensity; }
The final clamp of the intensity value at the end is necessary in case the mouse coordinate lies outside the widget's geometry.
Here's the definition of the MainWindow class:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); private slots: void vibrate(); private: XQVibra *vibra; };
MainWindow is a top level window that uses a XQVibra and a VibrationSurface. It also adds a menu option to the menu bar which can start a short vibration.
In the MainWindow constructor the XQVibra and the VibrationSurface are created. An action is added to the menu and is connected to the vibrate slot.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { vibra = new XQVibra(this); setCentralWidget(new VibrationSurface(vibra, this)); menuBar()->addAction(tr("Vibrate"), this, SLOT(vibrate())); }
The vibrate() slot offers a way to invoke the vibration in case no mouse is present on the device.
void MainWindow::vibrate() { vibra->setIntensity(75); vibra->start(2500); }
The XQVibra class requires a platform library to be included. It is included in the .pro file for the symbian target.
symbian { TARGET.UID3 = 0xecf47018 # TARGET.CAPABILITY += TARGET.EPOCSTACKSIZE = 0x14000 TARGET.EPOCHEAPSIZE = 0x020000 0x800000 LIBS += -lhwrmvibraclient include($$PWD/../../symbianpkgrules.pri) }