182 lines
6.5 KiB
C++
182 lines
6.5 KiB
C++
|
|
#include "NuclideLib.h"
|
|||
|
|
#include "ui_NuclideLib.h"
|
|||
|
|
#include "sqlitemanager.h"
|
|||
|
|
#include <QMessageBox>
|
|||
|
|
#include <QHeaderView>
|
|||
|
|
#include <QPainter>
|
|||
|
|
#include <QStyleOptionButton>
|
|||
|
|
#include <QMouseEvent>
|
|||
|
|
#include <QApplication>
|
|||
|
|
|
|||
|
|
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 = "核素发射射线信息";
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
// 设置委托
|
|||
|
|
m_delegate = new ButtonDelegate(this);
|
|||
|
|
ui->tableView->setItemDelegateForColumn(7, m_delegate);
|
|||
|
|
connect(m_delegate, &ButtonDelegate::buttonClicked, this, &NuclideLibManage::onButtonClicked);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
NuclideLibManage::~NuclideLibManage()
|
|||
|
|
{
|
|||
|
|
delete ui;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void NuclideLibManage::loadNuclideData()
|
|||
|
|
{
|
|||
|
|
QString sql = "SELECT ID, NAME, HALF_LIFE, HALF_LIFE_UNC, PARENT_NUCLIDE, CHILD_NUCLIDE FROM tbl_nuclide_library;";
|
|||
|
|
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["NAME"].toString()
|
|||
|
|
<< rec["HALF_LIFE"].toString()
|
|||
|
|
<< rec["HALF_LIFE_UNC"].toString()
|
|||
|
|
<< rec["PARENT_NUCLIDE"].toString()
|
|||
|
|
<< rec["CHILD_NUCLIDE"].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) { // item的下标1~5对应NAME到CHILD_NUCLIDE
|
|||
|
|
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()
|
|||
|
|
{
|
|||
|
|
loadNuclideData(); // 刷新数据
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void NuclideLibManage::on_pushButton_edit_clicked()
|
|||
|
|
{
|
|||
|
|
QModelIndexList selected = ui->tableView->selectionModel()->selectedRows();
|
|||
|
|
if (selected.isEmpty()) {
|
|||
|
|
QMessageBox::information(this, "提示", "请先选择要修改的核素行!");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
int row = selected.first().row();
|
|||
|
|
QString id = m_model->index(row, 0).data().toString(); // 隐藏的ID列
|
|||
|
|
QStringList data = getNuclideData(id);
|
|||
|
|
if (data.isEmpty()) return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void NuclideLibManage::on_pushButton_del_clicked()
|
|||
|
|
{
|
|||
|
|
QModelIndexList selected = ui->tableView->selectionModel()->selectedRows();
|
|||
|
|
if (selected.isEmpty()) {
|
|||
|
|
QMessageBox::information(this, "提示", "请先选择要删除的核素行!");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
int row = selected.first().row();
|
|||
|
|
QString id = m_model->index(row, 0).data().toString();
|
|||
|
|
|
|||
|
|
QMessageBox box(QMessageBox::Question, "提示", "确定删除该核素吗?",
|
|||
|
|
QMessageBox::Yes | QMessageBox::No, this);
|
|||
|
|
box.button(QMessageBox::Yes)->setText("确认");
|
|||
|
|
box.button(QMessageBox::No)->setText("取消");
|
|||
|
|
if (box.exec() != QMessageBox::Yes) return;
|
|||
|
|
|
|||
|
|
bool ok = SqliteManager::instance().deleteRow("tbl_nuclide_library", "ID = ?", {id});
|
|||
|
|
if (ok) {
|
|||
|
|
loadNuclideData();
|
|||
|
|
} else {
|
|||
|
|
QMessageBox::warning(this, "错误", "删除失败:" + SqliteManager::instance().lastError());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void NuclideLibManage::onButtonClicked(const QModelIndex &index)
|
|||
|
|
{
|
|||
|
|
int row = index.row();
|
|||
|
|
QString nuclideId = m_model->index(row, 0).data().toString();
|
|||
|
|
}
|