Ejemplo n.º 1
0
  static void setText(Index const& idx, std::string const& text, bool forceFormat = false)
  {
    Cell & cell = getCell(idx);

    if (forceFormat)
    {
      std::tie(cell.format, cell.text) = parseFormatAndValue(text);

      int width = getColumnWidth(idx.x);
      if (width < cell.text.size())
        currentDoc().columnWidth_[idx.x] = cell.text.size() + 1;
    }
    else
    {
      uint32_t format;
      std::tie(format, cell.text) = parseFormatAndValue(text);
    }

    if (currentDoc().width_ < (idx.x + 1))
      currentDoc().width_ = idx.x + 1;

    if (currentDoc().height_ < (idx.y + 1))
      currentDoc().height_ = (idx.y + 1);

    if (cell.text.front() == '=')
    {
      cell.hasExpression = true;
      cell.expression = parseExpression(cell.text.substr(1));
    }
    else
    {
      cell.hasExpression = false;
    }
  }
Ejemplo n.º 2
0
  void addColumn(int column)
  {
    column = std::min(column, currentDoc().width_ - 1);

    if (currentDoc().readOnly_)
      return;

    takeUndoSnapshot(EditAction::AddColumn, true);

    currentDoc().width_++;

    std::unordered_map<Index, Cell> newCells;

    for (std::pair<Index, Cell> cell : currentDoc().cells_)
    {
      if (cell.first.x >= column)
        cell.first.x++;

      for (auto & expr : cell.second.expression)
      {
        if (expr.startIndex_.x >= column)
          expr.startIndex_.x++;

        if (expr.endIndex_.x >= column)
          expr.endIndex_.x++;
      }

      newCells.insert(cell);
    }

    currentDoc().cells_ = std::move(newCells);
    evaluateDocument();
  }
Ejemplo n.º 3
0
  static bool exportCSV(std::string const& filename)
  {
    std::ofstream file(filename.c_str());
    if (!file.is_open())
    {
      flashMessage("Could not save document!");
      return false;
    }

    // Write all the cells
    for (int y = 0; y < currentDoc().height_; ++y)
    {
      for (int x = 0; x < currentDoc().width_; ++x)
      {
        Cell const& cell = getCell(Index(x, y));

        file << getText(cell);

        if (x < (currentDoc().width_ - 1))
        {
          file << currentDoc().delimiter_;
        }
        else
        {
          if (y < (currentDoc().height_ - 1))
            file << std::endl;
        }
      }
    }

    return true;
  }
Ejemplo n.º 4
0
void MainWindow::onCurrentChanged(int index)
{
    bool enabled = (index >= 0);
    m_import->setEnabled(enabled);
    m_save->setEnabled(enabled);
    m_saveAs->setEnabled(enabled);

    m_renderingMenu->clear();
    m_renderingMenu->setEnabled(enabled);

    m_modelingToolBar->clear();
    m_modelingToolBar->setVisible(enabled);

    m_viewToolBar->clear();
    m_viewToolBar->setVisible(enabled);

    if (m_pages->currentWidget()) {
        m_renderingMenu->addActions(currentDoc()->view()->renderActions()->actions());
        m_modelingToolBar->addActions(currentDoc()->modelingActions()->actions());
        m_viewToolBar->addActions(currentDoc()->view()->viewActions()->actions());
        setWindowTitle(qApp->applicationName() + " - " + currentDoc()->name());
        m_trees->setCurrentWidget(currentDoc()->tree());
    } else {
        setWindowTitle(qApp->applicationName());
    }
}
Ejemplo n.º 5
0
  void addRow(int row)
  {
    row = std::min(row, currentDoc().height_ - 1);

    if (currentDoc().readOnly_)
      return;

    takeUndoSnapshot(EditAction::AddRow, true);

    currentDoc().height_++;

    std::unordered_map<Index, Cell> newCells;

    for (std::pair<Index, Cell> cell : currentDoc().cells_)
    {
      if (cell.first.y > row)
        cell.first.y++;

      for (auto & expr : cell.second.expression)
      {
        if (expr.startIndex_.y > row)
          expr.startIndex_.y++;

        if (expr.endIndex_.y > row)
          expr.endIndex_.y++;
      }

      newCells.insert(cell);
    }

    currentDoc().cells_ = std::move(newCells);
    evaluateDocument();
  }
Ejemplo n.º 6
0
bool MainWindow::closeAll()
{
    while(currentDoc()) {
        if (!closeDoc(currentDoc()))
            return false;
    }
    return true;
}
Ejemplo n.º 7
0
  void setColumnWidth(int column, int width)
  {
    if (currentDoc().readOnly_)
      return;

    takeUndoSnapshot(EditAction::ColumnWidth, true);
    currentDoc().columnWidth_[column] = std::max(3, width);
  }
Ejemplo n.º 8
0
  std::string getCellText(Index const& idx)
  {
    if (idx.x < 0 || idx.x >= currentDoc().width_ || idx.y < 0 || idx.y >= currentDoc().height_)
      return "";

    Cell const& cell = currentDoc().cells_[idx];
    return getText(cell);
  }
Ejemplo n.º 9
0
  uint32_t getCellFormat(Index const& idx)
  {
    if (idx.x < 0 || idx.x >= currentDoc().width_ || idx.y < 0 || idx.y >= currentDoc().height_)
      return 0;

    Cell const& cell = currentDoc().cells_[idx];
    return cell.format;
  }
Ejemplo n.º 10
0
  int getColumnWidth(int column)
  {
    auto col = currentDoc().columnWidth_.find(column);

    if (col == currentDoc().columnWidth_.end())
      return DEFAULT_COLUMN_WIDTH.toInt();

    return (*col).second;
  }
Ejemplo n.º 11
0
  bool loadRaw(std::string const& data, std::string const& filename, char delimiter)
  {
    if (!loadCSV(data, delimiter))
      return false;

    currentDoc().filename_ = filename;
    currentDoc().readOnly_ = true;

    return true;
  }
Ejemplo n.º 12
0
  void increaseColumnWidth(int column)
  {
    if (currentDoc().readOnly_)
      return;

    int width = getColumnWidth(column);

    takeUndoSnapshot(EditAction::ColumnWidth, true);
    currentDoc().columnWidth_[column] = width + 1;
  }
Ejemplo n.º 13
0
  double getCellValue(Index const& idx)
  {
    if (idx.x < 0 || idx.x >= currentDoc().width_ || idx.y < 0 || idx.y >= currentDoc().height_)
      return 0.0;

    Cell & cell = currentDoc().cells_[idx];
    if (!cell.evaluated)
      evaluateCell(cell);

    return cell.value;
  }
Ejemplo n.º 14
0
  void createDefaultEmpty()
  {
    documentBuffers().push_back({});
    jumpToBuffer(documentBuffers().size() - 1);

    currentDoc().width_ = 0;
    currentDoc().height_ = 0;
    currentDoc().filename_ = "[No Name]";
    currentDoc().delimiter_ = ',';
    currentDoc().readOnly_ = false;
  }
Ejemplo n.º 15
0
  void setCellFormat(Index const& idx, uint32_t format)
  {
    if (idx.x < 0 || idx.x >= currentDoc().width_ || idx.y < 0 || idx.y >= currentDoc().height_)
      return;

    if (currentDoc().readOnly_)
      return;

    takeUndoSnapshot(EditAction::CellText, false);

    Cell & cell = currentDoc().cells_[idx];
    cell.format = format;
  }
Ejemplo n.º 16
0
  bool undo()
  {
    if (!currentBuffer().undoStack_.empty())
    {
      currentBuffer().redoStack_.emplace_back(currentDoc(), cursorPos(), EditAction::UndoRedo);

      UndoState const& state = currentBuffer().undoStack_.back();
      currentDoc() = state.doc_;
      cursorPos() = state.cursor_;

      currentBuffer().undoStack_.pop_back();
      return true;
    }

    return false;
  }
Ejemplo n.º 17
0
void MainWindow::import()
{
    QString file =
            QFileDialog::getOpenFileName(this, "Import", qgetenv("CASROOT") + "/data",
                                         "All (*);;BREP (*.brep *.rle);;IGES (*.igs *.iges);;STEP (*.stp *.step);;"
                                         "CSFDB (*.csfdb);;VRML (*.vrml);;STL (*.stl)");

    if(!file.isEmpty()) currentDoc()->import(file);
}
Ejemplo n.º 18
0
  void setCellText(Index const& idx, std::string const& text)
  {
    if (currentDoc().readOnly_)
      return;

    takeUndoSnapshot(EditAction::CellText, false);
    setText(idx, text);
    evaluateDocument();
  }
Ejemplo n.º 19
0
  bool load(std::string const& filename)
  {
    std::ifstream file(filename.c_str());
    if (!file.is_open())
    {
      logError("Could not open document '", filename, "'");
      flashMessage("Could not open document!");
      return false;
    }

    std::string data = "";
    std::string line = "";
    while (std::getline(file, line))
      data += line + "\n";

    if (data.size() == 0)
    {
      logError("No data in file '", filename, "'");
      return false;
    }

    // Determin if we are reading a zum file, of a csv type of file.
    if (data.size() > 5 && data[0] == 'Z' && data[1] == 'U' && data[2] == 'M' && data[3] == '1' && data[4] == '\n')
    {
      if (!loadZum1(data))
      {
        logError("Could not parse document '", filename, "'");
        return false;
      }
    }
    else
    {
      if (!loadCSV(data, 0))
      {
        logError("Could not parse document '", filename, "'");
        return false;
      }
    }

    currentDoc().filename_ = filename;
    currentDoc().readOnly_ = false;

    return true;
  }
Ejemplo n.º 20
0
  std::string getCellDisplayText(Index const& idx)
  {
    if (!hasCell(idx))
      return "";

    Cell const& cell = currentDoc().cells_[idx];

    if (cell.display.empty())
      return getText(cell);
    return cell.display;
  }
Ejemplo n.º 21
0
  void removeRow(int row)
  {
    if (row < 0 || row >= getRowCount())
      return;

    if (currentDoc().readOnly_)
      return;

    takeUndoSnapshot(EditAction::RemoveRow, false);

    currentDoc().height_--;

    std::unordered_map<Index, Cell> newCells;

    for (std::pair<Index, Cell> cell : currentDoc().cells_)
    {
      if (cell.first.y != row)
      {
        if (cell.first.y > row)
          cell.first.y--;

      for (auto & expr : cell.second.expression)
      {
        if (expr.startIndex_.y > row)
          expr.startIndex_.y--;

        if (expr.endIndex_.y > row)
          expr.endIndex_.y--;
      }

        newCells.insert(cell);
      }
    }

    currentDoc().cells_ = std::move(newCells);
    evaluateDocument();
  }
Ejemplo n.º 22
0
bool MainWindow::saveDocumentAs(Document* doc)
{
    QString file =
            QFileDialog::getSaveFileName(this, "Save As", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
                                         "All (*);;XmlOcaf (*.xml);;MDTV-Standard (*.std);;BinOcaf (*.cbf)");

    if(!file.isEmpty() && doc->saveAs(file)) {
        int index = documentIndex(doc);
        m_pages->setTabToolTip(index, doc->path());
        m_pages->setTabText(index, uniqueTitle(doc->name()));
        setWindowTitle(qApp->applicationName() + " - " + currentDoc()->name());
        return true;
    }

    return false;
}
Ejemplo n.º 23
0
  static void takeUndoSnapshot(EditAction action, bool canMerge)
  {
    bool createNew = true;

    if (!currentBuffer().undoStack_.empty())
    {
      UndoState & state = currentBuffer().undoStack_.back();

      if (state.action_ == action && (canMerge || forceUndoMerge_))
        createNew = false;
    }

    if (createNew)
    {
      currentBuffer().undoStack_.emplace_back(currentDoc(), cursorPos(), action);
      currentBuffer().redoStack_.clear();
    }
  }
Ejemplo n.º 24
0
Document* MainWindow::openDoc(const QString& file)
{
    Document* doc = m_app->openDoc(file);
    if (doc) {
        if (setCurrentDoc(doc)) {
            statusBar()->showMessage(doc->name() + " already loaded", 2000);
            return doc;
        } else {
            Document* prevDoc = currentDoc();
            addDocument(doc);
            doc->view()->fitAll();
            statusBar()->showMessage(doc->name() + " loaded", 2000);

            if(prevDoc && !prevDoc->ocafDoc()->IsSaved() && !prevDoc->ocafDoc()->IsModified())
                closeDoc(prevDoc);
        }
    }
    return doc;
}
Ejemplo n.º 25
0
  void removeColumn(int column)
  {
    if (currentDoc().readOnly_)
      return;

    takeUndoSnapshot(EditAction::RemoveColumn, false);

    currentDoc().width_--;

    std::unordered_map<int, int> newColumnWidth;
    std::unordered_map<Index, Cell> newCells;

    // Remove and update column info
    for (std::pair<int, int> col : currentDoc().columnWidth_)
    {
      if (col.first != column)
      {
        if (col.first > column)
          col.first--;

        newColumnWidth.insert(col);
      }
    }

    // Remove and update cells
    for (std::pair<Index, Cell> cell : currentDoc().cells_)
    {
      if (cell.first.x != column)
      {
        if (cell.first.x > column)
          cell.first.x--;

        for (auto & expr : cell.second.expression)
        {
          if (expr.startIndex_.x > column)
            expr.startIndex_.x--;

          if (expr.endIndex_.x > column)
            expr.endIndex_.x--;
        }

        newCells.insert(cell);
      }
    }

    currentDoc().cells_ = std::move(newCells);
    currentDoc().columnWidth_ = std::move(newColumnWidth);
    evaluateDocument();
  }
Ejemplo n.º 26
0
  void evaluateDocument()
  {
    Document & doc = currentDoc();

    for (auto & it : doc.cells_)
    {
      const Index idx = it.first;
      Cell & cell = it.second;

      cell.value = 0.0;

      if (cell.hasExpression)
      {
        if (cell.expression.empty())
        {
          cell.display = "#ERROR";
          cell.evaluated = true;
        }
        else
        {
          cell.evaluated = false;
          cell.display = "";
        }
      }
      else
      {
        cell.display = cell.text;
        cell.evaluated = true;

        try {
          cell.value = std::stod(cell.text);
        } catch (std::exception) {
        }
      }
    }

    for (auto & it : doc.cells_)
      evaluateCell(it.second);
  }
Ejemplo n.º 27
0
void MainWindow::saveAs()
{
    saveDocumentAs(currentDoc());
}
Ejemplo n.º 28
0
void MainWindow::save()
{
    saveDocument(currentDoc());
}
Ejemplo n.º 29
0
 int getRowCount()
 {
   return currentDoc().height_;
 }
Ejemplo n.º 30
0
 int getColumnCount()
 {
   return currentDoc().width_;
 }