diff --git a/lib_fiber/samples-gui/QtFiber/childwindows.cpp b/lib_fiber/samples-gui/QtFiber/childwindows.cpp deleted file mode 100644 index ec44ee877..000000000 --- a/lib_fiber/samples-gui/QtFiber/childwindows.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "stdafx.h" -#include -#include -#include "childwindows.h" - -ChildWindows::ChildWindows(QWidget *parent) -: QWidget(parent) -{ - setWindowTitle("The child window"); - - QVBoxLayout *layout = new QVBoxLayout(this); - closeButton = new QPushButton("Close", this); - layout->addWidget(closeButton); - - closeButton = new QPushButton("Close", this); - closeButton->setGeometry(QRect(QPoint(50, 50), QSize(100, 30))); - connect(closeButton, &QPushButton::clicked, this, &ChildWindows::onCloseButtonClicked); - - setFixedSize(300, 200); -} - -void ChildWindows::onCloseButtonClicked() -{ - this->close(); -} - -void ChildWindows::showEvent(QShowEvent *event) -{ - QWidget::showEvent(event); - qDebug() << "Second window is shown"; -} diff --git a/lib_fiber/samples-gui/QtFiber/childwindows.h b/lib_fiber/samples-gui/QtFiber/childwindows.h deleted file mode 100644 index f4aac632a..000000000 --- a/lib_fiber/samples-gui/QtFiber/childwindows.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef CHILDWINDOWS_H -#define CHILDWINDOWS_H - -#include -#include - -class ChildWindows : public QWidget -{ - Q_OBJECT -public: - explicit ChildWindows(QWidget *parent = nullptr); - ~ChildWindows() = default; - -protected: - void showEvent(QShowEvent *event) override; - -private slots: - void onCloseButtonClicked(); - -private: - QPushButton *closeButton; -}; - -#endif // CHILDWINDOWS_H diff --git a/lib_fiber/samples-gui/QtFiber/fiber_client.cpp b/lib_fiber/samples-gui/QtFiber/fiber_client.cpp index 753a8ce78..3a78ecca7 100644 --- a/lib_fiber/samples-gui/QtFiber/fiber_client.cpp +++ b/lib_fiber/samples-gui/QtFiber/fiber_client.cpp @@ -1,9 +1,10 @@ #include "stdafx.h" #include "patch.h" +#include "mainwindow.h" #include "fiber_client.h" -fiber_client::fiber_client(const char *ip, int port, size_t max, int delay) -: ip_(ip), port_(port), max_(max), delay_(delay) +fiber_client::fiber_client(MainWindow *parent, const char *ip, int port, size_t max, int delay) +: parent_(parent), ip_(ip), port_(port), max_(max), delay_(delay) { } @@ -22,6 +23,8 @@ void fiber_client::run() { char buf[4096]; std::string s("hello world!"); + parent_->setProgress(0); + for (size_t i = 0; i < max_; i++) { int ret = acl_fiber_send(conn, s.c_str(), (int) s.size(), 0); if (ret <= 0) { @@ -40,11 +43,14 @@ void fiber_client::run() { qDebug() << "Fiber-" << acl::fiber::self() << " recv: " << buf; } - if (delay_ > 0) { + parent_->setProgress((100 * i) / max_); + if (i % 10 == 0 && delay_ > 0) { acl::fiber::delay(delay_); } } + parent_->setProgress(100); + struct timeval end; gettimeofday(&end, nullptr); diff --git a/lib_fiber/samples-gui/QtFiber/fiber_client.h b/lib_fiber/samples-gui/QtFiber/fiber_client.h index 3635c0946..6b1e96029 100644 --- a/lib_fiber/samples-gui/QtFiber/fiber_client.h +++ b/lib_fiber/samples-gui/QtFiber/fiber_client.h @@ -1,15 +1,18 @@ #ifndef FIBER_CLIENT_H #define FIBER_CLIENT_H +class MainWindow; + class fiber_client : public acl::fiber { public: - fiber_client(const char *ip, int port, size_t max = 100, int delay = 0); + fiber_client(MainWindow *parent, const char *ip, int port, size_t max = 100, int delay = 0); protected: // @override void run() override; private: + MainWindow *parent_; std::string ip_; int port_; size_t max_; diff --git a/lib_fiber/samples-gui/QtFiber/main.cpp b/lib_fiber/samples-gui/QtFiber/main.cpp index cac162728..8e6aca982 100644 --- a/lib_fiber/samples-gui/QtFiber/main.cpp +++ b/lib_fiber/samples-gui/QtFiber/main.cpp @@ -43,7 +43,7 @@ static void app_run(int argc, char **argv) QApplication app(argc, argv); MainWindow w; - w.resize(800, 800); + //w.resize(1600, 800); w.show(); QTimer::singleShot(0, startupCallback); diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.cpp b/lib_fiber/samples-gui/QtFiber/mainwindow.cpp index a5d714eba..d3ce040dc 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.cpp +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.cpp @@ -5,7 +5,6 @@ #include #include "mainwindow.h" #include "./ui_mainwindow.h" -#include "childwindows.h" #include "inputdialog.h" #include "fiber_server.h" #include "fiber_client.h" @@ -16,58 +15,74 @@ MainWindow::MainWindow(QWidget *parent) , process_(new QProcess(this)) { ui_->setupUi(this); - - button_ = new QPushButton("Open Dialog", this); - button_->setGeometry(QRect(QPoint(100, 100), QSize(300, 50))); - connect(button_, &QPushButton::clicked, this, &MainWindow::onButtonClicked); - - start_server_ = new QPushButton("Start fiber server", this); - start_server_->setGeometry(QRect(QPoint(100, 150), QSize(300, 50))); - connect(start_server_, &QPushButton::clicked, this, &MainWindow::onStartServer); - - stop_server_ = new QPushButton("Stop fiber server", this); - stop_server_->setGeometry(QRect(QPoint(100, 200), QSize(300, 50))); - connect(stop_server_, &QPushButton::clicked, this, &MainWindow::onStopServer); - - start_client_ = new QPushButton("Start fiber client", this); - start_client_->setGeometry(QRect(QPoint(100, 250), QSize(300, 50))); - connect(start_client_, &QPushButton::clicked, this, &MainWindow::onStartClient); - - url_get_ = new QPushButton("Http download", this); - url_get_->setGeometry(QRect(QPoint(100, 300), QSize(300, 50))); - connect(url_get_, &QPushButton::clicked, this, &MainWindow::onUrlGet); - - start_schedule_ = new QPushButton("Start fiber schedule", this); - start_schedule_->setGeometry(QRect(QPoint(100, 350), QSize(300, 50))); - connect(start_schedule_, &QPushButton::clicked, this, &MainWindow::onStartSchedule); - - stop_schedule_ = new QPushButton("Stop fiber schedule", this); - stop_schedule_->setGeometry(QRect(QPoint(100, 400), QSize(300, 50))); - connect(stop_schedule_, &QPushButton::clicked, this, &MainWindow::onStopSchedule); - - open_child_ = new QPushButton("Open Child Window", this); - open_child_->setGeometry(QRect(QPoint(100, 450), QSize(300, 50))); - connect(open_child_, &QPushButton::clicked, this, &MainWindow::onOpenChildWindow); - - input_button_= new QPushButton("Open dialog", this); - input_button_->setGeometry(100, 500, 300,50); - connect(input_button_, &QPushButton::clicked, this, &MainWindow::onInputClicked); - - input_display_ = new QLabel("输入内容: ", this); - input_display_->setGeometry(100, 550, 200, 50); + stamp_ = new struct timeval; + gettimeofday(stamp_, nullptr); + + connect(ui_->clear, &QPushButton::clicked, this, &MainWindow::onClear); + connect(ui_->startSchedule, &QPushButton::clicked, this, &MainWindow::onStartSchedule); + connect(ui_->stopSchedule, &QPushButton::clicked, this, &MainWindow::onStopSchedule); + connect(ui_->startServer, &QPushButton::clicked, this, &MainWindow::onStartServer); + connect(ui_->stopServer, &QPushButton::clicked, this, &MainWindow::onStopServer); + connect(ui_->startClient, &QPushButton::clicked, this, &MainWindow::onStartClient); + connect(ui_->urlGet, &QPushButton::clicked, this, &MainWindow::onUrlGet); + + ui_->startSchedule->setEnabled(false); + ui_->stopServer->setEnabled(false); + ui_->startClient->setEnabled(false); } MainWindow::~MainWindow() { delete ui_; + delete stamp_; delete server_; qDebug() << "The main windows was destroied!"; } +void MainWindow::closeEvent(QCloseEvent *event) +{ +#if 1 + acl::fiber::schedule_stop(); // 停止协程调度器 + event->accept(); // 接受关闭事件 + return; +#else + QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Close", + tr("Are you sure you want to exit?\n"), + QMessageBox::No | QMessageBox::Yes, + QMessageBox::No); + + if (resBtn == QMessageBox::Yes) { + acl::fiber::schedule_stop(); + event->accept(); // 接受关闭事件 + } else { + event->ignore(); // 忽略关闭事件 + } +#endif +} + +void MainWindow::onClear() +{ + ui_->display->clear(); +} + +void MainWindow::setProgress(int n) +{ + if (n == 100) { + ui_->progress->setValue(100); + } else { + struct timeval now; + gettimeofday(&now, nullptr); + double tc = acl::stamp_sub(now, *stamp_); + if (tc >= 200) { + ui_->progress->setValue(n % 100); + gettimeofday(stamp_, nullptr); + } + } +} + void MainWindow::onAboutToQuit() { qDebug() << "onAboutToQuit called!"; -// acl::fiber::schedule_stop(); } void MainWindow::onButtonClicked() @@ -87,6 +102,9 @@ void MainWindow::onStartServer() return; } + ui_->stopServer->setEnabled(true); + ui_->startClient->setEnabled(true); + server_ = new fiber_server("127.0.0.1", 9001, this); qDebug() << "Start fiber server"; server_->start(); @@ -99,6 +117,8 @@ void MainWindow::onStopServer() server_->stop(); delete server_; server_ = nullptr; + ui_->stopServer->setEnabled(false); + ui_->startClient->setEnabled(false); } } @@ -109,7 +129,8 @@ void MainWindow::onStartClient() return; } - acl::fiber *fb = new fiber_client("127.0.0.1", 9001, 10000, 0); + ui_->progress->setValue(0); + acl::fiber *fb = new fiber_client(this, "127.0.0.1", 9001, 10000, 1); qDebug() << "Start fiber client"; fb->start(); qDebug() << "Fiber client started!"; @@ -151,6 +172,7 @@ void MainWindow::onDownloadFinish(bool ok, const acl::http_request& req) client->sprint_header(buf); qDebug() << "Got response body ok!"; qDebug() << buf.c_str(); + ui_->display->setText(buf.c_str()); } else { qDebug() << "Got response body error!"; } @@ -163,6 +185,9 @@ void MainWindow::onStartSchedule() return; } + ui_->stopSchedule->setEnabled(true); + ui_->urlGet->setEnabled(true); + qDebug() << "Begin schedule_gui!"; acl::fiber::schedule_gui(); qDebug() << "schedule_gui end!"; @@ -176,34 +201,14 @@ void MainWindow::onStopSchedule() server_ = nullptr; } + ui_->urlGet->setEnabled(false); + ui_->stopSchedule->setEnabled(false); + ui_->startSchedule->setEnabled(true); + acl::fiber::schedule_stop(); qDebug() << "Fiber schedule stopped!"; } -void MainWindow::onOpenChildWindow() -{ - if (child_window_ == nullptr) { - child_window_ = new ChildWindows(this); - } - - qDebug() << "Opening second window"; - - QScreen *screen = QApplication::primaryScreen(); - if (screen) { - QRect screenGeometry = screen->geometry(); - int x = (screenGeometry.width() - child_window_->width()) / 2; - int y = (screenGeometry.height() - child_window_->height()) / 2; - child_window_->move(x, y); - } - - child_window_->show(); - child_window_->raise(); - child_window_->activateWindow(); - qDebug() << "Second window visibility:" << child_window_->isVisible(); - qDebug() << "Second window isMinimized:" << child_window_->isMinimized(); - qDebug() << "Second window isActiveWindow:" << child_window_->isActiveWindow(); -} - void MainWindow::onInputClicked() { InputDialog dialog(this); @@ -218,7 +223,7 @@ void MainWindow::onInputClicked() void MainWindow::onDialogAccepted(const QString &text) { - input_display_->setText("输入内容: " + text); + //input_display_->setText("输入内容: " + text); } void MainWindow::keyPressEvent(QKeyEvent *event) diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.h b/lib_fiber/samples-gui/QtFiber/mainwindow.h index 33caf2b9d..718df57f7 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.h +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.h @@ -12,6 +12,7 @@ namespace acl { } class fiber_server; +struct timeval; QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -27,6 +28,8 @@ class MainWindow : public QMainWindow MainWindow(QWidget *parent = nullptr); ~MainWindow(); + void setProgress(int n); + protected: void keyPressEvent(QKeyEvent *event); @@ -36,11 +39,13 @@ class MainWindow : public QMainWindow void onStartClient(); void onStartSchedule(); void onStopSchedule(); - void onOpenChildWindow(); void onInputClicked(); void onUrlGet(); + void onClear(); + void onDialogAccepted(const QString &text); + void closeEvent(QCloseEvent *event) override; public: void onAboutToQuit(); @@ -50,21 +55,9 @@ class MainWindow : public QMainWindow private: Ui::MainWindow *ui_; - QPushButton *button_; - QPushButton *start_server_; - QPushButton *stop_server_; - QPushButton *start_client_; - QPushButton *start_schedule_; - QPushButton *stop_schedule_; - QPushButton *open_child_; - QPushButton *url_get_; - std::string url_; - QPushButton *input_button_; - QLabel *input_display_; fiber_server *server_ = nullptr; QProcess *process_; - - ChildWindows *child_window_ = nullptr; + struct timeval *stamp_; }; #endif // MAINWINDOW_H diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.ui b/lib_fiber/samples-gui/QtFiber/mainwindow.ui index 7bff7e1d3..ace77907f 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.ui +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.ui @@ -6,20 +6,135 @@ 0 0 - 800 - 600 + 1600 + 800 MainWindow - + + + + + 390 + 20 + 800 + 600 + + + + + + + 70 + 20 + 291 + 51 + + + + Start fiber schedule + + + + + + 70 + 80 + 291 + 51 + + + + Stop fiber schedule + + + + + + 70 + 140 + 291 + 51 + + + + Start fiber server + + + + + + 70 + 200 + 291 + 51 + + + + Stop fiber server + + + + + + 70 + 260 + 291 + 51 + + + + Start fiber client + + + + + + 70 + 320 + 291 + 51 + + + + Http download + + + + + + 1210 + 20 + 241 + 51 + + + + clear + + + + + + 390 + 630 + 801 + 41 + + + + 0 + + + 0 0 - 800 + 1600 21