|
|
#include "../headfiles/robotlistui.h"
|
|
|
#include "ui_robotlistui.h"
|
|
|
|
|
|
constexpr int MAX_DISPLAY_NUM = 10;
|
|
|
constexpr int COLUMN_NUM = 7;
|
|
|
|
|
|
/**
|
|
|
* @brief 自定义委托类用于设置状态列的颜色变换
|
|
|
* @date 2024年6月10日
|
|
|
* @author yyz
|
|
|
*/
|
|
|
class StateDelegate : public QStyledItemDelegate
|
|
|
{
|
|
|
public:
|
|
|
StateDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
|
|
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
|
|
|
{
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
if (index.column() == 1) { // 假设状态列是第1列
|
|
|
if (index.data().toString() == "已连接") {
|
|
|
opt.palette.setColor(QPalette::Text, QColor("#00FF00")); // 设置文本颜色为绿色
|
|
|
} else {
|
|
|
opt.palette.setColor(QPalette::Text, QColor("#808080")); // 设置文本颜色为灰色
|
|
|
}
|
|
|
}
|
|
|
|
|
|
QStyledItemDelegate::paint(painter, opt, index);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
robotlistUI::robotlistUI(QWidget *parent)
|
|
|
: QMainWindow(parent)
|
|
|
, ui(new Ui::robotlistUI)
|
|
|
, db(new DogDatabase)
|
|
|
{
|
|
|
ui->setupUi(this);
|
|
|
setupModel();
|
|
|
setupTableView();
|
|
|
}
|
|
|
|
|
|
robotlistUI::~robotlistUI()
|
|
|
{
|
|
|
delete ui;
|
|
|
delete db;
|
|
|
}
|
|
|
void robotlistUI::setupModel()
|
|
|
{
|
|
|
QList<Dog> dogs;
|
|
|
// 设置单元格的字体颜色
|
|
|
ui->robotList->setStyleSheet("QTableView { color: rgb(15,18,34); }");
|
|
|
|
|
|
if (db->select_all(dogs)) {
|
|
|
model = new QStandardItemModel(MAX_DISPLAY_NUM, COLUMN_NUM, this); // 7列,包括6个数据列和1个按钮列
|
|
|
model->setHorizontalHeaderLabels({"ID", "State", "IP", "Port", "Lon", "Lat", ""}); // 设置表头
|
|
|
QStandardItem *item;
|
|
|
for (int i = 0; i < MAX_DISPLAY_NUM; ++i) {
|
|
|
// 设置id这一列的格式
|
|
|
const Dog &dog = dogs[i];
|
|
|
item = new QStandardItem(dog.id);
|
|
|
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 禁止编辑
|
|
|
item->setForeground(QColor(22,25,40));
|
|
|
item->setTextAlignment(Qt::AlignCenter); // 设置字体居中
|
|
|
model->setItem(i, 0, item);
|
|
|
// 设置state这一列的格式
|
|
|
item = new QStandardItem(dog.state == 1 ? QString("已连接") : QString("未连接"));
|
|
|
item->setForeground(dog.state == 1 ? Qt::green : Qt::gray); // 设置文本颜色
|
|
|
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 禁止编辑
|
|
|
item->setTextAlignment(Qt::AlignCenter); // 设置字体居中
|
|
|
model->setItem(i, 1, item);
|
|
|
// 设置ip这一列的数据格式
|
|
|
item = new QStandardItem(dog.ip);
|
|
|
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 禁止编辑
|
|
|
item->setForeground(QColor(22,25,40));
|
|
|
item->setTextAlignment(Qt::AlignCenter); // 设置字体居中
|
|
|
model->setItem(i, 2, item);
|
|
|
// 设置port这一列的数据格式
|
|
|
item = new QStandardItem(QString::number(dog.port));
|
|
|
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 禁止编辑
|
|
|
item->setForeground(QColor(22,25,40));
|
|
|
item->setTextAlignment(Qt::AlignCenter); // 设置字体居中
|
|
|
model->setItem(i, 3, item);
|
|
|
// 设置lon这一列的数据格式
|
|
|
item = new QStandardItem(QString::number(dog.lon));
|
|
|
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 禁止编辑
|
|
|
item->setForeground(QColor(22,25,40));
|
|
|
item->setTextAlignment(Qt::AlignCenter); // 设置字体居中
|
|
|
model->setItem(i, 4, item);
|
|
|
// 设置lat这一列的数据格式
|
|
|
item = new QStandardItem(QString::number(dog.lat));
|
|
|
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 禁止编辑
|
|
|
item->setForeground(QColor(22,25,40));
|
|
|
item->setTextAlignment(Qt::AlignCenter); // 设置字体居中
|
|
|
model->setItem(i, 5, item);
|
|
|
}
|
|
|
} else {
|
|
|
// 处理错误情况
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void robotlistUI::setupTableView()
|
|
|
{
|
|
|
ui->robotList->setModel(model);
|
|
|
|
|
|
// 设置表头字体和颜色
|
|
|
QFont headerFont = ui->robotList->font();
|
|
|
headerFont.setBold(true);
|
|
|
|
|
|
// 表头的字体和字体颜色设置
|
|
|
ui->robotList->horizontalHeader()->setFont(headerFont);
|
|
|
ui->robotList->horizontalHeader()->setStyleSheet("color: rgb(83,88,117); background-color: transparent; ");
|
|
|
|
|
|
// 行号的字体和自已颜色设置
|
|
|
ui->robotList->verticalHeader()->setFont(headerFont);
|
|
|
ui->robotList->verticalHeader()->setStyleSheet("color: rgb(83,88,117); background-color: transparent");
|
|
|
|
|
|
|
|
|
|
|
|
// 设置行高
|
|
|
ui->robotList->verticalHeader()->setDefaultSectionSize(25);
|
|
|
|
|
|
// // 设置交替行颜色
|
|
|
// ui->robotList->setAlternatingRowColors(true);
|
|
|
// ui->robotList->setStyleSheet("QTableView::alternate-row { background-color: #E8E8E8; }");
|
|
|
|
|
|
// 启用列宽调整和列排序
|
|
|
ui->robotList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
|
|
ui->robotList->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
|
|
ui->robotList->setSortingEnabled(true);
|
|
|
|
|
|
|
|
|
// 为每一行添加按钮
|
|
|
for (int row = 0; row < model->rowCount(); ++row) {
|
|
|
QString robotId = model->data(model->index(row, 0)).toString();
|
|
|
QPushButton *button = createButton("启动", robotId);
|
|
|
ui->robotList->setIndexWidget(model->index(row, 6), button);
|
|
|
connect(button, &QPushButton::clicked, this, [=]{ handleButtonClicked(robotId); });
|
|
|
}
|
|
|
|
|
|
// 设置选择模式为单选
|
|
|
ui->robotList->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
// 设置代理
|
|
|
ui->robotList->setItemDelegateForColumn(1, new StateDelegate(this)); // 为状态列设置代理
|
|
|
}
|
|
|
|
|
|
QPushButton* robotlistUI::createButton(const QString &text, const QString &robotId)
|
|
|
{
|
|
|
QPushButton *button = new QPushButton(text);
|
|
|
button->setProperty("robotId", robotId);
|
|
|
return button;
|
|
|
}
|
|
|
|
|
|
void robotlistUI::handleButtonClicked(const QString &robotId)
|
|
|
{
|
|
|
// 这里实现打开对应机器人操作界面的逻辑
|
|
|
qDebug() << "Open robot with ID:" << robotId;
|
|
|
|
|
|
// 创建新的机器狗界面对象
|
|
|
RobotUI *rbtui = new RobotUI(this);
|
|
|
rbtui->show();
|
|
|
}
|
|
|
|