summaryrefslogtreecommitdiff
path: root/src/mainwindow.cc
blob: a443476dd9fddea3663712263d8125035a3c8854 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- c++ -*- */
/***************************************************************************
 *            mainwindow.cc
 *
 *  Sat Apr 23 18:59:48 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 "mainwindow.h"

#include <iostream>

#include <QToolBar>
#include <QDockWidget>
#include <QLabel>
#include <QPixmap>
#include <QListWidget>
#include <QPainter>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QAction>

#include "database.h"
#include "viewer.h"

MainWindow::MainWindow(Database& db)
	: db(db)
{
	setWindowTitle("Qookie");

	//client.connectToHost("127.0.0.1");
	client.connectToHost("nanny");

	{
		auto toolbar = new QToolBar();

		{
			auto act = toolbar->addAction("New Scratch Pad");
			connect(act, &QAction::triggered,
			        [&]()
			        {
				        viewer->scratchPad();
			        });
		}

		{
			auto spacer = new QWidget();
			spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
			toolbar->addWidget(spacer);
		}

		{
			auto act = toolbar->addAction("🍪ookie-Cast");
			connect(act, &QAction::triggered,
			        [&]()
			        {
				        const auto& title = viewer->getTitle();
				        const auto& html = viewer->getHtml();
				        client.writeData(title, html.toUtf8());
			        });
			act->setEnabled(false);
			QObject::connect(&client, &Client::isConnected,
			                 [act]()
			                 {
				                 act->setEnabled(true);
			                 });
			QObject::connect(&client, &Client::isDisconnected,
			                 [act]()
			                 {
				                 act->setEnabled(false);
			                 });
		}

		addToolBar(Qt::TopToolBarArea, toolbar);
	}

	{
		auto browser = new QDockWidget("Recipes");
		browser->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
		auto w = new QWidget();
		auto layout = new QVBoxLayout();
		w->setLayout(layout);

		auto e = new QLineEdit();
		layout->addWidget(e);
		connect(e, &QLineEdit::textChanged, this, &MainWindow::filterChanged);

		listWidget = new QListWidget(this);
		listWidget->setIconSize({64, 64});
		QObject::connect(listWidget, &QListWidget::currentRowChanged,
		                 this, &MainWindow::itemChanged);
		listWidget->setSortingEnabled(true);
		layout->addWidget(listWidget);

		browser->setWidget(w);

		addDockWidget(Qt::LeftDockWidgetArea, browser);
	}

	{
		viewer = new Viewer();
		setCentralWidget(viewer);
	}

	readDatabase();
}

MainWindow::~MainWindow()
{
}

void MainWindow::readDatabase()
{
	auto items = db.getRecipes();
	for(const auto& item : items)
	{
		QListWidgetItem* listItem = new QListWidgetItem();
		listItem->setText(QString::fromUtf8(item.title.data()));

		QImage image;
		if(!item.image.empty())
		{
			if(item.image.substr(0, 4) == "/9j/") // Image is base64 encoded
			{
				QByteArray data = QByteArray::fromBase64(QByteArray(item.image.data(), item.image.size()));
				image = QImage::fromData(data);
			}
			else
			{
				image = QImage::fromData((const uchar*)item.image.data(), item.image.size());
			}

			int h = image.height();
			int w = image.width();
			if(h > w)
			{
				image = image.copy({0, (h - w)/2, w, w});
			}
			else
			{
				image = image.copy({(w - h)/2, 0, h, h});
			}
		}
		else
		{
			image = QImage(QSize({64,64}), QImage::Format_RGBA8888);
			image.fill(0);
		}
		image = image.scaled({64,64});

		QString mark;
		QColor mark_color;
		switch(item.db)
		{
		case DatabaseSource::KRecipes:
			mark_color = QColor(0,0,200);
			mark = "K";
			break;
		case DatabaseSource::Gourmet:
			mark_color = QColor(100,100,25);
			mark = "G";
			break;
		case DatabaseSource::Qookie:
			mark_color = QColor(200,0,0);
			mark = "Q";
			break;
		}

		QPainter painter(&image);
		auto font = painter.font();
		font.setBold(true);
		font.setPixelSize(12);
		painter.setFont(font);
		painter.setPen(Qt::transparent);
		painter.setBrush(QColor(255, 255, 255, 180));
		painter.drawEllipse(QRect{-7,-6,20,20});
		painter.setPen(mark_color);
		painter.drawText(0, 10, mark);

		QIcon icon;
		icon.addPixmap(QPixmap::fromImage(image));
		listItem->setIcon(icon);

		listItem->setData(Qt::UserRole, (double)item.id);
		listWidget->addItem(listItem);
	}
}

void MainWindow::itemChanged(int)
{
	auto item = listWidget->currentItem();
	auto id = item->data(Qt::UserRole).toInt();
	auto recipe = db.getRecipe(id);
	viewer->show(recipe);
}

void MainWindow::filterChanged(const QString& text)
{
	QRegExp rx(text);
	rx.setCaseSensitivity(Qt::CaseInsensitive);
	for(int i = 0; i < listWidget->count(); ++i)
	{
		auto item = listWidget->item(i);
		item->setHidden(!item->text().contains(rx) && !text.isEmpty());
	}
}