Changeset 3e5fe28 in mmcs for mainwindow.C


Ignore:
Timestamp:
Dec 16, 2015, 12:44:28 PM (8 years ago)
Author:
rboet <rudmanmrrod@…>
Branches:
master
Children:
29305b5
Parents:
68e57de
Message:

Agregada nueva funcionabilidad para cargar matrices desde csv

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mainwindow.C

    r68e57de r3e5fe28  
    581581}
    582582
    583 void MainWindow::populateTable(QTableWidget * tableWidget) {
    584 
     583void MainWindow::populateTable(QTableWidget * tableWidget)
     584{
     585    //Se abre el archivo en modo lectura
    585586    QFile file(csvFilePath);
    586     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    587     {
    588         int row = 0;
    589         QString lineHead = file.readLine();
    590         const std::vector<std::string> rowVH =
    591                 csv_read_row(lineHead.toStdString(), csvSeparator);
    592 
    593 
    594         matrixSize = rowVH.size();
    595         tableWidget->setRowCount(matrixSize+1);
    596         tableWidget->setColumnCount(matrixSize+1);
    597 
    598         for(int column=0; column<matrixSize; column++) {
    599             QTableWidgetItem *newItem = new QTableWidgetItem(
    600                     QString::fromUtf8(rowVH[column].c_str()).
    601                     toLocal8Bit().constData());
    602             newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);//Se coloca como no editable
    603             CellStyleComponente(newItem);
    604             tableWidget->setItem(row, column+1, newItem);
    605         }
    606         ++row;
    607 
    608         while (!file.atEnd() and row<=matrixSize)
    609         {
    610             QTableWidgetItem *newItem = new QTableWidgetItem(
    611                     QString::fromUtf8(rowVH[row-1].c_str()).
    612                     toLocal8Bit().constData());
    613             newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);//Se coloca como no editable
    614             CellStyleComponente(newItem);
    615             tableWidget->setItem(row, 0, newItem);
    616 
    617             QString line = file.readLine();
    618 
    619             std::vector<std::string> rowV =
    620                     csv_read_row(line.toStdString(), csvSeparator);
    621             for(int column=0, leng=rowV.size();
    622             column < leng and column<matrixSize; column++) {
    623 
    624                 /*              Aqui se incorporan los valores luego de la coma(,)          */
    625                 QString rowVal = QString::fromUtf8(rowV[column].c_str());
    626                 double value = rowVal.toDouble();
    627 
    628 
    629                 QTableWidgetItem *newItem = new QTableWidgetItem(
    630                         numberFormat(value).
    631                         toLocal8Bit().constData());
    632                 newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);//Se coloca como no editable
    633                 newItem->setTextAlignment(Qt::AlignCenter);
    634                 tableWidget->setItem(row, column+1, newItem);
    635                 matrix[row-1][column] =  atof(rowV[column].c_str());
    636             }
    637             ++row;
    638         }
    639         file.close();
    640     }
    641 }
    642 
    643 std::vector<std::string> MainWindow::csv_read_row(std::string line,
    644                                                   char delimiter)
    645 {
    646     std::stringstream ss(line);    void msghere();
    647     return csv_read_row(ss, delimiter);
    648 }
    649 
    650 std::vector<std::string> MainWindow::csv_read_row(std::istream &in,
    651                                                   char delimiter)
    652 {
    653     std::stringstream ss;
    654     bool inquotes = false;
    655     std::vector<std::string> row;//relying on RVO
    656     while(in.good())
    657     {
    658         char c = in.get();
    659         if (!inquotes && c=='"') //beginquotechar
    660         {
    661             inquotes=true;
    662         }
    663         else if (inquotes && c=='"') //quotechar
    664         {
    665             if ( in.peek() == '"')//2 consecutive quotes resolve to 1
    666             {
    667                 ss << (char)in.get();
    668             }
    669             else //endquotechar
    670             {
    671                 inquotes=false;
    672             }
    673         }
    674         else if (!inquotes && c==delimiter) //end of field
    675         {
    676             row.push_back( ss.str() );
    677             ss.str("");
    678         }
    679         else if (!inquotes && (c=='\r' || c=='\n') )
    680         {
    681             if(in.peek()=='\n') { in.get(); }
    682             row.push_back( ss.str() );
    683             return row;
    684         }
    685         else
    686         {
    687             ss << c;
     587    file.open(QFile::ReadOnly | QFile::Text);
     588    QTextStream in(&file);
     589    QString linea = in.readLine();//Se lee la primera linea(con los nombres)
     590    QStringList lines = linea.split(";");//Se separan por ; y se guardan en una lista
     591    int column = lines.count();
     592    CrearTablaVacia(column+2,tableWidget);//Se crea una tabla vacia a modo de matriz cuadrada
     593    //Se leen los componentes y se distribuyen en filas y columnas
     594    for(int i=1;i<=column;i++)
     595    {
     596        QTableWidgetItem *itemFila = new QTableWidgetItem(lines.at(i-1));
     597        CellStyleComponente(itemFila);
     598        itemFila->setFlags(itemFila->flags() ^ Qt::ItemIsEditable);
     599        tableWidget->setItem(0,i,itemFila);
     600        QTableWidgetItem *itemColumna = new QTableWidgetItem(lines.at(i-1));
     601        CellStyleComponente(itemColumna);
     602        itemColumna->setFlags(itemColumna->flags() ^ Qt::ItemIsEditable);
     603        tableWidget->setItem(i,0,itemColumna);
     604    }
     605    int row = 1;
     606    while(!in.atEnd())//Se lee el archivo hasta el final
     607    {
     608        linea = in.readLine();
     609        lines = linea.split(";");
     610        //Se recorre el resto del archivo y se anexan los valores
     611        for(int i=1;i<=column;i++)
     612        {
     613            double value = 0;
     614            if(i<lines.count())//En caso de que hallan mas componentes que columnas
     615            {
     616                value = lines.at(i-1).toDouble();
     617            }
     618            QTableWidgetItem *tw = new QTableWidgetItem(numberFormat(value));
     619            tw->setFlags(tw->flags() ^ Qt::ItemIsEditable);
     620            tableWidget->setItem(row,i,tw);
     621        }
     622        row++;
     623    }
     624    file.close();
     625    //Aqui se evalua el caso particular de que las falten filas con respecto a los componentes
     626    if(row-1<column)
     627    {
     628        for (int j=row-1;j<=column;j++)
     629        {
     630            for(int i=1;i<=column;i++)
     631            {
     632                double value = 0;
     633                QTableWidgetItem *tw = new QTableWidgetItem(numberFormat(value));
     634                tw->setFlags(tw->flags() ^ Qt::ItemIsEditable);
     635                tableWidget->setItem(row-1,i,tw);
     636            }
     637            row++;
    688638        }
    689639    }
     
    24612411        }
    24622412    }
    2463     qDebug()<<"total de las cuentas"<<totalCuentas;
    24642413    estimarVectorPonderacion();
    24652414}
     
    24802429        }
    24812430    }
    2482     qDebug()<<"Vector Ponderacion"<<Vpond;
    24832431}
    24842432
Note: See TracChangeset for help on using the changeset viewer.