Added logo load and save in profile setup.

Sat, 05 Mar 2022 10:37:09 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 05 Mar 2022 10:37:09 +0100
changeset 41
dc4b659a320b
parent 40
ccdc1dbc0ebb
child 42
88e827ea7172

Added logo load and save in profile setup.

src/Setup.cpp file | annotate | diff | comparison | revisions
src/Setup.h file | annotate | diff | comparison | revisions
src/bmsapp.h file | annotate | diff | comparison | revisions
translations/bmsapp_en.ts file | annotate | diff | comparison | revisions
translations/bmsapp_nl.ts file | annotate | diff | comparison | revisions
ui/Setup.ui file | annotate | diff | comparison | revisions
--- a/src/Setup.cpp	Wed Mar 02 11:06:07 2022 +0100
+++ b/src/Setup.cpp	Sat Mar 05 10:37:09 2022 +0100
@@ -101,7 +101,12 @@
 	ui->yeastEdit->setCurrentIndex(pos);
     connect(ui->yeastEdit, &QComboBox::currentTextChanged, this, &Setup::is_changed);
 
-    // query.value(2).toString() logo varchar(1024)
+    /* logo */
+    logoByteArray = query.value(2).toByteArray();
+    QPixmap outPixmap = QPixmap();
+    outPixmap.loadFromData(logoByteArray);
+    ui->logoLabel->setPixmap(outPixmap);
+    ui->logoLabel->adjustSize();
 }
 
 
@@ -112,6 +117,65 @@
 }
 
 
+bool Setup::loadFile(const QString &fileName)
+{
+    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 false;
+    }
+    setImage(newImage);
+    setWindowFilePath(fileName);
+    is_changed();
+    return true;
+}
+
+
+void Setup::setImage(const QImage &newImage)
+{
+    image = newImage;
+
+    qDebug() << "setImage" << image.width() << image.height() << "size" << image.sizeInBytes();
+
+    QBuffer buffer(&logoByteArray);
+    buffer.open(QIODevice::WriteOnly);
+    image.save(&buffer, "PNG"); // writes image into logoByteArray in PNG format
+
+    ui->logoLabel->setPixmap(QPixmap::fromImage(image));
+    scaleFactor = 1.0;
+
+//    ui->logoLabel->resize(scaleFactor * ui->logoLabel->pixmap(Qt::ReturnByValue).size());
+    ui->logoLabel->adjustSize();
+}
+
+
+void Setup::on_openButton_clicked()
+{
+    static bool firstDialog = true;
+
+    qDebug() << "Setup open";
+
+    QFileDialog dialog(this, tr("Open File"));
+
+    if (firstDialog) {
+        firstDialog = false;
+        const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
+        dialog.setDirectory(picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
+    }
+
+    /* Only a few image formats are valid */
+    QStringList mimeTypeFilters ({ "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml" });
+    dialog.setMimeTypeFilters(mimeTypeFilters);
+    dialog.setNameFilter("Images (*.bmp *.BMP *.gif *.GIF *.jpg *.JPG *.png *.PNG *.svg *.SVG)");
+    dialog.setAcceptMode(QFileDialog::AcceptOpen);
+
+    while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().constFirst())) {}
+}
+
+
 /*
  * Also called from the Quit button if there are changes to save.
  */
@@ -131,10 +195,11 @@
     /*
      * Update all other data
      */
-    query.prepare("UPDATE profile_setup SET brewery_name=:brewery, factor_mashhop=:mashhop, factor_fwh=:fwh, factor_pellet=:pellet, "
-		  "factor_plug=:plug, factor_wethop=:wet, factor_cryohop=:cryo, color_method=:color, ibu_method=:ibu, "
+    query.prepare("UPDATE profile_setup SET brewery_name=:brewery, brewery_logo=:logo, factor_mashhop=:mashhop, factor_fwh=:fwh, "
+		  "factor_pellet=:pellet, factor_plug=:plug, factor_wethop=:wet, factor_cryohop=:cryo, color_method=:color, ibu_method=:ibu, "
 		  "brix_correction=:brix, grain_absorbtion=:grain, default_water=:water, my_yeastlab=:yeast WHERE record='1'");
     query.bindValue(":brewery", ui->breweryEdit->text());
+    query.bindValue(":logo", logoByteArray);
     query.bindValue(":mashhop", ui->mashhopEdit->value());
     query.bindValue(":fwh", ui->fwhEdit->value());
     query.bindValue(":pellet", ui->pelletEdit->value());
--- a/src/Setup.h	Wed Mar 02 11:06:07 2022 +0100
+++ b/src/Setup.h	Sat Mar 05 10:37:09 2022 +0100
@@ -2,6 +2,11 @@
 #define _SETUP_H
 
 #include <QDialog>
+#include <QFileDialog>
+
+QT_BEGIN_NAMESPACE
+class QLabel;
+QT_END_NAMESPACE
 
 namespace Ui {
 class Setup;
@@ -14,17 +19,24 @@
 public:
     explicit Setup(QWidget *parent = nullptr);
     ~Setup();
+    bool loadFile(const QString &);
 
 signals:
     void firstWindow();
 
 private slots:
+    void on_openButton_clicked();
     void on_quitButton_clicked();
     void on_saveButton_clicked();
     void is_changed();
 
 private:
     Ui::Setup *ui;
+    QByteArray logoByteArray;
+    QImage image;
+    double scaleFactor = 1;
+
+    void setImage(const QImage &newImage);
     bool fieldIsChanged = false;
 };
 
--- a/src/bmsapp.h	Wed Mar 02 11:06:07 2022 +0100
+++ b/src/bmsapp.h	Sat Mar 05 10:37:09 2022 +0100
@@ -22,6 +22,13 @@
 #include <QMessageBox>
 #include <QLabel>
 
+#include <QImage>
+#include <QImageReader>
+#include <QImageWriter>
+#include <QColorSpace>
+#include <QGraphicsView>
+#include <QFileDialog>
+
 #include "Utils.h"
 #include "database/database.h"
 
--- a/translations/bmsapp_en.ts	Wed Mar 02 11:06:07 2022 +0100
+++ b/translations/bmsapp_en.ts	Sat Mar 05 10:37:09 2022 +0100
@@ -973,8 +973,8 @@
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../ui/EditMisc.ui" line="469"/>
-        <location filename="../ui/EditMisc.ui" line="501"/>
+        <location filename="../ui/EditMisc.ui" line="481"/>
+        <location filename="../ui/EditMisc.ui" line="513"/>
         <source>Yes</source>
         <translation type="unfinished"></translation>
     </message>
@@ -2810,24 +2810,34 @@
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../ui/Setup.ui" line="529"/>
-        <location filename="../ui/Setup.ui" line="548"/>
-        <location filename="../ui/Setup.ui" line="567"/>
-        <location filename="../ui/Setup.ui" line="586"/>
+        <location filename="../ui/Setup.ui" line="535"/>
+        <location filename="../ui/Setup.ui" line="560"/>
+        <location filename="../ui/Setup.ui" line="585"/>
+        <location filename="../ui/Setup.ui" line="610"/>
         <source>Choose color</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../ui/Setup.ui" line="615"/>
+        <location filename="../ui/Setup.ui" line="623"/>
+        <source>Logo here</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../ui/Setup.ui" line="639"/>
         <source>Quit</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../ui/Setup.ui" line="632"/>
+        <location filename="../ui/Setup.ui" line="656"/>
         <source>Save</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
+        <location filename="../ui/Setup.ui" line="673"/>
+        <source>Load logo</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
         <location filename="../src/Setup.cpp" line="77"/>
         <source>Choose default water</source>
         <translation type="unfinished"></translation>
@@ -2838,24 +2848,34 @@
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="153"/>
+        <location filename="../src/Setup.cpp" line="126"/>
+        <source>Cannot load %1: %2</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../src/Setup.cpp" line="161"/>
+        <source>Open File</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../src/Setup.cpp" line="218"/>
         <source>Database error</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="154"/>
+        <location filename="../src/Setup.cpp" line="219"/>
         <source>MySQL error: %1
 %2
 %3</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="170"/>
+        <location filename="../src/Setup.cpp" line="235"/>
         <source>Setup changed</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="170"/>
+        <location filename="../src/Setup.cpp" line="235"/>
         <source>The setup has been modified
  Save changes?</source>
         <translation type="unfinished"></translation>
--- a/translations/bmsapp_nl.ts	Wed Mar 02 11:06:07 2022 +0100
+++ b/translations/bmsapp_nl.ts	Sat Mar 05 10:37:09 2022 +0100
@@ -995,8 +995,8 @@
         <translation>Altijd in voorraad:</translation>
     </message>
     <message>
-        <location filename="../ui/EditMisc.ui" line="469"/>
-        <location filename="../ui/EditMisc.ui" line="501"/>
+        <location filename="../ui/EditMisc.ui" line="481"/>
+        <location filename="../ui/EditMisc.ui" line="513"/>
         <source>Yes</source>
         <translation>Ja</translation>
     </message>
@@ -2850,24 +2850,34 @@
         <translation>Plato naar Brix correctie factor.</translation>
     </message>
     <message>
-        <location filename="../ui/Setup.ui" line="529"/>
-        <location filename="../ui/Setup.ui" line="548"/>
-        <location filename="../ui/Setup.ui" line="567"/>
-        <location filename="../ui/Setup.ui" line="586"/>
+        <location filename="../ui/Setup.ui" line="535"/>
+        <location filename="../ui/Setup.ui" line="560"/>
+        <location filename="../ui/Setup.ui" line="585"/>
+        <location filename="../ui/Setup.ui" line="610"/>
         <source>Choose color</source>
         <translation>Kies kleur</translation>
     </message>
     <message>
-        <location filename="../ui/Setup.ui" line="615"/>
+        <location filename="../ui/Setup.ui" line="623"/>
+        <source>Logo here</source>
+        <translation>Logo komt hier</translation>
+    </message>
+    <message>
+        <location filename="../ui/Setup.ui" line="639"/>
         <source>Quit</source>
         <translation>Terug</translation>
     </message>
     <message>
-        <location filename="../ui/Setup.ui" line="632"/>
+        <location filename="../ui/Setup.ui" line="656"/>
         <source>Save</source>
         <translation>Bewaar</translation>
     </message>
     <message>
+        <location filename="../ui/Setup.ui" line="673"/>
+        <source>Load logo</source>
+        <translation>Laad logo</translation>
+    </message>
+    <message>
         <location filename="../src/Setup.cpp" line="77"/>
         <source>Choose default water</source>
         <translation>Kies standaard water</translation>
@@ -2878,12 +2888,22 @@
         <translation>Kies laboratorium</translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="153"/>
+        <location filename="../src/Setup.cpp" line="126"/>
+        <source>Cannot load %1: %2</source>
+        <translation>Kan niet laden %1: %2</translation>
+    </message>
+    <message>
+        <location filename="../src/Setup.cpp" line="161"/>
+        <source>Open File</source>
+        <translation>Open bestand</translation>
+    </message>
+    <message>
+        <location filename="../src/Setup.cpp" line="218"/>
         <source>Database error</source>
         <translation>Database fout</translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="154"/>
+        <location filename="../src/Setup.cpp" line="219"/>
         <source>MySQL error: %1
 %2
 %3</source>
@@ -2892,12 +2912,12 @@
 %3</translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="170"/>
+        <location filename="../src/Setup.cpp" line="235"/>
         <source>Setup changed</source>
         <translation>Instellingen gewijzigd</translation>
     </message>
     <message>
-        <location filename="../src/Setup.cpp" line="170"/>
+        <location filename="../src/Setup.cpp" line="235"/>
         <source>The setup has been modified
  Save changes?</source>
         <translation>De instellingen zijn gewijzigd
--- a/ui/Setup.ui	Wed Mar 02 11:06:07 2022 +0100
+++ b/ui/Setup.ui	Sat Mar 05 10:37:09 2022 +0100
@@ -322,7 +322,7 @@
      <widget class="QLabel" name="grainLabel">
       <property name="geometry">
        <rect>
-        <x>440</x>
+        <x>400</x>
         <y>120</y>
         <width>161</width>
         <height>20</height>
@@ -338,7 +338,7 @@
      <widget class="QLabel" name="brixLabel">
       <property name="geometry">
        <rect>
-        <x>440</x>
+        <x>400</x>
         <y>150</y>
         <width>161</width>
         <height>20</height>
@@ -378,7 +378,7 @@
      <widget class="QLabel" name="colorLabel">
       <property name="geometry">
        <rect>
-        <x>850</x>
+        <x>800</x>
         <y>120</y>
         <width>121</width>
         <height>16</height>
@@ -394,7 +394,7 @@
      <widget class="QLabel" name="ibuLabel">
       <property name="geometry">
        <rect>
-        <x>850</x>
+        <x>800</x>
         <y>150</y>
         <width>121</width>
         <height>16</height>
@@ -410,7 +410,7 @@
      <widget class="QLabel" name="waterLabel">
       <property name="geometry">
        <rect>
-        <x>850</x>
+        <x>800</x>
         <y>180</y>
         <width>121</width>
         <height>16</height>
@@ -426,7 +426,7 @@
      <widget class="QLabel" name="yeastLabel">
       <property name="geometry">
        <rect>
-        <x>850</x>
+        <x>800</x>
         <y>210</y>
         <width>121</width>
         <height>16</height>
@@ -442,7 +442,7 @@
      <widget class="QDoubleSpinBox" name="grainEdit">
       <property name="geometry">
        <rect>
-        <x>620</x>
+        <x>580</x>
         <y>120</y>
         <width>101</width>
         <height>24</height>
@@ -479,7 +479,7 @@
      <widget class="QDoubleSpinBox" name="brixEdit">
       <property name="geometry">
        <rect>
-        <x>620</x>
+        <x>580</x>
         <y>150</y>
         <width>101</width>
         <height>24</height>
@@ -513,7 +513,7 @@
      <widget class="QComboBox" name="colorEdit">
       <property name="geometry">
        <rect>
-        <x>990</x>
+        <x>940</x>
         <y>120</y>
         <width>161</width>
         <height>23</height>
@@ -538,7 +538,7 @@
      <widget class="QComboBox" name="ibuEdit">
       <property name="geometry">
        <rect>
-        <x>990</x>
+        <x>940</x>
         <y>150</y>
         <width>161</width>
         <height>23</height>
@@ -563,7 +563,7 @@
      <widget class="QComboBox" name="waterEdit">
       <property name="geometry">
        <rect>
-        <x>990</x>
+        <x>940</x>
         <y>180</y>
         <width>211</width>
         <height>23</height>
@@ -588,7 +588,7 @@
      <widget class="QComboBox" name="yeastEdit">
       <property name="geometry">
        <rect>
-        <x>990</x>
+        <x>940</x>
         <y>210</y>
         <width>211</width>
         <height>23</height>
@@ -610,27 +610,27 @@
        <string>Choose color</string>
       </property>
      </widget>
-    </widget>
-   </item>
-   <item row="1" column="0">
-    <widget class="QWidget" name="bottomWidget" native="true">
-     <property name="minimumSize">
-      <size>
-       <width>0</width>
-       <height>24</height>
-      </size>
-     </property>
-     <property name="maximumSize">
-      <size>
-       <width>16777215</width>
-       <height>24</height>
-      </size>
-     </property>
+     <widget class="QLabel" name="logoLabel">
+      <property name="geometry">
+       <rect>
+        <x>940</x>
+        <y>260</y>
+        <width>211</width>
+        <height>211</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Logo here</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignCenter</set>
+      </property>
+     </widget>
      <widget class="QPushButton" name="quitButton">
       <property name="geometry">
        <rect>
-        <x>10</x>
-        <y>0</y>
+        <x>50</x>
+        <y>590</y>
         <width>80</width>
         <height>23</height>
        </rect>
@@ -646,8 +646,8 @@
      <widget class="QPushButton" name="saveButton">
       <property name="geometry">
        <rect>
-        <x>1170</x>
-        <y>0</y>
+        <x>1130</x>
+        <y>590</y>
         <width>80</width>
         <height>23</height>
        </rect>
@@ -660,6 +660,23 @@
         <normaloff>:icons/silk/disk.png</normaloff>:icons/silk/disk.png</iconset>
       </property>
      </widget>
+     <widget class="QPushButton" name="openButton">
+      <property name="geometry">
+       <rect>
+        <x>810</x>
+        <y>260</y>
+        <width>111</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Load logo</string>
+      </property>
+      <property name="icon">
+       <iconset resource="../../../../../../home/mbroek/MyProjects/bmsapp/resources/icons.qrc">
+        <normaloff>:/icons/silk/folder_picture.png</normaloff>:/icons/silk/folder_picture.png</iconset>
+      </property>
+     </widget>
     </widget>
    </item>
   </layout>

mercurial