source: mmcs/mainwindow.C @ 44d1e60

matrices
Last change on this file since 44d1e60 was 44d1e60, checked in by Jose Ruiz <joseruiz@…>, 9 years ago

commit inicial

  • Property mode set to 100644
File size: 9.3 KB
Line 
1#include "mainwindow.H"
2void MainWindow::slotLoadMatrix()
3{
4    formLoadMatrix = new  FormLoadMatrix(this);
5    formLoadMatrix->show();
6    connect(formLoadMatrix, SIGNAL(formAccepted(QString,int,char)),
7            this, SLOT(slotFormLoadMatrixAccepted(QString,int,char)));
8}
9void MainWindow::slotExportMatrix()
10{
11    QMessageBox::warning(this,"Warning", "Opcion en desarrollo",
12                         QMessageBox::Ok);
13}
14void MainWindow::closeEvent(QCloseEvent * event)
15{
16    if(QMessageBox::question(this, "Warning", "Are you sure?",
17                             QMessageBox::Yes | QMessageBox::No) ==
18       QMessageBox::No)
19    {
20        event->ignore();
21    }
22}
23
24void MainWindow::initGUI()
25{
26    createMenuBar();
27    createCentralWidget();
28}
29
30void MainWindow::createCentralWidget()
31{
32    QVBoxLayout * layoutCentralWidget = new QVBoxLayout;
33
34    QHBoxLayout * layoutTitle = new QHBoxLayout;
35    QLabel * lblTitle = new QLabel("Estructura de la Matriz de " \
36                                   "Contabilidad Social");
37    lblTitle->setFont(QFont("Aero Matics", 25, 1));
38    lblTitle->setAlignment(Qt::AlignCenter | Qt::AlignBottom);
39    layoutTitle->addWidget(lblTitle);
40    layoutCentralWidget->addLayout(layoutTitle);
41
42    QHBoxLayout * layoutMatrix = new QHBoxLayout;
43    QLabel * lblMatrix = new QLabel;
44    lblMatrix->setAlignment(Qt::AlignCenter);
45    lblMatrix->setPixmap(QPixmap("./img/Imagen-matriz.png"));
46    layoutMatrix->addWidget(lblMatrix);
47    layoutCentralWidget->addLayout(layoutMatrix);
48
49    QHBoxLayout * layoutFoot = new QHBoxLayout;
50    QLabel * lblFoot = new QLabel("Fundación Centro Nacional de Desarrollo e" \
51                                  " Investigación en\nTecnologías Libres." \
52                                  " Nodo Mérida.");
53    lblFoot->setFont(QFont("Aero Matics", 19, 1));
54    lblFoot->setAlignment(Qt::AlignRight | Qt::AlignBottom);
55    QLabel * lblLogo = new QLabel;
56    lblLogo->setPixmap(QPixmap("./img/logo_cenditel.jpg"));
57    lblLogo->setFixedWidth(lblLogo->pixmap()->width());
58    lblLogo->setAlignment(Qt::AlignRight | Qt::AlignBottom);
59    layoutFoot->addWidget(lblFoot);
60    layoutFoot->addWidget(lblLogo);
61    layoutCentralWidget->addLayout(layoutFoot);
62
63    QWidget * widget = new QWidget;
64    widget->setLayout(layoutCentralWidget);
65    setCentralWidget(widget);
66}
67
68void MainWindow::createMenuBar()
69{
70    menuFile.setTitle("&Archivo");
71
72    actionLoadMatrix.setText("&Cargar Matriz");
73    menuFile.addAction(&actionLoadMatrix);
74
75    actionExportMatrix.setText("&Exportar Matriz");
76    menuFile.addAction(&actionExportMatrix);
77    actionExportMatrix.setDisabled(true);
78
79    actionQuit.setText("&Salir");
80    menuFile.addAction(&actionQuit);
81
82
83    menuBar()->addMenu(&menuFile);
84}
85
86MainWindow::MainWindow()
87    : actionLoadMatrix(this), actionExportMatrix(this), actionQuit(this),
88    formLoadMatrix(0)
89{
90    initGUI();
91
92    connect(&actionLoadMatrix, SIGNAL(triggered()), this,
93            SLOT(slotLoadMatrix()));
94    connect(&actionExportMatrix, SIGNAL(triggered()), this,
95            SLOT(slotExportMatrix()));
96    connect(&actionQuit, SIGNAL(triggered()), this, SLOT(close()));
97   
98    showMaximized();
99}
100
101void MainWindow::slotFormLoadMatrixAccepted(const QString & filePath,
102                                            int accountNumber, char sep)
103{
104    QString msg = "Archivo: " + filePath + "\nNúmero de cuentas: " +
105                   QString().setNum(accountNumber) +
106                   "\nSeparador: " + sep;
107
108    //QMessageBox::information(this, "Información", msg);
109    csvFilePath = filePath;
110    csvSeparator = sep;
111    numAccounts = accountNumber;
112
113    createMatrixCentralWidget();
114}
115
116void MainWindow::slotFormLoadMatrixClosed()
117{
118    disconnect(formLoadMatrix, SIGNAL(formAccepted(QString,int,char)),
119               this, SLOT(slotFormLoadMatrixAccepted(QString,int,char)));
120    formLoadMatrix = 0;
121}
122
123
124
125void MainWindow::createMatrixCentralWidget()
126{
127    QHBoxLayout * layoutCentralWidget = new QHBoxLayout;
128    QVBoxLayout * layoutLateralWidget = new QVBoxLayout;
129
130    QTableWidget * tableWidget = new QTableWidget(this);
131    QGroupBox * groupBoxAccount = new QGroupBox;
132    QPushButton * buttonEnd = new QPushButton;
133
134    populateTable(tableWidget);
135
136    layoutCentralWidget->addWidget(tableWidget);
137    //layoutCentralWidget->addStretch();
138
139    QVBoxLayout * layoutAccounts = new QVBoxLayout;
140
141    layoutAccounts->addWidget(new StackWidget(numAccounts, groupBoxAccount));
142
143    groupBoxAccount->setFixedWidth(220);
144    groupBoxAccount->setLayout(layoutAccounts);;
145    groupBoxAccount->setTitle("Cuentas");
146    groupBoxAccount->setStyleSheet("QGroupBox {border: 1px solid gray; "
147                     "border-radius: 3px; margin-top: 0.5em;} "
148                     "QGroupBox::title { subcontrol-origin: margin; "
149                     "left: 10px; padding: 0 3px 0 3px; } ");
150
151
152
153    layoutLateralWidget->addStretch(1);
154    layoutLateralWidget->addWidget(groupBoxAccount);
155    buttonEnd->setText("Finalizar Carga");
156    buttonEnd->setFixedWidth(130);
157    buttonEnd->setStyleSheet("background-color: #358ccb; color: #fff;"
158                             "font-weight: bold; height: 30px; border: none;"
159                             "border-radius: 5px; margin-top: 40px;");
160    layoutLateralWidget->addWidget(buttonEnd);
161    layoutLateralWidget->addStretch(6);
162
163    layoutCentralWidget->addLayout(layoutLateralWidget);
164
165
166
167    //QWidget * widgetLateral = new QWidget;
168    //widgetLateral->setLayout(layoutLateralWidget);
169    //layoutCentralWidget->addWidget(widgetLateral,1,0);
170
171
172
173
174    QWidget * widget = new QWidget;
175    widget->setLayout(layoutCentralWidget);
176    setCentralWidget(widget);
177}
178
179void MainWindow::populateTable(QTableWidget * tableWidget) {
180
181
182    QFile file(csvFilePath);
183    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
184    {
185        int row = 0;
186        QString lineHead = file.readLine();
187        std::vector<std::string> rowVH =
188                csv_read_row(lineHead.toStdString(), csvSeparator);
189
190        matrixSize = rowVH.size();
191        tableWidget->setRowCount(matrixSize+1);
192        tableWidget->setColumnCount(matrixSize+1);
193
194        for(int column=0; column<matrixSize; column++) {
195            QTableWidgetItem *newItem = new QTableWidgetItem(
196                    QString::fromUtf8(rowVH[column].c_str()).
197                    toLocal8Bit().constData());
198            tableWidget->setItem(row, column+1, newItem);
199        }
200        ++row;
201
202        while (!file.atEnd() and row<=matrixSize)
203        {
204            QTableWidgetItem *newItem = new QTableWidgetItem(
205                    QString::fromUtf8(rowVH[row-1].c_str()).
206                    toLocal8Bit().constData());
207            tableWidget->setItem(row, 0, newItem);
208
209            QString line = file.readLine();
210            /*QStringList strings = line.split(csvSeparator);
211            for (int column = 0; column < strings.size(); ++column) {
212                QTableWidgetItem *newItem = new QTableWidgetItem(
213                        strings.at(column).toLocal8Bit().constData());
214                tableWidget->setItem(row, column, newItem);
215
216            }*/
217            std::vector<std::string> rowV =
218                    csv_read_row(line.toStdString(), csvSeparator);
219
220            for(int column=0, leng=rowV.size();
221            column < leng and column<matrixSize; column++) {
222
223                double value = (double)atof(rowV[column].c_str());
224
225                QTableWidgetItem *newItem = new QTableWidgetItem(
226                        numberFormat(value).
227                        toLocal8Bit().constData());
228
229                tableWidget->setItem(row, column+1, newItem);
230                matrix[row-1][column] =  atof(rowV[column].c_str());
231            }
232            ++row;
233        }
234        file.close();
235    }
236}
237
238std::vector<std::string> MainWindow::csv_read_row(std::string line,
239                                                  char delimiter)
240{
241    std::stringstream ss(line);
242    return csv_read_row(ss, delimiter);
243}
244
245std::vector<std::string> MainWindow::csv_read_row(std::istream &in,
246                                                  char delimiter)
247{
248    std::stringstream ss;
249    bool inquotes = false;
250    std::vector<std::string> row;//relying on RVO
251    while(in.good())
252    {
253        char c = in.get();
254        if (!inquotes && c=='"') //beginquotechar
255        {
256            inquotes=true;
257        }
258        else if (inquotes && c=='"') //quotechar
259        {
260            if ( in.peek() == '"')//2 consecutive quotes resolve to 1
261            {
262                ss << (char)in.get();
263            }
264            else //endquotechar
265            {
266                inquotes=false;
267            }
268        }
269        else if (!inquotes && c==delimiter) //end of field
270        {
271            row.push_back( ss.str() );
272            ss.str("");
273        }
274        else if (!inquotes && (c=='\r' || c=='\n') )
275        {
276            if(in.peek()=='\n') { in.get(); }
277            row.push_back( ss.str() );
278            return row;
279        }
280        else
281        {
282            ss << c;
283        }
284    }
285}
286
287QString MainWindow::numberFormat(double & d) {
288
289    int precision = 2;
290
291    QString stringNumber = QString::number(d, 'f', precision);
292    for(int point = 0, i = (stringNumber.lastIndexOf('.') == -1 ? stringNumber.length() : stringNumber.lastIndexOf('.')); i > 0; --i, ++point)
293    {
294        if(point != 0 && point % 3 == 0)
295        {
296            stringNumber.insert(i, '*');
297        }
298    }
299    stringNumber.replace(".", ",");
300    stringNumber.replace("*", ".");
301    return stringNumber;
302}
Note: See TracBrowser for help on using the repository browser.