Improve mouse logic for measure tool. Fix for #4628, #10029