Files
VX5_KRO_GearPump/scene/ScheduleShiftsEditor.cpp
T
2026-05-23 14:16:57 +08:00

292 lines
8.9 KiB
C++

#include "ScheduleShiftsEditor.h"
#include "ui_ScheduleShiftsEditor.h"
#include <QMouseEvent>
#include <QTimer>
#include <QModelIndex>
#include "PicScheduleShiftItem.h"
#include <QGridLayout>
#include <QDebug>
#include <QTableWidget>
#include <algorithm>
#include <QScrollBar>
ScheduleShiftsEditor::ScheduleShiftsEditor(QWidget *parent) :
QWidget(parent),
ui(new Ui::ScheduleShiftsEditor)
{
ui->setupUi(this);
m_generalSettings = new ItemGeneralSettings();
QGridLayout* layout = new QGridLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setMargin(0);
layout->addWidget(m_generalSettings);
ui->generalSettingsTab->setLayout(layout);
// 禁用编辑
ui->tableSchedules->setEditTriggers(QTableWidget::NoEditTriggers);
ui->tableSchedules->setColumnWidth(0, 50);
ui->tableSchedules->setColumnWidth(1, 100);
ui->tableSchedules->setColumnWidth(2, 340);
ui->tableSchedules->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
ui->tableSchedules->horizontalHeader()->setVisible(true);
ui->tableSchedules->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableSchedules->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->tableSchedules->verticalScrollBar()->scroll(0, 0);
// ui->tableSchedules->setModel(m_viewModel);
connect(ui->tableSchedules, &QTableWidget::itemSelectionChanged, this, &ScheduleShiftsEditor::onTableSelectionChanged);
connect(ui->tableSchedules, &QTableWidget::currentItemChanged, this, [this](QTableWidgetItem*, QTableWidgetItem*){onTableSelectionChanged();});
connect(ui->tableSchedules, &QTableWidget::currentCellChanged, this, [this](int, int, int, int){onTableSelectionChanged();});
connect(ui->LineEditScheduleName, &QLineEdit::textChanged, this, &ScheduleShiftsEditor::btnAddEnbCheck);
connect(ui->timeEdit, &QTimeEdit::timeChanged, this, [this](const QTime&) {btnAddEnbCheck("");});
ui->btnAdd->setEnabled(false);
ui->btnRemove->setEnabled(false);
applyStyleSheet();
}
ScheduleShiftsEditor::~ScheduleShiftsEditor()
{
delete ui;
}
void ScheduleShiftsEditor::setItemModel(QVector<ScheduleModel> schedulesModel, TextItemModel txtModel)
{
m_generalSettings->setViewModel(txtModel);
m_dataModel = schedulesModel;
applyDataModel();
}
TextItemModel ScheduleShiftsEditor::textItemModel()
{
return m_generalSettings->viewModel();
}
const QVector<ScheduleModel>& ScheduleShiftsEditor::scheduleModel() const
{
return m_dataModel;
}
PicScheduleShiftItem *ScheduleShiftsEditor::currEditItem()
{
return dynamic_cast<PicScheduleShiftItem*>(m_generalSettings->currentItem());
}
void ScheduleShiftsEditor::onBtnCloseClicked()
{
emit rejected();
}
void ScheduleShiftsEditor::onBtnAddClicked()
{
if(m_dataModel.size() >= 24) {
qWarning() << "The schedule shift has already reached the maximum number of 24 schedules.";
return;
}
auto t = ui->timeEdit->time();
auto b = std::binary_search(m_dataModel.begin(), m_dataModel.end(), ScheduleModel{t, QString()});
if(b) {
qWarning() << "The timestamp has already exist. " << t.toString("hh:mm");
return;
}
if(ui->LineEditScheduleName->text().isEmpty()) {
qWarning() << "The schedule name is empty.";
return;
}
ScheduleModel model;
model.startTime = t;
model.name = ui->LineEditScheduleName->text();
auto it = std::upper_bound(m_dataModel.begin(), m_dataModel.end(), model);
m_dataModel.insert(it, model);
qDebug() << "Schedule time: " << model.startTime.toString("hh:mm") << ", name: " << model.name << ", has added.";
applyDataModel();
ui->LineEditScheduleName->clear();
ui->btnRemove->setEnabled(false);
}
void ScheduleShiftsEditor::onBtnRemoveClicked()
{
if(ui->tableSchedules->currentRow() >= 0 && ui->tableSchedules->currentRow() < m_dataModel.size()) {
m_dataModel.remove(ui->tableSchedules->currentRow());
applyDataModel();
}
}
void ScheduleShiftsEditor::onTableSelectionChanged()
{
ui->btnRemove->setEnabled(ui->tableSchedules->currentRow() != -1);
}
void ScheduleShiftsEditor::btnAddEnbCheck(const QString &txt)
{
Q_UNUSED(txt);
ScheduleModel model {ui->timeEdit->time(), ""};
ui->btnAdd->setEnabled(!std::binary_search(m_dataModel.begin(), m_dataModel.end(), model) && !ui->LineEditScheduleName->text().isEmpty());
}
void ScheduleShiftsEditor::onBtnOkClicked()
{
emit accepted();
}
void ScheduleShiftsEditor::applyDataModel()
{
auto b = ui->tableSchedules->blockSignals(true);
ui->tableSchedules->clearContents();
ui->tableSchedules->setRowCount(m_dataModel.size());
for(int i = 0; i < m_dataModel.size(); ++i) {
QString time = m_dataModel[i].startTime.toString("hh:mm");
ui->tableSchedules->setItem(i, 0, new QTableWidgetItem(QString::number(i + 1)));
ui->tableSchedules->setItem(i, 1, new QTableWidgetItem(time));
ui->tableSchedules->setItem(i, 2, new QTableWidgetItem(m_dataModel[i].name));
}
ui->tableSchedules->blockSignals(b);
auto txt = QString::number(24 - m_dataModel.size());
ui->LabNbRemains->setText(QString::number(24 - m_dataModel.size()));
}
void ScheduleShiftsEditor::applyStyleSheet()
{
QString styleSheet =
R"(
/*
QPushButton {
background-color: #5396C1;
color: #ffffff;
border: 1px solid #000000;
border-radius: 3px;
font-size: 18px;
font-weight:500;
width: 180px;
height: 90px;
}
QPushButton:hover {
background-color: #5396C1;
}
QPushButton:disabled {
background-color: #7C828E;
color: #606060;
border-color: #808080;
}
*/
QPushButton#closeBtn, QPushButton#checkBtn{
background-color: #E6F7FF;
border: 1px solid #87CEEB;
border-radius: 4px;
font-size: 24px;
color: #228B22;
}
QPushButton#closeBtn:hover,QPushButton#checkBtn:hover{
background-color: #D1E9FF;
}
QPushButton#closeBtn:pressed,QPushButton#checkBtn:pressed{
background-color: #BAE7FF;
}
QPushButton {
background-color: #5396C1;
color: #ffffff;
border: 1px solid #000000;
border-radius: 3px;
font-size: 18px;
font-weight:500;
width: 100px;
height: 50px;
}
QPushButton:hover {
background-color: #5396C1;
}
QPushButton:disabled {
background-color: #7C828E;
color: #606060;
border-color: #808080;
}
QTabWidget::pane {
border: 1px solid #dddddd;
background-color: #ffffff;
}
QTabBar::tab:focus {
outline: none;
}
QTabBar::tab {
min-width: 90px;
min-height: 45px;
padding: 4px 10px;
margin-right: 4px;
border: 1px solid #dddddd;
border-bottom: none;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
background-color: #5396C1;
color: #ffffff;
}
QTabWidget::tab-bar {
top: -3px;
left: 10px;
}
QTabBar::tab:selected {
background-color: #6bb1e0;
color: #ffffff;
box-shadow: 0 -3px 8px rgba(44, 125, 232, 0.25);
}
)";
// QFile stylesFile(":/styles.qss");
// QTextStream ts(&stylesFile);
// auto str = ts.readAll();
// setStyleSheet(str);
// ui->tabWidget->setStyleSheet(str);
// // this->setStyleSheet(styleSheet);
}
SelectedStyleTimeEdit::SelectedStyleTimeEdit(QWidget *parent)
:QTimeEdit(parent)
{
lineEdit()->installEventFilter(this);
}
SelectedStyleTimeEdit::~SelectedStyleTimeEdit()
{
lineEdit()->removeEventFilter(this);
}
bool SelectedStyleTimeEdit::eventFilter(QObject *obj, QEvent *event)
{
if (obj == lineEdit() && event->type() == QEvent::MouseButtonPress) {
// 先让父类处理(这会让内部状态更新)
bool result = QTimeEdit::eventFilter(obj, event);
// 延迟执行选中,确保currentSection已经更新
QTimer::singleShot(0, this, [this]() {
QDateTimeEdit::Section section = currentSection();
setSelectedSection(section);
});
return result;
}
return QTimeEdit::eventFilter(obj, event);
}