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; } }
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(); }
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; }
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()); } }
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(); }
bool MainWindow::closeAll() { while(currentDoc()) { if (!closeDoc(currentDoc())) return false; } return true; }
void setColumnWidth(int column, int width) { if (currentDoc().readOnly_) return; takeUndoSnapshot(EditAction::ColumnWidth, true); currentDoc().columnWidth_[column] = std::max(3, width); }
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); }
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; }
int getColumnWidth(int column) { auto col = currentDoc().columnWidth_.find(column); if (col == currentDoc().columnWidth_.end()) return DEFAULT_COLUMN_WIDTH.toInt(); return (*col).second; }
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; }
void increaseColumnWidth(int column) { if (currentDoc().readOnly_) return; int width = getColumnWidth(column); takeUndoSnapshot(EditAction::ColumnWidth, true); currentDoc().columnWidth_[column] = width + 1; }
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; }
void createDefaultEmpty() { documentBuffers().push_back({}); jumpToBuffer(documentBuffers().size() - 1); currentDoc().width_ = 0; currentDoc().height_ = 0; currentDoc().filename_ = "[No Name]"; currentDoc().delimiter_ = ','; currentDoc().readOnly_ = false; }
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; }
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; }
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); }
void setCellText(Index const& idx, std::string const& text) { if (currentDoc().readOnly_) return; takeUndoSnapshot(EditAction::CellText, false); setText(idx, text); evaluateDocument(); }
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; }
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; }
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(); }
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; }
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(); } }
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; }
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(); }
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); }
void MainWindow::saveAs() { saveDocumentAs(currentDoc()); }
void MainWindow::save() { saveDocument(currentDoc()); }
int getRowCount() { return currentDoc().height_; }
int getColumnCount() { return currentDoc().width_; }