From 589262781dae164aa85cbe2654e2d59890d1b7a9 Mon Sep 17 00:00:00 2001 From: sunwen Date: Mon, 13 Jun 2022 13:26:49 +0800 Subject: [PATCH] Refactor utilities package. --- src/forms/settings/generalform.cpp | 158 +++---- src/main.cpp | 141 ++++--- src/utilities/InputObject.cpp | 394 +++++++++--------- src/utilities/InputObject.h | 35 +- .../{inputobject.ui => InputObject.ui} | 0 src/utilities/LanguageSwitcher.cpp | 38 ++ src/utilities/LanguageSwitcher.h | 25 ++ src/utilities/Locker.cpp | 53 +++ src/utilities/Locker.h | 26 ++ src/utilities/StdOutRedirector.cpp | 92 ++-- src/utilities/StdOutRedirector.h | 36 +- src/utilities/languageswitcher.cpp | 36 -- src/utilities/languageswitcher.h | 25 -- src/utilities/locker.cpp | 52 --- src/utilities/locker.h | 28 -- 15 files changed, 556 insertions(+), 583 deletions(-) rename src/utilities/{inputobject.ui => InputObject.ui} (100%) create mode 100644 src/utilities/LanguageSwitcher.cpp create mode 100644 src/utilities/LanguageSwitcher.h create mode 100644 src/utilities/Locker.cpp create mode 100644 src/utilities/Locker.h delete mode 100644 src/utilities/languageswitcher.cpp delete mode 100644 src/utilities/languageswitcher.h delete mode 100644 src/utilities/locker.cpp delete mode 100644 src/utilities/locker.h diff --git a/src/forms/settings/generalform.cpp b/src/forms/settings/generalform.cpp index ae55c4e..5ee43b0 100644 --- a/src/forms/settings/generalform.cpp +++ b/src/forms/settings/generalform.cpp @@ -17,100 +17,100 @@ GeneralForm::GeneralForm(QWidget* parent) : QWidget(parent) { - layout = new QVBoxLayout(this); - QWidget* lanHeader = new QWidget(this); - layout->addWidget(lanHeader); + layout = new QVBoxLayout(this); + QWidget* lanHeader = new QWidget(this); + layout->addWidget(lanHeader); - QHBoxLayout* lanHeaderLayout = new QHBoxLayout(lanHeader); - QLabel* lbl_lan = new QLabel(tr("Language")); - lanHeaderLayout->addWidget(lbl_lan); + QHBoxLayout* lanHeaderLayout = new QHBoxLayout(lanHeader); + QLabel* lbl_lan = new QLabel(tr("Language")); + lanHeaderLayout->addWidget(lbl_lan); - QPushButton* btnLan = new QPushButton(lanHeader); - lanHeaderLayout->addWidget(btnLan); - lanHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding)); + QPushButton* btnLan = new QPushButton(lanHeader); + lanHeaderLayout->addWidget(btnLan); + lanHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding)); - QWidget* instHeader = new QWidget(this); - layout->addWidget(instHeader); - QHBoxLayout* instHeaderLayout = new QHBoxLayout(instHeader); - QLabel* lbl_ins = new QLabel(tr("Institution Name")); - instHeaderLayout->addWidget(lbl_ins); - QLineEdit* instName = new QLineEdit(instHeader); - instName->setMaximumSize(QSize(300, 32768)); - instHeaderLayout->addWidget(instName); - instHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Fixed)); + QWidget* instHeader = new QWidget(this); + layout->addWidget(instHeader); + QHBoxLayout* instHeaderLayout = new QHBoxLayout(instHeader); + QLabel* lbl_ins = new QLabel(tr("Institution Name")); + instHeaderLayout->addWidget(lbl_ins); + QLineEdit* instName = new QLineEdit(instHeader); + instName->setMaximumSize(QSize(300, 32768)); + instHeaderLayout->addWidget(instName); + instHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Fixed)); - QLabel* lbl_insaddr = new QLabel(tr("Institution Addr")); - instHeaderLayout->addWidget(lbl_insaddr); - QLineEdit* instAddr = new QLineEdit(instHeader); - instHeaderLayout->addWidget(instAddr); - instAddr->setMaximumSize(QSize(300, 32768)); - instHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding)); + QLabel* lbl_insaddr = new QLabel(tr("Institution Addr")); + instHeaderLayout->addWidget(lbl_insaddr); + QLineEdit* instAddr = new QLineEdit(instHeader); + instHeaderLayout->addWidget(instAddr); + instAddr->setMaximumSize(QSize(300, 32768)); + instHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding)); - QWidget* lockHeader = new QWidget(this); - layout->addWidget(lockHeader); - QHBoxLayout* lockHeaderLayout = new QHBoxLayout(lockHeader); + QWidget* lockHeader = new QWidget(this); + layout->addWidget(lockHeader); + QHBoxLayout* lockHeaderLayout = new QHBoxLayout(lockHeader); - QLabel* lbl_lock = new QLabel(tr("Lock Screen")); - lockHeaderLayout->addWidget(lbl_lock); + QLabel* lbl_lock = new QLabel(tr("Lock Screen")); + lockHeaderLayout->addWidget(lbl_lock); - QLineEdit* lockTime = new QLineEdit(lockHeader); - lockTime->setMaximumSize(QSize(300, 32768)); - lockHeaderLayout->addWidget(lockTime); + QLineEdit* lockTime = new QLineEdit(lockHeader); + lockTime->setMaximumSize(QSize(300, 32768)); + lockHeaderLayout->addWidget(lockTime); - QLabel* ss = new QLabel(tr("s")); - lockHeaderLayout->addWidget(ss); - lockHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding)); + QLabel* ss = new QLabel(tr("s")); + lockHeaderLayout->addWidget(ss); + lockHeaderLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding)); - //... - layout->addSpacerItem(new QSpacerItem(20, 300, QSizePolicy::Minimum, QSizePolicy::Expanding)); + //... + layout->addSpacerItem(new QSpacerItem(20, 300, QSizePolicy::Minimum, QSizePolicy::Expanding)); - //init - btnLan->setText(JsonObject::Instance()->defaultLanguage()); - instName->setText(JsonObject::Instance()->institutionName()); - instAddr->setText(JsonObject::Instance()->institutionAddr()); - lockTime->setText(JsonObject::Instance()->lockScreenTimeout()); + //init + btnLan->setText(JsonObject::Instance()->defaultLanguage()); + instName->setText(JsonObject::Instance()->institutionName()); + instAddr->setText(JsonObject::Instance()->institutionAddr()); + lockTime->setText(JsonObject::Instance()->lockScreenTimeout()); - //connection - connect(instName, &QLineEdit::textChanged, [=](const QString& str) - { - JsonObject::Instance()->setInstitutionName(str); - }); + //connection + connect(instName, &QLineEdit::textChanged, [=](const QString& str) + { + JsonObject::Instance()->setInstitutionName(str); + }); - connect(instAddr, &QLineEdit::textChanged, [=](const QString& str) - { - JsonObject::Instance()->setInstitutionAddr(str); - }); - connect(lockTime, &QLineEdit::textChanged, [=](const QString& str) - { - //take effect - JsonObject::Instance()->setLockScreenTimeout(str); - Locker::Instance()->setTimer(JsonObject::Instance()->lockerCount()); + connect(instAddr, &QLineEdit::textChanged, [=](const QString& str) + { + JsonObject::Instance()->setInstitutionAddr(str); + }); + connect(lockTime, &QLineEdit::textChanged, [=](const QString& str) + { + //take effect + JsonObject::Instance()->setLockScreenTimeout(str); + Locker::getInstance()->setTimer(JsonObject::Instance()->lockerCount()); - }); + }); - connect(btnLan, &QPushButton::clicked, [=]() { - if (!dialog) { - dialog = new SelectDialog(this); - dialog->setWindowModality(Qt::WindowModal); - } + connect(btnLan, &QPushButton::clicked, [=]() { + if (!dialog) + { + dialog = new SelectDialog(this); + dialog->setWindowModality(Qt::WindowModal); + } dialog->setValues(JsonObject::Instance()->language()); - dialog->setSelectedValue(JsonObject::Instance()->defaultLanguage()); - if (dialog->exec() == QDialog::Accepted) - { - QString lan = dialog->getSelectedValue(); + dialog->setSelectedValue(JsonObject::Instance()->defaultLanguage()); + if (dialog->exec() == QDialog::Accepted) + { + QString lan = dialog->getSelectedValue(); - //take effect - JsonObject::Instance()->setDefaultLanguage(lan); - LanguageSwitcher::Instance()->setDefaultLanguage(lan); - btnLan->setText(JsonObject::Instance()->defaultLanguage()); + //take effect + JsonObject::Instance()->setDefaultLanguage(lan); + LanguageSwitcher::getInstance()->setDefaultLanguage(lan); + btnLan->setText(JsonObject::Instance()->defaultLanguage()); + } + }); - } - }); - - connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() { - lbl_lan->setText(tr("Language")); - lbl_ins->setText(tr("Institution Addr")); - lbl_insaddr->setText(tr("Institution Addr")); - lbl_lock->setText(tr("Lock Screen")); - }); + connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() { + lbl_lan->setText(tr("Language")); + lbl_ins->setText(tr("Institution Addr")); + lbl_insaddr->setText(tr("Institution Addr")); + lbl_lock->setText(tr("Lock Screen")); + }); } diff --git a/src/main.cpp b/src/main.cpp index df7f222..38cc07b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,99 +17,98 @@ #include "src/utilities/languageswitcher.h" QString loadFontFromFile(QString path) { - static QString font; - static bool loaded = false; - if (!loaded) - { - loaded = true; - int loadedFontID = QFontDatabase::addApplicationFont(path); - QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID); - if (!loadedFontFamilies.empty()) - font = loadedFontFamilies.at(0); - } - return font; + static QString font; + static bool loaded = false; + if (!loaded) + { + loaded = true; + int loadedFontID = QFontDatabase::addApplicationFont(path); + QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID); + if (!loadedFontFamilies.empty()) + font = loadedFontFamilies.at(0); + } + return font; } int main(int argc, char* argv[]) { - //QLocale::setDefault(QLocale::English); - qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard")); + //QLocale::setDefault(QLocale::English); + qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard")); #ifdef CUTE_STYLE - qputenv("QT_VIRTUALKEYBOARD_STYLE", QByteArray("retro")); + qputenv("QT_VIRTUALKEYBOARD_STYLE", QByteArray("retro")); #endif // CUTE_STYLE - //qputenv("QT_LOGGING_RULES", QByteArray("qt.virtualkeyboard=true")); + //qputenv("QT_LOGGING_RULES", QByteArray("qt.virtualkeyboard=true")); - QApplication a(argc, argv); - QString layouts_path = QString(QCoreApplication::applicationDirPath()).append("/layouts"); - qputenv("QT_VIRTUALKEYBOARD_LAYOUT_PATH", QByteArray(layouts_path.toStdString().c_str())); + QApplication a(argc, argv); + QString layouts_path = QString(QCoreApplication::applicationDirPath()).append("/layouts"); + qputenv("QT_VIRTUALKEYBOARD_LAYOUT_PATH", QByteArray(layouts_path.toStdString().c_str())); - QTextCodec* codec = QTextCodec::codecForName("utf8"); - QTextCodec::setCodecForLocale(codec); + QTextCodec* codec = QTextCodec::codecForName("utf8"); + QTextCodec::setCodecForLocale(codec); - //multi-language suppport - QTranslator translator; - LanguageSwitcher::Instance()->setTranslator(&translator); - LanguageSwitcher::Instance()->setDefaultLanguage(JsonObject::Instance()->defaultLanguage()); - a.installTranslator(&translator); + //multi-language suppport + LanguageSwitcher::getInstance()->setDefaultLanguage(JsonObject::Instance()->defaultLanguage()); + a.installTranslator(LanguageSwitcher::getInstance()->getTranslator()); - //QString fontName = loadFontFromFile(":/fonts/MicrosoftYahei.ttf"); - QString fontName = loadFontFromFile(":/fonts/DroidSansFallback.ttf"); - QFont font(fontName); - QApplication::setFont(font); + //QString fontName = loadFontFromFile(":/fonts/MicrosoftYahei.ttf"); + QString fontName = loadFontFromFile(":/fonts/DroidSansFallback.ttf"); + QFont font(fontName); + QApplication::setFont(font); - InputObject* obj = new InputObject(); - a.installEventFilter(obj); - SQLHelper::Open(); - MainWindow w; - UserOperationLog::Default()->init(); + InputObject* obj = new InputObject(); + a.installEventFilter(obj); + SQLHelper::Open(); + MainWindow w; + UserOperationLog::Default()->init(); - //暂时为了调试关闭锁屏 - //Locker::Instance()->start(); - //QObject::connect(obj, SIGNAL(touchScreen()), Locker::Instance(), SLOT(refreshTimer())); - QList gestures; - gestures << Qt::SwipeGesture; - gestures << Qt::PanGesture; - w.grabGestures(gestures); + //暂时为了调试关闭锁屏 + //Locker::Instance()->start(); + //QObject::connect(obj, SIGNAL(touchScreen()), Locker::Instance(), SLOT(refreshTimer())); + QList gestures; + gestures << Qt::SwipeGesture; + gestures << Qt::PanGesture; + w.grabGestures(gestures); - QStringList app_args = a.arguments(); - int ret = 0; + QStringList app_args = a.arguments(); + int ret = 0; - if (app_args.contains("-d")) - //if (true) - { + if (app_args.contains("-d")) + //if (true) + { - w.debugConsoleOn(); - w.showFullScreen(); + w.debugConsoleOn(); + w.showFullScreen(); - qInstallMessageHandler(MainWindow::QMessageOutput); - QThread thread; - StdOutRedirector redir; - redir.setOutputTF(w.getEdit()); - QTimer Timer; - redir.moveToThread(&thread); - QObject::connect(&Timer, SIGNAL(timeout()), &redir, SLOT(readOutsToTF())); - fflush(stdout); - Timer.start(1000); - thread.start(); + qInstallMessageHandler(MainWindow::QMessageOutput); + QThread thread; + StdOutRedirector redir; + redir.setOutputTF(w.getEdit()); + QTimer Timer; + redir.moveToThread(&thread); + QObject::connect(&Timer, SIGNAL(timeout()), &redir, SLOT(readOutsToTF())); + fflush(stdout); + Timer.start(1000); + thread.start(); - DeviceManager::Default()->initDevice(); - w.requestLogin(); + DeviceManager::Default()->initDevice(); + w.requestLogin(); - ret = a.exec(); - thread.terminate(); + ret = a.exec(); + thread.terminate(); - } - else { - w.showFullScreen(); - DeviceManager::Default()->initDevice(); - w.requestLogin(); - ret = a.exec(); - } - DeviceManager::Default()->close(); - return ret; + } + else + { + w.showFullScreen(); + DeviceManager::Default()->initDevice(); + w.requestLogin(); + ret = a.exec(); + } + DeviceManager::Default()->close(); + return ret; } diff --git a/src/utilities/InputObject.cpp b/src/utilities/InputObject.cpp index a4bfdcd..8c777ab 100644 --- a/src/utilities/InputObject.cpp +++ b/src/utilities/InputObject.cpp @@ -1,5 +1,6 @@ #include "InputObject.h" -#include "ui_inputobject.h" +#include "ui_InputObject.h" + #include #include #include @@ -10,250 +11,235 @@ #include #include -InputObject::InputObject(QWidget* parent) : - QWidget(parent), - ui(new Ui::InputObject) +InputObject::InputObject(QWidget* aParent) + : QWidget(aParent) + , mUI(new Ui::InputObject) + , mCurrentDateEdit(nullptr) + , mCurrentLineEdit(nullptr) + , mCurrentTextEdit(nullptr) { - ui->setupUi(this); - - initUi(); - - this->setVisible(false); - this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint); + mUI->setupUi(this); + initUi(); + setVisible(false); + setWindowFlags(Qt::Tool | Qt::FramelessWindowHint); } InputObject::~InputObject() { - delete ui; + delete mUI; } void InputObject::initUi() { - //#3c3c3c + //#3c3c3c #ifdef CUTE_STYLE - QString style = - "QWidget#WiBack{background-image: url(:/icons/bg.jpeg);}" - "QWidget#WiBo{background-color: black;}" - "QLineEdit,QDateEdit{background-color: rgba(0,0,0,0.5);\ + QString style = + "QWidget#WiBack{background-image: url(:/icons/bg.jpeg);}" + "QWidget#WiBo{background-color: black;}" + "QLineEdit,QDateEdit{background-color: rgba(0,0,0,0.5);\ min-height:100px;max-height:100px; min-width:500px;max-width:500px;\ border: 2px solid #ef9cba; border-radius:20px;\ color:white;margin:0;font-size:36px;}" - "QTextEdit{background-color: rgba(0,0,0,0.5);\ + "QTextEdit{background-color: rgba(0,0,0,0.5);\ min-height:300px;max-height:300px; min-width:700px;max-width:700px;\ border: 2px solid #ef9cba; border-radius:20px;\ font-family:Arial; color:white;margin:0;font-size:36px;}" - "QDateEdit::up-button{image: url(:/up.png);height:15px;}" - "QDateEdit::down-button{image:url(:/down.png);height:15px;}" - ; + "QDateEdit::up-button{image: url(:/up.png);height:15px;}" + "QDateEdit::down-button{image:url(:/down.png);height:15px;}" + ; #else - QString style = - "QWidget#WiBack{background-color: #383533;}" - "QWidget#WiBo{background-color: black;}" - "QLineEdit,QDateEdit,QTextEdit{\ + QString style = + "QWidget#WiBack{background-color: #383533;}" + "QWidget#WiBo{background-color: black;}" + "QLineEdit,QDateEdit,QTextEdit{\ background:qlineargradient(x1:0,y1:0,x2:1,y2:1,stop: 0.0 silver, stop: 1.0 grey);\ border: 1px solid #1e1b18; border-radius:20px;\ color:black;margin:0;font-size:36px;}" - "QLineEdit,QDateEdit{min-height:100px; max-height:100px; min-width:500px; max-width:500px;}" - "QTextEdit{min-height:300px;max-height:300px; min-width:700px;max-width:700px;}" + "QLineEdit,QDateEdit{min-height:100px; max-height:100px; min-width:500px; max-width:500px;}" + "QTextEdit{min-height:300px;max-height:300px; min-width:700px;max-width:700px;}" - "QDateEdit::up-button{image: url(:/up.png);height:15px;}" - "QDateEdit::down-button{image:url(:/down.png);height:15px;}" - ; + "QDateEdit::up-button{image: url(:/up.png);height:15px;}" + "QDateEdit::down-button{image:url(:/down.png);height:15px;}" + ; #endif - setStyleSheet(style); + setStyleSheet(style); - ui->dateEdit->setVisible(false); - ui->dateEdit->setMaximumDate(QDate::currentDate()); - ui->dateEdit->setDisplayFormat("yyyy/MM/dd"); - ui->dateEdit->setStyleSheet(style); - //this->setWindowOpacity(0.8); + mUI->dateEdit->setVisible(false); + mUI->dateEdit->setMaximumDate(QDate::currentDate()); + mUI->dateEdit->setDisplayFormat("yyyy/MM/dd"); + mUI->dateEdit->setStyleSheet(style); - connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, [=]() { - if (QGuiApplication::inputMethod()->isVisible()) return; - //qDebug() << "HidePanel"; - //qDebug() << "focusWidget" << qApp->focusWidget(); - this->HidePanel(); - this->hide(); - - }); + connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, [=]() { + if (QGuiApplication::inputMethod()->isVisible()) + { + return; + } + HidePanel(); + hide(); + }); } -bool InputObject::eventFilter(QObject* obj, QEvent* event) +bool InputObject::eventFilter(QObject* aObject, QEvent* aEvent) { - - if (event->type() == QEvent::MouseButtonPress) - { - emit touchScreen(); - - //qDebug() << "touchScreen()"; - } - if (event->type() == QEvent::KeyPress) { - QKeyEvent* keyEvent = static_cast(event); - if (keyEvent->key() == Qt::Key_Return && curDateEdit) - { - this->HidePanel(); - this->hide(); - } - } - //mouse button release means setfocus is useless - if (event->type() == QEvent::MouseButtonRelease) - { - //qDebug() << obj << event->type(); - - if (obj->objectName() == QString("qt_spinbox_lineedit")) - { - QDateEdit* _date = qobject_cast(obj->parent()); - if (_date == ui->dateEdit)return true; - if (_date->isEnabled()) - { - this->ShowPanel(); - return true; - } - } - if (obj->objectName() == QString("qt_scrollarea_viewport")) - { - QTextEdit* _text = qobject_cast(obj->parent()); - if (!_text) return false; - if (_text == ui->textEdit) return true; // - if (_text->isEnabled()) - { - this->ShowPanel(); - return true; - } - } - - if (obj->inherits("QLineEdit")) { - QLineEdit* tbx = qobject_cast(obj); - //you have to tell whether it is coming from qdateedit!!! - if (ui->lineEdit != tbx && tbx->isEnabled()) - { - //qDebug() << "ShowPanel"; - //qDebug() << "focusWidget" << qApp->focusWidget(); - //qDebug() << "lineEdit" << ui->lineEdit; - //qDebug() << "obj" << obj; - this->ShowPanel(); - return true; - } - } - - - - - - } - return QObject::eventFilter(obj, event); + if (aEvent->type() == QEvent::MouseButtonPress) + { + emit touchScreen(); + } + if (aEvent->type() == QEvent::KeyPress) + { + QKeyEvent* keyEvent = static_cast(aEvent); + if (keyEvent->key() == Qt::Key_Return && mCurrentDateEdit) + { + HidePanel(); + hide(); + } + } + //mouse button release means setfocus is useless + if (aEvent->type() == QEvent::MouseButtonRelease) + { + if (aObject->objectName() == QString("qt_spinbox_lineedit")) + { + QDateEdit* dateEdit = qobject_cast(aObject->parent()); + if (dateEdit == mUI->dateEdit) + { + return true; + } + if (dateEdit->isEnabled()) + { + ShowPanel(); + return true; + } + } + if (aObject->objectName() == QString("qt_scrollarea_viewport")) + { + QTextEdit* textEdit = qobject_cast(aObject->parent()); + if (!textEdit) + { + return false; + } + if (textEdit == mUI->textEdit) + { + return true; + } + if (textEdit->isEnabled()) + { + ShowPanel(); + return true; + } + } + if (aObject->inherits("QLineEdit")) + { + QLineEdit* lineEdit = qobject_cast(aObject); + //you have to tell whether it is coming from qdateedit!!! + if (mUI->lineEdit != lineEdit && lineEdit->isEnabled()) + { + ShowPanel(); + return true; + } + } + } + return QObject::eventFilter(aObject, aEvent); } -void InputObject::ShowPanel() { - if (!qApp->focusWidget()) return; - if (qApp->focusWidget()->inherits("QLineEdit")) - { - curLine = qobject_cast(qApp->focusWidget()); - ui->dateEdit->setVisible(false); - ui->lineEdit->setVisible(true); - ui->textEdit->setVisible(false); - } - if (qApp->focusWidget()->inherits("QDateEdit")) - { - curDateEdit = qobject_cast(qApp->focusWidget()); - ui->dateEdit->setVisible(true); - ui->lineEdit->setVisible(false); - ui->textEdit->setVisible(false); - } +void InputObject::ShowPanel() +{ + if (!qApp->focusWidget()) + { + return; + } + if (qApp->focusWidget()->inherits("QLineEdit")) + { + mCurrentLineEdit = qobject_cast(qApp->focusWidget()); + mUI->dateEdit->setVisible(false); + mUI->lineEdit->setVisible(true); + mUI->textEdit->setVisible(false); + } + if (qApp->focusWidget()->inherits("QDateEdit")) + { + mCurrentDateEdit = qobject_cast(qApp->focusWidget()); + mUI->dateEdit->setVisible(true); + mUI->lineEdit->setVisible(false); + mUI->textEdit->setVisible(false); + } - if (qApp->focusWidget()->inherits("QTextEdit")) - { - curText = qobject_cast(qApp->focusWidget()); - ui->dateEdit->setVisible(false); - ui->lineEdit->setVisible(false); - ui->textEdit->setVisible(true); - } + if (qApp->focusWidget()->inherits("QTextEdit")) + { + mCurrentTextEdit = qobject_cast(qApp->focusWidget()); + mUI->dateEdit->setVisible(false); + mUI->lineEdit->setVisible(false); + mUI->textEdit->setVisible(true); + } - this->showFullScreen(); - this->setVisible(true); + showFullScreen(); + setVisible(true); - this->activateWindow(); //it is quite important!!! - while (QApplication::activeWindow() != this) - { - qApp->processEvents(); - } + activateWindow(); //it is quite important!!! + while (QApplication::activeWindow() != this) + { + qApp->processEvents(); + } - if (curLine) { - ui->lineEdit->setText(curLine->text()); - ui->lineEdit->setEchoMode(curLine->echoMode()); - ui->lineEdit->setInputMethodHints(Qt::InputMethodHint::ImhNoAutoUppercase); - //ui->lineEdit->setFocus(Qt::MouseFocusReason); - ui->lineEdit->setFocus(); + if (mCurrentLineEdit) + { + mUI->lineEdit->setText(mCurrentLineEdit->text()); + mUI->lineEdit->setEchoMode(mCurrentLineEdit->echoMode()); + mUI->lineEdit->setInputMethodHints(Qt::InputMethodHint::ImhNoAutoUppercase); + mUI->lineEdit->setFocus(); - //while (QGuiApplication::focusObject() != ui->lineEdit) - //{ - // qApp->processEvents(); - //} + QEvent event(QEvent::RequestSoftwareInputPanel); + QApplication::sendEvent(mUI->lineEdit, &event); + } + if (mCurrentDateEdit) + { - QEvent event(QEvent::RequestSoftwareInputPanel); - QApplication::sendEvent(ui->lineEdit, &event); + mUI->dateEdit->setDateTime(mCurrentDateEdit->dateTime()); + mUI->dateEdit->setFocus(); + while (QGuiApplication::focusObject() != mUI->dateEdit) + { + qApp->processEvents(); + } + qDebug() << QGuiApplication::focusObject(); + QEvent event(QEvent::RequestSoftwareInputPanel); + QApplication::sendEvent(mUI->dateEdit, &event); + } - } - if (curDateEdit) - { - - ui->dateEdit->setDateTime(curDateEdit->dateTime()); - ui->dateEdit->setFocus(); - while (QGuiApplication::focusObject() != ui->dateEdit) - { - qApp->processEvents(); - } - qDebug() << QGuiApplication::focusObject(); - QEvent event(QEvent::RequestSoftwareInputPanel); - QApplication::sendEvent(ui->dateEdit, &event); - } - - if (curText) - { - ui->textEdit->setPlainText(curText->toPlainText()); - ui->textEdit->setFocus(); - while (QGuiApplication::focusObject() != ui->textEdit) - { - qApp->processEvents(); - } - //qDebug() << QGuiApplication::focusObject(); - QEvent event(QEvent::RequestSoftwareInputPanel); - QApplication::sendEvent(ui->textEdit, &event); - } + if (mCurrentTextEdit) + { + mUI->textEdit->setPlainText(mCurrentTextEdit->toPlainText()); + mUI->textEdit->setFocus(); + while (QGuiApplication::focusObject() != mUI->textEdit) + { + qApp->processEvents(); + } + QEvent event(QEvent::RequestSoftwareInputPanel); + QApplication::sendEvent(mUI->textEdit, &event); + } } - void InputObject::HidePanel() { - if (curLine) { - curLine->setText(ui->lineEdit->text()); - ui->lineEdit->clearFocus(); - ui->lineEdit->clear(); - //this->setVisible(false); - curLine = nullptr; - } - else if (curDateEdit) - { - //curDateEdit->setDateTime(QDateTime::currentDateTime()); - curDateEdit->setDateTime(ui->dateEdit->dateTime()); - ui->dateEdit->clearFocus(); - ui->dateEdit->clear(); - //this->setVisible(false); - curDateEdit = nullptr; - } - else if (curText) - { - curText->setText(ui->textEdit->toPlainText()); - ui->textEdit->clearFocus(); - ui->textEdit->clear(); - //this->setVisible(false); - curText = nullptr; - } - else - { - return; - } + if (mCurrentLineEdit) + { + mCurrentLineEdit->setText(mUI->lineEdit->text()); + mUI->lineEdit->clearFocus(); + mUI->lineEdit->clear(); + mCurrentLineEdit = nullptr; + } + else if (mCurrentDateEdit) + { + mCurrentDateEdit->setDateTime(mUI->dateEdit->dateTime()); + mUI->dateEdit->clearFocus(); + mUI->dateEdit->clear(); + mCurrentDateEdit = nullptr; + } + else if (mCurrentTextEdit) + { + mCurrentTextEdit->setText(mUI->textEdit->toPlainText()); + mUI->textEdit->clearFocus(); + mUI->textEdit->clear(); + mCurrentTextEdit = nullptr; + } } diff --git a/src/utilities/InputObject.h b/src/utilities/InputObject.h index 90ae424..a2096dc 100644 --- a/src/utilities/InputObject.h +++ b/src/utilities/InputObject.h @@ -2,35 +2,36 @@ #define INPUTOBJECT_H #include + class QLineEdit; class QDateEdit; class QTextEdit; -//#define CUTE_STYLE - -namespace Ui { - class InputObject; +namespace Ui +{ + class InputObject; } class InputObject : public QWidget { - Q_OBJECT + Q_OBJECT public: - explicit InputObject(QWidget* parent = nullptr); - ~InputObject(); - bool eventFilter(QObject* obj, QEvent* event); - void ShowPanel(); + explicit InputObject(QWidget* aParent = nullptr); + ~InputObject(); + bool eventFilter(QObject* aObject, QEvent* aEvent); + signals: - void touchScreen(); + void touchScreen(); + private: - Ui::InputObject* ui; - void initUi(); - // void ShowPanel(); - void HidePanel(); - QDateEdit* curDateEdit = nullptr; - QLineEdit* curLine = nullptr; - QTextEdit* curText = nullptr; + Ui::InputObject* mUI; + void initUi(); + void ShowPanel(); + void HidePanel(); + QDateEdit* mCurrentDateEdit = nullptr; + QLineEdit* mCurrentLineEdit = nullptr; + QTextEdit* mCurrentTextEdit = nullptr; }; #endif // INPUTOBJECT_H diff --git a/src/utilities/inputobject.ui b/src/utilities/InputObject.ui similarity index 100% rename from src/utilities/inputobject.ui rename to src/utilities/InputObject.ui diff --git a/src/utilities/LanguageSwitcher.cpp b/src/utilities/LanguageSwitcher.cpp new file mode 100644 index 0000000..bf9b8a6 --- /dev/null +++ b/src/utilities/LanguageSwitcher.cpp @@ -0,0 +1,38 @@ +#include "LanguageSwitcher.h" + +#include "event/EventCenter.h" + +LanguageSwitcher* LanguageSwitcher::getInstance() +{ + static LanguageSwitcher instance; + return &instance; +} +LanguageSwitcher::LanguageSwitcher() + : QObject() + , mTranslator(new QTranslator(this)) +{ +} + +LanguageSwitcher::~LanguageSwitcher() +{ +} + +void LanguageSwitcher::setTranslator(TranslatorPointer aTranslator) +{ + mTranslator = aTranslator; +} + +QTranslator* LanguageSwitcher::getTranslator() +{ + return mTranslator.get(); +} + +void LanguageSwitcher::setDefaultLanguage(const QString aLanguage) +{ + QString language = QString(":/translations/" + aLanguage + ".qm"); + if (mTranslator->load(language)) + { + EventCenter::Default()->triggerEvent(ReloadLanguage, nullptr, nullptr); + } +} + diff --git a/src/utilities/LanguageSwitcher.h b/src/utilities/LanguageSwitcher.h new file mode 100644 index 0000000..48d676c --- /dev/null +++ b/src/utilities/LanguageSwitcher.h @@ -0,0 +1,25 @@ +#ifndef LANGUAGESWITCHER_H +#define LANGUAGESWITCHER_H + +#include +#include +#include + +typedef std::shared_ptr TranslatorPointer; + +class LanguageSwitcher : public QObject +{ + Q_OBJECT +public: + static LanguageSwitcher* getInstance(); + void setDefaultLanguage(const QString aLanguage); + void setTranslator(TranslatorPointer aTranslator); + QTranslator* getTranslator(); + +private: + explicit LanguageSwitcher(); + ~LanguageSwitcher(); + TranslatorPointer mTranslator; +}; + +#endif // LANGUAGESWITCHER_H \ No newline at end of file diff --git a/src/utilities/Locker.cpp b/src/utilities/Locker.cpp new file mode 100644 index 0000000..5ecc990 --- /dev/null +++ b/src/utilities/Locker.cpp @@ -0,0 +1,53 @@ +#include "Locker.h" + +#include +#include "appvals/AppGlobalValues.h" +#include "event/EventCenter.h" +#include "json/jsonobject.h" + +Locker* Locker::getInstance() +{ + static Locker instance; + return &instance; +} + +Locker::Locker() + : QObject() + , mScreenTimer(new QTimer(this)) + , mCounter(JsonObject::Instance()->lockerCount()) +{ + connect(mScreenTimer, SIGNAL(timeout()), this, SLOT(coverScreen())); +} + +Locker::~Locker() +{ +} + +void Locker::start() +{ + mScreenTimer->start(mCounter); +} + +void Locker::coverScreen() +{ + if (AppGlobalValues::InProcessing().toBool()) + { + refreshTimer(); + } + else + { + mScreenTimer->stop(); + EventCenter::Default()->triggerEvent(GUIEvents::RequestLogin, nullptr, nullptr); + } +} +void Locker::setTimer(int aInterval) +{ + mCounter = aInterval; + mScreenTimer->stop(); + mScreenTimer->start(mCounter); +} +void Locker::refreshTimer() +{ + mScreenTimer->stop(); + mScreenTimer->start(mCounter); +} diff --git a/src/utilities/Locker.h b/src/utilities/Locker.h new file mode 100644 index 0000000..863f1e6 --- /dev/null +++ b/src/utilities/Locker.h @@ -0,0 +1,26 @@ +#ifndef LOCKER_H +#define LOCKER_H + +#include + +class QTimer; +class Locker : public QObject +{ + Q_OBJECT +public: + static Locker* getInstance(); + void start(); + void setTimer(int aInterval); + +public slots: + void refreshTimer(); + void coverScreen(); +private: + Locker(); + ~Locker(); + + QTimer* mScreenTimer; + int mCounter; +}; + +#endif // LOCKER_H diff --git a/src/utilities/StdOutRedirector.cpp b/src/utilities/StdOutRedirector.cpp index 7b79204..35f4c0a 100644 --- a/src/utilities/StdOutRedirector.cpp +++ b/src/utilities/StdOutRedirector.cpp @@ -1,67 +1,63 @@ #include "StdOutRedirector.h" + #include #include + StdOutRedirector::StdOutRedirector() - : QObject() + : QObject() { #ifdef __linux__ - if (pipe(fdguistd) == -1) - printf("failed!"); + if (pipe(mFdguistd) == -1) + printf("failed!"); #else - // Redirect - if (_pipe(fdguistd, 4096, _O_BINARY) == -1) - printf("failed!"); + // Redirect + if (_pipe(mFdguistd, 4096, _O_BINARY) == -1) + printf("failed!"); #endif - //int tr = fcntl(fdguistd, O_NONBLOCK); - // Duplicate stdout file descriptor (next line will close original) - fdStdOut = dup(fileno(stdout)); - // Duplicate write end of pipe to stdout file descriptor - if (dup2(fdguistd[1], fileno(stdout)) != 0) - printf("failed!"); - // Close original - close(1); - // Duplicate write end of original - dup2(fdguistd[1], 1); + //int tr = fcntl(fdguistd, O_NONBLOCK); + // Duplicate stdout file descriptor (next line will close original) + mFdStdOut = dup(fileno(stdout)); + // Duplicate write end of pipe to stdout file descriptor + if (dup2(mFdguistd[1], fileno(stdout)) != 0) + printf("failed!"); + // Close original + close(1); + // Duplicate write end of original + dup2(mFdguistd[1], 1); - buffer = new char[bufferSize]; + mBuffer = new char[mBufferSize]; } StdOutRedirector::~StdOutRedirector() { - delete[] buffer; + delete[] mBuffer; } void StdOutRedirector::readOutsToTF() { + size_t len; + char* copy; - size_t len; - char* copy; - - int n_out; - QString str; - //char buffer[512]; - //qDebug() << "begin read..."; - //qDebug() << "from qdebug..."; - //printf("from printf..."); - //std::cout << "from std::cout..." << std::endl; - fflush(stdout); - //Perhaps there is a non-blocking version of _read() that you can call ? - memset(buffer, 0, bufferSize); - n_out = read(fdguistd[0], buffer, bufferSize); - if (n_out <= 0) { - return; - } - if (n_out >= 1) { - str.append(QString(buffer)); - int con = str.lastIndexOf('\n'); - int remv = str.at(con - 1) == '\n' ? 1 : 0; - if (con > 0) { - str = str.remove(con - remv, str.length()); - output->append(str); - } - } - return; + int n_out; + QString str; + fflush(stdout); + //Perhaps there is a non-blocking version of _read() that you can call ? + memset(mBuffer, 0, mBufferSize); + n_out = read(mFdguistd[0], mBuffer, mBufferSize); + if (n_out <= 0) + { + return; + } + if (n_out >= 1) + { + str.append(QString(mBuffer)); + int con = str.lastIndexOf('\n'); + int remv = str.at(con - 1) == '\n' ? 1 : 0; + if (con > 0) + { + str = str.remove(con - remv, str.length()); + mOutput->append(str); + } + } + return; } - - - diff --git a/src/utilities/StdOutRedirector.h b/src/utilities/StdOutRedirector.h index 2242883..ebda38d 100644 --- a/src/utilities/StdOutRedirector.h +++ b/src/utilities/StdOutRedirector.h @@ -2,16 +2,6 @@ #include -//class StdOutRedirector : public QObject -//{ -// Q_OBJECT -// -//public: -// StdOutRedirector(QObject *parent); -// ~StdOutRedirector(); -//}; - - #include #include #include @@ -26,22 +16,22 @@ class StdOutRedirector : public QObject { - Q_OBJECT + Q_OBJECT public: - StdOutRedirector(); - ~StdOutRedirector(); - void setOutputTF(QTextEdit* _output) - { - output = _output; - } + StdOutRedirector(); + ~StdOutRedirector(); + void setOutputTF(QTextEdit* _output) + { + mOutput = _output; + } public slots: - void readOutsToTF(); + void readOutsToTF(); private: - QTextEdit* output; - int fdStdOut; - int fdguistd[2]; - char* buffer = nullptr; - const int bufferSize = 4096; + QTextEdit* mOutput; + int mFdStdOut; + int mFdguistd[2]; + char* mBuffer = nullptr; + const int mBufferSize = 4096; }; diff --git a/src/utilities/languageswitcher.cpp b/src/utilities/languageswitcher.cpp deleted file mode 100644 index 0e8a75e..0000000 --- a/src/utilities/languageswitcher.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "languageswitcher.h" -#include -#include "event/EventCenter.h" - -LanguageSwitcher* LanguageSwitcher::lsw = Q_NULLPTR; - -LanguageSwitcher* LanguageSwitcher::Instance() -{ - if (lsw == Q_NULLPTR) - { - lsw = new LanguageSwitcher(); - - } - return lsw; -} -LanguageSwitcher::LanguageSwitcher() -{} - -LanguageSwitcher::~LanguageSwitcher() -{} - -void LanguageSwitcher::setTranslator(QTranslator* tra) -{ - this->translator = tra; -} - -void LanguageSwitcher::setDefaultLanguage(QString str) -{ - QString lan = QString(":/translations/" + str + ".qm"); - if (translator->load(lan)) - { - EventCenter::Default()->triggerEvent(ReloadLanguage, nullptr, nullptr); - } - -} - diff --git a/src/utilities/languageswitcher.h b/src/utilities/languageswitcher.h deleted file mode 100644 index d119fc6..0000000 --- a/src/utilities/languageswitcher.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef LANGUAGESWITCHER_H -#define LANGUAGESWITCHER_H - -#include - -class QTranslator; -class LanguageSwitcher : public QObject -{ - Q_OBJECT -public: - static LanguageSwitcher* Instance(); - void setDefaultLanguage(QString str); - void setTranslator(QTranslator* tra); -signals: - -public slots: - -private: - explicit LanguageSwitcher(); - ~LanguageSwitcher(); - static LanguageSwitcher* lsw; - QTranslator* translator = nullptr; -}; - -#endif // LANGUAGESWITCHER_H \ No newline at end of file diff --git a/src/utilities/locker.cpp b/src/utilities/locker.cpp deleted file mode 100644 index cddb6fa..0000000 --- a/src/utilities/locker.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "locker.h" - -#include -#include "appvals/AppGlobalValues.h" -#include "event/EventCenter.h" -#include "json/jsonobject.h" -Locker* Locker::locker = Q_NULLPTR; - -Locker* Locker::Instance() -{ - if (locker == Q_NULLPTR) - { - locker = new Locker(); - } - return locker; -} -Locker::Locker() -{ - counter = JsonObject::Instance()->lockerCount(); - screenTimer = new QTimer(this); - connect(screenTimer, SIGNAL(timeout()), this, SLOT(coverScreen())); -} -Locker::~Locker() {} - -void Locker::start() -{ - screenTimer->start(counter); -} -void Locker::coverScreen() -{ - - if (AppGlobalValues::InProcessing().toBool()) { - refreshTimer(); - } - else - { - screenTimer->stop(); - EventCenter::Default()->triggerEvent(GUIEvents::RequestLogin, nullptr, nullptr); - } -} -void Locker::setTimer(int interval) -{ - this->counter = interval; - screenTimer->stop(); - screenTimer->start(this->counter); - -} -void Locker::refreshTimer() -{ - screenTimer->stop(); - screenTimer->start(counter); -} diff --git a/src/utilities/locker.h b/src/utilities/locker.h deleted file mode 100644 index 3b7b3e3..0000000 --- a/src/utilities/locker.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef LOCKER_H -#define LOCKER_H - -#include - -class QTimer; -class Locker : public QObject -{ - Q_OBJECT -public: - static Locker* Instance(); - void start(); - void setTimer(int interval); - -public slots: - void refreshTimer(); - void coverScreen(); -private: - Locker(); - ~Locker(); - - QTimer* screenTimer = nullptr; - int counter = 30000; - - static Locker* locker; -}; - -#endif // LOCKER_H