From 32ebb0722d6f74b97ef399404397e9c8bde06ed2 Mon Sep 17 00:00:00 2001 From: Krad Date: Fri, 19 Nov 2021 13:20:17 +0800 Subject: [PATCH] Merge change ,debug window, logout --- CMakeLists.txt | 1 + src/AccountFormDialog.cpp | 4 +-- src/StdOutRedirector.cpp | 62 ++++++++++++++++++++++++++++++++ src/StdOutRedirector.h | 45 ++++++++++++++++++++++++ src/event/EventCenter.h | 1 + src/loginwindow.cpp | 2 +- src/main.cpp | 45 +++++++++++++++--------- src/mainwindow.cpp | 74 ++++++++++++++++++++++++++++++++++++--- src/mainwindow.h | 24 ++++++++----- 9 files changed, 225 insertions(+), 33 deletions(-) create mode 100644 src/StdOutRedirector.cpp create mode 100644 src/StdOutRedirector.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 468daaa..928c393 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ set(PROJECT_NAME GUI) project(${PROJECT_NAME}) set(CMAKE_CXX_STANDARD 11) set(CMAKE_INCLUDE_CURRENT_DIR ON) +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -pthread") file(GLOB_RECURSE project_headers ./src/*.h) file(GLOB_RECURSE project_cpps ./src/*.cpp) diff --git a/src/AccountFormDialog.cpp b/src/AccountFormDialog.cpp index 24b4e63..8ee0feb 100644 --- a/src/AccountFormDialog.cpp +++ b/src/AccountFormDialog.cpp @@ -1,7 +1,6 @@ // // Created by Krad on 2021/11/10. // - #include "AccountFormDialog.h" #include "ChangePasswordFormDialog.h" #include @@ -9,6 +8,7 @@ #include #include #include +#include #include "db/SQLHelper.h" #include "models/User.h" AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) { @@ -84,7 +84,7 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo connect(btn_Logout, &QAbstractButton::clicked, [=](){ this->accept(); - this->parentWidget()->parentWidget()->parentWidget()->hide(); + EventCenter::Default()->triggerEvent(GUIEvents::RequestLogin, nullptr, nullptr); }); } diff --git a/src/StdOutRedirector.cpp b/src/StdOutRedirector.cpp new file mode 100644 index 0000000..4a36dd5 --- /dev/null +++ b/src/StdOutRedirector.cpp @@ -0,0 +1,62 @@ +#include "StdOutRedirector.h" +#include +#include +StdOutRedirector::StdOutRedirector() + : QObject() +{ +#ifdef __linux__ + if (pipe(fdguistd) == -1) + printf("failed!"); +#else + // Redirect + if (_pipe(fdguistd, 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); +} + +StdOutRedirector::~StdOutRedirector() +{ +} + +void StdOutRedirector::readOutsToTF() +{ + + int n_out; + char* buffer = new char[4096]; + QString str; + //char buffer[512]; + //qDebug() << "from qdebug..."; + //printf("from printf...\n"); + //std::cout << "from std::cout..." << std::endl; + fflush(stdout); + //Perhaps there is a non-blocking version of _read() that you can call ? + n_out = read(fdguistd[0], buffer, 4096); + + 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) { + str = str.remove(con - remv, str.length()); + output->append(str); + } + } + + + +} + + + diff --git a/src/StdOutRedirector.h b/src/StdOutRedirector.h new file mode 100644 index 0000000..788383c --- /dev/null +++ b/src/StdOutRedirector.h @@ -0,0 +1,45 @@ +#pragma once + +#include + +//class StdOutRedirector : public QObject +//{ +// Q_OBJECT +// +//public: +// StdOutRedirector(QObject *parent); +// ~StdOutRedirector(); +//}; + + +#include +#include +#include +#include +#include + +#ifdef __linux__ +#include +#else +#include +#endif + +class StdOutRedirector : public QObject +{ + Q_OBJECT +public: + StdOutRedirector(); + ~StdOutRedirector(); + void setOutputTF(QTextEdit* _output) + { + output = _output; + } + +public slots: + void readOutsToTF(); + +private: + QTextEdit* output; + int fdStdOut; + int fdguistd[2]; +}; diff --git a/src/event/EventCenter.h b/src/event/EventCenter.h index b15635b..555462f 100644 --- a/src/event/EventCenter.h +++ b/src/event/EventCenter.h @@ -8,6 +8,7 @@ #include #define ADD_EVENT()\ +ADD_EVENT_VALUE(RequestLogin)\ ADD_EVENT_VALUE(PatientSelected)\ ADD_EVENT_VALUE(RequestPreviewScan)\ ADD_EVENT_VALUE(RequestEmptyScan)\ diff --git a/src/loginwindow.cpp b/src/loginwindow.cpp index 4e861ea..701cfa1 100644 --- a/src/loginwindow.cpp +++ b/src/loginwindow.cpp @@ -135,7 +135,7 @@ void LoginWindow::doLogin() QString strPassWord = m_pPassWordEdit->text(); QString encryptedPassword = strPassWord; - strPassWord = "123456"; + strPassWord = "12345678"; diff --git a/src/main.cpp b/src/main.cpp index 3e0b394..8766922 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,10 @@ #include #include #include +#include +#include +#include + QString loadFontFromFile(QString path) { static QString font; @@ -49,22 +53,31 @@ int main(int argc, char* argv[]) //a.installEventFilter(obj); InputObject* obj = new InputObject(); a.installEventFilter(obj); - SQLHelper::Open(); - MainWindow w; - w.showFullScreen(); + SQLHelper::Open(); + MainWindow w; + + + QStringList app_args = a.arguments(); + if (app_args.contains("-d")) { + w.debugConsoleOn(); + 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(); + w.showFullScreen(); + w.requestLogin(); + return a.exec(); + } else{ + w.showFullScreen(); + w.requestLogin(); + return a.exec(); + } - LoginWindow l; - needLogin: - l.showFullScreen(); - int rec = l.exec(); - if (rec != QDialog::Accepted) { - return 0; - } - } -// l.deleteLater(); - w.centerWidgetShow(); - - - return a.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3bcc154..1b2efba 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -9,6 +9,35 @@ #include "ScanFormWidget.h" #include "guimessagedialog.h" #include "device/DeviceManager.h" +#include "loginwindow.h" +#include +#include + + +void MainWindow::QMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) +{ + + std::cout << msg.toStdString().c_str() << std::endl; + + //QByteArray localMsg = msg.toLocal8Bit(); + //switch (type) { + //case QtDebugMsg: + // fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + // break; + //case QtInfoMsg: + // fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + // break; + //case QtWarningMsg: + // fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + // break; + //case QtCriticalMsg: + // fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + // break; + //case QtFatalMsg: + // fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + // abort(); + //} +} MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), @@ -35,14 +64,14 @@ MainWindow::MainWindow(QWidget* parent) : "QLineEdit:enabled{background-color: #515151}" "QDateEdit{min-height:36px;max-height:36px; border:1px solid silver}" "QDateEdit:enabled{background-color: #515151}" - "QTextEdit{border:1px solid silver}" + //"QTextEdit{background-attachment: scroll; }" "QTextEdit:enabled{background-color: #515151}" "QComboBox{text-align:center;min-height:36px;max-height:36px; border:1px solid silver}" "QComboBox:enabled{background-color: #515151}" "QComboBox::drop-down{width:20px}" "QComboBox QAbstractItemView{min-width:120px;}" "QComboBox QAbstractItemView::item {min-height:60px;max-height:60px; border:1px solid white;}" - "QScrollBar:vertical {min-width: 50px;}" + //"QScrollBar:vertical {min-width: 50px;}" "QLabel{color:white; font-weight:bold; font-size:16px;}\n" "QWidget#topbarWidget{min-height:36px;max-height:36px;}\n" "QWidget#contentWidget{border-top:1px solid #515151;}\n" @@ -91,6 +120,7 @@ MainWindow::MainWindow(QWidget* parent) : QHBoxLayout* layout = new QHBoxLayout(); layout->setMargin(0); layout->addWidget(tab); + ui->centralWidget->setLayout(layout); this->setWindowFlags(Qt::Window); connect(EventCenter::Default(), &EventCenter::GUIErrorRaise, [=](QObject*, QObject* msg) { @@ -150,19 +180,53 @@ MainWindow::MainWindow(QWidget* parent) : delete msgDialog; msgDialog = nullptr; }); - DeviceManager::Default()->initDevice(); + connect(EventCenter::Default(), &EventCenter::RequestLogin, [=](QObject*, QObject* msg) { + this->requestLogin(); + }); + DeviceManager::Default()->initDevice(); centerWidgetHide(); } MainWindow::~MainWindow() { delete ui; + delete redir; } void MainWindow::centerWidgetHide() { - ui->centralWidget->hide(); + ui->centralWidget->setVisible(false); } void MainWindow::centerWidgetShow() { - ui->centralWidget->show(); + qApp->processEvents(); + ui->centralWidget->setVisible(true); +} + + +QTextEdit* MainWindow::getEdit() +{ + return this->console; +} + +void MainWindow::requestLogin() { + LoginWindow l(this); + l.showFullScreen(); + this->centerWidgetHide(); + while(l.result() != QDialog::Accepted) + { + l.exec(); + } + this->centerWidgetShow(); +} + +void MainWindow::debugConsoleOn() { + QTextEdit* text_edit = new QTextEdit(this); + ui->centralWidget->layout()->addWidget(text_edit); + const QString edit_style = + "QScrollBar:vertical{border: 0px solid grey; background:#2d2d2d; width: 15px; margin: 0px 0 0px 0; }" + "QScrollBar::handle:vertical{background:#5a5a5a;min-height: 25px;}" + "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical{background:#2d2d2d;}"; + text_edit->verticalScrollBar()->setStyleSheet(edit_style); + //clear buffer before redirect; + this->console = text_edit; } diff --git a/src/mainwindow.h b/src/mainwindow.h index 122aab6..2ab61cd 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -2,23 +2,29 @@ #define MAINWINDOW_H #include - +#include "StdOutRedirector.h" namespace Ui { -class MainWindow; + class MainWindow; } class GUIMessageDialog; class MainWindow : public QMainWindow { - Q_OBJECT + Q_OBJECT public: - explicit MainWindow(QWidget *parent = nullptr); - ~MainWindow(); - void centerWidgetHide(); - void centerWidgetShow(); + explicit MainWindow(QWidget* parent = nullptr); + ~MainWindow(); + static void QMessageOutput(QtMsgType, const QMessageLogContext&, const QString& msg); + void centerWidgetHide(); + void centerWidgetShow(); + void requestLogin(); + QTextEdit* getEdit(); + void debugConsoleOn(); private: - Ui::MainWindow *ui; - GUIMessageDialog* msgDialog = nullptr; + Ui::MainWindow* ui; + GUIMessageDialog* msgDialog = nullptr; + StdOutRedirector* redir = nullptr; + QTextEdit* console = nullptr; }; #endif // MAINWINDOW_H