Need some guidance to make a colour sampler plugin

Hi!

I’m trying to figure is there a way to make a plugin to implement additional colour sampler behavior and I’m kinda stuck.

The idea itself is: When a button on a docker is pressed the script activates colour sampler tool and when a user has clicked(and released a mouse/stylus) the script would switch to brush tool. This is needed to implement the color picker behaviour on a tablet, kinda akin to how Procreate do it.

As I understand there are kinda to ways to do it: one is to listen to mouse events and the other one is to somehow hook to the corresponding event like maybe some .changed() signal or something like that.

But since the documentation is all around the place I’m having a hard time understand where can I look for corresponding methods/signals/events and more importantly how to use them in this particular way.

(ideally it would be great just to have a checkbox in a tool itself but that’s for another forum topic I guess)

Thanks in advance!

The canvas is a Qt object.
I would start with getting access to it and catching mouse input with qt events.

Another option is to make a script that activates the color picker tool for limited amount of time, like 1.5 seconds and then activates your second tool.

Not ideal, but could save the trouble of handling events and all of that.

1 Like

oh! this sound like a very promising lead indeed! Thanks a lot!

I’ve been striking my head against the wall for two days already trying to get the ends of how and where to hook into all this qt-to-krita-native labyrinth and what will work and what’s not and docs wasn’t very helpful with that :frowning:

Indeed. That is why Python Development Tools plugin can come in handy.

1 Like

Here’s an excerpt of my plugin’s code to get the brush tool button.

from PyQt5.QtWidgets import QDockWidget, QToolButton

def loadTool(self):
        toolBox = Application.activeWindow().qwindow().findChild(QDockWidget, 'ToolBox')
        self.brushTool = toolBox.findChild(QToolButton, 'KritaShape/KisToolBrush')

The buttons are QToolButton class so you can use isChecked() or pressed() etc depending on your needs.

To find the names of the tool button objects for use with toolBox.findChild(), you can paste this onto the scripter and run it.

from PyQt5.QtWidgets import QDockWidget, QToolButton

toolBox = Application.activeWindow().qwindow().findChild(QDockWidget, 'ToolBox')
buttons = toolBox.findChildren(QToolButton)
for button in buttons:
  print(button.objectName())

I forgotten which plugin I learned this from, may be Buli Brush Switch? But you can always research the available plugins to see how others implement a similar function.

1 Like

this tool is a game changer for sure!

I’m using it right now and it’s super useful for figuring ins and outs of the api, I’m not sure atm it will help me with this particular plugin but it’s much more self-explanetary than qt docs, thanks a lot about it!

1 Like

a quick question about toolbox since we are on it: can I inject my own “tools” into it with my own plugin? (I haven’t tried it yet but maybe you know the answer already :slight_smile: )

Theoretically, Toolbox is a container (or a container for the container). And you can add a child Widget to it. So you will have to make a button dynamically and append it to toolbox. However, I am not sure how it will work.

You need to figure out what Kind of objects Krita uses for Tool Buttons. It might not be exposed to Python API.

If you considering paid options the Actuator plugin is something you can use to make your own tool panel.

1 Like

This plugin looks like an interesting thing to consider, I’ll look into it for sure!

Thanks for many useful insights, you’ve been super helpful!

1 Like

Just in case anyone stumble upon this thread:

I’ve finally got the plugin to work the way I wanted to and intended.

The idea from the start was to emulate(kinda) Procreate color picker behaviour on touch devices: Once you’ve clicked Colour Picker Icon your tool become, well, colour picker until you release the button(mouse or tablet) and then the tool would automatically switch to Brush Tool. It saves a lot of clicks and streamlines painting process on devices without a keyboard available with only touch controls at hand.

After trying several different methods to tackle the problem(including trying to connect to the colour indicator in a toolbar which failed because while it has a signal called “foregroundColorChanged” the signal itself is reserved not to colour change but for choosing a color from a pop-up window) I’ve found a way to get mouse events: QT has a special reserved method called EventFilter that can catch any QEvent(these include all mouse/tablet/keyboard events obviously) and it’s pretty trivial to attach such filters to any existing QObject. The only trick was to get the right window since events are restricted to the current widget. BUT! It’s a hierarchical thing i.e. these are working backwards up to the top parent. So if you want to catch mouse events application wide you attach your EventFilter to the top window(that you get from krita.instance.window() ) and you can catch any input events without much hastle.

You can find the one-button-docker implementation of this here: GitHub - mankysee/OneTimeColorSamplerTool: One Time Color Sampler Docker Tool for Krita

Or the same thing but in a docker that I am actually using for drawing on a tablet here: GitHub - mankysee/tabui_v2: A quick dirty shortcut launcher docker plugin for Krita

And thanks all who helped me in this thread your help was indispensable! I wouldn’t plow through without your inputs, thanks a lot!

2 Likes

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.