243 lines
6.8 KiB
C++
243 lines
6.8 KiB
C++
#include <front/pages/sqlControlWidget.h>
|
|
#include <qlabel.h>
|
|
#include <qcombobox.h>
|
|
#include <qlineedit.h>
|
|
#include <qpushbutton.h>
|
|
#include <qstandarditemmodel.h>
|
|
#include <qlayout.h>
|
|
#include <qtablewidget.h>
|
|
#include <appCore.hpp>
|
|
|
|
sqliteReader::sqliteReader()
|
|
{
|
|
readIniFile(".//config//sqliteReader.ini");
|
|
//_sqlManager = &_core._appConfig._sqlManager;
|
|
//std::vector<std::string> res=_sqlManager->getTableList();
|
|
//widgetInit(res);
|
|
}
|
|
|
|
sqliteReader::~sqliteReader()
|
|
{
|
|
//_sqlManager.uninitlize();
|
|
}
|
|
void sqliteReader::widgetInit(std::vector<std::string>tables)
|
|
{
|
|
//1
|
|
layout1 = new QVBoxLayout();
|
|
this->setLayout(layout1);
|
|
|
|
//1-1
|
|
QWidget* vLayout2Widget = new QWidget();
|
|
QHBoxLayout * vLayoutt2 = new QHBoxLayout();
|
|
vLayout2Widget->setLayout(vLayoutt2);
|
|
layout1->addWidget(vLayout2Widget);
|
|
|
|
//1-1-1
|
|
QLabel* vLayout2Label = new QLabel(u8"请选择日期");
|
|
vLayoutt2->addWidget(vLayout2Label);
|
|
|
|
QComboBox * Layou2MBox = new QComboBox();
|
|
for (auto &data : tables)
|
|
{
|
|
Layou2MBox->addItem(QString::fromStdString(data));
|
|
}
|
|
vLayoutt2->addWidget(Layou2MBox);
|
|
connect(Layou2MBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=]() {switchWidget(Layou2MBox->currentText().toStdString()); });
|
|
|
|
QLabel* vLayout2Label2 = new QLabel(u8"根据SN查询");
|
|
vLayoutt2->addWidget(vLayout2Label2);
|
|
|
|
QLineEdit *Layout2LEdit = new QLineEdit();
|
|
vLayoutt2->addWidget(Layout2LEdit);
|
|
|
|
QPushButton* layout2QPBtn = new QPushButton(u8"查询");
|
|
vLayoutt2->addWidget(layout2QPBtn);
|
|
|
|
QPushButton* dumpCsvBtn = new QPushButton(u8"导出为csv");
|
|
vLayoutt2->addWidget(dumpCsvBtn);
|
|
|
|
connect(layout2QPBtn,& QPushButton::clicked, this, [=]() {searchSN(Layou2MBox->currentText().toStdString(), Layout2LEdit->text().toStdString()); });
|
|
connect(dumpCsvBtn, &QPushButton::clicked, this, [=]() {_dumpToExcel.mainWidget->show(); });
|
|
//1-2
|
|
tableWidget = new QTableWidget();
|
|
|
|
|
|
layout1->addWidget(tableWidget);
|
|
switchWidget(Layou2MBox->currentText().toStdString());
|
|
|
|
}
|
|
void sqliteReader::switchWidget(std::string tableName)
|
|
{
|
|
#if 0
|
|
sqlTableDatas res = _sqlManager->selectAllData(tableName);
|
|
tableWidget->clear();
|
|
delete tableWidget;
|
|
tableWidget = new QTableWidget();
|
|
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
layout1->addWidget(tableWidget);
|
|
if (res.rowDatas.size() > 0)
|
|
{
|
|
int colsNum = res.rowDatas[0].size() + 1 - avoidMap.size();
|
|
tableWidget->setColumnCount(colsNum);
|
|
//tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("SN"));
|
|
//for (int i = 0; i < res.at(0).dataVec.size(); i++)
|
|
//创建列名
|
|
{
|
|
int index = 0;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
std::unordered_map<QString, QString>::iterator it2;
|
|
|
|
for (auto&data : res.rowDatas[0])
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.key));
|
|
if (it == avoidMap.end())
|
|
{
|
|
QString Name = QString::fromStdString(data.key);
|
|
it2 = cuttomName.find(Name);
|
|
if (it2 != cuttomName.end())
|
|
Name = it2->second;
|
|
tableWidget->setHorizontalHeaderItem(index, new QTableWidgetItem(Name));
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
//tableWidget->setColumnCount(res.size());
|
|
for (int i = 0; i < res.rowDatas.size(); i++)
|
|
{
|
|
//for (auto& data : res.rowDatas[0])
|
|
{
|
|
int row = tableWidget->rowCount();
|
|
tableWidget->insertRow(row);
|
|
//tableWidget->setItem(row, 0, new QTableWidgetItem(QString::fromStdString(data.key)));
|
|
{
|
|
int index = 0;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
for (auto& data : res.rowDatas[i])
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.key));
|
|
if (it == avoidMap.end())
|
|
{
|
|
tableWidget->setItem(row, index, new QTableWidgetItem(QString::fromStdString(data.value)));
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
//tableWidget->horizontalHeader
|
|
}
|
|
void sqliteReader::searchSN(std::string tableName, std::string SN)
|
|
{
|
|
#if 0
|
|
sqlTableDatas res = _sqlManager->selectRowData(SN);
|
|
tableWidget->clear();
|
|
delete tableWidget;
|
|
tableWidget = new QTableWidget();
|
|
layout1->addWidget(tableWidget);
|
|
if (res.rowDatas.size()>0)//非空
|
|
{
|
|
tableWidget->setColumnCount(res.rowDatas[0].size() - avoidMap.size());
|
|
//tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("SN"));
|
|
{
|
|
int index = 0;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
std::unordered_map<QString, QString>::iterator it2;
|
|
for (auto&data : res.rowDatas[0])
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.key));
|
|
if (it == avoidMap.end())
|
|
{
|
|
QString Name = QString::fromStdString(data.key);
|
|
it2 = cuttomName.find(Name);
|
|
if (it2 != cuttomName.end())
|
|
Name = it2->second;
|
|
tableWidget->setHorizontalHeaderItem(index, new QTableWidgetItem(Name));
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
|
|
//tableWidget->setColumnCount(res.dataVec.size());
|
|
|
|
int row = tableWidget->rowCount();
|
|
tableWidget->insertRow(row);
|
|
{
|
|
int index = 0;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
for (auto&data : res.rowDatas[0])
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.key));
|
|
if (it == avoidMap.end())
|
|
{
|
|
tableWidget->setItem(row, index, new QTableWidgetItem(QString::fromStdString(data.value)));
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(res.rowDatas.size()!=0)
|
|
{
|
|
tableWidget->setColumnCount(res.rowDatas[0].size() + 1);
|
|
tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("SN"));
|
|
tableWidget->setColumnCount(1);
|
|
}
|
|
|
|
int row = tableWidget->rowCount();
|
|
tableWidget->insertRow(row);
|
|
tableWidget->setItem(row, 0, new QTableWidgetItem(u8"找不到该数据!"));
|
|
}
|
|
|
|
#endif
|
|
//tableWidget->horizontalHeader
|
|
}
|
|
|
|
bool sqliteReader::readIniFile(const QString &filePath) {
|
|
//std::unordered_map<QString, QString> avoiData;
|
|
//std::unordered_map<QString, QString> cutomName;
|
|
QFile file(filePath);
|
|
|
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
QTextStream in(&file);
|
|
QString currentSection;
|
|
|
|
while (!in.atEnd()) {
|
|
QString line = in.readLine().trimmed();
|
|
if (line.startsWith('[') && line.endsWith(']')) {
|
|
// This line is a section header
|
|
currentSection = line.mid(1, line.length() - 2);
|
|
}
|
|
else if (!line.isEmpty() && !line.startsWith(';') && !currentSection.isEmpty()) {
|
|
// This line is a key-value pair within a section
|
|
int separatorIndex = line.indexOf('=');
|
|
if (separatorIndex != -1) {
|
|
if (!currentSection.compare("avoid"))
|
|
{
|
|
QString key = line.left(separatorIndex).trimmed();
|
|
QString value = line.mid(separatorIndex + 1).trimmed();
|
|
if (!value.compare("true"))
|
|
avoidMap.insert({ key, value });
|
|
}
|
|
else if (!currentSection.compare("customName"))
|
|
{
|
|
QString key = line.left(separatorIndex).trimmed();
|
|
QString value = line.mid(separatorIndex + 1).trimmed().toUtf8();
|
|
cuttomName.insert({ key, value });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
file.close();
|
|
}
|
|
else
|
|
{
|
|
file.close();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} |