piksel / pytouch-cube

Qt5 Label Editor for Brother P-Touch Cube label maker
MIT License
48 stars 9 forks source link

Add cli #31

Closed tyalie closed 2 years ago

tyalie commented 2 years ago

Integrated a fully fledged CLI that will not start the GUI. One can configure special parameters like chained printing with it. Either print a previously generated p3label project file or completely design your own label (with a few limitations)

Starting GUI can be done by either pytouch3 gui or just normal pytouch3

Limitations to designing own file:

  1. cannot specify margin
  2. fonts are scaled to center in the label - cannot declare font size
  3. otherwise everything should work
usage: pytouch3.py [-h] {gui,print} ...

Utility to print to the Brother P-Touch Cube system

options:
  -h, --help   show this help message and exit

Runtime mode:
  {gui,print}
    gui        Use the utlity in GUI mode
    print      Print using suplied arguments

### python3 pytouch3.py gui --help
options:
  -h, --help  show this help message and exit
  --seed      Setup the GUI with example printables

### python3 pytouch3.py print --help
usage: pytouch3.py print [-h] [--mirror-printing] [--auto-tape-cut]
                         [--half-cut] [--chain-print] [--label-end-cut]
                         [--high-res-print] [--clear-buf]
                         [--margin LABELMAKERCONFIG|MARGIN]
                         [--default-font DEFAULT_FONT] [--device DEVICE]
                         [--output OUTPUT]
                         {label,file} ...

options:
  -h, --help            show this help message and exit

config:
  --mirror-printing
  --auto-tape-cut
  --half-cut
  --chain-print
  --label-end-cut
  --high-res-print
  --clear-buf
  --margin LABELMAKERCONFIG|MARGIN
  --default-font DEFAULT_FONT
  --device DEVICE
  --output OUTPUT       don't print just write the output to a png

Print mode:
  {label,file}
    label               Configure printables. Order is preserved
    file                Open predefined label file that was generate from
                        the UI

### pytouch3 print label --help
usage: pytouch3.py print label [-h] [-t TEXT] [-q QR_CODE] [-s SPACING]
                               [-b BARCODE] [-l LABELED_BARCODE]
                               [-i IMAGE]

options:
  -h, --help            show this help message and exit
  -t TEXT, --text TEXT  Text to write. Font is taken from --default-font
  -q QR_CODE, --qr-code QR_CODE
                        Argument is the qr-code data
  -s SPACING, --spacing SPACING
                        Adds spacing. Argument should be int
  -b BARCODE, --barcode BARCODE
                        Add barcode delimited with ':' in the format
                        DATA:BARCODE_TYPE
  -l LABELED_BARCODE, --labeled-barcode LABELED_BARCODE
                        Add barcode with label. See --barcode
  -i IMAGE, --image IMAGE
                        Adds an image with format IMAGE_PATH:THRESHOLD.
                        Where threshold is an int

### pytouch print file --help
usage: pytouch3.py print file [-h] label_file_name

positional arguments:
  label_file_name

options:
  -h, --help       show this help message and exit
piksel commented 2 years ago

Hm, great idea, but I think we should use different entry .py files. Or perhaps we can generate two binaries anyway using pyinstaller/py2exe/py2app. It would also be nice if the CLI could run entirely without QT, but I guess that won't be possible unless the image processing is moved to PIL.

tyalie commented 2 years ago

hmh. I like that the CLI is wrapped in the same bianry. It's not uncommon for GUI applications (see firefox, inkscape, gimp, ...)

Regarding the installer scripts: I haven't really looked into the installer scripts at all yet. I feel like that right now they will not work under Linux 😅

I also thought about whether the CLI could run completely without Qt but I feel like that one would loose a lot of shared code base and possibly diverging behaviour in rendering. Moving everything to Pillow is possible, but it feels a bit hmh. Especially in regards to fonts Qt is presumably far more advanced. The current solution is a bit hacky on some parts I admit thoo.

piksel commented 2 years ago

It's not uncommon for GUI applications (see firefox, inkscape, gimp, ...)

Yeah, but all of them open the GUI when started without args. That would be fine as well.

Especially in regards to fonts Qt is presumably far more advanced. The current solution is a bit hacky on some parts I admit thoo.

Indeed, it's always been a bit of a hack and only worked on enough to be able to print what I needed at the moment. I even started rewriting the application for the web: https://dev.p1k.se/pytouch-cube/

tyalie commented 2 years ago

Yeah, but all of them open the GUI when started without args. That would be fine as well.

That the application starts without GUI parameter given should already be implemented ^^

You can explicitly specify the gui mode if you wanna send arguments to it (e.g. seeding). but if you leave it blank it works exactly as before.

Indeed, it's always been a bit of a hack

Tbqh I actually like your code. It doesn't feel hacky on many parts. Especially the rendering I honestly quite like. I think what I really do like about Qt here is that they can work with fonts extremely well. In the CLI you cannot align the font, but with some calculations in the background it is now so that a normal upper case letter is centered on the label. I don't know how hard that would have been with other tools. 😮‍💨

piksel commented 2 years ago

That the application starts without GUI parameter given should already be implemented ^^

Yeah, I just noticed that in the description. I'll take a closer look at this tomorrow.

piksel commented 2 years ago

Thoughts for expansion upon this PR later:

tyalie commented 2 years ago

Uh the seed command is a nice idea. But yeah it would require a fully featured CLI. I personally thought rather about maybe being able to add variables to the stored project file in the GUI and be able to replace those in the CLI later on.

The "automatic" font size should be selectable in the GUI as well, and probably be the default.

Yeah that would probably be nice tbqh.

The thing here is thoo that it should be implemented during the rendering of the fonts themselves. Some fonts like Comic Code have a bit weird Ascend and Descent values resulting in a smaller than necessary automatic font sizing. When I was trying to adapt the calculation I noticed sometimes the lower part of the font was cut away as the ascent was large enough to push everything to far down at a sensible font size.

piksel commented 2 years ago

To be clear, I only meant the -t Hello -i image.png:34 -t world arguments. They would still need to support more stuff, like setting font and font-size (optionally), but I don't think that would be too difficult to add. I might take a stab at it if you don't oppose the idea.

tyalie commented 2 years ago

No I like it. I initially decided to not implement them (yet) as it would have required to write custom types for argparse, but along the way I implemented the FILENAME:THRESHOLD syntax and did exactly that 🙈