Bug 340399 - Allow setting output format for --getcolor
Summary: Allow setting output format for --getcolor
Status: RESOLVED FIXED
Alias: None
Product: kdialog
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR wishlist
Target Milestone: ---
Assignee: Brad Hards
URL:
Keywords: junior-jobs
Depends on:
Blocks:
 
Reported: 2014-10-27 13:24 UTC by Christoph Feck
Modified: 2017-03-10 22:09 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed In: 17.04


Attachments
KDialog patch (4.36 KB, patch)
2015-02-17 15:11 UTC, Alexander
Details
allow set color format (3.45 KB, patch)
2017-02-17 09:59 UTC, yuehangwu
Details
v2_kdialog_allow_set_output_color_format (2.93 KB, patch)
2017-02-26 14:31 UTC, yuehangwu
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Christoph Feck 2014-10-27 13:24:52 UTC
The selected color is always printed as "#RRGGBB" in hex.

A new printf-style "--format" option could allow different outputs. Default is "#%02X%02X%02X", but users might want to use "QColor(%d, %d, %d)" instead for coding purposes.

Bonus points if it detects floating point specifiers (%e, %f, %g) and converts to 0...1 range.
Comment 1 Alexander 2015-02-17 15:11:56 UTC
Created attachment 91130 [details]
KDialog patch

The patch kdialog.diff applies to the file kde-baseapps/kdialog/kdialog.cpp. Two color outputs are allowed now: hex (or default) and QColor(%d,%d,%d). It is possible to specify the %f key for converting color components to 0...1 range.
Comment 2 Christoph Feck 2016-10-14 08:30:30 UTC
The idea was to use the sprintf family of functions (not sure if there is a Qt equivalent), so that the user can use any (valid) format string.
Comment 3 yuehangwu 2017-02-17 09:59:35 UTC
Created attachment 104072 [details]
allow set color format

use regx for strictly format check for (e, g, f, x, d) with specific precision.

e.g.
allowed: %3f%4f%5f, %2e%2e%4e
not allowed: %2e%2d%2e, %1f%1f%1f%1f
Comment 4 Christoph Feck 2017-02-22 23:47:16 UTC
Please read comment #2.
Comment 5 yuehangwu 2017-02-26 14:31:39 UTC
Created attachment 104233 [details]
v2_kdialog_allow_set_output_color_format

Hi Christoph,

New patch is based on QString's sprintf instead of cout concatenate in the previous patch and format specifier is more robost (e.g. "%.6f, %+2.f %3.3f") Please take a look, thx
Comment 6 Christoph Feck 2017-02-27 14:29:00 UTC
Thanks, that is already an improvement, but not ready yet.

I would have really preferred if you used our code review tools instead of adding the patch to this feature request, but I will spare you that for now and add my review comments here.

- The format should be extracted from a separate '--format' argument via the arguments parser. If other xdialog type command line tools use "--getcolor [format]" instead, please add a reference.

- QString::sprintf() is an obsolete member. Use QString::asprintf() or QString::vasprintf() instead.

- Format strings are expected to be UTF-8. Use .toUtf8().constData() instead of .toLocal8Bit().data()

- Your regexes do not parse '$' positional arguments (to reorder R,G,B), nor '%o', '%i', and '%u'. Flags '#' and '-' should be handled, too.

- Length specifiers such as 'h', 'll', 'L' etc. as well as variable specifiers '*' need to be stripped for safety. Otherwise it could reference wrong memory when reading the values from the stack.

- You are using contains(QRegularExpression("[dDxX]")) but _all_ your regexes contain 'd' for digits. Maybe use [0-9] explicitely.

- The format string can also contain ordinary characters before, between, and after the values, e.g. --format="QColor(%d, %d, %d)" or --format="#%02X%02X%02X". These need to be visible in the result.

- If possible, refactor the code to loop over number of arguments with a regexp for each of them. This is optional for now, and not required for passing review, but helps when alpha support is added to kdialog, which needs to handle 3 or 4 arguments for later RGB vs ARGB usage.

If you need a reference for printf-style format strings, please see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sprintf.html
Comment 7 yuehangwu 2017-02-27 16:47:03 UTC
Hi Christoph, firstly thanks for the above advice.

(In reply to Christoph Feck from comment #6)
> Thanks, that is already an improvement, but not ready yet.
> 
> I would have really preferred if you used our code review tools instead of
> adding the patch to this feature request, but I will spare you that for now
> and add my review comments here.
OK, is https://git.reviewboard.kde.org the right place?

> - Your regexes do not parse '$' positional arguments (to reorder R,G,B), nor
> '%o', '%i', and '%u'. Flags '#' and '-' should be handled, too.
> 
> - Length specifiers such as 'h', 'll', 'L' etc. as well as variable
> specifiers '*' need to be stripped for safety. Otherwise it could reference
> wrong memory when reading the values from the stack.
will revise the regx to suite more specifiers

> - You are using contains(QRegularExpression("[dDxX]")) but _all_ your
> regexes contain 'd' for digits. Maybe use [0-9] explicitely.
I think [d] is only matched when your specifier is char 'd' instead of \d. and when I feed "%0002d" to sprintf, I got warned. That's why I choose it as [1-9] for decimal case. right?

> - The format string can also contain ordinary characters before, between,
> and after the values, e.g. --format="QColor(%d, %d, %d)" or
> --format="#%02X%02X%02X". These need to be visible in the result.
OK, that means release "^" and "$" check for head and tail.
 
> - If possible, refactor the code to loop over number of arguments with a
> regexp for each of them. This is optional for now, and not required for
> passing review, but helps when alpha support is added to kdialog, which
> needs to handle 3 or 4 arguments for later RGB vs ARGB usage.
sounds like use "(%[1-9]+\\.?\\d*d|%d)" to loop over the arguments.
Comment 8 Christoph Feck 2017-03-01 03:06:53 UTC
> OK, is https://git.reviewboard.kde.org the right place?

Or Phabricator: https://phabricator.kde.org/differential/diff/create/
Comment 9 Christoph Feck 2017-03-10 22:09:11 UTC
Git commit c78683dde1641f26d2bbcff9f27493bf12dbb4b3 by Christoph Feck, on behalf of Yuehang Wu.
Committed on 10/03/2017 at 22:06.
Pushed by cfeck into branch 'master'.

Allow setting output format for --getcolor
FIXED-IN: 17.04

Differential Revision: https://phabricator.kde.org/D4966

M  +35   -3    src/kdialog.cpp

https://commits.kde.org/kdialog/c78683dde1641f26d2bbcff9f27493bf12dbb4b3