Exemple #1
0
    /**
    Process selected rows
    */
    void ReflMainViewPresenter::process()
    {
      if(m_model->rowCount() == 0)
      {
        m_view->giveUserWarning("Cannot process an empty Table","Warning");
        return;
      }

      std::vector<size_t> rows = m_view->getSelectedRowIndexes();
      if(rows.size() == 0)
      {
        //Does the user want to abort?
        if(!m_view->askUserYesNo("This will process all rows in the table. Continue?","Process all rows?"))
          return;

        //They want to process all rows, so populate rows with every index in the model
        for(size_t idx = 0; idx < m_model->rowCount(); ++idx)
          rows.push_back(idx);
      }

      //Maps group numbers to the list of rows in that group we want to process
      std::map<int,std::vector<size_t> > groups;
      for(auto it = rows.begin(); it != rows.end(); ++it)
      {
        try
        {
          validateRow(*it);

          const int group = m_model->Int(*it, COL_GROUP);
          groups[group].push_back(*it);
        }
        catch(std::exception& ex)
        {
          const std::string rowNo = Mantid::Kernel::Strings::toString<size_t>(*it + 1);
          m_view->giveUserCritical("Error found in row " + rowNo + ":\n" + ex.what(), "Error");
          return;
        }
      }

      int progress = 0;
      //Each group and each row within count as a progress step.
      const int maxProgress = (int)(rows.size() + groups.size());
      m_view->setProgressRange(progress, maxProgress);
      m_view->setProgress(progress);

      for(auto gIt = groups.begin(); gIt != groups.end(); ++gIt)
      {
        const std::vector<size_t> groupRows = gIt->second;

        //Process each row individually
        for(auto rIt = groupRows.begin(); rIt != groupRows.end(); ++rIt)
        {
          try
          {
            processRow(*rIt);
            m_view->setProgress(++progress);
          }
          catch(std::exception& ex)
          {
            const std::string rowNo = Mantid::Kernel::Strings::toString<size_t>(*rIt + 1);
            const std::string message = "Error encountered while processing row " + rowNo + ":\n";
            m_view->giveUserCritical(message + ex.what(), "Error");
            m_view->setProgress(0);
            return;
          }
        }

        try
        {
          stitchRows(groupRows);
          m_view->setProgress(++progress);
        }
        catch(std::exception& ex)
        {
          const std::string groupNo = Mantid::Kernel::Strings::toString<int>(gIt->first);
          const std::string message = "Error encountered while stitching group " + groupNo + ":\n";
          m_view->giveUserCritical(message + ex.what(), "Error");
          m_view->setProgress(0);
          return;
        }
      }
    }
void frmPriceImport::accept()
{
    if (!validateFilePath())
        return;

    QString delimiter = selectedDelimiter();
    if (delimiter.isEmpty())
        return;

    QFile file(ui->fileTxt->text());
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QString dateFormat = selectedDateFormat();
    historicalPricesMap newPricesMap;
    QTextStream in(&file);
    bool header = ui->columnOrderHeaderChk->isChecked();
    QHash<int, int> columns = selectedColumnOrder();

    int row = -1;
    while (!in.atEnd())
    {
        ++row;
        QStringList line = in.readLine().split(delimiter);

        if (row == 0 && header)
            continue;

        if (!validateRow(row, line, columns, dateFormat))
            return;

        QString symbol = line.at(columns.value(column_Symbol));
        QDate date = QDate::fromString(line.at(columns.value(column_Date)), dateFormat);
        double value = line.at(columns.value(column_Value)).toDouble();

        if (!tradeDateCalendar::isTradeDate(date.toJulianDay()))
            continue;

        QString type = line.at(columns.value(column_Type)).toUpper();
        historicalPrices::type priceType =
            type == "PRICE" ?
                historicalPrices::type_price :
                type == "DIVIDEND" ?
                    historicalPrices::type_dividend :
                    historicalPrices::type_split;

        newPricesMap.getHistoricalPrice(symbol).insert(date.toJulianDay(), value, priceType);
    }

    if (row == -1 || (row == 0 && header) || newPricesMap.isEmpty()) // nothing imported
    {
        QDialog::reject();
        return;
    }

    if (!mergePrices(newPricesMap, true))
        return;

    mergePrices(newPricesMap, false);
    QDialog::accept();
}