qute


Namequte JSON
Version 4.0.1 PyPI version JSON
download
home_pagehttps://github.com/mikemalinowski/qute
SummaryA Qt.py extension which aims to expose additional helper utilities to Qt which are common place within coding projects.
upload_time2024-09-17 18:07:39
maintainerNone
docs_urlNone
authorMike Malinowski
requires_pythonNone
licenseNone
keywords qute qt pyside pyside2 pyqt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            

#Overview

Qute is a wrapped extension of Marcus Ottosson's Qt.py. The emphasis is on
utilising the convience of Qt.py (allowing for use of PyQt, PySide and 
PySide2 seamlessly) whilst also exposing a set of common pieces of functionality
we tend to replicate and utilise in many places.


#Key Features

## General Usage

```python
import qute


class MyWidget(qute.QWidget):

    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent=parent)
        
        # -- Create a layout and set it as the base layout. Use 
        # -- qute to slim the layout - removing margins
        self.setLayout(
            qute.utilities.layouts.slimify(qute.QVBoxLayout())
        )
        
        # -- Create some widgets
        self.spinner = qute.QSpinBox()
        self.checker = qute.QCheckBox()
        
        # -- Add these to our layout
        self.layout().addWidget(self.spinner)
        self.layout().addWidget(self.checker)
        
        # -- Finally lets connect some signals and slots without
        # -- caring what it is
        qute.connectBlind(self.spinner, self.do_something)
        qute.connectBlind(self.checker, self.do_something)
        
    def do_something(self, *args, **kwargs):
        print('doing something...')

if __name__ == '__main__':

    # -- Use qute to get or create the QApplication instance
    q_app = qute.utilities.qApp()
    
    widget = MyWidget()
    widget.show()
    
    q_app.exec_()
```
In this example we see some of the features of qute in use, but most importantly is that it is usable in environments using either PyQt, PySide or PySide2 (thanks to Qt.py), and then utilises the various helper functionality defined within qute which you can read about below.


# Cross Application Support

This library is specifically intended for use when in environments where
you're actively trying to share/develop tools across multiple applications
which support PyQt, PySide or PySide2. 

The premise is that you can request the main application window using 
a common function regardless of the actual application - making it trivial
to implement a tool which works in multiple host applications without any
bespoke code.

The current list of supported applications are:

    * Native Python
    * Maya
    * 3dsmax
    * Motion Builder

Here is an example:

```python
import qute

class MyCrossApplicationTool(qute.QWidget):

    def __init__(self, parent=None):
        super(MyCrossApplicationTool, self).__init__(parent=parent)

        self.setLayout(qute.QVBoxLayout())
        self.layout().addWidget(qute.QLabel('This tool will launch and parent under Max, Maya, Motion Builder or Pure Python'))


# ------------------------------------------------------------------------------
def launch(blocking=False, *args, **kwargs):

    # -- This will return the running QApplication instance, or create
    # -- one if one is not present
    q_app = qute.qApp()

    # -- Create a window and set its parent 'blindly' to what qute
    # -- resolves as the main window.
    window = qute.QMainWindow(parent=qute.utilities.windows.mainWindow())

    # -- Assign our widget to the window
    window.setCentralWidget(MyCrossApplicationTool(*args, **kwargs))

    window.show()

    if blocking:
        q_app.exec_()

launch()
```

In the example above, we have a (somewhat simple!) tool, and we expose the
tool through a launch function which is creating a main window. The crucial
part is that the window is asking Qute to return the main application window
rather than you relying on an application specific Ui.

In doing this, you can copy/paste the code from the example into Max, Maya or
Motion Builder and you will get the same widget, and that widget will be 
correctly parented under that application, making your Ui incredibly portably
and re-usable without an application specific layer.


## Styling

Qute gives a convience function for applying stylesheets to Qt widgets. Crucually it also exposes a mechanism allowing you do define variables to be replaced within stylesheets. This helps when wanting to use the same values multiple times across a stylesheet.

For example, if we have a stylesheet such as:

```css
QWidget {
    background-color: rgb(BG_COLOR);
    color: rgb(TEXT_COLOR);
}

QLabel {
    padding-top: 7px;
    padding-bottom: 7px;
    background-color: transparent;
    color: rgb(TEXT_COLOR);
}
```

This can be assigned to a widget using:

```python
import qute

qute.utilities.styling.apply(
    css_str,
    apply_to=widget,
    BG_COLOR='50, 50, 50',
    TEXT_COLOR='255, 0, 0',
)
```
In this example we pass a CSS string and the widget we want to apply to. Any additional keywords will be used as search and replace elements. This is handy when wanting to change sections of your stylesheet easily. Your replacements can be numbers, strings or filepaths (just ensure your slashing is / and not \\). The ```space``` example stylesheet demonstrates this by using png files for widget backgrounds.

Equally, you can pass the full path to a css/qss file too:

```python
qute.utilities.styling.apply(
    '/usr/styles/my_style.qss',
    widget,
)
```

Alternatively you can have a library of style sheets and set the environment variable `QUTE_STYLE_PATH` to that location. By doing this you can pass the name of the style rather than the absolute path. Qute comes with one example stylesheet called `space` which can be used to demonstrate this as below:
```python
qute.utilities.styling.apply(
    'space',
    widget,
)   
```

This is an example of the space stylesheet:

![alt text](https://github.com/mikemalinowski/qute/blob/master/docs/space_demo.png?raw=true)



## Menu Generation

Generating menu's can be tedious and involve a lot of repetative code. In many cases a menu is made up of either actions, sseperators or sub-menus. 

Each of these are supported by the menu generation function ```qute.utilities.menus.menuFromDictionary```. The format of the dictionary you provide must conform to:

`{'Label': function}` or `{'Label': dict}` or `{'Label': None}`

If a function is given then the function is set as the callable when the item is clicked. If a dictionary is given as the value a submenu is generated (this is recusive, so you can nest menus). If the value is None then a Seperator will be added regardless of the key.

Here is an example:

```python
import qute

def foo():
    print('calling foo')

def bar():
    print('calling bar')

menu_definition = {
    'Foo': foo,
    '-': None,
    'More': dict(bar=bar)
}

menu = qute.utilities.menus.menuFromDictionary(menu_definition)
```

In this example we define some functions and add them as keys, we can then generate a QMenu from that dictionary. This is especially useful when you're dynamically generating menu from variable data.

You can also define icons for your menu. To utilise this mechanism your icons must have the same name as the label and end in .png. You can then define the path to the icons during the menu call as shown here:

```python
menu = qute.utilities.menus.menuFromDictionary(
    structure=menu_definition,
    icon_paths=[
        os.path.dirname(__file__),
    ]
)
```


## Derive

Derive is all about dynamically generating ui elements based on data types and being able to extract values from widgets without having to know what they are. This is particularly useful when generating ui elements on the fly without knowing what they are up front.

A good example of this is the exposure of options or attributes on a class without knowing exactly what those options are. We can see an example of that here:

```python
import qute

class Node:
    """
    Define a base class for something
    """
    
    def __init__(self):
        self.options=dict()

class Circle(Node):

    def __init__(self):
        self.options['radius'] = 5
        self.options['closed'] = True
   
class Quadtrilateral(Node):

    def __init__(self):
        self.options['force_rectangle']

def example_callback(*args, **kwargs):
    print('In Callback')
    
nodes = [
    Circle(),
    Quadtrilateral(),
    Quadtrilateral(),
    Circle(),
]

for node in nodes:
    for option, value in node.options:
    
        # -- Blindly create a widget to represent the widget
        widget = qute.utilities.derive.deriveWidget(
        value=value,
        label=option,
        )
        
        # -- Connect the change event of the widget
        # -- to our callback - without knowing what 
        # -- the widget is or what to connect
        qute.utilities.derive.connectBlind(widget, example_callback)
```

We can also ask for the value from a widget without knowing what the widget is. This can be done using:

```python
import qute

value = qute.utilities.derive.deriveValue(widget)
```

This mechanism makes it easier to create dynamic ui's, especially when you're trying to expose data which can be manipulated on code objects.



## Compatability

This has been tested under Python 2.7.13 and Python 3.6.6 under both Windows and Ubuntu.


## Contribute

If you would like to contribute thoughts, ideas, fixes or features please get in touch! mike@twisted.space


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mikemalinowski/qute",
    "name": "qute",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "qute qt pyside pyside2 pyqt",
    "author": "Mike Malinowski",
    "author_email": "mike.malinowski@outlook.com",
    "download_url": "https://files.pythonhosted.org/packages/92/98/1fafde34f78fcb7611ae5820f80680fe248409c8c598adadadfb656b1859/qute-4.0.1.tar.gz",
    "platform": null,
    "description": "\r\n\r\n#Overview\r\n\r\nQute is a wrapped extension of Marcus Ottosson's Qt.py. The emphasis is on\r\nutilising the convience of Qt.py (allowing for use of PyQt, PySide and \r\nPySide2 seamlessly) whilst also exposing a set of common pieces of functionality\r\nwe tend to replicate and utilise in many places.\r\n\r\n\r\n#Key Features\r\n\r\n## General Usage\r\n\r\n```python\r\nimport qute\r\n\r\n\r\nclass MyWidget(qute.QWidget):\r\n\r\n    def __init__(self, parent=None):\r\n        super(MyWidget, self).__init__(parent=parent)\r\n        \r\n        # -- Create a layout and set it as the base layout. Use \r\n        # -- qute to slim the layout - removing margins\r\n        self.setLayout(\r\n            qute.utilities.layouts.slimify(qute.QVBoxLayout())\r\n        )\r\n        \r\n        # -- Create some widgets\r\n        self.spinner = qute.QSpinBox()\r\n        self.checker = qute.QCheckBox()\r\n        \r\n        # -- Add these to our layout\r\n        self.layout().addWidget(self.spinner)\r\n        self.layout().addWidget(self.checker)\r\n        \r\n        # -- Finally lets connect some signals and slots without\r\n        # -- caring what it is\r\n        qute.connectBlind(self.spinner, self.do_something)\r\n        qute.connectBlind(self.checker, self.do_something)\r\n        \r\n    def do_something(self, *args, **kwargs):\r\n        print('doing something...')\r\n\r\nif __name__ == '__main__':\r\n\r\n    # -- Use qute to get or create the QApplication instance\r\n    q_app = qute.utilities.qApp()\r\n    \r\n    widget = MyWidget()\r\n    widget.show()\r\n    \r\n    q_app.exec_()\r\n```\r\nIn this example we see some of the features of qute in use, but most importantly is that it is usable in environments using either PyQt, PySide or PySide2 (thanks to Qt.py), and then utilises the various helper functionality defined within qute which you can read about below.\r\n\r\n\r\n# Cross Application Support\r\n\r\nThis library is specifically intended for use when in environments where\r\nyou're actively trying to share/develop tools across multiple applications\r\nwhich support PyQt, PySide or PySide2. \r\n\r\nThe premise is that you can request the main application window using \r\na common function regardless of the actual application - making it trivial\r\nto implement a tool which works in multiple host applications without any\r\nbespoke code.\r\n\r\nThe current list of supported applications are:\r\n\r\n    * Native Python\r\n    * Maya\r\n    * 3dsmax\r\n    * Motion Builder\r\n\r\nHere is an example:\r\n\r\n```python\r\nimport qute\r\n\r\nclass MyCrossApplicationTool(qute.QWidget):\r\n\r\n    def __init__(self, parent=None):\r\n        super(MyCrossApplicationTool, self).__init__(parent=parent)\r\n\r\n        self.setLayout(qute.QVBoxLayout())\r\n        self.layout().addWidget(qute.QLabel('This tool will launch and parent under Max, Maya, Motion Builder or Pure Python'))\r\n\r\n\r\n# ------------------------------------------------------------------------------\r\ndef launch(blocking=False, *args, **kwargs):\r\n\r\n    # -- This will return the running QApplication instance, or create\r\n    # -- one if one is not present\r\n    q_app = qute.qApp()\r\n\r\n    # -- Create a window and set its parent 'blindly' to what qute\r\n    # -- resolves as the main window.\r\n    window = qute.QMainWindow(parent=qute.utilities.windows.mainWindow())\r\n\r\n    # -- Assign our widget to the window\r\n    window.setCentralWidget(MyCrossApplicationTool(*args, **kwargs))\r\n\r\n    window.show()\r\n\r\n    if blocking:\r\n        q_app.exec_()\r\n\r\nlaunch()\r\n```\r\n\r\nIn the example above, we have a (somewhat simple!) tool, and we expose the\r\ntool through a launch function which is creating a main window. The crucial\r\npart is that the window is asking Qute to return the main application window\r\nrather than you relying on an application specific Ui.\r\n\r\nIn doing this, you can copy/paste the code from the example into Max, Maya or\r\nMotion Builder and you will get the same widget, and that widget will be \r\ncorrectly parented under that application, making your Ui incredibly portably\r\nand re-usable without an application specific layer.\r\n\r\n\r\n## Styling\r\n\r\nQute gives a convience function for applying stylesheets to Qt widgets. Crucually it also exposes a mechanism allowing you do define variables to be replaced within stylesheets. This helps when wanting to use the same values multiple times across a stylesheet.\r\n\r\nFor example, if we have a stylesheet such as:\r\n\r\n```css\r\nQWidget {\r\n    background-color: rgb(BG_COLOR);\r\n    color: rgb(TEXT_COLOR);\r\n}\r\n\r\nQLabel {\r\n    padding-top: 7px;\r\n    padding-bottom: 7px;\r\n    background-color: transparent;\r\n    color: rgb(TEXT_COLOR);\r\n}\r\n```\r\n\r\nThis can be assigned to a widget using:\r\n\r\n```python\r\nimport qute\r\n\r\nqute.utilities.styling.apply(\r\n    css_str,\r\n    apply_to=widget,\r\n    BG_COLOR='50, 50, 50',\r\n    TEXT_COLOR='255, 0, 0',\r\n)\r\n```\r\nIn this example we pass a CSS string and the widget we want to apply to. Any additional keywords will be used as search and replace elements. This is handy when wanting to change sections of your stylesheet easily. Your replacements can be numbers, strings or filepaths (just ensure your slashing is / and not \\\\). The ```space``` example stylesheet demonstrates this by using png files for widget backgrounds.\r\n\r\nEqually, you can pass the full path to a css/qss file too:\r\n\r\n```python\r\nqute.utilities.styling.apply(\r\n    '/usr/styles/my_style.qss',\r\n    widget,\r\n)\r\n```\r\n\r\nAlternatively you can have a library of style sheets and set the environment variable `QUTE_STYLE_PATH` to that location. By doing this you can pass the name of the style rather than the absolute path. Qute comes with one example stylesheet called `space` which can be used to demonstrate this as below:\r\n```python\r\nqute.utilities.styling.apply(\r\n    'space',\r\n    widget,\r\n)   \r\n```\r\n\r\nThis is an example of the space stylesheet:\r\n\r\n![alt text](https://github.com/mikemalinowski/qute/blob/master/docs/space_demo.png?raw=true)\r\n\r\n\r\n\r\n## Menu Generation\r\n\r\nGenerating menu's can be tedious and involve a lot of repetative code. In many cases a menu is made up of either actions, sseperators or sub-menus. \r\n\r\nEach of these are supported by the menu generation function ```qute.utilities.menus.menuFromDictionary```. The format of the dictionary you provide must conform to:\r\n\r\n`{'Label': function}` or `{'Label': dict}` or `{'Label': None}`\r\n\r\nIf a function is given then the function is set as the callable when the item is clicked. If a dictionary is given as the value a submenu is generated (this is recusive, so you can nest menus). If the value is None then a Seperator will be added regardless of the key.\r\n\r\nHere is an example:\r\n\r\n```python\r\nimport qute\r\n\r\ndef foo():\r\n    print('calling foo')\r\n\r\ndef bar():\r\n    print('calling bar')\r\n\r\nmenu_definition = {\r\n    'Foo': foo,\r\n    '-': None,\r\n    'More': dict(bar=bar)\r\n}\r\n\r\nmenu = qute.utilities.menus.menuFromDictionary(menu_definition)\r\n```\r\n\r\nIn this example we define some functions and add them as keys, we can then generate a QMenu from that dictionary. This is especially useful when you're dynamically generating menu from variable data.\r\n\r\nYou can also define icons for your menu. To utilise this mechanism your icons must have the same name as the label and end in .png. You can then define the path to the icons during the menu call as shown here:\r\n\r\n```python\r\nmenu = qute.utilities.menus.menuFromDictionary(\r\n    structure=menu_definition,\r\n    icon_paths=[\r\n        os.path.dirname(__file__),\r\n    ]\r\n)\r\n```\r\n\r\n\r\n## Derive\r\n\r\nDerive is all about dynamically generating ui elements based on data types and being able to extract values from widgets without having to know what they are. This is particularly useful when generating ui elements on the fly without knowing what they are up front.\r\n\r\nA good example of this is the exposure of options or attributes on a class without knowing exactly what those options are. We can see an example of that here:\r\n\r\n```python\r\nimport qute\r\n\r\nclass Node:\r\n    \"\"\"\r\n    Define a base class for something\r\n    \"\"\"\r\n    \r\n    def __init__(self):\r\n        self.options=dict()\r\n\r\nclass Circle(Node):\r\n\r\n    def __init__(self):\r\n        self.options['radius'] = 5\r\n        self.options['closed'] = True\r\n   \r\nclass Quadtrilateral(Node):\r\n\r\n    def __init__(self):\r\n        self.options['force_rectangle']\r\n\r\ndef example_callback(*args, **kwargs):\r\n    print('In Callback')\r\n    \r\nnodes = [\r\n    Circle(),\r\n    Quadtrilateral(),\r\n    Quadtrilateral(),\r\n    Circle(),\r\n]\r\n\r\nfor node in nodes:\r\n    for option, value in node.options:\r\n    \r\n        # -- Blindly create a widget to represent the widget\r\n        widget = qute.utilities.derive.deriveWidget(\r\n        value=value,\r\n        label=option,\r\n        )\r\n        \r\n        # -- Connect the change event of the widget\r\n        # -- to our callback - without knowing what \r\n        # -- the widget is or what to connect\r\n        qute.utilities.derive.connectBlind(widget, example_callback)\r\n```\r\n\r\nWe can also ask for the value from a widget without knowing what the widget is. This can be done using:\r\n\r\n```python\r\nimport qute\r\n\r\nvalue = qute.utilities.derive.deriveValue(widget)\r\n```\r\n\r\nThis mechanism makes it easier to create dynamic ui's, especially when you're trying to expose data which can be manipulated on code objects.\r\n\r\n\r\n\r\n## Compatability\r\n\r\nThis has been tested under Python 2.7.13 and Python 3.6.6 under both Windows and Ubuntu.\r\n\r\n\r\n## Contribute\r\n\r\nIf you would like to contribute thoughts, ideas, fixes or features please get in touch! mike@twisted.space\r\n\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Qt.py extension which aims to expose additional helper utilities to Qt which are common place within coding projects.",
    "version": "4.0.1",
    "project_urls": {
        "Homepage": "https://github.com/mikemalinowski/qute"
    },
    "split_keywords": [
        "qute",
        "qt",
        "pyside",
        "pyside2",
        "pyqt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92981fafde34f78fcb7611ae5820f80680fe248409c8c598adadadfb656b1859",
                "md5": "933c6b837d807c6c65bb50b736e9b740",
                "sha256": "35174860dd344cf763cd66e028666cd7d25f03f32049e6342cde5b235471ce07"
            },
            "downloads": -1,
            "filename": "qute-4.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "933c6b837d807c6c65bb50b736e9b740",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 55517,
            "upload_time": "2024-09-17T18:07:39",
            "upload_time_iso_8601": "2024-09-17T18:07:39.973627Z",
            "url": "https://files.pythonhosted.org/packages/92/98/1fafde34f78fcb7611ae5820f80680fe248409c8c598adadadfb656b1859/qute-4.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-17 18:07:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mikemalinowski",
    "github_project": "qute",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "qute"
}
        
Elapsed time: 0.34530s