#include "infoformdialog.h"
#include <QtWidgets>
#include <QtXml>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
InfoFormDialog::InfoFormDialog(QWidget *parent) :
QDialog(parent)
{
this->setWindowFlags(Qt::Window);
this->setFixedSize(904, 590);
//m_fileName = "InfoForm.xml";
m_fileName = "infoform.db";
initInfoFormDialog();
//readXML(m_fileName);
readDatabaseFile(m_fileName);
initTableWidget();
}
InfoFormDialog::~InfoFormDialog()
{
m_personInfoVec.clear();
}
void InfoFormDialog::initInfoFormDialog()
{
m_tableWidget = new TableWidget(this);
m_tableWidget->setFixedSize(880, 500);
m_tableWidget->setColumnCount(6);
m_tableWidget->setColumnWidth(0, 60);
m_tableWidget->setColumnWidth(1, 150);
m_tableWidget->setColumnWidth(2, 70);
m_tableWidget->setColumnWidth(3, 170);
m_tableWidget->setColumnWidth(4, 200);
m_tableWidget->setColumnWidth(5, 200);
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选中一行
m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); //设置为单选模式
m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed); //设置列宽固定
m_tableWidget->setItemDelegate(new ItemDelegate());
QStringList strList;
strList << "Num" << "Name" << "Sex" << "Birthday" << "Card" << "Contact";
m_tableWidget->setHorizontalHeaderLabels(strList);
m_tableWidget->verticalHeader()->setVisible(false);
m_okBtn = new PushButton(this);
m_okBtn->setText(tr("OK"));
m_okBtn->setFixedSize(80, 23);
connect(m_okBtn, SIGNAL(clicked(bool)), this, SLOT(onOk()));
m_cancelBtn = new PushButton(this);
m_cancelBtn->setText(tr("Cancel"));
m_cancelBtn->setFixedSize(80, 23);
connect(m_cancelBtn, SIGNAL(clicked(bool)), this, SLOT(onCancel()));
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addStretch();
hLayout->addWidget(m_okBtn);
hLayout->addWidget(m_cancelBtn);
QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->addWidget(m_tableWidget);
vLayout->addLayout(hLayout);
this->setLayout(vLayout);
}
void InfoFormDialog::initTableWidget()
{
int columnCount = m_tableWidget->columnCount();
for (int i = 0; i < m_personInfoVec.size(); i++) {
m_tableWidget->insertRow(i);
PersonInfo personInfo = m_personInfoVec.at(i);
for (int j = 0; j < columnCount; j++) {
QTableWidgetItem *item = new QTableWidgetItem;
if (!item)
continue;
m_tableWidget->setItem(i, j, item);
item->setTextAlignment(Qt::AlignCenter);
if (j == EnumColumnNum) {
item->setText(personInfo.m_num);
}
else if (j == EnumColumnName) {
item->setText(personInfo.m_name);
}
else if (j == EnumColumnSex) {
item->setText(personInfo.m_sex);
}
else if (j == EnumColumnBirthday) {
item->setText(personInfo.m_birthday);
}
else if (j == EnumColumnCard) {
item->setText(personInfo.m_card);
}
else if (j == EnumColumnContact) {
item->setText(personInfo.m_contact);
}
}
}
}
TableWidget* InfoFormDialog::getTableWidget()
{
return m_tableWidget;
}
void InfoFormDialog::readXML(const QString &filename)
{
QDomDocument doc;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
m_personInfoVec.clear();
QDomElement root = doc.documentElement();
QDomNode node = root.firstChild();
while (!node.isNull()) {
QString tagName = node.toElement().tagName();
if (tagName.compare("info") == 0)
{
PersonInfo personInfo;
QString tempStr = node.toElement().attribute("num"); //获取属性值
personInfo.m_num = tempStr;
tempStr = node.toElement().attribute("name");
personInfo.m_name = tempStr;
tempStr = node.toElement().attribute("sex");
personInfo.m_sex = tempStr;
tempStr = node.toElement().attribute("birthday");
personInfo.m_birthday = tempStr;
tempStr = node.toElement().attribute("card");
personInfo.m_card = tempStr;
tempStr = node.toElement().attribute("contact");
personInfo.m_contact = tempStr;
m_personInfoVec.append(personInfo);
}
node = node.nextSibling();
}
}
void InfoFormDialog::writeXML(const QString &filename)
{
QDomDocument doc;
QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
QDomElement root = doc.createElement("person");
doc.appendChild(root);
int rowCount = m_tableWidget->rowCount();
int columnCount = m_tableWidget->columnCount();
for (int row = 0; row < rowCount; row++) {
QDomElement child = doc.createElement("info");
root.appendChild(child);
for (int col = 0; col < columnCount; col++) {
QTableWidgetItem *item = m_tableWidget->item(row, col);
if (!item) {
continue;
}
QString strItem = item->text();
if (col == EnumColumnNum) {
child.setAttribute("num", strItem);
}
else if (col == EnumColumnName) {
child.setAttribute("name", strItem);
}
else if (col == EnumColumnSex) {
child.setAttribute("sex", strItem);
}
else if (col == EnumColumnBirthday) {
child.setAttribute("birthday", strItem);
}
else if (col == EnumColumnCard) {
child.setAttribute("card", strItem);
}
else if (col == EnumColumnContact) {
child.setAttribute("contact", strItem);
}
}
}
//保存数据
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
return;
QTextStream out(&file);
doc.save(out, 4, QDomNode::EncodingFromTextStream);
file.close();
}
void InfoFormDialog::clearDatabaseFile(const QString &databaseFile)
{
QString fileName = QApplication::applicationDirPath() + "/InfoForm/" + databaseFile;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "infoform");
db.setDatabaseName(fileName);
if (!db.open())
return;
QSqlQuery query(db);
db.transaction();
query.prepare("delete from personinfo");
query.exec();
db.commit();
db.close();
}
void InfoFormDialog::readDatabaseFile(const QString &databaseFile)
{
PersonInfo personInfo;
QString fileName = QApplication::applicationDirPath() + "/InfoForm/" + databaseFile;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "infoform");
db.setDatabaseName(fileName);
if (!db.open())
return;
QSqlQuery query(db);
db.transaction();
query.prepare("select * from personinfo");
query.exec();
while (query.next()) {
personInfo.m_num = query.value(0).toString();
personInfo.m_name = query.value(1).toString();
personInfo.m_sex = query.value(2).toString();
personInfo.m_birthday = query.value(3).toString();
personInfo.m_card = query.value(4).toString();
personInfo.m_contact = query.value(5).toString();
m_personInfoVec.append(personInfo);
}
db.commit();
db.close(