/* -*- 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 #include #include #include #include #include #include #include #include #include #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()); } }