155 lines
4.4 KiB
C++
155 lines
4.4 KiB
C++
#include "formtitle.h"
|
||
#include "ui_formtitle.h"
|
||
#include <QMessageBox>
|
||
#include <QMenu>
|
||
#include <QDebug>
|
||
#include "CallManage.h"
|
||
|
||
//demo,暂时不用
|
||
FormTitle::FormTitle(QWidget *parent, int indexID) :
|
||
QWidget(parent),
|
||
ui(new Ui::FormTitle)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
m_indexID =indexID;
|
||
|
||
//使能右键菜单功能
|
||
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
|
||
// 连接信号和槽
|
||
connect(ui->tableWidget, &QTableWidget::itemClicked, this, &FormTitle::onItemClicked);
|
||
|
||
//关联信号槽
|
||
connect(CallManage::getInstance(), SIGNAL(sig_DelCurve(int, QString)), this, SLOT(s_DelCurve(int, QString)));
|
||
}
|
||
|
||
FormTitle::~FormTitle()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
|
||
void FormTitle::Init()
|
||
{
|
||
//清空
|
||
ui->tableWidget->clearContents();
|
||
ui->tableWidget->verticalHeader()->hide();//隐藏左侧系统序号栏
|
||
ui->tableWidget->horizontalHeader()->hide();//隐藏上方系统序号栏
|
||
|
||
//因为tableWidget需要提前规定好行数与列数
|
||
int recordcount = 0; //总行数
|
||
ui->tableWidget->setColumnCount(1);
|
||
ui->tableWidget->setRowCount(recordcount); //动态设置行数
|
||
|
||
//设置所有列均匀分布并填充满整个空间
|
||
QHeaderView *header = ui->tableWidget->horizontalHeader();
|
||
for (int i = 0; i < ui->tableWidget->columnCount(); ++i) {
|
||
header->setSectionResizeMode(i, QHeaderView::Stretch);
|
||
}
|
||
}
|
||
|
||
void FormTitle::Add(QString strLineName, QColor lineColor)
|
||
{
|
||
int row = ui->tableWidget->rowCount();
|
||
ui->tableWidget->setRowCount(row + 1);
|
||
|
||
|
||
QFont font("微软雅黑", 10, QFont::Bold, false);
|
||
|
||
//
|
||
QTableWidgetItem* item = new QTableWidgetItem(strLineName);
|
||
item->setFlags(item->flags() & (~Qt::ItemIsEditable));
|
||
item->setForeground(QBrush(lineColor));// 设置字体颜色
|
||
item->setFont(font); // 应用新的字体
|
||
item->setTextAlignment(Qt::AlignCenter);//居中
|
||
//
|
||
ui->tableWidget->setItem(row, 0, item);
|
||
}
|
||
|
||
// 槽函数
|
||
void FormTitle::onItemClicked(QTableWidgetItem *item)
|
||
{
|
||
if (item) {
|
||
// 获取 item 的位置信息
|
||
int row = item->row();
|
||
int column = item->column();
|
||
// 处理 item 点击的逻辑
|
||
//qDebug() << "Item at row" << row << "column" << column << "clicked.";
|
||
|
||
emit sig_LineClicked(row);
|
||
|
||
QString message = "选中项: " + item->text();
|
||
QMessageBox::information(this, "提示", message);
|
||
}
|
||
}
|
||
|
||
void FormTitle::s_DelCurve(int indexID, QString strLineName)
|
||
{
|
||
if(m_indexID==indexID)//只处理自己的事件
|
||
{
|
||
for (int row = 0; row < ui->tableWidget->rowCount(); ++row)
|
||
{
|
||
QTableWidgetItem *item = ui->tableWidget->item(row, 0); // 假设你想检查第一列
|
||
if (item && item->text() == strLineName)
|
||
{
|
||
ui->tableWidget->removeRow(row);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void FormTitle::on_tableWidget_customContextMenuRequested(const QPoint &pos)
|
||
{
|
||
QTableWidgetItem *item = ui->tableWidget->itemAt(pos);
|
||
if(item)
|
||
{
|
||
int row = item->row();
|
||
emit sig_LineClicked(row);
|
||
|
||
QMenu *menu = new QMenu(ui->tableWidget);
|
||
QAction *actionDel = menu->addAction(tr("删除曲线"));
|
||
QAction *actionMove = menu->addAction(tr("移动曲线"));
|
||
connect(actionDel, SIGNAL(triggered()), this, SLOT(s_DelLine()));
|
||
connect(actionMove, SIGNAL(triggered()), this, SLOT(s_MoveLine()));
|
||
menu->exec(QCursor::pos());
|
||
}
|
||
else {
|
||
// QString message = "没有选中项,请选择要删除的曲线";
|
||
// QMessageBox::information(this, "提示", message);
|
||
}
|
||
}
|
||
|
||
void FormTitle::s_DelLine()
|
||
{
|
||
qDebug() << "s_DelLine at row";
|
||
|
||
QString strQuestion = "是否删除曲线?";
|
||
QMessageBox::StandardButton reply;
|
||
reply = QMessageBox::question(this, "提示", strQuestion,
|
||
QMessageBox::Yes | QMessageBox::No);
|
||
if (reply != QMessageBox::Yes) {
|
||
return; // 取消
|
||
}
|
||
|
||
emit removeSelectedGraphByTitle();
|
||
}
|
||
|
||
void FormTitle::s_MoveLine()
|
||
{
|
||
qDebug() << "s_DelLine at row";
|
||
|
||
QString strQuestion = "是否移动曲线到【道1】?";
|
||
QMessageBox::StandardButton reply;
|
||
reply = QMessageBox::question(this, "提示", strQuestion,
|
||
QMessageBox::Yes | QMessageBox::No);
|
||
if (reply != QMessageBox::Yes) {
|
||
return; // 取消
|
||
}
|
||
|
||
emit removeSelectedGraphByTitle();
|
||
|
||
emit CallManage::getInstance()->sig_AddLine(1);
|
||
}
|