summaryrefslogtreecommitdiff
path: root/src/viewer.cc
blob: b0b6f3558234e95188d8c16ad7a86b5cbb626a3f (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* -*- 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"
#include "client.h"

Viewer::Viewer()
{
	setLayout(new QHBoxLayout());
	textEdit = new QTextEdit();
	layout()->addWidget(textEdit);
	textEdit->setReadOnly(true);
	//client.connectToHost("127.0.0.1");
	client.connectToHost("nanny");
}

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

	QString ingredients;
	for(const auto& ingredient_group : recipe.ingredient_groups)
	{
		if(ingredient_group.title != "")
		{
			ingredients += "<h3>" + QString::fromUtf8(ingredient_group.title.data()) + "</h3>";
		}
		ingredients += "<ul>";
		for(const auto& ingredient : ingredient_group.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>";
			}
		}
		ingredients += "</ul>";
	}

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

	QString db;
	switch(recipe.db)
	{
	case DatabaseSource::KRecipes:
		db = "KRecipes";
		break;
	case DatabaseSource::Gourmet:
		db = "Gourmet";
		break;
	case DatabaseSource::Qookie:
		db = "Qookie";
		break;
	}

	QString html =
		"<center><strong style=\"font-size:28px;\">" + QString::fromUtf8(recipe.title.data()) + "</strong></center><br/>"
		"<table>"
		"<tr>"
		"<td>"
		"<p>" + QString::fromUtf8(recipe.description.data()) + "</p>"
		"<p>"
		"Database: " + db + " (id:" + QString::number(recipe.id) + ")<br/>"
		"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>Ingredienser</h2>" + ingredients +
		"</td>"
		"<td>"
		"<img height=\"300\" src=\"data:image/png;base64," + image.toBase64() + "\"/>"
		"</td>"
		"</tr>"
		"</table>"
		"<h2>Instruktioner</h2>"
		"<p>" + instructions + "</p>"
		;
	textEdit->setHtml(html);

	client.writeData(QString::fromUtf8(recipe.title.data()), html.toUtf8());
}