-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog_type.cpp
More file actions
90 lines (75 loc) · 2.47 KB
/
Copy pathdialog_type.cpp
File metadata and controls
90 lines (75 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* This file is part of compdb.
compdb - Cross plattform Electronic Component Database
Copyright (C) 2016 Mikael Ström
compdb 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.
compdb 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 compdb. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QMessageBox>
#include "dialog_type.h"
#include "ui_dialog_type.h"
DialogType::DialogType(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogType)
{
ui->setupUi(this);
model = new QSqlTableModel(this);
model->setTable("mounting");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setSort(1, Qt::AscendingOrder);
ui->tableView->setModel(model);
ui->tableView->hideColumn(0);
if (!model->select())
QMessageBox::critical(this, "compdb", "Can't open table mounting: " + model->lastError().text());
}
DialogType::~DialogType()
{
delete ui;
}
void DialogType::on_pb_new_clicked()
{
QSqlQuery query;
query.prepare("INSERT INTO mounting (name) VALUES ('<Mount>')");
if (!query.exec())
QMessageBox::critical(this, "compdb", "Can't add mount: " + query.lastError().text());
else {
int inserted_id = query.lastInsertId().toInt();
model->select();
for (int i = 0; i < model->rowCount(); ++i) {
QModelIndex index = model->index(i, 0);
if (model->data(index, Qt::DisplayRole).toInt() == inserted_id) {
QModelIndex item = model->index(i, 1);
ui->tableView->selectRow(i);
ui->tableView->scrollTo(item);
ui->tableView->edit(item);
break;
}
}
}
}
void DialogType::on_pb_delete_clicked()
{
QModelIndexList selected = ui->tableView->selectionModel()->selectedIndexes();
if (selected.count() > 0) {
QModelIndex index = model->index(selected.at(0).row(), 0);
int dbIndex = index.data().toInt();
QSqlQuery query;
query.prepare("DELETE FROM mounting WHERE id = :id");
query.bindValue(":id", dbIndex);
if (!query.exec())
QMessageBox::critical(this, "compdb", "Can't delete mounting: " + query.lastError().text());
model->select();
}
}
void DialogType::on_pb_close_clicked()
{
model->submitAll();
close();
}