| 9 | | Controller::Controller() { |
| 10 | | // add some child: |
| 11 | | new QLabel("Hello World", this); |
| | 12 | Controller::Controller(QWidget* parent) : QWidget(parent) { |
| | 13 | // set up the heap widgets |
| | 14 | worker = new ClientThread(this); |
| | 15 | log = new QTextEdit(this); |
| | 16 | QWidget* button_box = createButtonBar(); |
| | 17 | |
| | 18 | // set up the log |
| | 19 | log->setReadOnly(true); |
| | 20 | |
| | 21 | // create the global box |
| | 22 | QHBoxLayout* layout = new QHBoxLayout; |
| | 23 | layout->addWidget(button_box); |
| | 24 | layout->addWidget(log); |
| | 25 | setLayout(layout); |
| | 26 | |
| | 27 | // now going to fire up working thread |
| | 28 | connect(worker, SIGNAL(recievedResponse(QString, QString)), this, SLOT(logResponse(QString,QString)), Qt::QueuedConnection); |
| | 29 | |
| | 30 | qDebug("Starting worker thread"); |
| | 31 | worker->start(); |
| | 32 | qDebug("Finished starting thread"); |
| | 34 | |
| | 35 | QWidget* Controller::createButtonBar() { |
| | 36 | QWidget* box = new QWidget(this); |
| | 37 | QHBoxLayout* layout = new QHBoxLayout(box); |
| | 38 | |
| | 39 | start_button = new QPushButton(tr("Start"), box); |
| | 40 | connect(start_button, SIGNAL(clicked()), worker, SLOT(fire_start())); |
| | 41 | |
| | 42 | stop_button = new QPushButton(tr("Stop"), box); |
| | 43 | connect(stop_button, SIGNAL(clicked()), worker, SLOT(fire_stop())); |
| | 44 | |
| | 45 | ping_button = new QPushButton(tr("Ping"), box); |
| | 46 | connect(ping_button, SIGNAL(clicked()), worker, SLOT(fire_ping())); |
| | 47 | |
| | 48 | reset_button = new QPushButton(tr("Reset"), box); |
| | 49 | connect(reset_button, SIGNAL(clicked()), worker, SLOT(fire_reset())); |
| | 50 | |
| | 51 | QWidget* clear_log = new QPushButton(tr("Clear log"), box); |
| | 52 | connect(clear_log, SIGNAL(clicked()), log, SLOT(clear())); |
| | 53 | |
| | 54 | quit_button = new QPushButton(tr("Quit Client"), box); |
| | 55 | quit_button->setToolTip("Suspend or restart Client Thread"); |
| | 56 | connect(reset_button, SIGNAL(clicked()), this, SLOT(client_toggle_run())); |
| | 57 | |
| | 58 | box->setLayout(layout); |
| | 59 | return box; |
| | 60 | } |
| | 61 | |
| | 62 | void Controller::client_toggle_run() { |
| | 63 | // when user clicks quit_button |
| | 64 | bool on = worker->isRunning(); |
| | 65 | |
| | 66 | if(on) { |
| | 67 | worker->setAbort(true); |
| | 68 | } else { |
| | 69 | // fire up client |
| | 70 | worker->start(); |
| | 71 | } |
| | 72 | |
| | 73 | start_button->setEnabled(on); |
| | 74 | stop_button->setEnabled(on); |
| | 75 | ping_button->setEnabled(on); |
| | 76 | reset_button->setEnabled(on); |
| | 77 | quit_button->setText(on ? tr("Quit Client") : tr("Restart Client")); |
| | 78 | } |
| | 79 | |
| | 80 | void Controller::logResponse(QString code, QString comment) { |
| | 81 | log->moveCursor(QTextCursor::End); |
| | 82 | log->insertHtml("<br><b><font color=red>"+code+"</font></b> "+comment); |
| | 83 | } |