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

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

Agregada la funcionalidad de carga de year dinamicamente al seleccionar productos en la Interfaz

  • 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(ui.edtCompanyName->text().toStdString(), companyList);
216
217        for (List <CompanyRifName>::Iterator it(companyList); it.has_current();
218             it.next()) {
219
220            CompanyRifName & company = it.get_current();
221
222            QString text;
223
224            text.append(company.rif.c_str());
225            text.append(" -- ");
226            text.append(company.name.c_str());
227
228            QListWidgetItem * item = new QListWidgetItem(text);
229            item->setData(1, QVariant(company.rif.c_str()));
230
231            ui.companyList->addItem(item);
232        }
233    } catch (const std::exception & e) {
234
235        QString msg = "Se ha capturado la siguiente excepción: ";
236
237        msg.append(e.what());
238
239        QMessageBox::critical(this, "Error", msg);
240    }
241}
242
243void TempFilterForm::slotCompanySelected(QListWidgetItem * item) {
244
245    if (selectedItem != NULL) {
246        selectedItem->setSelected(false);
247        selectedItem = NULL;
248    }
249
250    ui.btnAddProductToSelectedList->setEnabled(false);
251    ui.btnRemoveProductFromSelectedList->setEnabled(false);
252    ui.productList->clear();
253
254    List <ProductIdName> productList;
255
256    I(item != NULL);
257
258    try {
259
260        listProductsByCompany(item->data(1).toString().toStdString(),
261                              productList);
262
263        for (List <ProductIdName>::Iterator it(productList); it.has_current();
264             it.next()) {
265
266            ProductIdName & product= it.get_current();
267
268            QString text;
269
270            text.append(product.name.c_str());
271            text.append(" -- ");
272            text.append(product.technicalSpecifications.c_str());
273
274            QListWidgetItem * item = new QListWidgetItem(text);
275            item->setData(1, QVariant(QString().setNum(product.id)));
276
277            ui.productList->addItem(item);
278        }
279    } catch (const std::exception & e) {
280
281        QString msg = "Se ha capturado la siguiente excepción: ";
282
283        msg.append(e.what());
284
285        QMessageBox::critical(this, "Error", msg);
286    }
287}
288
289void TempFilterForm::slotProductSelected(QListWidgetItem * item) {
290
291    if (selectedItem == item)
292        return;
293
294    ui.btnAddProductToSelectedList->setEnabled(true);
295    ui.btnRemoveProductFromSelectedList->setEnabled(false);
296
297    if (selectedItem != NULL)
298        selectedItem->setSelected(false);
299
300    selectedItem = item;
301}
302
303void TempFilterForm::slotSelectedProductSelected(QListWidgetItem * item) {
304
305    if (selectedItem == item)
306        return;
307
308    ui.btnRemoveProductFromSelectedList->setEnabled(true);
309    ui.btnAddProductToSelectedList->setEnabled(false);
310
311    if (selectedItem != NULL)
312        selectedItem->setSelected(false);
313
314    selectedItem = item;
315}
316
317void TempFilterForm::slotBtnAddProductToSelectedListClicked() {
318
319    long id = selectedItem->data(1).toString().toLong();
320
321    if (productIdSet.insert(id) == NULL)
322        return;
323
324    ui.btnRemoveProductFromSelectedList->setEnabled(true);
325    ui.btnAddProductToSelectedList->setEnabled(false);
326
327    QListWidgetItem * newItem = new QListWidgetItem(*selectedItem);
328
329    ui.selectedProductList->addItem(newItem);
330
331    selectedItem = newItem;
332    newItem->setSelected(true);
333
334    if (not ui.productList->selectedItems().isEmpty())
335        ui.productList->selectedItems().at(0)->setSelected(false);
336
337    List<std::string> years;
338
339    getProductionYears(id, years);
340
341    for (List<std::string>::Iterator it(years); it.has_current(); it.next()) {
342
343        const std::string & year = it.get_current();
344
345        unsigned short * ptrQuantity = yearsMap.search(year);
346
347        if (ptrQuantity == NULL) {
348            ptrQuantity = yearsMap.insert(year, 0);
349        }
350
351        ++(*ptrQuantity);
352    }
353
354    updateComboYear();
355}
356
357void TempFilterForm::slotBtnRemoveProductFromSelectedList() {
358
359    long id = selectedItem->data(1).toString().toLong();
360
361    productIdSet.remove(id);
362
363    delete selectedItem;
364    selectedItem = NULL;
365
366    if (ui.selectedProductList->selectedItems().isEmpty())
367        ui.btnRemoveProductFromSelectedList->setEnabled(false);
368    else
369        selectedItem = ui.selectedProductList->selectedItems().at(0);
370
371    List<std::string> years;
372
373    getProductionYears(id, years);
374
375    for (List<std::string>::Iterator it(years); it.has_current(); it.next()) {
376
377        const std::string & year = it.get_current();
378
379        unsigned short * ptrQuantity = yearsMap.search(year);
380
381        --(*ptrQuantity);
382
383        if (*ptrQuantity == 0) {
384            yearsMap.remove(year);
385        }
386    }
387
388    updateComboYear();
389}
390
391void TempFilterForm::slotGraphBuilderExecuted() {
392
393    ui.btnAccept->setEnabled(true);
394    ui.btnCancel->setEnabled(true);
395    ui.btnAddProductToSelectedList->setEnabled(true);
396    ui.btnRemoveProductFromSelectedList->setEnabled(true);
397    ui.companyList->setEnabled(true);
398    ui.productList->setEnabled(true);
399    ui.selectedProductList->setEnabled(true);
400    ui.comboStudyType->setEnabled(true);
401    ui.comboYear->setEnabled(true);
402    ui.spinDownstreamLevels->setEnabled(true);
403    ui.spinUpstreamLevels->setEnabled(true);
404
405    waitingPanel.stop();
406
407    Configuration::getInstance().removeDbFile();
408
409    if (process.exitStatus() == QProcess::CrashExit) {
410        QMessageBox::critical(
411                    this, "Error",
412                    "El programa de construcción de red se estrelló");
413        return;
414    }
415
416    SimType::getInstance().setType(
417                StudyType(ui.comboStudyType->currentIndex() - 1));
418
419    SimType::getInstance().setYear(ui.comboYear->currentText().toStdString());
420
421    emit signalFormAccepted();
422
423    close();
424
425}
426
427void TempFilterForm::closeEvent(QCloseEvent *) {
428    emit signalClosing();
429}
Note: See TracBrowser for help on using the repository browser.