Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 8060

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- symbianvibration.qdoc -->
  <title>Qt 4.8: Symbian Vibration Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Symbian Vibration Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#xqvibra-class-definition">XQVibra Class Definition</a></li>
<li class="level1"><a href="#vibrationsurface-class-definition">VibrationSurface Class Definition</a></li>
<li class="level1"><a href="#vibrationsurface-class-implementation">VibrationSurface Class Implementation</a></li>
<li class="level1"><a href="#mainwindow-class-definition">MainWindow Class Definition</a></li>
<li class="level1"><a href="#mainwindow-class-implementation">MainWindow Class Implementation</a></li>
<li class="level1"><a href="#symbian-vibration-library">Symbian Vibration Library</a></li>
</ul>
</div>
<h1 class="title">Symbian Vibration Example</h1>
<span class="subtitle"></span>
<!-- $$$widgets/symbianvibration-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="widgets-symbianvibration-mainwindow-cpp.html">widgets/symbianvibration/mainwindow.cpp</a></li>
<li><a href="widgets-symbianvibration-mainwindow-h.html">widgets/symbianvibration/mainwindow.h</a></li>
<li><a href="widgets-symbianvibration-vibrationsurface-cpp.html">widgets/symbianvibration/vibrationsurface.cpp</a></li>
<li><a href="widgets-symbianvibration-vibrationsurface-h.html">widgets/symbianvibration/vibrationsurface.h</a></li>
<li><a href="widgets-symbianvibration-xqvibra-cpp.html">widgets/symbianvibration/xqvibra.cpp</a></li>
<li><a href="widgets-symbianvibration-xqvibra-h.html">widgets/symbianvibration/xqvibra.h</a></li>
<li><a href="widgets-symbianvibration-xqvibra-p-cpp.html">widgets/symbianvibration/xqvibra_p.cpp</a></li>
<li><a href="widgets-symbianvibration-xqvibra-p-h.html">widgets/symbianvibration/xqvibra_p.h</a></li>
<li><a href="widgets-symbianvibration-main-cpp.html">widgets/symbianvibration/main.cpp</a></li>
<li><a href="widgets-symbianvibration-symbianvibration-pro.html">widgets/symbianvibration/symbianvibration.pro</a></li>
</ul>
<p>The Symbian Vibrator example shows how to get fine-grained vibration control on Symbian devices.<p>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 <tt>XQVibra</tt> 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.</p>
<p class="centerAlign"><img src="images/symbianvibration-example.png" alt="Screenshot of the Symbian Vibration example" /></p><p>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.</p>
<p>The example consists of four classes:</p>
<ul>
<li><tt>XQVibra</tt> is the vibration interface class taken from the Mobile Extensions for Qt for Symbian.</li>
<li><tt>XQVibraPrivate</tt> is the Symbian specific private implementation of the vibration implementation.</li>
<li><tt>VibrationSurface</tt> is a custom widget that uses a XQVibra instance to vibrate the device depending on where the user presses.</li>
<li><tt>MainWindow</tt> inherits from <a href="qmainwindow.html">QMainWindow</a> and contains a <tt>VibrationSurface</tt> as its central widget, and also has a menu from which it is possible to make the phone vibrate.</li>
</ul>
<a name="xqvibra-class-definition"></a>
<h2>XQVibra Class Definition</h2>
<p>The <tt>XQVibra</tt> 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.</p>
<pre class="cpp"> <span class="keyword">class</span> XQVibra : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">int</span> InfiniteDuration <span class="operator">=</span> <span class="number">0</span>;
     <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">int</span> MaxIntensity <span class="operator">=</span> <span class="number">100</span>;
     <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">int</span> MinIntensity <span class="operator">=</span> <span class="operator">-</span><span class="number">100</span>;

     <span class="keyword">enum</span> Error {
         NoError <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span>
         OutOfMemoryError<span class="operator">,</span>
         ArgumentError<span class="operator">,</span>
         VibraInUseError<span class="operator">,</span>
         HardwareError<span class="operator">,</span>
         TimeOutError<span class="operator">,</span>
         VibraLockedError<span class="operator">,</span>
         AccessDeniedError<span class="operator">,</span>
         UnknownError <span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>
     };

     <span class="keyword">enum</span> Status {
         StatusNotAllowed <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span>
         StatusOff<span class="operator">,</span>
         StatusOn
     };

     XQVibra(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
     <span class="operator">~</span>XQVibra();

     XQVibra<span class="operator">::</span>Status currentStatus() <span class="keyword">const</span>;
     XQVibra<span class="operator">::</span>Error error() <span class="keyword">const</span>;

 Q_SIGNALS:
     <span class="type">void</span> statusChanged(XQVibra<span class="operator">::</span>Status status);

 <span class="keyword">public</span> Q_SLOTS:
     <span class="type">bool</span> start(<span class="type">int</span> duration <span class="operator">=</span> InfiniteDuration);
     <span class="type">bool</span> stop();
     <span class="type">bool</span> setIntensity(<span class="type">int</span> intensity);

 <span class="keyword">private</span>:
     <span class="keyword">friend</span> <span class="keyword">class</span> XQVibraPrivate;
     XQVibraPrivate <span class="operator">*</span>d;
 };</pre>
<p><tt>XQVibra</tt> provides a very simple interface for us to use. The interesting part are the three slots <tt>start()</tt>, <tt>stop()</tt> and <tt>setIntensity()</tt>. 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 <tt>setIntensity()</tt> method should be called before starting vibration.</p>
<a name="vibrationsurface-class-definition"></a>
<h2>VibrationSurface Class Definition</h2>
<p><tt>VibrationSurface</tt> inherits from <a href="qwidget.html">QWidget</a> and acts like a controller for a <tt>XQVibra</tt> object. It responds to mouse events and performs custom painting.</p>
<pre class="cpp"> <span class="keyword">class</span> VibrationSurface : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
     Q_OBJECT
 <span class="keyword">public</span>:
     <span class="keyword">explicit</span> VibrationSurface(XQVibra <span class="operator">*</span>vibra<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">protected</span>:
     <span class="keyword">virtual</span> <span class="type">void</span> mousePressEvent(<span class="type"><a href="qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>);
     <span class="keyword">virtual</span> <span class="type">void</span> mouseMoveEvent(<span class="type"><a href="qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>);
     <span class="keyword">virtual</span> <span class="type">void</span> mouseReleaseEvent(<span class="type"><a href="qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>);
     <span class="keyword">virtual</span> <span class="type">void</span> paintEvent(<span class="type"><a href="qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>);

 <span class="keyword">private</span>:

     <span class="type">int</span> getIntensity(<span class="type">int</span> x<span class="operator">,</span> <span class="type">int</span> y);
     <span class="type">void</span> applyIntensity(<span class="type">int</span> x<span class="operator">,</span> <span class="type">int</span> y);

     XQVibra <span class="operator">*</span>vibra;
     <span class="type">int</span> lastIntensity;
 };</pre>
<p>The virtual event methods are reimplemented from <a href="qwidget.html">QWidget</a>. As can be seen, there is no public programmable interface beyond what <a href="qwidget.html">QWidget</a> provides.</p>
<a name="vibrationsurface-class-implementation"></a>
<h2>VibrationSurface Class Implementation</h2>
<p>Mouse events control the intensity of the vibration.</p>
<pre class="cpp"> <span class="type">void</span> VibrationSurface<span class="operator">::</span>mousePressEvent(<span class="type"><a href="qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
 {
     applyIntensity(event<span class="operator">-</span><span class="operator">&gt;</span>x()<span class="operator">,</span> event<span class="operator">-</span><span class="operator">&gt;</span>y());
     vibra<span class="operator">-</span><span class="operator">&gt;</span>start();
 }

 <span class="type">void</span> VibrationSurface<span class="operator">::</span>mouseMoveEvent(<span class="type"><a href="qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
 {
     applyIntensity(event<span class="operator">-</span><span class="operator">&gt;</span>x()<span class="operator">,</span> event<span class="operator">-</span><span class="operator">&gt;</span>y());
 }

 <span class="type">void</span> VibrationSurface<span class="operator">::</span>mouseReleaseEvent(<span class="type"><a href="qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>)
 {
     vibra<span class="operator">-</span><span class="operator">&gt;</span>stop();
 }</pre>
<p>Presses starts the vibration, movement changes the intensity and releases stops the vibration. To set the right amount of vibration, the private method <tt>applyIntensity()</tt> is used. It sets the vibration intensity according to which rectangle the mouse currently resides in.</p>
<pre class="cpp"> <span class="type">void</span> VibrationSurface<span class="operator">::</span>applyIntensity(<span class="type">int</span> x<span class="operator">,</span> <span class="type">int</span> y)
 {
     <span class="type">int</span> intensity <span class="operator">=</span> getIntensity(x<span class="operator">,</span> y);

     <span class="keyword">if</span> (intensity <span class="operator">!</span><span class="operator">=</span> lastIntensity) {
         vibra<span class="operator">-</span><span class="operator">&gt;</span>setIntensity(intensity);
         lastIntensity <span class="operator">=</span> intensity;
     }
 }</pre>
<p>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.</p>
<p>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.</p>
<pre class="cpp"> <span class="keyword">const</span> <span class="type">int</span> NumberOfLevels <span class="operator">=</span> <span class="number">10</span>;
 <span class="keyword">const</span> <span class="type">double</span> IntensityFactor <span class="operator">=</span> XQVibra<span class="operator">::</span>MaxIntensity <span class="operator">/</span> NumberOfLevels;</pre>
<p>Each rectangle has an intensity of one <tt>IntensityPerLevel</tt> more than the previous one.</p>
<pre class="cpp"> <span class="type">void</span> VibrationSurface<span class="operator">::</span>paintEvent(<span class="type"><a href="qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>)
 {
     <span class="type"><a href="qpainter.html">QPainter</a></span> painter(<span class="keyword">this</span>);
     <span class="type"><a href="qrect.html">QRect</a></span> rect <span class="operator">=</span> geometry();
     <span class="type">int</span> dx <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> dy <span class="operator">=</span> <span class="number">0</span>;

     <span class="keyword">if</span> (height() <span class="operator">&gt;</span> width()) {
         dy <span class="operator">=</span> height() <span class="operator">/</span> NumberOfLevels;
         rect<span class="operator">.</span>setHeight(dy);
     } <span class="keyword">else</span> {
         dx <span class="operator">=</span> width() <span class="operator">/</span> NumberOfLevels;
         rect<span class="operator">.</span>setWidth(dx);
     }</pre>
<p>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.</p>
<pre class="cpp">     <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> NumberOfLevels; i<span class="operator">+</span><span class="operator">+</span>) {
         <span class="type">int</span> x <span class="operator">=</span> i <span class="operator">*</span> dx;
         <span class="type">int</span> y <span class="operator">=</span> i <span class="operator">*</span> dy;
         <span class="type">int</span> intensity <span class="operator">=</span> getIntensity(x<span class="operator">,</span> y);
         <span class="type"><a href="qcolor.html">QColor</a></span> color <span class="operator">=</span> <span class="type"><a href="qcolor.html">QColor</a></span>(<span class="number">40</span><span class="operator">,</span> <span class="number">80</span><span class="operator">,</span> <span class="number">10</span>)<span class="operator">.</span>lighter(<span class="number">100</span> <span class="operator">+</span> intensity);

         rect<span class="operator">.</span>moveTo(x<span class="operator">,</span> y);
         painter<span class="operator">.</span>fillRect(rect<span class="operator">,</span> color);
         painter<span class="operator">.</span>setPen(color<span class="operator">.</span>darker());
         painter<span class="operator">.</span>drawText(rect<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>AlignCenter<span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span><span class="operator">::</span>number(intensity));
     }
 }</pre>
<p>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.</p>
<p>The intensity is calculated by dividing the greater of the width and height into <tt>NumberOfLevels</tt> slices.</p>
<pre class="cpp"> <span class="type">int</span> VibrationSurface<span class="operator">::</span>getIntensity(<span class="type">int</span> x<span class="operator">,</span> <span class="type">int</span> y)
 {
     <span class="type">int</span> level;
     <span class="type">int</span> coord;

     <span class="keyword">if</span> (height() <span class="operator">&gt;</span> width()) {
         level <span class="operator">=</span> height() <span class="operator">/</span> NumberOfLevels;
         coord <span class="operator">=</span> y;
     } <span class="keyword">else</span> {
         level <span class="operator">=</span> width() <span class="operator">/</span> NumberOfLevels;
         coord <span class="operator">=</span> x;
     }

     <span class="keyword">if</span> (level <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
         <span class="keyword">return</span> <span class="number">0</span>;
     }</pre>
<p>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.</p>
<p>When we know the axis along which the rectangles lie, we can find the one in which the mouse cursor lie.</p>
<pre class="cpp">     <span class="type">int</span> intensity <span class="operator">=</span> (coord <span class="operator">/</span> level <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> IntensityFactor;

     <span class="keyword">if</span> (intensity <span class="operator">&lt;</span> <span class="number">0</span>) {
         intensity <span class="operator">=</span> <span class="number">0</span>;
     } <span class="keyword">else</span> <span class="keyword">if</span> (intensity <span class="operator">&gt;</span> XQVibra<span class="operator">::</span>MaxIntensity) {
         intensity <span class="operator">=</span> XQVibra<span class="operator">::</span>MaxIntensity;
     }

     <span class="keyword">return</span> intensity;
 }</pre>
<p>The final clamp of the intensity value at the end is necessary in case the mouse coordinate lies outside the widget's geometry.</p>
<a name="mainwindow-class-definition"></a>
<h2>MainWindow Class Definition</h2>
<p>Here's the definition of the <tt>MainWindow</tt> class:</p>
<pre class="cpp"> <span class="keyword">class</span> MainWindow : <span class="keyword">public</span> <span class="type"><a href="qmainwindow.html">QMainWindow</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     MainWindow(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> vibrate();

 <span class="keyword">private</span>:
     XQVibra <span class="operator">*</span>vibra;
 };</pre>
<p><tt>MainWindow</tt> is a top level window that uses a <tt>XQVibra</tt> and a <tt>VibrationSurface</tt>. It also adds a menu option to the menu bar which can start a short vibration.</p>
<a name="mainwindow-class-implementation"></a>
<h2>MainWindow Class Implementation</h2>
<p>In the <tt>MainWindow</tt> constructor the <tt>XQVibra</tt> and the <tt>VibrationSurface</tt> are created. An action is added to the menu and is connected to the vibrate slot.</p>
<pre class="cpp"> MainWindow<span class="operator">::</span>MainWindow(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qmainwindow.html">QMainWindow</a></span>(parent)
 {
     vibra <span class="operator">=</span> <span class="keyword">new</span> XQVibra(<span class="keyword">this</span>);
     setCentralWidget(<span class="keyword">new</span> VibrationSurface(vibra<span class="operator">,</span> <span class="keyword">this</span>));
     menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Vibrate&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(vibrate()));
 }</pre>
<p>The <tt>vibrate()</tt> slot offers a way to invoke the vibration in case no mouse is present on the device.</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>vibrate()
 {
     vibra<span class="operator">-</span><span class="operator">&gt;</span>setIntensity(<span class="number">75</span>);
     vibra<span class="operator">-</span><span class="operator">&gt;</span>start(<span class="number">2500</span>);
 }</pre>
<a name="symbian-vibration-library"></a>
<h2>Symbian Vibration Library</h2>
<p>The <tt>XQVibra</tt> class requires a platform library to be included. It is included in the <tt>.pro</tt> file for the symbian target.</p>
<pre class="qml"> symbian {
     TARGET.UID3 = 0xecf47018
     # TARGET.CAPABILITY +=
     TARGET.EPOCSTACKSIZE = 0x14000
     TARGET.EPOCHEAPSIZE = 0x020000 0x800000
     LIBS += -lhwrmvibraclient
     include($$PWD/../../symbianpkgrules.pri)
 }</pre>
</div>
<!-- @@@widgets/symbianvibration -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>