Solved the segmentation fault when viewing recipes twice. Set colored display for the EBC color fields using a modified dynamic stylesheet.

Fri, 01 Apr 2022 14:58:57 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 01 Apr 2022 14:58:57 +0200
changeset 98
1425bf3e18ed
parent 97
8283bbf95806
child 99
053c0578cf58

Solved the segmentation fault when viewing recipes twice. Set colored display for the EBC color fields using a modified dynamic stylesheet.

src/EditRecipe.cpp file | annotate | diff | comparison | revisions
src/RecipesTree.cpp file | annotate | diff | comparison | revisions
src/Utils.cpp file | annotate | diff | comparison | revisions
src/Utils.h file | annotate | diff | comparison | revisions
--- a/src/EditRecipe.cpp	Thu Mar 31 23:10:57 2022 +0200
+++ b/src/EditRecipe.cpp	Fri Apr 01 14:58:57 2022 +0200
@@ -34,6 +34,12 @@
     ui->typeEdit->addItem(tr("Partial Mash"));
     ui->typeEdit->addItem(tr("All Grain"));
 
+    ui->color_methodEdit->addItem("Morey");
+    ui->color_methodEdit->addItem("Mosher");
+    ui->color_methodEdit->addItem("Daniels");
+    ui->color_methodEdit->addItem("Halberstadt");
+    ui->color_methodEdit->addItem("Naudts");
+
     if (id >= 0) {
 	query.prepare("SELECT * FROM recipes WHERE record = :recno");
 	query.bindValue(":recno", id);
@@ -87,17 +93,13 @@
 
 	ui->est_colorEdit->setValue(query.value(31).toDouble());
 	QColor color = Utils::ebc_to_color(query.value(31).toInt());
+	ui->est_colorEdit->setStyleSheet(Utils::ebc_to_style(query.value(31).toInt()));
 	ui->est_colorGlass->setColor(color);
         ui->est_colorShow->setPrecision(0);
-        ui->est_colorShow->setMarkerTextIsValue(true);
+        ui->est_colorShow->setMarkerTextIsValue(false);
 	ui->est_colorShow->setRange(query.value(15).toDouble(), query.value(16).toDouble());
         ui->est_colorShow->setValue(query.value(31).toDouble());
 
-	ui->color_methodEdit->addItem("Morey");
-    	ui->color_methodEdit->addItem("Mosher");
-    	ui->color_methodEdit->addItem("Daniels");
-    	ui->color_methodEdit->addItem("Halberstadt");
-    	ui->color_methodEdit->addItem("Naudts");
 	ui->color_methodEdit->setCurrentIndex(query.value(32).toInt());
 
 	// 33 est_ibu
@@ -291,7 +293,7 @@
 void EditRecipe::on_quitButton_clicked()
 {
     if (this->textIsChanged) {
-	int rc = QMessageBox::warning(this, tr("Recipe changed"), tr("The ingredient has been modified. Save changes?"),
+	int rc = QMessageBox::warning(this, tr("Recipe changed"), tr("The recipe has been modified. Save changes?"),
                                 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
         switch (rc) {
             case QMessageBox::Save:
--- a/src/RecipesTree.cpp	Thu Mar 31 23:10:57 2022 +0200
+++ b/src/RecipesTree.cpp	Fri Apr 01 14:58:57 2022 +0200
@@ -18,7 +18,7 @@
 #include "MainWindow.h"
 #include "EditRecipe.h"
 #include "config.h"
-
+#include "Utils.h"
 
 
 RecipesTree::RecipesTree(QWidget *parent) : QDialog(parent)
@@ -253,6 +253,19 @@
     connect(openButton, SIGNAL(clicked()), this, SLOT(on_openButton_clicked()));
 
     connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
+
+//    treeWidget->setAutoExpandDelay(1);
+//    treeWidget->setUpdatesEnabled(true);
+//    treeWidget->collapseAll();
+//    treeWidget->setRootIsDecorated(false);
+    treeWidget->setColumnCount(4);
+    treeWidget->setHeaderLabels({ tr("Guide"), tr("Group"), tr("Style"), tr("Recipe") });
+    treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
+//    treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
+
+    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_clicked(QTreeWidgetItem*, int)));
+    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_doubleclicked(QTreeWidgetItem*, int)));
+
     emit refreshTable();
 }
 
@@ -264,17 +277,6 @@
     qDebug() << "RecipesTree reload" << treeWidget->autoExpandDelay();
 
     treeWidget->clear();
-//    treeWidget->setAutoExpandDelay(1);
-//    treeWidget->setUpdatesEnabled(true);
-//    treeWidget->collapseAll();
-//    treeWidget->setRootIsDecorated(false);
-    treeWidget->setColumnCount(4);
-    treeWidget->setHeaderLabels({ tr("Guide"), tr("Group"), tr("Style"), tr("Recipe") });
-    treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
-//    treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
-
-    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_clicked(QTreeWidgetItem*, int)));
-    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_doubleclicked(QTreeWidgetItem*, int)));
 
     QSqlQuery query0;
     query0.prepare("SELECT DISTINCT st_guide FROM recipes ORDER BY st_guide");
@@ -358,7 +360,6 @@
 	return;
     }
 
-    qDebug() << "showRecipe 2";
     /*
      * Fill in basic details of the selected recipe.
      */
@@ -366,13 +367,9 @@
     query.prepare("SELECT batch_size,boil_size,boil_time,efficiency,est_og,est_fg,est_abv,est_color,color_method,est_ibu,ibu_method,est_carb,notes,name "
 		  "FROM recipes WHERE record=:record");
     query.bindValue(":record", record);
-    qDebug() << "showRecipe 3";
     query.exec();
-    qDebug() << "showRecipe 4";
     if (query.size() == 1) {
-	qDebug() << "showRecipe 5 ok";
 	query.first();
-	qDebug() << "showRecipe 6";
 	volumeEdit->setValue(query.value(0).toDouble());
 	boilvolumeEdit->setValue(query.value(1).toDouble());
 	boiltimeEdit->setValue(query.value(2).toInt());
@@ -381,6 +378,7 @@
 	fgEdit->setValue(query.value(5).toDouble());
 	abvEdit->setValue(query.value(6).toInt());
 	colorEdit->setValue(query.value(7).toInt());
+	colorEdit->setStyleSheet(Utils::ebc_to_style(query.value(7).toInt()));
 	colormethodEdit->setText(c_method[query.value(8).toInt()]);
 	ibuEdit->setValue(query.value(9).toInt());
 	ibumethodEdit->setText(i_method[query.value(10).toInt()]);
@@ -388,11 +386,10 @@
 	remarksEdit->setPlainText(query.value(12).toString());
 	recipeBox->setTitle(query.value(13).toString());
 	recipeBox->setEnabled(true);
-	qDebug() << "showRecipe 7";
     } else {
-	    qDebug() << "showRecipe 5 error";
 	qDebug() << "Error getting recipe record" << record;
     }
+    qDebug() << "showRecipe done";
 }
 
 
--- a/src/Utils.cpp	Thu Mar 31 23:10:57 2022 +0200
+++ b/src/Utils.cpp	Fri Apr 01 14:58:57 2022 +0200
@@ -35,9 +35,16 @@
 }
 
 
+/**
+ * Often used formulas divide or multiply with 1.97 to convert between EBC and SRM.
+ * Almost all software in the world use this '1.97' formula.
+ * The only alternative I have seen is "srm = (ebc * 0.375 + 0.46)", and that has
+ * almost the same results as the formulas used in this program.
+ * These formulas come from the Dutch 'brouwhulp' program written by Adrie Otten.
+ */
 double Utils::ebc_to_srm(double ebc)
 {
-    double srm = -1.32303E-12 * pow(ebc, 4) - 0.00000000291515 * pow(ebc, 3) + 0.00000818515 * pow(ebc, 2) + 0.372038 * ebc + 0.596351;
+    double srm = -0.00000000000132303 * pow(ebc, 4) - 0.00000000291515 * pow(ebc, 3) + 0.00000818515 * pow(ebc, 2) + 0.372038 * ebc + 0.596351;
     if (ebc < 0 || srm < 0)
 	qDebug() << "ebc_to_srm(" << ebc << ") =" << srm;
     return srm;
@@ -46,7 +53,6 @@
 
 double Utils::srm_to_ebc(double srm)
 {
-    // Formule van Adrie Otten. brouwhulp.
     double ebc = round( 0.000000000176506 * pow(srm, 4) + 0.000000154529 * pow(srm, 3) - 0.000159428 * pow(srm, 2) + 2.68837 * srm - 1.6004 );
     if ((ebc < 0) || (srm < 0))
 	qDebug() << "srm_to_ebc(" << srm << ") =" << ebc;
@@ -54,7 +60,6 @@
 }
 
 
-
 QString Utils::hours_to_string(int hours)
 {
     int dd, hh;
@@ -96,6 +101,7 @@
     if (i > 299)
 	i = 299;
 
+    // A well known table for SRM to RGB conversion, range 0.1 to 30 SRM.
     const int R[] {
  250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, //0
  250, 250, 250, 250, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, //2
@@ -147,8 +153,8 @@
  15, 14, 14, 14, 13, 13, 13, 12, 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 8,
  8, 8, 7, 7, 7, 6, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2};
 
- result = QColor::fromRgb(R[i], G[i], B[i]);
- return result;
+    result = QColor::fromRgb(R[i], G[i], B[i]);
+    return result;
 }
 
 
@@ -158,3 +164,15 @@
 }
 
 
+QString Utils::srm_to_style(int srm)
+{
+    QColor color = srm_to_color(srm);
+    return QString("background-color: %1; color: %2;").arg(color.name()).arg((srm > 15) ? "#E0E1E3" : "#19232D");
+}
+
+
+QString Utils::ebc_to_style(int ebc)
+{
+    return srm_to_style(ebc_to_srm(ebc));
+}
+
--- a/src/Utils.h	Thu Mar 31 23:10:57 2022 +0200
+++ b/src/Utils.h	Fri Apr 01 14:58:57 2022 +0200
@@ -18,8 +18,34 @@
     double srm_to_ebc(double srm);
 
     QString hours_to_string(int hours);
+
+    /**
+     * @brief Map SRM color to RGB and return as QColor.
+     * @param srm The srm value.
+     * @return QColor
+     */
     QColor srm_to_color(int srm);
+
+    /**
+     * @brief Map EBC color to RGB and return as QColor.
+     * @param ebc The ebc value.
+     * @return QColor
+     */
     QColor ebc_to_color(int ebc);
+
+    /**
+     * @brief Map SRM color to a QString for stylesheets.
+     * @param srm The srm value.
+     * @return A QString with stylesheet colors.
+     */
+    QString srm_to_style(int ebc);
+
+    /**
+     * @brief Map EBC color to a QString for stylesheets.
+     * @param ebc The ebc value.
+     * @return A QString with stylesheet colors.
+     */
+    QString ebc_to_style(int srm);
 }
 
 #endif

mercurial