logplus/DataOutput/src/NameMapDlg.cpp
2025-10-29 17:23:30 +08:00

265 lines
7.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "NameMapDlg.h"
#include "ui_NameMapDlg.h"
#include "qdir.h"
#include "qstring.h"
#include "qfiledialog.h"
#include <qtextstream.h>
#include "qinputdialog.h"
#include <qpushbutton.h>
// #include "ObjGraphicsHeadCell.h"
#include <QHeaderView>
#include "geometryutils.h"
NameMapDlg::NameMapDlg(QDialog *parent) :
QDialog(parent),
NM_UI(new Ui::NameMapDlg)
{
NM_UI->setupUi(this);
setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
setFixedSize(this->width(), this->height());
this->setWindowFlags(this->windowFlags() |Qt::Dialog);
this->setWindowModality(Qt::ApplicationModal);
NM_UI->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
QIcon icon;
icon.addFile(GetImagePath() + "UIMake/addRow.png");
NM_UI->pushButton_addRow->setIcon(icon);
icon.addFile(GetImagePath() + "UIMake/delRow.png");
NM_UI->pushButton_delRow->setIcon(icon);
QIcon icon_save;
icon_save.addFile(GetImagePath() + "UIMake/save.png");
NM_UI->pushButton_save->setIcon(icon_save);
QIcon icon_load, icon_download;
icon_load.addFile(GetImagePath() + "UIMake/load.png");
NM_UI->pushButton_Load->setIcon(icon_load);
//icon_download.addFile(GetImagePath() + "UIMake/download.png");
//NM_UI->pushButton_downLoad->setIcon(icon_download);
connect(NM_UI->comboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(slotComboxChanged()));
connect(NM_UI->pushButton, SIGNAL(clicked()), this, SLOT(slotApply()));
connect(NM_UI->pushButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(NM_UI->pushButton_2, SIGNAL(clicked()), this, SLOT(reject()));
connect(NM_UI->pushButton_3, SIGNAL(clicked()), this, SLOT(slotComboxChanged()));
connect(NM_UI->pushButton_addRow, SIGNAL(clicked()), this, SLOT(slotAddRow()));
connect(NM_UI->pushButton_delRow, SIGNAL(clicked()), this, SLOT(slotDelRow()));
connect(NM_UI->pushButton_save, SIGNAL(clicked()), this, SLOT(slotSave()));
connect(NM_UI->pushButton_Load, SIGNAL(clicked()), this, SLOT(slotLoad()));
//connect(NM_UI->pushButton_downLoad, SIGNAL(clicked()), this, SLOT(slotDownLoad()));
freshTableList();
freshTable();
}
NameMapDlg::~NameMapDlg()
{
delete NM_UI;
}
void NameMapDlg::freshTableList()
{
NM_UI->comboBox->clear();
tableList.clear();
QString mapFolder_Path = GetConfPath() + "output_nameMap";
QDir dir(mapFolder_Path);
if(!dir.exists()) return;
QStringList filters;
filters<<QString("*.ini");
dir.setFilter(QDir::Files | QDir::NoSymLinks); //设置类型过滤器,只为文件格式
dir.setNameFilters(filters); //设置文件名称过滤器只为filters格式
int dir_count = dir.count();
if(dir_count <= 0) return;
for(int i = 0; i < dir_count; i++)
{
QString file_name = dir[i].split(".ini")[0];
tableList.append(file_name);
NM_UI->comboBox->addItem(file_name, i);
}
//NM_UI->comboBox->addItems(tableList);
}
void NameMapDlg::freshTable()
{
QString tablePath = GetConfPath() + "output_nameMap\\" + NM_UI->comboBox->currentText() + ".ini";
QFile file;
file.setFileName(tablePath);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
curMap.clear();
while(!file.atEnd())
{
QString temp = QString(file.readLine());
QStringList tempList = temp.split(" ");
if(tempList.size() == 2) curMap.insert(tempList[0], tempList[1]);
}
NM_UI->tableWidget->clear();
NM_UI->tableWidget->setRowCount(curMap.size());
NM_UI->tableWidget->setColumnCount(2);
QStringList headers;
headers<<"原始名称"<<QStringLiteral("新名称");
NM_UI->tableWidget->setHorizontalHeaderLabels(headers);
QMap<QString, QString>::iterator iter = curMap.begin();
int curR = 0;
while(iter != curMap.end())
{
QTableWidgetItem *item1 = new QTableWidgetItem(iter.key());
QTableWidgetItem *item2 = new QTableWidgetItem(iter.value());
NM_UI->tableWidget->setItem(curR, 0, item1);
NM_UI->tableWidget->setItem(curR++, 1, item2);
iter++;
}
}
void NameMapDlg::freshTable(QString filePath)
{
QString tablePath = filePath;
QFile file;
file.setFileName(tablePath);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
curMap.clear();
while(!file.atEnd())
{
QString temp = QString(file.readLine());
QStringList tempList = temp.split(" ");
if(tempList.size() == 2) curMap.insert(tempList[0], tempList[1]);
}
NM_UI->tableWidget->clear();
NM_UI->tableWidget->setRowCount(curMap.size());
NM_UI->tableWidget->setColumnCount(2);
QStringList headers;
headers<<"原始名称"<<"新名称";
NM_UI->tableWidget->setHorizontalHeaderLabels(headers);
QMap<QString, QString>::iterator iter = curMap.begin();
int curR = 0;
while(iter != curMap.end())
{
QTableWidgetItem *item1 = new QTableWidgetItem(iter.key());
QTableWidgetItem *item2 = new QTableWidgetItem(iter.value());
NM_UI->tableWidget->setItem(curR, 0, item1);
NM_UI->tableWidget->setItem(curR++, 1, item2);
iter++;
}
}
void NameMapDlg::slotComboxChanged()
{
int index = NM_UI->comboBox->currentIndex();
freshTable();
}
void NameMapDlg::slotApply()
{
int rowNum = NM_UI->tableWidget->rowCount();
int colNum = NM_UI->tableWidget->columnCount();
curMap.clear();
for(int r = 0; r < rowNum; r++)
{
QString keyV, valueV;
for(int c = 0; c < colNum; c++)
{
if(NM_UI->tableWidget->item(r, c) == NULL) continue;
if(c == 0) keyV = NM_UI->tableWidget->item(r, c)->text().toUpper();
else valueV = NM_UI->tableWidget->item(r, c)->text().toUpper();
}
curMap.insert(keyV, valueV);
}
//emit signal_nameMap(curMap);
}
void NameMapDlg::slotAddRow()
{
int curRowNum = NM_UI->tableWidget->rowCount();
NM_UI->tableWidget->setRowCount(curRowNum + 1);
QTableWidgetItem *item1 = new QTableWidgetItem("oldName");
QTableWidgetItem *item2 = new QTableWidgetItem("newName");
NM_UI->tableWidget->setItem(curRowNum, 0, item1);
NM_UI->tableWidget->setItem(curRowNum, 1, item2);
}
void NameMapDlg::slotDelRow()
{
NM_UI->tableWidget->removeRow(NM_UI->tableWidget->currentRow());
}
void NameMapDlg::slotSave()
{
bool isOK;
QString fileName = QInputDialog::getText(NULL, "save file name",
"Please input the new name",
QLineEdit::Normal,
"表格名称",
&isOK);
if(isOK && fileName != "")
{
QString roadID_Path = GetConfPath() + "output_nameMap\\" + fileName + ".ini";
QFile file(roadID_Path);
if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
QString allText;
file.seek(0);
int rowNum = NM_UI->tableWidget->rowCount();
int colNum = NM_UI->tableWidget->columnCount();
for(int r = 0; r < rowNum; r++)
{
QString keyV, valueV;
for(int c = 0; c < colNum; c++)
{
if(NM_UI->tableWidget->item(r, c) == NULL) continue;
if(c == 0) keyV = NM_UI->tableWidget->item(r, c)->text().toUpper();
else valueV = NM_UI->tableWidget->item(r, c)->text().toUpper();
}
allText += keyV + " " + valueV + "\n";
}
QTextStream outText(&file);
outText << allText;
}file.close();
}
freshTableList();
}
void NameMapDlg::slotLoad()
{
QFileDialog *fileDlg = new QFileDialog(this);
fileDlg->setWindowTitle("选择文件");
fileDlg->setDirectory("C:\\Users\\Administrator\\Desktop");
fileDlg->setNameFilter(tr("File(*.ini*)"));
fileDlg->setViewMode(QFileDialog::Detail);
QStringList fileNames;
if(!fileDlg->exec()) return;
fileNames = fileDlg->selectedFiles();
freshTable(fileNames[0]);
}
void NameMapDlg::slotDownLoad()
{
QString file_path = QFileDialog::getSaveFileName(NULL,
"save file",
"C:\\Users\\Administrator\\Desktop" ,
"ini files (*.ini);;all files(*.*)");
}
QMap<QString, QString> NameMapDlg::getNameMap()
{
return curMap;
}