-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionTable.cpp
63 lines (57 loc) · 2.64 KB
/
ActionTable.cpp
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
//
// Created by viking on 8/12/18.
//
#include "ActionTable.h"
//#ifndef SQL_SQLSETUP_H
//#include <QDialog>
//#include <QtSql/QSqlDatabase>
//#endif
#include "Paper_Model.h"
#include "sqlForeignKeyDelegate.h"
#include "sqltablehheader.h"
#include "sqltabledelegate.h"
ActionTable::ActionTable(const QString databasename, const QString hostname,const QString username,const QString password, QDialog *parent):QDialog(parent)
{
setupUi(this);
db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(hostname);
db.setDatabaseName(databasename);
db.setUserName(username);
db.setPassword(password);
bool ok=db.open();
// can we open this database?
if(!ok) {
QMessageBox mess;
auto errormessage=QString("ERROR,CANNOT open database error ")+QString(db.lastError().text());
mess.setText(errormessage);
mess.exec();
exit(1);
}
sqlTableView = new SqlTableView(this);
verticalLayout->addWidget(sqlTableView);
model_papers = new Paper_Model(db,this,"papers"); // papers table model, do not populate on this instantiation
model_area = new Paper_Model(db,this,"area","1"); // area table model, populate with query for all database rows
sqlTableHHeader *paperheaderview=new sqlTableHHeader(this,model_papers,model_area); // horizontal headers
sqlTableView->setModel(model_papers);
sqlTableView->setHorizontalHeader(dynamic_cast<QHeaderView *>(paperheaderview));
auto *delegate_area=new sqlForeignKeyDelegate(model_area,sqlTableView); // data from model_area, parent = this delegate controls the behavior, allowing entry of area column data
auto *delegate_papertable = new sqlTableDelegate(sqlTableView);
sqlTableView->setItemDelegate(delegate_papertable);
sqlTableView->setItemDelegateForColumn(AREACOL,dynamic_cast<QAbstractItemDelegate *>(delegate_area));
sqlTableView->setItemDelegateForColumn(AREA2COL,dynamic_cast<QAbstractItemDelegate *>(delegate_area));
sqlTableView->horizontalHeader()->setStretchLastSection(true);
// buttons
connect(undo_all_edits_button,SIGNAL(clicked()),sqlTableView,SLOT(undo_all_edits()));
connect(save_button,SIGNAL(clicked()),model_papers,SLOT(save_model_to_database())); // save model to database
}
// database query for main table
QSqlError ActionTable::query_papers_table(const QString filter) const
{
auto sqlerror=model_papers->query(filter);
sqlTableView->resizeColumnsToContents();
sqlTableView->horizontalHeader()->setStretchLastSection(true);
sqlTableView->hideColumn(0);
sqlTableView->resizeColumnsToContents();
sqlTableView->resizeRowsToContents();
return sqlerror;
}