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 QtNfc 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 "mimeimagerecordeditor.h" |
52 | #include "ui_mimeimagerecordeditor.h" |
53 | |
54 | #include <QtGui/QImageReader> |
55 | #include <QtWidgets/QFileDialog> |
56 | #include <QtCore/QBuffer> |
57 | |
58 | static QString imageFormatToMimeType(const QByteArray &format) |
59 | { |
60 | if (format == "bmp" ) |
61 | return QStringLiteral("image/bmp" ); |
62 | else if (format == "gif" ) |
63 | return QStringLiteral("image/gif" ); |
64 | else if (format == "jpg" || format == "jpeg" ) |
65 | return QStringLiteral("image/jpeg" ); |
66 | else if (format == "mng" ) |
67 | return QStringLiteral("video/x-mng" ); |
68 | else if (format == "png" ) |
69 | return QStringLiteral("image/png" ); |
70 | else if (format == "pbm" ) |
71 | return QStringLiteral("image/x-portable-bitmap" ); |
72 | else if (format == "pgm" ) |
73 | return QStringLiteral("image/x-portable-graymap" ); |
74 | else if (format == "ppm" ) |
75 | return QStringLiteral("image/x-portable-pixmap" ); |
76 | else if (format == "tiff" ) |
77 | return QStringLiteral("image/tiff" ); |
78 | else if (format == "xbm" ) |
79 | return QStringLiteral("image/x-xbitmap" ); |
80 | else if (format == "xpm" ) |
81 | return QStringLiteral("image/x-xpixmap" ); |
82 | else if (format == "svg" ) |
83 | return QStringLiteral("image/svg+xml" ); |
84 | else |
85 | return QString(); |
86 | } |
87 | |
88 | MimeImageRecordEditor::MimeImageRecordEditor(QWidget *parent) : |
89 | QWidget(parent), |
90 | ui(new Ui::MimeImageRecordEditor) |
91 | { |
92 | ui->setupUi(this); |
93 | } |
94 | |
95 | MimeImageRecordEditor::~MimeImageRecordEditor() |
96 | { |
97 | delete ui; |
98 | } |
99 | |
100 | void MimeImageRecordEditor::setRecord(const QNdefRecord &record) |
101 | { |
102 | m_record = record; |
103 | |
104 | QByteArray data = record.payload(); |
105 | QBuffer buffer(&data); |
106 | buffer.open(openMode: QIODevice::ReadOnly); |
107 | |
108 | QImageReader reader(&buffer); |
109 | |
110 | ui->mimeImageType->setText(imageFormatToMimeType(format: reader.format())); |
111 | |
112 | ui->mimeImageImage->setPixmap(QPixmap::fromImage(image: reader.read())); |
113 | ui->mimeImageFile->clear(); |
114 | } |
115 | |
116 | QNdefRecord MimeImageRecordEditor::record() const |
117 | { |
118 | return m_record; |
119 | } |
120 | |
121 | void MimeImageRecordEditor::on_mimeImageOpen_clicked() |
122 | { |
123 | QString mimeDataFile = QFileDialog::getOpenFileName(parent: this, caption: tr(s: "Select Image File" )); |
124 | if (mimeDataFile.isEmpty()) |
125 | return; |
126 | |
127 | QFile imageFile(mimeDataFile); |
128 | if (!imageFile.open(flags: QIODevice::ReadOnly)) { |
129 | ui->mimeImageFile->clear(); |
130 | ui->mimeImageImage->clear(); |
131 | } |
132 | |
133 | QByteArray imageData = imageFile.readAll(); |
134 | |
135 | QBuffer buffer(&imageData); |
136 | buffer.open(openMode: QIODevice::ReadOnly); |
137 | |
138 | QImageReader reader(&buffer); |
139 | QString mimeType = imageFormatToMimeType(format: reader.format()); |
140 | ui->mimeImageType->setText(mimeType); |
141 | |
142 | QImage image = reader.read(); |
143 | |
144 | ui->mimeImageFile->setText(mimeDataFile); |
145 | ui->mimeImageImage->setPixmap(QPixmap::fromImage(image)); |
146 | |
147 | m_record.setTypeNameFormat(QNdefRecord::Mime); |
148 | m_record.setType(mimeType.toLatin1()); |
149 | m_record.setPayload(imageData); |
150 | } |
151 | |