summaryrefslogtreecommitdiff
path: root/src/viewer.cc
blob: 9ed0dba6438a66b77a45c62110e96e68bde479a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- c++ -*- */
/***************************************************************************
 *            viewer.cc
 *
 *  Sun Apr 24 09:04:57 CEST 2022
 *  Copyright 2022 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Qookie.
 *
 *  Qookie 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.
 *
 *  Qookie 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 Qookie; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "viewer.h"

#include <iostream>

#include <QTextEdit>
#include <QHBoxLayout>

#include "recipe.h"

Viewer::Viewer()
{
	setLayout(new QHBoxLayout());
	textEdit = new QTextEdit();
	layout()->addWidget(textEdit);
	textEdit->setReadOnly(true);
}

void Viewer::show(const Recipe& recipe)
{
	auto instructions = QString::fromUtf8(recipe.instructions.data());
	instructions.replace("\n", "<br/>");

	QString ingredients;
	for(const auto& ingredient : recipe.ingredients)
	{
		if(ingredient.amount > 0)
		{
			ingredients +=
				"<li>" +
				QString::number(ingredient.amount) + " " +
				QString::fromUtf8(ingredient.unit.data()) + " " +
				QString::fromUtf8(ingredient.item.data()) +
				"</li>";
		}
		else
		{
			ingredients +=
				"<li>" +
				QString::fromUtf8(ingredient.item.data()) +
				"</li>";
		}
	}

	QByteArray image(recipe.image.data(), recipe.image.size());
	if(recipe.image.substr(0, 4) == "/9j/") // Image is base64 encoded
	{
		image = QByteArray::fromBase64(image);
	}

	QString html =
		"<center><strong style=\"font-size:28px;\">" + QString::fromUtf8(recipe.title.data()) + "</strong></center><br/>"
		"<p>"
		"<img height=\"300\" src=\"data:image/png;base64," + image.toBase64() + "\"/>"
		"</p>"
		"<p>" + QString::fromUtf8(recipe.description.data()) + "</p>"
		"<p>"
		"Source: " + QString::fromUtf8(recipe.source.data()) + "<br/>"
		"Cuisine: " + QString::fromUtf8(recipe.cuisine.data()) + "<br/>"
		"Cooktime: " + QString::number(recipe.cooktime / 60) + "min<br/>"
		"Preptime: " + QString::number(recipe.preptime / 60) + "min<br/>"
		"Yields: " + QString::number(recipe.yields) + " " + QString::fromUtf8(recipe.yield_unit.data()) + "<br/>"
		"Tags: " + QString::fromUtf8(recipe.tags[0].data()) + ""
		"</p>"
		"<h2>Ingredients</h2>"
		"<ul>" + ingredients + "</ul>"
		"<h2>Instructions</h2>"
		"<p>" + instructions + "</p>"
		;
	textEdit->setHtml(html);
}