Newer
Older
CasicBioRecNew / PersonListForm.cpp
Tan Yue on 2 Jun 2022 7 KB 20220602 人脸识别优化整合
#include "PersonListForm.h"
#include "ui_PersonListForm.h"

PersonListForm::PersonListForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PersonListForm)
{
    ui->setupUi(this);

    // 加载css文件设置控件样式
    QFile file(QApplication::applicationDirPath() + "/qss/personList.css");
    if (file.open(QFile::ReadOnly))
    {
        QString qssStr = QLatin1String(file.readAll());
        this->setStyleSheet(qssStr);
        file.close();
    }

    // 设置弹簧的尺寸
    ui->vsContentTop->changeSize(0, 20);
    ui->vsTableTop->changeSize(0, 20);
    ui->vsContentBottom->changeSize(0, 40);

    // 初始化空的人员列表表格
    initPersonTableHeader();

    // 初始化部门下拉列表框
    SelectDeptUtil::getInstance().initSelectDept(ui->selectDept, CacheManager::getInstance().getDeptCachePtr());
}

PersonListForm::~PersonListForm()
{
    delete ui;
}

void PersonListForm::keyPressEvent(QKeyEvent * event)
{
    switch (event->key()) {
    case Qt::Key_Return: // 小键盘的Enter键
    case Qt::Key_Enter: // Enter键
        currentPage = 0;
        this->findPersonList();
        break;

    default:
        QWidget::keyPressEvent(event);
    }
}

void PersonListForm::calcTotalPage(int totalCount)
{
    double temp = (totalCount * 1.0) / (tableRowCount * 1.0);
    totalPage = ceil(temp);
}

void PersonListForm::findPersonList()
{
    SysPersonDao personDao;

    // 获取查询条件
    QString name = ui->inputName->text();
    QString dept = ui->selectDept->currentData().toString();

    // 计算分页查询的偏移量
    qint16 offset = currentPage * tableRowCount;

    // 查询总条数并计算总页数
    int totalCount = personDao.findRecordCountByNameAndDept(name, dept);
    calcTotalPage(totalCount);

    // 赋值表格内容
    QVector<QVariantMap> perList = personDao.findRecordsByNameAndDept(name, dept, tableRowCount, offset);

    // 有数据且查询结果为空 则重新查询上一页
    if (totalCount > 0 && perList.isEmpty())
    {
        // 触发查询上一页的操作 后续操作不再执行
        emit ui->btnPre->clicked();
        return ;
    }

    this->personList->clear();
    for (int i = 0; i < perList.size(); i++) {
        QVariantMap person = perList.at(i);
        this->personList->append(person);
    }

    // 更新显示表格
    this->updatePersonListTable();

    // 更新页码
    if (totalCount == 0)
    {
        ui->labCurrPage->setText("0");
    } else
    {
        ui->labCurrPage->setText(QString("%1 / %2 , %3").arg(currentPage + 1).arg(totalPage).arg(totalCount));
    }
}

void PersonListForm::initPersonTableHeader()
{
    itemModel = new QStandardItemModel(this);
    personList = new QList<QVariantMap>();

    // 添加表头
    QStringList header;
    header << "员工编号" << "姓名" << "部门 "<< "采集特征" << "操作";
    itemModel->setHorizontalHeaderLabels(header);

    // 设置表头的宽度为固定值 不可调整
    ui->personListTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
}

void PersonListForm::updatePersonListTable()
{
    // 清除表格原有数据
    itemModel->removeRows(0, itemModel->rowCount());

    ui->personListTable->setModel(itemModel);

    // 判断是否有分页
    int max = tableRowCount > personList->size() ? personList->size() : tableRowCount;
    for (int i = 0; i < max; i++) {
        QList<QStandardItem *> row;
        // 员工编号
        itemModel->setItem(i, 0, new QStandardItem(personList->at(i).find("person_code")->toString()));
        itemModel->item(i, 0)->setTextAlignment(Qt::AlignCenter);

        // 姓名
        itemModel->setItem(i, 1, new QStandardItem(personList->at(i).find("name")->toString()));
        itemModel->item(i, 1)->setTextAlignment(Qt::AlignCenter);

        // 部门
        itemModel->setItem(i, 2, new QStandardItem(personList->at(i).find("deptname")->toString()));
        itemModel->item(i, 2)->setTextAlignment(Qt::AlignCenter);

        // 采集的人脸/虹膜
        QString hasFace = personList->at(i).find("hasFace")->toString();
        QString hasIris = personList->at(i).find("hasIris")->toString();
        QWidget * widgetLabel = new QWidget();
        QHBoxLayout * hLayout = new QHBoxLayout();
        QLabel * labFace = new QLabel();
        QLabel * labIris = new QLabel();

        if (hasFace.isEmpty() || hasFace != "1")
        {
            labFace->setPixmap(QPixmap(":/images/faceNotCap.png"));
        } else {
            labFace->setPixmap(QPixmap(":/images/faceCaped.png"));
        }
        labFace->setFixedSize(QSize(40, 40));
        labFace->setAlignment(Qt::AlignCenter);

        if (hasIris.isEmpty() || hasIris != "1")
        {
            labIris->setPixmap(QPixmap(":/images/irisNotCap.png"));
        } else {
            labIris->setPixmap(QPixmap(":/images/irisCaped.png"));
        }
        labIris->setFixedSize(QSize(40, 40));
        labIris->setAlignment(Qt::AlignCenter);

        hLayout->addWidget(labFace);
        hLayout->addWidget(labIris);
        widgetLabel->setLayout(hLayout);

        ui->personListTable->setIndexWidget(itemModel->index(i, 3), widgetLabel);

        QPushButton * btnDele = new QPushButton("删除");
        btnDele->setProperty("personId", personList->at(i).find("id")->toString());
        btnDele->setProperty("personName", personList->at(i).find("name")->toString());
        btnDele->setProperty("class", "btnDelete");
        connect(btnDele, &QPushButton::clicked, this, &PersonListForm::btnDeleClicked);
        ui->personListTable->setIndexWidget(itemModel->index(i, 4), btnDele);
    }


    // 设置每行的高度
    for ( int i = 0; i < tableRowCount; i++) {
        ui->personListTable->setRowHeight(i, 50);
    }

    // 设置每列的宽度
    ui->personListTable->setColumnWidth(0, 150);
    ui->personListTable->setColumnWidth(1, 226); // =1280-100-100-4-other col
    ui->personListTable->setColumnWidth(2, 400);
    ui->personListTable->setColumnWidth(3, 150);
    ui->personListTable->setColumnWidth(4, 150);
}

void PersonListForm::on_btnRegist_clicked()
{
    emit switchToRegistForm("");
}

void PersonListForm::on_btnHome_clicked()
{
    this->currentPage = 0;
    emit backToHomePage();
}

void PersonListForm::on_btnBack_clicked()
{
    this->currentPage = 0;
    emit backToHomePage();
}

void PersonListForm::on_selectDept_currentIndexChanged(int index)
{
    currentPage = 0;
    this->findPersonList();
}

void PersonListForm::on_btnFirst_clicked()
{
    // 第一页
    currentPage = 0;
    findPersonList();
}

void PersonListForm::on_btnLast_clicked()
{
    // 最后一页
    currentPage = totalPage - 1;
    findPersonList();
}

void PersonListForm::on_btnPre_clicked()
{
    // 上一页
    if (currentPage > 0)
    {
        currentPage = currentPage - 1;
    }
    findPersonList();
}

void PersonListForm::on_btnNext_clicked()
{
    // 下一页
    if (currentPage < totalPage - 1)
    {
        currentPage++;
    }
    findPersonList();
}

void PersonListForm::btnDeleClicked()
{
    // 获取发送者的信息
    QPushButton * senderObj = (QPushButton *) sender();
    QString personId = senderObj->property("personId").toString();
    QString personName = senderObj->property("personName").toString();

    ConfirmTipsDialog confirmDlg;
    confirmDlg.setTipsText(QString("是否删除 %1").arg(personName));
    int ret = confirmDlg.exec();
    if (ret == 1)
    {
        SysPersonDao personDao;
        bool succ = personDao.dele(personId);

        OperationTipsDialog tipsDlg;
        tipsDlg.setTipsDialogType(succ);
        tipsDlg.setTipsText(QString("%1 删除%2").arg(personName).arg(succ == true ? "成功" : "失败"));
        tipsDlg.exec();

        if (succ == 1)
        {
            findPersonList();

            // 重新加载人脸库
            ProMemory::getInstance().initFaceFeatures();
        }
    }
}

void PersonListForm::on_personListTable_doubleClicked(const QModelIndex &index)
{
    int rowIndex = index.row();
    QString personId = personList->at(rowIndex).find("id")->toString();
    emit switchToRegistForm(personId);
}