Line2PEx - An improved line command?

This forum is for QCAD user developed script Add Ons and Plug-ins.
Please use this forum to request/submit your script Add-Ons & Plug-in idea's.

Moderators: Clive, andrew, Husky

Re: Line2PEx - An improved line command?

Postby riverbuoy » Fri Feb 14, 2014 11:39 pm

Having looked into this, the spinbox uses the locale of the computer it is running on, unless a specific locale has been assigned to the spinbox. The decimal point character can't be changed or overwritten, and changing the specific locale would not be a simple matter for users. As Andrew suggested, using an RMathLineEdit box is the easiest way to solve this.
I have attached the two affected files - Line2PEx.ui, and Line2PEx.js. Replace these two files in Draw->Line->Line2PEx.
Using an RMathLineEdit input, you can type any value into the box, or even an expression. If you enter a negative number then the restrict changes it to a positive number.
I have included the options in a group box so you can toggle them on and off using Alt-Z. When toggled off they won't interfere with existing shortcuts. Because other commands can change the position of relativezero I store its position when using another command, and restore it when control is returned to this command.

Hope this fixes the problem.
Attachments
Line2PEx.ui
(3.82 KiB) Downloaded 82 times
Line2PEx.js
(15.32 KiB) Downloaded 86 times
riverbuoy
Full Member
 
Posts: 50
Joined: Thu Oct 03, 2013 5:37 pm

Re: Line2PEx - An improved line command?

Postby andrew » Sat Feb 15, 2014 10:29 am

Thanks riverbuoy. I've noticed that you did not copy the changes I've made into your version (translation related). This means, I will have to re-apply my changes everytime you post a new version:
https://github.com/qcad/qcad/commit/1a9 ... 1232e9a192

For this reason, we actually use a version control system (git) which manages changes over time and allows merging. Perhaps that is something to look into in the future? Version control systems are among those tools you never wish to miss anymore once you start using them :)
User avatar
andrew
Site Admin
 
Posts: 2755
Joined: Fri Mar 30, 2007 6:07 am

Re: Line2PEx - An improved line command?

Postby Husky » Sat Feb 15, 2014 10:40 am

Hi riverbuoy,

I briefly tested the fix and my first impression is: Much better! Thanks!

Question: If I use the wrong decimal point character it will show me the wrong value in red - but the tool is still, in limits, usable.

Husky-2014.02.15-01.png
Husky-2014.02.15-01.png (24.95 KiB) Viewed 2012 times


If I compare this with the "Line from Angle LA" tool it will warn me with the value in red and block the tool completely.

Husky-2014.02.15-02.png
Husky-2014.02.15-02.png (20.33 KiB) Viewed 2012 times


I think that makes sense - is it possible to have the same behavior in your tool?

Additional thought: Would it make sense to add on the displayed angle the decree character so that we can see a 3.5° or 7° etc info in the drawing?
User avatar
Husky
Moderator
 
Posts: 713
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Line2PEx - An improved line command?

Postby riverbuoy » Tue May 13, 2014 4:08 pm

I have changed Line2PEx.js, as I found two issues.
First, trying to use a groupBox in the options toolbar is just too problematic. So I changed the key sequence 'Alt-Z' to disable the options toolbar. Once disabled, you can then type any shortcut without it conflicting with the line options. (If the shortcut doesn't start with 'C', 'U', 'R', 'T' or 'D' then you can just type the shortcut). If you type a shortcut then the option toolbar is re-enabled, and the position of relativezero is saved. When you return to the line command, the toolbar is enabled and relativezero is put back to where it was.
The second issue concerns returning to the line command. If, for example, you draw a circle and then return to the line command, and the undo button is enabled, then undo uses the system undo, so instead of deleting the last line segment, it will delete the circle and move relativezero to the previous position. So now you've deleted the circle you just drew and relativezero is in the wrong position.
I changed undo to use transactions instead of the system undo. So now when you press undo it will delete the last line segment and not the circle.
I have attached the file 'Line2PEx.js'. Replace the existing file with this one.
Attachments
Line2PEx.js
(15.83 KiB) Downloaded 61 times
riverbuoy
Full Member
 
Posts: 50
Joined: Thu Oct 03, 2013 5:37 pm

Re: Line2PEx - An improved line command?

Postby andrew » Fri Jun 20, 2014 12:29 pm

QCAD 3.6.0 comes with a new snap restriction tool which was designed to offer similar functionality as the Line2PEx to all tools:
Snap > Restrict Angle or Length

The new tool can be used similarly like Line2PEx:
- Start the regular line drawing tool Draw > Line > Line from 2 Points
- Set the start point of the line
- Click the restrict tool button in the line options tool bar (or the same button in the CAD tool bar at the left or choose menu Snap > Restrict Angle or Length or enter the key code EA):
Screen Shot 2014-06-20 at 13.13.21.png
Screen Shot 2014-06-20 at 13.13.21.png (18.21 KiB) Viewed 1542 times

- You can now enter a restriction angle (and optionally a restriction distance):
Screen Shot 2014-06-20 at 13.18.16.png
Screen Shot 2014-06-20 at 13.18.16.png (14.34 KiB) Viewed 1542 times


QCAD snaps to multiples of the given angle and distance.

This tool can be used in combination with any drawing or modification tool, whenever a coordinate is requested.

I've decided against displaying the angle / length inside the drawing space for now, although the code is still in Viewport.js, commented out under '// snap restriction info:' if someone wants to try it.
The angle and distance used are always visible in the status bar of course:
Screen Shot 2014-06-20 at 13.26.55.png
Screen Shot 2014-06-20 at 13.26.55.png (12.15 KiB) Viewed 1542 times


I understand that this a bit of a compromise, compared to the Line2PEx tool, so the original Line2PEx tool is still available under Misc > Drawing, same keycode as before.

As usual, any feedback is welcome.
User avatar
andrew
Site Admin
 
Posts: 2755
Joined: Fri Mar 30, 2007 6:07 am

Re: Line2PEx - An improved line command?

Postby riverbuoy » Mon Jun 23, 2014 6:21 pm

Hi Andrew,

Your two new entities, Ray and XLine, are great, and combining them with the line command is an excellent idea.
'Snap to Angle or Distance' is also great. I briefly thought about trying to do a similar thing when adding Restrict to the 'line' command, but my knowledge of the inner workings of QCAD is limited, and I realised it would be too dificult for me to do. So, I took the easy way out and resorted to using ECMAScript.
As you point out, your new restriction applies to all commands expecting coordinate input, which is far better. (By the way, I was just about to post 'DrawPolylineEx', which has the same additions as 'Line2PEx'. :roll: ). I think your new line command is the way to go.
Your hard work is much appreciated.

Now, I don't want to rain on your parade, but has anyone pointed out that the shortcut EA conflicts with 'Ellipse Arc'?

Playing with the line command, I noticed that the 'close' function doesn't move relativezero. This may be by design, but I would prefer it to move to the first point.
Similarly, if you start another command while in the line command, and relativezero is moved, when you exit that command, relativezero isn't returned to its original position.
Could I suggest, as an alternative to using single letter shortcuts, that you add two letter shortcuts to the Close, Undo and Redo buttons in the options toolbar? My suggestions would be CL, UN and RD (RE is already used for rectangle).
Could I also suggest adding the check for the 'enter' or 'return' key being pressed, and If the current prompt is 'First point', then pressing enter or return causes the first point to be set to the last position in the drawing? Husky, at least, thought this was a good idea.
I noticed that you added the distance to 'Display Angle', which is good. However, adding the code to 'snap info' means that the display is always on, which as I said might be annoying at times. Would it be possible for me to add a toggle button to the 'restrict to angle or distance' options toolbar, and add the display code to 'RestrictAngleLength.js'?
Perhaps you could have a vote on whether or not users want this feature.
Finally, if the restriction is active when trying to select the first point, it can stop you selecting the exact point you want. (This is also the same for the 'orthogonal', 'horizontal' and 'vertical' restrictions). Now I know you can easily switch it off using the button, but would it be better to switch off restrictions while selecting the first point?

I would be quite willing to make these changes and send them to you. Perhaps you could let me know what you think?

Anyway thanks again for these great additions.

riverbuoy
riverbuoy
Full Member
 
Posts: 50
Joined: Thu Oct 03, 2013 5:37 pm

Re: Line2PEx - An improved line command?

Postby andrew » Mon Jun 23, 2014 11:08 pm

riverbuoy wrote:Your two new entities, Ray and XLine, are great, and combining them with the line command is an excellent idea.


Thanks.

(By the way, I was just about to post 'DrawPolylineEx', which has the same additions as 'Line2PEx'. :roll: ).


Sorry about that. I should have let you know that I'm working towards that direction.

Now, I don't want to rain on your parade, but has anyone pointed out that the shortcut EA conflicts with 'Ellipse Arc'?


Thanks for pointing that out. I've changed that to 'EG'.

Playing with the line command, I noticed that the 'close' function doesn't move relativezero. This may be by design, but I would prefer it to move to the first point.


Yes, makes sense. I've adjusted that as well.

Similarly, if you start another command while in the line command, and relativezero is moved, when you exit that command, relativezero isn't returned to its original position.


Yes, that is not the case in general. This would take some refactoring to restore the relative zero position when an action resumes. This should be the case for all actions or none to be consistent.

Could I suggest, as an alternative to using single letter shortcuts, that you add two letter shortcuts to the Close, Undo and Redo buttons in the options toolbar? My suggestions would be CL, UN and RD (RE is already used for rectangle).


Yes, why not. Can you try this to get a feel if that makes sense when working with the tool?
Another idea would be to use a fixed first character for all options toolbar tools. E.g. '.C' for close and '.U', '.R' or '/C', '/U', '/R' or something like that.

Could I also suggest adding the check for the 'enter' or 'return' key being pressed, and If the current prompt is 'First point', then pressing enter or return causes the first point to be set to the last position in the drawing? Husky, at least, thought this was a good idea.


OK, can you try to port this over from Line2PEx? I think I missed that.

I noticed that you added the distance to 'Display Angle', which is good. However, adding the code to 'snap info' means that the display is always on, which as I said might be annoying at times.


Yes..

Would it be possible for me to add a toggle button to the 'restrict to angle or distance' options toolbar, and add the display code to 'RestrictAngleLength.js'?


Yes, sure. I'm not sure if the display code can be move there easily though.

Perhaps you could have a vote on whether or not users want this feature.


I think either an application preference or a tool bar toggle button would be fine. Users could then provide feedback based on that.

Finally, if the restriction is active when trying to select the first point, it can stop you selecting the exact point you want. (This is also the same for the 'orthogonal', 'horizontal' and 'vertical' restrictions). Now I know you can easily switch it off using the button, but would it be better to switch off restrictions while selecting the first point?


That's one of those tricky questions. When starting a new tool, all restrictions are switched off automatically. I think switching on / off snaps or restrictions automatically while in process of using a tool might be confusing. I guess it boils down to the question of whether QCAD can predict what the user wants to do.

I would be quite willing to make these changes and send them to you. Perhaps you could let me know what you think?


Yes, sure, feel free to experiment with the other ideas and submit patches or feedback.

Please note that I've already fixed the keycode and the relative zero issue:
https://github.com/qcad/qcad/commit/c55 ... 26b0c82887
User avatar
andrew
Site Admin
 
Posts: 2755
Joined: Fri Mar 30, 2007 6:07 am

Re: Line2PEx - An improved line command?

Postby riverbuoy » Wed Jun 25, 2014 5:39 pm

Andrew wrote:
Another idea would be to use a fixed first character for all options toolbar tools. E.g. '.C' for close and '.U', '.R' or '/C', '/U', '/R' or something like that.


I had another look at the international keyboard layouts. The stroke character (/) requires the shift key to be pressed in quite a few layouts. The dot character (.) requires the shift key to be pressed in fewer layouts. However, the comma character (,) is directly accessible in all layouts except for some russian layouts. So I decided to use the comma character as the fixed first character for all option toolbar tools. So the shortcuts are ',C' for the close button, ',U' for undo, and ',R' for redo. I have attached the altered file 'Line2P.ui'.

I have also attached 'Line2P.js', which has the following changes.
If the current prompt is 'First point', then pressing enter or return causes the first point to be set to the last position in the drawing. This is in the 'keyPressEvent' function.
I also added the 'hideUiOptions' function. This stores the position of relativezero. Then I added code to 'showUiOptions' to restore the position of relativezero. To add this functionality to all commands, I would assume you could just change the default functions, 'hideUiOptions' and 'showUiOptions' and have them save and restore relativezero. I don't know if this is correct, or even easy.

Anyway, this now has the same functionality as Line2PEx, except for the displaying of angles. But displaying angles would be controlled by 'restrict to angle/length', which I haven't tried as yet.

Finally, I discovered that if you choose Line2P as the very first command you run on starting QCAD, and you enable the restrict to angle or length function, then you can't select a first point. This is because 'getRelativeZero()' returns an invalid point. This means relativezero is not initialised on startup. Setting it to point 0,0 would be useful.

Perhaps you can let me know what you think. Thanks.

riverbuoy
Attachments
Line2P.js
(10.82 KiB) Downloaded 51 times
Line2P.ui
(3.08 KiB) Downloaded 47 times
riverbuoy
Full Member
 
Posts: 50
Joined: Thu Oct 03, 2013 5:37 pm

Re: Line2PEx - An improved line command?

Postby andrew » Wed Jun 25, 2014 10:36 pm

riverbuoy wrote:So the shortcuts are ',C' for the close button, ',U' for undo, and ',R' for redo.


OK. Eventually, that first key for tools in the options tool bar could even be configurable to some extent.

I have attached the altered file 'Line2P.ui'.


Excellent, thanks!

If the current prompt is 'First point', then pressing enter or return causes the first point to be set to the last position in the drawing. This is in the 'keyPressEvent' function.


OK, this is merged and committed, in case you want to update to the latest version of the file:
https://raw.githubusercontent.com/qcad/ ... /Line2P.js

I also added the 'hideUiOptions' function. This stores the position of relativezero. Then I added code to 'showUiOptions' to restore the position of relativezero. To add this functionality to all commands, I would assume you could just change the default functions, 'hideUiOptions' and 'showUiOptions' and have them save and restore relativezero. I don't know if this is correct, or even easy.


You're absolutely right. This makes a lot of sense, so I've implemented that at a higher level in EAction. This means that all tools should save / restore their relative zero position after updating file EAction.js:
https://raw.githubusercontent.com/qcad/ ... EAction.js

Diffs of all changes applied in this context are at: https://github.com/qcad/qcad/commit/1bb ... fdaa7980b2

Finally, I discovered that if you choose Line2P as the very first command you run on starting QCAD, and you enable the restrict to angle or length function, then you can't select a first point. This is because 'getRelativeZero()' returns an invalid point. This means relativezero is not initialised on startup. Setting it to point 0,0 would be useful.


OK, this has also been fixed for the next release.

Great work, thanks!
User avatar
andrew
Site Admin
 
Posts: 2755
Joined: Fri Mar 30, 2007 6:07 am

Re: Line2PEx - An improved line command?

Postby riverbuoy » Thu Jun 26, 2014 7:55 pm

Andrew

I downloaded the latest Line2P.js and EAction.js, and they are working great. Except for one slight problem. If you draw some line segments, then use another command (Circle for example), and then exit from the command, relativezero returns to its original position as expected. If you now draw another one or more line segments and then select 'restrict to angle/length', relativezero is moved to where it was when you returned from the other command.
I added the following line:-
Code: Select all
 this.relativeZeroPos = undefined;

to the end of the resumeEvent function in EAction.js. This seems to have solved the problem, so I have attached the file EAction.js with the line added.

OK. Eventually, that first key for tools in the options tool bar could even be configurable to some extent.

I agree that it could be changed. It could be an option in 'Application Preferences' under 'Widgets->Toolbars'. However the shortcuts are in the ui file (xml format), and they would all have to be changed. Seems quite diffcult and time consuming unless ther is a better/easier way of doing this.

riverbuoy
Attachments
EAction.js
(43.28 KiB) Downloaded 49 times
riverbuoy
Full Member
 
Posts: 50
Joined: Thu Oct 03, 2013 5:37 pm

Re: Line2PEx - An improved line command?

Postby andrew » Fri Jun 27, 2014 3:17 pm

riverbuoy wrote:This seems to have solved the problem, so I have attached the file EAction.js with the line added.


Great, thanks for the fix!

It could be an option in 'Application Preferences' under 'Widgets->Toolbars'. However the shortcuts are in the ui file (xml format), and they would all have to be changed. Seems quite diffcult and time consuming unless ther is a better/easier way of doing this.


The key codes could be set / adjusted programmatically (QAction::shortcut, QAction::setShortcut), so it might be possible to simply replace the comma with something user configurable.
User avatar
andrew
Site Admin
 
Posts: 2755
Joined: Fri Mar 30, 2007 6:07 am

Re: Line2PEx - An improved line command?

Postby riverbuoy » Mon Jun 30, 2014 2:07 am

Andrew wrote:
The key codes could be set / adjusted programmatically (QAction::shortcut, QAction::setShortcut), so it might be possible to simply replace the comma with something user configurable.


Looking at EAction I discovered that the default ShowUiOptions function calls the 'initUiOptions' function. To test this I added the following function to Line2P.
Code: Select all
Line2P.prototype.initUiOptions = function(resume) {
    var prefixChar = ".";
    var optionsToolBar = EAction.getOptionsToolBar();

    var w = optionsToolBar.findChild("Close");
    if (!isNull(w)) {
        w.shortcut = new QKeySequence(prefixChar + ",C");
    }
    var w = optionsToolBar.findChild("Undo");
    if (!isNull(w)) {
        w.shortcut = new QKeySequence(prefixChar + ",U");
    }
    var w = optionsToolBar.findChild("Redo");
    if (!isNull(w)) {
        w.shortcut = new QKeySequence(prefixChar + ",R");
    }
};

This replaces the shortcut when the ui options are shown, but the ui file is not chaged. Obviously the prefix character would be set/stored somewhere in Application->Preferences. The disadvantage is each command that has toolbar options would need to write an 'initUiOptions' function if they wanted to allow the user to change the shortcuts.
Andrew, Is this the kind of thing you were thinking about?

riverbuoy
riverbuoy
Full Member
 
Posts: 50
Joined: Thu Oct 03, 2013 5:37 pm

Re: Line2PEx - An improved line command?

Postby andrew » Mon Jun 30, 2014 8:36 am

riverbuoy wrote:Andrew, Is this the kind of thing you were thinking about?


Yes, exactly!

riverbuoy wrote:The disadvantage is each command that has toolbar options would need to write an 'initUiOptions' function if they wanted to allow the user to change the shortcuts.


It should be possible to do that in EAction.prototype.initUiOptions by iterating though all children (optionsToolbar.children() ). Then one could check for each child if it has a keycode starting with a ',' and if so, replace it. What do you think?
User avatar
andrew
Site Admin
 
Posts: 2755
Joined: Fri Mar 30, 2007 6:07 am

Re: Line2PEx - An improved line command?

Postby caigner » Mon Jun 30, 2014 2:52 pm

I love it. So far it works fine for me.

I have just one suggestion to make:

It would be nice to also have the option of relative angle-increments.

What I mean is: When starting with a horizontal line and then the next line is 15° up, it would be nice, if the shown angle of the next line would also be 15° (thus relative to the previous line angle).

Thanks,
Christian
caigner
Active Member
 
Posts: 25
Joined: Wed Jan 30, 2013 5:24 pm

Re: Line2PEx - An improved line command?

Postby riverbuoy » Thu Jul 03, 2014 6:58 pm

Andrew wrote:
It should be possible to do that in EAction.prototype.initUiOptions by iterating though all children (optionsToolbar.children() ). Then one could check for each child if it has a keycode starting with a ',' and if so, replace it. What do you think?


Yes, I agree. This actually ocurred to me after I posted it. This is what I came up with.
Code: Select all
EAction.prototype.initUiOptions = function(resume) {
    var prefixChar = "/";

    var optionsToolBar = EAction.getOptionsToolBar();
    if (isNull(optionsToolBar)) {
        return;
    }

    for (var i in optionsToolBar.children()) {
        var child = optionsToolBar.children()[i];
        var shortCut = child.shortcut;
        if (!isNull(shortCut) && !shortCut.isEmpty()) {
            var count = shortCut.count();
            if (count < 2 || count > 4) {
                // greater than 4 is actually an error
                continue;
            }

            var str = shortCut.toString();
            var firstChar = str[0];
            if (firstChar === prefixChar) {
                // no change needed
                continue;
            }

            // 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);
                }
            }
        }
    }
};

Instead of checking if the first character of the shortcut is a comma, I check to see if it is a printable character, and if so, replace it. However, if there is a plus sign(+) in the shortcut, that would indicate that a modifier key is part of the shortcut (Ctrl, Alt, Shift etc), so it doesn't change it. As I commented, the plus(+) and minus(-) keys should not be used as prefix because they are one key shortcuts which zoom in and out.
If you define 'initUiOptions' in a command, that overrides this function. I tested this with 'Line2P'. So if you didn't want shortcuts to be overridden, then you would define the function in the command. It can even be empty.
So, Andrew, you need to decide if you want to implement this, and if so, where it should be in 'Application->Preferences' and what it should be called.
The obvious place for the pro version is in the 'Shortcuts and Commands' option. But in the source code version it would need to be elsewhere. I'm not sure that it would be a good idea to have the option in different places.

riverbuoy
riverbuoy
Full Member
 
Posts: 50
Joined: Thu Oct 03, 2013 5:37 pm

PreviousNext

Return to QCAD 3 ' script Add-On & Plug-in challenge '

Who is online

Users browsing this forum: Husky and 2 guests

cron