Interesting problem. The idea with intercepting the guiAction is good but fails because you cannot force your slot to be triggered before the original slot. Slots are always triggered in the same order as signals were connected to them.
The best solution I can come up with is to do a proper inheritance and extend the existing Save class. The original guiAction can simply be removed from the menu and tool bar to avoid confusion and force the user to use our own implementation.
Here's a brief example class that inherits and extends the Save class. I called it ExBeforeSave and put it into scripts/Examples/IOExamples/ExBeforeSave, but it can be in any other location inside the scripts directory:
- Code: Select all
include("../../../File/Save/Save.js");
function ExBeforeSave(guiAction) {
Save.call(this, guiAction);
}
ExBeforeSave.prototype = new Save();
ExBeforeSave.prototype.beginEvent = function() {
// Do something here before saving...
// If file name returned by this.getDocument().getFileName()
// is empty at this point, the file has not been saved yet
// and the user will be forwarded to 'save as' automatically.
// Calling beginEvent of the original save class which does all the work:
Save.prototype.beginEvent.call(this);
this.terminate();
}
ExBeforeSave.init = function(basePath) {
// Remove the original save action from the file menu and the file tool bar:
var saveAction = RGuiAction.getByScriptFile("scripts/File/Save/Save.js");
File.getMenu().removeAction(saveAction);
File.getToolBar().removeAction(saveAction);
// Create our own save action instead:
var action = new RGuiAction(qsTranslate("Save", "&Save"), RMainWindowQt.getMainWindow());
action.setRequiresDocument(true);
action.setScriptFile(basePath + "/ExBeforeSave.js");
action.setIcon("scripts/File/Save/Save.svg");
action.setDefaultShortcut(new QKeySequence(QKeySequence.Save));
action.setDefaultCommands(["save"]);
action.setSortOrder(1000);
action.setNoState();
EAction.addGuiActionTo(action, File, true, true, false, false);
};
For the 'SaveAs' class, the same idea can be applied.