From c42da945ba01864b55358c95ed0833e37af8fcdc Mon Sep 17 00:00:00 2001 From: akshay-c Date: Fri, 26 Apr 2019 11:46:09 +0530 Subject: ConfDialog completed, issues with dialog close resolved --- ldmicro/CMakeLists.txt | 2 +- ldmicro/coildialog.cpp | 2 +- ldmicro/commentdialog.cpp | 2 +- ldmicro/confdialog.cpp | 172 ++++++++++++++++++++++++++-------------- ldmicro/contactsdialog.cpp | 2 +- ldmicro/ldmicro.cpp | 4 +- ldmicro/lib/linuxUI/linuxUI.cpp | 8 +- ldmicro/lib/linuxUI/linuxUI.h | 2 +- ldmicro/lutdialog.cpp | 12 +-- ldmicro/simpledialog.cpp | 2 +- ldmicro/simulate.cpp | 2 +- 11 files changed, 131 insertions(+), 79 deletions(-) diff --git a/ldmicro/CMakeLists.txt b/ldmicro/CMakeLists.txt index b354d8d..dced0da 100644 --- a/ldmicro/CMakeLists.txt +++ b/ldmicro/CMakeLists.txt @@ -141,7 +141,7 @@ endif() lang.cpp miscutil.cpp #add message boxes to Error(), CompileSuccessfulMessage() iolist.cpp - #confdialog.cpp + confdialog.cpp lutdialog.cpp resetdialog.cpp simpledialog.cpp diff --git a/ldmicro/coildialog.cpp b/ldmicro/coildialog.cpp index 758e308..f6677b9 100644 --- a/ldmicro/coildialog.cpp +++ b/ldmicro/coildialog.cpp @@ -89,7 +89,7 @@ static void MakeControls(void) QObject::connect(ButtonBox, SIGNAL(rejected()), CoilDialog, SLOT(reject())); } -inline void DestroyWindow() +static inline void DestroyWindow() { delete SourceInternalRelayRadio; delete SourceMcuPinRadio; diff --git a/ldmicro/commentdialog.cpp b/ldmicro/commentdialog.cpp index d9c6d9f..66832ef 100644 --- a/ldmicro/commentdialog.cpp +++ b/ldmicro/commentdialog.cpp @@ -47,7 +47,7 @@ static void MakeControls(void) FixedFont(CommentTextbox); } -inline void DestroyWindow(){ +static inline void DestroyWindow(){ delete CommentDialog; delete CommentTextbox; delete CommentGrid; diff --git a/ldmicro/confdialog.cpp b/ldmicro/confdialog.cpp index 099d35a..f63fae9 100644 --- a/ldmicro/confdialog.cpp +++ b/ldmicro/confdialog.cpp @@ -31,51 +31,65 @@ using namespace std; -static HWID ConfDialog; +static QDialog* ConfDialog; -static HWID CrystalTextbox; -static HWID CycleTextbox; -static HWID BaudTextbox; -static HWID OkButton; -static HWID CancelButton; +static QLineEdit* CrystalTextbox; +static QLineEdit* CycleTextbox; +static QLineEdit* BaudTextbox; +static QDialogButtonBox* ButtonBox; static LONG_PTR PrevCrystalProc; static LONG_PTR PrevCycleProc; static LONG_PTR PrevBaudProc; -HWID ConfGrid; -HWID ConfPackingBox; +QGridLayout* ConfGrid; + +static inline void DestroyWindow() +{ + delete ConfDialog; + delete CrystalTextbox; + delete CycleTextbox; + delete BaudTextbox; + delete ButtonBox; + delete ConfGrid; +} static void MakeControls(void) { // Creating text labels - HWID textLabel = gtk_label_new ("Cycle Time (ms):"); - HWID textLabel2 = gtk_label_new ("Crystal Frequency (MHz):"); - HWID textLabel3 = gtk_label_new ("UART Baud Rate (bps):"); - - // Creating text boxes - CycleTextbox = gtk_entry_new (); - gtk_entry_set_max_length (GTK_ENTRY (CycleTextbox), 0); - // gtk_entry_set_input_purpose (GTK_ENTRY (CycleTextbox), GTK_INPUT_PURPOSE_DIGITS); - CrystalTextbox = gtk_entry_new (); - gtk_entry_set_max_length (GTK_ENTRY (CrystalTextbox), 0); - BaudTextbox = gtk_entry_new (); - gtk_entry_set_max_length (GTK_ENTRY (BaudTextbox), 0); + + QLabel* textLabel = new QLabel(_("Cycle Time (ms):")); + QLabel* textLabel2 = new QLabel(_("Crystal Frequency (MHz):")); + QLabel* textLabel3 = new QLabel(_("UART Baud Rate (bps):")); + + CycleTextbox = new QLineEdit(); + CrystalTextbox = new QLineEdit(); + BaudTextbox = new QLineEdit(); + CycleTextbox->setValidator( + new QRegExpValidator( + QRegExp("[0-9]+"))); + CrystalTextbox->setValidator( + new QRegExpValidator( + QRegExp("[0-9]+"))); + BaudTextbox->setValidator( + new QRegExpValidator( + QRegExp("[0-9]+"))); if(!UartFunctionUsed()) { - gtk_widget_set_sensitive (BaudTextbox, FALSE); - gtk_widget_set_sensitive (textLabel3, FALSE); + BaudTextbox->setEnabled(FALSE); + textLabel3->setEnabled(FALSE); } if(Prog.mcu && (Prog.mcu->whichIsa == ISA_ANSIC || Prog.mcu->whichIsa == ISA_INTERPRETED)) { - gtk_widget_set_sensitive (CrystalTextbox, FALSE); - gtk_widget_set_sensitive (textLabel2, FALSE); + CrystalTextbox->setEnabled(FALSE); + textLabel2->setEnabled(FALSE); } - OkButton = gtk_button_new_with_label ("OK"); - CancelButton = gtk_button_new_with_label ("Cancel"); + ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok + | QDialogButtonBox::Cancel, Qt::Vertical, + ConfDialog); char explanation[1024] = ""; @@ -91,43 +105,45 @@ static void MakeControls(void) } } else { - strcpy(explanation, _("\n No serial instructions (UART Send/UART Receive) \n" - "are in use; add one to program before \n" - "setting baud rate.\r\n\r\n") ); + strcpy(explanation, _("No serial instructions (UART Send/UART Receive) " + "are in use; add one to program before setting baud rate.\r\n\r\n") ); } strcat(explanation, - _("The cycle time for the 'PLC' runtime generated by \n" "LDmicro is user-" - "configurable. Very short cycle \n" "times may not be achievable due " - "to processor \n" "speed constraints, and very long cycle times may \n" - "not be achievable due to hardware overflows. Cycle \n" "times between 10 ms \n" + _("The cycle time for the 'PLC' runtime generated by LDmicro is user-" + "configurable. Very short cycle times may not be achievable due " + "to processor speed constraints, and very long cycle times may not " + "be achievable due to hardware overflows. Cycle times between 10 ms " "and 100 ms will usually be practical.\r\n\r\n" - "The compiler must know what speed crystal you \n" "are using with the " - "micro to convert between timing \n" "in clock cycles and timing in" - "seconds. A 4 MHz to \n" "20 MHz crystal is typical; check the speed " - "grade of \n" "the part you are using to determine the maximum \n" "allowable" - "clock speed before choosing a crystal.\n")); + "The compiler must know what speed crystal you are using with the " + "micro to convert between timing in clock cycles and timing in " + "seconds. A 4 MHz to 20 MHz crystal is typical; check the speed " + "grade of the part you are using to determine the maximum allowable " + "clock speed before choosing a crystal.")); - HWID textLabel4 = gtk_label_new (explanation); + QLabel* textLabel4 = new QLabel(explanation); + textLabel4->setAlignment(Qt::AlignJustify); + textLabel4->setMaximumWidth(310); + textLabel4->setWordWrap(TRUE); // Creating required containers for packing - ConfGrid = gtk_grid_new(); - ConfPackingBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); - - gtk_grid_attach (GTK_GRID (ConfGrid), textLabel, 1, 2, 1, 1); - gtk_grid_attach (GTK_GRID (ConfGrid), CycleTextbox, 3, 2, 1, 1); - gtk_grid_attach (GTK_GRID (ConfGrid), OkButton, 6, 2, 2, 1); - gtk_grid_attach (GTK_GRID (ConfGrid), textLabel2, 1, 4, 1, 1); - gtk_grid_attach (GTK_GRID (ConfGrid), CrystalTextbox, 3, 4, 1, 1); - gtk_grid_attach (GTK_GRID (ConfGrid), CancelButton, 6, 4, 2, 1); - gtk_grid_attach (GTK_GRID (ConfGrid), textLabel3, 1, 6, 1, 1); - gtk_grid_attach (GTK_GRID (ConfGrid), BaudTextbox, 3, 6, 1, 1); - - gtk_grid_set_column_spacing (GTK_GRID (ConfGrid), 2); - - gtk_box_pack_start(GTK_BOX(ConfPackingBox), ConfGrid, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(ConfPackingBox), textLabel4, TRUE, TRUE, 0); - + ConfGrid->addWidget(textLabel, 0,0); + ConfGrid->addWidget(CycleTextbox, 0,1); + ConfGrid->addWidget(ButtonBox, 0, 2, 0, 2); + ConfGrid->addWidget(textLabel2, 1, 0); + ConfGrid->addWidget(CrystalTextbox, 1, 1); + ConfGrid->addWidget(textLabel3, 2, 0); + ConfGrid->addWidget(BaudTextbox, 2, 1); + ConfGrid->addWidget(textLabel4, 3, 0, 2, 0, + Qt::AlignJustify); + NiceFont(ConfDialog); + CycleTextbox->setFocus(); + // int height = textLabel4->pos().y(); + // printf("Height:%d\n", height); + // ConfDialog->setSize(ConfGrid->sizeHint()); + ConfDialog->adjustSize(); + QObject::connect(ButtonBox, SIGNAL(accepted()), ConfDialog, SLOT(accept())); + QObject::connect(ButtonBox, SIGNAL(rejected()), ConfDialog, SLOT(reject())); // PrevCycleProc = SetWindowLongPtr(CycleTextbox, GWLP_WNDPROC, // (LONG_PTR)MyNumberProc); @@ -137,7 +153,7 @@ static void MakeControls(void) // PrevBaudProc = SetWindowLongPtr(BaudTextbox, GWLP_WNDPROC, // (LONG_PTR)MyNumberProc); } - +/* //----------------------------------------------------------------------------- // Don't allow any characters other than 0-9. in the text boxes. //----------------------------------------------------------------------------- @@ -208,12 +224,48 @@ void ConfDialogSignalCall () { G_CALLBACK(ConfCallDestroyWindow), NULL); g_signal_connect (ConfDialog, "delete_event", G_CALLBACK (ConfCallDestroyWindow), NULL); } - +*/ void ShowConfDialog(void) { // The window's height will be resized later, to fit the explanation text. + ConfDialog = CreateWindowClient(_("PLC Configuration"), + 100, 100, 0, 0, MainWindow); + ConfGrid = new QGridLayout(ConfDialog); MakeControls(); - GdkEventKey* event; + char buf[16]; + sprintf(buf, "%.1f", (Prog.cycleTime / 1000.0)); + CycleTextbox->setText(buf); + + sprintf(buf, "%.6f", Prog.mcuClock / 1e6); + CrystalTextbox->setText(buf); + + sprintf(buf, "%d", Prog.baudRate); + BaudTextbox->setText(buf); + int ret = ConfDialog->exec(); + switch(ret) + { + case QDialog::Accepted: + { + char buf[16]; + strncpy(buf, CycleTextbox->text().toStdString().c_str(),16); + Prog.cycleTime = (int)(1000*atof(buf) + 0.5); + if(Prog.cycleTime == 0) { + Error(_("Zero cycle time not valid; resetting to 10 ms.")); + Prog.cycleTime = 10000; + } + + strncpy(buf, CrystalTextbox->text().toStdString().c_str(),16); + Prog.mcuClock = (int)(1e6*atof(buf) + 0.5); + + strncpy(buf, BaudTextbox->text().toStdString().c_str(),16); + Prog.baudRate = atoi(buf); + } + break; + case QDialog::Rejected: + break; + } + // DestroyWindow(); +/* GdkEventKey* event; ConfDialog = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(ConfDialog), "PLC Configuration"); @@ -241,5 +293,5 @@ void ShowConfDialog(void) ConfDialogSignalCall(); - return; + return;*/ } \ No newline at end of file diff --git a/ldmicro/contactsdialog.cpp b/ldmicro/contactsdialog.cpp index 32f794a..35c1c65 100644 --- a/ldmicro/contactsdialog.cpp +++ b/ldmicro/contactsdialog.cpp @@ -78,7 +78,7 @@ static void MakeControls(void) QObject::connect(ButtonBox, SIGNAL(rejected()), ContactsDialog, SLOT(reject())); } -inline void DestroyWindow (){ +static inline void DestroyWindow (){ delete NegatedCheckbox; delete SourceInternalRelayRadio; delete SourceInputPinRadio; diff --git a/ldmicro/ldmicro.cpp b/ldmicro/ldmicro.cpp index 3e19fb8..5639d1e 100644 --- a/ldmicro/ldmicro.cpp +++ b/ldmicro/ldmicro.cpp @@ -566,9 +566,9 @@ cmp: CHANGING_PROGRAM(DeleteSelectedFromProgram()); break; - /*case MNU_MCU_SETTINGS: + case MNU_MCU_SETTINGS: CHANGING_PROGRAM(ShowConfDialog()); - break;*/ + break; case MNU_SIMULATION_MODE: ToggleSimulationMode(); diff --git a/ldmicro/lib/linuxUI/linuxUI.cpp b/ldmicro/lib/linuxUI/linuxUI.cpp index 015cbb8..dc2dc8b 100644 --- a/ldmicro/lib/linuxUI/linuxUI.cpp +++ b/ldmicro/lib/linuxUI/linuxUI.cpp @@ -518,12 +518,12 @@ BOOL KillTimer(HWID hWid, UINT uIDEvent) return TRUE; } -void DestroyWindow (HWID widget) +/*void DestroyWindow (HWID widget) { - /*if (GTK_IS_WIDGET(widget)) + if (GTK_IS_WIDGET(widget)) { gtk_widget_destroy (widget); - }*/ -} + } +}*/ diff --git a/ldmicro/lib/linuxUI/linuxUI.h b/ldmicro/lib/linuxUI/linuxUI.h index 09f0c8b..8056cd8 100644 --- a/ldmicro/lib/linuxUI/linuxUI.h +++ b/ldmicro/lib/linuxUI/linuxUI.h @@ -310,7 +310,7 @@ BOOL KillTimer( HWID hWid, UINT uIDEvent); -void DestroyWindow (HWID widget); +// void DestroyWindow (HWID widget); class PaintWidget : public QWidget { diff --git a/ldmicro/lutdialog.cpp b/ldmicro/lutdialog.cpp index a4e5987..e244d1a 100644 --- a/ldmicro/lutdialog.cpp +++ b/ldmicro/lutdialog.cpp @@ -80,7 +80,7 @@ void CountFunction(QString text); void StringFunction(QString text); -inline void DestroyWindow (){ +static inline void DestroyWindow (){ delete Labels[0]; delete Labels[1]; delete Labels[2]; @@ -382,11 +382,11 @@ void ShowLookUpTableDialog(ElemLeaf *l) int i; for(i = 0; i < count; i++) { t->vals[i] = ValuesCache[i]; - printf("Value:%d\n", ValuesCache[i]); + // printf("Value:%d\n", ValuesCache[i]); } t->count = count; t->editAsString = asString; - printf("Count:%d\n", count); + // printf("Count:%d\n", count); } break; case QDialog::Rejected: @@ -400,7 +400,7 @@ void ShowLookUpTableDialog(ElemLeaf *l) void CheckBoxFunction(int state) { - printf("CheckBoxFunctionCalled\n"); + // printf("CheckBoxFunctionCalled\n"); asString = state; DestroyLutControls(); const char* buf; @@ -412,7 +412,7 @@ void CheckBoxFunction(int state) // ShowLookUpTableDialog function to improve performance in Qt void CountFunction(QString text) { - printf("TextFunction called:%s\n",text.toStdString().c_str()); + // printf("TextFunction called:%s\n",text.toStdString().c_str()); const char* buf; buf = text.toStdString().c_str(); if(atoi(buf) != ControlCount && !asString) { @@ -430,7 +430,7 @@ void CountFunction(QString text) // ShowLookUpTableDialog function to improve performance in Qt void StringFunction(QString text) { - printf("StringFunction called \n"); + // printf("StringFunction called \n"); char* scratch = (char*)text.toStdString().c_str(); // SendMessage(StringTextbox, WM_GETTEXT, (WPARAM)sizeof(scratch), // (LPARAM)scratch); diff --git a/ldmicro/simpledialog.cpp b/ldmicro/simpledialog.cpp index ed60ad2..6256ef8 100644 --- a/ldmicro/simpledialog.cpp +++ b/ldmicro/simpledialog.cpp @@ -60,7 +60,7 @@ static BOOL NoCheckingOnBox[MAX_BOXES]; #define SD_FORMATTED_STRING 0x0000010 #define SD_PERSIST 0x0000011 -inline void DestroyWindow (int boxes){ +static inline void DestroyWindow (int boxes){ for(int i = 0; i < boxes; i++) { delete Labels[i]; delete Textboxes[i]; diff --git a/ldmicro/simulate.cpp b/ldmicro/simulate.cpp index 2d9be0b..9df6a58 100644 --- a/ldmicro/simulate.cpp +++ b/ldmicro/simulate.cpp @@ -958,7 +958,7 @@ void DestroyUartSimulationWindow(void) FreezeDWORD(TerminalW); FreezeDWORD(TerminalH); - DestroyWindow(UartSimulationWindow); + // DestroyWindow(UartSimulationWindow); ProgramChanged(); UartSimulationWindow = NULL; } -- cgit