123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
-
- #pragma once
- #include <QString>
- #include <QMessageBox>
- #include <QMetaEnum>
- #include <QThread>
- #include <QTime>
- #include <QEventLoop>
- #include <QTimer>
- #include <QApplication>
- #include "DataStructure.h"
- // 由于类型转换支持需要,增加此头文件
- #include "../Common/CameraBaseClass/IBaseCamCommon.h"
- #include "../Common/CameraBaseClass/IBaseCamera.h"
- ///////////////////////////////////////////////////////////////
- // 工具类
- class Utility : public QObject
- {
- Q_OBJECT
- Q_ENUM(VALUE_TYPE)
- public:
-
- /// <summary>
- /// 判断字符串是否是纯数字(正则,正负整数和0)
- /// </summary>
- static bool isIntString(QString strInput)
- {
- QRegExp rx("^-?\\d+$");
- return rx.exactMatch(strInput);
- }
-
- /// <summary>
- /// 判断字符串是否是纯数字(正则,正负浮点数和0)
- /// </summary>
- /// <param name="strInput"></param>
- /// <returns></returns>
- static bool isFloatString(QString strInput)
- {
- QRegExp rx("^(\\-|\\+)?\\d+(\\.\\d+)?$");
- return rx.exactMatch(strInput);
- }
-
- /// <summary>
- /// 弹出Messagebox(Critical)
- /// </summary>
- /// <param name="strInfo"></param>
- #define CRITICAL_MESSAGE(x) Utility::VPCriticalMessageBox(x)
- static void VPCriticalMessageBox(const QString& strInfo)
- {
- qWarning() << strInfo;
- //QMessageBox::critical(nullptr, ("Visual Plus"), strInfo);
- }
- /// <summary>
- /// 弹出Messagebox(Information)
- /// </summary>
- /// <param name="strInfo"></param>
- #define INFORMATION_MESSAGE(x) Utility::VPInformationMessageBox(x)
- static void VPInformationMessageBox(const QString& strInfo)
- {
- qInfo() << strInfo;
- //QMessageBox::information(nullptr, ("Visual Plus"), strInfo);
- }
-
- /// <summary>
- /// 弹出MessageBox(question)
- /// </summary>
- /// <param name="strInfo"></param>
- #define QUESTION_MESSAGE(x) Utility::VPQuesitonMessageBox(x)
- static QMessageBox::StandardButton VPQuesitonMessageBox(const QString& strInfo)
- {
- return QMessageBox::question(nullptr, ("Visual Plus"), strInfo,
- QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
- }
-
- /// <summary>
- /// 设置控件为错误状态
- /// </summary>
- /// <param name="pWidget"></param>
- static void setControlError(QWidget* pWidget)
- {
- QPalette palette;
- palette.setColor(QPalette::Base, qRgb(255, 199, 206));
- pWidget->setPalette(palette);
- }
-
- /// <summary>
- /// 设置控件为正常状态
- /// </summary>
- /// <param name="pWidget"></param>
- static void setControlNormal(QWidget* pWidget)
- {
- QPalette palette;
- palette.setColor(QPalette::Base, Qt::white);
- pWidget->setPalette(palette);
- }
-
- /// <summary>
- /// 设置控件为正确状态
- /// </summary>
- /// <param name="pWidget"></param>
- static void setControlOK(QWidget* pWidget)
- {
- QPalette palette;
- palette.setColor(QPalette::Base, qRgb(255, 199, 206));
- pWidget->setPalette(palette);
- }
- /// <summary>
- /// 根据Type类型返回对应的类型字符串
- /// 此处对应DataStructure.h中的 VALUE_TYPE 顺序,要确保双方顺序同步
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- static QString getTypeString(VALUE_TYPE type)
- {
- int nIndex = static_cast<int>(type);
-
- // 2022-2-1,处理了Control_Base = 100 导致的序号问题
- int nControlBaseIndex = valueString.indexOf("ControlBase");
- if (nIndex >= 100)
- {
- nIndex -= 100;
- nIndex += nControlBaseIndex;
- }
- // 将序号转换为valueStrin
- if (nIndex < valueString.size())
- {
- return valueString.at(nIndex);
- }
- else
- {
- return valueString.last();
- }
- }
-
- /// <summary>
- /// 返回字符串对应的Type值
- /// </summary>
- /// <param name="strTypeString"></param>
- /// <returns></returns>
- static VALUE_TYPE getTypeByString(const QString& strTypeString )
- {
- int nIndex = valueString.indexOf(strTypeString);
- // 2022-2-1,处理了Control_Base = 100 导致的序号问题
- int nControlBaseIndex = valueString.indexOf("ControlBase");
- if (nIndex >= nControlBaseIndex)
- {
- nIndex += 100;
- }
- if (nIndex >= 0)
- {
- return static_cast<VALUE_TYPE>(nIndex);
- }
- else
- {
- return VALUE_TYPE::Type_Unknown;
- }
- }
- /// <summary>
- /// 获取数值对应的字符串
- /// </summary>
- /// <returns></returns>
- static QString getValueString(void** pValuePtr, VALUE_TYPE type)
- {
- if (type == VALUE_TYPE::Type_Int)
- {
- return QString::number(*(int*)pValuePtr);
- }
- else if (type == VALUE_TYPE::Type_String)
- {
- return *((QString*)pValuePtr);
- }
- else if (type == VALUE_TYPE::Type_Bool)
- {
- return (*(bool*)pValuePtr == true) ? "true" : "false";
- }
- else if (type == VALUE_TYPE::Type_Float)
- {
- return QString("%1").arg(*(float*)pValuePtr);
- }
- else if (type == VALUE_TYPE::Type_Double)
- {
- return QString("%1").arg(*(double*)pValuePtr);
- }
- else if (type == VALUE_TYPE::Type_StdString)
- {
- return QString("%1").arg((*(std::string*)pValuePtr).c_str());
- }
- else if (type == VALUE_TYPE::Type_String)
- {
- return (*(QString*)pValuePtr);
- }
- else if (type == VALUE_TYPE::Type_QImage)
- {
- if ((*(QImage*)pValuePtr).isNull() )
- {
- return QString("QImage");
- }
- return QString("size %1 * %2").arg((*(QImage*)pValuePtr).size().width()).arg((*(QImage*)pValuePtr).size().height());
- }
- else if (type == VALUE_TYPE::Type_HTuple)
- {
- return QString("HTuple");
- }
- else if (type == VALUE_TYPE::Type_HObject)
- {
- return QString("HObject");
- }
- else if (type == VALUE_TYPE::Type_HImage)
- {
- return QString("HImage");
- }
- else if (type == VALUE_TYPE::Type_ST_Pos)
- {
- return QString("HPose");
- }
- else if (type == VALUE_TYPE::Type_ST_HomMat2D)
- {
- return QString("HHomMat2D");
- }
- else if (type == VALUE_TYPE::Type_pIBaseCamera)
- {
- if (*pValuePtr == nullptr)
- {
- return QString("pIBaseCamera");
- }
- return QString("%1").arg(((IBaseCamera*)*pValuePtr)->serial());
- }
- else
- {
- return ("N/A");
- }
- }
- /// <summary>
- /// 根据边的方向获取对应Widget的边
- /// </summary>
- /// <param name="widget"></param>
- /// <param name="sideID"></param>
- /// <returns></returns>
- static QLine getSideLineByID(QWidget* widget, short sideID)
- {
- QRect rc = widget->geometry();
- if (sideID == 0)
- {
- return QLine(rc.topLeft(), rc.bottomLeft());
- }
- else if (sideID == 1)
- {
- return QLine(rc.topLeft(), rc.topRight());
- }
- else if (sideID == 2)
- {
- return QLine(rc.topRight(), rc.bottomRight());
- }
- else if (sideID == 3)
- {
- return QLine(rc.bottomLeft(), rc.bottomRight());
- }
- // 错误,不应该执行到这里
- else
- {
- return QLine(-1, -1, -1, -1);
- }
- }
- /// <summary>
- /// 辅助函数:判断两条线是否对齐(一条线是否处于另外一条线的延长线中)
- /// </summary>
- /// <param name="line1"></param>
- /// <param name="line2"></param>
- /// <returns></returns>
- // 用于对齐的精度
- #define ALIGN_PRECISION 2
- static bool isAligned(const QLine& line1, const QLine& line2)
- {
- // 2022-9-8 修改了对齐规则,少于2个像素的都认为是对齐了
- // 如果4个点的x都相同,则true
- if (qAbs(line1.p1().x() - line1.p2().x()) < ALIGN_PRECISION
- && qAbs(line2.p1().x() - line2.p2().x()) < ALIGN_PRECISION
- && qAbs(line1.p1().x() - line2.p2().x()) < ALIGN_PRECISION
- )
- {
- return true;
- }
- // 如果4个点的y都相同,则true
- if (qAbs(line1.p1().y() - line1.p2().y()) < ALIGN_PRECISION
- && qAbs(line2.p1().y() - line2.p2().y()) < ALIGN_PRECISION
- && qAbs(line1.p1().y() - line2.p2().y()) < ALIGN_PRECISION
- )
- {
- return true;
- }
- return false;
- }
- ///// <summary>
- ///// 计算给定Line的长度
- ///// </summary>
- ///// <returns></returns>
- //static int getLineLength(const QLine line)
- //{
- //
- //}
- ///// <summary>
- ///// 获取数值对应的字符串
- ///// </summary>
- ///// <returns></returns>
- //template<typename T>
- //static QString getValueString(T value, VALUE_TYPE type)
- //{
- // if (type == VALUE_TYPE::Type_Int)
- // {
- // return QString::number(value);
- // }
- // else if (type == VALUE_TYPE::Type_String)
- // {
- // return value;
- // }
- // else if (type == VALUE_TYPE::Type_Bool)
- // {
- // return value;
- // }
- // else if (type == VALUE_TYPE::Type_Float)
- // {
- // return QString("%1").arg(value);
- // }
- // else
- // {
- // return ("");
- // }
- //}
- /// <summary>
- /// Sleep ,单位 ms
- /// </summary>
- /// <param name="ms"></param>
- static void qSleep(int ms)
- {
- if (ms <= 0)
- {
- return;
- }
- //#ifdef Q_OS_WIN
- // ::Sleep(uint(ms));
- //#else
- // struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
- // nanosleep(&ts, NULL);
- //#endif
- //QTime qTime = QTime::currentTime().addMSecs(ms);
- //while (QTime::currentTime() < qTime)
- // QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
- QThread::msleep(ms);
- }
- /// <summary>
- /// 生成两个范围内的随机数
- /// </summary>
- /// <param name="start"></param>
- /// <param name="end"></param>
- static int genRandNumber(int start, int end)
- {
- qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
- return qrand() % (end - start) + start;
- }
- /// <summary>
- /// 将QColor转换成qss字符串
- /// </summary>
- /// <param name="color"></param>
- /// <param name="type">类型,有"RGBA" "RGB" "HEX" 三种</param>
- /// <returns></returns>
- static QString qColor2qString(const QColor& color, const QString type = "RGB")
- {
- if (type == "RGBA")
- {
- return QString("rgba(%1,%2,%3,%4)")
- .arg(color.red())
- .arg(color.green())
- .arg(color.blue())
- .arg(color.alpha());
- }
- else if (type == "RGB")
- {
- return QString("rgb(%1,%2,%3)")
- .arg(color.red())
- .arg(color.green())
- .arg(color.blue());
- }
- else if (type == "HEX")
- {
- return QString().sprintf("#%1%02X%02X%02X",
- color.red(),
- color.green(),
- color.blue())
- .arg(color.alpha() != 255 ? QString().sprintf("%02X", color.alpha()) : QString());
- }
- return color.name();
- }
- static QString getHalconErrMessage(int errCode)
- {
- QString message = ("未定义代码:");
- switch (errCode)
- {
- case H_ERR_WIPV1:
- message = ("参数1数据异常");
- break;
- case H_ERR_WIPV2:
- message = ("参数2数据异常");
- break;
- case H_ERR_WIPV3:
- message = ("参数3数据异常");
- break;
- case H_ERR_WIPV4:
- message = ("参数4数据异常");
- break;
- case H_ERR_WIPV5:
- message = ("参数5数据异常");
- break;
- case H_ERR_WIPV6:
- message = ("参数6数据异常");
- break;
- case H_ERR_WIPV7:
- message = ("参数7数据异常");
- break;
- case H_ERR_WIPV8:
- message = ("参数8数据异常");
- break;
- case H_ERR_WIPV9:
- message = ("参数9数据异常");
- break;
- case H_ERR_WIPV10:
- message = ("参数10数据异常");
- break;
- case H_ERR_WIPV11:
- message = ("参数11数据异常");
- break;
- case H_ERR_WIPV12:
- message = ("参数12数据异常");
- break;
- case H_ERR_WIPV13:
- message = ("参数13数据异常");
- break;
- case H_ERR_WIPV14:
- message = ("参数14数据异常");
- break;
- case H_ERR_WIPV15:
- message = ("参数15数据异常");
- break;
- case H_ERR_WIPV16:
- message = ("参数16数据异常");
- break;
- case H_ERR_WIPV17:
- message = ("参数17数据异常");
- break;
- case H_ERR_WIPV18:
- message = ("参数18数据异常");
- break;
- case H_ERR_WIPV19:
- message = ("参数19数据异常");
- break;
- case H_ERR_WIPV20:
- message = ("参数20数据异常");
- break;
- case H_ERR_JPGLIB_INFORMAT:
- message = ("jpg图像格式异常.");
- break;
- default:
- message += QString::number(errCode);
- break;
- }
- return message;
- }
- private:
-
- };
- // 用于在Tool的执行线程中弹出对话框
- //class MessageBoxInThread : public QObject
- //{
- // Q_OBJECT
- //private:
- // const QString m_title;
- // const QString m_message;
- //public:
- // MessageBoxInThread(const QString& title, const QString& message)
- // :m_title(title),
- // m_message(message)
- // {
- //
- // }
- // static void showInformation(const QString& title, const QString& message)
- // {
- // QEventLoop eventLoop;
- // auto messageBox = new MessageBoxInThread(title, message);
- // connect(messageBox, SIGNAL(destroyed()), &eventLoop, SLOT(quit()));
- // messageBox->readyShow();
- // eventLoop.exec();
- // }
- //
- //private:
- //
- // void readyShow(void)
- // {
- // this->moveToThread(qApp->thread());
- // QTimer::singleShot(0, this, SLOT(onShow()));
- // }
- //
- //private slots:
- //
- // void onShow(void)
- // {
- // QMessageBox::information(NULL, m_title, m_message);
- // this->deleteLater();
- // }
- //};
|