MCPcopy Create free account
hub / github.com/MiniZinc/MiniZincIDE / saveFile

Method saveFile

MiniZincIDE/mainwindow.cpp:1411–1477  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1409}
1410
1411void MainWindow::saveFile(CodeEditor* ce, const QString& f)
1412{
1413 QString filepath = f;
1414 int tabIndex = ui->tabWidget->indexOf(ce);
1415 if (filepath=="") {
1416 if (ce != curEditor) {
1417 ui->tabWidget->setCurrentIndex(tabIndex);
1418 }
1419 QString dialogPath = ce->filepath.isEmpty() ? getLastPath()+"/"+ce->filename: ce->filepath;
1420 QString selectedFilter = "Other (*)";
1421 if (dialogPath.endsWith(".mzn") || (ce->filepath.isEmpty() && ce->filename=="Playground"))
1422 selectedFilter = "MiniZinc model (*.mzn)";
1423 else if (dialogPath.endsWith(".dzn") || dialogPath.endsWith(".json"))
1424 selectedFilter = "MiniZinc data (*.dzn *.json)";
1425 else if (dialogPath.endsWith(".fzn"))
1426 selectedFilter = "FlatZinc (*.fzn)";
1427 else if (dialogPath.endsWith(".mzc"))
1428 selectedFilter = "MiniZinc solution checker (*.mzc)";
1429 filepath = QFileDialog::getSaveFileName(this,"Save file",dialogPath,"MiniZinc model (*.mzn);;MiniZinc data (*.dzn *.json);;MiniZinc solution checker (*.mzc);;FlatZinc (*.fzn);;Other (*)",&selectedFilter);
1430 if (!filepath.isNull()) {
1431 setLastPath(QFileInfo(filepath).absolutePath()+fileDialogSuffix);
1432 }
1433 }
1434 if (!filepath.isEmpty()) {
1435 if (filepath != ce->filepath && IDE::instance()->hasFile(filepath)) {
1436 QMessageBox::warning(this,"MiniZinc IDE","Cannot overwrite open file.",
1437 QMessageBox::Ok);
1438
1439 } else {
1440 IDE::instance()->fsWatch.removePath(filepath);
1441 QFile file(filepath);
1442 if (file.open(QFile::WriteOnly | QFile::Text)) {
1443 QTextStream out(&file);
1444#if QT_VERSION < 0x060000
1445 out.setCodec(QTextCodec::codecForName("UTF-8"));
1446#endif
1447 auto contents = ce->document()->toPlainText();
1448 out << contents;
1449 file.close();
1450 if (filepath != ce->filepath) {
1451 QTextDocument* newdoc =
1452 IDE::instance()->addDocument(filepath,ce->document(),ce);
1453 ce->setDocument(newdoc);
1454 if (ce->filepath != "") {
1455 IDE::instance()->removeEditor(ce->filepath,ce);
1456 }
1457 getProject().remove(ce->filepath);
1458 getProject().add(filepath);
1459 ce->filepath = filepath;
1460 }
1461 ce->document()->setModified(false);
1462 ce->filename = QFileInfo(filepath).fileName();
1463 ui->tabWidget->setTabText(tabIndex,ce->filename);
1464 updateRecentFiles(filepath);
1465 if (ce==curEditor)
1466 tabChange(tabIndex);
1467 auto* history = getProject().history();
1468 if (history != nullptr) {

Callers

nothing calls this directly

Calls 10

setCurrentIndexMethod · 0.80
hasFileMethod · 0.80
addDocumentMethod · 0.80
setDocumentMethod · 0.80
removeEditorMethod · 0.80
removeMethod · 0.80
addMethod · 0.80
setModifiedMethod · 0.80
historyMethod · 0.80
updateFileContentsMethod · 0.80

Tested by

no test coverage detected