src/EditProductTab13.cpp

Wed, 18 Jan 2023 16:17:31 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 18 Jan 2023 16:17:31 +0100
changeset 465
8fc909360552
parent 464
1fed3ff9a64e
child 466
68ef2cc3e8d2
permissions
-rw-r--r--

In EditProduct added tab number 13. When entered, a signal is generated to init this tab so that we can defer loading. Added a AddImage button, we can select an image, give it a type and comment and store it is a separate table. The global settings file now has storage for paths (images, download and beerxml).

/**
 * EditProduct.cpp is part of bmsapp.
 *
 * Tab 13, images and pictures.
 *
 * bmsapp is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * bmsapp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

void EditProduct::images_Init()
{
	qDebug() << "images_Init()";

	// Start spinner
	// Clean old picture areas
	// Load images data for this product uuid
	// Show thumbnails on the left.
	// If any, show picture 1
	// Stop spinner
}


void EditProduct::addImage_clicked()
{
    QString fileName;
    QByteArray imageByteArray;
    QSqlQuery query;
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp");

    QFileDialog dialog1(this, tr("Open File"));
    dialog1.setDirectory(settings.value("paths/images").toString());
    /* Only a few image formats are valid */
    QStringList mimeTypeFilters ({ "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml" });
    dialog1.setMimeTypeFilters(mimeTypeFilters);
    dialog1.setNameFilter("Images (*.bmp *.BMP *.gif *.GIF *.jpg *.JPG *.png *.PNG *.svg *.SVG)");
    dialog1.setAcceptMode(QFileDialog::AcceptOpen);
//    dialog1.setOption(QFileDialog::DontUseNativeDialog);
    if (dialog1.exec() != QDialog::Accepted)
	return;

    /*
     * Save our current path
     */
    settings.setValue("paths/images", dialog1.directory().absolutePath());

    fileName = dialog1.selectedFiles().constFirst();
    QImageReader reader(fileName);
    reader.setAutoTransform(true);
    const QImage newImage = reader.read();
    if (newImage.isNull()) {
	QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load %1: %2")
                                 .arg(QDir::toNativeSeparators(fileName), reader.errorString()));
        return;
    }
    qDebug() << "Image" << fileName << newImage.width() << newImage.height() << "size" << newImage.sizeInBytes();

    QBuffer buffer(&imageByteArray);
    buffer.open(QIODevice::WriteOnly);
    newImage.save(&buffer, "PNG");

    /*
     * Now that we have selected a valid image, create a new dialog so
     * we can add extra information for this image and a Save button.
     */
    QDialog* dialog = new QDialog(this);
    dialog->resize(500, 490);
    QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    buttonBox->setGeometry(QRect(10, 440, 480, 32));
    buttonBox->setLayoutDirection(Qt::LeftToRight);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Save);
    buttonBox->setCenterButtons(true);

    QLabel *imageLabel = new QLabel(dialog);
    imageLabel->setObjectName(QString::fromUtf8("imageLabel"));
    imageLabel->setGeometry(QRect(10, 10, 480, 320));
    imageLabel->setAlignment(Qt::AlignCenter);
    imageLabel->setText(tr("Image here"));
    QPixmap outPixmap = QPixmap();
    outPixmap.loadFromData(imageByteArray);
    if (outPixmap.width() > 480 || outPixmap.height() > 320)
	imageLabel->setPixmap(outPixmap.scaled(480, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    else
	imageLabel->setPixmap(outPixmap);

    QLabel *typeLabel = new QLabel(dialog);
    typeLabel->setObjectName(QString::fromUtf8("typeLabel"));
    typeLabel->setText(tr("Image type:"));
    typeLabel->setGeometry(QRect(10, 360, 141, 20));
    typeLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);

    QComboBox *typeEdit = new QComboBox(dialog);
    typeEdit->setObjectName(QString::fromUtf8("typeEdit"));
    typeEdit->setGeometry(QRect(160, 360, 161, 23));
    for (int i = 0; i < 7; i++)
	typeEdit->addItem(g_prod_pic_types[i]);
    typeEdit->setCurrentIndex(0);

    QLabel *commentLabel = new QLabel(dialog);
    commentLabel->setObjectName(QString::fromUtf8("commentLabel"));
    commentLabel->setText(tr("Image comment:"));
    commentLabel->setGeometry(QRect(10, 390, 141, 20));
    commentLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);

    QLineEdit *commentEdit = new QLineEdit(dialog);
    commentEdit->setObjectName(QString::fromUtf8("commentEdit"));
    commentEdit->setGeometry(QRect(160, 390, 320, 23));
    commentEdit->setToolTip(tr("The comment for this image."));

    connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
    connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));

    dialog->setModal(true);
    dialog->exec();
    if (dialog->result() == QDialog::Rejected) {
	return;
    }
    disconnect(buttonBox, nullptr, nullptr, nullptr);

    query.prepare("INSERT INTO products_pics SET uuid=:uuid, pic_type=:pic_type, pic_data=:pic_data, pic_comment=:pic_comment");
    query.bindValue(":uuid", product->uuid);
    query.bindValue(":pic_type", g_prod_pic_types[typeEdit->currentIndex()]);
    query.bindValue(":pic_data", imageByteArray);
    query.bindValue(":pic_comment", commentEdit->text());

    query.exec();
    if (query.lastError().isValid()) {
	qWarning() << "addImage_clicked()" << query.lastError();
	QMessageBox::warning(this, tr("Database error"), tr("MySQL error: %1\n%2\n%3")
		.arg(query.lastError().nativeErrorCode()).arg(query.lastError().driverText()).arg(query.lastError().databaseText()));
    } else {
	qDebug() << "new image Saved";
    }

    //emit refreshAll();
}

mercurial