-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuibuilder.cpp
More file actions
148 lines (129 loc) · 4.92 KB
/
Copy pathuibuilder.cpp
File metadata and controls
148 lines (129 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "uibuilder.h"
#include <QMessageBox>
#include <QLabel> // Asegúrate de incluir QLabel
#include <QLineEdit> // Asegúrate de incluir QLineEdit
#include <QComboBox> // Asegúrate de incluir QComboBox
#include <QLayout> // Asegúrate de incluir QLayout (para QLayoutItem)
UiBuilder::UiBuilder(Ui::MainWindow *ui, QObject *parent)
: QObject(parent), ui(ui) {}
void UiBuilder::generarCampos() {
int numVariables = ui->spinVariables_2->value();
int numRestricciones = ui->spinRestricciones_2->value();
QLayoutItem* item;
while ((item = ui->layoutObjetivo_2->takeAt(0)) != nullptr) {
if (item->widget()) {
delete item->widget();
}
delete item;
}
while ((item = ui->layoutRestricciones_2->takeAt(0)) != nullptr) {
if (item->widget()) {
delete item->widget();
}
delete item;
}
coefObjetivo.clear();
coefRestricciones.clear();
signoRestricciones.clear();
ladoDerecho.clear();
for (int i = 0; i < numVariables; ++i) {
QLineEdit* edit = new QLineEdit();
edit->setFixedWidth(70);
edit->setPlaceholderText("0");
coefObjetivo.append(edit);
ui->layoutObjetivo_2->addWidget(edit, 0, i * 3);
QLabel* label = new QLabel("x" + QString::number(i + 1));
label->setContentsMargins(5, 0, 10, 0);
ui->layoutObjetivo_2->addWidget(label, 0, i * 3 + 1);
if (i < numVariables - 1) {
QLabel* plus = new QLabel("+");
plus->setContentsMargins(0, 0, 10, 0);
ui->layoutObjetivo_2->addWidget(plus, 0, i * 3 + 2);
}
}
for (int i = 0; i < numRestricciones; ++i) {
QVector<QLineEdit*> fila;
for (int j = 0; j < numVariables; ++j) {
QLineEdit* edit = new QLineEdit();
edit->setFixedWidth(70);
edit->setPlaceholderText("0");
fila.append(edit);
ui->layoutRestricciones_2->addWidget(edit, i, j * 3);
QLabel* label = new QLabel("x" + QString::number(j + 1));
label->setContentsMargins(5, 0, 10, 0);
ui->layoutRestricciones_2->addWidget(label, i, j * 3 + 1);
if (j < numVariables - 1) {
QLabel* plus = new QLabel("+");
plus->setContentsMargins(0, 0, 10, 0);
ui->layoutRestricciones_2->addWidget(plus, i, j * 3 + 2);
}
}
coefRestricciones.append(fila);
QComboBox* signo = new QComboBox();
signo->addItems({"<=", "=", ">="});
signo->setFixedWidth(80);
signoRestricciones.append(signo);
ui->layoutRestricciones_2->addWidget(signo, i, numVariables * 3);
QLineEdit* ld = new QLineEdit();
ld->setFixedWidth(80);
ld->setPlaceholderText("0");
ladoDerecho.append(ld);
ui->layoutRestricciones_2->addWidget(ld, i, numVariables * 3 + 1);
}
}
DatosEntrada UiBuilder::leerDatos() {
DatosEntrada datos;
bool todoOk = true;
for (auto* edit : coefObjetivo) {
QString texto = edit->text().trimmed();
if (texto.isEmpty()) {
QMessageBox::warning(nullptr, "Error", "Por favor llena todos los campos de la función objetivo.");
todoOk = false;
break;
}
bool ok;
double valor = texto.toDouble(&ok);
if (!ok) {
QMessageBox::warning(nullptr, "Error", "Coeficiente inválido en la función objetivo: " + texto);
todoOk = false;
break;
}
datos.objetivo.append(valor);
}
for (int i = 0; i < coefRestricciones.size() && todoOk; ++i) {
QVector<double> fila;
for (auto* edit : coefRestricciones[i]) {
QString texto = edit->text().trimmed();
if (texto.isEmpty()) {
fila.append(0.0);
} else {
bool ok;
double valor = texto.toDouble(&ok);
if (!ok) {
QMessageBox::warning(nullptr, "Error", "Coeficiente inválido en restricción " + QString::number(i + 1) + ": " + texto);
todoOk = false;
break;
}
fila.append(valor);
}
}
datos.restricciones.append(fila);
datos.signos.append(signoRestricciones[i]->currentText());
QString textoLD = ladoDerecho[i]->text().trimmed();
if (textoLD.isEmpty()) {
QMessageBox::warning(nullptr, "Error", "Lado derecho vacío en restricción " + QString::number(i + 1));
todoOk = false;
break;
}
bool ok;
double valorLD = textoLD.toDouble(&ok);
if (!ok) {
QMessageBox::warning(nullptr, "Error", "Lado derecho inválido en restricción " + QString::number(i + 1) + ": " + textoLD);
todoOk = false;
break;
}
datos.ladosDerechos.append(valorLD);
}
datos.ok = todoOk;
return datos;
}