src/RecipesTree.cpp

changeset 81
562ed7d1b74d
child 82
84e5dcab868f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/RecipesTree.cpp	Wed Mar 23 22:00:28 2022 +0100
@@ -0,0 +1,196 @@
+/**
+ * RecipesTree.cpp is part of bmsapp.
+ *
+ * bmsapp 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.
+ *
+ * bmsapp 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "RecipesTree.h"
+//#include "EditMisc.h"
+#include "MainWindow.h"
+#include "config.h"
+#include "bmsapp.h"
+
+
+
+RecipesTree::RecipesTree(QWidget *parent) : QDialog(parent)
+{
+    qDebug() << "RecipesTree start";
+
+    gridLayout = new QGridLayout(this);
+    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
+    treeWidget = new QTreeWidget(this);
+    treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
+    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+    sizePolicy.setHorizontalStretch(0);
+    sizePolicy.setVerticalStretch(0);
+    sizePolicy.setHeightForWidth(treeWidget->sizePolicy().hasHeightForWidth());
+    treeWidget->setSizePolicy(sizePolicy);
+    gridLayout->addWidget(treeWidget, 0, 0, 1, 1);
+
+    listView = new QListView(this);
+    listView->setObjectName(QString::fromUtf8("listView"));
+    QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
+    sizePolicy1.setHorizontalStretch(0);
+    sizePolicy1.setVerticalStretch(0);
+    sizePolicy1.setHeightForWidth(listView->sizePolicy().hasHeightForWidth());
+    listView->setSizePolicy(sizePolicy1);
+    listView->setMinimumSize(QSize(500, 0));
+    gridLayout->addWidget(listView, 0, 1, 2, 1);
+
+    groupBox = new QGroupBox(this);
+    groupBox->setObjectName(QString::fromUtf8("groupBox"));
+    QSizePolicy sizePolicy2(QSizePolicy::Preferred, QSizePolicy::Preferred);
+    sizePolicy2.setHorizontalStretch(0);
+    sizePolicy2.setVerticalStretch(0);
+    sizePolicy2.setHeightForWidth(groupBox->sizePolicy().hasHeightForWidth());
+    groupBox->setSizePolicy(sizePolicy2);
+    groupBox->setMinimumSize(QSize(0, 0));
+    horizontalLayout = new QHBoxLayout(groupBox);
+    horizontalLayout->setSpacing(200);
+    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
+    horizontalLayout->setContentsMargins(0, 0, 0, 0);
+
+    quitButton = new QPushButton(groupBox);
+    quitButton->setObjectName(QString::fromUtf8("quitButton"));
+    quitButton->setText(tr("Quit"));
+    QIcon icon;
+    icon.addFile(QString::fromUtf8(":icons/silk/door_out.png"), QSize(), QIcon::Normal, QIcon::Off);
+    quitButton->setIcon(icon);
+    horizontalLayout->addWidget(quitButton);
+
+    insertButton = new QPushButton(groupBox);
+    insertButton->setObjectName(QString::fromUtf8("insertButton"));
+    insertButton->setText(tr("New"));
+    QIcon icon3;
+    icon3.addFile(QString::fromUtf8(":icons/silk/table_row_insert.png"), QSize(), QIcon::Normal, QIcon::Off);
+    insertButton->setIcon(icon3);
+    horizontalLayout->addWidget(insertButton);
+
+    openButton = new QPushButton(groupBox);
+    openButton->setObjectName(QString::fromUtf8("openButton"));
+    openButton->setText(tr("Open"));
+    QIcon icon1;
+    icon1.addFile(QString::fromUtf8(":icons/silk/cup_go.png"), QSize(), QIcon::Normal, QIcon::Off);
+    openButton->setIcon(icon1);
+    horizontalLayout->addWidget(openButton);
+
+    gridLayout->addWidget(groupBox, 1, 0, 1, 1);
+
+    connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromRecipesTree()));
+    //connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked()));
+    //connect(exportButton, SIGNAL(clicked()), this, SLOT(on_exportButton_clicked()));
+
+//    connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
+    emit refreshTable();
+}
+
+
+void RecipesTree::refreshTable()
+{
+    QTreeWidgetItem *st_guide, *st_group, *st_name, *name;
+
+    qDebug() << "RecipesTree reload";
+
+    treeWidget->setRootIsDecorated(false);
+    treeWidget->resizeColumnToContents(2);
+    treeWidget->setColumnCount(5);
+    treeWidget->setHeaderLabels(QStringList() << "Guide" << "Group" << "Style" << "Recipe" << "Record");
+
+    QSqlQuery query0;
+    query0.prepare("SELECT DISTINCT st_guide FROM recipes ORDER BY st_guide");
+    query0.exec();
+    query0.first();
+    for (int i = 0; i < query0.size() ; i++) {
+    	st_guide = new QTreeWidgetItem(treeWidget);
+	st_guide->setText(0, query0.value(0).toString());
+
+	QSqlQuery query1;
+	query1.prepare("SELECT DISTINCT st_letter FROM recipes WHERE st_guide=:guide ORDER BY st_letter");
+	query1.bindValue(":guide", query0.value(0).toString());
+	query1.exec();
+	query1.first();
+	for (int j = 0; j < query1.size(); j++) {
+	    st_group = new QTreeWidgetItem(treeWidget);
+	    st_group->setText(1, query1.value(0).toString());
+	    st_group->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
+	    st_guide->addChild( st_group );
+
+	    QSqlQuery query2;
+            query2.prepare("SELECT DISTINCT st_name FROM recipes WHERE st_guide=:guide AND st_letter=:group ORDER BY st_name");
+            query2.bindValue(":guide", query0.value(0).toString());
+	    query2.bindValue(":group", query1.value(0).toString());
+            query2.exec();
+            query2.first();
+	    for (int k = 0; k < query2.size(); k++) {
+		st_name = new QTreeWidgetItem(treeWidget);
+		st_name->setText(2, query2.value(0).toString());
+		st_group->addChild( st_name );
+
+		QSqlQuery query3;
+            	query3.prepare("SELECT name,record FROM recipes WHERE st_guide=:guide AND st_letter=:group AND st_name=:name ORDER BY name");
+            	query3.bindValue(":guide", query0.value(0).toString());
+            	query3.bindValue(":group", query1.value(0).toString());
+		query3.bindValue(":name", query2.value(0).toString());
+            	query3.exec();
+            	query3.first();
+		for (int l = 0; l < query3.size(); l++) {
+		    name = new QTreeWidgetItem(treeWidget);
+                    name->setText(3, query3.value(0).toString());
+		    name->setText(4, query3.value(1).toString());
+		    st_name->addChild( name );
+
+		    query3.next();
+		}
+
+//		qDebug() << query0.value(0).toString() << query1.value(0).toString() << query2.value(0).toString();
+		query2.next();
+	    }
+	    query1.next();
+	}
+    	query0.next();
+    }
+
+//    treeWidget->expandAll();
+
+//    QSqlQuery query("SELECT * FROM recipes ORDER BY st_guide,st_letter,st_name,name");
+//    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
+}
+
+
+RecipesTree::~RecipesTree() {}
+
+
+void RecipesTree::edit(int recno)
+{
+//    EditMisc dialog(recno, this);
+    /* Signal from editor if a refresh is needed */
+//    connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
+//    dialog.setModal(true);
+//    dialog.exec();
+}
+
+
+void RecipesTree::on_editButton_clicked()
+{
+    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
+    int recno = pb->objectName().toInt();
+    edit(recno);
+}
+
+
+void RecipesTree::on_insertButton_clicked()
+{
+    edit(-1);
+}
+
+

mercurial