qCNC 0.82-alpha
/home/trilog/Desktop/project/gui/Final/src/mainwindow.cpp
Go to the documentation of this file.
00001 
00042 #include "mainwindow.h"
00043 #include "global_includes.h"
00044 MainWindow::MainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::MainWindow) {
00045 
00046     Socketconnect = 0;
00047 
00048 
00049     ui->setupUi(this);
00050     setConnections();
00051     ui->listWidget->clear();
00052     ui->listWidget_2->clear();
00053     setWindowTitle(PROGNAME);
00054     read_gcode = 0;
00055 
00056 
00057 
00058     get_settings(&settings);
00059     CNC_mode = 1;
00060     CNC_step = 0;
00061     constatus = 0;
00062 
00063     gtemp = &first;
00064     ui->progressBar->setValue(0);
00065     ui->progressBar_2->setValue(0);
00066     ui->progressBar_3->setValue(0);
00067     if ( settings.Progenkill == 1) {
00068         ui->menu_CNC_Controll->setEnabled(true);
00069 
00070     }
00071     else
00072         ui->menu_CNC_Controll->setEnabled(false);
00073 
00074     startTimer(10);
00075 
00076 
00077 }
00078 void MainWindow::timerEvent( QTimerEvent* ){
00079 
00080     if ( Socketconnect == 1) {
00081         double x;
00082         double y;
00083         double z;
00084 
00085        // ui->label_13->setText(ui->comboBox->currentText());
00086         returnV = 0;
00087         time++;
00088         if (CNC_step == 2 && CNC_mode ==1 && read_gcode == 1) {
00089             if ( resend == 0) {
00090                 gtemp=gtemp->next;
00091                 returned.x = gtemp->x;
00092                 returned.y = gtemp->y;
00093                 returned.z = gtemp->z;
00094                 returned.status = 1; // 1= to motor-task
00095                 x = (returned.x)*xscalec;
00096                 y = (returned.y)*yscalec;
00097                 z = (returned.z)*zscalec;
00098                 ui->progressBar->setValue(round(x));
00099                 ui->progressBar_2->setValue(round(y));
00100                 ui->progressBar_3->setValue(round(z));
00101             }
00102             else if (resend == 1) {
00103                 returned.x = gtemp->x;
00104                 returned.y = gtemp->y;
00105                 returned.z = gtemp->z;
00106                 returned.status = 1; // 1= to motor-task
00107             }
00108         }
00109 
00110         if (CNC_step != 56 && read_gcode == 1) {
00111             returnV = TCP_controll(time,&sock,CNC_mode,CNC_step,&returned, &constatus);
00112 
00113             switch (returnV) {
00114             case -25 :
00115                 qDebug() <<"Pause Error";
00116                 QMessageBox::critical(this, "Critical error","An unrecoverable critical error occured. Killing system");
00117                 close();
00118                 break;
00119             case -1 : //disconnect
00120                 qDebug() << "Disconnect occured\n";
00121                 Socketconnect = 0;
00122                 break;
00123             case 0 :
00124                 resend = 0;
00125                 break;
00126             case 11 :
00127                 qDebug() << "Successful INIT\n";
00128                 CNC_step = 2;
00129                 break;
00130             case 20 : //kill
00131                 qDebug() << "Achived kill\n";
00132                 CNC_step = 9;
00133                 first.next = NULL;
00134                 last.next = NULL;
00135                 closeSocket(&sock);
00136                 Socketconnect = 0;
00137                 ui->graphicsView->setScene(NULL);
00138                 break;
00139             case 25 :
00140                 qDebug() << "Pause intitialized";
00141                 pCNC_mode = CNC_step;
00142                 CNC_step = 56;
00143                 break;
00144             case 27 :
00145                 first.next = NULL;
00146                 last.next = NULL;
00147                 break;
00148             case 34 :
00149                 file_mode();
00150                 break;
00151             case 35 :
00152                 gui_mode();
00153                 break;
00154             case 36 :
00155                 standalone_mode();
00156                 break;
00157             case 50 :
00158                 qDebug() << "Continue achived";
00159                 CNC_step = pCNC_mode;
00160                 break;
00161             case 55 :
00162                 resend = 1;
00163                 break;
00164             default :
00165                 qDebug() << "Invalid return";
00166                 break;
00167             }
00168         }
00169     }
00170 }
00171 
00172 
00173 MainWindow::~MainWindow()
00174 {
00175     delete ui;
00176 }
00177 
00178 void MainWindow::gui_mode() {
00179     ui->menu_CNC_Controll->setEnabled(false);
00180     ui->actionOpen_file->setEnabled(false);
00181     ui->tab_2->setEnabled(true);
00182 }
00183 
00184 void MainWindow::file_mode() {
00185     ui->tab_2->setEnabled(false);
00186     ui->menu_CNC_Controll->setEnabled(true);
00187     ui->actionOpen_file->setEnabled(true);
00188 
00189 }
00190 
00191 void MainWindow::standalone_mode() {
00192     closeSocket(&sock);
00193     ui->menu_CNC_Controll->setEnabled(true);
00194     ui->tab_2->setEnabled(true);
00195     ui->actionOpen_file->setEnabled(false);
00196 }
00197 
00208 void MainWindow::set_Prog()
00209 {
00210     Dialog *prog = new Dialog(this, &settings);
00211 
00212     prog->show();
00213 
00214 }
00215 
00226 void MainWindow::about()
00227 {
00228     TSabout *abt = new TSabout(this);
00229     abt->setWindowTitle(tr("About QlightRead"));
00230     abt->resize(465,670);
00231     abt->show();
00232 
00233     }
00234 
00245 void MainWindow::aboutQt()
00246 {
00247     }
00248 
00249 void MainWindow::TCPsetdiag()
00250 {
00251     netSetupDialog *setpro = new netSetupDialog(this, &settings);
00252     setpro->resize(210,130);
00253     setpro->show();
00254 }
00255 
00256 void MainWindow::TCPConnect() {
00257 
00258     if ( Socketconnect == 1 ) {
00259         QMessageBox::information(this, "Connection seems active", "You seem to be connected, If you are not, please disconnect and then reconnect");
00260 
00261     }
00262     else {
00263     Socketconnect = 0;
00264     QByteArray ba;
00265     ba = settings.TCPaddress.toLocal8Bit();
00266 
00267     sock = connectSocket(ba.data(),settings.TCPport);
00268     if ( sock <= 0) {
00269         QMessageBox::critical(this, "Could not connect to Socket device!", "An Error occured while trying to connect to specified socket. Please check you settings, and retry!");
00270         return;
00271     }
00272     else {
00273 
00274 
00275         Socketconnect = 1;
00276         }
00277     }
00278 
00279 }
00280 
00281 void MainWindow::cncclear(){
00282     CNC_step = 23;
00283 }
00284 void MainWindow::cncstart(){
00285     CNC_step = 1;
00286 
00287 
00288 
00289    // Gcode_read(" ");
00290 }
00291 
00292 void MainWindow::cncstop(){
00293     CNC_step = 9;
00294 
00295 }
00296 
00297 void MainWindow::cncreset(){
00298     CNC_step = 23;
00299 }
00300 void MainWindow::cncpause(){
00301     if (CNC_step == 56)
00302         CNC_step = 55;
00303     else
00304         CNC_step = 5;
00305 }
00306 
00307 
00318 void MainWindow::set_Serial()
00319 {
00320 
00321     SDeviceEditDialog *serialset = new SDeviceEditDialog(this, &settings);
00322     serialset->setWindowTitle("Serial Interface Settings");
00323     serialset->resize(190,80);
00324     serialset->show();
00325 
00326 
00327 }
00328 
00329 void MainWindow::debug_cntr() {
00330     //G_Log.append(ui->lineEdit->text());
00331     //G_Log.append("\n");
00332 
00333     ui->listWidget->addItem(command_phase(ui->lineEdit->text()));
00334 
00335 
00336     ui->listWidget_2->addItem(ui->lineEdit->text());
00337     ui->lineEdit->clear();
00338 }
00339 
00340 
00341 
00342 void MainWindow::decode_gcode() {
00343    QString tt;
00344     QString fileName = QFileDialog::getOpenFileName(this,
00345                            tr("Choose a file"), ".",
00346                            tr("Plot Data [*.gc] (*.gc)"));
00347     if (!fileName.isEmpty()) {
00348         Gcode_read(fileName);
00349         debug_w_form(fileName ,"decode_gcode()","systemdebug","debug");
00350     maxy = 0.0;
00351     maxx = 0.0;
00352     map_linkedlist(&maxx, &maxy, &maxz, &first,&last);
00353     read_gcode = 1;
00354     debug_w_form("Mapped linked list" ,"decode_gcode()","systemdebug","debug");
00355     update_preview();
00356     xscalec = 188/maxx;
00357     yscalec = 178/maxy;
00358     zscalec = 61/maxz;
00359     ui->progressBar->setRange(0,round(xscalec*maxx));
00360     ui->progressBar->setValue(0);
00361     ui->progressBar_2->setRange(0,round(yscalec*maxy));
00362     ui->progressBar_2->setValue(0);
00363     ui->progressBar_3->setRange(0,round(zscalec*maxz));
00364     ui->progressBar_3->setValue(0);
00365     ui->horizontalSlider->setRange(0,round(xscalec*maxx));
00366     ui->horizontalSlider_2->setRange(0, round(yscalec*maxy));
00367     ui->horizontalSlider_3->setRange(0,round(zscalec*maxz));
00368 
00369   //  qDebug() << yscalec * maxy;
00370 
00371     tt.append("set scaling to : [x|y|z] ");
00372     temp.setNum(xscalec);
00373     tt.append(temp);
00374     tt.append("|");
00375     temp.setNum(yscalec);
00376     tt.append(temp);
00377     tt.append("|");
00378     temp.setNum(zscalec);
00379     tt.append(temp);
00380     debug_w_form(tt ,"decode_gcode()","systemdebug","debug");
00381     }
00382     else
00383         return;
00384 
00385 
00386 
00387 }
00388 
00389 QString MainWindow::command_phase(QString edit/*, qint8 userid*/) {
00390     if (edit == "close") {
00391             close();
00392             return "*System command envoced*";
00393     }
00394     else if (edit == "readonly" || edit == "observe") {
00395         qDebug() << "Readonly console mode enabled";
00396         ui->lineEdit->setEnabled(false);
00397         ui->pushButton->setText("Connect");
00398         connect(ui->pushButton, SIGNAL(clicked()),this,SLOT(reenable_cons()));
00399         return "*System command envoced* You are now in readonly mode. to reconnect press the connect button";
00400     }
00401 
00402     //else
00403          //return debug_w_form(ui->lineEdit->text(),"host", "test");
00404 
00405 
00406 }
00407 
00408 void MainWindow::debug_w_form(QString edit, QString sender, QString dest, QString msgtype) {
00409 
00410     QString temp;
00411 
00412     temp.clear();
00413     temp.append(sender);
00414     temp.append("|");
00415     temp.append(dest);
00416     temp.append("[");
00417     temp.append(msgtype);
00418     temp.append("]:-> ");
00419     temp.append(edit);
00420     ui->listWidget->addItem(temp);
00421 
00422 }
00423 void MainWindow::timestamp() {
00424     QDateTime setTime = QDateTime::fromString(QString("1970-07-10T14:15:09"),Qt::ISODate);
00425     QDateTime current = QDateTime::currentDateTime();
00426     uint msecs = setTime.time().msecsTo(current.time());
00427 
00428     //QString z= newl.currentTime();
00429     ui->listWidget->addItem(QString::number(msecs));
00430 }
00431 
00432 void MainWindow::setConnections() {
00433     connect(ui->actionOpen_file,SIGNAL(triggered()), this, SLOT(decode_gcode()));
00434     connect(ui->actionConnect_to_TCP_IP_host,SIGNAL(triggered()),this, SLOT(TCPConnect()));
00435     connect(ui->actionAbout,SIGNAL(triggered()) ,this, SLOT(about()));
00436     connect(ui->actionAbout_QT,SIGNAL(triggered()), qApp, SLOT(aboutQt()));
00437     //CNC-controlls
00438     connect(ui->actionClear,SIGNAL(triggered()),this,SLOT(cncclear()));
00439     connect(ui->actionPause,SIGNAL(triggered()),this,SLOT(cncpause()));
00440     connect(ui->actionReset,SIGNAL(triggered()),this,SLOT(cncreset()));
00441     connect(ui->actionStart,SIGNAL(triggered()),this,SLOT(cncstart()));
00442     connect(ui->actionStop,SIGNAL(triggered()),this,SLOT(cncstop()));
00443 
00444     //preferences
00445     connect(ui->actionPreferences, SIGNAL(triggered()),this,SLOT(set_Prog()));
00446     connect(ui->actionTCP_IP, SIGNAL(triggered()), this, SLOT(TCPsetdiag()));
00447     connect(ui->actionSerial_debug, SIGNAL(triggered()), this, SLOT(set_Serial()));
00448 
00449     connect(ui->pushButton, SIGNAL(clicked()),this,SLOT(debug_cntr()));
00450     connect(ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(debug_cntr()));
00451     connect(ui->pushButton_12,SIGNAL(clicked()),this, SLOT(cncstart()));
00452     connect(ui->pushButton_13, SIGNAL(clicked()), this, SLOT(cncstop()));
00453     connect(ui->pushButton_14, SIGNAL(clicked()), this, SLOT(cncpause()));
00454     connect(ui->pushButton_11, SIGNAL(clicked()), this,SLOT(cncclear()));
00455     connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(set_mode()));
00456     connect(ui->actionConnect_to_serial_debug_interface, SIGNAL(triggered()),this,SLOT(SerialConnect()));
00457 }
00458 
00459 void MainWindow::set_mode() {
00460     ui->label_13->setText(ui->comboBox->currentText());
00461     if ( ui->comboBox->currentIndex() == 0) {
00462         CNC_mode = 1;
00463         ui->listWidget_2->addItem("Setting CNC Mode to : File Mode");
00464         debug_w_form("File Mode enabled","InternalModeSetter","CNC-MCU","debug");
00465 
00466     }
00467     if (ui->comboBox->currentIndex() == 1) {
00468         CNC_mode = 2;
00469         ui->listWidget_2->addItem("Setting CNC Mode to : GUI Control Mode");
00470              debug_w_form("GUI Control enabled","InternalModeSetter","CNC-MCU","debug");
00471     }
00472     if (ui->comboBox->currentIndex() == 2) {
00473         CNC_mode = 3;
00474         ui->listWidget_2->addItem("Setting CNC Mode to : Standalone");
00475              debug_w_form("Standalone enabled","InternalModeSetter","CNC-MCU","debug");
00476     }
00477 
00478 }
00479 
00480 void MainWindow::setview(qint8 k) {
00481     if ( k == 1){
00482         qDebug()<<"MainWindow::setview: envoked s_debug\n";
00483         ui->tab->setEnabled(TRUE);
00484     }
00485 }
00486 
00487 
00488 
00489 void MainWindow::write_to_file(QString fileName, QString pstr)  {
00490     QFile file(fileName);
00491     if (!file.open(QFile::WriteOnly | QFile::Text)) {
00492         QMessageBox::warning(this, tr(PROGNAME),
00493                              tr("Cannot write file %1:\n%2.")
00494                              .arg(fileName)
00495                              .arg(file.errorString()));
00496         return;
00497     }
00498     QTextStream out(&file);
00499     QApplication::setOverrideCursor(Qt::WaitCursor);
00500     out <<pstr<< endl;
00501     //out <<"<0001:00000>\n";
00502     QApplication::restoreOverrideCursor();
00503     QString test;
00504     test.append("Saved '%1' ");
00505     //test.append((QString *)file.size());
00506     test.append("bytes written!");
00507     statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000);
00508 
00509 
00510 }
00511 
00522 void MainWindow::custom_title_set(QString C_dev) {
00523     QString temp;
00524     temp.clear();
00525     //temp.append(TITLE_STRING);
00526     temp.append(" <Connected> <");
00527     temp.append(C_dev);
00528     temp.append(">");
00529     setWindowTitle(temp);
00530 }
00531 
00532 void MainWindow::TCPcontrolsend(protocol CNCdat) {
00533 
00534     //StartTransfer(Temp);
00535 }
00536 
00537  void MainWindow::update_preview() {
00538 
00539     double x1 = 0.0;
00540     double y1 = 0.0;
00541     double px1 = 0.0;
00542     double py1 = 0.0;
00543     double scalex = 0.0;
00544     double scaley = 0.0;
00545     double z = 0.0;
00546     gcode_linkedL *temp;
00547     temp = new gcode_linkedL;
00548     *temp = first;
00549     scalex = 620/(maxx);
00550     scaley = 500/(maxy);
00551     ui->progressBar->setRange(0,620);
00552     ui->progressBar_2->setRange(0,500);
00553  //   qDebug() << scalex << " || " << maxx << endl;
00554 //    qDebug() << scaley << " || " << maxy << endl;
00555     QGraphicsScene *scene = new QGraphicsScene;
00556     scene->setSceneRect( 0, 0, 640, 510);
00557 
00558     while (temp->next != NULL) {
00559         px1 = x1;
00560         py1 = y1;
00561         x1 = (temp->x)*scalex;
00562         y1 = (temp->y)*scaley;
00563         z = temp->z;
00564         if (z <0)
00565             z = z*-1;
00566        // qDebug() << x1 << " || " << y1 << " || " << z <<  endl;
00567          temp=temp->next;
00568         if (px1 == 0 && py1 == 0)
00569             continue;
00570         else {
00571 
00572          //   if (z = 0)
00573                 scene->addLine(QLineF(px1,py1,x1,y1));
00574                 ui->progressBar->setValue(round(x1));
00575                 ui->progressBar_2->setValue(round(y1));
00576                 ui->progressBar_3->setValue(round(z));
00577         }
00578 
00579     }
00580 
00581     ui->graphicsView->setScene(scene);
00582     ui->graphicsView->setRenderHint(QPainter::Antialiasing);
00583     ui->graphicsView->rotate(180);
00584     //ui->graphicsView->resize(10,10);
00585     ui->graphicsView->show();
00586 }
00587 
00588 
00589  void MainWindow::SerialConnect() {
00590      Sconnect = new SerialPortReader(this, &settings.SerialDevice);
00591      if (Sconnect->isOpen()) {
00592         Sconnect->run();
00593          connect(Sconnect,SIGNAL(serialSignal(int*)),this,SLOT(Swrite()));
00594      }
00595  }
00596  void MainWindow::Swrite() {
00597      debug_w_form(*Sconnect->str,"SerialPort","sysdebug","debug");
00598  }
00599 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines