• Stars
    star
    127
  • Rank 281,133 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compile SASS files to Qt stylesheets

QtSASS: Compile SCSS files to Qt stylesheets

License - MIT OpenCollective Backers Join the chat at https://gitter.im/spyder-ide/public
Github build status Codecov coverage Scrutinizer Code Quality

Copyright © 2015 Yann Lanthony

Copyright © 2017–2018 Spyder Project Contributors

Overview

SASS brings countless amazing features to CSS. Besides being used in web development, CSS is also the way to stylize Qt-based desktop applications. However, Qt's CSS has a few variations that prevent the direct use of SASS compiler.

The purpose of this tool is to fill the gap between SASS and Qt-CSS by handling those variations.

Qt's CSS specificities

The goal of QtSASS is to be able to generate a Qt-CSS stylesheet based on a 100% valid SASS file. This is how it deals with Qt's specifics and how you should modify your CSS stylesheet to use QtSASS.

"!" in selectors

Qt allows to define the style of a widget according to its states, like this:

QLineEdit:enabled {
...
}

However, a "not" state is problematic because it introduces an exclamation mark in the selector's name, which is not valid SASS/CSS:

QLineEdit:!editable {
...
}

QtSASS allows "!" in selectors' names; the SASS file is preprocessed and any occurence of :! is replaced by :_qnot_ (for "Qt not"). However, using this feature prevents from having a 100% valid SASS file, so this support of ! might change in the future. This can be replaced by the direct use of the _qnot_ keyword in your SASS file:

QLineEdit:_qnot_editable { /* will generate QLineEdit:!editable { */
...
}

qlineargradient

The qlineargradient function also has a non-valid CSS syntax.

qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.1 blue, stop: 0.8 green)

To support qlineargradient QtSASS provides a preprocessor and a SASS implementation of the qlineargradient function. The above QSS syntax will be replaced with the following:

qlineargradient(0, 0, 0, 1, (0.1 blue, 0.8 green))

You may also use this syntax directly in your QtSASS.

qlineargradient(0, 0, 0, 1, (0.1 blue, 0.8 green))
# the stops parameter is a list, so you can also use variables:
$stops = 0.1 blue, 0.8 green
qlineargradient(0, 0, 0, 0, $stops)

qrgba

Qt's rgba:

rgba(255, 128, 128, 50%)

is replaced by CSS rgba:

rgba(255, 128, 128, 0.5)

Executable usage

To compile your SASS stylesheet to a Qt compliant CSS file:

# If -o is omitted, output will be printed to console
qtsass style.scss -o style.css

To use the watch mode and get your stylesheet auto recompiled on each file save:

# If -o is omitted, output will be print to console
qtsass style.scss -o style.css -w

To compile a directory containing SASS stylesheets to Qt compliant CSS files:

qtsass ./static/scss -o ./static/css

You can also use watch mode to watch the entire directory for changes.

qtsass ./static/scss -o ./static/css -w

Set the Environment Variable QTSASS_DEBUG to 1 or pass the --debug flag to enable logging.

qtsass ./static/scss -o ./static/css --debug

API methods

compile(string, **kwargs)

Conform and Compile QtSASS source code to CSS.

This function conforms QtSASS to valid SCSS before passing it to sass.compile. Any keyword arguments you provide will be combined with qtsass's default keyword arguments and passed to sass.compile.

Examples:

>>> import qtsass
>>> qtsass.compile("QWidget {background: rgb(0, 0, 0);}")
QWidget {background:black;}

Arguments:

  • string: QtSASS source code to conform and compile.
  • kwargs: Keyword arguments to pass to sass.compile

Returns:

  • Qt compliant CSS string

compile_filename(input_file, output_file=None, **kwargs):

Compile and return a QtSASS file as Qt compliant CSS. Optionally save to a file.

Examples:

>>> import qtsass
>>> qtsass.compile_filename("dummy.scss", "dummy.css")
>>> css = qtsass.compile_filename("dummy.scss")

Arguments:

  • input_file: Path to QtSass file.
  • output_file: Path to write Qt compliant CSS.
  • kwargs: Keyword arguments to pass to sass.compile

Returns:

  • Qt compliant CSS string

compile_dirname(input_dir, output_dir, **kwargs):

Compiles QtSASS files in a directory including subdirectories.

>>> import qtsass
>>> qtsass.compile_dirname("./scss", "./css")

Arguments:

  • input_dir: Path to directory containing QtSass files.
  • output_dir: Directory to write compiled Qt compliant CSS files to.
  • kwargs: Keyword arguments to pass to sass.compile

enable_logging(level=None, handler=None):

Enable logging for qtsass.

Sets the qtsass logger's level to: 1. the provided logging level 2. logging.DEBUG if the QTSASS_DEBUG envvar is a True value 3. logging.WARNING

>>> import logging
>>> import qtsass
>>> handler = logging.StreamHandler()
>>> formatter = logging.Formatter('%(level)-8s: %(name)s> %(message)s')
>>> handler.setFormatter(formatter)
>>> qtsass.enable_logging(level=logging.DEBUG, handler=handler)

Arguments:

  • level: Optional logging level
  • handler: Optional handler to add

watch(source, destination, compiler=None, Watcher=None):

Watches a source file or directory, compiling QtSass files when modified.

The compiler function defaults to compile_filename when source is a file and compile_dirname when source is a directory.

Arguments:

  • source: Path to source QtSass file or directory.
  • destination: Path to output css file or directory.
  • compiler: Compile function (optional)
  • Watcher: Defaults to qtsass.watchers.Watcher (optional)

Returns:

  • qtsass.watchers.Watcher instance

Contributing

Everyone is welcome to contribute!

Sponsors

Spyder and its subprojects are funded thanks to the generous support of

QuansightNumfocus

and the donations we have received from our users around the world through Open Collective:

Sponsors

Please consider becoming a sponsor!

More Repositories

1

spyder

Official repository for Spyder - The Scientific Python Development Environment
Python
8,151
star
2

qtpy

Provides an uniform layer to support PyQt5, PySide2, PyQt6, PySide6 with a single codebase
Python
963
star
3

qtawesome

Iconic fonts in PyQt and PySide applications
Python
786
star
4

spyder-notebook

Jupyter notebook integration with Spyder
Python
520
star
5

spyder-terminal

Run system terminals inside Spyder. Works on Linux, macOS and Windows.
Python
228
star
6

spyder-vim

A plugin for Spyder to enable Vim keybindings
Python
124
star
7

spyder-unittest

A plugin for Spyder to run tests and view the results
Python
79
star
8

spyder-reports

Spyder plugin to render Markdown reports using Pweave as a backend
Python
70
star
9

loghub

Changelog generator based on github milestones or tags.
Python
41
star
10

spyder-kernels

Jupyter Kernels for the Spyder console
Python
38
star
11

spyder-autopep8

A plugin to run the autopep8 python linter from within the spyder editor.
Python
35
star
12

spyder-docs

Documentation for Spyder, the Scientific Python Development Environment
Python
33
star
13

spyder-line-profiler

This is a plugin to run the python line profiler from within the spyder editor.
Python
33
star
14

docrepr

Generate rich representations for docstrings
Python
26
star
15

website-spyder

Spyder IDE website sources. Built with Lektor.
JavaScript
23
star
16

conda-manager

Graphical conda package manager
Python
22
star
17

spyder-memory-profiler

This is a plugin to run the python memory_profiler from within the spyder editor.
Python
12
star
18

pysyntect

Python bindings for Syntect library
Rust
9
star
19

lektor-icon

A modern, single-page-focused theme for the Lektor static CMS
CSS
8
star
20

binder-environments

Binder environments to test pull requests in our organization
Python
8
star
21

spyder-vcs

Python
8
star
22

spyder-plugin-cookiecutter

Cookiecutter template for a Spyder IDE plugin (compatible with Spyder IDE versions 3.x and 4.x)
Python
7
star
23

mac-application

Instructions to build the native Spyder MacOS X application
Shell
7
star
24

three-merge

Simple Python library to perform a 3-way merge between strings
Python
7
star
25

langchain-provider

Completion provider using Langchain and OpenAI for Spyder 6+
Python
5
star
26

website-production

Spyder IDE deployed website.
HTML
5
star
27

Spyder-Workshop

Spyder Workshop
Python
5
star
28

plugin-examples

Plugin examples for the new Spyder 5 API
Python
5
star
29

gettext-helpers

Helper functions and scripts for gettext translation in Python
Python
4
star
30

envs-manager

A manager for Python environments and packages managers
Python
4
star
31

ux-improvements

Discussion about UX improvements for Spyder 5 and beyond
4
star
32

community

Community repository for Spyder IDE
3
star
33

governance-and-guidelines

Governance documents and formal guidelines for the Spyder project as a whole
3
star
34

spyder-remote-services

Python
3
star
35

spyder-env-manager

A plugin for Spyder 6+ to manage Python virtual environments and packages
Python
2
star
36

action-close-duplicate-issues

Github action to close duplicate issues with a custom message
TypeScript
1
star
37

spyder-website

Svelte
1
star
38

spyder5-plugin-cookiecutter

Cookiecutter for a Spyder 5 plugin
Python
1
star