This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Horizontal image flip, keeping cursor position

Tags: None
(comma "," separated)
User avatar
rksk16it
Registered Member
Posts
21
Karma
0
OS
I needed a modification to horizontal image flip, such that my drawing pointer stays at the same pixel after mirroring, so that I dont have to pan the image to go back to the position I was working on. Its not a problem if I zoom out enough, but it is when zoomed in.

I tried canvas mirror instead of flipping image, but it gives weird behavior. Sometimes it keeps the image at roughly same position (few pixels drift), but sometimes it drifts too far.

So I wrote a function (in python), which performs image flipping but keeps the pixel under the cursor. It does this by readjusting the horizontal scroll of the canvas. Posting the code of that function here in case someone finds it useful, or if someone can suggest a better method, or maybe something like this exists already and I overlooked it.

Code: Select all
CANVAS_GET_ZOOM_FIX = 3.0 / 12.5

def hmirror_maintain_cursor():
  krita = Krita.instance()
  action_mirror_horizontal = krita.action("mirrorImageHorizontal")
  doc = krita.activeDocument()
  window = krita.activeWindow()
  if window == None:
    return
  canvas = window.activeView().canvas()
  window = window.qwindow()
  controller = None
  if window.isActiveWindow():
    views = window.findChildren(QWidget, QRegExp("view_.*"))
    for view in views:
      if view.underMouse():
        controller = view.findChild(QAbstractScrollArea)
        break
  if controller == None:
    return

  hbar = controller.horizontalScrollBar()

  dwidth = doc.width()
  zoom = canvas.zoomLevel() * CANVAS_GET_ZOOM_FIX
  hval = hbar.value()
  cpos = hbar.mapFromGlobal(QCursor.pos()).x()
  imax = int(dwidth * zoom)
  imid = int(imax/2)

  delta = cpos + hval - imid

  action_mirror_horizontal.trigger()
  doc.waitForDone()

  hbar.setValue(hval - (2*delta))
yikescloud
Registered Member
Posts
4
Karma
0
Seems like krita changes its ui hierarchy, this hack didn't work anymore
User avatar
rksk16it
Registered Member
Posts
21
Karma
0
OS
yikescloud wrote:Seems like krita changes its ui hierarchy, this hack didn't work anymore


I just tested this, seems to work fine. Can you elaborate what went wrong ? Was there any error messages too ?
yikescloud
Registered Member
Posts
4
Karma
0
Thanks for your reply! ;D
I think I know where it goes wrong. I run the script in the krita internal scripter, so maybe the window is lose focus
Code: Select all
if window.isActiveWindow():
if view.underMouse():

Code break on these 2 lines, now I fix it, thanks!

By the way, i notice CANVAS_GET_ZOOM_FIX=3.0/12.5, is this magic number is a constant in krita source code? Seem zoom must multiply this number can return the right result.
User avatar
rksk16it
Registered Member
Posts
21
Karma
0
OS
yikescloud wrote:Thanks for your reply! ;D
I think I know where it goes wrong. I run the script in the krita internal scripter, so maybe the window is lose focus
Code: Select all
if window.isActiveWindow():
if view.underMouse():

Code break on these 2 lines, now I fix it, thanks!

By the way, i notice CANVAS_GET_ZOOM_FIX=3.0/12.5, is this magic number is a constant in krita source code? Seem zoom must multiply this number can return the right result.


Sorry, got caught up with something, so couldnt reply sooner.

Yeah, you are right on both accounts. This code only makes sense if that window is on focus, and that constant is a magic number. Without that constant I wasnt able to get zoom right, and it was always off by that factor. I didnt look into the source code, I just observed (by good old print statements lol) that the ratio of zoom i want vs zoom I am getting is fixed but not 1, or atleast thats what I hazily remember. I wrote this long time ago.


Bookmarks



Who is online

Registered users: bancha, Bing [Bot], Google [Bot], Sogou [Bot]