You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
whellvalue/tpvs17/tpMain/lpImageCaliUI.cpp

274 lines
7.8 KiB
C++

#include "lpImageCaliUI.h"
#include "QFileDialog"
#include "lpGlobalConfig.h"
#include <QSettings>
#pragma execution_character_set("utf-8")
lpImageCaliUI::lpImageCaliUI(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
m_srcImgView = replaceWidget<AwesomeImgViewer>(ui.widget);
connect(m_srcImgView, SIGNAL(filterroiChanged(const AwesomeRoiInfo& , QString)), this, SLOT(onROIChange(const AwesomeRoiInfo&, QString)));
connect(m_srcImgView, SIGNAL(pixelClicked(QPoint)), this, SLOT(onPixelClicked(QPoint)));
connect(m_srcImgView, SIGNAL(roiLockIng(QString)), this, SLOT(onRoiLockIng(QString)));
connect(m_srcImgView, SIGNAL(sgImageScale(qreal)), this, SLOT(onImageScale(qreal)));
connect(ui.m_pbLoadImg, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbApply, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbExit, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
connect(ui.m_pbLock, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
QString strPath = QApplication::applicationDirPath() + "/showImg.ini";//图像展示比例的参数
QSettings setting(strPath, QSettings::IniFormat);
double nScale = setting.value("ShowImg/Scale_Standard", 0.53).toDouble();
m_srcImgView->setInitScale(nScale);
}
lpImageCaliUI::~lpImageCaliUI()
{
if (m_srcImgView) {
delete m_srcImgView;
m_srcImgView = nullptr;
}
}
template<typename _Widget>
_Widget* lpImageCaliUI::replaceWidget(QWidget* pSrcWidget)
{
if (!pSrcWidget) {
return nullptr;
}
QWidget* pParent = qobject_cast<QWidget*>(pSrcWidget->parent());
if (!pParent) {
return nullptr;
}
_Widget* pDstWidget = new _Widget;
auto *pFrom = pParent->layout()->replaceWidget(pSrcWidget, pDstWidget);
delete pFrom;
delete pSrcWidget;
return pDstWidget;
}
void lpImageCaliUI::showEvent(QShowEvent *event)
{
ui.checkBox->setChecked(lpGlobalConfig::instance()->bTransPos);
ui.m_lineEdit_P1X->setText(QString("%1").arg(lpGlobalConfig::instance()->point1.x()));
ui.m_lineEdit_P1Y->setText(QString("%1").arg(lpGlobalConfig::instance()->point1.y()));
ui.m_lineEdit_P2X->setText(QString("%1").arg(lpGlobalConfig::instance()->point2.x()));
ui.m_lineEdit_P2Y->setText(QString("%1").arg(lpGlobalConfig::instance()->point2.y()));
ui.m_lineEdit_Xoffset->setText(QString("%1").arg(lpGlobalConfig::instance()->pointXOffset));
ui.m_lineEdit_Yoffset->setText(QString("%1").arg(lpGlobalConfig::instance()->pointYOffset));
m_point1 = lpGlobalConfig::instance()->point1;
m_point2 = lpGlobalConfig::instance()->point2;
double pixlen = CalLength(m_point1, m_point2);
ui.m_lineEdit_pixlength->setText(QString("%1").arg(pixlen));
ui.m_lineEdit_length->setText(QString("%1").arg(lpGlobalConfig::instance()->fLength));
ui.m_lineEdit_scale->setText(QString("%1").arg(lpGlobalConfig::instance()->fScale));
if (m_srcImgView)
{
m_srcImgView->onClearAllROI();
QString DstPath = QApplication::applicationDirPath() + "\\user\\StandImage.png";
QImage img;
img.load(DstPath);
m_imageH = img.height();
m_imageW = img.width();
m_srcImgView->setImg(img);
AddPoint(m_point1, "P1");
AddPoint(m_point2, "P2");
m_srcImgView->setLabelVisible(true);
m_srcImgView->setLockAll(true);
}
ui.m_pbLock->setText(tr("解锁"));
}
void lpImageCaliUI::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange)
{
ui.retranslateUi(this);
}
}
Q_SLOT void lpImageCaliUI::onButtonClicked()
{
QString strObj = sender()->objectName();
if (strObj == "m_pbLoadImg")
{
QString fileName = QFileDialog::getOpenFileName(this, tr("选择标定图"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
if (!fileName.isEmpty())
{
QString DstPath = QApplication::applicationDirPath() + "\\user\\StandImage.png";
QString sourcePath = fileName;
DstPath.replace("\\", "/");
if (sourcePath == DstPath) {
}
if (!QFile::exists(sourcePath)) {
}
QDir *createfile = new QDir;
bool exist = createfile->exists(DstPath);
if (exist) {
createfile->remove(DstPath);
}
if (!QFile::copy(sourcePath, DstPath)) {
}
QImage img;
img.load(fileName);
if (m_srcImgView) {
m_srcImgView->setImg(img);
}
delete createfile;
createfile = nullptr;
}
}
// else if ("m_pbTestAdd" == strObj) {
// if (m_srcImgView)
// {
// int x = 1100;
// int y = 800;
// int xp = (x - m_imageW / 2);
// int yp = (y - m_imageH / 2);
//
// m_srcImgView->addPointRoi(QPointF(xp, yp), 15, 0,"P1",QColor(255,0,0),QColor(255,0,255));
//
// m_srcImgView->addPointRoi(QPointF(xp+100, yp), 15, 0, "P2", QColor(255, 234, 0), QColor(255, 0, 255));
//
// m_srcImgView->setLabelVisible(true);
// }
//
// }
// else if ("m_pbTestSub" == strObj) {
//
// }
else if("m_pbApply" == strObj)
{
double pixlength = ui.m_lineEdit_pixlength->text().toDouble();
double length = ui.m_lineEdit_length->text().toDouble();
double scale = length * 1.0 / ( pixlength > 0 ? pixlength:length);
ui.m_lineEdit_scale->setText(QString("%1").arg(scale));
lpGlobalConfig::instance()->pointXOffset = ui.m_lineEdit_Xoffset->text().toDouble();
lpGlobalConfig::instance()->pointYOffset = ui.m_lineEdit_Yoffset->text().toDouble();
lpGlobalConfig::instance()->point1 = m_point1;
lpGlobalConfig::instance()->point2 = m_point2;
lpGlobalConfig::instance()->fLength = length;
lpGlobalConfig::instance()->fScale = scale;
lpGlobalConfig::instance()->bTransPos = ui.checkBox->isChecked();
lpGlobalConfig::instance()->saveStandParam();
ui.label_info->setText("参数已生效");
ui.label_info->setStyleSheet("background-color: rgb(250, 168, 55);");
if (m_timerID == 0)
{
m_timerID = startTimer(1000);
}
}
else if("m_pbExit" == strObj)
{
close();
}
else if("m_pbLock" == strObj)
{
QString str = ui.m_pbLock->text();
if (str == "解锁") {
ui.m_pbLock->setText(tr("上锁"));
if (m_srcImgView)
{
m_srcImgView->setLockAll(false);
}
}
else {
if (m_srcImgView)
{
m_srcImgView->setLockAll(true);
}
ui.m_pbLock->setText(tr("解锁"));
}
}
}
Q_SLOT void lpImageCaliUI::onROIChange(const AwesomeRoiInfo& roiInfo, QString strID)
{
if (strID == "P1")
{
QPointF p = roiInfo.vertex.at(0);
m_point1.setX(p.x());
m_point1.setY(p.y());
ui.m_lineEdit_P1X->setText(QString("%1").arg(m_point1.x()));
ui.m_lineEdit_P1Y->setText(QString("%1").arg(m_point1.y()));
}
else if (strID == "P2")
{
QPointF p = roiInfo.vertex.at(0);
m_point2.setX(p.x());
m_point2.setY(p.y());
ui.m_lineEdit_P2X->setText(QString("%1").arg(m_point2.x()));
ui.m_lineEdit_P2Y->setText(QString("%1").arg(m_point2.y()));
}
double pixlen = CalLength(m_point1, m_point2);
ui.m_lineEdit_pixlength->setText(QString("%1").arg(pixlen));
}
Q_SLOT void lpImageCaliUI::onPixelClicked(QPoint point)
{
int a = 0;
}
Q_SLOT void lpImageCaliUI::onImageScale(qreal value)
{
QString strPath = QApplication::applicationDirPath() + "/showImg.ini";
QSettings setting(strPath, QSettings::IniFormat);
QObject *obj = sender();
setting.setValue("ShowImg/Scale_Standard", value);
}
double lpImageCaliUI::CalLength(QPoint &p1, QPoint &p2)
{
double pixLen = sqrt(pow(p1.x() - p2.x(), 2) + pow(p1.y() - p2.y(), 2));
return pixLen;
}
void lpImageCaliUI::AddPoint(QPoint &p,QString strName)
{
if (m_srcImgView)
{
int x = p.x();
int y = p.y();
int xp = (x - m_imageW / 2);
int yp = (y - m_imageH / 2);
m_srcImgView->addPointRoi(QPointF(xp, yp), lpGlobalConfig::instance()->pointCircle, 0, strName, QColor(0, 255, 0), QColor(255, 0, 255));
}
}
Q_SLOT void lpImageCaliUI::onRoiLockIng(QString strName)
{
ui.label_info->setText("不能操作,请解锁");
ui.label_info->setStyleSheet("background-color: rgb(255, 68, 55);");
if (m_timerID == 0)
{
m_timerID = startTimer(1000);
}
}
void lpImageCaliUI::timerEvent(QTimerEvent *event)
{
if (m_timerID == event->timerId())
{
killTimer(m_timerID);
m_timerID = 0;
ui.label_info->setText("");
ui.label_info->setStyleSheet("");
}
}