#include "renameModel.h" #include "projectModelDlg.h" #include "ui_renameModel.h" RenameModel::RenameModel(QWidget *parent) : QDialog(parent) , ui(new Ui::renameModel) ,_pParent(nullptr) { ui->setupUi(this); this->setWindowFlags(Qt::FramelessWindowHint | windowFlags()); _pParent = dynamic_cast(parent); initial(); } RenameModel::~RenameModel() { delete ui; } void RenameModel::initial() { connect(ui->btn_ok,&QPushButton::clicked,this,&RenameModel::onOkClicked); connect(ui->btn_cancel,&QPushButton::clicked,this,&RenameModel::onCancelClicked); } void RenameModel::showCenter() { if (!_pParent) { qWarning("No parent widget found; dialog will appear at the default position."); show(); return; } // 父窗口的几何信息 QRect parentRect = _pParent->geometry(); // 对话框的几何信息 int dialogWidth = width(); int dialogHeight = height(); // 计算中心位置 int x = parentRect.x() + (parentRect.width() - dialogWidth) / 2; int y = parentRect.y() + (parentRect.height() - dialogHeight) / 2; // 移动对话框到中心位置并显示 move(x, y); show(); setShowName(); } void RenameModel::setShowName() { if(_pParent) { QString str = _pParent->getProjectName(); ui->lineEdit_name->setText(str); ui->lineEdit_name->setSelection(0,str.length()); } } bool RenameModel::couldSave() { if(_pParent) { QString str = _pParent->getProjectName(); if(str == QString::fromWCharArray(L"新建")) { ui->label_info->setText(QString::fromWCharArray(L"请输入需保存的名称")); return false; } else { //todo:判断输入的名称是否存在 return true; } } } void RenameModel::onOkClicked() { if(_pParent) { if(couldSave()) { //todo:保存 _pParent->generate(ui->lineEdit_name->text()); hide(); } else { } ui->label_info->clear(); } } void RenameModel::onCancelClicked() { hide(); ui->label_info->clear(); }