accelerometerwidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "accelerometerwidget.h"
  2. #include <QString>
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QGridLayout>
  6. #include <QPushButton>
  7. #include <QMessageBox>
  8. #include <QFile>
  9. #include <phonon/MediaObject>
  10. accelerometerWidget::accelerometerWidget(QWidget *parent)
  11. : QWidget(parent)
  12. {
  13. setFont(QFont("Nokia Sans", 50, QFont::Bold));
  14. QGridLayout *grid = new QGridLayout;
  15. label1 = new QLabel("X");
  16. label1->setAlignment(Qt::AlignCenter);
  17. grid->addWidget(label1, 0, 0);
  18. label2 = new QLabel("Y");
  19. label2->setAlignment(Qt::AlignCenter);
  20. grid->addWidget(label2, 0, 1);
  21. label3 = new QLabel("Z");
  22. label3->setAlignment(Qt::AlignCenter);
  23. grid->addWidget(label3, 0, 2);
  24. xlabel = new QLabel("NaN");
  25. xlabel->setAlignment(Qt::AlignCenter);
  26. grid->addWidget(xlabel, 1, 0);
  27. ylabel = new QLabel("NaN");
  28. ylabel->setAlignment(Qt::AlignCenter);
  29. grid->addWidget(ylabel, 1, 1);
  30. zlabel = new QLabel("NaN");
  31. zlabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
  32. grid->addWidget(zlabel, 1, 2);
  33. currentxtx = new QLabel("NaN");
  34. currentxtx->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
  35. grid->addWidget(currentxtx, 2, 0);
  36. currentxty = new QLabel("NaN");
  37. currentxty->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
  38. grid->addWidget(currentxty, 2, 1);
  39. QPushButton *bouton = new QPushButton("Exit",this);
  40. grid->addWidget(bouton,2,2);
  41. connect(bouton,SIGNAL(clicked()),this,SLOT(close()));
  42. setLayout(grid);
  43. myThread = new accelerometerClass(this);
  44. connect(myThread, SIGNAL(deviceOrientation(QString, QString, QString)),
  45. this, SLOT(showData(QString, QString, QString)));
  46. myThread->start(QThread::NormalPriority);
  47. fileAlarm = new QFile(":/audio/CarAlarm.wav");
  48. if(!fileAlarm)
  49. qWarning()<<"Object fileAlarm is empty";
  50. fileCar = new QFile(":/audio/bip_car.wav");
  51. if(!fileCar)
  52. qWarning()<<"Object fileAlarm is empty";
  53. car = new Phonon::MediaObject(this);
  54. bipCar = new Phonon::MediaObject(this);
  55. car->setCurrentSource(fileAlarm);
  56. bipCar->setCurrentSource(fileCar);
  57. //connect(car, SIGNAL(finished()),this,SLOT(play()));
  58. }
  59. //void accelerometerWidget::currentValue(QString x, QString y)
  60. //{
  61. //}
  62. void accelerometerWidget::valueCurrent()
  63. {
  64. current=false;
  65. }
  66. void accelerometerWidget::showData(QString x, QString y, QString z)
  67. {
  68. if(!current)
  69. {
  70. //currentValue(x,y);
  71. currentx=x;
  72. currenty=y;
  73. current=true;
  74. }
  75. currentxtx->setText(currentx);
  76. currentxty->setText(currenty);
  77. if(currentx.toInt() + 250 < x.toInt() || currentx.toInt() - 250 > x.toInt() )
  78. {
  79. xlabel->setText("ALARME!");
  80. //SLOT(voitureAlarm());
  81. //connect(car, SIGNAL(finished()),this,SLOT(play()));
  82. car->play();
  83. }else
  84. xlabel->setText(x);
  85. if(currenty.toInt() + 250 < y.toInt() || currenty.toInt() - 250 > y.toInt() )
  86. {
  87. ylabel->setText("ALARME!");
  88. //if(car->isFinished())
  89. SLOT(voitureAlarm());
  90. //bipCar->play();
  91. }else
  92. ylabel->setText(y);
  93. zlabel->setText(z);
  94. }
  95. void accelerometerWidget::voitureAlarm()
  96. {
  97. //car->play();
  98. //connect(car, SIGNAL(finished()),this,SLOT(play()));
  99. // QSound::play("audio/CarAlarm.wav");
  100. //if (!QSound::isAvailable()) {
  101. // Bail out. Programs in which sound is not critical
  102. // could just silently (hehe) ignore the lack of a server.
  103. //
  104. /* QMessageBox::warning(this,"No Sound",
  105. "<p><b>Sorry, you are not running the Network Audio System.</b>"
  106. "<p>If you have the `au' command, run it in the background before this program. "
  107. "The latest release of the Network Audio System can be obtained from:"
  108. "<pre>\n"
  109. " &nbsp;\n"
  110. " ftp.ncd.com:/pub/ncd/technology/src/nas\n"
  111. " ftp.x.org:/contrib/audio/nas\n"
  112. "</pre>"
  113. "<p>Release 1.2 of NAS is also included with the X11R6"
  114. "contrib distribution."
  115. "<p>After installing NAS, you will then need to reconfigure Qt with NAS sound support");}
  116. */
  117. }
  118. void accelerometerWidget::voitureBip()
  119. {
  120. //QSound::play("Cbip_car.wav");
  121. bipCar->play();
  122. //connect(bipCar, SIGNAL(finished()),this,SLOT(play()));
  123. }