249 lines
9.4 KiB
C++
249 lines
9.4 KiB
C++
#include "NuclideLib.h"
|
||
#include "ui_NuclideLib.h"
|
||
#include "sqlitemanager.h"
|
||
#include <QMessageBox>
|
||
#include <QHeaderView>
|
||
#include <QPainter>
|
||
#include <QStyleOptionButton>
|
||
#include <QMouseEvent>
|
||
#include <QApplication>
|
||
#include <QDebug>
|
||
#include <QDateTime>
|
||
#include "NuclideRayListDialog.h"
|
||
ButtonDelegate::ButtonDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
|
||
|
||
void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||
{
|
||
if (index.column() == 7) {
|
||
QStyleOptionButton button;
|
||
button.rect = option.rect;
|
||
button.text = QStringLiteral(u"核素发射射线信息");
|
||
button.state = QStyle::State_Enabled;
|
||
if (option.state & QStyle::State_MouseOver)
|
||
button.state |= QStyle::State_MouseOver;
|
||
QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
|
||
} else {
|
||
QStyledItemDelegate::paint(painter, option, index);
|
||
}
|
||
}
|
||
|
||
bool ButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
|
||
{
|
||
if (index.column() == 7 && event->type() == QEvent::MouseButtonRelease) {
|
||
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
|
||
QRect buttonRect = option.rect;
|
||
if (buttonRect.contains(mouseEvent->pos())) {
|
||
emit buttonClicked(index);
|
||
return true;
|
||
}
|
||
}
|
||
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
||
}
|
||
|
||
NuclideLibManage::NuclideLibManage(QWidget *parent) :
|
||
QWidget(parent),
|
||
ui(new Ui::NuclideLibManage)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
m_model = new QStandardItemModel(0, 8, this);
|
||
m_model->setHorizontalHeaderLabels({"ID", "序号", "核素名称", "半衰期", "半衰期不确定度", "母体核素名称", "子体核素名称", "操作"});
|
||
ui->tableView->setModel(m_model);
|
||
|
||
ui->tableView->setColumnHidden(0, true);
|
||
|
||
ui->tableView->setColumnWidth(1, 90); // 序号
|
||
ui->tableView->setColumnWidth(7, 280); // 操作列
|
||
ui->tableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
|
||
ui->tableView->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
|
||
ui->tableView->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch);
|
||
ui->tableView->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);
|
||
ui->tableView->horizontalHeader()->setSectionResizeMode(6, QHeaderView::Stretch);
|
||
|
||
ui->tableView->setAlternatingRowColors(true);
|
||
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
ui->tableView->setFocusPolicy(Qt::NoFocus);
|
||
ui->tableView->verticalHeader()->setVisible(false);
|
||
|
||
QString dbPath = QCoreApplication::applicationDirPath() + "/nuclideLib.db";
|
||
qDebug()<<dbPath;
|
||
bool dbOpened = SqliteManager::instance().openDatabase(dbPath);
|
||
if (!dbOpened) {
|
||
QMessageBox::critical(this, "数据库错误", "打开数据库失败:" + SqliteManager::instance().lastError());
|
||
return;
|
||
}
|
||
|
||
// 设置委托
|
||
m_delegate = new ButtonDelegate(this);
|
||
ui->tableView->setItemDelegateForColumn(7, m_delegate);
|
||
connect(m_delegate, &ButtonDelegate::buttonClicked, this, &NuclideLibManage::onButtonClicked);
|
||
loadNuclideData();
|
||
|
||
}
|
||
|
||
NuclideLibManage::~NuclideLibManage()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void NuclideLibManage::loadNuclideData()
|
||
{
|
||
QString sql = "SELECT ID, NUCLIDE_NAME, HALF_LIFE, HALF_LIFE_UNCERTAINTY, PARENT_NUCLIDE_NAME, CHILD_NUCLIDE_NAME FROM nuclideLib;";
|
||
auto rows = SqliteManager::instance().selectRows(sql);
|
||
|
||
m_listNuclide.clear();
|
||
m_model->setRowCount(0);
|
||
int row = 0;
|
||
for (const auto& rec : rows) {
|
||
QStringList item;
|
||
item << rec["ID"].toString()
|
||
<< rec["NUCLIDE_NAME"].toString()
|
||
<< rec["HALF_LIFE"].toString()
|
||
<< rec["HALF_LIFE_UNCERTAINTY"].toString()
|
||
<< rec["PARENT_NUCLIDE_NAME"].toString()
|
||
<< rec["CHILD_NUCLIDE_NAME"].toString();
|
||
m_listNuclide.append(item);
|
||
|
||
// 插入模型行(界面显示逻辑不变)
|
||
QList<QStandardItem*> rowItems;
|
||
// 第0列:ID(隐藏)
|
||
QStandardItem *idItem = new QStandardItem(item[0]);
|
||
idItem->setTextAlignment(Qt::AlignCenter);
|
||
idItem->setFlags(idItem->flags() & ~Qt::ItemIsEditable);
|
||
rowItems << idItem;
|
||
// 第1列:序号(行号+1)
|
||
QStandardItem *seqItem = new QStandardItem(QString::number(row + 1));
|
||
seqItem->setTextAlignment(Qt::AlignCenter);
|
||
seqItem->setFlags(seqItem->flags() & ~Qt::ItemIsEditable);
|
||
rowItems << seqItem;
|
||
// 第2~6列:核素名称、半衰期等
|
||
for (int col = 1; col <= 5; ++col) {
|
||
QStandardItem *dataItem = new QStandardItem(item[col]);
|
||
dataItem->setTextAlignment(Qt::AlignCenter);
|
||
dataItem->setFlags(dataItem->flags() & ~Qt::ItemIsEditable);
|
||
rowItems << dataItem;
|
||
}
|
||
// 第7列:操作列
|
||
QStandardItem *opItem = new QStandardItem("");
|
||
opItem->setFlags(opItem->flags() & ~Qt::ItemIsEditable);
|
||
rowItems << opItem;
|
||
|
||
m_model->appendRow(rowItems);
|
||
++row;
|
||
}
|
||
}
|
||
|
||
QStringList NuclideLibManage::getNuclideData(const QString &id)
|
||
{
|
||
for (const auto& item : m_listNuclide) {
|
||
if (item.at(0) == id)
|
||
return item;
|
||
}
|
||
return QStringList();
|
||
}
|
||
|
||
void NuclideLibManage::on_pushButton_add_clicked()
|
||
{
|
||
NuclideEditDialog dlg(false, {}, this);
|
||
if (dlg.exec() == QDialog::Accepted) {
|
||
QStringList data = dlg.getNuclideData();
|
||
if (data.size() < 5) return;
|
||
|
||
QMap<QString, QVariant> fieldValues;
|
||
fieldValues["NUCLIDE_NAME"] = data[0];
|
||
fieldValues["HALF_LIFE"] = data[1];
|
||
fieldValues["HALF_LIFE_UNCERTAINTY"] = data[2].toDouble(); // 显式转换为REAL类型
|
||
fieldValues["PARENT_NUCLIDE_NAME"] = data[3];
|
||
fieldValues["CHILD_NUCLIDE_NAME"] = data[4];
|
||
fieldValues["CREATE_TIME"] = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
|
||
|
||
qint64 newId = SqliteManager::instance().insertRow("nuclideLib", fieldValues);
|
||
if (newId > 0) {
|
||
QMessageBox::information(this, QStringLiteral(u"成功"), QStringLiteral(u"核素添加成功!"));
|
||
loadNuclideData();
|
||
} else {
|
||
QMessageBox::warning(this, QStringLiteral(u"失败"), QStringLiteral(u"添加失败:") + SqliteManager::instance().lastError());
|
||
}
|
||
}
|
||
}
|
||
|
||
void NuclideLibManage::on_pushButton_edit_clicked()
|
||
{
|
||
QModelIndexList selected = ui->tableView->selectionModel()->selectedRows();
|
||
if (selected.isEmpty()) {
|
||
QMessageBox::information(this, QStringLiteral(u"提示"), QStringLiteral(u"请先选择要修改的核素行!"));
|
||
return;
|
||
}
|
||
|
||
int row = selected.first().row();
|
||
QString id = m_model->index(row, 0).data().toString();
|
||
QStringList data = getNuclideData(id);
|
||
if (data.isEmpty()) return;
|
||
|
||
NuclideEditDialog dlg(true, data, this);
|
||
if (dlg.exec() == QDialog::Accepted) {
|
||
QStringList newData = dlg.getNuclideData();
|
||
if (newData.size() < 6) return;
|
||
|
||
QMap<QString, QVariant> fieldValues;
|
||
fieldValues["NUCLIDE_NAME"] = newData[1];
|
||
fieldValues["HALF_LIFE"] = newData[2];
|
||
fieldValues["HALF_LIFE_UNCERTAINTY"] = newData[3].toDouble(); // 显式转换为REAL类型
|
||
fieldValues["PARENT_NUCLIDE_NAME"] = newData[4];
|
||
fieldValues["CHILD_NUCLIDE_NAME"] = newData[5];
|
||
|
||
int affectedRows = SqliteManager::instance().updateRow(
|
||
"nuclideLib",
|
||
fieldValues,
|
||
"ID = ?",
|
||
{newData[0]}
|
||
);
|
||
|
||
if (affectedRows > 0) {
|
||
QMessageBox::information(this, QStringLiteral(u"成功"), QStringLiteral(u"核素修改成功!"));
|
||
loadNuclideData();
|
||
} else if (affectedRows == 0) {
|
||
QMessageBox::information(this, QStringLiteral(u"提示"), QStringLiteral(u"未修改任何数据!"));
|
||
} else {
|
||
QMessageBox::warning(this, QStringLiteral(u"失败"), QStringLiteral(u"修改失败:") + SqliteManager::instance().lastError());
|
||
}
|
||
}
|
||
}
|
||
|
||
void NuclideLibManage::on_pushButton_del_clicked()
|
||
{
|
||
QModelIndexList selected = ui->tableView->selectionModel()->selectedRows();
|
||
if (selected.isEmpty()) {
|
||
QMessageBox::information(this, QStringLiteral(u"提示"), QStringLiteral(u"请先选择要删除的核素行!"));
|
||
return;
|
||
}
|
||
|
||
int row = selected.first().row();
|
||
QString id = m_model->index(row, 0).data().toString();
|
||
|
||
QMessageBox box(QMessageBox::Question, QStringLiteral(u"提示"), QStringLiteral(u"确定删除该核素吗?"),
|
||
QMessageBox::Yes | QMessageBox::No, this);
|
||
box.button(QMessageBox::Yes)->setText(QStringLiteral(u"确认"));
|
||
box.button(QMessageBox::No)->setText(QStringLiteral(u"取消"));
|
||
if (box.exec() != QMessageBox::Yes) return;
|
||
|
||
bool ok = SqliteManager::instance().deleteRow("nuclideLib", "ID = ?", {id});
|
||
if (ok) {
|
||
loadNuclideData();
|
||
} else {
|
||
QMessageBox::warning(this, QStringLiteral(u"错误"), QStringLiteral(u"删除失败:") + SqliteManager::instance().lastError());
|
||
}
|
||
}
|
||
|
||
void NuclideLibManage::onButtonClicked(const QModelIndex &index)
|
||
{
|
||
int row = index.row();
|
||
QString nuclideId = m_model->index(row, 0).data().toString();
|
||
QString nuclideName = m_model->index(row, 2).data().toString();
|
||
|
||
// 打开射线信息管理对话框
|
||
NuclideRayListDialog dlg(nuclideId, nuclideName, this);
|
||
dlg.exec();
|
||
|
||
}
|