File indexing completed on 2025-01-05 05:22:00

0001 /*
0002  * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk>
0003  *
0004  * Permission is hereby granted, free of charge, to any person
0005  * obtaining a copy of this software and associated documentation
0006  * files (the "Software"), to deal in the Software without
0007  * restriction, including without limitation the rights to use,
0008  * copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following
0011  * conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be
0014  * included in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0018  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0020  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0021  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0022  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 #include "metadatapane.h"
0026 #include "recipeparser.h"
0027 #include <QStandardItemModel>
0028 #include <QTreeView>
0029 #include <QVBoxLayout>
0030 #include <QFile>
0031 
0032 MetaDataPane::MetaDataPane(QWidget* parent) : PaneBase(parent)
0033 {
0034     m_model = std::make_unique<QStandardItemModel>();
0035     auto layout = std::make_unique<QVBoxLayout>();
0036     auto treeview = std::make_unique<QTreeView>();
0037     treeview->setModel(m_model.get());
0038     treeview->setHeaderHidden(true);
0039     layout->addWidget(treeview.release());
0040     setLayout(layout.release());
0041 }
0042 
0043 MetaDataPane::~MetaDataPane()
0044 {
0045     // for smart pointers
0046 }
0047 
0048 
0049 void MetaDataPane::openPath(const QString& path)
0050 {
0051     m_model->clear();
0052 
0053     if (path.isEmpty() || !QFile::exists(path)) {
0054         return;
0055     }
0056 
0057     QFile f(path);
0058     f.open(QIODevice::ReadOnly);
0059     auto parsedRecipe = RecipeParser::parseRecipe(&f);
0060     f.close();
0061 
0062 
0063     if (parsedRecipe.title.isEmpty() && parsedRecipe.tags.isEmpty() && parsedRecipe.ingredients.isEmpty()) {
0064         return;
0065     }
0066 
0067     m_model->appendRow(new QStandardItem(parsedRecipe.title));
0068 
0069     auto ingredients = std::make_unique<QStandardItem>(QString("ingredients (%1)").arg(parsedRecipe.ingredients.size()));
0070     for(auto ingredient : qAsConst(parsedRecipe.ingredients)) {
0071         ingredients->appendRow(new QStandardItem(ingredient.ingredient));
0072     }
0073     m_model->appendRow(ingredients.release());
0074 
0075     auto tags  = std::make_unique<QStandardItem>(QString("tags (%1)").arg(parsedRecipe.tags.size()));
0076     for(auto tag : qAsConst(parsedRecipe.tags)) {
0077         tags->appendRow(new QStandardItem(tag));
0078     }
0079     m_model->appendRow(tags.release());
0080     auto meta = std::make_unique<QStandardItem>(QString("meta (%1)").arg(parsedRecipe.otherMeta.size()));
0081     for(auto it = parsedRecipe.otherMeta.constBegin(), end = parsedRecipe.otherMeta.constEnd(); it != end; it++) {
0082         auto child = std::make_unique<QStandardItem>(it.key());
0083         const auto values = it.value();
0084         for(const auto& value : values)
0085         {
0086             child->appendRow(new QStandardItem(value));
0087         }
0088         meta->appendRow(child.release());
0089     }
0090     m_model->appendRow(meta.release());
0091 
0092 }
0093