/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/#include "mainwindow.h"#include "ui_mainwindow.h"#include "textrecordeditor.h"#include "urirecordeditor.h"#include "mimeimagerecordeditor.h"#include <QtCore/QTime>#include <QtGui/QMenu>#include <QtGui/QVBoxLayout>#include <QtGui/QFrame>#include <QtGui/QLabel>#include <QtGui/QFileDialog>#include <qnearfieldmanager.h>#include <qnearfieldtarget.h>#include <qndefrecord.h>#include <qndefnfctextrecord.h>#include <qndefnfcurirecord.h>#include <qndefmessage.h>#include <QtCore/QDebug>class EmptyRecordLabel : publicQLabel
{
Q_OBJECT
public:
EmptyRecordLabel() : QLabel(tr("Empty Record")) { }
~EmptyRecordLabel() { }
void setRecord(constQNdefRecord&record)
{
Q_UNUSED(record);
}
QNdefRecord record() const
{
returnQNdefRecord();
}
};
class UnknownRecordLabel : publicQLabel
{
Q_OBJECT
public:
UnknownRecordLabel() : QLabel(tr("Unknown Record Type")) { }
~UnknownRecordLabel() { }
void setRecord(constQNdefRecord&record) { m_record = record; }
QNdefRecord record() const { return m_record; }
private:
QNdefRecord m_record;
};
template<typename T>void addRecord(Ui::MainWindow *ui,constQNdefRecord&record =QNdefRecord())
{
QVBoxLayout*vbox = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
if (!vbox)
return;
if (!vbox->isEmpty()) {
QFrame*hline =newQFrame;
hline->setFrameShape(QFrame::HLine);
hline->setObjectName(QLatin1String("line-spacer"));
vbox->addWidget(hline);
}
T *recordEditor =new T;
recordEditor->setObjectName(QLatin1String("record-editor"));
if (!record.isEmpty())
recordEditor->setRecord(record);
vbox->addWidget(recordEditor);
}
MainWindow::MainWindow(QWidget*parent)
: QMainWindow(parent), ui(new Ui::MainWindow), m_manager(newQNearFieldManager(this)),
m_touchAction(NoAction)
{
ui->setupUi(this);
QMenu*addRecordMenu =newQMenu(this);
addRecordMenu->addAction(tr("NFC Text Record"),this, SLOT(addNfcTextRecord()));
addRecordMenu->addAction(tr("NFC URI Record"),this, SLOT(addNfcUriRecord()));
addRecordMenu->addAction(tr("MIME Image Record"),this, SLOT(addMimeImageRecord()));
addRecordMenu->addAction(tr("Empty Record"),this, SLOT(addEmptyRecord()));
ui->addRecord->setMenu(addRecordMenu);
QVBoxLayout*vbox =newQVBoxLayout;
ui->scrollAreaWidgetContents->setLayout(vbox);
connect(m_manager, SIGNAL(targetDetected(QNearFieldTarget*)),this, SLOT(targetDetected(QNearFieldTarget*)));
connect(m_manager, SIGNAL(targetLost(QNearFieldTarget*)),this, SLOT(targetLost(QNearFieldTarget*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addNfcTextRecord()
{
addRecord<TextRecordEditor>(ui);
}
void MainWindow::addNfcUriRecord()
{
addRecord<UriRecordEditor>(ui);
}
void MainWindow::addMimeImageRecord()
{
addRecord<MimeImageRecordEditor>(ui);
}
void MainWindow::addEmptyRecord()
{
addRecord<EmptyRecordLabel>(ui);
}
void MainWindow::loadMessage()
{
QString filename =QFileDialog::getOpenFileName(this, tr("Select NDEF Message"));
if (filename.isEmpty())
return;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
return;
QByteArray ndef = file.readAll();
ndefMessageRead(QNdefMessage::fromByteArray(ndef));
file.close();
}
void MainWindow::saveMessage()
{
QString filename =QFileDialog::getSaveFileName(this, tr("Select NDEF Message"));
if (filename.isEmpty())
return;
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
return;
file.write(ndefMessage().toByteArray());
file.close();
}
void MainWindow::touchReceive()
{
ui->status->setStyleSheet(QLatin1String("background: blue"));
m_touchAction = ReadNdef;
m_manager->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess);
m_manager->startTargetDetection();
}
void MainWindow::touchStore()
{
ui->status->setStyleSheet(QLatin1String("background: yellow"));
m_touchAction = WriteNdef;
m_manager->setTargetAccessModes(QNearFieldManager::NdefWriteTargetAccess);
m_manager->startTargetDetection();
}
void MainWindow::targetDetected(QNearFieldTarget*target)
{
switch (m_touchAction) {
case NoAction:
break;
case ReadNdef:
connect(target, SIGNAL(ndefMessageRead(QNdefMessage)),this, SLOT(ndefMessageRead(QNdefMessage)));
connect(target, SIGNAL(error(QNearFieldTarget::Error,QNearFieldTarget::RequestId)),this, SLOT(targetError(QNearFieldTarget::Error,QNearFieldTarget::RequestId)));
m_request = target->readNdefMessages();
break;
case WriteNdef:
connect(target, SIGNAL(ndefMessagesWritten()),this, SLOT(ndefMessageWritten()));
connect(target, SIGNAL(error(QNearFieldTarget::Error,QNearFieldTarget::RequestId)),this, SLOT(targetError(QNearFieldTarget::Error,QNearFieldTarget::RequestId)));
m_request = target->writeNdefMessages(QList<QNdefMessage>() << ndefMessage());
break;
}
}
void MainWindow::targetLost(QNearFieldTarget*target)
{
target->deleteLater();
}
void MainWindow::ndefMessageRead(constQNdefMessage&message)
{
clearMessage();
foreach (constQNdefRecord&record, message) {
if (record.isRecordType<QNdefNfcTextRecord>()) {
addRecord<TextRecordEditor>(ui, record);
} elseif (record.isRecordType<QNdefNfcUriRecord>()) {
addRecord<UriRecordEditor>(ui, record);
} elseif (record.typeNameFormat() ==QNdefRecord::Mime &&
record.type().startsWith("image/")) {
addRecord<MimeImageRecordEditor>(ui, record);
} elseif (record.isEmpty()) {
addRecord<EmptyRecordLabel>(ui);
} else {
addRecord<UnknownRecordLabel>(ui, record);
}
}
ui->status->setStyleSheet(QString());
m_manager->setTargetAccessModes(QNearFieldManager::NoTargetAccess);
m_manager->stopTargetDetection();
m_request =QNearFieldTarget::RequestId();
ui->statusBar->clearMessage();
}
void MainWindow::ndefMessageWritten()
{
ui->status->setStyleSheet(QString());
m_manager->setTargetAccessModes(QNearFieldManager::NoTargetAccess);
m_manager->stopTargetDetection();
m_request =QNearFieldTarget::RequestId();
ui->statusBar->clearMessage();
}
void MainWindow::targetError(QNearFieldTarget::Error error,constQNearFieldTarget::RequestId &id)
{
Q_UNUSED(error);
Q_UNUSED(id);
if (m_request == id) {
switch (error) {
caseQNearFieldTarget::NoError:
ui->statusBar->clearMessage();
break;
caseQNearFieldTarget::UnsupportedError:
ui->statusBar->showMessage(tr("Unsupported tag"));
break;
caseQNearFieldTarget::TargetOutOfRangeError:
ui->statusBar->showMessage(tr("Tag removed from field"));
break;
caseQNearFieldTarget::NoResponseError:
ui->statusBar->showMessage(tr("No response from tag"));
break;
caseQNearFieldTarget::ChecksumMismatchError:
ui->statusBar->showMessage(tr("Checksum mismatch"));
break;
caseQNearFieldTarget::InvalidParametersError:
ui->statusBar->showMessage(tr("Invalid parameters"));
break;
caseQNearFieldTarget::NdefReadError:
ui->statusBar->showMessage(tr("NDEF read error"));
break;
caseQNearFieldTarget::NdefWriteError:
ui->statusBar->showMessage(tr("NDEF write error"));
break;
default:
ui->statusBar->showMessage(tr("Unknown error"));
}
ui->status->setStyleSheet(QString());
m_manager->setTargetAccessModes(QNearFieldManager::NoTargetAccess);
m_manager->stopTargetDetection();
m_request =QNearFieldTarget::RequestId();
}
}
void MainWindow::clearMessage()
{
QWidget*scrollArea = ui->scrollAreaWidgetContents;
qDeleteAll(scrollArea->findChildren<QWidget*>(QLatin1String("line-spacer")));
qDeleteAll(scrollArea->findChildren<QWidget*>(QLatin1String("record-editor")));
}
QNdefMessage MainWindow::ndefMessage() const
{
QVBoxLayout*vbox = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
if (!vbox)
returnQNdefMessage();
QNdefMessage message;
for (int i =0; i < vbox->count(); ++i) {
QWidget*widget = vbox->itemAt(i)->widget();
if (TextRecordEditor *editor = qobject_cast<TextRecordEditor *>(widget)) {
message.append(editor->record());
} elseif (UriRecordEditor *editor = qobject_cast<UriRecordEditor *>(widget)) {
message.append(editor->record());
} elseif (MimeImageRecordEditor *editor = qobject_cast<MimeImageRecordEditor *>(widget)) {
message.append(editor->record());
} elseif (qobject_cast<EmptyRecordLabel *>(widget)) {
message.append(QNdefRecord());
} elseif (UnknownRecordLabel *label = qobject_cast<UnknownRecordLabel *>(widget)) {
message.append(label->record());
}
}
return message;
}
#include "mainwindow.moc"
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !