/** * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved. * * This file is part of the QCAD project. * * QCAD is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * QCAD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with QCAD. */ /** * \defgroup ecma_file File Tools * \ingroup ecma_scripts * * \brief This module contains ECMAScript implementations of the * tools in the file menu. */ include("scripts/EAction.js"); /** * \class File * \brief Base class for all file menu tools. * \ingroup ecma_file */ function File(guiAction) { EAction.call(this, guiAction); } File.prototype = new EAction(); File.includeBasePath = includeBasePath; File.prototype.beginEvent = function() { EAction.prototype.beginEvent.call(this); if (!isNull(this.getGuiAction()) && this.getGuiAction().objectName==="FileToolsPanelAction") { EAction.showCadToolBarPanel("FileToolsPanel"); this.terminate(); } }; File.getMenu = function() { var menu = EAction.getMenu(File.getTitle(), "FileMenu"); menu.setProperty("scriptFile", File.includeBasePath + "/File.js"); return menu; }; File.getToolBar = function() { // var tb = EAction.getToolBar(File.getTitle(), "FileToolBar"); // return tb; }; File.getCadToolBarPanel = function() { var mtb = EAction.getMainCadToolBarPanel(); var actionName = "FileToolsPanelAction"; if (!isNull(mtb) && mtb.findChild(actionName)==undefined) { var action = new RGuiAction(qsTr("File Tools"), mtb); action.setScriptFile(File.includeBasePath + "/File.js"); action.objectName = actionName; action.setRequiresDocument(false); action.setIcon(File.includeBasePath + "/File.svg"); //action.setStatusTip(qsTr("Show file tools")); action.setNoState(); action.setDefaultCommands(["filemenu"]); action.setGroupSortOrder(10); action.setSortOrder(100); action.setWidgetNames(["!MainToolsPanel"]); } var tb = EAction.getCadToolBarPanel( File.getTitle(), "FileToolsPanel", true ); return tb; }; File.getToolMatrixPanel = function() { RSettings.setValue("File/VisibleInMatrixPanel", false, false); return EAction.getToolMatrixPanel(File.getTitle(), "FileMatrixPanel", 10); }; File.getTitle = function() { return qsTr("项目"); // return qsTr("&File"); }; File.prototype.getTitle = function() { return File.getTitle(); }; File.init = function() { File.getMenu(); File.getToolBar(); File.getCadToolBarPanel(); File.getToolMatrixPanel(); }; /** * \return Suggestion for initial file path for saving the given file or * a file related to the given file. * * \param filePath Typically the path and file name of the drawing file. * \param extension File extension to use for exported file (bmp, pdf, dxf, etc). */ File.getInitialSaveAsPath = function(filePath, extension) { var ret = ""; var fi; if (isNull(filePath) || filePath.length === 0) { var mdiChild = EAction.getMdiChild(); var fn; if (!isNull(mdiChild)) { fn = stripDirtyFlag(EAction.getMdiChild().windowTitle) } else { fn = "File"; } fi = new QFileInfo(QDir.homePath()); ret = fi.absoluteFilePath() + QDir.separator + fn + "." + extension; } else { fi = new QFileInfo(filePath); ret = fi.path() + QDir.separator + fi.completeBaseName() + "." + extension; } return ret; }; /** * Advanced file save as dialog with extension completion. * This is what QFileDialog.getSaveFileName should be. * * \param parentWidget Parent widget or null * \param caption Dialog caption * \param path Initial path with file name of the dialog * \param fileName Initial file name to suggest to user * \param filterStrings Array of filter strings in the format 'My File Type (*.mft *.mftype)' * * \return Array with complete file path and selected name filter or undefined. */ File.getSaveFileName = function(parentWidget, caption, path, filterStrings, defaultNameFilter) { var fileDialog = new QFileDialog(parentWidget); // use native dialog: fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog()); // overwrite is handled after extension has been added: fileDialog.setOption(QFileDialog.DontConfirmOverwrite, true); if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) { fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true); } var fiDir = new QFileInfo(path); fileDialog.setDirectory(fiDir.absolutePath()); fileDialog.fileMode = QFileDialog.AnyFile; fileDialog.acceptMode = QFileDialog.AcceptSave; filterStrings = translateFilterStrings(filterStrings); fileDialog.setNameFilters(filterStrings); // preselect configured name filter: if (isNull(defaultNameFilter) || defaultNameFilter.length===0) { // preselect default name filter DXF R27: for (var i=0; i