| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307 |
- /**
- * 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.
- */
- include("scripts/library.js");
- include("scripts/WidgetFactory.js");
- include("scripts/Widgets/OptionsToolBar/OptionsToolBar.js");
- /**
- * Base class for all ECMAScript based actions.
- */
- function EAction(guiAction) {
- RActionAdapter.call(this);
- this.guiAction = guiAction;
- if (!isNull(guiAction)) {
- this.setGuiAction(guiAction);
- }
- // Constructor is called without arguments to create prototype
- // for derived class. At this point, includeBasePath is set correctly:
- if (isNull(guiAction)) {
- this.includeBasePath = includeBasePath;
- }
- this.delegate = undefined;
- this.state = undefined;
- this.settingsGroup = undefined;
- this.uiFile = undefined;
- this.splitUiOptions = false;
- this.additionalOptionsToolBars = undefined;
- this.optionsToolBarEnabled = false;
- this.relativeZeroPos = undefined;
- // set to true to prefer dialog over options tool bar:
- this.useDialog = false;
- this.dialogUiFile = undefined;
- this.waitingForContextMenu = false;
- this.optOutRelativeZeroResume = false;
- this.resuming = false;
- }
- EAction.prototype = new RActionAdapter();
- EAction.includeBasePath = includeBasePath;
- // some commonly used translated strings:
- EAction.trBack = qsTr("Back");
- EAction.trCancel = qsTr("Cancel");
- EAction.trDone = qsTr("Done");
- EAction.crossCursor = undefined;
- EAction.noRelativeZeroResume = false;
- /**
- * Called when the user starts this action by clicking a button, choosing a
- * menu, entering a command, etc.
- *
- * Shows the GUI elements that refer to this action as checked. Displays GUI
- * options if this.uiFile is set to a valid UI file. Displays tool help in Help
- * Viewer if available.
- */
- EAction.prototype.beginEvent = function() {
- if (this.delegate) {
- this.delegate.beginEvent();
- }
- if (this.hasNoState()) {
- return;
- }
- var appWin = EAction.getMainWindow();
- if (!isNull(this.getGuiAction())) {
- this.getGuiAction().setChecked(true);
- }
- this.showUiOptions(false);
- // reset snap to auto snap / no restriction:
- if (isNull(this.getGuiAction())
- || (this.getGuiAction().getGroup() !== "snaps" &&
- this.getGuiAction().getGroup() !== "snaprestrictions" &&
- this.getGuiAction().property("NoSnapReset")!==true)) {
- var di = this.getDocumentInterface();
- if (!isNull(di)) {
- if (!di.isSnapLocked()) {
- var guiAction = RGuiAction.getByScriptFile("scripts/Snap/SnapAuto/SnapAuto.js");
- if (!isNull(guiAction)) {
- guiAction.slotTrigger();
- }
- di.setSnap(new RSnapAuto());
- guiAction = RGuiAction.getByScriptFile("scripts/Snap/RestrictOff/RestrictOff.js");
- if (!isNull(guiAction)) {
- guiAction.slotTrigger();
- }
- di.setSnapRestriction(new RRestrictOff(di));
- }
- }
- }
- };
- /**
- * Called when this action is terminated.
- *
- * Shows the GUI elements that refer to this action as un-checked. Hides GUI
- * options if this.uiFile is set to a valid UI file. Clears up mouse tips, etc.
- */
- EAction.prototype.finishEvent = function() {
- if (this.hasNoState()) {
- return;
- }
- var guiAction = this.getGuiAction();
- // unckeck tool GUI action:
- if (!isNull(guiAction) && !isDeleted(guiAction)) {
- if (guiAction.getGroup().isEmpty() || guiAction.isOverride()) {
- guiAction.setChecked(false);
- }
- }
- this.hideUiOptions();
- // clear mouse tips:
- if (!isNull(EAction.getMainWindow())) {
- this.setLeftMouseTip("");
- this.setRightMouseTip("");
- }
- // reset CAD tool bar:
- if (!isNull(guiAction) && guiAction.getGroup() === "") {
- var appWin = EAction.getMainWindow();
- var cadToolBar = appWin.findChild("CadToolBar");
- if (!isNull(cadToolBar) && cadToolBar.getCurrentPanelName()==="SnapToolsPanel") {
- cadToolBar.back();
- }
- }
- // to remove snap info:
- var di = this.getDocumentInterface();
- if (!isNull(di)) {
- di.repaintViews();
- }
- this.setCommandPrompt();
- };
- /**
- * Does nothing. Needs to be implemented here to avoid recursion when child
- * classes call the base class implementation.
- */
- EAction.prototype.mousePressEvent = function(event) {
- };
- EAction.prototype.mouseReleaseEvent = function(event) {
- if (event.button() === Qt.RightButton) {
- this.escapeEvent();
- }
- };
- EAction.prototype.mouseDoubleClickEvent = function(event) {
- if (event.button() == Qt.RightButton) {
- // right double-click to reset:
- if (RSettings.getBoolValue("GraphicsView/RightDoubleClickToReset", false)) {
- var di = this.getDocumentInterface();
- if (!isNull(di)) {
- di.killAllActions();
- }
- EAction.showMainTools();
- }
- }
- };
- /**
- * Sets the UI options toolbar content for this tool. By default, a tool shows
- * the options toolbar defined in "DefaultOptions.ui".
- *
- * \param uiFile A path to a single UI file or an array of UI file paths.
- * \param split If true, the options are split up into multiple toolbars.
- */
- EAction.prototype.setUiOptions = function(uiFile, split) {
- if (isNull(split)) {
- split = false;
- }
- this.splitUiOptions = split;
- if (isNull(uiFile)) {
- this.uiFile = undefined;
- return;
- }
- if (isArray(uiFile)) {
- this.uiFile = uiFile;
- } else {
- this.uiFile = [ uiFile ];
- }
- };
- /**
- * Set the state of this action. Action implementations overwrite this function
- * to initialize this action for the given state. This includes setting the
- * appropriate click mode, displaying the appropriate CAD toolbar, setting
- * mouse tips, mouse cursor, etc.
- */
- EAction.prototype.setState = function(state) {
- if (!isNumber(state)) {
- qWarning("EAction.prototype.setState: invalid state");
- debugger;
- return;
- }
- this.state = state;
- this.initState();
- if (state!==-1) {
- this.simulateMouseMoveEvent();
- }
- };
- /**
- * Called to initialize a new state of this action. Overwrite to
- * set mouse tips, show desired toolbars, etc.
- */
- EAction.prototype.initState = function() {
- };
- /**
- * Called whenever the action resumes its operation, for example after it was
- * temporary suspended for another action.
- */
- EAction.prototype.resumeEvent = function() {
- if (this.hasNoState()) {
- return;
- }
- var appWin = EAction.getMainWindow();
- if (!isNull(this.getGuiAction())) {
- this.getGuiAction().setChecked(true);
- }
- this.showUiOptions(true);
- if (!isNull(this.state)) {
- this.setState(this.state);
- }
- if (!this.optOutRelativeZeroResume) {
- // restore relative zero position when returning from another command:
- var di = this.getDocumentInterface();
- if (!isNull(di) && isValidVector(this.relativeZeroPos)) {
- if (EAction.noRelativeZeroResume===true) {
- EAction.noRelativeZeroResume = false;
- }
- else {
- di.setRelativeZero(this.relativeZeroPos);
- }
- }
- this.relativeZeroPos = undefined;
- }
- };
- /**
- * Called whenever the action is suspended, for example if another action is
- * started while this action is active.
- */
- EAction.prototype.suspendEvent = function() {
- if (this.hasNoState()) {
- return;
- }
- if (!isNull(this.getGuiAction())) {
- this.getGuiAction().setChecked(false);
- }
- this.hideUiOptions();
- // store relative zero position:
- var di = this.getDocumentInterface();
- if (!isNull(di)) {
- this.relativeZeroPos = di.getRelativeZero();
- }
- };
- /**
- * Shows the UI options of this action if this.uiFile is set. UI options are
- * typically shown in a horizontal toolbar at the top of the drawing area.
- *
- * Usually this function does not need to be called directly by implementing
- * actions.
- *
- * \param resume Action was resumed from suspension.
- * \param restoreFromSettings Restore settings from document / config file (default).
- */
- EAction.prototype.showUiOptions = function(resume, restoreFromSettings) {
- if (isNull(restoreFromSettings)) {
- restoreFromSettings = true;
- }
- if (!isNull(this.optionWidgetActions)) {
- return;
- }
- if (this.optionsToolBarEnabled === false) {
- return;
- }
- var optionsToolBar = EAction.getOptionsToolBar();
- if (isNull(optionsToolBar)) {
- qWarning("EAction.prototype.showUiOptions: no options toolbar");
- return;
- }
- this.optionWidgetActions = [];
- var guiAction = this.getGuiAction();
- if (!isNull(guiAction) &&
- guiAction.getGroup() !== "snaps" &&
- guiAction.getGroup() !== "snaprestrictions") {
- // automatically add icon of current action to toolbar:
- if (!guiAction.icon.isNull() && !guiAction.isOverride()) {
- var iconLabel = optionsToolBar.findChild("Icon");
- if (!isNull(iconLabel)) {
- iconLabel.toolTip = qsTr("Active Tool:") + " " + guiAction.text.replace("&", "");
- OptionsToolBar.setIcon(guiAction.icon);
- iconLabel.setFixedWidth(iconLabel.height + 12);
- }
- }
- }
- // add widgets from action specific options toolbar:
- if (isNull(this.uiFile)) {
- return;
- }
- this.resuming = true;
- this.optionWidgetActions = [];
- for (var i = 0; i < this.uiFile.length; ++i) {
- var uiFile = this.uiFile[i];
- var wOptions = this.createWidget(uiFile);
- //qDebug("adjust icons for uiFile: ", uiFile);
- if (isNull(wOptions)) {
- qWarning("Cannot create widget from UI file: ", uiFile);
- continue;
- }
- // create new toolbar for additional tool options:
- if (this.splitUiOptions === true && i != 0) {
- EAction.getMainWindow().addToolBarBreak();
- optionsToolBar = new QToolBar(EAction.getMainWindow());
- optionsToolBar.objectName = wOptions.objectName;
- EAction.getMainWindow().addToolBar(Qt.TopToolBarArea, optionsToolBar);
- if (isNull(this.additionalOptionsToolBars)) {
- this.additionalOptionsToolBars = [];
- }
- this.additionalOptionsToolBars.push(optionsToolBar);
- }
- // show:
- this.settingsGroup = wOptions.objectName;
- this.optionWidgetActions = this.optionWidgetActions
- .concat(WidgetFactory.moveChildren(wOptions, optionsToolBar,
- this.settingsGroup));
- // required for undocked options tool bar:
- if (optionsToolBar.floating) {
- optionsToolBar.resize(optionsToolBar.sizeHint.width(), optionsToolBar.height);
- }
- destr(wOptions);
- // automatically add separator to toolbar:
- var a = optionsToolBar.addSeparator();
- a.objectName = "LastSeparator" + i;
- this.optionWidgetActions.push(a);
- // give action a chance to initialize toolbar widgets that cannot
- // be initialized in Qt Designer:
- this.initUiOptions(resume, optionsToolBar);
- // restore previously stored state:
- if (restoreFromSettings===true) {
- WidgetFactory.restoreState(optionsToolBar, this.settingsGroup, this);
- }
- }
- // hide options tool bar widgets which are shown in a dialog instead:
- this.hideOptionsToolBarWidgets();
- this.resuming = false;
- };
- /**
- * Overwrite to initialize combo boxes and other UI elements of the options tool
- * bar.
- */
- EAction.prototype.initUiOptions = function(resume, optionsToolBar) {
- var prefixChar = RSettings.getStringValue("ToolBar/PrefixChar", ",");
- if (isNull(optionsToolBar)) {
- return;
- }
- var children = optionsToolBar.children();
- for (var i=0; i<children.length; i++) {
- var child = children[i];
- if (isNull(child)) {
- continue;
- }
- var shortCut = child.shortcut;
- if (!isNull(shortCut) && !shortCut.isEmpty()) {
- var str = shortCut.toString();
- var firstChar = str[0];
- // if first key is Ctrl then the string starts with Ctrl+
- // so check for a + sign, which indicates a modifier key has been used
- // '+' and '-' should not be used as prefix, as they zoom in and out
- if (str.indexOf('+') === -1) { // no plus sign
- if (firstChar >= '!' && firstChar <= '~') { // isgraph()
- str = str.replace(firstChar, prefixChar);
- child.shortcut = new QKeySequence(str);
- child.toolTip = RGuiAction.formatToolTip(child.toolTip, str);
- }
- }
- }
- }
- };
- /**
- * Hides the UI options of this action.
- *
- * Usually this function does not need to be called directly by implementing
- * actions.
- * \param saveToSettings if true, the state is saved to settings
- */
- EAction.prototype.hideUiOptions = function(saveToSettings) {
- if (isNull(saveToSettings)) {
- saveToSettings = true;
- }
- if (isNull(this.optionWidgetActions)) {
- return;
- }
- if (this.optionsToolBarEnabled === false) {
- return;
- }
- var optionsToolBar = EAction.getOptionsToolBar();
- if (isNull(optionsToolBar)) {
- return;
- }
- if (saveToSettings) {
- WidgetFactory.saveState(optionsToolBar, this.settingsGroup);
- }
- // automatically reset icon of current action from toolbar:
- if (!isNull(this.getGuiAction()) &&
- this.getGuiAction().getGroup() !== "snaps" &&
- this.getGuiAction().getGroup() !== "snaprestrictions") {
- if (!this.getGuiAction().icon.isNull()) {
- var iconLabel = optionsToolBar.findChild("Icon");
- if (!isNull(iconLabel)) {
- OptionsToolBar.setIcon("scripts/Reset/Reset.svg");
- iconLabel.toolTip = qsTr("Active Tool:") + " " + qsTr("None");
- }
- }
- }
- // delete all option widgets / actions of this tool:
- var i;
- while (this.optionWidgetActions.length>0) {
- var a = this.optionWidgetActions.pop();
- if (!isNull(optionsToolBar)) {
- optionsToolBar.removeAction(a);
- }
- destr(a);
- }
- // delete additional toolbars of this tool if available:
- if (!isNull(this.additionalOptionsToolBars)) {
- for (i = 0; i < this.additionalOptionsToolBars.length; ++i) {
- var tb = this.additionalOptionsToolBars[i];
- if (isDeleted(tb)) {
- continue;
- }
- destr(tb);
- }
- }
- // required for undocked options tool bar:
- if (optionsToolBar.floating) {
- optionsToolBar.resize(optionsToolBar.sizeHint.width(), optionsToolBar.height);
- }
- this.optionWidgetActions = undefined;
- this.additionalOptionsToolBars = undefined;
- };
- /**
- * Hides all widgets in the options tool bar which are shown in the tool dialog instead.
- */
- EAction.prototype.hideOptionsToolBarWidgets = function(widgets, noSyncWidgets) {
- if (!this.useDialog) {
- return;
- }
- if (isNull(widgets)) {
- widgets = [];
- }
- if (isNull(noSyncWidgets)) {
- noSyncWidgets = [];
- }
- // collect widgets which are tagged to be shown in the dialog:
- var i, c;
- //var widgets = [];
- //var noSyncWidgets = [];
- var optionsToolBar = EAction.getOptionsToolBar();
- var children = optionsToolBar.children();
- for (i = 0; i < children.length; ++i) {
- c = children[i];
- if (isNull(c)) {
- continue;
- }
- if (c["HideInDialogMode"]===true) {
- destr(c);
- }
- else if (c["MoveToDialog"]===true) {
- widgets.push(c);
- }
- else {
- if (isQWidget(c)) {
- noSyncWidgets.push(c);
- }
- }
- }
- if (widgets.length===0) {
- // no widgets to show in dialog, return to normal operation without dialog:
- return;
- }
- WidgetFactory.saveState(optionsToolBar, this.settingsGroup);
- for (i = 0; i < widgets.length; i++) {
- var a = optionsToolBar.findChild(widgets[i].objectName + "Action");
- if (!isNull(a)) {
- a.visible = false;
- }
- }
- // remove double separators:
- OptionsToolBar.normalizeSeparators(this);
- };
- /**
- * Called when user presses enter. Default implementation calls showDialog if configured.
- */
- EAction.prototype.enterEvent = function() {
- if (RSettings.getBoolValue("Transform/EnterShowsDialog", false)===true) {
- this.showDialog();
- }
- };
- /**
- * Show dialog to enter some or all of the options.
- * The default implementation creates a dialog based on this.dialogUiFile.
- * Widgets in the options tool bar which have the property "MoveToDialog" or
- * "HideInDialogMode" set are hidden in the options tool bar.
- */
- EAction.prototype.showDialog = function() {
- if (!this.useDialog) {
- return QDialog.Rejected;
- }
- var di = this.getDocumentInterface();
- if (!isNull(di)) {
- di.clearPreview();
- di.repaintViews();
- }
- var widgets = [];
- var noSyncWidgets = [];
- this.hideOptionsToolBarWidgets(widgets, noSyncWidgets);
- // show dialog and move tool bar widgets to its layout:
- var formLayout = undefined;
- if (isNull(this.dialogUiFile)) {
- return QDialog.Rejected;
- }
- this.dialog = WidgetFactory.createDialog(this.includeBasePath, this.dialogUiFile, EAction.getMainWindow());
- this.initUiOptions(false, this.dialog);
- WidgetFactory.restoreState(this.dialog, this.settingsGroup, this);
- var title = this.getToolTitle();
- if (title.indexOf("\t")!==-1) {
- // remove shortcuts Windows:
- title = title.replace(/\t.*/g, "");
- }
- this.dialog.windowTitle = title;
- this.dialog.windowIcon = new QIcon();
- // give focus to control with custom property 'DefaultFocus':
- var children = this.dialog.children();
- var i;
- for (i=0; i<children.length; i++) {
- if (children[i].property("DefaultFocus")===true) {
- children[i].setFocus();
- if (isFunction(children[i].selectAll)) {
- // select all text in line edits:
- children[i].selectAll();
- }
- break;
- }
- }
- this.initDialog(this.dialog);
- var ret = this.dialog.exec();
- // TODO:
- //WidgetFactory.saveState(this.dialog, "Shape");
- WidgetFactory.saveState(this.dialog, this.settingsGroup);
- // sync invisible part of options tool bar with dialog:
- for (i=0; i<noSyncWidgets.length; i++) {
- if (isDeleted(noSyncWidgets[i])) {
- continue;
- }
- noSyncWidgets[i].setProperty("Loaded", true);
- }
- var optionsToolBar = EAction.getOptionsToolBar();
- WidgetFactory.restoreState(optionsToolBar, this.settingsGroup, this);
- for (i=0; i<noSyncWidgets.length; i++) {
- if (isDeleted(noSyncWidgets[i])) {
- continue;
- }
- noSyncWidgets[i].setProperty("Loaded", false);
- }
- destr(this.dialog);
- this.dialog = undefined;
- // var view = EAction.getGraphicsView();
- // if (!isNull(view)) {
- // // view loses focus because of dialog:
- // view.giveFocus();
- // }
- EAction.activateMainWindow();
- this.simulateMouseMoveEvent();
- //this.updatePreview(true);
- return ret;
- };
- /**
- * Called before dialog is shown. Default implementation does nothing.
- */
- EAction.prototype.initDialog = function(dialog) {
- };
- /**
- * Called if the user clicks the reset button of an action. A reset button may
- * be displayed as part of the UI options of this action and must have the
- * object name "Reset".
- */
- EAction.prototype.slotReset = function() {
- if (this.optionsToolBarEnabled === false) {
- return;
- }
- var optionsToolBar = EAction.getOptionsToolBar();
- if (isNull(optionsToolBar)) {
- return;
- }
- WidgetFactory.resetState(optionsToolBar, this.settingsGroup);
- };
- /**
- * Called when the user presses Escape or clicks the right mouse button.
- */
- EAction.prototype.escapeEvent = function() {
- this.setArrowCursor();
- this.getDocumentInterface().clearPreview();
- this.getDocumentInterface().repaintViews();
- this.terminate();
- };
- /**
- * Called when the user presses a key. By default, the event is ignored.
- */
- EAction.prototype.keyPressEvent = function(event) {
- if (isNull(event)) {
- return;
- }
- // prevent focus shift when entering keycode (e.g. "D2"):
- var appWin = EAction.getMainWindow();
- if (appWin.getKeyLog().length===0) {
- if ((event.key() >= Qt.Key_0.valueOf() && event.key() <= Qt.Key_9.valueOf()) || event.key() === Qt.Key_Period.valueOf()) {
- // number or dot entered:
- // give focus to first input widget in options tool bar and set text to number entered:
- var w = OptionsToolBar.getFirstInputWidget();
- if (!isNull(w)) {
- w.setFocus(Qt.OtherFocusReason);
- w.text = String.fromCharCode(event.key());
- event.accept();
- return;
- }
- }
- }
- event.ignore();
- };
- /**
- * Called when the user releases a key. By default, the event is ignored.
- */
- EAction.prototype.keyReleaseEvent = function(event) {
- event.ignore();
- };
- /**
- * Sets the current cursor of the current document interface (all views attached
- * to it) to crosshair cursor.
- */
- EAction.prototype.setCrosshairCursor = function() {
- if (isNull(EAction.crossCursor)) {
- if (RSettings.getBoolValue("GraphicsView/SystemCursors", false)===true) {
- EAction.crossCursor = new QCursor(Qt.CrossCursor);
- }
- else {
- var bitmap, mask
- if (RSettings.getDevicePixelRatio()===2 && RS.getSystemId()!=="osx") {
- bitmap = new QBitmap(EAction.includeBasePath + "/CrosshairCursor@2x.png", "PNG");
- mask = new QBitmap(EAction.includeBasePath + "/CrosshairCursorMask@2x.png", "PNG");
- //bitmap.setDevicePixelRatio(2);
- //mask.setDevicePixelRatio(2);
- EAction.crossCursor = new QCursor(bitmap, mask, 30, 30);
- }
- else {
- bitmap = new QBitmap(EAction.includeBasePath + "/CrosshairCursor.png", "PNG");
- mask = new QBitmap(EAction.includeBasePath + "/CrosshairCursorMask.png", "PNG");
- EAction.crossCursor = new QCursor(bitmap, mask, 15, 15);
- }
- }
- }
- this.setCursor(EAction.crossCursor, "CrossCursor");
- };
- /**
- * Sets the current cursor of the current document interface (all views attached
- * to it) to the normal arrow cursor.
- */
- EAction.prototype.setArrowCursor = function() {
- this.setCursor(new QCursor(Qt.ArrowCursor));
- };
- EAction.prototype.setCursor = function(cursor, name) {
- var di = this.getDocumentInterface();
- if (!isNull(di)) {
- di.setCursor(cursor);
- }
- // if (!isNull(name)) {
- // var views = this.getGraphicsViews();
- // for (var i=0; i<views.length; i++) {
- // if (isFunction(views[i].setProperty)) {
- // views[i].setProperty("CursorName", name);
- // }
- // }
- // }
- };
- /**
- * Creates a new widget based on the given UI file with the given parent. This
- * is a helper function for WidgetFactory.createWidget.
- */
- EAction.prototype.createWidget = function(uiFile, parent) {
- return WidgetFactory.createWidget(this.includeBasePath, uiFile, parent);
- };
- /**
- * Creates a new dialog based on the given UI file with the given parent. This
- * is a helper function for WidgetFactory.createDialog.
- */
- EAction.prototype.createDialog = function(uiFile, parent) {
- return WidgetFactory.createDialog(this.includeBasePath, uiFile, parent);
- };
- /**
- * \return The current document or null.
- */
- EAction.getDocument = function() {
- var di = EAction.getDocumentInterface();
- if (isNull(di)) {
- return undefined;
- }
- return di.getDocument();
- };
- /**
- * \return The storage of the current document or null.
- */
- EAction.getStorage = function() {
- var doc = EAction.getDocument();
- if (isNull(doc)) {
- return undefined;
- }
- return doc.getStorage();
- };
- EAction.prototype.getDocumentInterface = function() {
- if (!isNull(global.gDocumentInterface)) {
- return global.gDocumentInterface;
- }
- return RAction.prototype.getDocumentInterface.call(this);
- };
- /**
- * \return The current document interface or null.
- */
- EAction.getDocumentInterface = function() {
- if (!isNull(global.gDocumentInterface)) {
- return global.gDocumentInterface;
- }
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return undefined;
- }
- return appWin.getDocumentInterface();
- };
- /**
- * \return The current main window.
- */
- EAction.getMainWindow = function() {
- if (!isNull(global.gMainWindow)) {
- return global.gMainWindow;
- }
- return RMainWindowQt.getMainWindow();
- };
- /**
- * Reactivates the main window after showing a dialog.
- * This is necessary with Qt>=5.
- * Not necessary with Qt>=5.10.
- */
- EAction.activateMainWindow = function() {
- // if (RSettings.isQt(5)) {
- // var appWin = EAction.getMainWindow();
- // if (isNull(appWin)) {
- // return;
- // }
- // appWin.activateWindow();
- // var view = EAction.getGraphicsView();
- // if (!isNull(view)) {
- // // view loses focus because of dialogs:
- // view.giveFocus();
- // }
- // }
- };
- /**
- * \return The current MDI child or null.
- */
- EAction.getMdiChild = function() {
- var appWin = RMainWindowQt.getMainWindow();
- if (isNull(appWin)) {
- return undefined;
- }
- return appWin.getMdiChild();
- };
- /**
- * \return The MDI area or null.
- */
- EAction.getMdiArea = function() {
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return undefined;
- }
- return appWin.getMdiArea();
- };
- /**
- * \return List of all graphics views attached to the current document interface.
- */
- EAction.prototype.getGraphicsViews = function() {
- var di = this.getDocumentInterface();
- if (isNull(di)) {
- return [];
- }
- return EAction.getGraphicsViews(di);
- };
- EAction.getGraphicsViews = function(di) {
- var ret = [];
- var scenes = di.getGraphicsScenes();
- for (var i=0; i<scenes.length; i++) {
- var views = scenes[i].getGraphicsViews();
- ret = ret.concat(views);
- }
- return ret;
- }
- EAction.prototype.getGraphicsView = function() {
- var di = this.getDocumentInterface();
- if (isNull(di)) {
- return undefined;
- }
- return di.getLastKnownViewWithFocus();
- };
- /**
- * \return Graphics view with focus or last known graphics view with focus.
- */
- EAction.getGraphicsView = function() {
- var di = EAction.getDocumentInterface();
- if (isNull(di)) {
- return undefined;
- }
- return di.getLastKnownViewWithFocus();
- };
- /**
- * \return the existing menu with the given object name or, if no such menu
- * exists, a new menu with the given title and the given object name.
- *
- * \param title Translated title to use for the menu. E.g. qsTr("&File"). \param
- * objectName Object name to use for the menu. Used mainly for later
- * identification of the menu. E.g. "file". \param initFunction Function that is
- * called only if a new menu is created. Used to initialize the menu, e.g.
- * connecting signals, etc.
- */
- EAction.getMenu = function(title, objectName, initFunction) {
- if (isNull(objectName)) {
- objectName = "";
- }
- var appWin = EAction.getMainWindow();
- var menuBar = appWin.menuBar();
- var menu;
- var children = menuBar.children();
- for (var i in children) {
- menu = children[i];
- if (!isNull(menu) && menu.objectName === objectName) {
- break;
- }
- menu = undefined;
- }
- if (isNull(menu)) {
- menu = new QMenu(title, menuBar);
- menu.objectName = objectName;
- menuBar.addMenu(menu);
- if (initFunction != undefined) {
- initFunction(menu);
- }
- }
- return menu;
- };
- /**
- * \return the existing submenu with the given object name or creates a new
- * submenu as child of the given menu, with the given sort order, title and
- * icon.
- *
- * \param menu Parent menu.
- * \param sortOrder Sort order among other submenus. All
- * submenus within the same menu are ordered by this number.
- * \param title Translated title of the submenu. E.g. qsTr("&Line").
- * \param objectName Object name to use for the menu. Used mainly for
- * later identification of the menu.
- * E.g. "line". \param iconFile Path and file name of icon to use for this
- * submenu.
- */
- EAction.getSubMenu = function(menu, groupSortOrder, sortOrder, title, objectName, iconFile) {
- var subMenu = menu.findChild(objectName);
- if (isNull(subMenu)) {
- //var action = menu.addMenu(subMenu);
- var action = new RGuiAction(title, menu);
- action.objectName = objectName + "Action";
- //subMenu = new QMenu(title + " {%1,%2}".arg(groupSortOrder).arg(sortOrder), menu);
- subMenu = new QMenu(title, menu);
- subMenu.objectName = objectName;
- if (!isNull(iconFile)) {
- subMenu.icon = new QIcon(iconFile);
- }
- action.setMenu(subMenu);
- RGuiAction.setGroupSortOrderStatic(action, groupSortOrder);
- RGuiAction.setSortOrderStatic(action, sortOrder);
- RGuiAction.setWidgetNamesStatic(action, [menu.objectName]);
- // re-add action at correct position:
- //RGuiAction.addToWidget(action, menu);
- // subMenu = new QMenu(title, menu);
- // var subMenuActions = menu.actions();
- // var actionBefore = undefined;
- // for (var i = 0; i < subMenuActions.length; ++i) {
- // var so = subMenuActions[i].property("SortOrder");
- // if (!isNull(so) && so > sortOrder) {
- // actionBefore = subMenuActions[i];
- // break;
- // }
- // }
- // subMenu = new QMenu(title, menu);
- // if (!isNull(iconFile)) {
- // subMenu.icon = new QIcon(iconFile);
- // }
- // var separator;
- // if (addSeparator) {
- // separator = new RGuiAction("", RMainWindowQt.getMainWindow());
- // separator.setSeparator(true);
- // separator.setSortOrder(sortOrder - 1);
- // separator.addToMenu(menu);
- // }
- // var subMenuAction;
- // if (isNull(actionBefore)) {
- // subMenuAction = menu.addMenu(subMenu);
- // } else {
- // subMenuAction = menu.insertMenu(actionBefore, subMenu);
- // }
- // subMenu.objectName = objectName;
- // subMenuAction.setProperty("SortOrder", sortOrder);
- }
- return subMenu;
- };
- /**
- * \return Existing or new toolbar.
- *
- * \param title Translated title of the toolbar. E.g. qsTr("Snap").
- * \param objectName Object name to use for the toolbar. Used mainly for later
- * identification of the toolbar. E.g. "line".
- * \param toolBarArea Area where the new toolbar should be added. Note that the
- * actual location where the toolbar is added may also depend on the previously
- * saved state of the tool bars. Defaults to Qt.TopToolBarArea.
- */
- EAction.getToolBar = function(title, objectName, toolBarArea, category, before) {
- if (isNull(objectName)) {
- objectName = "";
- }
- if (isNull(toolBarArea)) {
- toolBarArea = Qt.TopToolBarArea;
- }
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return undefined;
- }
- var tb = undefined;
- if (objectName.length !== 0) {
- tb = appWin.findChild(objectName);
- }
- if (!isNull(tb) && !isOfType(tb, QToolBar)) {
- qWarning("Not a toolbar: " + tb);
- return undefined;
- }
- if (isNull(tb)) {
- tb = new QToolBar(title);
- tb.objectName = objectName;
- if (!isNull(category)) {
- tb.setProperty("Category", category);
- }
- var s = RSettings.getIntValue("ToolBar/IconSize", tb.iconSize.width());
- tb.iconSize = new QSize(s,s);
- if (isNull(before)) {
- appWin.addToolBar(toolBarArea, tb);
- }
- else {
- appWin.insertToolBar(before, tb);
- }
- }
- return tb;
- };
- /**
- * \return Existing or new dock.
- *
- * \param title Translated title of the dock widget. E.g. qsTr("Snap").
- * \param objectName Object name to use for the dock widget. Used mainly for later
- * identification of the dock widget. E.g. "line".
- * \param toolBarArea Area where the new dock should be added. Note that the
- * actual location where the dock is added may also depend on the previously
- * saved state of the docks. Defaults to Qt.LeftDockWidgetArea.
- */
- EAction.getToolMatrixPanel = function(title, objectName, order) {
- if (isNull(objectName)) {
- objectName = "";
- }
- include("scripts/Widgets/CadToolMatrix/CadToolMatrix.js");
- return CadToolMatrix.getToolMatrixPanel(title, objectName, order);
- };
- /**
- * \return Existing or new options toolbar (used to display options for the
- * current tool).
- */
- EAction.getOptionsToolBar = function() {
- var appWin = EAction.getMainWindow();
- if (isNull(EAction.optionsToolBar)) {
- if (!isNull(appWin)) {
- appWin.addToolBarBreak();
- }
- EAction.optionsToolBar = EAction.getToolBar(qsTr("Options"), "Options");
- }
- if (!isNull(EAction.optionsToolBar)) {
- if (!RSettings.getOriginalArguments().contains("-no-show")) {
- EAction.optionsToolBar.visible = false;
- }
- }
- return EAction.optionsToolBar;
- };
- /**
- * \return Existing or new block toolbar (used to display options for the
- * current block).
- */
- //EAction.getBlockToolBar = function() {
- // if (isNull(EAction.blockToolBar)) {
- // EAction.blockToolBar = EAction.getToolBar(qsTr("Block Options"), "BlockOptions");
- // }
- // return EAction.blockToolBar;
- //};
- /**
- * \return The main (top) CAD toolbar panel.
- */
- EAction.getMainCadToolBarPanel = function() {
- return EAction.getCadToolBarPanel(qsTr("Main Tools"), "MainToolsPanel", false);
- };
- /**
- * \return Existing or new CAD toolbar panel. Each CAD toolbar panel contains
- * a set of tools, e.g. to draw lines, change the snap tool, etc. The CAD tool
- * bar itself is created automatically when this function is called for the
- * first time.
- *
- * \param title Translated title to use for the CAD toolbar panel. E.g.
- * qsTr("Snap"). \param objectName Object name to use for the panel. Used mainly
- * for later identification of the panel. E.g. "snap". \param hasBackButton True
- * to display a back button at the top or left to allow the user to navigate
- * back to the main CAD toolbar panel.
- */
- EAction.getCadToolBarPanel = function(title, objectName, hasBackButton) {
- var appWin = EAction.getMainWindow();
- var cadToolBar = appWin.findChild("CadToolBar");
- // create CAD toolbar if it does not exist already:
- if (isNull(cadToolBar)) {
- cadToolBar = new RCadToolBar(qsTr("CAD Tools"), appWin);
- cadToolBar.objectName = "CadToolBar";
- // cadToolBar.updateIconSize();
- cadToolBar.topLevelChanged.connect(function(onOff) {
- RSettings.setValue("CadToolBar/VerticalWhenFloating", false);
- });
- cadToolBar.orientationChanged.connect(function(orientation) {
- RSettings.setValue("CadToolBar/VerticalWhenFloating", false);
- });
- }
- return cadToolBar.getPanel(title, objectName, hasBackButton);
- };
- /**
- * Shows the CAD toolbar panel with the given objectName.
- */
- EAction.showCadToolBarPanel = function(objectName) {
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- var toolBar = appWin.findChild("CadToolBar");
- toolBar.showPanel(objectName);
- };
- /**
- * Shows the CAD toolbar panel with the snap tools.
- */
- EAction.showSnapTools = function() {
- EAction.showCadToolBarPanel("SnapToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the main tools.
- */
- EAction.showMainTools = function() {
- EAction.showCadToolBarPanel("MainToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the point tools.
- */
- EAction.showPointTools = function() {
- EAction.showCadToolBarPanel("PointToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the line tools.
- */
- EAction.showLineTools = function() {
- EAction.showCadToolBarPanel("LineToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the arc tools.
- */
- EAction.showArcTools = function() {
- EAction.showCadToolBarPanel("ArcToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the circle tools.
- */
- EAction.showCircleTools = function() {
- EAction.showCadToolBarPanel("CircleToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the dimension tools.
- */
- EAction.showDimensionTools = function() {
- EAction.showCadToolBarPanel("DimensionToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the ellipse tools.
- */
- EAction.showEllipseTools = function() {
- EAction.showCadToolBarPanel("EllipseToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the spline tools.
- */
- EAction.showSplineTools = function() {
- EAction.showCadToolBarPanel("SplineToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the polyline tools.
- */
- EAction.showPolylineTools = function() {
- EAction.showCadToolBarPanel("PolylineToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the shape tools.
- */
- EAction.showShapeTools = function() {
- EAction.showCadToolBarPanel("ShapeToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the modification tools.
- */
- EAction.showModificationTools = function() {
- EAction.showCadToolBarPanel("ModifyToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the measuring / info tools.
- */
- EAction.showInformationTools = function() {
- EAction.showCadToolBarPanel("InformationToolsPanel");
- };
- /**
- * Shows the CAD toolbar panel with the selection tools.
- */
- EAction.showSelectTools = function() {
- EAction.showCadToolBarPanel("SelectToolsPanel");
- };
- /**
- * Helper function to conveniently add the given action to a menu, toolbar and
- * CAD toolbar.
- *
- * \param action RAction object.
- * \param iface Interface to use to retrieve the appropriate menu, toolbar and CAD toolbar panel.
- * \param addToMenu True to add this action to the appropriate menu.
- * \param addToToolbar True to add this action to the appropriate toolbar.
- * \param addToCadToolbar True to add this action to the appropriate CAD toolbar panel.
- * \param addSeparator True to add a separator before this action.
- */
- EAction.addGuiActionTo = function(action, iface, addToMenu, addToToolBar,
- addToCadToolBar, addSeparator) {
- var className = action.getScriptClass();
- if (isNull(addToMenu)) {
- addToMenu = false;
- }
- if (isNull(addToToolBar)) {
- addToToolBar = false;
- }
- if (isNull(addToCadToolBar)) {
- addToCadToolBar = false;
- }
- if (isNull(addSeparator)) {
- addSeparator = false;
- }
- var addToUserToolBar1 = false;
- var addToUserToolBar2 = false;
- if (className.length!==0) {
- var key = className + "/VisibleInMenu";
- addToMenu = RSettings.getBoolValue(key, addToMenu);
- if (!RSettings.hasValue(key)) {
- RSettings.setValue(key, addToMenu);
- }
- key = className + "/VisibleInToolBar";
- addToToolBar = RSettings.getBoolValue(key, addToToolBar);
- if (!RSettings.hasValue(key)) {
- RSettings.setValue(key, addToToolBar);
- }
- key = className + "/VisibleInCadToolBar";
- addToCadToolBar = RSettings.getBoolValue(key, addToCadToolBar);
- if (!RSettings.hasValue(key)) {
- RSettings.setValue(key, addToCadToolBar);
- }
- key = className + "/VisibleInUserToolBar1";
- addToUserToolBar1 = RSettings.getBoolValue(key, addToUserToolBar1);
- if (!RSettings.hasValue(key)) {
- RSettings.setValue(key, addToUserToolBar1);
- }
- key = className + "/VisibleInUserToolBar2";
- addToUserToolBar2 = RSettings.getBoolValue(key, addToUserToolBar2);
- if (!RSettings.hasValue(key)) {
- RSettings.setValue(key, addToUserToolBar2);
- }
- }
- if (isNull(iface)) {
- qWarning("EAction.js:", "addGuiActionTo(): iface not defined");
- }
- if (action.icon.isNull() && !action.isIconDisabled()) {
- action.setIcon(autoPath("scripts/Empty.svg"));
- }
- var separator;
- if (addSeparator) {
- separator = new RGuiAction("", RMainWindowQt.getMainWindow());
- separator.setSeparator(true);
- //separator.setSortOrder(action.getSortOrder() - 1);
- }
- if (addToMenu) {
- if (isFunction(iface.getMenu)) {
- var menu = iface.getMenu();
- if (!isNull(menu)) {
- if (!isNull(separator)) {
- separator.addToMenu(menu);
- }
- action.addToMenu(menu);
- }
- }
- }
- if (addToToolBar) {
- if (isFunction(iface.getToolBar)) {
- var tb = iface.getToolBar();
- if (!isNull(tb)) {
- action.addToToolBar(tb);
- if (addSeparator) {
- var sp = new RGuiAction("", RMainWindowQt.getMainWindow());
- sp.setSeparator(true);
- sp.setSortOrder(action.getSortOrder() - 1);
- sp.addToToolBar(tb);
- }
- }
- }
- }
- if (addToCadToolBar) {
- if (isFunction(iface.getCadToolBarPanel)) {
- var ctb = iface.getCadToolBarPanel();
- if (!isNull(ctb)) {
- // if (!isNull(separator)) {
- // CadToolBarPanel.prototype.addAction.call(ctb, separator);
- // }
- //CadToolBarPanel.prototype.addAction.call(ctb, action);
- //ctb.addAction(action);
- action.addToWidget(ctb);
- }
- }
- }
- var appWin = EAction.getMainWindow();
- if (addToUserToolBar1) {
- var tb = appWin.findChild("UserToolBar1");
- if (!isNull(tb)) {
- action.addToToolBar(tb);
- }
- }
- if (addToUserToolBar2) {
- var tb = appWin.findChild("UserToolBar2");
- if (!isNull(tb)) {
- action.addToToolBar(tb);
- }
- }
- };
- //EAction.addGuiActionToWidgets = function(action, widgetNames) {
- // var appWin = EAction.getMainWindow();
- // var className = action.getScriptClass();
- // var addToMenu, addToToolBar, addToCadToolBar;
- // if (className.length!==0) {
- // var key = className + "/VisibleInMenu";
- // addToMenu = RSettings.getBoolValue(key, addToMenu);
- // if (!RSettings.hasValue(key)) {
- // RSettings.setValue(key, addToMenu);
- // }
- // key = className + "/VisibleInToolBar";
- // addToToolBar = RSettings.getBoolValue(key, addToToolBar);
- // if (!RSettings.hasValue(key)) {
- // RSettings.setValue(key, addToToolBar);
- // }
- // key = className + "/VisibleInCadToolBar";
- // addToCadToolBar = RSettings.getBoolValue(key, addToCadToolBar);
- // if (!RSettings.hasValue(key)) {
- // RSettings.setValue(key, addToCadToolBar);
- // }
- // }
- // var i, w;
- // var oldWidgets = action.associatedWidgets();
- // for (i=0; i<oldWidgets.length; i++) {
- // w = oldWidgets[i];
- // w.removeAction(action);
- // }
- // for (i=0; i<widgetNames.length; i++) {
- // var wn = widgetNames[i];
- // w = appWin.findChild(wn);
- // action.addToWidget(w);
- // }
- //};
- /**
- * \return The boolean value of the given variable from the document if it
- * exists in the document, from the settings file if it exists there. If the
- * variable does not exist in the document nor the settings file, the given
- * defaultValue is returned.
- */
- EAction.getBoolValue = function(varName, defaultValue, document) {
- if (isNull(document)) {
- document = EAction.getDocument();
- }
- if (!isNull(document)) {
- var val = document.getVariable(varName);
- if (typeof (val) != "undefined") {
- return val;
- }
- }
- return RSettings.getBoolValue(varName, defaultValue);
- };
- /**
- * \return The int value of the given variable from the document if it exists in
- * the document, from the settings file if it exists there. If the variable does
- * not exist in the document nor the settings file, the given defaultValue is
- * returned.
- */
- EAction.getIntValue = function(varName, defaultValue, document) {
- if (isNull(document)) {
- document = EAction.getDocument();
- }
- if (!isNull(document)) {
- var val = document.getVariable(varName);
- if (typeof (val) != "undefined") {
- return parseInt(val, 10);
- }
- }
- return RSettings.getIntValue(varName, parseInt(defaultValue, 10));
- };
- /**
- * \return The float value of the given variable from the document if it exists
- * in the document, from the settings file if it exists there. If the variable
- * does not exist in the document nor the settings file, the given defaultValue
- * is returned.
- */
- EAction.getDoubleValue = function(varName, defaultValue, document) {
- if (isNull(document)) {
- document = EAction.getDocument();
- }
- if (!isNull(document)) {
- var val = document.getVariable(varName);
- if (typeof (val) != "undefined") {
- return parseFloat(val);
- }
- }
- return RSettings.getDoubleValue(varName, parseFloat(defaultValue));
- };
- /**
- * \return The string value of the given variable from the document if it exists
- * in the document, from the settings file if it exists there. If the variable
- * does not exist in the document nor the settings file, the given defaultValue
- * is returned.
- */
- EAction.getStringValue = function(varName, defaultValue, document) {
- if (isNull(document)) {
- document = EAction.getDocument();
- }
- if (!isNull(document)) {
- var val = document.getVariable(varName);
- if (typeof (val) != "undefined") {
- return val.toString();
- }
- }
- return RSettings.getStringValue(varName, defaultValue);
- };
- /**
- * \return The value of the given variable from the document if it exists in the
- * document, from the settings file if it exists there. If the variable does not
- * exist in the document nor the settings file, the given defaultValue is
- * returned.
- */
- EAction.getValue = function(varName, defaultValue, document) {
- if (isNull(document)) {
- document = EAction.getDocument();
- }
- if (!isNull(document)) {
- var val = document.getVariable(varName, defaultValue);
- if (!isNull(val)) {
- return val;
- }
- }
- return RSettings.getValue(varName, defaultValue);
- };
- /**
- * Adds the given widgets to the status bar of the application.
- * \param sortOrder Defines the sort order of widgets in the status bar.
- */
- EAction.addToStatusBar = function(widget, sortOrder) {
- include("scripts/Widgets/StatusBar/StatusBar.js");
- StatusBar.addWidget(widget, sortOrder);
- };
- EAction.prototype.getScales = function() {
- var doc = this.getDocument();
- if (isNull(doc)) {
- return [];
- }
- return EAction.getScales(doc.getUnit());
- };
- EAction.getScales = function(unit) {
- var scales;
- if (RUnit.isMetric(unit)) {
- // common metric scales:
- scales = new Array(
- "1:1", "1:2", "1:5", "1:10", "1:20", "1:25", "1:50", "1:75",
- "1:100", "1:125", "1:150", "1:175",
- "1:200", "1:250", "1:500", "1:750",
- "1:1000", "1:2500", "1:5000", "1:7500",
- "1:10000", "1:25000", "1:50000", "1:75000",
- "1:100000", "1:250000", "1:500000", "1:750000",
- "1:1000000",
- "2:1", "5:1", "10:1", "20:1", "25:1", "50:1", "75:1",
- "100:1", "125:1", "150:1", "175:1",
- "200:1", "250:1", "500:1", "750:1",
- "1000:1", "2500:1", "5000:1", "7500:1",
- "10000:1", "25000:1", "50000:1", "75000:1",
- "100000:1", "250000:1", "500000:1", "750000:1",
- "1000000:1");
- } else {
- // common imperial scales:
- scales = new Array(
- '1" = 1"', '1" = 2"', '1" = 4"', '1" = 8"', '1" = 16"',
- '1" = 32"', '1" = 64"', '1" = 128"', '1" = 256"',
- '1" = 512"', '1" = 1024"', '1" = 2048"', '1" = 4096"',
- '1/4096" = 1\'-0"', '1/2048" = 1\'-0"', '1/1024" = 1\'-0"',
- '1/512" = 1\'-0"', '1/256" = 1\'-0"', '1/128" = 1\'-0"',
- '1/64" = 1\'-0"',
- '1/32" = 1\'-0"',
- '1/16" = 1\'-0"',
- '3/32" = 1\'-0"',
- '1/8" = 1\'-0"',
- '3/16" = 1\'-0"',
- '1/4" = 1\'-0"',
- '3/8" = 1\'-0"',
- '1/2" = 1\'-0"',
- '3/4" = 1\'-0"',
- '1" = 1\'-0"', '2" = 1\'-0"', '3" = 1\'-0"', '4" = 1\'-0"',
- '5" = 1\'-0"', '6" = 1\'-0"', '7" = 1\'-0"', '8" = 1\'-0"',
- '9" = 1\'-0"', '10" = 1\'-0"', '11" = 1\'-0"');
- }
- return scales;
- };
- /**
- * Called by updatePreview. Implementations must return an ROperation subclass
- * object for the operation that should be previewed.
- */
- EAction.prototype.getOperation = function(preview) {
- return undefined;
- };
- /**
- * Called by updatePreview. Implementations must return an array of RShape
- * objects that should be displayed as auxiliary construction.
- */
- EAction.prototype.getAuxPreview = function() {
- return undefined;
- };
- /**
- * Called by updatePreview. Implementations must return an array of entity IDs
- * which should be highlighted.
- */
- EAction.prototype.getHighlightedEntities = function() {
- return undefined;
- };
- /**
- * Called by updatePreview. Implementations must handle errors during preview.
- */
- EAction.prototype.handlePreviewError = function() {
- return;
- };
- /**
- * Updates the preview based on the operation returned by getOperation and the
- * auxiliary construction returned be getAuxPreview.
- *
- * \param clear True to clear the preview first. This is necessary if the
- * preview is updated not as a result of a mouse move event, e.g. when changing
- * action parameters in the options toolbar.
- */
- EAction.prototype.updatePreview = function(clear) {
- if (isNull(clear)) {
- clear = false;
- }
- var di = this.getDocumentInterface();
- if (isNull(di)) {
- return;
- }
- if (clear === true) {
- di.clearPreview();
- }
- var op = this.getOperation(true);
- if (!isNull(op)) {
- di.previewOperation(op);
- }
- else {
- this.handlePreviewError();
- }
- var i;
- var previewShapes = this.getAuxPreview();
- if (!isNull(previewShapes)) {
- for (i = 0; i < previewShapes.length; ++i) {
- di.addAuxShapeToPreview(previewShapes[i]);
- }
- }
- var highlightedEntities = this.getHighlightedEntities();
- if (!isNull(highlightedEntities)) {
- for (i = 0; i < highlightedEntities.length; ++i) {
- di.highlightEntity(highlightedEntities[i]);
- }
- }
- di.repaintViews();
- };
- /**
- * Applies the operation returned by this.getOperation().
- */
- EAction.prototype.applyOperation = function() {
- var di = this.getDocumentInterface();
- var op = this.getOperation(false);
- if (isNull(op)) {
- qWarning("Operation returned by this.getOperation() is undefined");
- return false;
- }
- this.complementOperation(op);
- var t = di.applyOperation(op);
- this.postOperation(t);
- return true;
- };
- /**
- * Can be reimplemented to complement the operation of an action.
- */
- EAction.prototype.complementOperation = function(op) {
- return;
- };
- /**
- * Can be reimplemented to work with the transaction returned by the operation.
- */
- EAction.prototype.postOperation = function(transaction) {
- return;
- };
- /**
- * High level event triggered when a coordinate was specified by the user of a
- * coordinate specification should be previewed.
- */
- EAction.prototype.pickCoordinate = function(event, preview) {
- };
- /**
- * Default implementation. Calls pickCoordinate(event, false)
- */
- EAction.prototype.coordinateEvent = function(event) {
- // don't let invalid positions go through for most cases (this is overridden for DefaultAction):
- var pos = event.getModelPosition();
- if (!isValidVector(pos)) {
- EAction.handleUserWarning(qsTr("Invalid position"));
- return;
- }
- this.pickCoordinate(event, false);
- };
- /**
- * Default implementation. Calls pickCoordinate(event, true)
- */
- EAction.prototype.coordinateEventPreview = function(event) {
- this.pickCoordinate(event, true);
- };
- /**
- * High level event triggered when an entity was picked by the user or to
- * preview picking an entity.
- */
- EAction.prototype.pickEntity = function(event, preview) {
- };
- /**
- * Default implementation. Calls pickEntity(event, false)
- */
- EAction.prototype.entityPickEvent = function(event) {
- this.pickEntity(event, false);
- };
- /**
- * Default implementation. Calls pickEntity(event, true)
- */
- EAction.prototype.entityPickEventPreview = function(event) {
- this.pickEntity(event, true);
- };
- /**
- * High level event triggered when a command line command should be applied or
- * previewed.
- */
- EAction.prototype.applyCommand = function(event, preview) {
- };
- /**
- * Default implementation. Calls applyCommand(event, false)
- */
- EAction.prototype.commandEvent = function(event) {
- this.applyCommand(event, false);
- };
- /**
- * Default implementation. Calls applyCommand(event, true)
- */
- EAction.prototype.commandEventPreview = function(event) {
- this.applyCommand(event, true);
- };
- EAction.prototype.simulateMouseMoveEvent = function() {
- var di = this.getDocumentInterface();
- if (isNull(di)) {
- return;
- }
- var view = di.getLastKnownViewWithFocus();
- if (isNull(view)) {
- return;
- }
- view = getRGraphicsView(view);
- if (isNull(view)) {
- return;
- }
- view.simulateMouseMoveEvent();
- };
- /**
- * Shows the given warning to the user.
- *
- * \param escape True to escape rich text and disaply the message
- * exactly as given (default), false to allow rich text in message
- * (colors, indentation, links, etc.)
- */
- EAction.handleUserWarning = function(message, escape) {
- if (isNull(message)) {
- return;
- }
- if (isNull(escape)) {
- escape = true;
- }
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- appWin.handleUserWarning(message, false, escape);
- };
- EAction.handleUserMessage = function(message, escape) {
- if (isNull(message)) {
- return;
- }
- if (isNull(escape)) {
- escape = true;
- }
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- appWin.handleUserMessage(message, escape);
- };
- EAction.handleUserInfo = function(message, escape) {
- if (isNull(message)) {
- return;
- }
- if (isNull(escape)) {
- escape = true;
- }
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- appWin.handleUserInfo(message, escape);
- };
- EAction.handleUserCommand = function(message, escape) {
- if (isNull(message)) {
- return;
- }
- if (isNull(escape)) {
- escape = true;
- }
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- appWin.handleUserCommand(message, escape);
- };
- EAction.setProgress = function(p) {
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- appWin.setProgress(p);
- };
- EAction.setProgressText = function(t) {
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- EAction.handleUserMessage(t);
- appWin.setProgressText(t);
- };
- EAction.setProgressEnd = function() {
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- appWin.setProgressEnd();
- };
- /**
- * Checks if the given entity is editable (not on a locked layer) and prints a
- * warning if quiet is false and this is not the case.
- */
- EAction.assertEditable = function(entity, quiet) {
- if (!entity.isEditable()) {
- if (!quiet) {
- if (!entity.isInWorkingSet()) {
- EAction.handleUserWarning(qsTr("Entity is not in working set."));
- }
- else {
- EAction.handleUserWarning(qsTr("Entity is on a locked layer."));
- }
- }
- return false;
- }
- return true;
- };
- /**
- * Allows all actions to handle property change events. This is necessary to
- * ensure that properties can be changed even if a tool is active (e.g. while
- * drawing lines).
- */
- EAction.prototype.propertyChangeEvent = function(event) {
- var di = this.getDocumentInterface();
- if (isNull(di)) {
- return;
- }
- var operation = new RChangePropertyOperation(event);
- di.applyOperation(operation);
- di.clearPreview();
- di.repaintViews();
- };
- EAction.prototype.setLeftMouseTip = function(tip) {
- var appWin = EAction.getMainWindow();
- if (!isNull(appWin) && isFunction(appWin.setLeftMouseTip)) {
- appWin.setLeftMouseTip(tip);
- }
- };
- EAction.prototype.setRightMouseTip = function(tip) {
- var appWin = EAction.getMainWindow();
- if (!isNull(appWin) && isFunction(appWin.setRightMouseTip)) {
- appWin.setRightMouseTip(tip);
- }
- };
- EAction.prototype.setCommandPrompt = function(prompt) {
- if (isNull(prompt)) {
- prompt = "";
- }
- var appWin = EAction.getMainWindow();
- if (!isNull(appWin) && isFunction(appWin.setCommandPrompt)) {
- appWin.setCommandPrompt(prompt);
- }
- };
- EAction.prototype.getToolTitle = function() {
- var guiAction = this.getGuiAction();
- if (isNull(guiAction)) {
- return "";
- }
- return guiAction.text.replace("&", "");
- };
- EAction.disableCoordinateWidget = function() {
- EAction.enableCoordinateWidget(false);
- };
- EAction.enableCoordinateWidget = function(enable) {
- if (isNull(enable)) {
- enable=true;
- }
- var appWin = EAction.getMainWindow();
- if (isNull(appWin)) {
- return;
- }
- var statusBar = appWin.findChild("StatusBar");
- if (isNull(statusBar)) {
- return;
- }
- var cd = statusBar.findChild("CoordinateDisplay");
- if (!isNull(cd)) {
- cd.enabled = enable;
- }
- };
- /**
- * \return ID of entity closest to the mouse cursor. No handling of ambiguous results.
- * \param event RRInputEvent
- * \param range Cursor range in pixels
- */
- EAction.getEntityIdUnderCursor = function(di, event, range, selectable) {
- if (isNull(di)) {
- return [];
- }
- if (isNull(selectable)) {
- selectable = false;
- }
- var view = event.getGraphicsView();
- if (isNull(range)) {
- range = RSettings.getPickRange();
- }
- range = view.mapDistanceFromView(range);
- var strictRange = view.mapDistanceFromView(10);
- return di.getClosestEntity(event.getModelPosition(), range, strictRange, !selectable);
- };
- /**
- * \return Array of entity IDs of entities closest to the mouse cursor.
- * \param event RRInputEvent
- * \param range Cursor range in pixels
- */
- EAction.getEntityIdsUnderCursor = function(di, event, range, selectable) {
- if (isNull(di)) {
- return [];
- }
- if (isNull(selectable)) {
- selectable = false;
- }
- var view = event.getGraphicsView();
- if (isNull(range)) {
- range = RSettings.getPickRange();
- }
- range = view.mapDistanceFromView(range);
- var doc = di.getDocument();
- return doc.queryIntersectedEntitiesXY(new RBox(event.getModelPosition(), range), false, !selectable);
- };
- /**
- * \return Entity ID under the mouse cursor. User may choose between multiple candidates if
- * result is ambiguous and Alt key is pressed.
- *
- * \param event RInputEvent
- * \param preview for previewing purposes
- * \param selectable Only return selectable (editable) entities
- */
- EAction.prototype.getEntityId = function(event, preview, selectable) {
- if (isNull(preview)) {
- preview = false;
- }
- if (isNull(selectable)) {
- selectable = false;
- }
- var di = this.getDocumentInterface();
- if (isNull(di)) {
- return RObject.INVALID_ID;
- }
- return EAction.getEntityId(di, this, event, preview, selectable);
- };
- EAction.getEntityId = function(di, action, event, preview, selectable) {
- if (isNull(preview)) {
- preview = false;
- }
- if (isNull(selectable)) {
- selectable = false;
- }
- var altPressed = isAltPressed(event);
- if (!altPressed || preview || RSettings.getBoolValue("GraphicsView/DisableAltPicking", false)===true) {
- if (!isNull(action.idFromContextMenu)) {
- // user already chose an entity from the context menu:
- return action.idFromContextMenu;
- }
- else {
- return EAction.getEntityIdUnderCursor(di, event, undefined, selectable);
- }
- }
- // fixed range, we never want to show a lot of entities in the context menu:
- var r = 10 * (RSettings.getHighResolutionGraphicsView() ? RSettings.getDevicePixelRatio() : 1);
- var entityIds = EAction.getEntityIdsUnderCursor(di, event, r, selectable);
- entityIds = di.getStorage().orderBackToFront(entityIds);
- if (entityIds.length===0) {
- // no entity under cursor:
- return RObject.INVALID_ID;
- }
- if (entityIds.length===1) {
- // one single entity under cursor:
- return entityIds[0];
- }
- // multiple entities under cursor, offer context menu:
- var ret = RObject.INVALID_ID;
- var doc = di.getDocument();
- var menu = new QMenu(EAction.getGraphicsView());
- menu.objectName = "EntityContextMenu";
- // reacts to hovering and clicking of context menu items:
- function Reactor(id) {
- this.id = id;
- }
- Reactor.prototype.trigger = function() {
- ret = this.id;
- };
- Reactor.prototype.hover = function() {
- di.clearPreview();
- di.highlightEntity(this.id);
- action.idFromContextMenu = this.id;
- action.entityPickEventPreview(event);
- action.idFromContextMenu = undefined;
- di.repaintViews();
- };
- // invalid entity id is used to to cancel:
- entityIds.push(RObject.INVALID_ID);
- var a, reactor;
- for (var i=0; i<entityIds.length && i<20; i++) {
- var id = entityIds[i];
- var str;
- var icon = undefined;
- if (id!==RObject.INVALID_ID) {
- var e = doc.queryEntityDirect(id);
- str = entityTypeToString(e.getType(), false);
- //str += " / " + e.getProperty(REntity.PropertyDisplayedColor)[0].getName();
- str += " / " + e.getLayerName();
- if (isFunction(e.getLength)) {
- var l = e.getLength();
- if (isNumber(l)) {
- str += " / " + qsTr("Length:") + " " + numberToString(e.getLength(), 3);
- }
- }
- var col = e.getProperty(REntity.PropertyDisplayedColor, true)[0];
- if (!isNull(col)) {
- icon = RColor.getIcon(col, new QSize(16,16));
- }
- }
- else {
- str = qsTr("Cancel");
- menu.addSeparator();
- }
- // add menu entry in format [Type] / [Layer] / [Color] / [Length]
- a = menu.addAction(str);
- if (!isNull(icon)) {
- a.icon = icon;
- a.iconVisibleInMenu = true;
- }
- reactor = new Reactor(id);
- a.triggered.connect(reactor, reactor.trigger);
- a.hovered.connect(reactor, reactor.hover);
- }
- // show context menu:
- if (!menu.isEmpty()) {
- this.waitingForContextMenu = true;
- menu.exec(new QPoint(QCursor.pos().x(), QCursor.pos().y()+10));
- this.waitingForContextMenu = false;
- di.clearPreview();
- }
- return ret;
- };
- EAction.prototype.isEntitySnappable = function(e) {
- if (!isEntity(e)) {
- return false;
- }
- var layerId = e.getLayerId();
- var data = e.getData();
- var doc = data.getDocument();
- if (isNull(doc)) {
- return false;
- }
- return doc.isLayerSnappable(layerId);
- };
- EAction.prototype.isEntityEditable = function(e) {
- if (!isEntity(e)) {
- return false;
- }
- return e.isEditable();
- };
- /**
- * Some common, shared translated warnings:
- */
- EAction.warnNotBlockReference = function() {
- EAction.handleUserWarning(qsTr("Entity is not a block reference."));
- };
- EAction.warnNotLineArc = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line or arc."))
- };
- EAction.warnNotLineArcPolyline = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc or polyline."))
- };
- EAction.warnNotLine = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line."));
- };
- EAction.warnNotArc = function() {
- EAction.handleUserWarning(qsTr("Entity is not an arc."));
- };
- EAction.warnNotArcCircle = function() {
- EAction.handleUserWarning(qsTr("Entity is not an arc or circle."));
- };
- EAction.warnNotLineArcCircle = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc or circle."));
- };
- EAction.warnNotLineArcSplineEllipseArcPolyline = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc, spline, ellipse arc or polyline."));
- };
- EAction.warnNotArcCircleEllipse = function() {
- EAction.handleUserWarning(qsTr("Entity is not an arc, circle or ellipse."));
- };
- EAction.warnNotLineArcCircleEllipse = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc, circle or ellipse."));
- };
- EAction.warnNotLineArcCircleEllipsePolyline = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc, circle, ellipse or polyline."));
- };
- EAction.warnNotLineArcCircleEllipseSpline = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc, circle, ellipse or spline."));
- };
- EAction.warnNotLineArcCircleSplinePolyline = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc, circle, spline or polyline."));
- };
- EAction.warnNotLineArcCircleEllipseSplinePolyline = function() {
- EAction.handleUserWarning(qsTr("Entity is not a line, arc, circle, ellipse, spline or polyline."));
- };
- EAction.warnNotPolyline = function() {
- EAction.handleUserWarning(qsTr("Entity is not a polyline."));
- };
- EAction.warnNotSplineWithFitPoints = function() {
- EAction.handleUserWarning(qsTr("Entity is not a spline with fit points."));
- };
|