diff --git a/comtool/api/api.pri b/comtool/api/api.pri new file mode 100644 index 0000000..39e8942 --- /dev/null +++ b/comtool/api/api.pri @@ -0,0 +1,7 @@ +HEADERS += \ + $$PWD/app.h \ + $$PWD/quiwidget.h + +SOURCES += \ + $$PWD/app.cpp \ + $$PWD/quiwidget.cpp diff --git a/comtool/api/app.cpp b/comtool/api/app.cpp new file mode 100644 index 0000000..062332e --- /dev/null +++ b/comtool/api/app.cpp @@ -0,0 +1,201 @@ +#include "app.h" +#include "quiwidget.h" + +QString App::ConfigFile = "config.ini"; +QString App::SendFileName = "send.txt"; +QString App::DeviceFileName = "device.txt"; + +QString App::PortName = "COM1"; +int App::BaudRate = 9600; +int App::DataBit = 8; +QString App::Parity = "无"; +double App::StopBit = 1; + +bool App::HexSend = false; +bool App::HexReceive = false; +bool App::Debug = false; +bool App::AutoClear = false; + +bool App::AutoSend = false; +int App::SendInterval = 1000; +bool App::AutoSave = false; +int App::SaveInterval = 5000; + +QString App::Mode = "Tcp_Client"; +QString App::ServerIP = "127.0.0.1"; +int App::ServerPort = 6000; +int App::ListenPort = 6000; +int App::SleepTime = 100; +bool App::AutoConnect = false; + +void App::readConfig() +{ + if (!checkConfig()) { + return; + } + + QSettings set(App::ConfigFile, QSettings::IniFormat); + + set.beginGroup("ComConfig"); + App::PortName = set.value("PortName").toString(); + App::BaudRate = set.value("BaudRate").toInt(); + App::DataBit = set.value("DataBit").toInt(); + App::Parity = set.value("Parity").toString(); + App::StopBit = set.value("StopBit").toInt(); + + App::HexSend = set.value("HexSend").toBool(); + App::HexReceive = set.value("HexReceive").toBool(); + App::Debug = set.value("Debug").toBool(); + App::AutoClear = set.value("AutoClear").toBool(); + + App::AutoSend = set.value("AutoSend").toBool(); + App::SendInterval = set.value("SendInterval").toInt(); + App::AutoSave = set.value("AutoSave").toBool(); + App::SaveInterval = set.value("SaveInterval").toInt(); + set.endGroup(); + + set.beginGroup("NetConfig"); + App::Mode = set.value("Mode").toString(); + App::ServerIP = set.value("ServerIP").toString(); + App::ServerPort = set.value("ServerPort").toInt(); + App::ListenPort = set.value("ListenPort").toInt(); + App::SleepTime = set.value("SleepTime").toInt(); + App::AutoConnect = set.value("AutoConnect").toBool(); + set.endGroup(); +} + +void App::writeConfig() +{ + QSettings set(App::ConfigFile, QSettings::IniFormat); + + set.beginGroup("ComConfig"); + set.setValue("PortName", App::PortName); + set.setValue("BaudRate", App::BaudRate); + set.setValue("DataBit", App::DataBit); + set.setValue("Parity", App::Parity); + set.setValue("StopBit", App::StopBit); + + set.setValue("HexSend", App::HexSend); + set.setValue("HexReceive", App::HexReceive); + set.setValue("Debug", App::Debug); + set.setValue("AutoClear", App::AutoClear); + + set.setValue("AutoSend", App::AutoSend); + set.setValue("SendInterval", App::SendInterval); + set.setValue("AutoSave", App::AutoSave); + set.setValue("SaveInterval", App::SaveInterval); + set.endGroup(); + + set.beginGroup("NetConfig"); + set.setValue("Mode", App::Mode); + set.setValue("ServerIP", App::ServerIP); + set.setValue("ServerPort", App::ServerPort); + set.setValue("ListenPort", App::ListenPort); + set.setValue("SleepTime", App::SleepTime); + set.setValue("AutoConnect", App::AutoConnect); + set.endGroup(); +} + +void App::newConfig() +{ +#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) + App::Parity = App::Parity.toLatin1(); +#endif + writeConfig(); +} + +bool App::checkConfig() +{ + //如果配置文件大小为0,则以初始值继续运行,并生成配置文件 + QFile file(App::ConfigFile); + if (file.size() == 0) { + newConfig(); + return false; + } + + //如果配置文件不完整,则以初始值继续运行,并生成配置文件 + if (file.open(QFile::ReadOnly)) { + bool ok = true; + while (!file.atEnd()) { + QString line = file.readLine(); + line = line.replace("\r", ""); + line = line.replace("\n", ""); + QStringList list = line.split("="); + + if (list.count() == 2) { + if (list.at(1) == "") { + ok = false; + break; + } + } + } + + if (!ok) { + newConfig(); + return false; + } + } else { + newConfig(); + return false; + } + + return true; +} + +QStringList App::Intervals = QStringList(); +QStringList App::Datas = QStringList(); +QStringList App::Keys = QStringList(); +QStringList App::Values = QStringList(); + +void App::readSendData() +{ + //读取发送数据列表 + App::Datas.clear(); + QString fileName = QString("%1/%2").arg(QUIHelper::appPath()).arg(App::SendFileName); + QFile file(fileName); + if (file.size() > 0 && file.open(QFile::ReadOnly | QIODevice::Text)) { + while (!file.atEnd()) { + QString line = file.readLine(); + line = line.trimmed(); + line = line.replace("\r", ""); + line = line.replace("\n", ""); + if (!line.isEmpty()) { + App::Datas.append(line); + } + } + + file.close(); + } +} + +void App::readDeviceData() +{ + //读取转发数据列表 + App::Keys.clear(); + App::Values.clear(); + QString fileName = QString("%1/%2").arg(QUIHelper::appPath()).arg(App::DeviceFileName); + QFile file(fileName); + if (file.size() > 0 && file.open(QFile::ReadOnly | QIODevice::Text)) { + while (!file.atEnd()) { + QString line = file.readLine(); + line = line.trimmed(); + line = line.replace("\r", ""); + line = line.replace("\n", ""); + if (!line.isEmpty()) { + QStringList list = line.split(";"); + QString key = list.at(0); + QString value; + for (int i = 1; i < list.count(); i++) { + value += QString("%1;").arg(list.at(i)); + } + + //去掉末尾分号 + value = value.mid(0, value.length() - 1); + App::Keys.append(key); + App::Values.append(value); + } + } + + file.close(); + } +} diff --git a/comtool/api/app.h b/comtool/api/app.h new file mode 100644 index 0000000..22c3840 --- /dev/null +++ b/comtool/api/app.h @@ -0,0 +1,51 @@ +#ifndef APP_H +#define APP_H + +#include "head.h" + +class App +{ +public: + static QString ConfigFile; //配置文件路径 + static QString SendFileName; //发送配置文件名 + static QString DeviceFileName; //模拟设备数据文件名 + + static QString PortName; //串口号 + static int BaudRate; //波特率 + static int DataBit; //数据位 + static QString Parity; //校验位 + static double StopBit; //停止位 + + static bool HexSend; //16进制发送 + static bool HexReceive; //16进制接收 + static bool Debug; //模拟设备 + static bool AutoClear; //自动清空 + + static bool AutoSend; //自动发送 + static int SendInterval; //自动发送间隔 + static bool AutoSave; //自动保存 + static int SaveInterval; //自动保存间隔 + + static QString Mode; //转换模式 + static QString ServerIP; //服务器IP + static int ServerPort; //服务器端口 + static int ListenPort; //监听端口 + static int SleepTime; //延时时间 + static bool AutoConnect; //自动重连 + + //读写配置参数及其他操作 + static void readConfig(); //读取配置参数 + static void writeConfig(); //写入配置参数 + static void newConfig(); //以初始值新建配置文件 + static bool checkConfig(); //校验配置文件 + + static QStringList Intervals; + static QStringList Datas; + static QStringList Keys; + static QStringList Values; + static void readSendData(); + static void readDeviceData(); + +}; + +#endif // APP_H diff --git a/comtool/api/quiwidget.cpp b/comtool/api/quiwidget.cpp new file mode 100644 index 0000000..3153671 --- /dev/null +++ b/comtool/api/quiwidget.cpp @@ -0,0 +1,3688 @@ +#include "quiwidget.h" +#ifdef Q_OS_ANDROID +#include "qandroid.h" +#endif + +#ifdef arma7 +#define TOOL true +#else +#define TOOL false +#endif + +QUIWidget::QUIWidget(QWidget *parent) : QDialog(parent) +{ + this->initControl(); + this->initForm(); + QUIHelper::setFormInCenter(this); +} + +QUIWidget::~QUIWidget() +{ +} + +bool QUIWidget::eventFilter(QObject *watched, QEvent *event) +{ + static QPoint mousePoint; + static bool mousePressed = false; + + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->type() == QEvent::MouseButtonPress) { + if (mouseEvent->button() == Qt::LeftButton) { + mousePressed = true; + mousePoint = mouseEvent->globalPos() - this->pos(); + } + } else if (mouseEvent->type() == QEvent::MouseButtonRelease) { + mousePressed = false; + } else if (mouseEvent->type() == QEvent::MouseMove) { + if (mousePressed && (mouseEvent->buttons() && Qt::LeftButton)) { + if (this->property("canMove").toBool()) { + this->move(mouseEvent->globalPos() - mousePoint); + } + } + } else if (mouseEvent->type() == QEvent::MouseButtonDblClick) { + //以下写法可以将双击识别限定在标题栏 + if (this->btnMenu_Max->isVisible() && watched == this->widgetTitle) { + //if (this->btnMenu_Max->isVisible()) { + this->on_btnMenu_Max_clicked(); + } + } + + return QWidget::eventFilter(watched, event); +} + +QLabel *QUIWidget::getLabIco() const +{ + return this->labIco; +} + +QLabel *QUIWidget::getLabTitle() const +{ + return this->labTitle; +} + +QToolButton *QUIWidget::getBtnMenu() const +{ + return this->btnMenu; +} + +QPushButton *QUIWidget::getBtnMenuMin() const +{ + return this->btnMenu_Min; +} + +QPushButton *QUIWidget::getBtnMenuMax() const +{ + return this->btnMenu_Max; +} + +QPushButton *QUIWidget::getBtnMenuMClose() const +{ + return this->btnMenu_Close; +} + +QString QUIWidget::getTitle() const +{ + return this->title; +} + +Qt::Alignment QUIWidget::getAlignment() const +{ + return this->alignment; +} + +bool QUIWidget::getMinHide() const +{ + return this->minHide; +} + +bool QUIWidget::getExitAll() const +{ + return this->exitAll; +} + +QSize QUIWidget::sizeHint() const +{ + return QSize(600, 450); +} + +QSize QUIWidget::minimumSizeHint() const +{ + return QSize(200, 150); +} + +void QUIWidget::initControl() +{ + this->setObjectName(QString::fromUtf8("QUIWidget")); + this->resize(900, 750); + verticalLayout1 = new QVBoxLayout(this); + verticalLayout1->setSpacing(0); + verticalLayout1->setContentsMargins(11, 11, 11, 11); + verticalLayout1->setObjectName(QString::fromUtf8("verticalLayout1")); + verticalLayout1->setContentsMargins(1, 1, 1, 1); + widgetMain = new QWidget(this); + widgetMain->setObjectName(QString::fromUtf8("widgetMainQUI")); + verticalLayout2 = new QVBoxLayout(widgetMain); + verticalLayout2->setSpacing(0); + verticalLayout2->setContentsMargins(11, 11, 11, 11); + verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout2")); + verticalLayout2->setContentsMargins(0, 0, 0, 0); + widgetTitle = new QWidget(widgetMain); + widgetTitle->setObjectName(QString::fromUtf8("widgetTitle")); + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(widgetTitle->sizePolicy().hasHeightForWidth()); + widgetTitle->setSizePolicy(sizePolicy); + widgetTitle->setMinimumSize(QSize(0, 30)); + horizontalLayout4 = new QHBoxLayout(widgetTitle); + horizontalLayout4->setSpacing(0); + horizontalLayout4->setContentsMargins(11, 11, 11, 11); + horizontalLayout4->setObjectName(QString::fromUtf8("horizontalLayout4")); + horizontalLayout4->setContentsMargins(0, 0, 0, 0); + + labIco = new QLabel(widgetTitle); + labIco->setObjectName(QString::fromUtf8("labIco")); + QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred); + sizePolicy1.setHorizontalStretch(0); + sizePolicy1.setVerticalStretch(0); + sizePolicy1.setHeightForWidth(labIco->sizePolicy().hasHeightForWidth()); + labIco->setSizePolicy(sizePolicy1); + labIco->setMinimumSize(QSize(30, 0)); + labIco->setAlignment(Qt::AlignCenter); + horizontalLayout4->addWidget(labIco); + + labTitle = new QLabel(widgetTitle); + labTitle->setObjectName(QString::fromUtf8("labTitle")); + QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Preferred); + sizePolicy2.setHorizontalStretch(0); + sizePolicy2.setVerticalStretch(0); + sizePolicy2.setHeightForWidth(labTitle->sizePolicy().hasHeightForWidth()); + labTitle->setSizePolicy(sizePolicy2); + labTitle->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter); + horizontalLayout4->addWidget(labTitle); + + widgetMenu = new QWidget(widgetTitle); + widgetMenu->setObjectName(QString::fromUtf8("widgetMenu")); + sizePolicy1.setHeightForWidth(widgetMenu->sizePolicy().hasHeightForWidth()); + widgetMenu->setSizePolicy(sizePolicy1); + horizontalLayout = new QHBoxLayout(widgetMenu); + horizontalLayout->setSpacing(0); + horizontalLayout->setContentsMargins(11, 11, 11, 11); + horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); + horizontalLayout->setContentsMargins(0, 0, 0, 0); + + btnMenu = new QToolButton(widgetMenu); + btnMenu->setObjectName(QString::fromUtf8("btnMenu")); + QSizePolicy sizePolicy3(QSizePolicy::Fixed, QSizePolicy::Expanding); + sizePolicy3.setHorizontalStretch(0); + sizePolicy3.setVerticalStretch(0); + sizePolicy3.setHeightForWidth(btnMenu->sizePolicy().hasHeightForWidth()); + btnMenu->setSizePolicy(sizePolicy3); + btnMenu->setMinimumSize(QSize(30, 0)); + btnMenu->setMaximumSize(QSize(30, 16777215)); + btnMenu->setFocusPolicy(Qt::NoFocus); + btnMenu->setPopupMode(QToolButton::InstantPopup); + horizontalLayout->addWidget(btnMenu); + + btnMenu_Min = new QPushButton(widgetMenu); + btnMenu_Min->setObjectName(QString::fromUtf8("btnMenu_Min")); + QSizePolicy sizePolicy4(QSizePolicy::Minimum, QSizePolicy::Expanding); + sizePolicy4.setHorizontalStretch(0); + sizePolicy4.setVerticalStretch(0); + sizePolicy4.setHeightForWidth(btnMenu_Min->sizePolicy().hasHeightForWidth()); + btnMenu_Min->setSizePolicy(sizePolicy4); + btnMenu_Min->setMinimumSize(QSize(30, 0)); + btnMenu_Min->setMaximumSize(QSize(30, 16777215)); + btnMenu_Min->setCursor(QCursor(Qt::ArrowCursor)); + btnMenu_Min->setFocusPolicy(Qt::NoFocus); + horizontalLayout->addWidget(btnMenu_Min); + + btnMenu_Max = new QPushButton(widgetMenu); + btnMenu_Max->setObjectName(QString::fromUtf8("btnMenu_Max")); + sizePolicy3.setHeightForWidth(btnMenu_Max->sizePolicy().hasHeightForWidth()); + btnMenu_Max->setSizePolicy(sizePolicy3); + btnMenu_Max->setMinimumSize(QSize(30, 0)); + btnMenu_Max->setMaximumSize(QSize(30, 16777215)); + btnMenu_Max->setCursor(QCursor(Qt::ArrowCursor)); + btnMenu_Max->setFocusPolicy(Qt::NoFocus); + horizontalLayout->addWidget(btnMenu_Max); + + btnMenu_Close = new QPushButton(widgetMenu); + btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close")); + sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth()); + btnMenu_Close->setSizePolicy(sizePolicy3); + btnMenu_Close->setMinimumSize(QSize(30, 0)); + btnMenu_Close->setMaximumSize(QSize(30, 16777215)); + btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor)); + btnMenu_Close->setFocusPolicy(Qt::NoFocus); + horizontalLayout->addWidget(btnMenu_Close); + horizontalLayout4->addWidget(widgetMenu); + verticalLayout2->addWidget(widgetTitle); + + widget = new QWidget(widgetMain); + widget->setObjectName(QString::fromUtf8("widget")); + verticalLayout3 = new QVBoxLayout(widget); + verticalLayout3->setSpacing(0); + verticalLayout3->setContentsMargins(11, 11, 11, 11); + verticalLayout3->setObjectName(QString::fromUtf8("verticalLayout3")); + verticalLayout3->setContentsMargins(0, 0, 0, 0); + verticalLayout2->addWidget(widget); + verticalLayout1->addWidget(widgetMain); + + connect(this->btnMenu_Min, SIGNAL(clicked()), this, SLOT(on_btnMenu_Min_clicked())); + connect(this->btnMenu_Max, SIGNAL(clicked()), this, SLOT(on_btnMenu_Max_clicked())); + connect(this->btnMenu_Close, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); +} + +void QUIWidget::initForm() +{ + //设置图形字体 + setIcon(QUIWidget::Lab_Ico, QUIConfig::IconMain, 11); + setIcon(QUIWidget::BtnMenu, QUIConfig::IconMenu); + setIcon(QUIWidget::BtnMenu_Min, QUIConfig::IconMin); + setIcon(QUIWidget::BtnMenu_Normal, QUIConfig::IconNormal); + setIcon(QUIWidget::BtnMenu_Close, QUIConfig::IconClose); + + this->setProperty("form", true); + this->setProperty("canMove", true); + this->widgetTitle->setProperty("form", "title"); + this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint); + + //设置标题及对齐方式 + title = "QUI Demo"; + alignment = Qt::AlignLeft | Qt::AlignVCenter; + minHide = false; + exitAll = true; + mainWidget = 0; + + setVisible(QUIWidget::BtnMenu, false); + + //绑定事件过滤器监听鼠标移动 + this->installEventFilter(this); + this->widgetTitle->installEventFilter(this); + + //添加换肤菜单 + QStringList styleNames; + styleNames << "银色" << "蓝色" << "浅蓝色" << "深蓝色" << "灰色" << "浅灰色" << "深灰色" << "黑色" + << "浅黑色" << "深黑色" << "PS黑色" << "黑色扁平" << "白色扁平" << "蓝色扁平" << "紫色" << "黑蓝色" << "视频黑"; + + foreach (QString styleName, styleNames) { + QAction *action = new QAction(styleName, this); + connect(action, SIGNAL(triggered(bool)), this, SLOT(changeStyle())); + this->btnMenu->addAction(action); + } +} + +void QUIWidget::changeStyle() +{ + QAction *act = (QAction *)sender(); + QString name = act->text(); + QString qssFile = ":/qss/lightblue.css"; + + if (name == "银色") { + qssFile = ":/qss/silvery.css"; + QUIHelper::setStyle(QUIWidget::Style_Silvery); + } else if (name == "蓝色") { + qssFile = ":/qss/blue.css"; + QUIHelper::setStyle(QUIWidget::Style_Blue); + } else if (name == "浅蓝色") { + qssFile = ":/qss/lightblue.css"; + QUIHelper::setStyle(QUIWidget::Style_LightBlue); + } else if (name == "深蓝色") { + qssFile = ":/qss/darkblue.css"; + QUIHelper::setStyle(QUIWidget::Style_DarkBlue); + } else if (name == "灰色") { + qssFile = ":/qss/gray.css"; + QUIHelper::setStyle(QUIWidget::Style_Gray); + } else if (name == "浅灰色") { + qssFile = ":/qss/lightgray.css"; + QUIHelper::setStyle(QUIWidget::Style_LightGray); + } else if (name == "深灰色") { + qssFile = ":/qss/darkgray.css"; + QUIHelper::setStyle(QUIWidget::Style_DarkGray); + } else if (name == "黑色") { + qssFile = ":/qss/black.css"; + QUIHelper::setStyle(QUIWidget::Style_Black); + } else if (name == "浅黑色") { + qssFile = ":/qss/lightblack.css"; + QUIHelper::setStyle(QUIWidget::Style_LightBlack); + } else if (name == "深黑色") { + qssFile = ":/qss/darkblack.css"; + QUIHelper::setStyle(QUIWidget::Style_DarkBlack); + } else if (name == "PS黑色") { + qssFile = ":/qss/psblack.css"; + QUIHelper::setStyle(QUIWidget::Style_PSBlack); + } else if (name == "黑色扁平") { + qssFile = ":/qss/flatblack.css"; + QUIHelper::setStyle(QUIWidget::Style_FlatBlack); + } else if (name == "白色扁平") { + qssFile = ":/qss/flatwhite.css"; + QUIHelper::setStyle(QUIWidget::Style_FlatWhite); + } else if (name == "蓝色扁平") { + qssFile = ":/qss/flatblue.css"; + QUIHelper::setStyle(QUIWidget::Style_FlatBlue); + } else if (name == "紫色") { + qssFile = ":/qss/purple.css"; + QUIHelper::setStyle(QUIWidget::Style_Purple); + } else if (name == "黑蓝色") { + qssFile = ":/qss/blackblue.css"; + QUIHelper::setStyle(QUIWidget::Style_BlackBlue); + } else if (name == "视频黑") { + qssFile = ":/qss/blackvideo.css"; + QUIHelper::setStyle(QUIWidget::Style_BlackVideo); + } + + emit changeStyle(qssFile); +} + +void QUIWidget::setIcon(QUIWidget::Widget widget, const QChar &str, quint32 size) +{ + if (widget == QUIWidget::Lab_Ico) { + setIconMain(str, size); + } else if (widget == QUIWidget::BtnMenu) { + QUIConfig::IconMenu = str; + IconHelper::Instance()->setIcon(this->btnMenu, str, size); + } else if (widget == QUIWidget::BtnMenu_Min) { + QUIConfig::IconMin = str; + IconHelper::Instance()->setIcon(this->btnMenu_Min, str, size); + } else if (widget == QUIWidget::BtnMenu_Max) { + QUIConfig::IconMax = str; + IconHelper::Instance()->setIcon(this->btnMenu_Max, str, size); + } else if (widget == QUIWidget::BtnMenu_Normal) { + QUIConfig::IconNormal = str; + IconHelper::Instance()->setIcon(this->btnMenu_Max, str, size); + } else if (widget == QUIWidget::BtnMenu_Close) { + QUIConfig::IconClose = str; + IconHelper::Instance()->setIcon(this->btnMenu_Close, str, size); + } +} + +void QUIWidget::setIconMain(const QChar &str, quint32 size) +{ + QUIConfig::IconMain = str; + IconHelper::Instance()->setIcon(this->labIco, str, size); + QUIMessageBox::Instance()->setIconMain(str, size); + QUIInputBox::Instance()->setIconMain(str, size); + QUIDateSelect::Instance()->setIconMain(str, size); +} + +void QUIWidget::setPixmap(QUIWidget::Widget widget, const QString &file, const QSize &size) +{ + //按照宽高比自动缩放 + QPixmap pix = QPixmap(file); + pix = pix.scaled(size, Qt::KeepAspectRatio); + if (widget == QUIWidget::Lab_Ico) { + this->labIco->setPixmap(pix); + } else if (widget == QUIWidget::BtnMenu) { + this->btnMenu->setIcon(QIcon(file)); + } else if (widget == QUIWidget::BtnMenu_Min) { + this->btnMenu_Min->setIcon(QIcon(file)); + } else if (widget == QUIWidget::BtnMenu_Max) { + this->btnMenu_Max->setIcon(QIcon(file)); + } else if (widget == QUIWidget::BtnMenu_Close) { + this->btnMenu_Close->setIcon(QIcon(file)); + } +} + +void QUIWidget::setVisible(QUIWidget::Widget widget, bool visible) +{ + if (widget == QUIWidget::Lab_Ico) { + this->labIco->setVisible(visible); + } else if (widget == QUIWidget::BtnMenu) { + this->btnMenu->setVisible(visible); + } else if (widget == QUIWidget::BtnMenu_Min) { + this->btnMenu_Min->setVisible(visible); + } else if (widget == QUIWidget::BtnMenu_Max) { + this->btnMenu_Max->setVisible(visible); + } else if (widget == QUIWidget::BtnMenu_Close) { + this->btnMenu_Close->setVisible(visible); + } +} + +void QUIWidget::setOnlyCloseBtn() +{ + this->btnMenu->setVisible(false); + this->btnMenu_Min->setVisible(false); + this->btnMenu_Max->setVisible(false); +} + +void QUIWidget::setTitleHeight(int height) +{ + this->widgetTitle->setFixedHeight(height); +} + +void QUIWidget::setBtnWidth(int width) +{ + this->labIco->setFixedWidth(width); + this->btnMenu->setFixedWidth(width); + this->btnMenu_Min->setFixedWidth(width); + this->btnMenu_Max->setFixedWidth(width); + this->btnMenu_Close->setFixedWidth(width); +} + +void QUIWidget::setTitle(const QString &title) +{ + if (this->title != title) { + this->title = title; + this->labTitle->setText(title); + this->setWindowTitle(this->labTitle->text()); + } +} + +void QUIWidget::setAlignment(Qt::Alignment alignment) +{ + if (this->alignment != alignment) { + this->alignment = alignment; + this->labTitle->setAlignment(alignment); + } +} + +void QUIWidget::setMinHide(bool minHide) +{ + if (this->minHide != minHide) { + this->minHide = minHide; + } +} + +void QUIWidget::setExitAll(bool exitAll) +{ + if (this->exitAll != exitAll) { + this->exitAll = exitAll; + } +} + +void QUIWidget::setMainWidget(QWidget *mainWidget) +{ + //一个QUI窗体对象只能设置一个主窗体 + if (this->mainWidget == 0) { + //将子窗体添加到布局 + this->widget->layout()->addWidget(mainWidget); + //自动设置大小 + resize(mainWidget->width(), mainWidget->height() + this->widgetTitle->height()); + this->mainWidget = mainWidget; + } +} + +void QUIWidget::on_btnMenu_Min_clicked() +{ + if (minHide) { + hide(); + } else { + showMinimized(); + } +} + +void QUIWidget::on_btnMenu_Max_clicked() +{ + static bool max = false; + static QRect location = this->geometry(); + + if (max) { + this->setGeometry(location); + setIcon(QUIWidget::BtnMenu_Normal, QUIConfig::IconNormal); + } else { + location = this->geometry(); + this->setGeometry(qApp->desktop()->availableGeometry()); + setIcon(QUIWidget::BtnMenu_Max, QUIConfig::IconMax); + } + + this->setProperty("canMove", max); + max = !max; +} + +void QUIWidget::on_btnMenu_Close_clicked() +{ + //先发送关闭信号 + emit closing(); + mainWidget->close(); + if (exitAll) { + this->close(); + } +} + + +QScopedPointer QUIMessageBox::self; +QUIMessageBox *QUIMessageBox::Instance() +{ + if (self.isNull()) { + static QMutex mutex; + QMutexLocker locker(&mutex); + if (self.isNull()) { + self.reset(new QUIMessageBox); + } + } + + return self.data(); +} + +QUIMessageBox::QUIMessageBox(QWidget *parent) : QDialog(parent) +{ + this->initControl(); + this->initForm(); + QUIHelper::setFormInCenter(this); +} + +QUIMessageBox::~QUIMessageBox() +{ + delete widgetMain; +} + +void QUIMessageBox::closeEvent(QCloseEvent *) +{ + closeSec = 0; + currentSec = 0; +} + +bool QUIMessageBox::eventFilter(QObject *watched, QEvent *event) +{ + static QPoint mousePoint; + static bool mousePressed = false; + + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->type() == QEvent::MouseButtonPress) { + if (mouseEvent->button() == Qt::LeftButton) { + mousePressed = true; + mousePoint = mouseEvent->globalPos() - this->pos(); + return true; + } + } else if (mouseEvent->type() == QEvent::MouseButtonRelease) { + mousePressed = false; + return true; + } else if (mouseEvent->type() == QEvent::MouseMove) { + if (mousePressed && (mouseEvent->buttons() && Qt::LeftButton)) { + this->move(mouseEvent->globalPos() - mousePoint); + return true; + } + } + + return QWidget::eventFilter(watched, event); +} + +void QUIMessageBox::initControl() +{ + this->setObjectName(QString::fromUtf8("QUIMessageBox")); + + verticalLayout1 = new QVBoxLayout(this); + verticalLayout1->setSpacing(0); + verticalLayout1->setObjectName(QString::fromUtf8("verticalLayout1")); + verticalLayout1->setContentsMargins(1, 1, 1, 1); + widgetTitle = new QWidget(this); + widgetTitle->setObjectName(QString::fromUtf8("widgetTitle")); + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(widgetTitle->sizePolicy().hasHeightForWidth()); + widgetTitle->setSizePolicy(sizePolicy); + widgetTitle->setMinimumSize(QSize(0, TitleMinSize)); + horizontalLayout3 = new QHBoxLayout(widgetTitle); + horizontalLayout3->setSpacing(0); + horizontalLayout3->setObjectName(QString::fromUtf8("horizontalLayout3")); + horizontalLayout3->setContentsMargins(0, 0, 0, 0); + labIco = new QLabel(widgetTitle); + labIco->setObjectName(QString::fromUtf8("labIco")); + QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred); + sizePolicy1.setHorizontalStretch(0); + sizePolicy1.setVerticalStretch(0); + sizePolicy1.setHeightForWidth(labIco->sizePolicy().hasHeightForWidth()); + labIco->setSizePolicy(sizePolicy1); + labIco->setMinimumSize(QSize(TitleMinSize, 0)); + labIco->setAlignment(Qt::AlignCenter); + + horizontalLayout3->addWidget(labIco); + + labTitle = new QLabel(widgetTitle); + labTitle->setObjectName(QString::fromUtf8("labTitle")); + labTitle->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter); + + horizontalLayout3->addWidget(labTitle); + + labTime = new QLabel(widgetTitle); + labTime->setObjectName(QString::fromUtf8("labTime")); + QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Preferred); + sizePolicy2.setHorizontalStretch(0); + sizePolicy2.setVerticalStretch(0); + sizePolicy2.setHeightForWidth(labTime->sizePolicy().hasHeightForWidth()); + labTime->setSizePolicy(sizePolicy2); + labTime->setAlignment(Qt::AlignCenter); + + horizontalLayout3->addWidget(labTime); + + widgetMenu = new QWidget(widgetTitle); + widgetMenu->setObjectName(QString::fromUtf8("widgetMenu")); + sizePolicy1.setHeightForWidth(widgetMenu->sizePolicy().hasHeightForWidth()); + widgetMenu->setSizePolicy(sizePolicy1); + horizontalLayout4 = new QHBoxLayout(widgetMenu); + horizontalLayout4->setSpacing(0); + horizontalLayout4->setObjectName(QString::fromUtf8("horizontalLayout4")); + horizontalLayout4->setContentsMargins(0, 0, 0, 0); + btnMenu_Close = new QPushButton(widgetMenu); + btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close")); + QSizePolicy sizePolicy3(QSizePolicy::Minimum, QSizePolicy::Expanding); + sizePolicy3.setHorizontalStretch(0); + sizePolicy3.setVerticalStretch(0); + sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth()); + btnMenu_Close->setSizePolicy(sizePolicy3); + btnMenu_Close->setMinimumSize(QSize(TitleMinSize, 0)); + btnMenu_Close->setMaximumSize(QSize(TitleMinSize, 16777215)); + btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor)); + btnMenu_Close->setFocusPolicy(Qt::NoFocus); + btnMenu_Close->setFlat(true); + + horizontalLayout4->addWidget(btnMenu_Close); + horizontalLayout3->addWidget(widgetMenu); + verticalLayout1->addWidget(widgetTitle); + + widgetMain = new QWidget(this); + widgetMain->setObjectName(QString::fromUtf8("widgetMainQUI")); + verticalLayout2 = new QVBoxLayout(widgetMain); + verticalLayout2->setSpacing(5); + verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout2")); + verticalLayout2->setContentsMargins(5, 5, 5, 5); + frame = new QFrame(widgetMain); + frame->setObjectName(QString::fromUtf8("frame")); + frame->setFrameShape(QFrame::Box); + frame->setFrameShadow(QFrame::Sunken); + verticalLayout4 = new QVBoxLayout(frame); + verticalLayout4->setObjectName(QString::fromUtf8("verticalLayout4")); + verticalLayout4->setContentsMargins(-1, 9, -1, -1); + horizontalLayout1 = new QHBoxLayout(); + horizontalLayout1->setObjectName(QString::fromUtf8("horizontalLayout1")); + labIcoMain = new QLabel(frame); + labIcoMain->setObjectName(QString::fromUtf8("labIcoMain")); + labIcoMain->setAlignment(Qt::AlignCenter); + horizontalLayout1->addWidget(labIcoMain); + horizontalSpacer1 = new QSpacerItem(5, 0, QSizePolicy::Minimum, QSizePolicy::Minimum); + horizontalLayout1->addItem(horizontalSpacer1); + + labInfo = new QLabel(frame); + labInfo->setObjectName(QString::fromUtf8("labInfo")); + QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Expanding); + sizePolicy4.setHorizontalStretch(0); + sizePolicy4.setVerticalStretch(0); + sizePolicy4.setHeightForWidth(labInfo->sizePolicy().hasHeightForWidth()); + labInfo->setSizePolicy(sizePolicy4); + labInfo->setMinimumSize(QSize(0, TitleMinSize)); + labInfo->setScaledContents(false); + labInfo->setWordWrap(true); + horizontalLayout1->addWidget(labInfo); + verticalLayout4->addLayout(horizontalLayout1); + + horizontalLayout2 = new QHBoxLayout(); + horizontalLayout2->setObjectName(QString::fromUtf8("horizontalLayout2")); + horizontalSpacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); + horizontalLayout2->addItem(horizontalSpacer2); + + btnOk = new QPushButton(frame); + btnOk->setObjectName(QString::fromUtf8("btnOk")); + btnOk->setMinimumSize(QSize(85, 0)); + btnOk->setFocusPolicy(Qt::StrongFocus); + btnOk->setIcon(QIcon(":/image/btn_ok.png")); + horizontalLayout2->addWidget(btnOk); + + btnCancel = new QPushButton(frame); + btnCancel->setObjectName(QString::fromUtf8("btnCancel")); + btnCancel->setMinimumSize(QSize(85, 0)); + btnCancel->setFocusPolicy(Qt::StrongFocus); + btnCancel->setIcon(QIcon(":/image/btn_close.png")); + horizontalLayout2->addWidget(btnCancel); + + verticalLayout4->addLayout(horizontalLayout2); + verticalLayout2->addWidget(frame); + verticalLayout1->addWidget(widgetMain); + + widgetTitle->raise(); + widgetMain->raise(); + frame->raise(); + + btnOk->setText("确定"); + btnCancel->setText("取消"); + + connect(btnOk, SIGNAL(clicked()), this, SLOT(on_btnOk_clicked())); + connect(btnCancel, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); + connect(btnMenu_Close, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); +} + +void QUIMessageBox::initForm() +{ + IconHelper::Instance()->setIcon(labIco, QUIConfig::IconMain, QUIConfig::FontSize + 2); + IconHelper::Instance()->setIcon(btnMenu_Close, QUIConfig::IconClose, QUIConfig::FontSize); + + this->setProperty("form", true); + this->widgetTitle->setProperty("form", "title"); + if (TOOL) { + this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); + } else { + this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); + } + + this->setWindowTitle(this->labTitle->text()); + +#ifdef __arm__ + int width = 90; + int iconWidth = 22; + int iconHeight = 22; + this->setFixedSize(350, 180); + labIcoMain->setFixedSize(40, 40); +#else + int width = 80; + int iconWidth = 18; + int iconHeight = 18; + this->setFixedSize(280, 150); + labIcoMain->setFixedSize(30, 30); +#endif + + QList btns = this->frame->findChildren(); + foreach (QPushButton *btn, btns) { + btn->setMinimumWidth(width); + btn->setIconSize(QSize(iconWidth, iconHeight)); + } + + closeSec = 0; + currentSec = 0; + + QTimer *timer = new QTimer(this); + timer->setInterval(1000); + connect(timer, SIGNAL(timeout()), this, SLOT(checkSec())); + timer->start(); + + this->installEventFilter(this); +} + +void QUIMessageBox::checkSec() +{ + if (closeSec == 0) { + return; + } + + if (currentSec < closeSec) { + currentSec++; + } else { + this->close(); + } + + QString str = QString("关闭倒计时 %1 s").arg(closeSec - currentSec + 1); + this->labTime->setText(str); +} + +void QUIMessageBox::on_btnOk_clicked() +{ + done(QMessageBox::Yes); + close(); +} + +void QUIMessageBox::on_btnMenu_Close_clicked() +{ + done(QMessageBox::No); + close(); +} + +void QUIMessageBox::setIconMain(const QChar &str, quint32 size) +{ + IconHelper::Instance()->setIcon(this->labIco, str, size); +} + +void QUIMessageBox::setMessage(const QString &msg, int type, int closeSec) +{ + this->closeSec = closeSec; + this->currentSec = 0; + this->labTime->clear(); + checkSec(); + + //图片存在则取图片,不存在则取图形字体 + int size = this->labIcoMain->size().height(); + bool exist = !QImage(":/image/msg_info.png").isNull(); + if (type == 0) { + if (exist) { + this->labIcoMain->setStyleSheet("border-image: url(:/image/msg_info.png);"); + } else { + IconHelper::Instance()->setIcon(this->labIcoMain, 0xf05a, size); + } + + this->btnCancel->setVisible(false); + this->labTitle->setText("提示"); + } else if (type == 1) { + if (exist) { + this->labIcoMain->setStyleSheet("border-image: url(:/image/msg_question.png);"); + } else { + IconHelper::Instance()->setIcon(this->labIcoMain, 0xf059, size); + } + + this->labTitle->setText("询问"); + } else if (type == 2) { + if (exist) { + this->labIcoMain->setStyleSheet("border-image: url(:/image/msg_error.png);"); + } else { + IconHelper::Instance()->setIcon(this->labIcoMain, 0xf057, size); + } + + this->btnCancel->setVisible(false); + this->labTitle->setText("错误"); + } + + this->labInfo->setText(msg); + this->setWindowTitle(this->labTitle->text()); +} + + +QScopedPointer QUITipBox::self; +QUITipBox *QUITipBox::Instance() +{ + if (self.isNull()) { + static QMutex mutex; + QMutexLocker locker(&mutex); + if (self.isNull()) { + self.reset(new QUITipBox); + } + } + + return self.data(); +} + +QUITipBox::QUITipBox(QWidget *parent) : QDialog(parent) +{ + this->initControl(); + this->initForm(); +} + +QUITipBox::~QUITipBox() +{ + delete widgetMain; +} + +void QUITipBox::closeEvent(QCloseEvent *) +{ + closeSec = 0; + currentSec = 0; +} + +bool QUITipBox::eventFilter(QObject *watched, QEvent *event) +{ + static QPoint mousePoint; + static bool mousePressed = false; + + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->type() == QEvent::MouseButtonPress) { + if (mouseEvent->button() == Qt::LeftButton) { + mousePressed = true; + mousePoint = mouseEvent->globalPos() - this->pos(); + return true; + } + } else if (mouseEvent->type() == QEvent::MouseButtonRelease) { + mousePressed = false; + return true; + } else if (mouseEvent->type() == QEvent::MouseMove) { + if (mousePressed && (mouseEvent->buttons() && Qt::LeftButton)) { + this->move(mouseEvent->globalPos() - mousePoint); + return true; + } + } + + return QWidget::eventFilter(watched, event); +} + +void QUITipBox::initControl() +{ + this->setObjectName(QString::fromUtf8("QUITipBox")); + + verticalLayout = new QVBoxLayout(this); + verticalLayout->setSpacing(0); + verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); + verticalLayout->setContentsMargins(1, 1, 1, 1); + widgetTitle = new QWidget(this); + widgetTitle->setObjectName(QString::fromUtf8("widgetTitle")); + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(widgetTitle->sizePolicy().hasHeightForWidth()); + widgetTitle->setSizePolicy(sizePolicy); + horizontalLayout2 = new QHBoxLayout(widgetTitle); + horizontalLayout2->setSpacing(0); + horizontalLayout2->setObjectName(QString::fromUtf8("horizontalLayout2")); + horizontalLayout2->setContentsMargins(0, 0, 0, 0); + labIco = new QLabel(widgetTitle); + labIco->setObjectName(QString::fromUtf8("labIco")); + labIco->setMinimumSize(QSize(TitleMinSize, 0)); + labIco->setAlignment(Qt::AlignCenter); + horizontalLayout2->addWidget(labIco); + + labTitle = new QLabel(widgetTitle); + labTitle->setObjectName(QString::fromUtf8("labTitle")); + labTitle->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter); + horizontalLayout2->addWidget(labTitle); + + labTime = new QLabel(widgetTitle); + labTime->setObjectName(QString::fromUtf8("labTime")); + QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Preferred); + sizePolicy1.setHorizontalStretch(0); + sizePolicy1.setVerticalStretch(0); + sizePolicy1.setHeightForWidth(labTime->sizePolicy().hasHeightForWidth()); + labTime->setSizePolicy(sizePolicy1); + horizontalLayout2->addWidget(labTime); + + widgetMenu = new QWidget(widgetTitle); + widgetMenu->setObjectName(QString::fromUtf8("widgetMenu")); + QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Preferred); + sizePolicy2.setHorizontalStretch(0); + sizePolicy2.setVerticalStretch(0); + sizePolicy2.setHeightForWidth(widgetMenu->sizePolicy().hasHeightForWidth()); + widgetMenu->setSizePolicy(sizePolicy2); + horizontalLayout = new QHBoxLayout(widgetMenu); + horizontalLayout->setSpacing(0); + horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); + horizontalLayout->setContentsMargins(0, 0, 0, 0); + btnMenu_Close = new QPushButton(widgetMenu); + btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close")); + QSizePolicy sizePolicy3(QSizePolicy::Minimum, QSizePolicy::Expanding); + sizePolicy3.setHorizontalStretch(0); + sizePolicy3.setVerticalStretch(0); + sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth()); + btnMenu_Close->setSizePolicy(sizePolicy3); + btnMenu_Close->setMinimumSize(QSize(TitleMinSize, 0)); + btnMenu_Close->setMaximumSize(QSize(TitleMinSize, 16777215)); + btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor)); + btnMenu_Close->setFocusPolicy(Qt::NoFocus); + btnMenu_Close->setFlat(true); + + horizontalLayout->addWidget(btnMenu_Close); + horizontalLayout2->addWidget(widgetMenu); + verticalLayout->addWidget(widgetTitle); + + widgetMain = new QWidget(this); + widgetMain->setObjectName(QString::fromUtf8("widgetMainQUI")); + widgetMain->setAutoFillBackground(true); + verticalLayout2 = new QVBoxLayout(widgetMain); + verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout2")); + labInfo = new QLabel(widgetMain); + labInfo->setObjectName(QString::fromUtf8("labInfo")); + labInfo->setScaledContents(true); + verticalLayout2->addWidget(labInfo); + verticalLayout->addWidget(widgetMain); + + connect(btnMenu_Close, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); +} + +void QUITipBox::initForm() +{ + IconHelper::Instance()->setIcon(labIco, QUIConfig::IconMain, QUIConfig::FontSize + 2); + IconHelper::Instance()->setIcon(btnMenu_Close, QUIConfig::IconClose, QUIConfig::FontSize); + + this->setProperty("form", true); + this->widgetTitle->setProperty("form", "title"); + this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); + this->setWindowTitle(this->labTitle->text()); + +#ifdef __arm__ + this->setFixedSize(350, 180); +#else + this->setFixedSize(280, 150); +#endif + + closeSec = 0; + currentSec = 0; + + QTimer *timer = new QTimer(this); + timer->setInterval(1000); + connect(timer, SIGNAL(timeout()), this, SLOT(checkSec())); + timer->start(); + + this->installEventFilter(this); + + //字体加大 + QFont font; + font.setPixelSize(QUIConfig::FontSize + 3); + font.setBold(true); + this->labInfo->setFont(font); + + //显示和隐藏窗体动画效果 + animation = new QPropertyAnimation(this, "pos"); + animation->setDuration(500); + animation->setEasingCurve(QEasingCurve::InOutQuad); +} + +void QUITipBox::checkSec() +{ + if (closeSec == 0) { + return; + } + + if (currentSec < closeSec) { + currentSec++; + } else { + this->close(); + } + + QString str = QString("关闭倒计时 %1 s").arg(closeSec - currentSec + 1); + this->labTime->setText(str); +} + +void QUITipBox::on_btnMenu_Close_clicked() +{ + done(QMessageBox::No); + close(); +} + +void QUITipBox::setIconMain(const QChar &str, quint32 size) +{ + IconHelper::Instance()->setIcon(this->labIco, str, size); +} + +void QUITipBox::setTip(const QString &title, const QString &tip, bool fullScreen, bool center, int closeSec) +{ + this->closeSec = closeSec; + this->currentSec = 0; + this->labTime->clear(); + checkSec(); + + this->fullScreen = fullScreen; + this->labTitle->setText(title); + this->labInfo->setText(tip); + this->labInfo->setAlignment(center ? Qt::AlignCenter : Qt::AlignLeft); + this->setWindowTitle(this->labTitle->text()); + + QRect rect = fullScreen ? qApp->desktop()->availableGeometry() : qApp->desktop()->geometry(); + int width = rect.width(); + int height = rect.height(); + int x = width - this->width(); + int y = height - this->height(); + + //移到右下角 + this->move(x, y); + + //启动动画 + animation->stop(); + animation->setStartValue(QPoint(x, height)); + animation->setEndValue(QPoint(x, y)); + animation->start(); +} + +void QUITipBox::hide() +{ + QRect rect = fullScreen ? qApp->desktop()->availableGeometry() : qApp->desktop()->geometry(); + int width = rect.width(); + int height = rect.height(); + int x = width - this->width(); + int y = height - this->height(); + + //启动动画 + animation->stop(); + animation->setStartValue(QPoint(x, y)); + animation->setEndValue(QPoint(x, qApp->desktop()->geometry().height())); + animation->start(); +} + + +QScopedPointer QUIInputBox::self; +QUIInputBox *QUIInputBox::Instance() +{ + if (self.isNull()) { + static QMutex mutex; + QMutexLocker locker(&mutex); + if (self.isNull()) { + self.reset(new QUIInputBox); + } + } + + return self.data(); +} + +QUIInputBox::QUIInputBox(QWidget *parent) : QDialog(parent) +{ + this->initControl(); + this->initForm(); + QUIHelper::setFormInCenter(this); +} + +QUIInputBox::~QUIInputBox() +{ + delete widgetMain; +} + +void QUIInputBox::showEvent(QShowEvent *) +{ + txtValue->setFocus(); + this->activateWindow(); +} + +void QUIInputBox::initControl() +{ + this->setObjectName(QString::fromUtf8("QUIInputBox")); + + verticalLayout1 = new QVBoxLayout(this); + verticalLayout1->setSpacing(0); + verticalLayout1->setObjectName(QString::fromUtf8("verticalLayout1")); + verticalLayout1->setContentsMargins(1, 1, 1, 1); + widgetTitle = new QWidget(this); + widgetTitle->setObjectName(QString::fromUtf8("widgetTitle")); + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(widgetTitle->sizePolicy().hasHeightForWidth()); + widgetTitle->setSizePolicy(sizePolicy); + widgetTitle->setMinimumSize(QSize(0, TitleMinSize)); + horizontalLayout1 = new QHBoxLayout(widgetTitle); + horizontalLayout1->setSpacing(0); + horizontalLayout1->setObjectName(QString::fromUtf8("horizontalLayout1")); + horizontalLayout1->setContentsMargins(0, 0, 0, 0); + labIco = new QLabel(widgetTitle); + labIco->setObjectName(QString::fromUtf8("labIco")); + QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred); + sizePolicy1.setHorizontalStretch(0); + sizePolicy1.setVerticalStretch(0); + sizePolicy1.setHeightForWidth(labIco->sizePolicy().hasHeightForWidth()); + labIco->setSizePolicy(sizePolicy1); + labIco->setMinimumSize(QSize(TitleMinSize, 0)); + labIco->setAlignment(Qt::AlignCenter); + + horizontalLayout1->addWidget(labIco); + + labTitle = new QLabel(widgetTitle); + labTitle->setObjectName(QString::fromUtf8("labTitle")); + labTitle->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter); + + horizontalLayout1->addWidget(labTitle); + + labTime = new QLabel(widgetTitle); + labTime->setObjectName(QString::fromUtf8("labTime")); + QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Preferred); + sizePolicy2.setHorizontalStretch(0); + sizePolicy2.setVerticalStretch(0); + sizePolicy2.setHeightForWidth(labTime->sizePolicy().hasHeightForWidth()); + labTime->setSizePolicy(sizePolicy2); + labTime->setAlignment(Qt::AlignCenter); + + horizontalLayout1->addWidget(labTime); + + widgetMenu = new QWidget(widgetTitle); + widgetMenu->setObjectName(QString::fromUtf8("widgetMenu")); + sizePolicy1.setHeightForWidth(widgetMenu->sizePolicy().hasHeightForWidth()); + widgetMenu->setSizePolicy(sizePolicy1); + horizontalLayout2 = new QHBoxLayout(widgetMenu); + horizontalLayout2->setSpacing(0); + horizontalLayout2->setObjectName(QString::fromUtf8("horizontalLayout2")); + horizontalLayout2->setContentsMargins(0, 0, 0, 0); + btnMenu_Close = new QPushButton(widgetMenu); + btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close")); + QSizePolicy sizePolicy3(QSizePolicy::Minimum, QSizePolicy::Expanding); + sizePolicy3.setHorizontalStretch(0); + sizePolicy3.setVerticalStretch(0); + sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth()); + btnMenu_Close->setSizePolicy(sizePolicy3); + btnMenu_Close->setMinimumSize(QSize(TitleMinSize, 0)); + btnMenu_Close->setMaximumSize(QSize(TitleMinSize, 16777215)); + btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor)); + btnMenu_Close->setFocusPolicy(Qt::NoFocus); + btnMenu_Close->setFlat(true); + + horizontalLayout2->addWidget(btnMenu_Close); + horizontalLayout1->addWidget(widgetMenu); + verticalLayout1->addWidget(widgetTitle); + + widgetMain = new QWidget(this); + widgetMain->setObjectName(QString::fromUtf8("widgetMainQUI")); + verticalLayout2 = new QVBoxLayout(widgetMain); + verticalLayout2->setSpacing(5); + verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout2")); + verticalLayout2->setContentsMargins(5, 5, 5, 5); + frame = new QFrame(widgetMain); + frame->setObjectName(QString::fromUtf8("frame")); + frame->setFrameShape(QFrame::Box); + frame->setFrameShadow(QFrame::Sunken); + verticalLayout3 = new QVBoxLayout(frame); + verticalLayout3->setObjectName(QString::fromUtf8("verticalLayout3")); + labInfo = new QLabel(frame); + labInfo->setObjectName(QString::fromUtf8("labInfo")); + labInfo->setScaledContents(false); + labInfo->setWordWrap(true); + verticalLayout3->addWidget(labInfo); + + txtValue = new QLineEdit(frame); + txtValue->setObjectName(QString::fromUtf8("txtValue")); + verticalLayout3->addWidget(txtValue); + + cboxValue = new QComboBox(frame); + cboxValue->setObjectName(QString::fromUtf8("cboxValue")); + verticalLayout3->addWidget(cboxValue); + + lay = new QHBoxLayout(); + lay->setObjectName(QString::fromUtf8("lay")); + horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); + lay->addItem(horizontalSpacer); + + btnOk = new QPushButton(frame); + btnOk->setObjectName(QString::fromUtf8("btnOk")); + btnOk->setMinimumSize(QSize(85, 0)); + btnOk->setIcon(QIcon(":/image/btn_ok.png")); + lay->addWidget(btnOk); + + btnCancel = new QPushButton(frame); + btnCancel->setObjectName(QString::fromUtf8("btnCancel")); + btnCancel->setMinimumSize(QSize(85, 0)); + btnCancel->setIcon(QIcon(":/image/btn_close.png")); + lay->addWidget(btnCancel); + + verticalLayout3->addLayout(lay); + verticalLayout2->addWidget(frame); + verticalLayout1->addWidget(widgetMain); + + QWidget::setTabOrder(txtValue, btnOk); + QWidget::setTabOrder(btnOk, btnCancel); + + labTitle->setText("输入框"); + btnOk->setText("确定"); + btnCancel->setText("取消"); + + connect(btnOk, SIGNAL(clicked()), this, SLOT(on_btnOk_clicked())); + connect(btnCancel, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); + connect(btnMenu_Close, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); +} + +void QUIInputBox::initForm() +{ + IconHelper::Instance()->setIcon(labIco, QUIConfig::IconMain, QUIConfig::FontSize + 2); + IconHelper::Instance()->setIcon(btnMenu_Close, QUIConfig::IconClose, QUIConfig::FontSize); + + this->setProperty("form", true); + this->widgetTitle->setProperty("form", "title"); + if (TOOL) { + this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); + } else { + this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); + } + + this->setWindowTitle(this->labTitle->text()); + +#ifdef __arm__ + int width = 90; + int iconWidth = 22; + int iconHeight = 22; + this->setFixedSize(350, 180); +#else + int width = 80; + int iconWidth = 18; + int iconHeight = 18; + this->setFixedSize(280, 150); +#endif + + QList btns = this->frame->findChildren(); + foreach (QPushButton *btn, btns) { + btn->setMinimumWidth(width); + btn->setIconSize(QSize(iconWidth, iconHeight)); + } + + closeSec = 0; + currentSec = 0; + + QTimer *timer = new QTimer(this); + timer->setInterval(1000); + connect(timer, SIGNAL(timeout()), this, SLOT(checkSec())); + timer->start(); + + this->installEventFilter(this); +} + +void QUIInputBox::checkSec() +{ + if (closeSec == 0) { + return; + } + + if (currentSec < closeSec) { + currentSec++; + } else { + this->close(); + } + + QString str = QString("关闭倒计时 %1 s").arg(closeSec - currentSec + 1); + this->labTime->setText(str); +} + +void QUIInputBox::setParameter(const QString &title, int type, int closeSec, + QString placeholderText, bool pwd, + const QString &defaultValue) +{ + this->closeSec = closeSec; + this->currentSec = 0; + this->labTime->clear(); + this->labInfo->setText(title); + checkSec(); + + if (type == 0) { + this->cboxValue->setVisible(false); + this->txtValue->setPlaceholderText(placeholderText); + this->txtValue->setText(defaultValue); + + if (pwd) { + this->txtValue->setEchoMode(QLineEdit::Password); + } + } else if (type == 1) { + this->txtValue->setVisible(false); + this->cboxValue->addItems(defaultValue.split("|")); + } +} + +QString QUIInputBox::getValue() const +{ + return this->value; +} + +void QUIInputBox::closeEvent(QCloseEvent *) +{ + closeSec = 0; + currentSec = 0; +} + +bool QUIInputBox::eventFilter(QObject *watched, QEvent *event) +{ + static QPoint mousePoint; + static bool mousePressed = false; + + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->type() == QEvent::MouseButtonPress) { + if (mouseEvent->button() == Qt::LeftButton) { + mousePressed = true; + mousePoint = mouseEvent->globalPos() - this->pos(); + return true; + } + } else if (mouseEvent->type() == QEvent::MouseButtonRelease) { + mousePressed = false; + return true; + } else if (mouseEvent->type() == QEvent::MouseMove) { + if (mousePressed && (mouseEvent->buttons() && Qt::LeftButton)) { + this->move(mouseEvent->globalPos() - mousePoint); + return true; + } + } + + return QWidget::eventFilter(watched, event); +} + +void QUIInputBox::on_btnOk_clicked() +{ + if (this->txtValue->isVisible()) { + value = this->txtValue->text(); + } else if (this->cboxValue->isVisible()) { + value = this->cboxValue->currentText(); + } + + done(QMessageBox::Ok); + close(); +} + +void QUIInputBox::on_btnMenu_Close_clicked() +{ + done(QMessageBox::Cancel); + close(); +} + +void QUIInputBox::setIconMain(const QChar &str, quint32 size) +{ + IconHelper::Instance()->setIcon(this->labIco, str, size); +} + + +QScopedPointer QUIDateSelect::self; +QUIDateSelect *QUIDateSelect::Instance() +{ + if (self.isNull()) { + static QMutex mutex; + QMutexLocker locker(&mutex); + if (self.isNull()) { + self.reset(new QUIDateSelect); + } + } + + return self.data(); +} + +QUIDateSelect::QUIDateSelect(QWidget *parent) : QDialog(parent) +{ + this->initControl(); + this->initForm(); + QUIHelper::setFormInCenter(this); +} + +QUIDateSelect::~QUIDateSelect() +{ + delete widgetMain; +} + +bool QUIDateSelect::eventFilter(QObject *watched, QEvent *event) +{ + static QPoint mousePoint; + static bool mousePressed = false; + + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->type() == QEvent::MouseButtonPress) { + if (mouseEvent->button() == Qt::LeftButton) { + mousePressed = true; + mousePoint = mouseEvent->globalPos() - this->pos(); + return true; + } + } else if (mouseEvent->type() == QEvent::MouseButtonRelease) { + mousePressed = false; + return true; + } else if (mouseEvent->type() == QEvent::MouseMove) { + if (mousePressed && (mouseEvent->buttons() && Qt::LeftButton)) { + this->move(mouseEvent->globalPos() - mousePoint); + return true; + } + } + + return QWidget::eventFilter(watched, event); +} + +void QUIDateSelect::initControl() +{ + this->setObjectName(QString::fromUtf8("QUIDateSelect")); + + verticalLayout = new QVBoxLayout(this); + verticalLayout->setSpacing(0); + verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); + verticalLayout->setContentsMargins(1, 1, 1, 1); + widgetTitle = new QWidget(this); + widgetTitle->setObjectName(QString::fromUtf8("widgetTitle")); + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(widgetTitle->sizePolicy().hasHeightForWidth()); + widgetTitle->setSizePolicy(sizePolicy); + widgetTitle->setMinimumSize(QSize(0, TitleMinSize)); + horizontalLayout1 = new QHBoxLayout(widgetTitle); + horizontalLayout1->setSpacing(0); + horizontalLayout1->setObjectName(QString::fromUtf8("horizontalLayout1")); + horizontalLayout1->setContentsMargins(0, 0, 0, 0); + labIco = new QLabel(widgetTitle); + labIco->setObjectName(QString::fromUtf8("labIco")); + QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred); + sizePolicy1.setHorizontalStretch(0); + sizePolicy1.setVerticalStretch(0); + sizePolicy1.setHeightForWidth(labIco->sizePolicy().hasHeightForWidth()); + labIco->setSizePolicy(sizePolicy1); + labIco->setMinimumSize(QSize(TitleMinSize, 0)); + labIco->setAlignment(Qt::AlignCenter); + horizontalLayout1->addWidget(labIco); + + labTitle = new QLabel(widgetTitle); + labTitle->setObjectName(QString::fromUtf8("labTitle")); + QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Preferred); + sizePolicy2.setHorizontalStretch(0); + sizePolicy2.setVerticalStretch(0); + sizePolicy2.setHeightForWidth(labTitle->sizePolicy().hasHeightForWidth()); + labTitle->setSizePolicy(sizePolicy2); + labTitle->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter); + horizontalLayout1->addWidget(labTitle); + + widgetMenu = new QWidget(widgetTitle); + widgetMenu->setObjectName(QString::fromUtf8("widgetMenu")); + sizePolicy1.setHeightForWidth(widgetMenu->sizePolicy().hasHeightForWidth()); + widgetMenu->setSizePolicy(sizePolicy1); + horizontalLayout = new QHBoxLayout(widgetMenu); + horizontalLayout->setSpacing(0); + horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); + horizontalLayout->setContentsMargins(0, 0, 0, 0); + btnMenu_Close = new QPushButton(widgetMenu); + btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close")); + QSizePolicy sizePolicy3(QSizePolicy::Minimum, QSizePolicy::Expanding); + sizePolicy3.setHorizontalStretch(0); + sizePolicy3.setVerticalStretch(0); + sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth()); + btnMenu_Close->setSizePolicy(sizePolicy3); + btnMenu_Close->setMinimumSize(QSize(TitleMinSize, 0)); + btnMenu_Close->setMaximumSize(QSize(TitleMinSize, 16777215)); + btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor)); + btnMenu_Close->setFocusPolicy(Qt::NoFocus); + btnMenu_Close->setFlat(true); + + horizontalLayout->addWidget(btnMenu_Close); + horizontalLayout1->addWidget(widgetMenu); + verticalLayout->addWidget(widgetTitle); + + widgetMain = new QWidget(this); + widgetMain->setObjectName(QString::fromUtf8("widgetMainQUI")); + verticalLayout1 = new QVBoxLayout(widgetMain); + verticalLayout1->setSpacing(6); + verticalLayout1->setObjectName(QString::fromUtf8("verticalLayout1")); + verticalLayout1->setContentsMargins(6, 6, 6, 6); + frame = new QFrame(widgetMain); + frame->setObjectName(QString::fromUtf8("frame")); + frame->setFrameShape(QFrame::Box); + frame->setFrameShadow(QFrame::Sunken); + gridLayout = new QGridLayout(frame); + gridLayout->setObjectName(QString::fromUtf8("gridLayout")); + labStart = new QLabel(frame); + labStart->setObjectName(QString::fromUtf8("labStart")); + labStart->setFocusPolicy(Qt::TabFocus); + gridLayout->addWidget(labStart, 0, 0, 1, 1); + + btnOk = new QPushButton(frame); + btnOk->setObjectName(QString::fromUtf8("btnOk")); + btnOk->setMinimumSize(QSize(85, 0)); + btnOk->setCursor(QCursor(Qt::PointingHandCursor)); + btnOk->setFocusPolicy(Qt::StrongFocus); + btnOk->setIcon(QIcon(":/image/btn_ok.png")); + gridLayout->addWidget(btnOk, 0, 2, 1, 1); + + labEnd = new QLabel(frame); + labEnd->setObjectName(QString::fromUtf8("labEnd")); + labEnd->setFocusPolicy(Qt::TabFocus); + gridLayout->addWidget(labEnd, 1, 0, 1, 1); + + btnClose = new QPushButton(frame); + btnClose->setObjectName(QString::fromUtf8("btnClose")); + btnClose->setMinimumSize(QSize(85, 0)); + btnClose->setCursor(QCursor(Qt::PointingHandCursor)); + btnClose->setFocusPolicy(Qt::StrongFocus); + btnClose->setIcon(QIcon(":/image/btn_close.png")); + gridLayout->addWidget(btnClose, 1, 2, 1, 1); + + dateStart = new QDateTimeEdit(frame); + dateStart->setObjectName(QString::fromUtf8("dateStart")); + QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Fixed); + sizePolicy4.setHorizontalStretch(0); + sizePolicy4.setVerticalStretch(0); + sizePolicy4.setHeightForWidth(dateStart->sizePolicy().hasHeightForWidth()); + dateStart->setSizePolicy(sizePolicy4); + dateStart->setCalendarPopup(true); + gridLayout->addWidget(dateStart, 0, 1, 1, 1); + + dateEnd = new QDateTimeEdit(frame); + dateEnd->setObjectName(QString::fromUtf8("dateEnd")); + sizePolicy4.setHeightForWidth(dateEnd->sizePolicy().hasHeightForWidth()); + dateEnd->setSizePolicy(sizePolicy4); + dateEnd->setCalendarPopup(true); + + gridLayout->addWidget(dateEnd, 1, 1, 1, 1); + verticalLayout1->addWidget(frame); + verticalLayout->addWidget(widgetMain); + + QWidget::setTabOrder(labStart, labEnd); + QWidget::setTabOrder(labEnd, dateStart); + QWidget::setTabOrder(dateStart, dateEnd); + QWidget::setTabOrder(dateEnd, btnOk); + QWidget::setTabOrder(btnOk, btnClose); + + labTitle->setText("日期时间选择"); + labStart->setText("开始时间"); + labEnd->setText("结束时间"); + btnOk->setText("确定"); + btnClose->setText("关闭"); + + dateStart->setDate(QDate::currentDate()); + dateEnd->setDate(QDate::currentDate().addDays(1)); + + dateStart->calendarWidget()->setGridVisible(true); + dateEnd->calendarWidget()->setGridVisible(true); + dateStart->calendarWidget()->setLocale(QLocale::Chinese); + dateEnd->calendarWidget()->setLocale(QLocale::Chinese); + setFormat("yyyy-MM-dd"); + + connect(btnOk, SIGNAL(clicked()), this, SLOT(on_btnOk_clicked())); + connect(btnClose, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); + connect(btnMenu_Close, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked())); +} + +void QUIDateSelect::initForm() +{ + IconHelper::Instance()->setIcon(labIco, QUIConfig::IconMain, QUIConfig::FontSize + 2); + IconHelper::Instance()->setIcon(btnMenu_Close, QUIConfig::IconClose, QUIConfig::FontSize); + + this->setProperty("form", true); + this->widgetTitle->setProperty("form", "title"); + if (TOOL) { + this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); + } else { + this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); + } + + this->setWindowTitle(this->labTitle->text()); + +#ifdef __arm__ + int width = 90; + int iconWidth = 22; + int iconHeight = 22; + this->setFixedSize(370, 160); +#else + int width = 80; + int iconWidth = 18; + int iconHeight = 18; + this->setFixedSize(320, 130); +#endif + + QList btns = this->frame->findChildren(); + foreach (QPushButton *btn, btns) { + btn->setMinimumWidth(width); + btn->setIconSize(QSize(iconWidth, iconHeight)); + } + + this->installEventFilter(this); +} + +void QUIDateSelect::on_btnOk_clicked() +{ + if (dateStart->dateTime() > dateEnd->dateTime()) { + QUIHelper::showMessageBoxError("开始时间不能大于结束时间!", 3); + return; + } + + startDateTime = dateStart->dateTime().toString(format); + endDateTime = dateEnd->dateTime().toString(format); + + done(QMessageBox::Ok); + close(); +} + +void QUIDateSelect::on_btnMenu_Close_clicked() +{ + done(QMessageBox::Cancel); + close(); +} + +QString QUIDateSelect::getStartDateTime() const +{ + return this->startDateTime; +} + +QString QUIDateSelect::getEndDateTime() const +{ + return this->endDateTime; +} + +void QUIDateSelect::setIconMain(const QChar &str, quint32 size) +{ + IconHelper::Instance()->setIcon(this->labIco, str, size); +} + +void QUIDateSelect::setFormat(const QString &format) +{ + this->format = format; + this->dateStart->setDisplayFormat(format); + this->dateEnd->setDisplayFormat(format); +} + + +QScopedPointer IconHelper::self; +IconHelper *IconHelper::Instance() +{ + if (self.isNull()) { + static QMutex mutex; + QMutexLocker locker(&mutex); + if (self.isNull()) { + self.reset(new IconHelper); + } + } + + return self.data(); +} + +IconHelper::IconHelper(QObject *parent) : QObject(parent) +{ + //判断图形字体是否存在,不存在则加入 + QFontDatabase fontDb; + if (!fontDb.families().contains("FontAwesome")) { + int fontId = fontDb.addApplicationFont(":/image/fontawesome-webfont.ttf"); + QStringList fontName = fontDb.applicationFontFamilies(fontId); + if (fontName.count() == 0) { + qDebug() << "load fontawesome-webfont.ttf error"; + } + } + + if (fontDb.families().contains("FontAwesome")) { + iconFont = QFont("FontAwesome"); +#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0)) + iconFont.setHintingPreference(QFont::PreferNoHinting); +#endif + } +} + +QFont IconHelper::getIconFont() +{ + return this->iconFont; +} + +void IconHelper::setIcon(QLabel *lab, const QChar &str, quint32 size) +{ + iconFont.setPixelSize(size); + lab->setFont(iconFont); + lab->setText(str); +} + +void IconHelper::setIcon(QAbstractButton *btn, const QChar &str, quint32 size) +{ + iconFont.setPixelSize(size); + btn->setFont(iconFont); + btn->setText(str); +} + +QPixmap IconHelper::getPixmap(const QColor &color, const QChar &str, quint32 size, + quint32 pixWidth, quint32 pixHeight, int flags) +{ + QPixmap pix(pixWidth, pixHeight); + pix.fill(Qt::transparent); + + QPainter painter; + painter.begin(&pix); + painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); + painter.setPen(color); + + iconFont.setPixelSize(size); + painter.setFont(iconFont); + painter.drawText(pix.rect(), flags, str); + painter.end(); + + return pix; +} + +QPixmap IconHelper::getPixmap(QToolButton *btn, bool normal) +{ + QPixmap pix; + int index = btns.indexOf(btn); + if (index >= 0) { + if (normal) { + pix = pixNormal.at(index); + } else { + pix = pixDark.at(index); + } + } + + return pix; +} + +QPixmap IconHelper::getPixmap(QToolButton *btn, int type) +{ + QPixmap pix; + int index = btns.indexOf(btn); + if (index >= 0) { + if (type == 0) { + pix = pixNormal.at(index); + } else if (type == 1) { + pix = pixHover.at(index); + } else if (type == 2) { + pix = pixPressed.at(index); + } else if (type == 3) { + pix = pixChecked.at(index); + } + } + + return pix; +} + +void IconHelper::setStyle(QFrame *frame, QList btns, QList pixChar, + quint32 iconSize, quint32 iconWidth, quint32 iconHeight, + const QString &normalBgColor, const QString &darkBgColor, + const QString &normalTextColor, const QString &darkTextColor) +{ + int btnCount = btns.count(); + int charCount = pixChar.count(); + if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) { + return; + } + + QStringList qss; + qss.append(QString("QFrame>QToolButton{border-style:none;border-width:0px;" + "background-color:%1;color:%2;}").arg(normalBgColor).arg(normalTextColor)); + qss.append(QString("QFrame>QToolButton:hover,QFrame>QToolButton:pressed,QFrame>QToolButton:checked" + "{background-color:%1;color:%2;}").arg(darkBgColor).arg(darkTextColor)); + + frame->setStyleSheet(qss.join("")); + + //存储对应按钮对象,方便鼠标移上去的时候切换图片 + for (int i = 0; i < btnCount; i++) { + QChar c = QChar(pixChar.at(i)); + QPixmap pixNormal = getPixmap(normalTextColor, c, iconSize, iconWidth, iconHeight); + QPixmap pixDark = getPixmap(darkTextColor, c, iconSize, iconWidth, iconHeight); + + QToolButton *btn = btns.at(i); + btn->setIcon(QIcon(pixNormal)); + btn->setIconSize(QSize(iconWidth, iconHeight)); + btn->installEventFilter(this); + + this->btns.append(btn); + this->pixNormal.append(pixNormal); + this->pixDark.append(pixDark); + this->pixHover.append(pixDark); + this->pixPressed.append(pixDark); + this->pixChecked.append(pixDark); + } +} + +void IconHelper::setStyle(QWidget *widget, const QString &type, int borderWidth, const QString &borderColor, + const QString &normalBgColor, const QString &darkBgColor, + const QString &normalTextColor, const QString &darkTextColor) +{ + QString strBorder; + if (type == "top") { + strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "right") { + strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "bottom") { + strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "left") { + strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } + + QStringList qss; + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;" + "color:%2;background:%3;}").arg(type).arg(normalTextColor).arg(normalBgColor)); + + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover," + "QWidget[flag=\"%1\"] QAbstractButton:pressed," + "QWidget[flag=\"%1\"] QAbstractButton:checked{" + "border-style:solid;%2border-color:%3;color:%4;background:%5;}") + .arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor)); + + widget->setStyleSheet(qss.join("")); +} + +void IconHelper::removeStyle(QList btns) +{ + for (int i = 0; i < btns.count(); i++) { + for (int j = 0; j < this->btns.count(); j++) { + if (this->btns.at(j) == btns.at(i)) { + this->btns.at(j)->removeEventFilter(this); + this->btns.removeAt(j); + this->pixNormal.removeAt(j); + this->pixDark.removeAt(j); + this->pixHover.removeAt(j); + this->pixPressed.removeAt(j); + this->pixChecked.removeAt(j); + break; + } + } + } +} + +void IconHelper::setStyle(QWidget *widget, QList btns, QList pixChar, + quint32 iconSize, quint32 iconWidth, quint32 iconHeight, + const QString &type, int borderWidth, const QString &borderColor, + const QString &normalBgColor, const QString &darkBgColor, + const QString &normalTextColor, const QString &darkTextColor) +{ + int btnCount = btns.count(); + int charCount = pixChar.count(); + if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) { + return; + } + + QString strBorder; + if (type == "top") { + strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "right") { + strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "bottom") { + strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "left") { + strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } + + //如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色 + QStringList qss; + if (btns.at(0)->toolButtonStyle() == Qt::ToolButtonTextBesideIcon) { + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}") + .arg(type).arg(strBorder).arg(normalBgColor).arg(normalTextColor).arg(normalBgColor)); + } else { + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}") + .arg(type).arg(normalTextColor).arg(normalBgColor)); + } + + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover," + "QWidget[flag=\"%1\"] QAbstractButton:pressed," + "QWidget[flag=\"%1\"] QAbstractButton:checked{" + "border-style:solid;%2border-color:%3;color:%4;background:%5;}") + .arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor)); + + qss.append(QString("QWidget#%1{background:%2;}").arg(widget->objectName()).arg(normalBgColor)); + qss.append(QString("QWidget>QToolButton{border-width:0px;" + "background-color:%1;color:%2;}").arg(normalBgColor).arg(normalTextColor)); + qss.append(QString("QWidget>QToolButton:hover,QWidget>QToolButton:pressed,QWidget>QToolButton:checked{" + "background-color:%1;color:%2;}").arg(darkBgColor).arg(darkTextColor)); + + widget->setStyleSheet(qss.join("")); + + //存储对应按钮对象,方便鼠标移上去的时候切换图片 + for (int i = 0; i < btnCount; i++) { + QChar c = QChar(pixChar.at(i)); + QPixmap pixNormal = getPixmap(normalTextColor, c, iconSize, iconWidth, iconHeight); + QPixmap pixDark = getPixmap(darkTextColor, c, iconSize, iconWidth, iconHeight); + + QToolButton *btn = btns.at(i); + btn->setIcon(QIcon(pixNormal)); + btn->setIconSize(QSize(iconWidth, iconHeight)); + btn->installEventFilter(this); + + this->btns.append(btn); + this->pixNormal.append(pixNormal); + this->pixDark.append(pixDark); + this->pixHover.append(pixDark); + this->pixPressed.append(pixDark); + this->pixChecked.append(pixDark); + } +} + +void IconHelper::setStyle(QWidget *widget, QList btns, QList pixChar, const IconHelper::StyleColor &styleColor) +{ + int btnCount = btns.count(); + int charCount = pixChar.count(); + if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) { + return; + } + + quint32 iconSize = styleColor.iconSize; + quint32 iconWidth = styleColor.iconWidth; + quint32 iconHeight = styleColor.iconHeight; + quint32 borderWidth = styleColor.borderWidth; + QString type = styleColor.type; + + QString strBorder; + if (type == "top") { + strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "right") { + strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "bottom") { + strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } else if (type == "left") { + strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;") + .arg(borderWidth).arg(borderWidth * 2); + } + + //如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色 + QStringList qss; + if (btns.at(0)->toolButtonStyle() == Qt::ToolButtonTextBesideIcon) { + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}") + .arg(type).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor)); + } else { + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}") + .arg(type).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor)); + } + + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}") + .arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.hoverTextColor).arg(styleColor.hoverBgColor)); + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:pressed{border-style:solid;%2border-color:%3;color:%4;background:%5;}") + .arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.pressedTextColor).arg(styleColor.pressedBgColor)); + qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:checked{border-style:solid;%2border-color:%3;color:%4;background:%5;}") + .arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.checkedTextColor).arg(styleColor.checkedBgColor)); + + qss.append(QString("QWidget#%1{background:%2;}").arg(widget->objectName()).arg(styleColor.normalBgColor)); + qss.append(QString("QWidget>QToolButton{border-width:0px;background-color:%1;color:%2;}").arg(styleColor.normalBgColor).arg(styleColor.normalTextColor)); + qss.append(QString("QWidget>QToolButton:hover{background-color:%1;color:%2;}").arg(styleColor.hoverBgColor).arg(styleColor.hoverTextColor)); + qss.append(QString("QWidget>QToolButton:pressed{background-color:%1;color:%2;}").arg(styleColor.pressedBgColor).arg(styleColor.pressedTextColor)); + qss.append(QString("QWidget>QToolButton:checked{background-color:%1;color:%2;}").arg(styleColor.checkedBgColor).arg(styleColor.checkedTextColor)); + + widget->setStyleSheet(qss.join("")); + + //存储对应按钮对象,方便鼠标移上去的时候切换图片 + for (int i = 0; i < btnCount; i++) { + QChar c = QChar(pixChar.at(i)); + QPixmap pixNormal = getPixmap(styleColor.normalTextColor, c, iconSize, iconWidth, iconHeight); + QPixmap pixHover = getPixmap(styleColor.hoverTextColor, c, iconSize, iconWidth, iconHeight); + QPixmap pixPressed = getPixmap(styleColor.pressedTextColor, c, iconSize, iconWidth, iconHeight); + QPixmap pixChecked = getPixmap(styleColor.checkedTextColor, c, iconSize, iconWidth, iconHeight); + + QToolButton *btn = btns.at(i); + btn->setIcon(QIcon(pixNormal)); + btn->setIconSize(QSize(iconWidth, iconHeight)); + btn->installEventFilter(this); + + this->btns.append(btn); + this->pixNormal.append(pixNormal); + this->pixDark.append(pixHover); + this->pixHover.append(pixHover); + this->pixPressed.append(pixPressed); + this->pixChecked.append(pixChecked); + } +} + +bool IconHelper::eventFilter(QObject *watched, QEvent *event) +{ + if (watched->inherits("QToolButton")) { + QToolButton *btn = (QToolButton *)watched; + int index = btns.indexOf(btn); + if (index >= 0) { + if (event->type() == QEvent::Enter) { + btn->setIcon(QIcon(pixHover.at(index))); + } else if (event->type() == QEvent::MouseButtonPress) { + btn->setIcon(QIcon(pixPressed.at(index))); + } else if (event->type() == QEvent::Leave) { + if (btn->isChecked()) { + btn->setIcon(QIcon(pixChecked.at(index))); + } else { + btn->setIcon(QIcon(pixNormal.at(index))); + } + } + } + } + + return QObject::eventFilter(watched, event); +} + + +QScopedPointer TrayIcon::self; +TrayIcon *TrayIcon::Instance() +{ + if (self.isNull()) { + static QMutex mutex; + QMutexLocker locker(&mutex); + if (self.isNull()) { + self.reset(new TrayIcon); + } + } + + return self.data(); +} + +TrayIcon::TrayIcon(QObject *parent) : QObject(parent) +{ + mainWidget = 0; + trayIcon = new QSystemTrayIcon(this); + connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason))); + menu = new QMenu(QApplication::desktop()); + exitDirect = true; +} + +void TrayIcon::iconIsActived(QSystemTrayIcon::ActivationReason reason) +{ + switch (reason) { + case QSystemTrayIcon::Trigger: + case QSystemTrayIcon::DoubleClick: { + mainWidget->showNormal(); + break; + } + + default: + break; + } +} + +void TrayIcon::setExitDirect(bool exitDirect) +{ + if (this->exitDirect != exitDirect) { + this->exitDirect = exitDirect; + } +} + +void TrayIcon::setMainWidget(QWidget *mainWidget) +{ + this->mainWidget = mainWidget; + menu->addAction("主界面", mainWidget, SLOT(showNormal())); + + if (exitDirect) { + menu->addAction("退出", this, SLOT(closeAll())); + } else { + menu->addAction("退出", this, SIGNAL(trayIconExit())); + } + + trayIcon->setContextMenu(menu); +} + +void TrayIcon::showMessage(const QString &title, const QString &msg, QSystemTrayIcon::MessageIcon icon, int msecs) +{ + trayIcon->showMessage(title, msg, icon, msecs); +} + +void TrayIcon::setIcon(const QString &strIcon) +{ + trayIcon->setIcon(QIcon(strIcon)); +} + +void TrayIcon::setToolTip(const QString &tip) +{ + trayIcon->setToolTip(tip); +} + +void TrayIcon::setVisible(bool visible) +{ + trayIcon->setVisible(visible); +} + +void TrayIcon::closeAll() +{ + trayIcon->hide(); + trayIcon->deleteLater(); + exit(0); +} + + +int QUIHelper::deskWidth() +{ + //没有必要每次都获取,只有当变量为空时才去获取一次 + static int width = 0; + if (width == 0) { + width = qApp->desktop()->availableGeometry().width(); + } + + return width; +} + +int QUIHelper::deskHeight() +{ + //没有必要每次都获取,只有当变量为空时才去获取一次 + static int height = 0; + if (height == 0) { + height = qApp->desktop()->availableGeometry().height(); + } + + return height; +} + +QString QUIHelper::appName() +{ + //没有必要每次都获取,只有当变量为空时才去获取一次 + static QString name; + if (name.isEmpty()) { + name = qApp->applicationFilePath(); + QStringList list = name.split("/"); + name = list.at(list.count() - 1).split(".").at(0); + } + + return name; +} + +QString QUIHelper::appPath() +{ +#ifdef Q_OS_ANDROID + return QString("/sdcard/Android/%1").arg(appName()); +#else + return qApp->applicationDirPath(); +#endif +} + +void QUIHelper::initRand() +{ + //初始化随机数种子 + QTime t = QTime::currentTime(); + qsrand(t.msec() + t.second() * 1000); +} + +void QUIHelper::initDb(const QString &dbName) +{ + initFile(QString(":/%1.db").arg(appName()), dbName); +} + +void QUIHelper::initFile(const QString &sourceName, const QString &targetName) +{ + //判断文件是否存在,不存在则从资源文件复制出来 + QFile file(targetName); + if (!file.exists() || file.size() == 0) { + file.remove(); + QUIHelper::copyFile(sourceName, targetName); + } +} + +void QUIHelper::newDir(const QString &dirName) +{ + QString strDir = dirName; + + //如果路径中包含斜杠字符则说明是绝对路径 + //linux系统路径字符带有 / windows系统 路径字符带有 :/ + if (!strDir.startsWith("/") && !strDir.contains(":/")) { + strDir = QString("%1/%2").arg(QUIHelper::appPath()).arg(strDir); + } + + QDir dir(strDir); + if (!dir.exists()) { + dir.mkpath(strDir); + } +} + +void QUIHelper::writeInfo(const QString &info, const QString &filePath) +{ + QString fileName = QString("%1/%2/%3_runinfo_%4.txt").arg(QUIHelper::appPath()) + .arg(filePath).arg(QUIHelper::appName()).arg(QDate::currentDate().toString("yyyyMM")); + + QFile file(fileName); + file.open(QIODevice::WriteOnly | QIODevice::Append | QFile::Text); + QTextStream stream(&file); + stream << DATETIME << " " << info << NEWLINE; + file.close(); +} + +void QUIHelper::writeError(const QString &info, const QString &filePath) +{ + //正式运行屏蔽掉输出错误信息,调试阶段才需要 + return; + QString fileName = QString("%1/%2/%3_runerror_%4.txt").arg(QUIHelper::appPath()) + .arg(filePath).arg(QUIHelper::appName()).arg(QDate::currentDate().toString("yyyyMM")); + + QFile file(fileName); + file.open(QIODevice::WriteOnly | QIODevice::Append | QFile::Text); + QTextStream stream(&file); + stream << DATETIME << " " << info << NEWLINE; + file.close(); +} + +void QUIHelper::setStyle(QUIWidget::Style style) +{ + QString qssFile = ":/qss/lightblue.css"; + + if (style == QUIWidget::Style_Silvery) { + qssFile = ":/qss/silvery.css"; + } else if (style == QUIWidget::Style_Blue) { + qssFile = ":/qss/blue.css"; + } else if (style == QUIWidget::Style_LightBlue) { + qssFile = ":/qss/lightblue.css"; + } else if (style == QUIWidget::Style_DarkBlue) { + qssFile = ":/qss/darkblue.css"; + } else if (style == QUIWidget::Style_Gray) { + qssFile = ":/qss/gray.css"; + } else if (style == QUIWidget::Style_LightGray) { + qssFile = ":/qss/lightgray.css"; + } else if (style == QUIWidget::Style_DarkGray) { + qssFile = ":/qss/darkgray.css"; + } else if (style == QUIWidget::Style_Black) { + qssFile = ":/qss/black.css"; + } else if (style == QUIWidget::Style_LightBlack) { + qssFile = ":/qss/lightblack.css"; + } else if (style == QUIWidget::Style_DarkBlack) { + qssFile = ":/qss/darkblack.css"; + } else if (style == QUIWidget::Style_PSBlack) { + qssFile = ":/qss/psblack.css"; + } else if (style == QUIWidget::Style_FlatBlack) { + qssFile = ":/qss/flatblack.css"; + } else if (style == QUIWidget::Style_FlatWhite) { + qssFile = ":/qss/flatwhite.css"; + } else if (style == QUIWidget::Style_Purple) { + qssFile = ":/qss/purple.css"; + } else if (style == QUIWidget::Style_BlackBlue) { + qssFile = ":/qss/blackblue.css"; + } else if (style == QUIWidget::Style_BlackVideo) { + qssFile = ":/qss/blackvideo.css"; + } + + QFile file(qssFile); + + if (file.open(QFile::ReadOnly)) { + QString qss = QLatin1String(file.readAll()); + QString paletteColor = qss.mid(20, 7); + getQssColor(qss, QUIConfig::TextColor, QUIConfig::PanelColor, QUIConfig::BorderColor, QUIConfig::NormalColorStart, + QUIConfig::NormalColorEnd, QUIConfig::DarkColorStart, QUIConfig::DarkColorEnd, QUIConfig::HighColor); + + qApp->setPalette(QPalette(QColor(paletteColor))); + qApp->setStyleSheet(qss); + file.close(); + } +} + +void QUIHelper::setStyle(const QString &qssFile, QString &paletteColor, QString &textColor) +{ + QFile file(qssFile); + if (file.open(QFile::ReadOnly)) { + QString qss = QLatin1String(file.readAll()); + paletteColor = qss.mid(20, 7); + textColor = qss.mid(49, 7); + getQssColor(qss, QUIConfig::TextColor, QUIConfig::PanelColor, QUIConfig::BorderColor, QUIConfig::NormalColorStart, + QUIConfig::NormalColorEnd, QUIConfig::DarkColorStart, QUIConfig::DarkColorEnd, QUIConfig::HighColor); + + qApp->setPalette(QPalette(QColor(paletteColor))); + qApp->setStyleSheet(qss); + file.close(); + } +} + +void QUIHelper::setStyle(const QString &qssFile, QString &textColor, QString &panelColor, QString &borderColor, + QString &normalColorStart, QString &normalColorEnd, + QString &darkColorStart, QString &darkColorEnd, QString &highColor) +{ + QFile file(qssFile); + if (file.open(QFile::ReadOnly)) { + QString qss = QLatin1String(file.readAll()); + getQssColor(qss, textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor); + qApp->setPalette(QPalette(QColor(panelColor))); + qApp->setStyleSheet(qss); + file.close(); + } +} + +void QUIHelper::getQssColor(const QString &qss, QString &textColor, QString &panelColor, QString &borderColor, + QString &normalColorStart, QString &normalColorEnd, + QString &darkColorStart, QString &darkColorEnd, QString &highColor) +{ + QString str = qss; + + QString flagTextColor = "TextColor:"; + int indexTextColor = str.indexOf(flagTextColor); + if (indexTextColor >= 0) { + textColor = str.mid(indexTextColor + flagTextColor.length(), 7); + } + + QString flagPanelColor = "PanelColor:"; + int indexPanelColor = str.indexOf(flagPanelColor); + if (indexPanelColor >= 0) { + panelColor = str.mid(indexPanelColor + flagPanelColor.length(), 7); + } + + QString flagBorderColor = "BorderColor:"; + int indexBorderColor = str.indexOf(flagBorderColor); + if (indexBorderColor >= 0) { + borderColor = str.mid(indexBorderColor + flagBorderColor.length(), 7); + } + + QString flagNormalColorStart = "NormalColorStart:"; + int indexNormalColorStart = str.indexOf(flagNormalColorStart); + if (indexNormalColorStart >= 0) { + normalColorStart = str.mid(indexNormalColorStart + flagNormalColorStart.length(), 7); + } + + QString flagNormalColorEnd = "NormalColorEnd:"; + int indexNormalColorEnd = str.indexOf(flagNormalColorEnd); + if (indexNormalColorEnd >= 0) { + normalColorEnd = str.mid(indexNormalColorEnd + flagNormalColorEnd.length(), 7); + } + + QString flagDarkColorStart = "DarkColorStart:"; + int indexDarkColorStart = str.indexOf(flagDarkColorStart); + if (indexDarkColorStart >= 0) { + darkColorStart = str.mid(indexDarkColorStart + flagDarkColorStart.length(), 7); + } + + QString flagDarkColorEnd = "DarkColorEnd:"; + int indexDarkColorEnd = str.indexOf(flagDarkColorEnd); + if (indexDarkColorEnd >= 0) { + darkColorEnd = str.mid(indexDarkColorEnd + flagDarkColorEnd.length(), 7); + } + + QString flagHighColor = "HighColor:"; + int indexHighColor = str.indexOf(flagHighColor); + if (indexHighColor >= 0) { + highColor = str.mid(indexHighColor + flagHighColor.length(), 7); + } +} + +QPixmap QUIHelper::ninePatch(const QString &picName, int horzSplit, int vertSplit, int dstWidth, int dstHeight) +{ + QPixmap pix(picName); + return ninePatch(pix, horzSplit, vertSplit, dstWidth, dstHeight); +} + +QPixmap QUIHelper::ninePatch(const QPixmap &pix, int horzSplit, int vertSplit, int dstWidth, int dstHeight) +{ + int pixWidth = pix.width(); + int pixHeight = pix.height(); + + QPixmap pix1 = pix.copy(0, 0, horzSplit, vertSplit); + QPixmap pix2 = pix.copy(horzSplit, 0, pixWidth - horzSplit * 2, vertSplit); + QPixmap pix3 = pix.copy(pixWidth - horzSplit, 0, horzSplit, vertSplit); + + QPixmap pix4 = pix.copy(0, vertSplit, horzSplit, pixHeight - vertSplit * 2); + QPixmap pix5 = pix.copy(horzSplit, vertSplit, pixWidth - horzSplit * 2, pixHeight - vertSplit * 2); + QPixmap pix6 = pix.copy(pixWidth - horzSplit, vertSplit, horzSplit, pixHeight - vertSplit * 2); + + QPixmap pix7 = pix.copy(0, pixHeight - vertSplit, horzSplit, vertSplit); + QPixmap pix8 = pix.copy(horzSplit, pixHeight - vertSplit, pixWidth - horzSplit * 2, pixWidth - horzSplit * 2); + QPixmap pix9 = pix.copy(pixWidth - horzSplit, pixHeight - vertSplit, horzSplit, vertSplit); + + //保持高度拉宽 + pix2 = pix2.scaled(dstWidth - horzSplit * 2, vertSplit, Qt::IgnoreAspectRatio); + //保持宽度拉高 + pix4 = pix4.scaled(horzSplit, dstHeight - vertSplit * 2, Qt::IgnoreAspectRatio); + //宽高都缩放 + pix5 = pix5.scaled(dstWidth - horzSplit * 2, dstHeight - vertSplit * 2, Qt::IgnoreAspectRatio); + //保持宽度拉高 + pix6 = pix6.scaled(horzSplit, dstHeight - vertSplit * 2, Qt::IgnoreAspectRatio); + //保持高度拉宽 + pix8 = pix8.scaled(dstWidth - horzSplit * 2, vertSplit); + + //生成宽高图片并填充透明背景颜色 + QPixmap resultImg(dstWidth, dstHeight); + resultImg.fill(Qt::transparent); + + QPainter painter; + painter.begin(&resultImg); + + if (!resultImg.isNull()) { + painter.drawPixmap(0, 0, pix1); + painter.drawPixmap(horzSplit, 0, pix2); + painter.drawPixmap(dstWidth - horzSplit, 0, pix3); + + painter.drawPixmap(0, vertSplit, pix4); + painter.drawPixmap(horzSplit, vertSplit, pix5); + painter.drawPixmap(dstWidth - horzSplit, vertSplit, pix6); + + painter.drawPixmap(0, dstHeight - vertSplit, pix7); + painter.drawPixmap(horzSplit, dstHeight - vertSplit, pix8); + painter.drawPixmap(dstWidth - horzSplit, dstHeight - vertSplit, pix9); + } + + painter.end(); + + return resultImg; +} + +void QUIHelper::setLabStyle(QLabel *lab, quint8 type) +{ + QString qssDisable = QString("QLabel::disabled{background:none;color:%1;}").arg(QUIConfig::BorderColor); + QString qssRed = "QLabel{border:none;background-color:rgb(214,64,48);color:rgb(255,255,255);}" + qssDisable; + QString qssGreen = "QLabel{border:none;background-color:rgb(46,138,87);color:rgb(255,255,255);}" + qssDisable; + QString qssBlue = "QLabel{border:none;background-color:rgb(67,122,203);color:rgb(255,255,255);}" + qssDisable; + QString qssDark = "QLabel{border:none;background-color:rgb(75,75,75);color:rgb(255,255,255);}" + qssDisable; + + if (type == 0) { + lab->setStyleSheet(qssRed); + } else if (type == 1) { + lab->setStyleSheet(qssGreen); + } else if (type == 2) { + lab->setStyleSheet(qssBlue); + } else if (type == 3) { + lab->setStyleSheet(qssDark); + } +} + +void QUIHelper::setFormInCenter(QWidget *frm) +{ + int frmX = frm->width(); + int frmY = frm->height(); + QDesktopWidget w; + int deskWidth = w.availableGeometry().width(); + int deskHeight = w.availableGeometry().height(); + QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2); + frm->move(movePoint); +} + +void QUIHelper::setTranslator(const QString &qmFile) +{ + QTranslator *translator = new QTranslator(qApp); + translator->load(qmFile); + qApp->installTranslator(translator); +} + +void QUIHelper::setCode() +{ +#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) +#if _MSC_VER + QTextCodec *codec = QTextCodec::codecForName("gbk"); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); +#endif + QTextCodec::setCodecForLocale(codec); + QTextCodec::setCodecForCStrings(codec); + QTextCodec::setCodecForTr(codec); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); + QTextCodec::setCodecForLocale(codec); +#endif +} + +void QUIHelper::sleep(int msec) +{ + QTime dieTime = QTime::currentTime().addMSecs(msec); + while (QTime::currentTime() < dieTime) { + QCoreApplication::processEvents(QEventLoop::AllEvents, 100); + } +} + +void QUIHelper::setSystemDateTime(const QString &year, const QString &month, const QString &day, const QString &hour, const QString &min, const QString &sec) +{ +#ifdef Q_OS_WIN + QProcess p(0); + p.start("cmd"); + p.waitForStarted(); + p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1()); + p.closeWriteChannel(); + p.waitForFinished(1000); + p.close(); + p.start("cmd"); + p.waitForStarted(); + p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1()); + p.closeWriteChannel(); + p.waitForFinished(1000); + p.close(); +#else + QString cmd = QString("date %1%2%3%4%5.%6").arg(month).arg(day).arg(hour).arg(min).arg(year).arg(sec); + system(cmd.toLatin1()); + system("hwclock -w"); +#endif +} + +void QUIHelper::runWithSystem(const QString &strName, const QString &strPath, bool autoRun) +{ +#ifdef Q_OS_WIN + QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); + reg.setValue(strName, autoRun ? strPath : ""); +#endif +} + +bool QUIHelper::isIP(const QString &ip) +{ + QRegExp RegExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"); + return RegExp.exactMatch(ip); +} + +bool QUIHelper::isMac(const QString &mac) +{ + QRegExp RegExp("^[A-F0-9]{2}(-[A-F0-9]{2}){5}$"); + return RegExp.exactMatch(mac); +} + +bool QUIHelper::isTel(const QString &tel) +{ + if (tel.length() != 11) { + return false; + } + + if (!tel.startsWith("13") && !tel.startsWith("14") && !tel.startsWith("15") && !tel.startsWith("18")) { + return false; + } + + return true; +} + +bool QUIHelper::isEmail(const QString &email) +{ + if (!email.contains("@") || !email.contains(".com")) { + return false; + } + + return true; +} + +int QUIHelper::strHexToDecimal(const QString &strHex) +{ + bool ok; + return strHex.toInt(&ok, 16); +} + +int QUIHelper::strDecimalToDecimal(const QString &strDecimal) +{ + bool ok; + return strDecimal.toInt(&ok, 10); +} + +int QUIHelper::strBinToDecimal(const QString &strBin) +{ + bool ok; + return strBin.toInt(&ok, 2); +} + +QString QUIHelper::strHexToStrBin(const QString &strHex) +{ + uchar decimal = strHexToDecimal(strHex); + QString bin = QString::number(decimal, 2); + uchar len = bin.length(); + + if (len < 8) { + for (int i = 0; i < 8 - len; i++) { + bin = "0" + bin; + } + } + + return bin; +} + +QString QUIHelper::decimalToStrBin1(int decimal) +{ + QString bin = QString::number(decimal, 2); + uchar len = bin.length(); + + if (len <= 8) { + for (int i = 0; i < 8 - len; i++) { + bin = "0" + bin; + } + } + + return bin; +} + +QString QUIHelper::decimalToStrBin2(int decimal) +{ + QString bin = QString::number(decimal, 2); + uchar len = bin.length(); + + if (len <= 16) { + for (int i = 0; i < 16 - len; i++) { + bin = "0" + bin; + } + } + + return bin; +} + +QString QUIHelper::decimalToStrHex(int decimal) +{ + QString temp = QString::number(decimal, 16); + if (temp.length() == 1) { + temp = "0" + temp; + } + + return temp; +} + +QByteArray QUIHelper::intToByte(int i) +{ + QByteArray result; + result.resize(4); + result[3] = (uchar)(0x000000ff & i); + result[2] = (uchar)((0x0000ff00 & i) >> 8); + result[1] = (uchar)((0x00ff0000 & i) >> 16); + result[0] = (uchar)((0xff000000 & i) >> 24); + return result; +} + +QByteArray QUIHelper::intToByteRec(int i) +{ + QByteArray result; + result.resize(4); + result[0] = (uchar)(0x000000ff & i); + result[1] = (uchar)((0x0000ff00 & i) >> 8); + result[2] = (uchar)((0x00ff0000 & i) >> 16); + result[3] = (uchar)((0xff000000 & i) >> 24); + return result; +} + +int QUIHelper::byteToInt(const QByteArray &data) +{ + int i = data.at(3) & 0x000000ff; + i |= ((data.at(2) << 8) & 0x0000ff00); + i |= ((data.at(1) << 16) & 0x00ff0000); + i |= ((data.at(0) << 24) & 0xff000000); + return i; +} + +int QUIHelper::byteToIntRec(const QByteArray &data) +{ + int i = data.at(0) & 0x000000ff; + i |= ((data.at(1) << 8) & 0x0000ff00); + i |= ((data.at(2) << 16) & 0x00ff0000); + i |= ((data.at(3) << 24) & 0xff000000); + return i; +} + +quint32 QUIHelper::byteToUInt(const QByteArray &data) +{ + quint32 i = data.at(3) & 0x000000ff; + i |= ((data.at(2) << 8) & 0x0000ff00); + i |= ((data.at(1) << 16) & 0x00ff0000); + i |= ((data.at(0) << 24) & 0xff000000); + return i; +} + +quint32 QUIHelper::byteToUIntRec(const QByteArray &data) +{ + quint32 i = data.at(0) & 0x000000ff; + i |= ((data.at(1) << 8) & 0x0000ff00); + i |= ((data.at(2) << 16) & 0x00ff0000); + i |= ((data.at(3) << 24) & 0xff000000); + return i; +} + +QByteArray QUIHelper::ushortToByte(ushort i) +{ + QByteArray result; + result.resize(2); + result[1] = (uchar)(0x000000ff & i); + result[0] = (uchar)((0x0000ff00 & i) >> 8); + return result; +} + +QByteArray QUIHelper::ushortToByteRec(ushort i) +{ + QByteArray result; + result.resize(2); + result[0] = (uchar) (0x000000ff & i); + result[1] = (uchar) ((0x0000ff00 & i) >> 8); + return result; +} + +int QUIHelper::byteToUShort(const QByteArray &data) +{ + int i = data.at(1) & 0x000000FF; + i |= ((data.at(0) << 8) & 0x0000FF00); + + if (i >= 32768) { + i = i - 65536; + } + + return i; +} + +int QUIHelper::byteToUShortRec(const QByteArray &data) +{ + int i = data.at(0) & 0x000000FF; + i |= ((data.at(1) << 8) & 0x0000FF00); + + if (i >= 32768) { + i = i - 65536; + } + + return i; +} + +QString QUIHelper::getXorEncryptDecrypt(const QString &str, char key) +{ + QByteArray data = str.toLatin1(); + int size = data.size(); + for (int i = 0; i < size; i++) { + data[i] = data[i] ^ key; + } + + return QLatin1String(data); +} + +uchar QUIHelper::getOrCode(const QByteArray &data) +{ + int len = data.length(); + uchar result = 0; + + for (int i = 0; i < len; i++) { + result ^= data.at(i); + } + + return result; +} + +uchar QUIHelper::getCheckCode(const QByteArray &data) +{ + int len = data.length(); + uchar temp = 0; + + for (uchar i = 0; i < len; i++) { + temp += data.at(i); + } + + return temp % 256; +} + +QString QUIHelper::getValue(quint8 value) +{ + QString result = QString::number(value); + if (result.length() <= 1) { + result = QString("0%1").arg(result); + } + return result; +} + +//函数功能:计算CRC16 +//参数1:*data 16位CRC校验数据, +//参数2:len 数据流长度 +//参数3:init 初始化值 +//参数4:table 16位CRC查找表 + +//逆序CRC计算 +quint16 QUIHelper::getRevCrc_16(quint8 *data, int len, quint16 init, const quint16 *table) +{ + quint16 cRc_16 = init; + quint8 temp; + + while(len-- > 0) { + temp = cRc_16 >> 8; + cRc_16 = (cRc_16 << 8) ^ table[(temp ^ *data++) & 0xff]; + } + + return cRc_16; +} + +//正序CRC计算 +quint16 QUIHelper::getCrc_16(quint8 *data, int len, quint16 init, const quint16 *table) +{ + quint16 cRc_16 = init; + quint8 temp; + + while(len-- > 0) { + temp = cRc_16 & 0xff; + cRc_16 = (cRc_16 >> 8) ^ table[(temp ^ *data++) & 0xff]; + } + + return cRc_16; +} + +//Modbus CRC16校验 +quint16 QUIHelper::getModbus16(quint8 *data, int len) +{ + //MODBUS CRC-16表 8005 逆序 + const quint16 table_16[256] = { + 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, + 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, + 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, + 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, + 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, + 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, + 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, + 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, + 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, + 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, + 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, + 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, + 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, + 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, + 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, + 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, + 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, + 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, + 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, + 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, + 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, + 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, + 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, + 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, + 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, + 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, + 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, + 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, + 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, + 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, + 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, + 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 + }; + + return getCrc_16(data, len, 0xFFFF, table_16); +} + +//CRC16校验 +QByteArray QUIHelper::getCRCCode(const QByteArray &data) +{ + quint16 result = getModbus16((quint8 *)data.data(), data.length()); + return QUIHelper::ushortToByteRec(result); +} + +QString QUIHelper::byteArrayToAsciiStr(const QByteArray &data) +{ + QString temp; + int len = data.size(); + + for (int i = 0; i < len; i++) { + //0x20为空格,空格以下都是不可见字符 + char b = data.at(i); + + if (0x00 == b) { + temp += QString("\\NUL"); + } else if (0x01 == b) { + temp += QString("\\SOH"); + } else if (0x02 == b) { + temp += QString("\\STX"); + } else if (0x03 == b) { + temp += QString("\\ETX"); + } else if (0x04 == b) { + temp += QString("\\EOT"); + } else if (0x05 == b) { + temp += QString("\\ENQ"); + } else if (0x06 == b) { + temp += QString("\\ACK"); + } else if (0x07 == b) { + temp += QString("\\BEL"); + } else if (0x08 == b) { + temp += QString("\\BS"); + } else if (0x09 == b) { + temp += QString("\\HT"); + } else if (0x0A == b) { + temp += QString("\\LF"); + } else if (0x0B == b) { + temp += QString("\\VT"); + } else if (0x0C == b) { + temp += QString("\\FF"); + } else if (0x0D == b) { + temp += QString("\\CR"); + } else if (0x0E == b) { + temp += QString("\\SO"); + } else if (0x0F == b) { + temp += QString("\\SI"); + } else if (0x10 == b) { + temp += QString("\\DLE"); + } else if (0x11 == b) { + temp += QString("\\DC1"); + } else if (0x12 == b) { + temp += QString("\\DC2"); + } else if (0x13 == b) { + temp += QString("\\DC3"); + } else if (0x14 == b) { + temp += QString("\\DC4"); + } else if (0x15 == b) { + temp += QString("\\NAK"); + } else if (0x16 == b) { + temp += QString("\\SYN"); + } else if (0x17 == b) { + temp += QString("\\ETB"); + } else if (0x18 == b) { + temp += QString("\\CAN"); + } else if (0x19 == b) { + temp += QString("\\EM"); + } else if (0x1A == b) { + temp += QString("\\SUB"); + } else if (0x1B == b) { + temp += QString("\\ESC"); + } else if (0x1C == b) { + temp += QString("\\FS"); + } else if (0x1D == b) { + temp += QString("\\GS"); + } else if (0x1E == b) { + temp += QString("\\RS"); + } else if (0x1F == b) { + temp += QString("\\US"); + } else if (0x7F == b) { + temp += QString("\\x7F"); + } else if (0x5C == b) { + temp += QString("\\x5C"); + } else if (0x20 >= b) { + temp += QString("\\x%1").arg(decimalToStrHex((quint8)b)); + } else { + temp += QString("%1").arg(b); + } + } + + return temp.trimmed(); +} + +QByteArray QUIHelper::hexStrToByteArray(const QString &str) +{ + QByteArray senddata; + int hexdata, lowhexdata; + int hexdatalen = 0; + int len = str.length(); + senddata.resize(len / 2); + char lstr, hstr; + + for (int i = 0; i < len;) { + hstr = str.at(i).toLatin1(); + if (hstr == ' ') { + i++; + continue; + } + + i++; + if (i >= len) { + break; + } + + lstr = str.at(i).toLatin1(); + hexdata = convertHexChar(hstr); + lowhexdata = convertHexChar(lstr); + + if ((hexdata == 16) || (lowhexdata == 16)) { + break; + } else { + hexdata = hexdata * 16 + lowhexdata; + } + + i++; + senddata[hexdatalen] = (char)hexdata; + hexdatalen++; + } + + senddata.resize(hexdatalen); + return senddata; +} + +char QUIHelper::convertHexChar(char ch) +{ + if ((ch >= '0') && (ch <= '9')) { + return ch - 0x30; + } else if ((ch >= 'A') && (ch <= 'F')) { + return ch - 'A' + 10; + } else if ((ch >= 'a') && (ch <= 'f')) { + return ch - 'a' + 10; + } else { + return (-1); + } +} + +QByteArray QUIHelper::asciiStrToByteArray(const QString &str) +{ + QByteArray buffer; + int len = str.length(); + QString letter; + QString hex; + + for (int i = 0; i < len; i++) { + letter = str.at(i); + + if (letter == "\\") { + i++; + letter = str.mid(i, 1); + + if (letter == "x") { + i++; + hex = str.mid(i, 2); + buffer.append(strHexToDecimal(hex)); + i++; + continue; + } else if (letter == "N") { + i++; + hex = str.mid(i, 1); + + if (hex == "U") { + i++; + hex = str.mid(i, 1); + + if (hex == "L") { //NUL=0x00 + buffer.append((char)0x00); + continue; + } + } else if (hex == "A") { + i++; + hex = str.mid(i, 1); + + if (hex == "K") { //NAK=0x15 + buffer.append(0x15); + continue; + } + } + } else if (letter == "S") { + i++; + hex = str.mid(i, 1); + + if (hex == "O") { + i++; + hex = str.mid(i, 1); + + if (hex == "H") { //SOH=0x01 + buffer.append(0x01); + continue; + } else { //SO=0x0E + buffer.append(0x0E); + i--; + continue; + } + } else if (hex == "T") { + i++; + hex = str.mid(i, 1); + + if (hex == "X") { //STX=0x02 + buffer.append(0x02); + continue; + } + } else if (hex == "I") { //SI=0x0F + buffer.append(0x0F); + continue; + } else if (hex == "Y") { + i++; + hex = str.mid(i, 1); + + if (hex == "N") { //SYN=0x16 + buffer.append(0x16); + continue; + } + } else if (hex == "U") { + i++; + hex = str.mid(i, 1); + + if (hex == "B") { //SUB=0x1A + buffer.append(0x1A); + continue; + } + } + } else if (letter == "E") { + i++; + hex = str.mid(i, 1); + + if (hex == "T") { + i++; + hex = str.mid(i, 1); + + if (hex == "X") { //ETX=0x03 + buffer.append(0x03); + continue; + } else if (hex == "B") { //ETB=0x17 + buffer.append(0x17); + continue; + } + } else if (hex == "O") { + i++; + hex = str.mid(i, 1); + + if (hex == "T") { //EOT=0x04 + buffer.append(0x04); + continue; + } + } else if (hex == "N") { + i++; + hex = str.mid(i, 1); + + if (hex == "Q") { //ENQ=0x05 + buffer.append(0x05); + continue; + } + } else if (hex == "M") { //EM=0x19 + buffer.append(0x19); + continue; + } else if (hex == "S") { + i++; + hex = str.mid(i, 1); + + if (hex == "C") { //ESC=0x1B + buffer.append(0x1B); + continue; + } + } + } else if (letter == "A") { + i++; + hex = str.mid(i, 1); + + if (hex == "C") { + i++; + hex = str.mid(i, 1); + + if (hex == "K") { //ACK=0x06 + buffer.append(0x06); + continue; + } + } + } else if (letter == "B") { + i++; + hex = str.mid(i, 1); + + if (hex == "E") { + i++; + hex = str.mid(i, 1); + + if (hex == "L") { //BEL=0x07 + buffer.append(0x07); + continue; + } + } else if (hex == "S") { //BS=0x08 + buffer.append(0x08); + continue; + } + } else if (letter == "C") { + i++; + hex = str.mid(i, 1); + + if (hex == "R") { //CR=0x0D + buffer.append(0x0D); + continue; + } else if (hex == "A") { + i++; + hex = str.mid(i, 1); + + if (hex == "N") { //CAN=0x18 + buffer.append(0x18); + continue; + } + } + } else if (letter == "D") { + i++; + hex = str.mid(i, 1); + + if (hex == "L") { + i++; + hex = str.mid(i, 1); + + if (hex == "E") { //DLE=0x10 + buffer.append(0x10); + continue; + } + } else if (hex == "C") { + i++; + hex = str.mid(i, 1); + + if (hex == "1") { //DC1=0x11 + buffer.append(0x11); + continue; + } else if (hex == "2") { //DC2=0x12 + buffer.append(0x12); + continue; + } else if (hex == "3") { //DC3=0x13 + buffer.append(0x13); + continue; + } else if (hex == "4") { //DC2=0x14 + buffer.append(0x14); + continue; + } + } + } else if (letter == "F") { + i++; + hex = str.mid(i, 1); + + if (hex == "F") { //FF=0x0C + buffer.append(0x0C); + continue; + } else if (hex == "S") { //FS=0x1C + buffer.append(0x1C); + continue; + } + } else if (letter == "H") { + i++; + hex = str.mid(i, 1); + + if (hex == "T") { //HT=0x09 + buffer.append(0x09); + continue; + } + } else if (letter == "L") { + i++; + hex = str.mid(i, 1); + + if (hex == "F") { //LF=0x0A + buffer.append(0x0A); + continue; + } + } else if (letter == "G") { + i++; + hex = str.mid(i, 1); + + if (hex == "S") { //GS=0x1D + buffer.append(0x1D); + continue; + } + } else if (letter == "R") { + i++; + hex = str.mid(i, 1); + + if (hex == "S") { //RS=0x1E + buffer.append(0x1E); + continue; + } + } else if (letter == "U") { + i++; + hex = str.mid(i, 1); + + if (hex == "S") { //US=0x1F + buffer.append(0x1F); + continue; + } + } else if (letter == "V") { + i++; + hex = str.mid(i, 1); + + if (hex == "T") { //VT=0x0B + buffer.append(0x0B); + continue; + } + } else if (letter == "\\") { + //如果连着的是多个\\则对应添加\对应的16进制0x5C + buffer.append(0x5C); + continue; + } else { + //将对应的\[前面的\\也要加入 + buffer.append(0x5C); + buffer.append(letter.toLatin1()); + continue; + } + } + + buffer.append(str.mid(i, 1).toLatin1()); + + } + + return buffer; +} + +QString QUIHelper::byteArrayToHexStr(const QByteArray &data) +{ + QString temp = ""; + QString hex = data.toHex(); + + for (int i = 0; i < hex.length(); i = i + 2) { + temp += hex.mid(i, 2) + " "; + } + + return temp.trimmed().toUpper(); +} + +QString QUIHelper::getSaveName(const QString &filter, QString defaultDir) +{ + return QFileDialog::getSaveFileName(0, "选择文件", defaultDir , filter); +} + +QString QUIHelper::getFileName(const QString &filter, QString defaultDir) +{ + return QFileDialog::getOpenFileName(0, "选择文件", defaultDir , filter); +} + +QStringList QUIHelper::getFileNames(const QString &filter, QString defaultDir) +{ + return QFileDialog::getOpenFileNames(0, "选择文件", defaultDir, filter); +} + +QString QUIHelper::getFolderName() +{ + return QFileDialog::getExistingDirectory(); +} + +QString QUIHelper::getFileNameWithExtension(const QString &strFilePath) +{ + QFileInfo fileInfo(strFilePath); + return fileInfo.fileName(); +} + +QStringList QUIHelper::getFolderFileNames(const QStringList &filter) +{ + QStringList fileList; + QString strFolder = QFileDialog::getExistingDirectory(); + + if (!strFolder.length() == 0) { + QDir myFolder(strFolder); + + if (myFolder.exists()) { + fileList = myFolder.entryList(filter); + } + } + + return fileList; +} + +bool QUIHelper::folderIsExist(const QString &strFolder) +{ + QDir tempFolder(strFolder); + return tempFolder.exists(); +} + +bool QUIHelper::fileIsExist(const QString &strFile) +{ + QFile tempFile(strFile); + return tempFile.exists(); +} + +bool QUIHelper::copyFile(const QString &sourceFile, const QString &targetFile) +{ + bool ok; + ok = QFile::copy(sourceFile, targetFile); + //将复制过去的文件只读属性取消 + ok = QFile::setPermissions(targetFile, QFile::WriteOwner); + return ok; +} + +void QUIHelper::deleteDirectory(const QString &path) +{ + QDir dir(path); + if (!dir.exists()) { + return; + } + + dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); + QFileInfoList fileList = dir.entryInfoList(); + + foreach (QFileInfo fi, fileList) { + if (fi.isFile()) { + fi.dir().remove(fi.fileName()); + } else { + deleteDirectory(fi.absoluteFilePath()); + dir.rmdir(fi.absoluteFilePath()); + } + } +} + +bool QUIHelper::ipLive(const QString &ip, int port, int timeout) +{ + QTcpSocket tcpClient; + tcpClient.abort(); + tcpClient.connectToHost(ip, port); + //超时没有连接上则判断不在线 + return tcpClient.waitForConnected(timeout); +} + +QString QUIHelper::getHtml(const QString &url) +{ + QNetworkAccessManager *manager = new QNetworkAccessManager(); + QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url))); + QByteArray responseData; + QEventLoop eventLoop; + QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit())); + eventLoop.exec(); + responseData = reply->readAll(); + return QString(responseData); +} + +QString QUIHelper::getNetIP(const QString &webCode) +{ + QString web = webCode; + web = web.replace(' ', ""); + web = web.replace("\r", ""); + web = web.replace("\n", ""); + QStringList list = web.split("
"); + QString tar = list.at(3); + QStringList ip = tar.split("="); + return ip.at(1); +} + +QString QUIHelper::getLocalIP() +{ + QStringList ips; + QList addrs = QNetworkInterface::allAddresses(); + foreach (QHostAddress addr, addrs) { + QString ip = addr.toString(); + if (QUIHelper::isIP(ip)) { + ips << ip; + } + } + + //优先取192开头的IP,如果获取不到IP则取127.0.0.1 + QString ip = "127.0.0.1"; + foreach (QString str, ips) { + if (str.startsWith("192.168.1") || str.startsWith("192")) { + ip = str; + break; + } + } + + return ip; +} + +QString QUIHelper::urlToIP(const QString &url) +{ + QHostInfo host = QHostInfo::fromName(url); + return host.addresses().at(0).toString(); +} + +bool QUIHelper::isWebOk() +{ + //能接通百度IP说明可以通外网 + return ipLive("115.239.211.112", 80); +} + +void QUIHelper::showMessageBoxInfo(const QString &info, int closeSec, bool exec) +{ +#ifdef Q_OS_ANDROID + QAndroid::Instance()->makeToast(info); +#else + if (exec) { + QUIMessageBox msg; + msg.setMessage(info, 0, closeSec); + msg.exec(); + } else { + QUIMessageBox::Instance()->setMessage(info, 0, closeSec); + QUIMessageBox::Instance()->show(); + } +#endif +} + +void QUIHelper::showMessageBoxError(const QString &info, int closeSec, bool exec) +{ +#ifdef Q_OS_ANDROID + QAndroid::Instance()->makeToast(info); +#else + if (exec) { + QUIMessageBox msg; + msg.setMessage(info, 2, closeSec); + msg.exec(); + } else { + QUIMessageBox::Instance()->setMessage(info, 2, closeSec); + QUIMessageBox::Instance()->show(); + } +#endif +} + +int QUIHelper::showMessageBoxQuestion(const QString &info) +{ + QUIMessageBox msg; + msg.setMessage(info, 1); + return msg.exec(); +} + +void QUIHelper::showTipBox(const QString &title, const QString &tip, bool fullScreen, bool center, int closeSec) +{ + QUITipBox::Instance()->setTip(title, tip, fullScreen, center, closeSec); + QUITipBox::Instance()->show(); +} + +void QUIHelper::hideTipBox() +{ + QUITipBox::Instance()->hide(); +} + +QString QUIHelper::showInputBox(const QString &title, int type, int closeSec, + const QString &placeholderText, bool pwd, + const QString &defaultValue) +{ + QUIInputBox input; + input.setParameter(title, type, closeSec, placeholderText, pwd, defaultValue); + input.exec(); + return input.getValue(); +} + +void QUIHelper::showDateSelect(QString &dateStart, QString &dateEnd, const QString &format) +{ + QUIDateSelect select; + select.setFormat(format); + select.exec(); + dateStart = select.getStartDateTime(); + dateEnd = select.getEndDateTime(); +} + +QString QUIHelper::setPushButtonQss(QPushButton *btn, int radius, int padding, + const QString &normalColor, + const QString &normalTextColor, + const QString &hoverColor, + const QString &hoverTextColor, + const QString &pressedColor, + const QString &pressedTextColor) +{ + QStringList list; + list.append(QString("QPushButton{border-style:none;padding:%1px;border-radius:%2px;color:%3;background:%4;}") + .arg(padding).arg(radius).arg(normalTextColor).arg(normalColor)); + list.append(QString("QPushButton:hover{color:%1;background:%2;}") + .arg(hoverTextColor).arg(hoverColor)); + list.append(QString("QPushButton:pressed{color:%1;background:%2;}") + .arg(pressedTextColor).arg(pressedColor)); + + QString qss = list.join(""); + btn->setStyleSheet(qss); + return qss; +} + +QString QUIHelper::setLineEditQss(QLineEdit *txt, int radius, int borderWidth, + const QString &normalColor, + const QString &focusColor) +{ + QStringList list; + list.append(QString("QLineEdit{border-style:none;padding:3px;border-radius:%1px;border:%2px solid %3;}") + .arg(radius).arg(borderWidth).arg(normalColor)); + list.append(QString("QLineEdit:focus{border:%1px solid %2;}") + .arg(borderWidth).arg(focusColor)); + + QString qss = list.join(""); + txt->setStyleSheet(qss); + return qss; +} + +QString QUIHelper::setProgressBarQss(QProgressBar *bar, int barHeight, + int barRadius, int fontSize, + const QString &normalColor, + const QString &chunkColor) +{ + + QStringList list; + list.append(QString("QProgressBar{font:%1pt;background:%2;max-height:%3px;border-radius:%4px;text-align:center;border:1px solid %2;}") + .arg(fontSize).arg(normalColor).arg(barHeight).arg(barRadius)); + list.append(QString("QProgressBar:chunk{border-radius:%2px;background-color:%1;}") + .arg(chunkColor).arg(barRadius)); + + QString qss = list.join(""); + bar->setStyleSheet(qss); + return qss; +} + +QString QUIHelper::setSliderQss(QSlider *slider, int sliderHeight, + const QString &normalColor, + const QString &grooveColor, + const QString &handleBorderColor, + const QString &handleColor, + const QString &textColor) +{ + int sliderRadius = sliderHeight / 2; + int handleSize = (sliderHeight * 3) / 2 + (sliderHeight / 5); + int handleRadius = handleSize / 2; + int handleOffset = handleRadius / 2; + + QStringList list; + int handleWidth = handleSize + sliderHeight / 5 - 1; + list.append(QString("QSlider::horizontal{min-height:%1px;color:%2;}").arg(sliderHeight * 2).arg(textColor)); + list.append(QString("QSlider::groove:horizontal{background:%1;height:%2px;border-radius:%3px;}") + .arg(normalColor).arg(sliderHeight).arg(sliderRadius)); + list.append(QString("QSlider::add-page:horizontal{background:%1;height:%2px;border-radius:%3px;}") + .arg(normalColor).arg(sliderHeight).arg(sliderRadius)); + list.append(QString("QSlider::sub-page:horizontal{background:%1;height:%2px;border-radius:%3px;}") + .arg(grooveColor).arg(sliderHeight).arg(sliderRadius)); + list.append(QString("QSlider::handle:horizontal{width:%3px;margin-top:-%4px;margin-bottom:-%4px;border-radius:%5px;" + "background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 %1,stop:0.8 %2);}") + .arg(handleColor).arg(handleBorderColor).arg(handleWidth).arg(handleOffset).arg(handleRadius)); + + //偏移一个像素 + handleWidth = handleSize + sliderHeight / 5; + list.append(QString("QSlider::vertical{min-width:%1px;color:%2;}").arg(sliderHeight * 2).arg(textColor)); + list.append(QString("QSlider::groove:vertical{background:%1;width:%2px;border-radius:%3px;}") + .arg(normalColor).arg(sliderHeight).arg(sliderRadius)); + list.append(QString("QSlider::add-page:vertical{background:%1;width:%2px;border-radius:%3px;}") + .arg(grooveColor).arg(sliderHeight).arg(sliderRadius)); + list.append(QString("QSlider::sub-page:vertical{background:%1;width:%2px;border-radius:%3px;}") + .arg(normalColor).arg(sliderHeight).arg(sliderRadius)); + list.append(QString("QSlider::handle:vertical{height:%3px;margin-left:-%4px;margin-right:-%4px;border-radius:%5px;" + "background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 %1,stop:0.8 %2);}") + .arg(handleColor).arg(handleBorderColor).arg(handleWidth).arg(handleOffset).arg(handleRadius)); + + QString qss = list.join(""); + slider->setStyleSheet(qss); + return qss; +} + +QString QUIHelper::setRadioButtonQss(QRadioButton *rbtn, int indicatorRadius, + const QString &normalColor, + const QString &checkColor) +{ + int indicatorWidth = indicatorRadius * 2; + + QStringList list; + list.append(QString("QRadioButton::indicator{border-radius:%1px;width:%2px;height:%2px;}") + .arg(indicatorRadius).arg(indicatorWidth)); + list.append(QString("QRadioButton::indicator::unchecked{background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5," + "stop:0.6 #FFFFFF,stop:0.7 %1);}").arg(normalColor)); + list.append(QString("QRadioButton::indicator::checked{background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5," + "stop:0 %1,stop:0.3 %1,stop:0.4 #FFFFFF,stop:0.6 #FFFFFF,stop:0.7 %1);}").arg(checkColor)); + + QString qss = list.join(""); + rbtn->setStyleSheet(qss); + return qss; +} + +QString QUIHelper::setScrollBarQss(QWidget *scroll, int radius, int min, int max, + const QString &bgColor, + const QString &handleNormalColor, + const QString &handleHoverColor, + const QString &handlePressedColor) +{ + //滚动条离背景间隔 + int padding = 0; + + QStringList list; + + //handle:指示器,滚动条拉动部分 add-page:滚动条拉动时增加的部分 sub-page:滚动条拉动时减少的部分 add-line:递增按钮 sub-line:递减按钮 + + //横向滚动条部分 + list.append(QString("QScrollBar:horizontal{background:%1;padding:%2px;border-radius:%3px;min-height:%4px;max-height:%4px;}") + .arg(bgColor).arg(padding).arg(radius).arg(max)); + list.append(QString("QScrollBar::handle:horizontal{background:%1;min-width:%2px;border-radius:%3px;}") + .arg(handleNormalColor).arg(min).arg(radius)); + list.append(QString("QScrollBar::handle:horizontal:hover{background:%1;}") + .arg(handleHoverColor)); + list.append(QString("QScrollBar::handle:horizontal:pressed{background:%1;}") + .arg(handlePressedColor)); + list.append(QString("QScrollBar::add-page:horizontal{background:none;}")); + list.append(QString("QScrollBar::sub-page:horizontal{background:none;}")); + list.append(QString("QScrollBar::add-line:horizontal{background:none;}")); + list.append(QString("QScrollBar::sub-line:horizontal{background:none;}")); + + //纵向滚动条部分 + list.append(QString("QScrollBar:vertical{background:%1;padding:%2px;border-radius:%3px;min-width:%4px;max-width:%4px;}") + .arg(bgColor).arg(padding).arg(radius).arg(max)); + list.append(QString("QScrollBar::handle:vertical{background:%1;min-height:%2px;border-radius:%3px;}") + .arg(handleNormalColor).arg(min).arg(radius)); + list.append(QString("QScrollBar::handle:vertical:hover{background:%1;}") + .arg(handleHoverColor)); + list.append(QString("QScrollBar::handle:vertical:pressed{background:%1;}") + .arg(handlePressedColor)); + list.append(QString("QScrollBar::add-page:vertical{background:none;}")); + list.append(QString("QScrollBar::sub-page:vertical{background:none;}")); + list.append(QString("QScrollBar::add-line:vertical{background:none;}")); + list.append(QString("QScrollBar::sub-line:vertical{background:none;}")); + + QString qss = list.join(""); + scroll->setStyleSheet(qss); + return qss; +} + + +QChar QUIConfig::IconMain = QChar(0xf072); +QChar QUIConfig::IconMenu = QChar(0xf0d7); +QChar QUIConfig::IconMin = QChar(0xf068); +QChar QUIConfig::IconMax = QChar(0xf2d2); +QChar QUIConfig::IconNormal = QChar(0xf2d0); +QChar QUIConfig::IconClose = QChar(0xf00d); + +#ifdef __arm__ +#ifdef Q_OS_ANDROID +QString QUIConfig::FontName = "Droid Sans Fallback"; +int QUIConfig::FontSize = 15; +#else +QString QUIConfig::FontName = "WenQuanYi Micro Hei"; +int QUIConfig::FontSize = 18; +#endif +#else +QString QUIConfig::FontName = "Microsoft Yahei"; +int QUIConfig::FontSize = 12; +#endif + +QString QUIConfig::TextColor = "#F0F0F0"; +QString QUIConfig::PanelColor = "#F0F0F0"; +QString QUIConfig::BorderColor = "#F0F0F0"; +QString QUIConfig::NormalColorStart = "#F0F0F0"; +QString QUIConfig::NormalColorEnd = "#F0F0F0"; +QString QUIConfig::DarkColorStart = "#F0F0F0"; +QString QUIConfig::DarkColorEnd = "#F0F0F0"; +QString QUIConfig::HighColor = "#F0F0F0"; diff --git a/comtool/api/quiwidget.h b/comtool/api/quiwidget.h new file mode 100644 index 0000000..a571365 --- /dev/null +++ b/comtool/api/quiwidget.h @@ -0,0 +1,871 @@ +#ifndef QUIWIDGET_H +#define QUIWIDGET_H + +#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz")) +#define TIME qPrintable(QTime::currentTime().toString("HH:mm:ss")) +#define QDATE qPrintable(QDate::currentDate().toString("yyyy-MM-dd")) +#define QTIME qPrintable(QTime::currentTime().toString("HH-mm-ss")) +#define DATETIME qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) +#define STRDATETIME qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss")) +#define STRDATETIMEMS qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss-zzz")) + +#ifdef Q_OS_WIN +#define NEWLINE "\r\n" +#else +#define NEWLINE "\n" +#endif + +#ifdef __arm__ +#define TitleMinSize 40 +#else +#define TitleMinSize 30 +#endif + +/** + * QUI无边框窗体控件 作者:feiyangqingyun(QQ:517216493) + * 1:内置 N >= 17 套精美样式,可直接切换,也可自定义样式路径 + * 2:可设置部件(左上角图标/最小化按钮/最大化按钮/关闭按钮)的图标或者图片及是否可见 + * 3:可集成设计师插件,直接拖曳使用,所见即所得 + * 4:如果需要窗体可拖动大小,设置 setSizeGripEnabled(true); + * 5:可设置全局样式 setStyle + * 6:可弹出消息框,可选阻塞模式和不阻塞,默认不阻塞 showMessageBoxInfo + * 7:可弹出错误框,可选阻塞模式和不阻塞,默认不阻塞 showMessageBoxError + * 8:可弹出询问框 showMessageBoxError + * 9:可弹出右下角信息框 showTipBox + * 10:可弹出输入框 showInputBox + * 11:可弹出时间范围选择框 showDateSelect + * 12:消息框支持设置倒计时关闭 + * 13:集成图形字体设置方法及根据指定文字获取图片 + * 14:集成设置窗体居中显示/设置翻译文件/设置编码/设置延时/设置系统时间等静态方法 + * 15:集成获取应用程序文件名/文件路径 等方法 + */ + +#include "head.h" + +#ifdef quc +#if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) +#include +#else +#include +#endif + +class QDESIGNER_WIDGET_EXPORT QUIWidget : public QDialog +#else +class QUIWidget : public QDialog +#endif + +{ + Q_OBJECT + Q_ENUMS(Style) + Q_PROPERTY(QString title READ getTitle WRITE setTitle) + Q_PROPERTY(Qt::Alignment alignment READ getAlignment WRITE setAlignment) + Q_PROPERTY(bool minHide READ getMinHide WRITE setMinHide) + Q_PROPERTY(bool exitAll READ getExitAll WRITE setExitAll) + +public: + //将部分对象作为枚举值暴露给外部 + enum Widget { + Lab_Ico = 0, //左上角图标 + BtnMenu = 1, //下拉菜单按钮 + BtnMenu_Min = 2, //最小化按钮 + BtnMenu_Max = 3, //最大化按钮 + BtnMenu_Normal = 4, //还原按钮 + BtnMenu_Close = 5 //关闭按钮 + }; + + //样式枚举 + enum Style { + Style_Silvery = 0, //银色样式 + Style_Blue = 1, //蓝色样式 + Style_LightBlue = 2, //淡蓝色样式 + Style_DarkBlue = 3, //深蓝色样式 + Style_Gray = 4, //灰色样式 + Style_LightGray = 5, //浅灰色样式 + Style_DarkGray = 6, //深灰色样式 + Style_Black = 7, //黑色样式 + Style_LightBlack = 8, //浅黑色样式 + Style_DarkBlack = 9, //深黑色样式 + Style_PSBlack = 10, //PS黑色样式 + Style_FlatBlack = 11, //黑色扁平样式 + Style_FlatWhite = 12, //白色扁平样式 + Style_FlatBlue = 13, //蓝色扁平样式 + Style_Purple = 14, //紫色样式 + Style_BlackBlue = 15, //黑蓝色样式 + Style_BlackVideo = 16 //视频监控黑色样式 + }; + +public: + explicit QUIWidget(QWidget *parent = 0); + ~QUIWidget(); + +protected: + bool eventFilter(QObject *watched, QEvent *event); + +private: + QVBoxLayout *verticalLayout1; + QWidget *widgetMain; + QVBoxLayout *verticalLayout2; + QWidget *widgetTitle; + QHBoxLayout *horizontalLayout4; + QLabel *labIco; + QLabel *labTitle; + QWidget *widgetMenu; + QHBoxLayout *horizontalLayout; + QToolButton *btnMenu; + QPushButton *btnMenu_Min; + QPushButton *btnMenu_Max; + QPushButton *btnMenu_Close; + QWidget *widget; + QVBoxLayout *verticalLayout3; + +private: + QString title; //标题 + Qt::Alignment alignment; //标题文本对齐 + bool minHide; //最小化隐藏 + bool exitAll; //退出整个程序 + QWidget *mainWidget; //主窗体对象 + +public: + QLabel *getLabIco() const; + QLabel *getLabTitle() const; + QToolButton *getBtnMenu() const; + QPushButton *getBtnMenuMin() const; + QPushButton *getBtnMenuMax() const; + QPushButton *getBtnMenuMClose() const; + + QString getTitle() const; + Qt::Alignment getAlignment() const; + bool getMinHide() const; + bool getExitAll() const; + + QSize sizeHint() const; + QSize minimumSizeHint() const; + +private slots: + void initControl(); //初始化控件 + void initForm(); //初始化窗体 + void changeStyle(); //更换样式 + +private slots: + void on_btnMenu_Min_clicked(); + void on_btnMenu_Max_clicked(); + void on_btnMenu_Close_clicked(); + +public Q_SLOTS: + //设置部件图标 + void setIcon(QUIWidget::Widget widget, const QChar &str, quint32 size = 12); + void setIconMain(const QChar &str, quint32 size = 12); + //设置部件图片 + void setPixmap(QUIWidget::Widget widget, const QString &file, const QSize &size = QSize(16, 16)); + //设置部件是否可见 + void setVisible(QUIWidget::Widget widget, bool visible = true); + //设置只有关闭按钮 + void setOnlyCloseBtn(); + + //设置标题栏高度 + void setTitleHeight(int height); + //设置按钮统一宽度 + void setBtnWidth(int width); + + //设置标题及文本样式 + void setTitle(const QString &title); + void setAlignment(Qt::Alignment alignment); + + //设置最小化隐藏 + void setMinHide(bool minHide); + + //设置退出时候直接退出整个应用程序 + void setExitAll(bool exitAll); + + //设置主窗体 + void setMainWidget(QWidget *mainWidget); + +Q_SIGNALS: + void changeStyle(const QString &qssFile); + void closing(); +}; + +//弹出信息框类 +class QUIMessageBox : public QDialog +{ + Q_OBJECT + +public: + static QUIMessageBox *Instance(); + explicit QUIMessageBox(QWidget *parent = 0); + ~QUIMessageBox(); + +protected: + void closeEvent(QCloseEvent *); + bool eventFilter(QObject *watched, QEvent *event); + +private: + static QScopedPointer self; + + QVBoxLayout *verticalLayout1; + QWidget *widgetTitle; + QHBoxLayout *horizontalLayout3; + QLabel *labIco; + QLabel *labTitle; + QLabel *labTime; + QWidget *widgetMenu; + QHBoxLayout *horizontalLayout4; + QPushButton *btnMenu_Close; + QWidget *widgetMain; + QVBoxLayout *verticalLayout2; + QFrame *frame; + QVBoxLayout *verticalLayout4; + QHBoxLayout *horizontalLayout1; + QLabel *labIcoMain; + QSpacerItem *horizontalSpacer1; + QLabel *labInfo; + QHBoxLayout *horizontalLayout2; + QSpacerItem *horizontalSpacer2; + QPushButton *btnOk; + QPushButton *btnCancel; + +private: + int closeSec; //总显示时间 + int currentSec; //当前已显示时间 + +private slots: + void initControl(); //初始化控件 + void initForm(); //初始化窗体 + void checkSec(); //校验倒计时 + +private slots: + void on_btnOk_clicked(); + void on_btnMenu_Close_clicked(); + +public Q_SLOTS: + void setIconMain(const QChar &str, quint32 size = 12); + void setMessage(const QString &msg, int type, int closeSec = 0); +}; + +//右下角弹出框类 +class QUITipBox : public QDialog +{ + Q_OBJECT + +public: + static QUITipBox *Instance(); + explicit QUITipBox(QWidget *parent = 0); + ~QUITipBox(); + +protected: + void closeEvent(QCloseEvent *); + bool eventFilter(QObject *watched, QEvent *event); + +private: + static QScopedPointer self; + + QVBoxLayout *verticalLayout; + QWidget *widgetTitle; + QHBoxLayout *horizontalLayout2; + QLabel *labIco; + QLabel *labTitle; + QLabel *labTime; + QWidget *widgetMenu; + QHBoxLayout *horizontalLayout; + QPushButton *btnMenu_Close; + QWidget *widgetMain; + QVBoxLayout *verticalLayout2; + QLabel *labInfo; + + QPropertyAnimation *animation; + bool fullScreen; + +private: + int closeSec; //总显示时间 + int currentSec; //当前已显示时间 + +private slots: + void initControl(); //初始化控件 + void initForm(); //初始化窗体 + void checkSec(); //校验倒计时 + +private slots: + void on_btnMenu_Close_clicked(); + +public Q_SLOTS: + void setIconMain(const QChar &str, quint32 size = 12); + void setTip(const QString &title, const QString &tip, bool fullScreen = false, bool center = true, int closeSec = 0); + void hide(); +}; + + +//弹出输入框类 +class QUIInputBox : public QDialog +{ + Q_OBJECT + +public: + static QUIInputBox *Instance(); + explicit QUIInputBox(QWidget *parent = 0); + ~QUIInputBox(); + +protected: + void showEvent(QShowEvent *); + void closeEvent(QCloseEvent *); + bool eventFilter(QObject *watched, QEvent *event); + +private: + static QScopedPointer self; + + QVBoxLayout *verticalLayout1; + QWidget *widgetTitle; + QHBoxLayout *horizontalLayout1; + QLabel *labIco; + QLabel *labTitle; + QLabel *labTime; + QWidget *widgetMenu; + QHBoxLayout *horizontalLayout2; + QPushButton *btnMenu_Close; + QWidget *widgetMain; + QVBoxLayout *verticalLayout2; + QFrame *frame; + QVBoxLayout *verticalLayout3; + QLabel *labInfo; + QLineEdit *txtValue; + QComboBox *cboxValue; + QHBoxLayout *lay; + QSpacerItem *horizontalSpacer; + QPushButton *btnOk; + QPushButton *btnCancel; + +private: + int closeSec; //总显示时间 + int currentSec; //当前已显示时间 + QString value; //当前值 + +private slots: + void initControl(); //初始化控件 + void initForm(); //初始化窗体 + void checkSec(); //校验倒计时 + +private slots: + void on_btnOk_clicked(); + void on_btnMenu_Close_clicked(); + +public: + QString getValue()const; + +public Q_SLOTS: + void setIconMain(const QChar &str, quint32 size = 12); + void setParameter(const QString &title, int type = 0, int closeSec = 0, + QString placeholderText = QString(), bool pwd = false, + const QString &defaultValue = QString()); + +}; + +//弹出日期选择对话框 +class QUIDateSelect : public QDialog +{ + Q_OBJECT + +public: + static QUIDateSelect *Instance(); + explicit QUIDateSelect(QWidget *parent = 0); + ~QUIDateSelect(); + +protected: + bool eventFilter(QObject *watched, QEvent *event); + +private: + static QScopedPointer self; + + QVBoxLayout *verticalLayout; + QWidget *widgetTitle; + QHBoxLayout *horizontalLayout1; + QLabel *labIco; + QLabel *labTitle; + QWidget *widgetMenu; + QHBoxLayout *horizontalLayout; + QPushButton *btnMenu_Close; + QWidget *widgetMain; + QVBoxLayout *verticalLayout1; + QFrame *frame; + QGridLayout *gridLayout; + QLabel *labStart; + QPushButton *btnOk; + QLabel *labEnd; + QPushButton *btnClose; + QDateTimeEdit *dateStart; + QDateTimeEdit *dateEnd; + +private: + QString startDateTime; //开始时间 + QString endDateTime; //结束时间 + QString format; //日期时间格式 + +private slots: + void initControl(); //初始化控件 + void initForm(); //初始化窗体 + +private slots: + void on_btnOk_clicked(); + void on_btnMenu_Close_clicked(); + +public: + //获取当前选择的开始时间和结束时间 + QString getStartDateTime() const; + QString getEndDateTime() const; + +public Q_SLOTS: + void setIconMain(const QChar &str, quint32 size = 12); + void setFormat(const QString &format); + +}; + +//图形字体处理类 +class IconHelper : public QObject +{ + Q_OBJECT + +public: + static IconHelper *Instance(); + explicit IconHelper(QObject *parent = 0); + + //获取图形字体 + QFont getIconFont(); + + //设置图形字体到标签 + void setIcon(QLabel *lab, const QChar &str, quint32 size = 12); + //设置图形字体到按钮 + void setIcon(QAbstractButton *btn, const QChar &str, quint32 size = 12); + + //获取指定图形字体,可以指定文字大小,图片宽高,文字对齐 + QPixmap getPixmap(const QColor &color, const QChar &str, quint32 size = 12, + quint32 pixWidth = 15, quint32 pixHeight = 15, + int flags = Qt::AlignCenter); + + //根据按钮获取该按钮对应的图标 + QPixmap getPixmap(QToolButton *btn, bool normal); + QPixmap getPixmap(QToolButton *btn, int type); + + //指定QFrame导航按钮样式,带图标 + void setStyle(QFrame *frame, QList btns, QList pixChar, + quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15, + const QString &normalBgColor = "#2FC5A2", + const QString &darkBgColor = "#3EA7E9", + const QString &normalTextColor = "#EEEEEE", + const QString &darkTextColor = "#FFFFFF"); + + //指定导航面板样式,不带图标 + static void setStyle(QWidget *widget, const QString &type = "left", int borderWidth = 3, + const QString &borderColor = "#029FEA", + const QString &normalBgColor = "#292F38", + const QString &darkBgColor = "#1D2025", + const QString &normalTextColor = "#54626F", + const QString &darkTextColor = "#FDFDFD"); + + //移除导航面板样式,防止重复 + void removeStyle(QList btns); + + //指定QWidget导航面板样式,带图标和效果切换 + void setStyle(QWidget *widget, QList btns, QList pixChar, + quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15, + const QString &type = "left", int borderWidth = 3, + const QString &borderColor = "#029FEA", + const QString &normalBgColor = "#292F38", + const QString &darkBgColor = "#1D2025", + const QString &normalTextColor = "#54626F", + const QString &darkTextColor = "#FDFDFD"); + + struct StyleColor { + quint32 iconSize; + quint32 iconWidth; + quint32 iconHeight; + quint32 borderWidth; + QString type; + QString borderColor; + QString normalBgColor; + QString normalTextColor; + QString hoverBgColor; + QString hoverTextColor; + QString pressedBgColor; + QString pressedTextColor; + QString checkedBgColor; + QString checkedTextColor; + + StyleColor() + { + iconSize = 12; + iconWidth = 15; + iconHeight = 15; + borderWidth = 3; + type = "left"; + borderColor = "#029FEA"; + normalBgColor = "#292F38"; + normalTextColor = "#54626F"; + hoverBgColor = "#40444D"; + hoverTextColor = "#FDFDFD"; + pressedBgColor = "#404244"; + pressedTextColor = "#FDFDFD"; + checkedBgColor = "#44494F"; + checkedTextColor = "#FDFDFD"; + } + }; + + //指定QWidget导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色 + void setStyle(QWidget *widget, QList btns, QList pixChar, const StyleColor &styleColor); + +protected: + bool eventFilter(QObject *watched, QEvent *event); + +private: + static QScopedPointer self; + + QFont iconFont; //图形字体 + QList btns; //按钮队列 + QList pixNormal; //正常图片队列 + QList pixDark; //加深图片队列 + QList pixHover; //悬停图片队列 + QList pixPressed; //按下图片队列 + QList pixChecked; //选中图片队列 +}; + +//托盘图标类 +class TrayIcon : public QObject +{ + Q_OBJECT +public: + static TrayIcon *Instance(); + explicit TrayIcon(QObject *parent = 0); + +private: + static QScopedPointer self; + + QWidget *mainWidget; //对应所属主窗体 + QSystemTrayIcon *trayIcon; //托盘对象 + QMenu *menu; //右键菜单 + bool exitDirect; //是否直接退出 + +private slots: + void iconIsActived(QSystemTrayIcon::ActivationReason reason); + +public Q_SLOTS: + //设置是否直接退出,如果不是直接退出则发送信号给主界面 + void setExitDirect(bool exitDirect); + + //设置所属主窗体 + void setMainWidget(QWidget *mainWidget); + + //显示消息 + void showMessage(const QString &title, const QString &msg, + QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int msecs = 5000); + + //设置图标 + void setIcon(const QString &strIcon); + //设置提示信息 + void setToolTip(const QString &tip); + //设置是否可见 + void setVisible(bool visible); + //退出所有 + void closeAll(); + +Q_SIGNALS: + void trayIconExit(); +}; + + +//全局静态方法类 +class QUIHelper : public QObject +{ + Q_OBJECT +public: + //桌面宽度高度 + static int deskWidth(); + static int deskHeight(); + + //程序本身文件名称 + static QString appName(); + //程序当前所在路径 + static QString appPath(); + + //初始化随机数种子 + static void initRand(); + + //初始化数据库 + static void initDb(const QString &dbName); + //初始化文件,不存在则拷贝 + static void initFile(const QString &sourceName, const QString &targetName); + + //新建目录 + static void newDir(const QString &dirName); + + //写入消息到额外的的消息日志文件 + static void writeInfo(const QString &info, const QString &filePath = "log"); + static void writeError(const QString &info, const QString &filePath = "log"); + + //设置全局样式 + static void setStyle(QUIWidget::Style style); + static void setStyle(const QString &qssFile, QString &paletteColor, QString &textColor); + static void setStyle(const QString &qssFile, QString &textColor, + QString &panelColor, QString &borderColor, + QString &normalColorStart, QString &normalColorEnd, + QString &darkColorStart, QString &darkColorEnd, + QString &highColor); + + //根据QSS样式获取对应颜色值 + static void getQssColor(const QString &qss, QString &textColor, + QString &panelColor, QString &borderColor, + QString &normalColorStart, QString &normalColorEnd, + QString &darkColorStart, QString &darkColorEnd, + QString &highColor); + + //九宫格图片 horzSplit-宫格1/3/7/9宽度 vertSplit-宫格1/3/7/9高度 dstWidth-目标图片宽度 dstHeight-目标图片高度 + static QPixmap ninePatch(const QString &picName, int horzSplit, int vertSplit, int dstWidth, int dstHeight); + static QPixmap ninePatch(const QPixmap &pix, int horzSplit, int vertSplit, int dstWidth, int dstHeight); + + //设置标签颜色 + static void setLabStyle(QLabel *lab, quint8 type); + + //设置窗体居中显示 + static void setFormInCenter(QWidget *frm); + //设置翻译文件 + static void setTranslator(const QString &qmFile = ":/image/qt_zh_CN.qm"); + //设置编码 + static void setCode(); + //设置延时 + static void sleep(int msec); + //设置系统时间 + static void setSystemDateTime(const QString &year, const QString &month, const QString &day, + const QString &hour, const QString &min, const QString &sec); + //设置开机自启动 + static void runWithSystem(const QString &strName, const QString &strPath, bool autoRun = true); + + //判断是否是IP地址 + static bool isIP(const QString &ip); + + //判断是否是MAC地址 + static bool isMac(const QString &mac); + + //判断是否是合法的电话号码 + static bool isTel(const QString &tel); + + //判断是否是合法的邮箱地址 + static bool isEmail(const QString &email); + + + //16进制字符串转10进制 + static int strHexToDecimal(const QString &strHex); + + //10进制字符串转10进制 + static int strDecimalToDecimal(const QString &strDecimal); + + //2进制字符串转10进制 + static int strBinToDecimal(const QString &strBin); + + //16进制字符串转2进制字符串 + static QString strHexToStrBin(const QString &strHex); + + //10进制转2进制字符串一个字节 + static QString decimalToStrBin1(int decimal); + + //10进制转2进制字符串两个字节 + static QString decimalToStrBin2(int decimal); + + //10进制转16进制字符串,补零. + static QString decimalToStrHex(int decimal); + + + //int转字节数组 + static QByteArray intToByte(int i); + static QByteArray intToByteRec(int i); + + //字节数组转int + static int byteToInt(const QByteArray &data); + static int byteToIntRec(const QByteArray &data); + static quint32 byteToUInt(const QByteArray &data); + static quint32 byteToUIntRec(const QByteArray &data); + + //ushort转字节数组 + static QByteArray ushortToByte(ushort i); + static QByteArray ushortToByteRec(ushort i); + + //字节数组转ushort + static int byteToUShort(const QByteArray &data); + static int byteToUShortRec(const QByteArray &data); + + //异或加密算法 + static QString getXorEncryptDecrypt(const QString &str, char key); + + //异或校验 + static uchar getOrCode(const QByteArray &data); + + //计算校验码 + static uchar getCheckCode(const QByteArray &data); + + //字符串补全 + static QString getValue(quint8 value); + + //CRC校验 + static quint16 getRevCrc_16(quint8 *data, int len, quint16 init, const quint16 *table); + static quint16 getCrc_16(quint8 *data, int len, quint16 init, const quint16 *table); + static quint16 getModbus16(quint8 *data, int len); + static QByteArray getCRCCode(const QByteArray &data); + + + //字节数组转Ascii字符串 + static QString byteArrayToAsciiStr(const QByteArray &data); + + //16进制字符串转字节数组 + static QByteArray hexStrToByteArray(const QString &str); + static char convertHexChar(char ch); + + //Ascii字符串转字节数组 + static QByteArray asciiStrToByteArray(const QString &str); + + //字节数组转16进制字符串 + static QString byteArrayToHexStr(const QByteArray &data); + + //获取保存的文件 + static QString getSaveName(const QString &filter, QString defaultDir = QCoreApplication::applicationDirPath()); + + //获取选择的文件 + static QString getFileName(const QString &filter, QString defaultDir = QCoreApplication::applicationDirPath()); + + //获取选择的文件集合 + static QStringList getFileNames(const QString &filter, QString defaultDir = QCoreApplication::applicationDirPath()); + + //获取选择的目录 + static QString getFolderName(); + + //获取文件名,含拓展名 + static QString getFileNameWithExtension(const QString &strFilePath); + + //获取选择文件夹中的文件 + static QStringList getFolderFileNames(const QStringList &filter); + + //文件夹是否存在 + static bool folderIsExist(const QString &strFolder); + + //文件是否存在 + static bool fileIsExist(const QString &strFile); + + //复制文件 + static bool copyFile(const QString &sourceFile, const QString &targetFile); + + //删除文件夹下所有文件 + static void deleteDirectory(const QString &path); + + //判断IP地址及端口是否在线 + static bool ipLive(const QString &ip, int port, int timeout = 1000); + + //获取网页所有源代码 + static QString getHtml(const QString &url); + + //获取本机公网IP地址 + static QString getNetIP(const QString &webCode); + + //获取本机IP + static QString getLocalIP(); + + //Url地址转为IP地址 + static QString urlToIP(const QString &url); + + //判断是否通外网 + static bool isWebOk(); + + + //弹出消息框 + static void showMessageBoxInfo(const QString &info, int closeSec = 0, bool exec = false); + //弹出错误框 + static void showMessageBoxError(const QString &info, int closeSec = 0, bool exec = false); + //弹出询问框 + static int showMessageBoxQuestion(const QString &info); + + //弹出+隐藏右下角信息框 + static void showTipBox(const QString &title, const QString &tip, bool fullScreen = false, + bool center = true, int closeSec = 0); + static void hideTipBox(); + + //弹出输入框 + static QString showInputBox(const QString &title, int type = 0, int closeSec = 0, + const QString &placeholderText = QString(), bool pwd = false, + const QString &defaultValue = QString()); + + //弹出日期选择框 + static void showDateSelect(QString &dateStart, QString &dateEnd, const QString &format = "yyyy-MM-dd"); + + + //设置按钮样式 + static QString setPushButtonQss(QPushButton *btn, //按钮对象 + int radius = 5, //圆角半径 + int padding = 8, //间距 + const QString &normalColor = "#34495E", //正常颜色 + const QString &normalTextColor = "#FFFFFF", //文字颜色 + const QString &hoverColor = "#4E6D8C", //悬停颜色 + const QString &hoverTextColor = "#F0F0F0", //悬停文字颜色 + const QString &pressedColor = "#2D3E50", //按下颜色 + const QString &pressedTextColor = "#B8C6D1"); //按下文字颜色 + + //设置文本框样式 + static QString setLineEditQss(QLineEdit *txt, //文本框对象 + int radius = 3, //圆角半径 + int borderWidth = 2, //边框大小 + const QString &normalColor = "#DCE4EC", //正常颜色 + const QString &focusColor = "#34495E"); //选中颜色 + + //设置进度条样式 + static QString setProgressBarQss(QProgressBar *bar, + int barHeight = 8, //进度条高度 + int barRadius = 5, //进度条半径 + int fontSize = 9, //文字字号 + const QString &normalColor = "#E8EDF2", //正常颜色 + const QString &chunkColor = "#E74C3C"); //进度颜色 + + //设置滑块条样式 + static QString setSliderQss(QSlider *slider, //滑动条对象 + int sliderHeight = 8, //滑动条高度 + const QString &normalColor = "#E8EDF2", //正常颜色 + const QString &grooveColor = "#1ABC9C", //滑块颜色 + const QString &handleBorderColor = "#1ABC9C", //指示器边框颜色 + const QString &handleColor = "#FFFFFF", //指示器颜色 + const QString &textColor = "#000000"); //文字颜色 + + //设置单选框样式 + static QString setRadioButtonQss(QRadioButton *rbtn, //单选框对象 + int indicatorRadius = 8, //指示器圆角角度 + const QString &normalColor = "#D7DBDE", //正常颜色 + const QString &checkColor = "#34495E"); //选中颜色 + + //设置滚动条样式 + static QString setScrollBarQss(QWidget *scroll, //滚动条对象 + int radius = 6, //圆角角度 + int min = 120, //指示器最小长度 + int max = 12, //滚动条最大长度 + const QString &bgColor = "#606060", //背景色 + const QString &handleNormalColor = "#34495E", //指示器正常颜色 + const QString &handleHoverColor = "#1ABC9C", //指示器悬停颜色 + const QString &handlePressedColor = "#E74C3C"); //指示器按下颜色 +}; + +//全局变量控制 +class QUIConfig +{ +public: + //全局图标 + static QChar IconMain; //标题栏左上角图标 + static QChar IconMenu; //下拉菜单图标 + static QChar IconMin; //最小化图标 + static QChar IconMax; //最大化图标 + static QChar IconNormal; //还原图标 + static QChar IconClose; //关闭图标 + + static QString FontName; //全局字体名称 + static int FontSize; //全局字体大小 + + //样式表颜色值 + static QString TextColor; //文字颜色 + static QString PanelColor; //面板颜色 + static QString BorderColor; //边框颜色 + static QString NormalColorStart;//正常状态开始颜色 + static QString NormalColorEnd; //正常状态结束颜色 + static QString DarkColorStart; //加深状态开始颜色 + static QString DarkColorEnd; //加深状态结束颜色 + static QString HighColor; //高亮颜色 +}; + +#endif // QUIWIDGET_H diff --git a/comtool/comtool.pro b/comtool/comtool.pro new file mode 100644 index 0000000..f6ca6b5 --- /dev/null +++ b/comtool/comtool.pro @@ -0,0 +1,36 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2016-09-19T22:25:56 +# +#------------------------------------------------- + +QT += core gui network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = comtool +TEMPLATE = app +MOC_DIR = temp/moc +RCC_DIR = temp/rcc +UI_DIR = temp/ui +OBJECTS_DIR = temp/obj +DESTDIR = bin +RC_FILE = other/main.rc + +SOURCES += main.cpp +HEADERS += head.h +RESOURCES += other/main.qrc +CONFIG += warn_off + +include ($$PWD/api/api.pri) +include ($$PWD/form/form.pri) +include ($$PWD/qextserialport/qextserialport.pri) + +INCLUDEPATH += $$PWD +INCLUDEPATH += $$PWD/api +INCLUDEPATH += $$PWD/form +INCLUDEPATH += $$PWD/qextserialport + +#INCLUDEPATH += /usr/local/openssl-1.0.2m-h3-gcc-4.9.2/include +#LIBS += -L/usr/local/openssl-1.0.2m-h3-gcc-4.9.2/lib -lssl -lcrypto +#LIBS += -L/usr/local/h3_rootfsv -lXdmcp diff --git a/comtool/file/device.txt b/comtool/file/device.txt new file mode 100644 index 0000000..bb54788 --- /dev/null +++ b/comtool/file/device.txt @@ -0,0 +1 @@ +01 03 00 00 00 06 C5 C8;01 03 0C 00 01 00 01 00 00 00 01 00 01 00 01 37 DC \ No newline at end of file diff --git a/comtool/file/send.txt b/comtool/file/send.txt new file mode 100644 index 0000000..51b3944 --- /dev/null +++ b/comtool/file/send.txt @@ -0,0 +1,17 @@ +16 FF 01 01 E0 E1 +16 FF 01 01 E1 E2 +16 01 02 DF BC 16 01 02 DF BC 16 01 02 DF BC 12 13 14 15 +16 00 00 04 D0 F0 F1 65 C4 +16 00 00 04 D0 05 AB 5A C4 +16 01 10 02 F0 03 06 16 01 11 02 F0 03 06 16 01 12 02 F0 03 06 16 01 13 02 F0 03 06 16 01 +14 02 F0 03 06 16 01 15 02 F0 03 06 16 01 16 02 F0 03 06 +16 11 01 03 E8 01 10 0E +16 11 01 03 E8 01 12 10 +16 11 01 03 E8 01 14 12 +16 11 01 03 E8 01 15 13 +DISARMEDALL +BURGLARY 012 +BYPASS 012 +DISARMED 012 +16 00 01 01 D1 D3 +16 01 11 11 \ No newline at end of file diff --git a/comtool/form/form.pri b/comtool/form/form.pri new file mode 100644 index 0000000..c4b3563 --- /dev/null +++ b/comtool/form/form.pri @@ -0,0 +1,8 @@ +FORMS += \ + $$PWD/frmcomtool.ui + +HEADERS += \ + $$PWD/frmcomtool.h + +SOURCES += \ + $$PWD/frmcomtool.cpp diff --git a/comtool/form/frmcomtool.cpp b/comtool/form/frmcomtool.cpp new file mode 100644 index 0000000..2cd21af --- /dev/null +++ b/comtool/form/frmcomtool.cpp @@ -0,0 +1,584 @@ +#include "frmcomtool.h" +#include "ui_frmcomtool.h" +#include "quiwidget.h" + +frmComTool::frmComTool(QWidget *parent) : QWidget(parent), ui(new Ui::frmComTool) +{ + ui->setupUi(this); + this->initForm(); + this->initConfig(); + QUIHelper::setFormInCenter(this); +} + +frmComTool::~frmComTool() +{ + delete ui; +} + +void frmComTool::initForm() +{ + comOk = false; + com = 0; + sleepTime = 10; + receiveCount = 0; + sendCount = 0; + isShow = true; + + ui->cboxSendInterval->addItems(App::Intervals); + ui->cboxData->addItems(App::Datas); + + //读取数据 + timerRead = new QTimer(this); + timerRead->setInterval(100); + connect(timerRead, SIGNAL(timeout()), this, SLOT(readData())); + + //发送数据 + timerSend = new QTimer(this); + connect(timerSend, SIGNAL(timeout()), this, SLOT(sendData())); + connect(ui->btnSend, SIGNAL(clicked()), this, SLOT(sendData())); + + //保存数据 + timerSave = new QTimer(this); + connect(timerSave, SIGNAL(timeout()), this, SLOT(saveData())); + connect(ui->btnSave, SIGNAL(clicked()), this, SLOT(saveData())); + + ui->tabWidget->setCurrentIndex(0); + changeEnable(false); + + tcpOk = false; + socket = new QTcpSocket(this); + socket->abort(); + connect(socket, SIGNAL(readyRead()), this, SLOT(readDataNet())); + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(readErrorNet())); + + timerConnect = new QTimer(this); + connect(timerConnect, SIGNAL(timeout()), this, SLOT(connectNet())); + timerConnect->setInterval(3000); + timerConnect->start(); + +#ifdef __arm__ + ui->widgetRight->setFixedWidth(280); +#endif +} + +void frmComTool::initConfig() +{ + QStringList comList; + for (int i = 1; i <= 20; i++) { + comList << QString("COM%1").arg(i); + } + + comList << "ttyUSB0" << "ttyS0" << "ttyS1" << "ttyS2" << "ttyS3" << "ttyS4"; + comList << "ttymxc1" << "ttymxc2" << "ttymxc3" << "ttymxc4"; + comList << "ttySAC1" << "ttySAC2" << "ttySAC3" << "ttySAC4"; + ui->cboxPortName->addItems(comList); + ui->cboxPortName->setCurrentIndex(ui->cboxPortName->findText(App::PortName)); + connect(ui->cboxPortName, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + QStringList baudList; + baudList << "50" << "75" << "100" << "134" << "150" << "200" << "300" << "600" << "1200" + << "1800" << "2400" << "4800" << "9600" << "14400" << "19200" << "38400" + << "56000" << "57600" << "76800" << "115200" << "128000" << "256000"; + + ui->cboxBaudRate->addItems(baudList); + ui->cboxBaudRate->setCurrentIndex(ui->cboxBaudRate->findText(QString::number(App::BaudRate))); + connect(ui->cboxBaudRate, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + QStringList dataBitsList; + dataBitsList << "5" << "6" << "7" << "8"; + + ui->cboxDataBit->addItems(dataBitsList); + ui->cboxDataBit->setCurrentIndex(ui->cboxDataBit->findText(QString::number(App::DataBit))); + connect(ui->cboxDataBit, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + QStringList parityList; + parityList << "无" << "奇" << "偶"; +#ifdef Q_OS_WIN + parityList << "标志"; +#endif + parityList << "空格"; + + ui->cboxParity->addItems(parityList); + ui->cboxParity->setCurrentIndex(ui->cboxParity->findText(App::Parity)); + connect(ui->cboxParity, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + QStringList stopBitsList; + stopBitsList << "1"; +#ifdef Q_OS_WIN + stopBitsList << "1.5"; +#endif + stopBitsList << "2"; + + ui->cboxStopBit->addItems(stopBitsList); + ui->cboxStopBit->setCurrentIndex(ui->cboxStopBit->findText(QString::number(App::StopBit))); + connect(ui->cboxStopBit, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + ui->ckHexSend->setChecked(App::HexSend); + connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig())); + + ui->ckHexReceive->setChecked(App::HexReceive); + connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig())); + + ui->ckDebug->setChecked(App::Debug); + connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig())); + + ui->ckAutoClear->setChecked(App::AutoClear); + connect(ui->ckAutoClear, SIGNAL(stateChanged(int)), this, SLOT(saveConfig())); + + ui->ckAutoSend->setChecked(App::AutoSend); + connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig())); + + ui->ckAutoSave->setChecked(App::AutoSave); + connect(ui->ckAutoSave, SIGNAL(stateChanged(int)), this, SLOT(saveConfig())); + + QStringList sendInterval; + QStringList saveInterval; + sendInterval << "100" << "300" << "500"; + + for (int i = 1000; i <= 10000; i = i + 1000) { + sendInterval << QString::number(i); + saveInterval << QString::number(i); + } + + ui->cboxSendInterval->addItems(sendInterval); + ui->cboxSaveInterval->addItems(saveInterval); + + ui->cboxSendInterval->setCurrentIndex(ui->cboxSendInterval->findText(QString::number(App::SendInterval))); + connect(ui->cboxSendInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + ui->cboxSaveInterval->setCurrentIndex(ui->cboxSaveInterval->findText(QString::number(App::SaveInterval))); + connect(ui->cboxSaveInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + timerSend->setInterval(App::SendInterval); + timerSave->setInterval(App::SaveInterval); + + if (App::AutoSend) { + timerSend->start(); + } + + if (App::AutoSave) { + timerSave->start(); + } + + //串口转网络部分 + ui->cboxMode->setCurrentIndex(ui->cboxMode->findText(App::Mode)); + connect(ui->cboxMode, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + ui->txtServerIP->setText(App::ServerIP); + connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); + + ui->txtServerPort->setText(QString::number(App::ServerPort)); + connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); + + ui->txtListenPort->setText(QString::number(App::ListenPort)); + connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); + + QStringList values; + values << "0" << "10" << "50"; + + for (int i = 100; i < 1000; i = i + 100) { + values << QString("%1").arg(i); + } + + ui->cboxSleepTime->addItems(values); + + ui->cboxSleepTime->setCurrentIndex(ui->cboxSleepTime->findText(QString::number(App::SleepTime))); + connect(ui->cboxSleepTime, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); + + ui->ckAutoConnect->setChecked(App::AutoConnect); + connect(ui->ckAutoConnect, SIGNAL(stateChanged(int)), this, SLOT(saveConfig())); +} + +void frmComTool::saveConfig() +{ + App::PortName = ui->cboxPortName->currentText(); + App::BaudRate = ui->cboxBaudRate->currentText().toInt(); + App::DataBit = ui->cboxDataBit->currentText().toInt(); + App::Parity = ui->cboxParity->currentText(); + App::StopBit = ui->cboxStopBit->currentText().toDouble(); + + App::HexSend = ui->ckHexSend->isChecked(); + App::HexReceive = ui->ckHexReceive->isChecked(); + App::Debug = ui->ckDebug->isChecked(); + App::AutoClear = ui->ckAutoClear->isChecked(); + + App::AutoSend = ui->ckAutoSend->isChecked(); + App::AutoSave = ui->ckAutoSave->isChecked(); + + int sendInterval = ui->cboxSendInterval->currentText().toInt(); + if (sendInterval != App::SendInterval) { + App::SendInterval = sendInterval; + timerSend->setInterval(App::SendInterval); + } + + int saveInterval = ui->cboxSaveInterval->currentText().toInt(); + if (saveInterval != App::SaveInterval) { + App::SaveInterval = saveInterval; + timerSave->setInterval(App::SaveInterval); + } + + App::Mode = ui->cboxMode->currentText(); + App::ServerIP = ui->txtServerIP->text().trimmed(); + App::ServerPort = ui->txtServerPort->text().toInt(); + App::ListenPort = ui->txtListenPort->text().toInt(); + App::SleepTime = ui->cboxSleepTime->currentText().toInt(); + App::AutoConnect = ui->ckAutoConnect->isChecked(); + + App::writeConfig(); +} + +void frmComTool::changeEnable(bool b) +{ + ui->cboxBaudRate->setEnabled(!b); + ui->cboxDataBit->setEnabled(!b); + ui->cboxParity->setEnabled(!b); + ui->cboxPortName->setEnabled(!b); + ui->cboxStopBit->setEnabled(!b); + ui->btnSend->setEnabled(b); + ui->ckAutoSend->setEnabled(b); + ui->ckAutoSave->setEnabled(b); +} + +void frmComTool::append(int type, const QString &data, bool clear) +{ + static int currentCount = 0; + static int maxCount = 100; + + if (clear) { + ui->txtMain->clear(); + currentCount = 0; + return; + } + + if (currentCount >= maxCount) { + ui->txtMain->clear(); + currentCount = 0; + } + + if (!isShow) { + return; + } + + //过滤回车换行符 + QString strData = data; + strData = strData.replace("\r", ""); + strData = strData.replace("\n", ""); + + //不同类型不同颜色显示 + QString strType; + if (type == 0) { + strType = "串口发送 >>"; + ui->txtMain->setTextColor(QColor("dodgerblue")); + } else if (type == 1) { + strType = "串口接收 <<"; + ui->txtMain->setTextColor(QColor("red")); + } else if (type == 2) { + strType = "处理延时 >>"; + ui->txtMain->setTextColor(QColor("gray")); + } else if (type == 3) { + strType = "正在校验 >>"; + ui->txtMain->setTextColor(QColor("green")); + } else if (type == 4) { + strType = "网络发送 >>"; + ui->txtMain->setTextColor(QColor(24, 189, 155)); + } else if (type == 5) { + strType = "网络接收 <<"; + ui->txtMain->setTextColor(QColor(255, 107, 107)); + } else if (type == 6) { + strType = "提示信息 >>"; + ui->txtMain->setTextColor(QColor(100, 184, 255)); + } + + strData = QString("时间[%1] %2 %3").arg(TIMEMS).arg(strType).arg(strData); + ui->txtMain->append(strData); + currentCount++; +} + +void frmComTool::readData() +{ + if (com->bytesAvailable() <= 0) { + return; + } + + QUIHelper::sleep(sleepTime); + QByteArray data = com->readAll(); + int dataLen = data.length(); + if (dataLen <= 0) { + return; + } + + if (isShow) { + QString buffer; + if (ui->ckHexReceive->isChecked()) { + buffer = QUIHelper::byteArrayToHexStr(data); + } else { + //buffer = QUIHelper::byteArrayToAsciiStr(data); + buffer = QString::fromLocal8Bit(data); + } + + //启用调试则模拟调试数据 + if (ui->ckDebug->isChecked()) { + int count = App::Keys.count(); + for (int i = 0; i < count; i++) { + if (buffer.startsWith(App::Keys.at(i))) { + sendData(App::Values.at(i)); + break; + } + } + } + + append(1, buffer); + receiveCount = receiveCount + data.size(); + ui->btnReceiveCount->setText(QString("接收 : %1 字节").arg(receiveCount)); + + //启用网络转发则调用网络发送数据 + if (tcpOk) { + socket->write(data); + append(4, QString(buffer)); + } + } +} + +void frmComTool::sendData() +{ + QString str = ui->cboxData->currentText(); + if (str.isEmpty()) { + ui->cboxData->setFocus(); + return; + } + + sendData(str); + + if (ui->ckAutoClear->isChecked()) { + ui->cboxData->setCurrentIndex(-1); + ui->cboxData->setFocus(); + } +} + +void frmComTool::sendData(QString data) +{ + if (com == 0 || !com->isOpen()) { + return; + } + + //短信猫调试 + if (data.startsWith("AT")) { + data += "\r"; + } + + QByteArray buffer; + + if (ui->ckHexSend->isChecked()) { + buffer = QUIHelper::hexStrToByteArray(data); + } else { + buffer = QUIHelper::asciiStrToByteArray(data); + } + + com->write(buffer); + append(0, data); + sendCount = sendCount + buffer.size(); + ui->btnSendCount->setText(QString("发送 : %1 字节").arg(sendCount)); +} + +void frmComTool::saveData() +{ + QString tempData = ui->txtMain->toPlainText(); + + if (tempData == "") { + return; + } + + QDateTime now = QDateTime::currentDateTime(); + QString name = now.toString("yyyy-MM-dd-HH-mm-ss"); + QString fileName = QString("%1/%2.txt").arg(QUIHelper::appPath()).arg(name); + + QFile file(fileName); + file.open(QFile::WriteOnly | QIODevice::Text); + QTextStream out(&file); + out << tempData; + file.close(); + + on_btnClear_clicked(); +} + +void frmComTool::on_btnOpen_clicked() +{ + if (ui->btnOpen->text() == "打开串口") { + com = new QextSerialPort(ui->cboxPortName->currentText(), QextSerialPort::Polling); + comOk = com->open(QIODevice::ReadWrite); + + if (comOk) { + //清空缓冲区 + com->flush(); + //设置波特率 + com->setBaudRate((BaudRateType)ui->cboxBaudRate->currentText().toInt()); + //设置数据位 + com->setDataBits((DataBitsType)ui->cboxDataBit->currentText().toInt()); + //设置校验位 + com->setParity((ParityType)ui->cboxParity->currentIndex()); + //设置停止位 + com->setStopBits((StopBitsType)ui->cboxStopBit->currentIndex()); + com->setFlowControl(FLOW_OFF); + com->setTimeout(10); + + changeEnable(true); + ui->btnOpen->setText("关闭串口"); + timerRead->start(); + } + } else { + timerRead->stop(); + com->close(); + com->deleteLater(); + + changeEnable(false); + ui->btnOpen->setText("打开串口"); + on_btnClear_clicked(); + comOk = false; + } +} + +void frmComTool::on_btnSendCount_clicked() +{ + sendCount = 0; + ui->btnSendCount->setText("发送 : 0 字节"); +} + +void frmComTool::on_btnReceiveCount_clicked() +{ + receiveCount = 0; + ui->btnReceiveCount->setText("接收 : 0 字节"); +} + +void frmComTool::on_btnStopShow_clicked() +{ + if (ui->btnStopShow->text() == "停止显示") { + isShow = false; + ui->btnStopShow->setText("开始显示"); + } else { + isShow = true; + ui->btnStopShow->setText("停止显示"); + } +} + +void frmComTool::on_btnData_clicked() +{ + QString fileName = QString("%1/%2").arg(QUIHelper::appPath()).arg("send.txt"); + QFile file(fileName); + if (!file.exists()) { + return; + } + + if (ui->btnData->text() == "管理数据") { + ui->txtMain->setReadOnly(false); + ui->txtMain->clear(); + file.open(QFile::ReadOnly | QIODevice::Text); + QTextStream in(&file); + ui->txtMain->setText(in.readAll()); + file.close(); + ui->btnData->setText("保存数据"); + } else { + ui->txtMain->setReadOnly(true); + file.open(QFile::WriteOnly | QIODevice::Text); + QTextStream out(&file); + out << ui->txtMain->toPlainText(); + file.close(); + ui->txtMain->clear(); + ui->btnData->setText("管理数据"); + App::readSendData(); + } +} + +void frmComTool::on_btnClear_clicked() +{ + append(0, "", true); +} + +void frmComTool::on_btnStart_clicked() +{ + if (ui->btnStart->text() == "启动") { + if (App::ServerIP == "" || App::ServerPort == 0) { + append(6, "IP地址和远程端口不能为空"); + return; + } + + socket->connectToHost(App::ServerIP, App::ServerPort); + if (socket->waitForConnected(100)) { + ui->btnStart->setText("停止"); + append(6, "连接服务器成功"); + tcpOk = true; + } + } else { + socket->disconnectFromHost(); + if (socket->state() == QAbstractSocket::UnconnectedState || socket->waitForDisconnected(100)) { + ui->btnStart->setText("启动"); + append(6, "断开服务器成功"); + tcpOk = false; + } + } +} + +void frmComTool::on_ckAutoSend_stateChanged(int arg1) +{ + if (arg1 == 0) { + ui->cboxSendInterval->setEnabled(false); + timerSend->stop(); + } else { + ui->cboxSendInterval->setEnabled(true); + timerSend->start(); + } +} + +void frmComTool::on_ckAutoSave_stateChanged(int arg1) +{ + if (arg1 == 0) { + ui->cboxSaveInterval->setEnabled(false); + timerSave->stop(); + } else { + ui->cboxSaveInterval->setEnabled(true); + timerSave->start(); + } +} + +void frmComTool::connectNet() +{ + if (!tcpOk && App::AutoConnect && ui->btnStart->text() == "启动") { + if (App::ServerIP != "" && App::ServerPort != 0) { + socket->connectToHost(App::ServerIP, App::ServerPort); + if (socket->waitForConnected(100)) { + ui->btnStart->setText("停止"); + append(6, "连接服务器成功"); + tcpOk = true; + } + } + } +} + +void frmComTool::readDataNet() +{ + if (socket->bytesAvailable() > 0) { + QUIHelper::sleep(App::SleepTime); + QByteArray data = socket->readAll(); + + QString buffer; + if (ui->ckHexReceive->isChecked()) { + buffer = QUIHelper::byteArrayToHexStr(data); + } else { + buffer = QUIHelper::byteArrayToAsciiStr(data); + } + + append(5, buffer); + + //将收到的网络数据转发给串口 + if (comOk) { + com->write(data); + append(0, buffer); + } + } +} + +void frmComTool::readErrorNet() +{ + ui->btnStart->setText("启动"); + append(6, QString("连接服务器失败,%1").arg(socket->errorString())); + socket->disconnectFromHost(); + tcpOk = false; +} diff --git a/comtool/form/frmcomtool.h b/comtool/form/frmcomtool.h new file mode 100644 index 0000000..8ba400c --- /dev/null +++ b/comtool/form/frmcomtool.h @@ -0,0 +1,68 @@ +#ifndef FRMCOMTOOL_H +#define FRMCOMTOOL_H + +#include +#include "qtcpsocket.h" +#include "qextserialport.h" + +namespace Ui +{ +class frmComTool; +} + +class frmComTool : public QWidget +{ + Q_OBJECT + +public: + explicit frmComTool(QWidget *parent = 0); + ~frmComTool(); + +private: + Ui::frmComTool *ui; + + bool comOk; //串口是否打开 + QextSerialPort *com; //串口通信对象 + QTimer *timerRead; //定时读取串口数据 + QTimer *timerSend; //定时发送串口数据 + QTimer *timerSave; //定时保存串口数据 + + int sleepTime; //接收延时时间 + int sendCount; //发送数据计数 + int receiveCount; //接收数据计数 + bool isShow; //是否显示数据 + + bool tcpOk; //网络是否正常 + QTcpSocket *socket; //网络连接对象 + QTimer *timerConnect; //定时器重连 + +private slots: + void initForm(); //初始化窗体数据 + void initConfig(); //初始化配置文件 + void saveConfig(); //保存配置文件 + void readData(); //读取串口数据 + void sendData(); //发送串口数据 + void sendData(QString data);//发送串口数据带参数 + void saveData(); //保存串口数据 + + void changeEnable(bool b); //改变状态 + void append(int type, const QString &data, bool clear = false); + +private slots: + void connectNet(); + void readDataNet(); + void readErrorNet(); + +private slots: + void on_btnOpen_clicked(); + void on_btnStopShow_clicked(); + void on_btnSendCount_clicked(); + void on_btnReceiveCount_clicked(); + void on_btnClear_clicked(); + void on_btnData_clicked(); + void on_btnStart_clicked(); + void on_ckAutoSend_stateChanged(int arg1); + void on_ckAutoSave_stateChanged(int arg1); +}; + +#endif // FRMCOMTOOL_H diff --git a/comtool/form/frmcomtool.ui b/comtool/form/frmcomtool.ui new file mode 100644 index 0000000..4b6fe20 --- /dev/null +++ b/comtool/form/frmcomtool.ui @@ -0,0 +1,473 @@ + + + frmComTool + + + + 0 + 0 + 800 + 600 + + + + + + + true + + + QFrame::StyledPanel + + + Qt::ScrollBarAsNeeded + + + Qt::ScrollBarAsNeeded + + + true + + + + + + + + 200 + 16777215 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + QFrame::Box + + + QFrame::Sunken + + + + + + 串口号 + + + + + + + + 1 + 0 + + + + + + + + 波特率 + + + + + + + + 1 + 0 + + + + + + + + 数据位 + + + + + + + + 1 + 0 + + + + + + + + 校验位 + + + + + + + + 1 + 0 + + + + + + + + 停止位 + + + + + + + + 1 + 0 + + + + + + + + 打开串口 + + + + + + + + + + QTabWidget::South + + + 1 + + + + 串口配置 + + + + + + Hex发送 + + + + + + + Hex接收 + + + + + + + 模拟设备 + + + + + + + 自动清空 + + + + + + + + + 自动发送 + + + + + + + + 0 + 0 + + + + + + + + 自动保存 + + + + + + + + 0 + 0 + + + + + + + + + + 发送 : 0 字节 + + + + + + + 接收 : 0 字节 + + + + + + + 停止显示 + + + + + + + 保存数据 + + + + + + + 管理数据 + + + + + + + 清空数据 + + + + + + + Qt::Vertical + + + + 20 + 2 + + + + + + + + + 网络配置 + + + + + + + + 监听端口 + + + + + + + 远程地址 + + + + + + + + + + + + + + + + + 远程端口 + + + + + + + + + + + + + + 延时时间 + + + + + + + + + + 转换模式 + + + + + + + + Tcp_Client + + + + + Tcp_Server + + + + + Udp_Client + + + + + Udp_Server + + + + + + + + + + 启动 + + + + + + + Qt::Vertical + + + + 20 + 59 + + + + + + + + 自动重连网络 + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + true + + + false + + + + + + + + 80 + 0 + + + + + 80 + 16777215 + + + + 发送 + + + + + + + + + + + diff --git a/comtool/head.h b/comtool/head.h new file mode 100644 index 0000000..634b2a2 --- /dev/null +++ b/comtool/head.h @@ -0,0 +1,11 @@ +#include +#include +#include + +#if (QT_VERSION > QT_VERSION_CHECK(5,0,0)) +#include +#endif + +#include "app.h" + +#pragma execution_character_set("utf-8") diff --git a/comtool/main.cpp b/comtool/main.cpp new file mode 100644 index 0000000..1905cf4 --- /dev/null +++ b/comtool/main.cpp @@ -0,0 +1,31 @@ +#include "frmcomtool.h" +#include "quiwidget.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + a.setWindowIcon(QIcon(":/main.ico")); + + QFont font; + font.setFamily(QUIConfig::FontName); + font.setPixelSize(QUIConfig::FontSize); + a.setFont(font); + + //设置编码以及加载中文翻译文件 + QUIHelper::setCode(); + QUIHelper::setTranslator(":/qt_zh_CN.qm"); + QUIHelper::setTranslator(":/widgets.qm"); + QUIHelper::initRand(); + + App::Intervals << "1" << "10" << "20" << "50" << "100" << "200" << "300" << "500" << "1000" << "1500" << "2000" << "3000" << "5000" << "10000"; + App::ConfigFile = QString("%1/%2.ini").arg(QUIHelper::appPath()).arg(QUIHelper::appName()); + App::readConfig(); + App::readSendData(); + App::readDeviceData(); + + frmComTool w; + w.setWindowTitle("串口调试助手V2019 QQ: 517216493"); + w.show(); + + return a.exec(); +} diff --git a/comtool/other/main.ico b/comtool/other/main.ico new file mode 100644 index 0000000..3a69f0a Binary files /dev/null and b/comtool/other/main.ico differ diff --git a/comtool/other/main.qrc b/comtool/other/main.qrc new file mode 100644 index 0000000..91c2747 --- /dev/null +++ b/comtool/other/main.qrc @@ -0,0 +1,5 @@ + + + main.ico + + diff --git a/comtool/other/main.rc b/comtool/other/main.rc new file mode 100644 index 0000000..fc0d770 --- /dev/null +++ b/comtool/other/main.rc @@ -0,0 +1 @@ +IDI_ICON1 ICON DISCARDABLE "main.ico" \ No newline at end of file diff --git a/comtool/qextserialport/qextserialport.cpp b/comtool/qextserialport/qextserialport.cpp new file mode 100644 index 0000000..66e987c --- /dev/null +++ b/comtool/qextserialport/qextserialport.cpp @@ -0,0 +1,1095 @@ +/**************************************************************************** +** Copyright (c) 2000-2003 Wayne Roth +** Copyright (c) 2004-2007 Stefan Sander +** Copyright (c) 2007 Michal Policht +** Copyright (c) 2008 Brandon Fosdick +** Copyright (c) 2009-2010 Liam Staskawicz +** Copyright (c) 2011 Debao Zhang +** All right reserved. +** Web: http://code.google.com/p/qextserialport/ +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ + +#include "qextserialport.h" +#include "qextserialport_p.h" +#include +#include +#include +#include + +/*! + \class PortSettings + + \brief The PortSettings class contain port settings + + Structure to contain port settings. + + \code + BaudRateType BaudRate; + DataBitsType DataBits; + ParityType Parity; + StopBitsType StopBits; + FlowType FlowControl; + long Timeout_Millisec; + \endcode +*/ + +QextSerialPortPrivate::QextSerialPortPrivate(QextSerialPort *q) + : lock(QReadWriteLock::Recursive), q_ptr(q) +{ + lastErr = E_NO_ERROR; + settings.BaudRate = BAUD9600; + settings.Parity = PAR_NONE; + settings.FlowControl = FLOW_OFF; + settings.DataBits = DATA_8; + settings.StopBits = STOP_1; + settings.Timeout_Millisec = 10; + settingsDirtyFlags = DFE_ALL; + + platformSpecificInit(); +} + +QextSerialPortPrivate::~QextSerialPortPrivate() +{ + platformSpecificDestruct(); +} + +void QextSerialPortPrivate::setBaudRate(BaudRateType baudRate, bool update) +{ + switch (baudRate) { +#ifdef Q_OS_WIN + + //Windows Special + case BAUD14400: + case BAUD56000: + case BAUD128000: + case BAUD256000: + QESP_PORTABILITY_WARNING() << "QextSerialPort Portability Warning: POSIX does not support baudRate:" << baudRate; +#elif defined(Q_OS_UNIX) + + //Unix Special + case BAUD50: + case BAUD75: + case BAUD134: + case BAUD150: + case BAUD200: + case BAUD1800: +# ifdef B76800 + case BAUD76800: +# endif +# if defined(B230400) && defined(B4000000) + case BAUD230400: + case BAUD460800: + case BAUD500000: + case BAUD576000: + case BAUD921600: + case BAUD1000000: + case BAUD1152000: + case BAUD1500000: + case BAUD2000000: + case BAUD2500000: + case BAUD3000000: + case BAUD3500000: + case BAUD4000000: +# endif + QESP_PORTABILITY_WARNING() << "QextSerialPort Portability Warning: Windows does not support baudRate:" << baudRate; +#endif + + case BAUD110: + case BAUD300: + case BAUD600: + case BAUD1200: + case BAUD2400: + case BAUD4800: + case BAUD9600: + case BAUD19200: + case BAUD38400: + case BAUD57600: + case BAUD115200: +#if defined(Q_OS_WIN) || defined(Q_OS_MAC) + default: +#endif + settings.BaudRate = baudRate; + settingsDirtyFlags |= DFE_BaudRate; + + if (update && q_func()->isOpen()) { + updatePortSettings(); + } + + break; +#if !(defined(Q_OS_WIN) || defined(Q_OS_MAC)) + + default: + QESP_WARNING() << "QextSerialPort does not support baudRate:" << baudRate; +#endif + } +} + +void QextSerialPortPrivate::setParity(ParityType parity, bool update) +{ + switch (parity) { + case PAR_SPACE: + if (settings.DataBits == DATA_8) { +#ifdef Q_OS_WIN + QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems."); +#else + QESP_WARNING("Space parity with 8 data bits is not supported by POSIX systems."); +#endif + } + + break; + +#ifdef Q_OS_WIN + + /*mark parity - WINDOWS ONLY*/ + case PAR_MARK: + QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Mark parity is not supported by POSIX systems"); + break; +#endif + + case PAR_NONE: + case PAR_EVEN: + case PAR_ODD: + break; + + default: + QESP_WARNING() << "QextSerialPort does not support Parity:" << parity; + } + + settings.Parity = parity; + settingsDirtyFlags |= DFE_Parity; + + if (update && q_func()->isOpen()) { + updatePortSettings(); + } +} + +void QextSerialPortPrivate::setDataBits(DataBitsType dataBits, bool update) +{ + switch (dataBits) { + + case DATA_5: + if (settings.StopBits == STOP_2) { + QESP_WARNING("QextSerialPort: 5 Data bits cannot be used with 2 stop bits."); + } else { + settings.DataBits = dataBits; + settingsDirtyFlags |= DFE_DataBits; + } + + break; + + case DATA_6: +#ifdef Q_OS_WIN + if (settings.StopBits == STOP_1_5) { + QESP_WARNING("QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits."); + } else +#endif + { + settings.DataBits = dataBits; + settingsDirtyFlags |= DFE_DataBits; + } + + break; + + case DATA_7: +#ifdef Q_OS_WIN + if (settings.StopBits == STOP_1_5) { + QESP_WARNING("QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits."); + } else +#endif + { + settings.DataBits = dataBits; + settingsDirtyFlags |= DFE_DataBits; + } + + break; + + case DATA_8: +#ifdef Q_OS_WIN + if (settings.StopBits == STOP_1_5) { + QESP_WARNING("QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits."); + } else +#endif + { + settings.DataBits = dataBits; + settingsDirtyFlags |= DFE_DataBits; + } + + break; + + default: + QESP_WARNING() << "QextSerialPort does not support Data bits:" << dataBits; + } + + if (update && q_func()->isOpen()) { + updatePortSettings(); + } +} + +void QextSerialPortPrivate::setStopBits(StopBitsType stopBits, bool update) +{ + switch (stopBits) { + + /*one stop bit*/ + case STOP_1: + settings.StopBits = stopBits; + settingsDirtyFlags |= DFE_StopBits; + break; + +#ifdef Q_OS_WIN + + /*1.5 stop bits*/ + case STOP_1_5: + QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX."); + + if (settings.DataBits != DATA_5) { + QESP_WARNING("QextSerialPort: 1.5 stop bits can only be used with 5 data bits"); + } else { + settings.StopBits = stopBits; + settingsDirtyFlags |= DFE_StopBits; + } + + break; +#endif + + /*two stop bits*/ + case STOP_2: + if (settings.DataBits == DATA_5) { + QESP_WARNING("QextSerialPort: 2 stop bits cannot be used with 5 data bits"); + } else { + settings.StopBits = stopBits; + settingsDirtyFlags |= DFE_StopBits; + } + + break; + + default: + QESP_WARNING() << "QextSerialPort does not support stop bits: " << stopBits; + } + + if (update && q_func()->isOpen()) { + updatePortSettings(); + } +} + +void QextSerialPortPrivate::setFlowControl(FlowType flow, bool update) +{ + settings.FlowControl = flow; + settingsDirtyFlags |= DFE_Flow; + + if (update && q_func()->isOpen()) { + updatePortSettings(); + } +} + +void QextSerialPortPrivate::setTimeout(long millisec, bool update) +{ + settings.Timeout_Millisec = millisec; + settingsDirtyFlags |= DFE_TimeOut; + + if (update && q_func()->isOpen()) { + updatePortSettings(); + } +} + +void QextSerialPortPrivate::setPortSettings(const PortSettings &settings, bool update) +{ + setBaudRate(settings.BaudRate, false); + setDataBits(settings.DataBits, false); + setStopBits(settings.StopBits, false); + setParity(settings.Parity, false); + setFlowControl(settings.FlowControl, false); + setTimeout(settings.Timeout_Millisec, false); + settingsDirtyFlags = DFE_ALL; + + if (update && q_func()->isOpen()) { + updatePortSettings(); + } +} + + +void QextSerialPortPrivate::_q_canRead() +{ + qint64 maxSize = bytesAvailable_sys(); + + if (maxSize > 0) { + char *writePtr = readBuffer.reserve(size_t(maxSize)); + qint64 bytesRead = readData_sys(writePtr, maxSize); + + if (bytesRead < maxSize) { + readBuffer.chop(maxSize - bytesRead); + } + + Q_Q(QextSerialPort); + Q_EMIT q->readyRead(); + } +} + +/*! \class QextSerialPort + + \brief The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems. + + \section1 Usage + QextSerialPort offers both a polling and event driven API. Event driven + is typically easier to use, since you never have to worry about checking + for new data. + + \bold Example + \code + QextSerialPort *port = new QextSerialPort("COM1"); + connect(port, SIGNAL(readyRead()), myClass, SLOT(onDataAvailable())); + port->open(); + + void MyClass::onDataAvailable() + { + QByteArray data = port->readAll(); + processNewData(usbdata); + } + \endcode + + \section1 Compatibility + The user will be notified of errors and possible portability conflicts at run-time + by default. + + For example, if a application has used BAUD1800, when it is runing under unix, you + will get following message. + + \code + QextSerialPort Portability Warning: Windows does not support baudRate:1800 + \endcode + + This behavior can be turned off by defining macro QESP_NO_WARN (to turn off all warnings) + or QESP_NO_PORTABILITY_WARN (to turn off portability warnings) in the project. + + + \bold Author: Stefan Sander, Michal Policht, Brandon Fosdick, Liam Staskawicz, Debao Zhang +*/ + +/*! + \enum QextSerialPort::QueryMode + + This enum type specifies query mode used in a serial port: + + \value Polling + asynchronously read and write + \value EventDriven + synchronously read and write +*/ + +/*! + \fn void QextSerialPort::dsrChanged(bool status) + This signal is emitted whenever dsr line has changed its state. You may + use this signal to check if device is connected. + + \a status true when DSR signal is on, false otherwise. + */ + + +/*! + \fn QueryMode QextSerialPort::queryMode() const + Get query mode. + */ + +/*! + Default constructor. Note that the name of the device used by a QextSerialPort is dependent on + your OS. Possible naming conventions and their associated OS are: + + \code + + OS Constant Used By Naming Convention + ------------- ------------- ------------------------ + Q_OS_WIN Windows COM1, COM2 + Q_OS_IRIX SGI/IRIX /dev/ttyf1, /dev/ttyf2 + Q_OS_HPUX HP-UX /dev/tty1p0, /dev/tty2p0 + Q_OS_SOLARIS SunOS/Slaris /dev/ttya, /dev/ttyb + Q_OS_OSF Digital UNIX /dev/tty01, /dev/tty02 + Q_OS_FREEBSD FreeBSD /dev/ttyd0, /dev/ttyd1 + Q_OS_OPENBSD OpenBSD /dev/tty00, /dev/tty01 + Q_OS_LINUX Linux /dev/ttyS0, /dev/ttyS1 + /dev/ttyS0, /dev/ttyS1 + \endcode + + This constructor assigns the device name to the name of the first port on the specified system. + See the other constructors if you need to open a different port. Default \a mode is EventDriven. + As a subclass of QObject, \a parent can be specified. +*/ + +QextSerialPort::QextSerialPort(QextSerialPort::QueryMode mode, QObject *parent) + : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) +{ +#ifdef Q_OS_WIN + setPortName(QLatin1String("COM1")); + +#elif defined(Q_OS_IRIX) + setPortName(QLatin1String("/dev/ttyf1")); + +#elif defined(Q_OS_HPUX) + setPortName(QLatin1String("/dev/tty1p0")); + +#elif defined(Q_OS_SOLARIS) + setPortName(QLatin1String("/dev/ttya")); + +#elif defined(Q_OS_OSF) //formally DIGITAL UNIX + setPortName(QLatin1String("/dev/tty01")); + +#elif defined(Q_OS_FREEBSD) + setPortName(QLatin1String("/dev/ttyd1")); + +#elif defined(Q_OS_OPENBSD) + setPortName(QLatin1String("/dev/tty00")); + +#else + setPortName(QLatin1String("/dev/ttyS0")); +#endif + setQueryMode(mode); +} + +/*! + Constructs a serial port attached to the port specified by name. + \a name is the name of the device, which is windowsystem-specific, + e.g."COM1" or "/dev/ttyS0". \a mode +*/ +QextSerialPort::QextSerialPort(const QString &name, QextSerialPort::QueryMode mode, QObject *parent) + : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) +{ + setQueryMode(mode); + setPortName(name); +} + +/*! + Constructs a port with default name and specified \a settings. +*/ +QextSerialPort::QextSerialPort(const PortSettings &settings, QextSerialPort::QueryMode mode, QObject *parent) + : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) +{ + Q_D(QextSerialPort); + setQueryMode(mode); + d->setPortSettings(settings); +} + +/*! + Constructs a port with specified \a name , \a mode and \a settings. +*/ +QextSerialPort::QextSerialPort(const QString &name, const PortSettings &settings, QextSerialPort::QueryMode mode, QObject *parent) + : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) +{ + Q_D(QextSerialPort); + setPortName(name); + setQueryMode(mode); + d->setPortSettings(settings); +} + +/*! + Opens a serial port and sets its OpenMode to \a mode. + Note that this function does not specify which device to open. + Returns true if successful; otherwise returns false.This function has no effect + if the port associated with the class is already open. The port is also + configured to the current settings, as stored in the settings structure. +*/ +bool QextSerialPort::open(OpenMode mode) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (mode != QIODevice::NotOpen && !isOpen()) { + d->open_sys(mode); + } + + return isOpen(); +} + + +/*! \reimp + Closes a serial port. This function has no effect if the serial port associated with the class + is not currently open. +*/ +void QextSerialPort::close() +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (isOpen()) { + // Be a good QIODevice and call QIODevice::close() before really close() + // so the aboutToClose() signal is emitted at the proper time + QIODevice::close(); // mark ourselves as closed + d->close_sys(); + d->readBuffer.clear(); + } +} + +/*! + Flushes all pending I/O to the serial port. This function has no effect if the serial port + associated with the class is not currently open. +*/ +void QextSerialPort::flush() +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (isOpen()) { + d->flush_sys(); + } +} + +/*! \reimp + Returns the number of bytes waiting in the port's receive queue. This function will return 0 if + the port is not currently open, or -1 on error. +*/ +qint64 QextSerialPort::bytesAvailable() const +{ + QWriteLocker locker(&d_func()->lock); + + if (isOpen()) { + qint64 bytes = d_func()->bytesAvailable_sys(); + + if (bytes != -1) { + return bytes + d_func()->readBuffer.size() + + QIODevice::bytesAvailable(); + } else { + return -1; + } + } + + return 0; +} + +/*! \reimp + +*/ +bool QextSerialPort::canReadLine() const +{ + QReadLocker locker(&d_func()->lock); + return QIODevice::canReadLine() || d_func()->readBuffer.canReadLine(); +} + +/*! + * Set desired serial communication handling style. You may choose from polling + * or event driven approach. This function does nothing when port is open; to + * apply changes port must be reopened. + * + * In event driven approach read() and write() functions are acting + * asynchronously. They return immediately and the operation is performed in + * the background, so they doesn't freeze the calling thread. + * To determine when operation is finished, QextSerialPort runs separate thread + * and monitors serial port events. Whenever the event occurs, adequate signal + * is emitted. + * + * When polling is set, read() and write() are acting synchronously. Signals are + * not working in this mode and some functions may not be available. The advantage + * of polling is that it generates less overhead due to lack of signals emissions + * and it doesn't start separate thread to monitor events. + * + * Generally event driven approach is more capable and friendly, although some + * applications may need as low overhead as possible and then polling comes. + * + * \a mode query mode. + */ +void QextSerialPort::setQueryMode(QueryMode mode) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (mode != d->queryMode) { + d->queryMode = mode; + } +} + +/*! + Sets the \a name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0". +*/ +void QextSerialPort::setPortName(const QString &name) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + d->port = name; +} + +/*! + Returns the name set by setPortName(). +*/ +QString QextSerialPort::portName() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->port; +} + +QextSerialPort::QueryMode QextSerialPort::queryMode() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->queryMode; +} + +/*! + Reads all available data from the device, and returns it as a QByteArray. + This function has no way of reporting errors; returning an empty QByteArray() + can mean either that no data was currently available for reading, or that an error occurred. +*/ +QByteArray QextSerialPort::readAll() +{ + int avail = this->bytesAvailable(); + return (avail > 0) ? this->read(avail) : QByteArray(); +} + +/*! + Returns the baud rate of the serial port. For a list of possible return values see + the definition of the enum BaudRateType. +*/ +BaudRateType QextSerialPort::baudRate() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->settings.BaudRate; +} + +/*! + Returns the number of data bits used by the port. For a list of possible values returned by + this function, see the definition of the enum DataBitsType. +*/ +DataBitsType QextSerialPort::dataBits() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->settings.DataBits; +} + +/*! + Returns the type of parity used by the port. For a list of possible values returned by + this function, see the definition of the enum ParityType. +*/ +ParityType QextSerialPort::parity() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->settings.Parity; +} + +/*! + Returns the number of stop bits used by the port. For a list of possible return values, see + the definition of the enum StopBitsType. +*/ +StopBitsType QextSerialPort::stopBits() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->settings.StopBits; +} + +/*! + Returns the type of flow control used by the port. For a list of possible values returned + by this function, see the definition of the enum FlowType. +*/ +FlowType QextSerialPort::flowControl() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->settings.FlowControl; +} + +/*! + \reimp + Returns true if device is sequential, otherwise returns false. Serial port is sequential device + so this function always returns true. Check QIODevice::isSequential() documentation for more + information. +*/ +bool QextSerialPort::isSequential() const +{ + return true; +} + +/*! + Return the error number, or 0 if no error occurred. +*/ +ulong QextSerialPort::lastError() const +{ + QReadLocker locker(&d_func()->lock); + return d_func()->lastErr; +} + +/*! + Returns the line status as stored by the port function. This function will retrieve the states + of the following lines: DCD, CTS, DSR, and RI. On POSIX systems, the following additional lines + can be monitored: DTR, RTS, Secondary TXD, and Secondary RXD. The value returned is an unsigned + long with specific bits indicating which lines are high. The following constants should be used + to examine the states of individual lines: + + \code + Mask Line + ------ ---- + LS_CTS CTS + LS_DSR DSR + LS_DCD DCD + LS_RI RI + LS_RTS RTS (POSIX only) + LS_DTR DTR (POSIX only) + LS_ST Secondary TXD (POSIX only) + LS_SR Secondary RXD (POSIX only) + \endcode + + This function will return 0 if the port associated with the class is not currently open. +*/ +unsigned long QextSerialPort::lineStatus() +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (isOpen()) { + return d->lineStatus_sys(); + } + + return 0; +} + +/*! + Returns a human-readable description of the last device error that occurred. +*/ +QString QextSerialPort::errorString() +{ + Q_D(QextSerialPort); + QReadLocker locker(&d->lock); + + switch (d->lastErr) { + case E_NO_ERROR: + return tr("No Error has occurred"); + + case E_INVALID_FD: + return tr("Invalid file descriptor (port was not opened correctly)"); + + case E_NO_MEMORY: + return tr("Unable to allocate memory tables (POSIX)"); + + case E_CAUGHT_NON_BLOCKED_SIGNAL: + return tr("Caught a non-blocked signal (POSIX)"); + + case E_PORT_TIMEOUT: + return tr("Operation timed out (POSIX)"); + + case E_INVALID_DEVICE: + return tr("The file opened by the port is not a valid device"); + + case E_BREAK_CONDITION: + return tr("The port detected a break condition"); + + case E_FRAMING_ERROR: + return tr("The port detected a framing error (usually caused by incorrect baud rate settings)"); + + case E_IO_ERROR: + return tr("There was an I/O error while communicating with the port"); + + case E_BUFFER_OVERRUN: + return tr("Character buffer overrun"); + + case E_RECEIVE_OVERFLOW: + return tr("Receive buffer overflow"); + + case E_RECEIVE_PARITY_ERROR: + return tr("The port detected a parity error in the received data"); + + case E_TRANSMIT_OVERFLOW: + return tr("Transmit buffer overflow"); + + case E_READ_FAILED: + return tr("General read operation failure"); + + case E_WRITE_FAILED: + return tr("General write operation failure"); + + case E_FILE_NOT_FOUND: + return tr("The %1 file doesn't exists").arg(this->portName()); + + case E_PERMISSION_DENIED: + return tr("Permission denied"); + + case E_AGAIN: + return tr("Device is already locked"); + + default: + return tr("Unknown error: %1").arg(d->lastErr); + } +} + +/*! + Destructs the QextSerialPort object. +*/ +QextSerialPort::~QextSerialPort() +{ + if (isOpen()) { + close(); + } + + delete d_ptr; +} + +/*! + Sets the flow control used by the port to \a flow. Possible values of flow are: + \code + FLOW_OFF No flow control + FLOW_HARDWARE Hardware (RTS/CTS) flow control + FLOW_XONXOFF Software (XON/XOFF) flow control + \endcode +*/ +void QextSerialPort::setFlowControl(FlowType flow) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (d->settings.FlowControl != flow) { + d->setFlowControl(flow, true); + } +} + +/*! + Sets the parity associated with the serial port to \a parity. The possible values of parity are: + \code + PAR_SPACE Space Parity + PAR_MARK Mark Parity + PAR_NONE No Parity + PAR_EVEN Even Parity + PAR_ODD Odd Parity + \endcode +*/ +void QextSerialPort::setParity(ParityType parity) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (d->settings.Parity != parity) { + d->setParity(parity, true); + } +} + +/*! + Sets the number of data bits used by the serial port to \a dataBits. Possible values of dataBits are: + \code + DATA_5 5 data bits + DATA_6 6 data bits + DATA_7 7 data bits + DATA_8 8 data bits + \endcode + + \bold note: + This function is subject to the following restrictions: + \list + \o 5 data bits cannot be used with 2 stop bits. + \o 1.5 stop bits can only be used with 5 data bits. + \o 8 data bits cannot be used with space parity on POSIX systems. + \endlist + */ +void QextSerialPort::setDataBits(DataBitsType dataBits) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (d->settings.DataBits != dataBits) { + d->setDataBits(dataBits, true); + } +} + +/*! + Sets the number of stop bits used by the serial port to \a stopBits. Possible values of stopBits are: + \code + STOP_1 1 stop bit + STOP_1_5 1.5 stop bits + STOP_2 2 stop bits + \endcode + + \bold note: + This function is subject to the following restrictions: + \list + \o 2 stop bits cannot be used with 5 data bits. + \o 1.5 stop bits cannot be used with 6 or more data bits. + \o POSIX does not support 1.5 stop bits. + \endlist +*/ +void QextSerialPort::setStopBits(StopBitsType stopBits) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (d->settings.StopBits != stopBits) { + d->setStopBits(stopBits, true); + } +} + +/*! + Sets the baud rate of the serial port to \a baudRate. Note that not all rates are applicable on + all platforms. The following table shows translations of the various baud rate + constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an * + are speeds that are usable on both Windows and POSIX. + \code + + RATE Windows Speed POSIX Speed + ----------- ------------- ----------- + BAUD50 X 50 + BAUD75 X 75 + *BAUD110 110 110 + BAUD134 X 134.5 + BAUD150 X 150 + BAUD200 X 200 + *BAUD300 300 300 + *BAUD600 600 600 + *BAUD1200 1200 1200 + BAUD1800 X 1800 + *BAUD2400 2400 2400 + *BAUD4800 4800 4800 + *BAUD9600 9600 9600 + BAUD14400 14400 X + *BAUD19200 19200 19200 + *BAUD38400 38400 38400 + BAUD56000 56000 X + *BAUD57600 57600 57600 + BAUD76800 X 76800 + *BAUD115200 115200 115200 + BAUD128000 128000 X + BAUD230400 X 230400 + BAUD256000 256000 X + BAUD460800 X 460800 + BAUD500000 X 500000 + BAUD576000 X 576000 + BAUD921600 X 921600 + BAUD1000000 X 1000000 + BAUD1152000 X 1152000 + BAUD1500000 X 1500000 + BAUD2000000 X 2000000 + BAUD2500000 X 2500000 + BAUD3000000 X 3000000 + BAUD3500000 X 3500000 + BAUD4000000 X 4000000 + \endcode +*/ + +void QextSerialPort::setBaudRate(BaudRateType baudRate) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (d->settings.BaudRate != baudRate) { + d->setBaudRate(baudRate, true); + } +} + +/*! + For Unix: + + Sets the read and write timeouts for the port to \a millisec milliseconds. + Note that this is a per-character timeout, i.e. the port will wait this long for each + individual character, not for the whole read operation. This timeout also applies to the + bytesWaiting() function. + + \bold note: + POSIX does not support millisecond-level control for I/O timeout values. Any + timeout set using this function will be set to the next lowest tenth of a second for + the purposes of detecting read or write timeouts. For example a timeout of 550 milliseconds + will be seen by the class as a timeout of 500 milliseconds for the purposes of reading and + writing the port. However millisecond-level control is allowed by the select() system call, + so for example a 550-millisecond timeout will be seen as 550 milliseconds on POSIX systems for + the purpose of detecting available bytes in the read buffer. + + For Windows: + + Sets the read and write timeouts for the port to \a millisec milliseconds. + Setting 0 indicates that timeouts are not used for read nor write operations; + however read() and write() functions will still block. Set -1 to provide + non-blocking behaviour (read() and write() will return immediately). + + \bold note: this function does nothing in event driven mode. +*/ +void QextSerialPort::setTimeout(long millisec) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (d->settings.Timeout_Millisec != millisec) { + d->setTimeout(millisec, true); + } +} + +/*! + Sets DTR line to the requested state (\a set default to high). This function will have no effect if + the port associated with the class is not currently open. +*/ +void QextSerialPort::setDtr(bool set) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (isOpen()) { + d->setDtr_sys(set); + } +} + +/*! + Sets RTS line to the requested state \a set (high by default). + This function will have no effect if + the port associated with the class is not currently open. +*/ +void QextSerialPort::setRts(bool set) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + + if (isOpen()) { + d->setRts_sys(set); + } +} + +/*! \reimp + Reads a block of data from the serial port. This function will read at most maxlen bytes from + the serial port and place them in the buffer pointed to by data. Return value is the number of + bytes actually read, or -1 on error. + + \warning before calling this function ensure that serial port associated with this class + is currently open (use isOpen() function to check if port is open). +*/ +qint64 QextSerialPort::readData(char *data, qint64 maxSize) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + qint64 bytesFromBuffer = 0; + + if (!d->readBuffer.isEmpty()) { + bytesFromBuffer = d->readBuffer.read(data, maxSize); + + if (bytesFromBuffer == maxSize) { + return bytesFromBuffer; + } + } + + qint64 bytesFromDevice = d->readData_sys(data + bytesFromBuffer, maxSize - bytesFromBuffer); + + if (bytesFromDevice < 0) { + return -1; + } + + return bytesFromBuffer + bytesFromDevice; +} + +/*! \reimp + Writes a block of data to the serial port. This function will write len bytes + from the buffer pointed to by data to the serial port. Return value is the number + of bytes actually written, or -1 on error. + + \warning before calling this function ensure that serial port associated with this class + is currently open (use isOpen() function to check if port is open). +*/ +qint64 QextSerialPort::writeData(const char *data, qint64 maxSize) +{ + Q_D(QextSerialPort); + QWriteLocker locker(&d->lock); + return d->writeData_sys(data, maxSize); +} + +#include "moc_qextserialport.cpp" diff --git a/comtool/qextserialport/qextserialport.h b/comtool/qextserialport/qextserialport.h new file mode 100644 index 0000000..f60e14a --- /dev/null +++ b/comtool/qextserialport/qextserialport.h @@ -0,0 +1,234 @@ +/**************************************************************************** +** Copyright (c) 2000-2003 Wayne Roth +** Copyright (c) 2004-2007 Stefan Sander +** Copyright (c) 2007 Michal Policht +** Copyright (c) 2008 Brandon Fosdick +** Copyright (c) 2009-2010 Liam Staskawicz +** Copyright (c) 2011 Debao Zhang +** All right reserved. +** Web: http://code.google.com/p/qextserialport/ +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ + +#ifndef _QEXTSERIALPORT_H_ +#define _QEXTSERIALPORT_H_ + +#include +#include "qextserialport_global.h" +#ifdef Q_OS_UNIX +#include +#endif +/*line status constants*/ +// ### QESP2.0 move to enum +#define LS_CTS 0x01 +#define LS_DSR 0x02 +#define LS_DCD 0x04 +#define LS_RI 0x08 +#define LS_RTS 0x10 +#define LS_DTR 0x20 +#define LS_ST 0x40 +#define LS_SR 0x80 + +/*error constants*/ +// ### QESP2.0 move to enum +#define E_NO_ERROR 0 +#define E_INVALID_FD 1 +#define E_NO_MEMORY 2 +#define E_CAUGHT_NON_BLOCKED_SIGNAL 3 +#define E_PORT_TIMEOUT 4 +#define E_INVALID_DEVICE 5 +#define E_BREAK_CONDITION 6 +#define E_FRAMING_ERROR 7 +#define E_IO_ERROR 8 +#define E_BUFFER_OVERRUN 9 +#define E_RECEIVE_OVERFLOW 10 +#define E_RECEIVE_PARITY_ERROR 11 +#define E_TRANSMIT_OVERFLOW 12 +#define E_READ_FAILED 13 +#define E_WRITE_FAILED 14 +#define E_FILE_NOT_FOUND 15 +#define E_PERMISSION_DENIED 16 +#define E_AGAIN 17 + +enum BaudRateType { +#if defined(Q_OS_UNIX) || defined(qdoc) + BAUD50 = 50, //POSIX ONLY + BAUD75 = 75, //POSIX ONLY + BAUD134 = 134, //POSIX ONLY + BAUD150 = 150, //POSIX ONLY + BAUD200 = 200, //POSIX ONLY + BAUD1800 = 1800, //POSIX ONLY +# if defined(B76800) || defined(qdoc) + BAUD76800 = 76800, //POSIX ONLY +# endif +# if (defined(B230400) && defined(B4000000)) || defined(qdoc) + BAUD230400 = 230400, //POSIX ONLY + BAUD460800 = 460800, //POSIX ONLY + BAUD500000 = 500000, //POSIX ONLY + BAUD576000 = 576000, //POSIX ONLY + BAUD921600 = 921600, //POSIX ONLY + BAUD1000000 = 1000000, //POSIX ONLY + BAUD1152000 = 1152000, //POSIX ONLY + BAUD1500000 = 1500000, //POSIX ONLY + BAUD2000000 = 2000000, //POSIX ONLY + BAUD2500000 = 2500000, //POSIX ONLY + BAUD3000000 = 3000000, //POSIX ONLY + BAUD3500000 = 3500000, //POSIX ONLY + BAUD4000000 = 4000000, //POSIX ONLY +# endif +#endif //Q_OS_UNIX +#if defined(Q_OS_WIN) || defined(qdoc) + BAUD14400 = 14400, //WINDOWS ONLY + BAUD56000 = 56000, //WINDOWS ONLY + BAUD128000 = 128000, //WINDOWS ONLY + BAUD256000 = 256000, //WINDOWS ONLY +#endif //Q_OS_WIN + BAUD110 = 110, + BAUD300 = 300, + BAUD600 = 600, + BAUD1200 = 1200, + BAUD2400 = 2400, + BAUD4800 = 4800, + BAUD9600 = 9600, + BAUD19200 = 19200, + BAUD38400 = 38400, + BAUD57600 = 57600, + BAUD115200 = 115200 +}; + +enum DataBitsType { + DATA_5 = 5, + DATA_6 = 6, + DATA_7 = 7, + DATA_8 = 8 +}; + +enum ParityType { + PAR_NONE, + PAR_ODD, + PAR_EVEN, +#if defined(Q_OS_WIN) || defined(qdoc) + PAR_MARK, //WINDOWS ONLY +#endif + PAR_SPACE +}; + +enum StopBitsType { + STOP_1, +#if defined(Q_OS_WIN) || defined(qdoc) + STOP_1_5, //WINDOWS ONLY +#endif + STOP_2 +}; + +enum FlowType { + FLOW_OFF, + FLOW_HARDWARE, + FLOW_XONXOFF +}; + +/** + * structure to contain port settings + */ +struct PortSettings { + BaudRateType BaudRate; + DataBitsType DataBits; + ParityType Parity; + StopBitsType StopBits; + FlowType FlowControl; + long Timeout_Millisec; +}; + +class QextSerialPortPrivate; +class QEXTSERIALPORT_EXPORT QextSerialPort: public QIODevice +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QextSerialPort) + Q_ENUMS(QueryMode) + Q_PROPERTY(QString portName READ portName WRITE setPortName) + Q_PROPERTY(QueryMode queryMode READ queryMode WRITE setQueryMode) +public: + enum QueryMode { + Polling, + EventDriven + }; + + explicit QextSerialPort(QueryMode mode = EventDriven, QObject *parent = 0); + explicit QextSerialPort(const QString &name, QueryMode mode = EventDriven, QObject *parent = 0); + explicit QextSerialPort(const PortSettings &s, QueryMode mode = EventDriven, QObject *parent = 0); + QextSerialPort(const QString &name, const PortSettings &s, QueryMode mode = EventDriven, QObject *parent = 0); + + ~QextSerialPort(); + + QString portName() const; + QueryMode queryMode() const; + BaudRateType baudRate() const; + DataBitsType dataBits() const; + ParityType parity() const; + StopBitsType stopBits() const; + FlowType flowControl() const; + + bool open(OpenMode mode); + bool isSequential() const; + void close(); + void flush(); + qint64 bytesAvailable() const; + bool canReadLine() const; + QByteArray readAll(); + + ulong lastError() const; + + ulong lineStatus(); + QString errorString(); + +public Q_SLOTS: + void setPortName(const QString &name); + void setQueryMode(QueryMode mode); + void setBaudRate(BaudRateType); + void setDataBits(DataBitsType); + void setParity(ParityType); + void setStopBits(StopBitsType); + void setFlowControl(FlowType); + void setTimeout(long); + + void setDtr(bool set = true); + void setRts(bool set = true); + +Q_SIGNALS: + void dsrChanged(bool status); + +protected: + qint64 readData(char *data, qint64 maxSize); + qint64 writeData(const char *data, qint64 maxSize); + +private: + Q_DISABLE_COPY(QextSerialPort) + +#ifdef Q_OS_WIN + Q_PRIVATE_SLOT(d_func(), void _q_onWinEvent(HANDLE)) +#endif + Q_PRIVATE_SLOT(d_func(), void _q_canRead()) + + QextSerialPortPrivate *const d_ptr; +}; + +#endif diff --git a/comtool/qextserialport/qextserialport.pri b/comtool/qextserialport/qextserialport.pri new file mode 100644 index 0000000..12e8a78 --- /dev/null +++ b/comtool/qextserialport/qextserialport.pri @@ -0,0 +1,9 @@ +HEADERS += \ + $$PWD/qextserialport.h \ + $$PWD/qextserialport_global.h \ + $$PWD/qextserialport_p.h + +SOURCES += $$PWD/qextserialport.cpp + +win32:SOURCES += $$PWD/qextserialport_win.cpp +unix:SOURCES += $$PWD/qextserialport_unix.cpp diff --git a/comtool/qextserialport/qextserialport_global.h b/comtool/qextserialport/qextserialport_global.h new file mode 100644 index 0000000..da91069 --- /dev/null +++ b/comtool/qextserialport/qextserialport_global.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** Copyright (c) 2000-2003 Wayne Roth +** Copyright (c) 2004-2007 Stefan Sander +** Copyright (c) 2007 Michal Policht +** Copyright (c) 2008 Brandon Fosdick +** Copyright (c) 2009-2010 Liam Staskawicz +** Copyright (c) 2011 Debao Zhang +** All right reserved. +** Web: http://code.google.com/p/qextserialport/ +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ + +#ifndef QEXTSERIALPORT_GLOBAL_H +#define QEXTSERIALPORT_GLOBAL_H + +#include + +#ifdef QEXTSERIALPORT_BUILD_SHARED +# define QEXTSERIALPORT_EXPORT Q_DECL_EXPORT +#elif defined(QEXTSERIALPORT_USING_SHARED) +# define QEXTSERIALPORT_EXPORT Q_DECL_IMPORT +#else +# define QEXTSERIALPORT_EXPORT +#endif + +// ### for compatible with old version. should be removed in QESP 2.0 +#ifdef _TTY_NOWARN_ +# define QESP_NO_WARN +#endif +#ifdef _TTY_NOWARN_PORT_ +# define QESP_NO_PORTABILITY_WARN +#endif + +/*if all warning messages are turned off, flag portability warnings to be turned off as well*/ +#ifdef QESP_NO_WARN +# define QESP_NO_PORTABILITY_WARN +#endif + +/*macros for warning and debug messages*/ +#ifdef QESP_NO_PORTABILITY_WARN +# define QESP_PORTABILITY_WARNING while (false)qWarning +#else +# define QESP_PORTABILITY_WARNING qWarning +#endif /*QESP_NOWARN_PORT*/ + +#ifdef QESP_NO_WARN +# define QESP_WARNING while (false)qWarning +#else +# define QESP_WARNING qWarning +#endif /*QESP_NOWARN*/ + +#endif // QEXTSERIALPORT_GLOBAL_H + diff --git a/comtool/qextserialport/qextserialport_p.h b/comtool/qextserialport/qextserialport_p.h new file mode 100644 index 0000000..d128c65 --- /dev/null +++ b/comtool/qextserialport/qextserialport_p.h @@ -0,0 +1,277 @@ +/**************************************************************************** +** Copyright (c) 2000-2003 Wayne Roth +** Copyright (c) 2004-2007 Stefan Sander +** Copyright (c) 2007 Michal Policht +** Copyright (c) 2008 Brandon Fosdick +** Copyright (c) 2009-2010 Liam Staskawicz +** Copyright (c) 2011 Debao Zhang +** All right reserved. +** Web: http://code.google.com/p/qextserialport/ +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ + +#ifndef _QEXTSERIALPORT_P_H_ +#define _QEXTSERIALPORT_P_H_ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the QESP API. It exists for the convenience +// of other QESP classes. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qextserialport.h" +#include +#ifdef Q_OS_UNIX +# include +#elif (defined Q_OS_WIN) +# include +#endif +#include + +// This is QextSerialPort's read buffer, needed by posix system. +// ref: QRingBuffer & QIODevicePrivateLinearBuffer +class QextReadBuffer +{ +public: + inline QextReadBuffer(size_t growth = 4096) + : len(0), first(0), buf(0), capacity(0), basicBlockSize(growth) + { + } + + ~QextReadBuffer() + { + delete buf; + } + + inline void clear() + { + first = buf; + len = 0; + } + + inline int size() const + { + return len; + } + + inline bool isEmpty() const + { + return len == 0; + } + + inline int read(char *target, int size) + { + int r = qMin(size, len); + + if (r == 1) { + *target = *first; + --len; + ++first; + } else { + memcpy(target, first, r); + len -= r; + first += r; + } + + return r; + } + + inline char *reserve(size_t size) + { + if ((first - buf) + len + size > capacity) { + size_t newCapacity = qMax(capacity, basicBlockSize); + + while (newCapacity < len + size) { + newCapacity *= 2; + } + + if (newCapacity > capacity) { + // allocate more space + char *newBuf = new char[newCapacity]; + memmove(newBuf, first, len); + delete buf; + buf = newBuf; + capacity = newCapacity; + } else { + // shift any existing data to make space + memmove(buf, first, len); + } + + first = buf; + } + + char *writePtr = first + len; + len += (int)size; + return writePtr; + } + + inline void chop(int size) + { + if (size >= len) { + clear(); + } else { + len -= size; + } + } + + inline void squeeze() + { + if (first != buf) { + memmove(buf, first, len); + first = buf; + } + + size_t newCapacity = basicBlockSize; + + while (newCapacity < size_t(len)) { + newCapacity *= 2; + } + + if (newCapacity < capacity) { + char *tmp = static_cast(realloc(buf, newCapacity)); + + if (tmp) { + buf = tmp; + capacity = newCapacity; + } + } + } + + inline QByteArray readAll() + { + char *f = first; + int l = len; + clear(); + return QByteArray(f, l); + } + + inline int readLine(char *target, int size) + { + int r = qMin(size, len); + char *eol = static_cast(memchr(first, '\n', r)); + + if (eol) { + r = 1 + (eol - first); + } + + memcpy(target, first, r); + len -= r; + first += r; + return int(r); + } + + inline bool canReadLine() const + { + return memchr(first, '\n', len); + } + +private: + int len; + char *first; + char *buf; + size_t capacity; + size_t basicBlockSize; +}; + +class QWinEventNotifier; +class QReadWriteLock; +class QSocketNotifier; + +class QextSerialPortPrivate +{ + Q_DECLARE_PUBLIC(QextSerialPort) +public: + QextSerialPortPrivate(QextSerialPort *q); + ~QextSerialPortPrivate(); + enum DirtyFlagEnum { + DFE_BaudRate = 0x0001, + DFE_Parity = 0x0002, + DFE_StopBits = 0x0004, + DFE_DataBits = 0x0008, + DFE_Flow = 0x0010, + DFE_TimeOut = 0x0100, + DFE_ALL = 0x0fff, + DFE_Settings_Mask = 0x00ff //without TimeOut + }; + mutable QReadWriteLock lock; + QString port; + PortSettings settings; + QextReadBuffer readBuffer; + int settingsDirtyFlags; + ulong lastErr; + QextSerialPort::QueryMode queryMode; + + // platform specific members +#ifdef Q_OS_UNIX + int fd; + QSocketNotifier *readNotifier; + struct termios currentTermios; + struct termios oldTermios; +#elif (defined Q_OS_WIN) + HANDLE handle; + OVERLAPPED overlap; + COMMCONFIG commConfig; + COMMTIMEOUTS commTimeouts; + QWinEventNotifier *winEventNotifier; + DWORD eventMask; + QList pendingWrites; + QReadWriteLock *bytesToWriteLock; +#endif + + /*fill PortSettings*/ + void setBaudRate(BaudRateType baudRate, bool update = true); + void setDataBits(DataBitsType dataBits, bool update = true); + void setParity(ParityType parity, bool update = true); + void setStopBits(StopBitsType stopbits, bool update = true); + void setFlowControl(FlowType flow, bool update = true); + void setTimeout(long millisec, bool update = true); + void setPortSettings(const PortSettings &settings, bool update = true); + + void platformSpecificDestruct(); + void platformSpecificInit(); + void translateError(ulong error); + void updatePortSettings(); + + qint64 readData_sys(char *data, qint64 maxSize); + qint64 writeData_sys(const char *data, qint64 maxSize); + void setDtr_sys(bool set = true); + void setRts_sys(bool set = true); + bool open_sys(QIODevice::OpenMode mode); + bool close_sys(); + bool flush_sys(); + ulong lineStatus_sys(); + qint64 bytesAvailable_sys() const; + +#ifdef Q_OS_WIN + void _q_onWinEvent(HANDLE h); +#endif + void _q_canRead(); + + QextSerialPort *q_ptr; +}; + +#endif //_QEXTSERIALPORT_P_H_ diff --git a/comtool/qextserialport/qextserialport_unix.cpp b/comtool/qextserialport/qextserialport_unix.cpp new file mode 100644 index 0000000..5a78148 --- /dev/null +++ b/comtool/qextserialport/qextserialport_unix.cpp @@ -0,0 +1,559 @@ +/**************************************************************************** +** Copyright (c) 2000-2003 Wayne Roth +** Copyright (c) 2004-2007 Stefan Sander +** Copyright (c) 2007 Michal Policht +** Copyright (c) 2008 Brandon Fosdick +** Copyright (c) 2009-2010 Liam Staskawicz +** Copyright (c) 2011 Debao Zhang +** All right reserved. +** Web: http://code.google.com/p/qextserialport/ +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ + +#include "qextserialport.h" +#include "qextserialport_p.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void QextSerialPortPrivate::platformSpecificInit() +{ + fd = 0; + readNotifier = 0; +} + +/*! + Standard destructor. +*/ +void QextSerialPortPrivate::platformSpecificDestruct() +{ +} + +static QString fullPortName(const QString &name) +{ + if (name.startsWith(QLatin1Char('/'))) { + return name; + } + + return QLatin1String("/dev/") + name; +} + +bool QextSerialPortPrivate::open_sys(QIODevice::OpenMode mode) +{ + Q_Q(QextSerialPort); + + //note: linux 2.6.21 seems to ignore O_NDELAY flag + if ((fd = ::open(fullPortName(port).toLatin1() , O_RDWR | O_NOCTTY | O_NDELAY)) != -1) { + + /*In the Private class, We can not call QIODevice::open()*/ + q->setOpenMode(mode); // Flag the port as opened + ::tcgetattr(fd, &oldTermios); // Save the old termios + currentTermios = oldTermios; // Make a working copy + ::cfmakeraw(¤tTermios); // Enable raw access + + /*set up other port settings*/ + currentTermios.c_cflag |= CREAD | CLOCAL; + currentTermios.c_lflag &= (~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG)); + currentTermios.c_iflag &= (~(INPCK | IGNPAR | PARMRK | ISTRIP | ICRNL | IXANY)); + currentTermios.c_oflag &= (~OPOST); + currentTermios.c_cc[VMIN] = 0; +#ifdef _POSIX_VDISABLE // Is a disable character available on this system? + // Some systems allow for per-device disable-characters, so get the + // proper value for the configured device + const long vdisable = ::fpathconf(fd, _PC_VDISABLE); + currentTermios.c_cc[VINTR] = vdisable; + currentTermios.c_cc[VQUIT] = vdisable; + currentTermios.c_cc[VSTART] = vdisable; + currentTermios.c_cc[VSTOP] = vdisable; + currentTermios.c_cc[VSUSP] = vdisable; +#endif //_POSIX_VDISABLE + settingsDirtyFlags = DFE_ALL; + updatePortSettings(); + + if (queryMode == QextSerialPort::EventDriven) { + readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, q); + q->connect(readNotifier, SIGNAL(activated(int)), q, SLOT(_q_canRead())); + } + + return true; + } else { + translateError(errno); + return false; + } +} + +bool QextSerialPortPrivate::close_sys() +{ + // Force a flush and then restore the original termios + flush_sys(); + // Using both TCSAFLUSH and TCSANOW here discards any pending input + ::tcsetattr(fd, TCSAFLUSH | TCSANOW, &oldTermios); // Restore termios + ::close(fd); + + if (readNotifier) { + delete readNotifier; + readNotifier = 0; + } + + return true; +} + +bool QextSerialPortPrivate::flush_sys() +{ + ::tcdrain(fd); + return true; +} + +qint64 QextSerialPortPrivate::bytesAvailable_sys() const +{ + int bytesQueued; + + if (::ioctl(fd, FIONREAD, &bytesQueued) == -1) { + return (qint64) - 1; + } + + return bytesQueued; +} + +/*! + Translates a system-specific error code to a QextSerialPort error code. Used internally. +*/ +void QextSerialPortPrivate::translateError(ulong error) +{ + switch (error) { + case EBADF: + case ENOTTY: + lastErr = E_INVALID_FD; + break; + + case EINTR: + lastErr = E_CAUGHT_NON_BLOCKED_SIGNAL; + break; + + case ENOMEM: + lastErr = E_NO_MEMORY; + break; + + case EACCES: + lastErr = E_PERMISSION_DENIED; + break; + + case EAGAIN: + lastErr = E_AGAIN; + break; + } +} + +void QextSerialPortPrivate::setDtr_sys(bool set) +{ + int status; + ::ioctl(fd, TIOCMGET, &status); + + if (set) { + status |= TIOCM_DTR; + } else { + status &= ~TIOCM_DTR; + } + + ::ioctl(fd, TIOCMSET, &status); +} + +void QextSerialPortPrivate::setRts_sys(bool set) +{ + int status; + ::ioctl(fd, TIOCMGET, &status); + + if (set) { + status |= TIOCM_RTS; + } else { + status &= ~TIOCM_RTS; + } + + ::ioctl(fd, TIOCMSET, &status); +} + +unsigned long QextSerialPortPrivate::lineStatus_sys() +{ + unsigned long Status = 0, Temp = 0; + ::ioctl(fd, TIOCMGET, &Temp); + + if (Temp & TIOCM_CTS) { + Status |= LS_CTS; + } + + if (Temp & TIOCM_DSR) { + Status |= LS_DSR; + } + + if (Temp & TIOCM_RI) { + Status |= LS_RI; + } + + if (Temp & TIOCM_CD) { + Status |= LS_DCD; + } + + if (Temp & TIOCM_DTR) { + Status |= LS_DTR; + } + + if (Temp & TIOCM_RTS) { + Status |= LS_RTS; + } + + if (Temp & TIOCM_ST) { + Status |= LS_ST; + } + + if (Temp & TIOCM_SR) { + Status |= LS_SR; + } + + return Status; +} + +/*! + Reads a block of data from the serial port. This function will read at most maxSize bytes from + the serial port and place them in the buffer pointed to by data. Return value is the number of + bytes actually read, or -1 on error. + + \warning before calling this function ensure that serial port associated with this class + is currently open (use isOpen() function to check if port is open). +*/ +qint64 QextSerialPortPrivate::readData_sys(char *data, qint64 maxSize) +{ + int retVal = ::read(fd, data, maxSize); + + if (retVal == -1) { + lastErr = E_READ_FAILED; + } + + return retVal; +} + +/*! + Writes a block of data to the serial port. This function will write maxSize bytes + from the buffer pointed to by data to the serial port. Return value is the number + of bytes actually written, or -1 on error. + + \warning before calling this function ensure that serial port associated with this class + is currently open (use isOpen() function to check if port is open). +*/ +qint64 QextSerialPortPrivate::writeData_sys(const char *data, qint64 maxSize) +{ + int retVal = ::write(fd, data, maxSize); + + if (retVal == -1) { + lastErr = E_WRITE_FAILED; + } + + return (qint64)retVal; +} + +static void setBaudRate2Termios(termios *config, int baudRate) +{ +#ifdef CBAUD + config->c_cflag &= (~CBAUD); + config->c_cflag |= baudRate; +#else + ::cfsetispeed(config, baudRate); + ::cfsetospeed(config, baudRate); +#endif +} + +/* + All the platform settings was performed in this function. +*/ +void QextSerialPortPrivate::updatePortSettings() +{ + if (!q_func()->isOpen() || !settingsDirtyFlags) { + return; + } + + if (settingsDirtyFlags & DFE_BaudRate) { + switch (settings.BaudRate) { + case BAUD50: + setBaudRate2Termios(¤tTermios, B50); + break; + + case BAUD75: + setBaudRate2Termios(¤tTermios, B75); + break; + + case BAUD110: + setBaudRate2Termios(¤tTermios, B110); + break; + + case BAUD134: + setBaudRate2Termios(¤tTermios, B134); + break; + + case BAUD150: + setBaudRate2Termios(¤tTermios, B150); + break; + + case BAUD200: + setBaudRate2Termios(¤tTermios, B200); + break; + + case BAUD300: + setBaudRate2Termios(¤tTermios, B300); + break; + + case BAUD600: + setBaudRate2Termios(¤tTermios, B600); + break; + + case BAUD1200: + setBaudRate2Termios(¤tTermios, B1200); + break; + + case BAUD1800: + setBaudRate2Termios(¤tTermios, B1800); + break; + + case BAUD2400: + setBaudRate2Termios(¤tTermios, B2400); + break; + + case BAUD4800: + setBaudRate2Termios(¤tTermios, B4800); + break; + + case BAUD9600: + setBaudRate2Termios(¤tTermios, B9600); + break; + + case BAUD19200: + setBaudRate2Termios(¤tTermios, B19200); + break; + + case BAUD38400: + setBaudRate2Termios(¤tTermios, B38400); + break; + + case BAUD57600: + setBaudRate2Termios(¤tTermios, B57600); + break; +#ifdef B76800 + + case BAUD76800: + setBaudRate2Termios(¤tTermios, B76800); + break; +#endif + + case BAUD115200: + setBaudRate2Termios(¤tTermios, B115200); + break; +#if defined(B230400) && defined(B4000000) + + case BAUD230400: + setBaudRate2Termios(¤tTermios, B230400); + break; + + case BAUD460800: + setBaudRate2Termios(¤tTermios, B460800); + break; + + case BAUD500000: + setBaudRate2Termios(¤tTermios, B500000); + break; + + case BAUD576000: + setBaudRate2Termios(¤tTermios, B576000); + break; + + case BAUD921600: + setBaudRate2Termios(¤tTermios, B921600); + break; + + case BAUD1000000: + setBaudRate2Termios(¤tTermios, B1000000); + break; + + case BAUD1152000: + setBaudRate2Termios(¤tTermios, B1152000); + break; + + case BAUD1500000: + setBaudRate2Termios(¤tTermios, B1500000); + break; + + case BAUD2000000: + setBaudRate2Termios(¤tTermios, B2000000); + break; + + case BAUD2500000: + setBaudRate2Termios(¤tTermios, B2500000); + break; + + case BAUD3000000: + setBaudRate2Termios(¤tTermios, B3000000); + break; + + case BAUD3500000: + setBaudRate2Termios(¤tTermios, B3500000); + break; + + case BAUD4000000: + setBaudRate2Termios(¤tTermios, B4000000); + break; +#endif +#ifdef Q_OS_MAC + + default: + setBaudRate2Termios(¤tTermios, settings.BaudRate); + break; +#endif + } + } + + if (settingsDirtyFlags & DFE_Parity) { + switch (settings.Parity) { + case PAR_SPACE: + /*space parity not directly supported - add an extra data bit to simulate it*/ + settingsDirtyFlags |= DFE_DataBits; + break; + + case PAR_NONE: + currentTermios.c_cflag &= (~PARENB); + break; + + case PAR_EVEN: + currentTermios.c_cflag &= (~PARODD); + currentTermios.c_cflag |= PARENB; + break; + + case PAR_ODD: + currentTermios.c_cflag |= (PARENB | PARODD); + break; + } + } + + /*must after Parity settings*/ + if (settingsDirtyFlags & DFE_DataBits) { + if (settings.Parity != PAR_SPACE) { + currentTermios.c_cflag &= (~CSIZE); + + switch (settings.DataBits) { + case DATA_5: + currentTermios.c_cflag |= CS5; + break; + + case DATA_6: + currentTermios.c_cflag |= CS6; + break; + + case DATA_7: + currentTermios.c_cflag |= CS7; + break; + + case DATA_8: + currentTermios.c_cflag |= CS8; + break; + } + } else { + /*space parity not directly supported - add an extra data bit to simulate it*/ + currentTermios.c_cflag &= ~(PARENB | CSIZE); + + switch (settings.DataBits) { + case DATA_5: + currentTermios.c_cflag |= CS6; + break; + + case DATA_6: + currentTermios.c_cflag |= CS7; + break; + + case DATA_7: + currentTermios.c_cflag |= CS8; + break; + + case DATA_8: + /*this will never happen, put here to Suppress an warning*/ + break; + } + } + } + + if (settingsDirtyFlags & DFE_StopBits) { + switch (settings.StopBits) { + case STOP_1: + currentTermios.c_cflag &= (~CSTOPB); + break; + + case STOP_2: + currentTermios.c_cflag |= CSTOPB; + break; + } + } + + if (settingsDirtyFlags & DFE_Flow) { + switch (settings.FlowControl) { + case FLOW_OFF: + currentTermios.c_cflag &= (~CRTSCTS); + currentTermios.c_iflag &= (~(IXON | IXOFF | IXANY)); + break; + + case FLOW_XONXOFF: + /*software (XON/XOFF) flow control*/ + currentTermios.c_cflag &= (~CRTSCTS); + currentTermios.c_iflag |= (IXON | IXOFF | IXANY); + break; + + case FLOW_HARDWARE: + currentTermios.c_cflag |= CRTSCTS; + currentTermios.c_iflag &= (~(IXON | IXOFF | IXANY)); + break; + } + } + + /*if any thing in currentTermios changed, flush*/ + if (settingsDirtyFlags & DFE_Settings_Mask) { + ::tcsetattr(fd, TCSAFLUSH, ¤tTermios); + } + + if (settingsDirtyFlags & DFE_TimeOut) { + int millisec = settings.Timeout_Millisec; + + if (millisec == -1) { + ::fcntl(fd, F_SETFL, O_NDELAY); + } else { + //O_SYNC should enable blocking ::write() + //however this seems not working on Linux 2.6.21 (works on OpenBSD 4.2) + ::fcntl(fd, F_SETFL, O_SYNC); + } + + ::tcgetattr(fd, ¤tTermios); + currentTermios.c_cc[VTIME] = millisec / 100; + ::tcsetattr(fd, TCSAFLUSH, ¤tTermios); + } + + settingsDirtyFlags = 0; +} diff --git a/comtool/qextserialport/qextserialport_win.cpp b/comtool/qextserialport/qextserialport_win.cpp new file mode 100644 index 0000000..50828af --- /dev/null +++ b/comtool/qextserialport/qextserialport_win.cpp @@ -0,0 +1,476 @@ +/**************************************************************************** +** Copyright (c) 2000-2003 Wayne Roth +** Copyright (c) 2004-2007 Stefan Sander +** Copyright (c) 2007 Michal Policht +** Copyright (c) 2008 Brandon Fosdick +** Copyright (c) 2009-2010 Liam Staskawicz +** Copyright (c) 2011 Debao Zhang +** All right reserved. +** Web: http://code.google.com/p/qextserialport/ +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ + +#include "qextserialport.h" +#include "qextserialport_p.h" +#include +#include +#include +#include +#include +#include +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) +# include +#else +# include +#endif +void QextSerialPortPrivate::platformSpecificInit() +{ + handle = INVALID_HANDLE_VALUE; + ZeroMemory(&overlap, sizeof(OVERLAPPED)); + overlap.hEvent = CreateEvent(NULL, true, false, NULL); + winEventNotifier = 0; + bytesToWriteLock = new QReadWriteLock; +} + +void QextSerialPortPrivate::platformSpecificDestruct() +{ + CloseHandle(overlap.hEvent); + delete bytesToWriteLock; +} + + +/*! + \internal + COM ports greater than 9 need \\.\ prepended + + This is only need when open the port. +*/ +static QString fullPortNameWin(const QString &name) +{ + QRegExp rx(QLatin1String("^COM(\\d+)")); + QString fullName(name); + + if (fullName.contains(rx)) { + fullName.prepend(QLatin1String("\\\\.\\")); + } + + return fullName; +} + +bool QextSerialPortPrivate::open_sys(QIODevice::OpenMode mode) +{ + Q_Q(QextSerialPort); + DWORD confSize = sizeof(COMMCONFIG); + commConfig.dwSize = confSize; + DWORD dwFlagsAndAttributes = 0; + + if (queryMode == QextSerialPort::EventDriven) { + dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED; + } + + /*open the port*/ + handle = CreateFileW((wchar_t *)fullPortNameWin(port).utf16(), GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL); + + if (handle != INVALID_HANDLE_VALUE) { + q->setOpenMode(mode); + /*configure port settings*/ + GetCommConfig(handle, &commConfig, &confSize); + GetCommState(handle, &(commConfig.dcb)); + + /*set up parameters*/ + commConfig.dcb.fBinary = TRUE; + commConfig.dcb.fInX = FALSE; + commConfig.dcb.fOutX = FALSE; + commConfig.dcb.fAbortOnError = FALSE; + commConfig.dcb.fNull = FALSE; + /* Dtr default to true. See Issue 122*/ + commConfig.dcb.fDtrControl = TRUE; + /*flush all settings*/ + settingsDirtyFlags = DFE_ALL; + updatePortSettings(); + + //init event driven approach + if (queryMode == QextSerialPort::EventDriven) { + if (!SetCommMask(handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) { + QESP_WARNING() << "failed to set Comm Mask. Error code:" << GetLastError(); + return false; + } + + winEventNotifier = new QWinEventNotifier(overlap.hEvent, q); + qRegisterMetaType("HANDLE"); + q->connect(winEventNotifier, SIGNAL(activated(HANDLE)), q, SLOT(_q_onWinEvent(HANDLE)), Qt::DirectConnection); + WaitCommEvent(handle, &eventMask, &overlap); + } + + return true; + } + + return false; +} + +bool QextSerialPortPrivate::close_sys() +{ + flush_sys(); + CancelIo(handle); + + if (CloseHandle(handle)) { + handle = INVALID_HANDLE_VALUE; + } + + if (winEventNotifier) { + winEventNotifier->setEnabled(false); + winEventNotifier->deleteLater(); + winEventNotifier = 0; + } + + foreach (OVERLAPPED *o, pendingWrites) { + CloseHandle(o->hEvent); + delete o; + } + + pendingWrites.clear(); + return true; +} + +bool QextSerialPortPrivate::flush_sys() +{ + FlushFileBuffers(handle); + return true; +} + +qint64 QextSerialPortPrivate::bytesAvailable_sys() const +{ + DWORD Errors; + COMSTAT Status; + + if (ClearCommError(handle, &Errors, &Status)) { + return Status.cbInQue; + } + + return (qint64) - 1; +} + +/* + Translates a system-specific error code to a QextSerialPort error code. Used internally. +*/ +void QextSerialPortPrivate::translateError(ulong error) +{ + if (error & CE_BREAK) { + lastErr = E_BREAK_CONDITION; + } else if (error & CE_FRAME) { + lastErr = E_FRAMING_ERROR; + } else if (error & CE_IOE) { + lastErr = E_IO_ERROR; + } else if (error & CE_MODE) { + lastErr = E_INVALID_FD; + } else if (error & CE_OVERRUN) { + lastErr = E_BUFFER_OVERRUN; + } else if (error & CE_RXPARITY) { + lastErr = E_RECEIVE_PARITY_ERROR; + } else if (error & CE_RXOVER) { + lastErr = E_RECEIVE_OVERFLOW; + } else if (error & CE_TXFULL) { + lastErr = E_TRANSMIT_OVERFLOW; + } +} + +/* + Reads a block of data from the serial port. This function will read at most maxlen bytes from + the serial port and place them in the buffer pointed to by data. Return value is the number of + bytes actually read, or -1 on error. + + \warning before calling this function ensure that serial port associated with this class + is currently open (use isOpen() function to check if port is open). +*/ +qint64 QextSerialPortPrivate::readData_sys(char *data, qint64 maxSize) +{ + DWORD bytesRead = 0; + bool failed = false; + + if (queryMode == QextSerialPort::EventDriven) { + OVERLAPPED overlapRead; + ZeroMemory(&overlapRead, sizeof(OVERLAPPED)); + + if (!ReadFile(handle, (void *)data, (DWORD)maxSize, &bytesRead, &overlapRead)) { + if (GetLastError() == ERROR_IO_PENDING) { + GetOverlappedResult(handle, &overlapRead, &bytesRead, true); + } else { + failed = true; + } + } + } else if (!ReadFile(handle, (void *)data, (DWORD)maxSize, &bytesRead, NULL)) { + failed = true; + } + + if (!failed) { + return (qint64)bytesRead; + } + + lastErr = E_READ_FAILED; + return -1; +} + +/* + Writes a block of data to the serial port. This function will write len bytes + from the buffer pointed to by data to the serial port. Return value is the number + of bytes actually written, or -1 on error. + + \warning before calling this function ensure that serial port associated with this class + is currently open (use isOpen() function to check if port is open). +*/ +qint64 QextSerialPortPrivate::writeData_sys(const char *data, qint64 maxSize) +{ + DWORD bytesWritten = 0; + bool failed = false; + + if (queryMode == QextSerialPort::EventDriven) { + OVERLAPPED *newOverlapWrite = new OVERLAPPED; + ZeroMemory(newOverlapWrite, sizeof(OVERLAPPED)); + newOverlapWrite->hEvent = CreateEvent(NULL, true, false, NULL); + + if (WriteFile(handle, (void *)data, (DWORD)maxSize, &bytesWritten, newOverlapWrite)) { + CloseHandle(newOverlapWrite->hEvent); + delete newOverlapWrite; + } else if (GetLastError() == ERROR_IO_PENDING) { + // writing asynchronously...not an error + QWriteLocker writelocker(bytesToWriteLock); + pendingWrites.append(newOverlapWrite); + } else { + QESP_WARNING() << "QextSerialPort write error:" << GetLastError(); + failed = true; + + if (!CancelIo(newOverlapWrite->hEvent)) { + QESP_WARNING("QextSerialPort: couldn't cancel IO"); + } + + if (!CloseHandle(newOverlapWrite->hEvent)) { + QESP_WARNING("QextSerialPort: couldn't close OVERLAPPED handle"); + } + + delete newOverlapWrite; + } + } else if (!WriteFile(handle, (void *)data, (DWORD)maxSize, &bytesWritten, NULL)) { + failed = true; + } + + if (!failed) { + return (qint64)bytesWritten; + } + + lastErr = E_WRITE_FAILED; + return -1; +} + +void QextSerialPortPrivate::setDtr_sys(bool set) +{ + EscapeCommFunction(handle, set ? SETDTR : CLRDTR); +} + +void QextSerialPortPrivate::setRts_sys(bool set) +{ + EscapeCommFunction(handle, set ? SETRTS : CLRRTS); +} + +ulong QextSerialPortPrivate::lineStatus_sys(void) +{ + unsigned long Status = 0, Temp = 0; + GetCommModemStatus(handle, &Temp); + + if (Temp & MS_CTS_ON) { + Status |= LS_CTS; + } + + if (Temp & MS_DSR_ON) { + Status |= LS_DSR; + } + + if (Temp & MS_RING_ON) { + Status |= LS_RI; + } + + if (Temp & MS_RLSD_ON) { + Status |= LS_DCD; + } + + return Status; +} + +/* + Triggered when there's activity on our HANDLE. +*/ +void QextSerialPortPrivate::_q_onWinEvent(HANDLE h) +{ + Q_Q(QextSerialPort); + + if (h == overlap.hEvent) { + if (eventMask & EV_RXCHAR) { + if (q->sender() != q && bytesAvailable_sys() > 0) { + _q_canRead(); + } + } + + if (eventMask & EV_TXEMPTY) { + /* + A write completed. Run through the list of OVERLAPPED writes, and if + they completed successfully, take them off the list and delete them. + Otherwise, leave them on there so they can finish. + */ + qint64 totalBytesWritten = 0; + QList overlapsToDelete; + + foreach (OVERLAPPED *o, pendingWrites) { + DWORD numBytes = 0; + + if (GetOverlappedResult(handle, o, &numBytes, false)) { + overlapsToDelete.append(o); + totalBytesWritten += numBytes; + } else if (GetLastError() != ERROR_IO_INCOMPLETE) { + overlapsToDelete.append(o); + QESP_WARNING() << "CommEvent overlapped write error:" << GetLastError(); + } + } + + if (q->sender() != q && totalBytesWritten > 0) { + QWriteLocker writelocker(bytesToWriteLock); + Q_EMIT q->bytesWritten(totalBytesWritten); + } + + foreach (OVERLAPPED *o, overlapsToDelete) { + OVERLAPPED *toDelete = pendingWrites.takeAt(pendingWrites.indexOf(o)); + CloseHandle(toDelete->hEvent); + delete toDelete; + } + } + + if (eventMask & EV_DSR) { + if (lineStatus_sys() & LS_DSR) { + Q_EMIT q->dsrChanged(true); + } else { + Q_EMIT q->dsrChanged(false); + } + } + } + + WaitCommEvent(handle, &eventMask, &overlap); +} + +void QextSerialPortPrivate::updatePortSettings() +{ + if (!q_ptr->isOpen() || !settingsDirtyFlags) { + return; + } + + //fill struct : COMMCONFIG + if (settingsDirtyFlags & DFE_BaudRate) { + commConfig.dcb.BaudRate = settings.BaudRate; + } + + if (settingsDirtyFlags & DFE_Parity) { + commConfig.dcb.Parity = (BYTE)settings.Parity; + commConfig.dcb.fParity = (settings.Parity == PAR_NONE) ? FALSE : TRUE; + } + + if (settingsDirtyFlags & DFE_DataBits) { + commConfig.dcb.ByteSize = (BYTE)settings.DataBits; + } + + if (settingsDirtyFlags & DFE_StopBits) { + switch (settings.StopBits) { + case STOP_1: + commConfig.dcb.StopBits = ONESTOPBIT; + break; + + case STOP_1_5: + commConfig.dcb.StopBits = ONE5STOPBITS; + break; + + case STOP_2: + commConfig.dcb.StopBits = TWOSTOPBITS; + break; + } + } + + if (settingsDirtyFlags & DFE_Flow) { + switch (settings.FlowControl) { + /*no flow control*/ + case FLOW_OFF: + commConfig.dcb.fOutxCtsFlow = FALSE; + commConfig.dcb.fRtsControl = RTS_CONTROL_DISABLE; + commConfig.dcb.fInX = FALSE; + commConfig.dcb.fOutX = FALSE; + break; + + /*software (XON/XOFF) flow control*/ + case FLOW_XONXOFF: + commConfig.dcb.fOutxCtsFlow = FALSE; + commConfig.dcb.fRtsControl = RTS_CONTROL_DISABLE; + commConfig.dcb.fInX = TRUE; + commConfig.dcb.fOutX = TRUE; + break; + + /*hardware flow control*/ + case FLOW_HARDWARE: + commConfig.dcb.fOutxCtsFlow = TRUE; + commConfig.dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; + commConfig.dcb.fInX = FALSE; + commConfig.dcb.fOutX = FALSE; + break; + } + } + + //fill struct : COMMTIMEOUTS + if (settingsDirtyFlags & DFE_TimeOut) { + if (queryMode != QextSerialPort::EventDriven) { + int millisec = settings.Timeout_Millisec; + + if (millisec == -1) { + commTimeouts.ReadIntervalTimeout = MAXDWORD; + commTimeouts.ReadTotalTimeoutConstant = 0; + } else { + commTimeouts.ReadIntervalTimeout = millisec; + commTimeouts.ReadTotalTimeoutConstant = millisec; + } + + commTimeouts.ReadTotalTimeoutMultiplier = 0; + commTimeouts.WriteTotalTimeoutMultiplier = millisec; + commTimeouts.WriteTotalTimeoutConstant = 0; + } else { + commTimeouts.ReadIntervalTimeout = MAXDWORD; + commTimeouts.ReadTotalTimeoutMultiplier = 0; + commTimeouts.ReadTotalTimeoutConstant = 0; + commTimeouts.WriteTotalTimeoutMultiplier = 0; + commTimeouts.WriteTotalTimeoutConstant = 0; + } + } + + + if (settingsDirtyFlags & DFE_Settings_Mask) { + SetCommConfig(handle, &commConfig, sizeof(COMMCONFIG)); + } + + if ((settingsDirtyFlags & DFE_TimeOut)) { + SetCommTimeouts(handle, &commTimeouts); + } + + settingsDirtyFlags = 0; +} diff --git a/comtool/qextserialport/readme.txt b/comtool/qextserialport/readme.txt new file mode 100644 index 0000000..9f01e4c --- /dev/null +++ b/comtool/qextserialport/readme.txt @@ -0,0 +1,16 @@ +ʹ÷ +proļ +include(qextserialport/qextserialport.pri) + +QextSerialPort *GSM_COM = new QextSerialPort(portName, QextSerialPort::EventDriven); + isOpen = GSM_COM->open(QIODevice::ReadWrite); + if (isOpen) { + GSM_COM->flush(); + GSM_COM->setBaudRate(BAUD9600); + GSM_COM->setDataBits(DATA_8); + GSM_COM->setParity(PAR_NONE); + GSM_COM->setStopBits(STOP_1); + GSM_COM->setFlowControl(FLOW_OFF); + GSM_COM->setTimeout(10); + } + diff --git a/comtool/readme.txt b/comtool/readme.txt new file mode 100644 index 0000000..bd58449 --- /dev/null +++ b/comtool/readme.txt @@ -0,0 +1,16 @@ +ܣ +1֧16ݷա +2֧windowsCOM9ϵĴͨš +3ʵʱʾշֽڴСԼ״̬ +4֧qt汾ײ4.7.0 4.8.5 4.8.7 5.4.1 5.7.0 5.8.0 +5ִ֧תշ + +߼ܣ +1ɹҪ͵ݣÿֻҪѡݼɣݡ +2ģ豸ظݣҪ濪ģ豸ظݡյúõָʱظõĻظָָյ0x16 0x00 0xFF 0x01Ҫظ0x16 0x00 0xFE 0x01ֻҪSendData.txtһ16 00 FF 01:16 00 FE 01ɡ +3ɶʱݺͱݵıļ:Ĭϼ5ӣɸļʱ䡣 +4ڲϽյʱͣʾ鿴ݣ̨Ȼݵرմ鿴ѽյݡ +5ÿյݶһݣѽڵģʱ +6һԴ洦룬Ĵͨ࣬XP/WIN7/UBUNTU/ARMLINUXϵͳ³ɹ벢С + +иõĽQ(517216493)лл \ No newline at end of file