| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtSensors module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QtCore/QDebug> |
| 52 | #include <QtWidgets/QTreeWidget> |
| 53 | |
| 54 | #include "mainwindow.h" |
| 55 | #include "ui_mainwindow.h" |
| 56 | |
| 57 | #include <qsensorgesture.h> |
| 58 | #include <qsensorgesturemanager.h> |
| 59 | |
| 60 | MainWindow::MainWindow(QWidget *parent) |
| 61 | : QMainWindow(parent), |
| 62 | ui(new Ui::MainWindow) |
| 63 | { |
| 64 | ui->setupUi(this); |
| 65 | //! [0] |
| 66 | |
| 67 | QSensorGestureManager manager; |
| 68 | |
| 69 | Q_FOREACH (const QString &gesture, manager.gestureIds()) { |
| 70 | |
| 71 | QTreeWidgetItem *gestureId = new QTreeWidgetItem(ui->treeWidget); |
| 72 | QStringList recognizerSignals = manager.recognizerSignals(recognizerId: gesture); |
| 73 | gestureId->setText(column: 0,atext: gesture); |
| 74 | |
| 75 | for (int i = 0; i < recognizerSignals.count(); i++) { |
| 76 | QTreeWidgetItem *oneSignal = new QTreeWidgetItem(gestureId); |
| 77 | oneSignal->setText(column: 0,atext: recognizerSignals.at(i)); |
| 78 | } |
| 79 | ui->treeWidget->insertTopLevelItem(index: 0,item: gestureId); |
| 80 | } |
| 81 | //! [0] |
| 82 | |
| 83 | |
| 84 | ui->textEdit->setReadOnly(true); |
| 85 | } |
| 86 | |
| 87 | MainWindow::~MainWindow() |
| 88 | { |
| 89 | delete ui; |
| 90 | } |
| 91 | |
| 92 | void MainWindow::detectedShake(const QString &name) |
| 93 | { |
| 94 | QString str = "<font size=+2><B>" +name+"</b></font><br>" ; |
| 95 | ui->textEdit->insertHtml(text: str); |
| 96 | ui->textEdit->ensureCursorVisible(); |
| 97 | } |
| 98 | |
| 99 | void MainWindow::on_pushButton_clicked() |
| 100 | { |
| 101 | ui->textEdit->clear(); |
| 102 | } |
| 103 | |
| 104 | void MainWindow::onShake() |
| 105 | { |
| 106 | QString str = "<font size=+2><B>onShake()</b></font><br>" ; |
| 107 | ui->textEdit->insertHtml(text: str); |
| 108 | ui->textEdit->ensureCursorVisible(); |
| 109 | } |
| 110 | |
| 111 | void MainWindow::on_startPushButton_clicked() |
| 112 | { |
| 113 | if (ui->treeWidget->currentItem() == 0) |
| 114 | return; |
| 115 | QString currentRecognizer; |
| 116 | |
| 117 | if (ui->treeWidget->currentItem()->childCount() == 0) { |
| 118 | currentRecognizer = ui->treeWidget->currentItem()->parent()->text(column: 0); |
| 119 | } else { |
| 120 | currentRecognizer = ui->treeWidget->currentItem()->text(column: 0); |
| 121 | } |
| 122 | |
| 123 | if (recognizerMap.contains(akey: currentRecognizer)) |
| 124 | return; |
| 125 | //! [1] |
| 126 | QSensorGestureManager manager; |
| 127 | QSensorGesture *thisGesture = new QSensorGesture(QStringList() << currentRecognizer, this); |
| 128 | |
| 129 | if (currentRecognizer.contains(s: "QtSensors.shake" )) { |
| 130 | connect(sender: thisGesture,SIGNAL(shake()), |
| 131 | receiver: this,SLOT(onShake())); |
| 132 | } |
| 133 | |
| 134 | connect(sender: thisGesture,SIGNAL(detected(QString)), |
| 135 | receiver: this,SLOT(detectedShake(QString))); |
| 136 | thisGesture->startDetection(); |
| 137 | |
| 138 | //! [1] |
| 139 | |
| 140 | recognizerMap.insert(akey: currentRecognizer,avalue: thisGesture); |
| 141 | |
| 142 | QString str = QString("<font size=+2><B>Started %1</b></font><br>" ).arg(a: currentRecognizer); |
| 143 | ui->textEdit->insertHtml(text: str); |
| 144 | ui->textEdit->ensureCursorVisible(); |
| 145 | } |
| 146 | |
| 147 | void MainWindow::on_stopPushButton_clicked() |
| 148 | { |
| 149 | if (ui->treeWidget->currentItem() == 0) |
| 150 | return; |
| 151 | QString currentRecognizer; |
| 152 | |
| 153 | if (ui->treeWidget->currentItem()->childCount() == 0) { |
| 154 | currentRecognizer = ui->treeWidget->currentItem()->parent()->text(column: 0); |
| 155 | } else { |
| 156 | currentRecognizer = ui->treeWidget->currentItem()->text(column: 0); |
| 157 | } |
| 158 | |
| 159 | if (!recognizerMap.contains(akey: currentRecognizer)) |
| 160 | return; |
| 161 | //! [2] |
| 162 | |
| 163 | recognizerMap[currentRecognizer]->stopDetection(); |
| 164 | |
| 165 | if (currentRecognizer == "QtSensors.shake" ) { |
| 166 | disconnect(sender: recognizerMap[currentRecognizer],SIGNAL(shake()), |
| 167 | receiver: this,SLOT(onShake())); |
| 168 | } |
| 169 | disconnect(sender: recognizerMap[currentRecognizer],SIGNAL(detected(QString)), |
| 170 | receiver: this,SLOT(detectedShake(QString))); |
| 171 | //! [2] |
| 172 | |
| 173 | recognizerMap.take(akey: currentRecognizer); |
| 174 | |
| 175 | QString str = QString("<font size=+2><B>Stopped %1</b></font><br>" ).arg(a: currentRecognizer); |
| 176 | ui->textEdit->insertHtml(text: str); |
| 177 | ui->textEdit->ensureCursorVisible(); |
| 178 | } |
| 179 | |