From e5d35d6b643dbb8ecbd3ece2a0d84ec93cadbfc1 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 23 Apr 2022 22:02:06 +0200 Subject: First contact --- src/database.cc | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/database.h | 48 +++++++++++++++++++++ src/mainwindow.cc | 111 +++++++++++++++++++++++++++++++++++++++++++++++ src/mainwindow.h | 48 +++++++++++++++++++++ src/qookie.cc | 45 +++++++++++++++++++ src/recipe.h | 47 ++++++++++++++++++++ 6 files changed, 426 insertions(+) create mode 100644 src/database.cc create mode 100644 src/database.h create mode 100644 src/mainwindow.cc create mode 100644 src/mainwindow.h create mode 100644 src/qookie.cc create mode 100644 src/recipe.h (limited to 'src') diff --git a/src/database.cc b/src/database.cc new file mode 100644 index 0000000..5ae045a --- /dev/null +++ b/src/database.cc @@ -0,0 +1,127 @@ +/* -*- c++ -*- */ +/*************************************************************************** + * database.cc + * + * Sat Apr 23 19:09:12 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 2 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 "database.h" + +#include +#include + +// https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm + +Database::Database(const std::string& file) +{ + auto rc = sqlite3_open(file.data(), &db); // https://sqlite.org/c3ref/open.html + if(rc) + { + std::cerr << "Can't open database: " << sqlite3_errmsg(db) << '\n'; + return; + } + else + { + std::cout << "Opened database successfully\n"; + } +} + +Database::~Database() +{ + sqlite3_close(db); +} + +std::deque Database::getRecipes() +{ + std::deque items; + + std::string sql = "select id, title, image from recipe"; + sqlite3_stmt *statement; + if(sqlite3_prepare_v2(db, sql.data(), sql.length(), &statement, 0) != SQLITE_OK) + { + std::cerr << "Open database failed\n"; + return {}; + } + + int result = 0; + while(true) + { + RecipeItem item; + result = sqlite3_step(statement); + + if(result == SQLITE_ROW) + { + item.id = sqlite3_column_int(statement, 0); + + int title_size = sqlite3_column_bytes(statement, 1); + item.title.append((const char*)sqlite3_column_blob(statement, 1), title_size); + + int image_size = sqlite3_column_bytes(statement, 2); + item.image.append((const char*)sqlite3_column_blob(statement, 2), image_size); + } + else + { + break; + } + items.push_back(item); + } + + return items; +} + +Recipe Database::getRecipe(int id) +{ + // TODO + + std::string sql = "select id, title, image from recipe where id=" + id; + sqlite3_stmt *statement; + if(sqlite3_prepare_v2(db, sql.data(), sql.length(), &statement, 0) != SQLITE_OK) + { + std::cerr << "Open database failed\n"; + return {}; + } + + int result = 0; + while(true) + { + Recipe item; + result = sqlite3_step(statement); + + if(result == SQLITE_ROW) + { + item.id = sqlite3_column_int(statement, 0); + + int title_size = sqlite3_column_bytes(statement, 1); + item.title.append((const char*)sqlite3_column_blob(statement, 1), title_size); + + int image_size = sqlite3_column_bytes(statement, 2); + item.image.append((const char*)sqlite3_column_blob(statement, 2), image_size); + } + else + { + break; + } + return item; + } + + return {}; // Not found +} diff --git a/src/database.h b/src/database.h new file mode 100644 index 0000000..1235551 --- /dev/null +++ b/src/database.h @@ -0,0 +1,48 @@ +/* -*- c++ -*- */ +/*************************************************************************** + * database.h + * + * Sat Apr 23 19:09:12 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 2 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. + */ +#pragma once + +#include +#include +#include + +#include "recipe.h" + +struct sqlite3; + +class Database +{ +public: + Database(const std::string& file); + ~Database(); + + std::deque getRecipes(); + Recipe getRecipe(int id); + +private: + sqlite3 *db{nullptr}; +}; diff --git a/src/mainwindow.cc b/src/mainwindow.cc new file mode 100644 index 0000000..c1b05f7 --- /dev/null +++ b/src/mainwindow.cc @@ -0,0 +1,111 @@ +/* -*- 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 2 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 "database.h" + +MainWindow::MainWindow(Database& db) + : db(db) +{ + //connect(document, SIGNAL(documentStatusChanged(bool)), this, SLOT(updateDocumentStatus(bool))); + //updateDocumentStatus(document->hasChanged()); + + // + // Create the toolbar + // + QToolBar *toolbar = new QToolBar("A toolbar"); + + QAction *act_load = toolbar->addAction("Load"); + // connect(act_load, SIGNAL(triggered()), &document, SLOT(load())); + + QAction *act_save = toolbar->addAction("Save"); + // connect(act_save, SIGNAL(triggered()), &document, SLOT(save())); + + // + // Create the browser docking widget + // + QDockWidget *browser = new QDockWidget("Browser"); + listWidget = new QListWidget(this); + listWidget->setIconSize({128, 128}); + browser->setWidget(listWidget); + + // + // Create the viewer + // +// viewer = new Viewer(); +// setCentralWidget(viewer); + + addToolBar(Qt::TopToolBarArea, toolbar); + addDockWidget(Qt::LeftDockWidgetArea, browser); + +// connect signal: +// void itemChanged(QListWidgetItem *item) in listWidget to itemchanged slot + + readDatabase(); +} + +MainWindow::~MainWindow() +{ +} + +//void MainWindow::updateDocumentStatus(bool changed) +//{ +// setWindowTitle(QString("foo bar") + (changed?"*":"")); +//} + +void MainWindow::readDatabase() +{ + auto items = db.getRecipes(); + for(const auto& item : items) + { + QListWidgetItem* listItem = new QListWidgetItem(); + listItem->setText(QString::fromUtf8(item.title.data())); + if(!item.image.empty()) + { + QIcon icon; + QImage image = QImage::fromData((const uchar*)item.image.data(), item.image.size()); + icon.addPixmap(QPixmap::fromImage(image)); + listItem->setIcon(icon); + } + listItem->setData(Qt::UserRole, item.id); + //listItem->setSizeHint({200,200}); + listWidget->addItem(listItem); + } +} + +//itemchanged slot(item*) +//{ +// id = item->data(Qt::UserRole); +// auto recipe = db.getRecipe(id); +// viewer->show(recipe); +//} diff --git a/src/mainwindow.h b/src/mainwindow.h new file mode 100644 index 0000000..210bf5e --- /dev/null +++ b/src/mainwindow.h @@ -0,0 +1,48 @@ +/* -*- c++ -*- */ +/*************************************************************************** + * mainwindow.h + * + * 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 2 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. + */ +#pragma once + +#include +class QListWidget; +class Database; + +class MainWindow : + public QMainWindow +{ +Q_OBJECT +public: + MainWindow(Database& db); + ~MainWindow(); + +public slots: +// void updateDocumentStatus(bool changed); + +private: + void readDatabase(); + QListWidget *listWidget; + Database& db; +}; diff --git a/src/qookie.cc b/src/qookie.cc new file mode 100644 index 0000000..2a75a63 --- /dev/null +++ b/src/qookie.cc @@ -0,0 +1,45 @@ +/* -*- c++ -*- */ +/*************************************************************************** + * qookie.cc + * + * Sat Apr 23 18:40:39 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 2 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 + +#include + +#include "mainwindow.h" +#include "database.h" + +int main(int argc, char *argv[]) +{ + QApplication qapp(argc, argv); + + Database db("/mnt/atuin/docs/tine/Fritid/Mad/recipes.db"); + //Database db("/mnt/atuin/docs/tine/Fritid/Mad/krecipes.krecdb"); + + MainWindow mainwindow(db); + mainwindow.show(); + + return qapp.exec(); +} diff --git a/src/recipe.h b/src/recipe.h new file mode 100644 index 0000000..8e4b579 --- /dev/null +++ b/src/recipe.h @@ -0,0 +1,47 @@ +/* -*- c++ -*- */ +/*************************************************************************** + * recipe.h + * + * Sat Apr 23 21:29:02 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 2 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. + */ +#pragma once + +#include + +struct RecipeItem +{ + int id; + std::string title; + std::string image; +}; + +struct Recipe +{ + int id; + std::string title; + std::string image; + + // . + // . + // . +}; -- cgit v1.2.3