source: observatorio/simulacion/SimEscenariosEconomicos-Qtgui/tempFilterForm.C @ 6f07bdb

simulacion
Last change on this file since 6f07bdb was 6f07bdb, checked in by Alejandro <amujica@…>, 9 years ago

interfaz grafica modificada para soportar los cambios de una sola conexion a la base de datos

  • Property mode set to 100644
File size: 11.6 KB
Line 
1/*
2  Copyright (C) 2012
3  Alejandro Mujica (amujica@cenditel.gob.ve)
4  Erwin Paredes (eparedes@cenditel.gob.ve)
5  José Ruiz (jruiz@cenditel.gob.ve)
6  Rodolfo Rangel (rrangel@cenditel.gob.ve)
7  Julie Vera (jvera@cenditel.gob.ve)
8
9  CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en
10  Tecnologías Libres
11
12  Este programa es software libre; Usted puede usarlo bajo los términos de la
13  licencia de software GPL versión 2.0 de la Free Software Foundation.
14
15  Este programa se distribuye con la esperanza de que sea útil, pero SIN
16  NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o
17  ADECUACIÓN A UN PROPÓSITO PARTICULAR.
18  Consulte la licencia GPL para más detalles. Usted debe recibir una copia
19  de la GPL junto con este programa; si no, escriba a la Free Software
20  Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA.
21*/
22
23/*
24  Este archivo contiene la implementación de la clase TempFilterForm, la cual
25  repesenta el formulario utilizado para filtrar los productos que serán las
26  raíces de la red que se va a construir para estudiar.
27
28  Autor: Alejandro J. Mujica
29  Fecha: 04/12/2013
30*/
31
32# include <tempFilterForm.H>
33
34# include <QMessageBox>
35
36# include <ioManager.H>
37# include <dbProperties.H>
38
39# include <configuration.H>
40# include <simType.H>
41
42struct FillComboYear
43{
44    QComboBox * ptrComboYear;
45
46    FillComboYear(QComboBox * _ptrComboYear)
47        : ptrComboYear(_ptrComboYear) {
48        // Empty
49    }
50
51    void operator () (const std::string & year, const unsigned short &) {
52        ptrComboYear->addItem(year.c_str());
53    }
54};
55
56void TempFilterForm::moveToCenter(QWidget * parent) {
57
58    QPoint halfForm(int(width() / 2), int(height() / 2));
59
60    QPoint halfParent(int(parent->width() / 2), int(parent->height() / 2));
61
62    QPoint position = parent->pos() + halfParent - halfForm;
63
64    move(position);
65}
66
67void TempFilterForm::updateComboYear() {
68
69    ui.comboYear->clear();
70
71    ui.comboYear->addItem("Seleccione Año");
72
73    yearsMap.for_each<FillComboYear>(FillComboYear(ui.comboYear));
74}
75
76TempFilterForm::TempFilterForm(QWidget *parent)
77    : QWidget(parent, Qt::Tool) {
78
79    ui.setupUi(this);
80
81    moveToCenter(parent);
82
83    selectedItem = NULL;
84
85    ui.btnAddProductToSelectedList->setEnabled(false);
86
87    ui.btnRemoveProductFromSelectedList->setEnabled(false);
88
89    ui.edtCompanyName->setFocus();
90
91    ui.layoutWaitingPanel->addWidget(&waitingPanel, Qt::AlignCenter);
92
93    connect(ui.btnAccept, SIGNAL(clicked()),
94            this, SLOT(slotBtnAcceptClicked()));
95
96    connect(ui.edtCompanyName, SIGNAL(textChanged(QString)),
97            this, SLOT(slotTextChanged()));
98
99    connect(ui.companyList, SIGNAL(itemClicked(QListWidgetItem*)),
100            this, SLOT(slotCompanySelected(QListWidgetItem*)));
101
102    connect(ui.productList, SIGNAL(itemClicked(QListWidgetItem*)),
103            this, SLOT(slotProductSelected(QListWidgetItem*)));
104
105    connect(ui.selectedProductList, SIGNAL(itemClicked(QListWidgetItem*)),
106            this, SLOT(slotSelectedProductSelected(QListWidgetItem*)));
107
108    connect(ui.btnAddProductToSelectedList, SIGNAL(clicked()),
109            this, SLOT(slotBtnAddProductToSelectedListClicked()));
110
111    connect(ui.btnRemoveProductFromSelectedList, SIGNAL(clicked()),
112            this, SLOT(slotBtnRemoveProductFromSelectedList()));
113
114    connect(&process, SIGNAL(finished(int)),
115            this, SLOT(slotGraphBuilderExecuted()));
116
117    ui.edtCompanyName->setFocus();
118}
119
120void TempFilterForm::slotBtnAcceptClicked() {
121
122    if (ui.selectedProductList->count() == 0) {
123        QMessageBox::critical(this, "Error",
124                              "No se ha seleccionado ningún producto");
125
126        ui.edtCompanyName->setFocus();
127
128        return;
129    }
130
131    if (ui.comboStudyType->currentIndex() == 0) {
132        QMessageBox::critical(this, "Error",
133                              "Debe seleccionar un tipo de estudio");
134
135        ui.comboStudyType->setFocus();
136
137        return;
138    }
139
140    if (ui.comboYear->currentIndex() == 0) {
141        QMessageBox::critical(this, "Error",
142                              "Debe seleccionar un año de estudio");
143
144        ui.comboYear->setFocus();
145
146        return;
147    }
148
149    Configuration & conf = Configuration::getInstance();
150
151    std::string rootsFileName = FILE_PATH(conf, Xml, Roots, xml).c_str();
152
153    IOManager ioManager;
154
155    DynDlist <long> productIdList;
156
157    for (size_t i = 0; i < ui.selectedProductList->count(); ++i) {
158
159        QListWidgetItem * item = ui.selectedProductList->item(i);
160
161        productIdList.append(item->data(1).toString().toLong());
162    }
163
164    ioManager.createXmlRootsFile(ui.comboYear->currentText().toStdString(),
165                                 productIdList, ui.spinUpstreamLevels->value(),
166                                 ui.spinDownstreamLevels->value(),
167                                 rootsFileName);
168
169    conf.createDbFile();
170
171    std::string graphXmlFileName = FILE_PATH(conf, Xml, Graph, xml).c_str();
172
173    QStringList args;
174
175    args.append(QString("-i") + rootsFileName.c_str());
176    args.append(QString("-o") + graphXmlFileName.c_str());
177    args.append(QString("-b") + conf.getDbFileName().c_str());
178
179    process.start(conf.getGraphBuilderCommand().c_str(), args);
180
181    waitingPanel.start();
182
183    ui.btnAccept->setEnabled(false);
184    ui.btnCancel->setEnabled(false);
185    ui.btnAddProductToSelectedList->setEnabled(false);
186    ui.btnRemoveProductFromSelectedList->setEnabled(false);
187    ui.companyList->setEnabled(false);
188    ui.productList->setEnabled(false);
189    ui.selectedProductList->setEnabled(false);
190    ui.comboStudyType->setEnabled(false);
191    ui.comboYear->setEnabled(false);
192    ui.spinDownstreamLevels->setEnabled(false);
193    ui.spinUpstreamLevels->setEnabled(false);
194}
195
196void TempFilterForm::slotTextChanged() {
197
198    if (selectedItem != NULL) {
199        selectedItem->setSelected(false);
200        selectedItem = NULL;
201    }
202
203    ui.btnAddProductToSelectedList->setEnabled(false);
204    ui.btnRemoveProductFromSelectedList->setEnabled(false);
205    ui.companyList->clear();
206    ui.productList->clear();
207
208    if (ui.edtCompanyName->text().size() < 3)
209        return;
210
211    List <CompanyRifName> companyList;
212
213    try {
214
215        listCompaniesLike(conn, ui.edtCompanyName->text().toStdString(),
216                          companyList);
217
218        for (List <CompanyRifName>::Iterator it(companyList); it.has_current();
219             it.next()) {
220
221            CompanyRifName & company = it.get_current();
222
223            QString text;
224
225            text.append(company.rif.c_str());
226            text.append(" -- ");
227            text.append(company.name.c_str());
228
229            QListWidgetItem * item = new QListWidgetItem(text);
230            item->setData(1, QVariant(company.rif.c_str()));
231
232            ui.companyList->addItem(item);
233        }
234    } catch (const std::exception & e) {
235
236        QString msg = "Se ha capturado la siguiente excepción: ";
237
238        msg.append(e.what());
239
240        QMessageBox::critical(this, "Error", msg);
241    }
242}
243
244void TempFilterForm::slotCompanySelected(QListWidgetItem * item) {
245
246    if (selectedItem != NULL) {
247        selectedItem->setSelected(false);
248        selectedItem = NULL;
249    }
250
251    ui.btnAddProductToSelectedList->setEnabled(false);
252    ui.btnRemoveProductFromSelectedList->setEnabled(false);
253    ui.productList->clear();
254
255    List <ProductIdName> productList;
256
257    I(item != NULL);
258
259    try {
260
261        listProductsByCompany(conn, item->data(1).toString().toStdString(),
262                              productList);
263
264        for (List <ProductIdName>::Iterator it(productList); it.has_current();
265             it.next()) {
266
267            ProductIdName & product= it.get_current();
268
269            QString text;
270
271            text.append(product.name.c_str());
272            text.append(" -- ");
273            text.append(product.technicalSpecifications.c_str());
274
275            QListWidgetItem * item = new QListWidgetItem(text);
276            item->setData(1, QVariant(QString().setNum(product.id)));
277
278            ui.productList->addItem(item);
279        }
280    } catch (const std::exception & e) {
281
282        QString msg = "Se ha capturado la siguiente excepción: ";
283
284        msg.append(e.what());
285
286        QMessageBox::critical(this, "Error", msg);
287    }
288}
289
290void TempFilterForm::slotProductSelected(QListWidgetItem * item) {
291
292    if (selectedItem == item)
293        return;
294
295    ui.btnAddProductToSelectedList->setEnabled(true);
296    ui.btnRemoveProductFromSelectedList->setEnabled(false);
297
298    if (selectedItem != NULL)
299        selectedItem->setSelected(false);
300
301    selectedItem = item;
302}
303
304void TempFilterForm::slotSelectedProductSelected(QListWidgetItem * item) {
305
306    if (selectedItem == item)
307        return;
308
309    ui.btnRemoveProductFromSelectedList->setEnabled(true);
310    ui.btnAddProductToSelectedList->setEnabled(false);
311
312    if (selectedItem != NULL)
313        selectedItem->setSelected(false);
314
315    selectedItem = item;
316}
317
318void TempFilterForm::slotBtnAddProductToSelectedListClicked() {
319
320    long id = selectedItem->data(1).toString().toLong();
321
322    if (productIdSet.insert(id) == NULL)
323        return;
324
325    ui.btnRemoveProductFromSelectedList->setEnabled(true);
326    ui.btnAddProductToSelectedList->setEnabled(false);
327
328    QListWidgetItem * newItem = new QListWidgetItem(*selectedItem);
329
330    ui.selectedProductList->addItem(newItem);
331
332    selectedItem = newItem;
333    newItem->setSelected(true);
334
335    if (not ui.productList->selectedItems().isEmpty())
336        ui.productList->selectedItems().at(0)->setSelected(false);
337
338    List<std::string> years;
339
340    getProductionYears(conn, id, years);
341
342    for (List<std::string>::Iterator it(years); it.has_current(); it.next()) {
343
344        const std::string & year = it.get_current();
345
346        unsigned short * ptrQuantity = yearsMap.search(year);
347
348        if (ptrQuantity == NULL) {
349            ptrQuantity = yearsMap.insert(year, 0);
350        }
351
352        ++(*ptrQuantity);
353    }
354
355    updateComboYear();
356}
357
358void TempFilterForm::slotBtnRemoveProductFromSelectedList() {
359
360    long id = selectedItem->data(1).toString().toLong();
361
362    productIdSet.remove(id);
363
364    delete selectedItem;
365    selectedItem = NULL;
366
367    if (ui.selectedProductList->selectedItems().isEmpty())
368        ui.btnRemoveProductFromSelectedList->setEnabled(false);
369    else
370        selectedItem = ui.selectedProductList->selectedItems().at(0);
371
372    List<std::string> years;
373
374    getProductionYears(conn, id, years);
375
376    for (List<std::string>::Iterator it(years); it.has_current(); it.next()) {
377
378        const std::string & year = it.get_current();
379
380        unsigned short * ptrQuantity = yearsMap.search(year);
381
382        --(*ptrQuantity);
383
384        if (*ptrQuantity == 0) {
385            yearsMap.remove(year);
386        }
387    }
388
389    updateComboYear();
390}
391
392void TempFilterForm::slotGraphBuilderExecuted() {
393
394    ui.btnAccept->setEnabled(true);
395    ui.btnCancel->setEnabled(true);
396    ui.btnAddProductToSelectedList->setEnabled(true);
397    ui.btnRemoveProductFromSelectedList->setEnabled(true);
398    ui.companyList->setEnabled(true);
399    ui.productList->setEnabled(true);
400    ui.selectedProductList->setEnabled(true);
401    ui.comboStudyType->setEnabled(true);
402    ui.comboYear->setEnabled(true);
403    ui.spinDownstreamLevels->setEnabled(true);
404    ui.spinUpstreamLevels->setEnabled(true);
405
406    waitingPanel.stop();
407
408    Configuration::getInstance().removeDbFile();
409
410    if (process.exitStatus() == QProcess::CrashExit) {
411        QMessageBox::critical(
412                    this, "Error",
413                    "El programa de construcción de red se estrelló");
414        return;
415    }
416
417    SimType::getInstance().setType(
418                StudyType(ui.comboStudyType->currentIndex() - 1));
419
420    SimType::getInstance().setYear(ui.comboYear->currentText().toStdString());
421
422    emit signalFormAccepted();
423
424    close();
425
426}
427
428void TempFilterForm::closeEvent(QCloseEvent *) {
429    emit signalClosing();
430}
Note: See TracBrowser for help on using the repository browser.