src/ProfileMashs.cpp

changeset 49
29cf6e350063
child 75
5f953abbd73c
equal deleted inserted replaced
48:ddd1171ecda5 49:29cf6e350063
1 /**
2 * ProfileMashs.cpp is part of bmsapp.
3 *
4 * bmsapp is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * bmsapp is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "ProfileMashs.h"
18 #include "EditProfileMash.h"
19 #include "../ui/ui_ProfileMashs.h"
20 #include "config.h"
21 #include "bmsapp.h"
22
23
24 ProfileMashs::ProfileMashs(QWidget *parent) : QDialog(parent), ui(new Ui::ProfileMashs)
25 {
26 qDebug() << "ProfileMashs start";
27
28 ui->setupUi(this);
29 emit refreshTable();
30
31 setWindowTitle( QString("BMSapp - %1 - Profile Mashs").arg(VERSIONSTRING) );
32 }
33
34
35 void ProfileMashs::refreshTable()
36 {
37 QString w;
38 QWidget* pWidget;
39 QLabel *label;
40 QHBoxLayout* pLayout;
41
42 qDebug() << "ProfileMashs reload";
43
44 QSqlQuery query("SELECT * FROM profile_mash ORDER BY name");
45 const QStringList labels({tr("Name"), tr("Notes"), tr("Steps"), tr("Edit")});
46
47 ui->tableMashs->setColumnCount(4);
48 ui->tableMashs->setColumnWidth(0, 250); /* Name */
49 ui->tableMashs->setColumnWidth(1, 675); /* Notes */
50 ui->tableMashs->setColumnWidth(2, 75); /* Steps */
51 ui->tableMashs->setColumnWidth(3, 80); /* Edit button */
52 ui->tableMashs->setRowCount(query.size());
53 ui->tableMashs->setHorizontalHeaderLabels(labels);
54 ui->tableMashs->verticalHeader()->hide();
55 ui->tableMashs->setFixedSize(1080 + 24, 640); /* Even if this is too large, it works */
56
57 QTableWidgetItem *rightitem = new QTableWidgetItem();
58 rightitem->setTextAlignment(Qt::AlignRight);
59
60 query.first();
61 for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
62 ui->tableMashs->setItem(ridx, 0, new QTableWidgetItem(query.value(1).toString())); /* Name */
63 ui->tableMashs->setItem(ridx, 1, new QTableWidgetItem(query.value(2).toString())); /* Notes */
64
65 QJsonParseError parseError;
66 const auto& json = query.value(3).toString();
67
68 if (!json.trimmed().isEmpty()) {
69 const auto& formattedJson = QString("%1").arg(json);
70 const auto& doc = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
71
72 if (parseError.error != QJsonParseError::NoError)
73 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
74
75 // qDebug() << " ** " << doc << doc.isArray() << doc.array().size() ;
76
77 w = QString("%1").arg(doc.array().size());
78 QTableWidgetItem *item = new QTableWidgetItem(w);
79 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
80 ui->tableMashs->setItem(ridx, 2, item);
81 }
82
83 /* Add the Edit button */
84 pWidget = new QWidget();
85 QPushButton* btn_edit = new QPushButton();
86 btn_edit->setObjectName(QString("%1").arg(query.value(0).toString())); /* Send record with the button */
87 btn_edit->setText(tr("Edit"));
88 connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editButton_clicked()));
89 pLayout = new QHBoxLayout(pWidget);
90 pLayout->addWidget(btn_edit);
91 pLayout->setContentsMargins(5, 0, 5, 0);
92 pWidget->setLayout(pLayout);
93 ui->tableMashs->setCellWidget(ridx, 3, pWidget);
94 query.next();
95 }
96
97 setWindowTitle( QString("BMSapp - %1 - Profile Mashs").arg(VERSIONSTRING) );
98 }
99
100
101 ProfileMashs::~ProfileMashs()
102 {
103 qDebug() << "ProfileMashs done";
104 delete ui;
105 }
106
107
108 void ProfileMashs::edit(int recno)
109 {
110 qDebug() << "ProfileMashs edit:" << recno;
111
112 EditProfileMash dialog(recno, this);
113 /* Signal from editor if a refresh is needed */
114 connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
115 dialog.setModal(true);
116 dialog.exec();
117 }
118
119
120 void ProfileMashs::on_editButton_clicked()
121 {
122 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
123 int recno = pb->objectName().toInt();
124 qDebug() << Q_FUNC_INFO << recno;
125 edit(recno);
126 }
127
128
129 void ProfileMashs::on_insertButton_clicked()
130 {
131 qDebug() << Q_FUNC_INFO;
132 edit(-1);
133 }
134
135
136 void ProfileMashs::on_quitButton_clicked()
137 {
138 emit firstWindow();
139 }
140

mercurial