Mirror.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /****************************************************************************
  2. **
  3. ** Trolltech hereby grants a license to use the Qt/Eclipse Integration
  4. ** plug-in (the software contained herein), in binary form, solely for the
  5. ** purpose of creating code to be used with Trolltech's Qt software.
  6. **
  7. ** Qt Designer is licensed under the terms of the GNU General Public
  8. ** License versions 2.0 and 3.0 ("GPL License"). Trolltech offers users the
  9. ** right to use certain no GPL licensed software under the terms of its GPL
  10. ** Exception version 1.2 (http://trolltech.com/products/qt/gplexception).
  11. **
  12. ** THIS SOFTWARE IS PROVIDED BY TROLLTECH AND ITS CONTRIBUTORS (IF ANY) "AS
  13. ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  14. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  15. ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. ** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  23. **
  24. ** Since we now have the GPL exception I think that the "special exception
  25. ** is no longer needed. The license text proposed above (other than the
  26. ** special exception portion of it) is the BSD license and we have added
  27. ** the BSD license as a permissible license under the exception.
  28. **
  29. ****************************************************************************/
  30. #include "Mirror.h"
  31. #include <QtGui>
  32. const QSize DEFAULT_SIZE = QSize(640, 360);
  33. const QRect MIRROR_SIZE = QRect(129, 37, 389, 292);
  34. #define FILTER_PNG "*.PNG"
  35. Mirror::Mirror(QWidget *parent) :
  36. QWidget(parent), camera(0), videoWidget(0)
  37. {
  38. ui.setupUi(this);
  39. setupVideo();
  40. setupAbout();
  41. setupCamera();
  42. setupSettings();
  43. ui.listWidget->hide();
  44. }
  45. Mirror::~Mirror()
  46. {
  47. delete camera;
  48. delete videoWidget;
  49. }
  50. void Mirror::setupSettings()
  51. {
  52. QDir dir(":/images/background", FILTER_PNG);
  53. QFileInfoList list = dir.entryInfoList( QDir::Files );
  54. for (int i = 0; i < list.size(); ++i)
  55. {
  56. QIcon icon(list[i].absoluteFilePath());
  57. QListWidgetItem* item = new QListWidgetItem(icon, list[i].baseName(), ui.listWidget );
  58. }
  59. connect(ui.settingsButton, SIGNAL(toggled(bool)), this, SLOT(showSettings(bool)));
  60. connect(ui.listWidget,SIGNAL(itemClicked(QListWidgetItem*)),
  61. this, SLOT(changeBackground(QListWidgetItem*)));
  62. connect(ui.listWidget,SIGNAL(doubleClicked(QListWidgetItem*)),
  63. this, SLOT(changeBackgroundAndReturn(QListWidgetItem*)));
  64. }
  65. void Mirror::setupVideo()
  66. {
  67. videoWidget = new QVideoWidget(ui.frame);
  68. videoWidget->setObjectName(QString::fromUtf8("videoWidget"));
  69. videoWidget->setGeometry(MIRROR_SIZE);
  70. }
  71. void Mirror::setupAbout()
  72. {
  73. QPixmap pixmapAbout(":/images/about.png");
  74. about = new QAboutSplashScreen(this,pixmapAbout);
  75. about->setGeometry(MIRROR_SIZE);
  76. connect( ui.aboutButton,SIGNAL(toggled(bool)),this,SLOT(showAbout(bool)));
  77. connect( about, SIGNAL(exitAbout()), this, SLOT(showAbout()));
  78. }
  79. void Mirror::showAbout(bool show)
  80. {
  81. ui.aboutButton->setChecked(show);
  82. ui.settingsButton->setChecked(false);
  83. ui.listWidget->hide();
  84. if( show )
  85. {
  86. videoWidget->hide();
  87. about->show();
  88. }
  89. else
  90. {
  91. videoWidget->show();
  92. about->hide();
  93. }
  94. }
  95. void Mirror::showSettings(bool show)
  96. {
  97. ui.settingsButton->setChecked(show);
  98. ui.aboutButton->setChecked(false);
  99. about->hide();
  100. if( show )
  101. {
  102. videoWidget->hide();
  103. ui.listWidget->show();
  104. }
  105. else
  106. {
  107. videoWidget->show();
  108. ui.listWidget->hide();
  109. }
  110. }
  111. void Mirror::setupCamera()
  112. {
  113. QByteArray cameraDevice = QCamera::availableDevices()[1];
  114. camera = new QCamera(cameraDevice);
  115. connect(camera, SIGNAL(error(QCamera::Error)), this, SLOT(error(QCamera::Error)));
  116. camera->setFocusMode(QCamera::AutoFocus);
  117. // camera->setMirrorEnabled( true );
  118. videoWidget->setMediaObject(camera);
  119. if (camera->state() == QCamera::ActiveState) {
  120. camera->stop();
  121. }
  122. camera->start();
  123. }
  124. void Mirror::changeBackground(QListWidgetItem* item)
  125. {
  126. QString baseName = item->text();
  127. QString style;
  128. QTextStream(&style) << "background-image: url(:/images/background/" << baseName << ".png);";
  129. ui.frame->setStyleSheet(
  130. QApplication::translate("Mirror", qPrintable(style), 0, QApplication::UnicodeUTF8));
  131. update();
  132. }
  133. void Mirror::changeBackgroundAndReturn(QListWidgetItem* item)
  134. {
  135. changeBackground( item );
  136. showSettings( false );
  137. }
  138. void Mirror::error(QCamera::Error e)
  139. {
  140. switch (e) {
  141. case QCamera::CameraError:
  142. {
  143. QMessageBox::warning(this, "Camera error", "General camera error");
  144. break;
  145. }
  146. case QCamera::NotReadyToCaptureError:
  147. {
  148. QMessageBox::warning(this, "Camera error", "Camera not ready to capture image");
  149. break;
  150. }
  151. case QCamera::InvalidRequestError:
  152. {
  153. QMessageBox::warning(this, "Camera error", "Invalid request");
  154. break;
  155. }
  156. case QCamera::ServiceMissingError:
  157. {
  158. QMessageBox::warning(this, "Camera error", "Service missing");
  159. break;
  160. }
  161. case QCamera::NotSupportedFeatureError:
  162. {
  163. QMessageBox::warning(this, "Camera error", "Not supported feature");
  164. break;
  165. }
  166. default:
  167. break;
  168. };
  169. }
  170. //void Mirror::paintEvent(QPaintEvent* evt)
  171. //{
  172. // QPainter painter(this);
  173. //
  174. // if (!pixmap->isNull())
  175. // {
  176. //// QPoint centerPoint(0,0);
  177. //// // Scale new image which size is widgetSize
  178. //// QPixmap scaledPixmap = pixmap->scaled(widgetSize, Qt::KeepAspectRatio);
  179. //// // Calculate image center position into screen
  180. //// centerPoint.setX((widgetSize.width()-scaledPixmap.width())/2);
  181. //// centerPoint.setY((widgetSize.height()-scaledPixmap.height())/2);
  182. // // Draw image
  183. // painter.drawPixmap(QPoint(0,0),*pixmap);
  184. // }
  185. //}
  186. //void Mirror::setupBackground()
  187. //{
  188. // //Using QPalette you can set background image as follows.
  189. // QPalette p = palette();
  190. //
  191. // QString privatePathQt(QDir::currentPath());
  192. // QString privatePathSymbian(QDir::toNativeSeparators(privatePathQt));
  193. // privatePathSymbian.append( "\\bg.PNG" );
  194. // //Load image to QPixmap, Give full path of image
  195. // QPixmap pixmap1(privatePathSymbian); //For emulator C: is ..\epoc32\winscw\c so image must be at that location
  196. //
  197. // p.setBrush(QPalette::Background, pixmap1);
  198. // setPalette(p);
  199. //}