15 * |
15 * |
16 * You should have received a copy of the GNU General Public License |
16 * You should have received a copy of the GNU General Public License |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 */ |
18 */ |
19 |
19 |
20 //void EditProduct::taste_date_changed(QDate val) |
20 void EditProduct::images_Init() |
21 //{ |
21 { |
22 // product->taste_date = ui->taste_dateEdit->nullDate(); |
22 qDebug() << "images_Init()"; |
23 // is_changed(); |
23 |
24 // setStage(); |
24 // Start spinner |
25 //} |
25 // Clean old picture areas |
|
26 // Load images data for this product uuid |
|
27 // Show thumbnails on the left. |
|
28 // If any, show picture 1 |
|
29 // Stop spinner |
|
30 } |
26 |
31 |
27 |
32 |
|
33 void EditProduct::addImage_clicked() |
|
34 { |
|
35 QString fileName; |
|
36 QByteArray imageByteArray; |
|
37 QSqlQuery query; |
|
38 QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp"); |
|
39 |
|
40 QFileDialog dialog1(this, tr("Open File")); |
|
41 dialog1.setDirectory(settings.value("paths/images").toString()); |
|
42 /* Only a few image formats are valid */ |
|
43 QStringList mimeTypeFilters ({ "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml" }); |
|
44 dialog1.setMimeTypeFilters(mimeTypeFilters); |
|
45 dialog1.setNameFilter("Images (*.bmp *.BMP *.gif *.GIF *.jpg *.JPG *.png *.PNG *.svg *.SVG)"); |
|
46 dialog1.setAcceptMode(QFileDialog::AcceptOpen); |
|
47 // dialog1.setOption(QFileDialog::DontUseNativeDialog); |
|
48 if (dialog1.exec() != QDialog::Accepted) |
|
49 return; |
|
50 |
|
51 /* |
|
52 * Save our current path |
|
53 */ |
|
54 settings.setValue("paths/images", dialog1.directory().absolutePath()); |
|
55 |
|
56 fileName = dialog1.selectedFiles().constFirst(); |
|
57 QImageReader reader(fileName); |
|
58 reader.setAutoTransform(true); |
|
59 const QImage newImage = reader.read(); |
|
60 if (newImage.isNull()) { |
|
61 QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load %1: %2") |
|
62 .arg(QDir::toNativeSeparators(fileName), reader.errorString())); |
|
63 return; |
|
64 } |
|
65 qDebug() << "Image" << fileName << newImage.width() << newImage.height() << "size" << newImage.sizeInBytes(); |
|
66 |
|
67 QBuffer buffer(&imageByteArray); |
|
68 buffer.open(QIODevice::WriteOnly); |
|
69 newImage.save(&buffer, "PNG"); |
|
70 |
|
71 /* |
|
72 * Now that we have selected a valid image, create a new dialog so |
|
73 * we can add extra information for this image and a Save button. |
|
74 */ |
|
75 QDialog* dialog = new QDialog(this); |
|
76 dialog->resize(500, 490); |
|
77 QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog); |
|
78 buttonBox->setObjectName(QString::fromUtf8("buttonBox")); |
|
79 buttonBox->setGeometry(QRect(10, 440, 480, 32)); |
|
80 buttonBox->setLayoutDirection(Qt::LeftToRight); |
|
81 buttonBox->setOrientation(Qt::Horizontal); |
|
82 buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Save); |
|
83 buttonBox->setCenterButtons(true); |
|
84 |
|
85 QLabel *imageLabel = new QLabel(dialog); |
|
86 imageLabel->setObjectName(QString::fromUtf8("imageLabel")); |
|
87 imageLabel->setGeometry(QRect(10, 10, 480, 320)); |
|
88 imageLabel->setAlignment(Qt::AlignCenter); |
|
89 imageLabel->setText(tr("Image here")); |
|
90 QPixmap outPixmap = QPixmap(); |
|
91 outPixmap.loadFromData(imageByteArray); |
|
92 if (outPixmap.width() > 480 || outPixmap.height() > 320) |
|
93 imageLabel->setPixmap(outPixmap.scaled(480, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation)); |
|
94 else |
|
95 imageLabel->setPixmap(outPixmap); |
|
96 |
|
97 QLabel *typeLabel = new QLabel(dialog); |
|
98 typeLabel->setObjectName(QString::fromUtf8("typeLabel")); |
|
99 typeLabel->setText(tr("Image type:")); |
|
100 typeLabel->setGeometry(QRect(10, 360, 141, 20)); |
|
101 typeLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); |
|
102 |
|
103 QComboBox *typeEdit = new QComboBox(dialog); |
|
104 typeEdit->setObjectName(QString::fromUtf8("typeEdit")); |
|
105 typeEdit->setGeometry(QRect(160, 360, 161, 23)); |
|
106 for (int i = 0; i < 7; i++) |
|
107 typeEdit->addItem(g_prod_pic_types[i]); |
|
108 typeEdit->setCurrentIndex(0); |
|
109 |
|
110 QLabel *commentLabel = new QLabel(dialog); |
|
111 commentLabel->setObjectName(QString::fromUtf8("commentLabel")); |
|
112 commentLabel->setText(tr("Image comment:")); |
|
113 commentLabel->setGeometry(QRect(10, 390, 141, 20)); |
|
114 commentLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); |
|
115 |
|
116 QLineEdit *commentEdit = new QLineEdit(dialog); |
|
117 commentEdit->setObjectName(QString::fromUtf8("commentEdit")); |
|
118 commentEdit->setGeometry(QRect(160, 390, 320, 23)); |
|
119 commentEdit->setToolTip(tr("The comment for this image.")); |
|
120 |
|
121 connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); |
|
122 connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept())); |
|
123 |
|
124 dialog->setModal(true); |
|
125 dialog->exec(); |
|
126 if (dialog->result() == QDialog::Rejected) { |
|
127 return; |
|
128 } |
|
129 disconnect(buttonBox, nullptr, nullptr, nullptr); |
|
130 |
|
131 query.prepare("INSERT INTO products_pics SET uuid=:uuid, pic_type=:pic_type, pic_data=:pic_data, pic_comment=:pic_comment"); |
|
132 query.bindValue(":uuid", product->uuid); |
|
133 query.bindValue(":pic_type", g_prod_pic_types[typeEdit->currentIndex()]); |
|
134 query.bindValue(":pic_data", imageByteArray); |
|
135 query.bindValue(":pic_comment", commentEdit->text()); |
|
136 |
|
137 query.exec(); |
|
138 if (query.lastError().isValid()) { |
|
139 qWarning() << "addImage_clicked()" << query.lastError(); |
|
140 QMessageBox::warning(this, tr("Database error"), tr("MySQL error: %1\n%2\n%3") |
|
141 .arg(query.lastError().nativeErrorCode()).arg(query.lastError().driverText()).arg(query.lastError().databaseText())); |
|
142 } else { |
|
143 qDebug() << "new image Saved"; |
|
144 } |
|
145 |
|
146 //emit refreshAll(); |
|
147 } |
|
148 |
|
149 |