Add option to revert graphics colors in dark mode (#12076)

This amends [5d47a7ee57506/lyxgit]
This commit is contained in:
Juergen Spitzmueller 2021-01-18 10:46:16 +01:00
parent 6766ca2c99
commit 9a6f7ece97
8 changed files with 105 additions and 87 deletions

View File

@ -7,6 +7,10 @@ changes happened in particular if possible. A good example would be
-----------------------
2021-01-18 Jürgen Spitzmüller <spitz@lyx.org>
* Format incremented to 603: New InsetGraphics param darkModeSensitive
This advises LyX to revert colors in dark mode.
2021-01-17 Jürgen Spitzmüller <spitz@lyx.org>
* Format incremented to 602: Allow semantic branch colors
\color can now also take lyx names besides hexnames.

View File

@ -4067,6 +4067,25 @@ def revert_branch_colors(document):
i += 1
def revert_darkmode_graphics(document):
" Revert darkModeSensitive InsetGraphics param "
i = 0
while (True):
i = find_token(document.body, "\\begin_inset Graphics", i)
if i == -1:
break
j = find_end_of_inset(document.body, i)
if j == -1:
document.warning("Can't find end of graphics inset at line %d!!" %(i))
i += 1
continue
k = find_token(document.body, "\tdarkModeSensitive", i, j)
if k != -1:
del document.body[k]
i += 1
##
# Conversion hub
#
@ -4130,10 +4149,12 @@ convert = [
[599, []],
[600, []],
[601, [convert_math_refs]],
[602, [convert_branch_colors]]
[602, [convert_branch_colors]],
[603, []]
]
revert = [[601, [revert_branch_colors]],
revert = [[602, [revert_darkmode_graphics]],
[601, [revert_branch_colors]],
[600, []],
[599, [revert_math_refs]],
[598, [revert_hrquotes]],

View File

@ -127,7 +127,7 @@ public:
/// draw an image from the image cache
virtual void image(int x, int y, int w, int h,
graphics::Image const & image, bool revert_in_darkmode = false) = 0;
graphics::Image const & image, bool const revert_in_darkmode = false) = 0;
/// draw a string at position x, y (y is the baseline).
virtual void text(int x, int y, docstring const & str, FontInfo const & f) = 0;

View File

@ -194,14 +194,8 @@ GuiGraphics::GuiGraphics(GuiView & lv)
this, SLOT(change_adaptor()));
connect(draftCB, SIGNAL(stateChanged(int)),
this, SLOT(change_adaptor()));
// FIXME: we should connect to clicked() when we move to Qt 4.2 because
// the toggled(bool) signal is also triggered when we update the widgets
// (rgh-4/07) this isn't as much or a problem as it was, because we're now
// using blockSignals() to keep from triggering that signal when we call
// setChecked(). Note, too, that clicked() would get called whenever it
// is clicked, even right clicked (I think), not just whenever it is
// toggled.
connect(displayGB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
connect(displayGB, SIGNAL(clicked(bool)), this, SLOT(change_adaptor()));
connect(darkModeCB, SIGNAL(clicked(bool)), this, SLOT(change_adaptor()));
connect(displayscale, SIGNAL(textChanged(const QString &)),
this, SLOT(change_adaptor()));
connect(groupCO, SIGNAL(currentIndexChanged(int)),
@ -223,6 +217,7 @@ GuiGraphics::GuiGraphics(GuiView & lv)
bc().addReadOnly(draftCB);
bc().addReadOnly(clip);
bc().addReadOnly(displayGB);
bc().addReadOnly(darkModeCB);
bc().addReadOnly(sizeGB);
bc().addReadOnly(rotationGB);
bc().addReadOnly(latexoptions);
@ -555,6 +550,7 @@ void GuiGraphics::paramsToDialog(InsetGraphicsParams const & params)
// Update the draft and clip mode
draftCB->setChecked(params.draft);
clip->setChecked(params.clip);
darkModeCB->setChecked(params.darkModeSensitive);
displayGB->setChecked(params.display);
displayscale->setText(toqstr(convert<string>(params.lyxscale)));
@ -678,6 +674,7 @@ void GuiGraphics::applyView()
igp.draft = draftCB->isChecked();
igp.clip = clip->isChecked();
igp.display = displayGB->isChecked();
igp.darkModeSensitive = darkModeCB->isChecked();
//the graphics section
if (scaleCB->isChecked() && !Scale->text().isEmpty()) {

View File

@ -234,7 +234,7 @@ void GuiPainter::arc(int x, int y, unsigned int w, unsigned int h,
void GuiPainter::image(int x, int y, int w, int h, graphics::Image const & i,
bool revert_in_darkmode)
bool const revert_in_darkmode)
{
graphics::GuiImage const & qlimage =
static_cast<graphics::GuiImage const &>(i);
@ -247,25 +247,7 @@ void GuiPainter::image(int x, int y, int w, int h, graphics::Image const & i,
QColor text_color = palette.color(QPalette::Active, QPalette::WindowText);
QColor bg_color = palette.color(QPalette::Active, QPalette::Window);
// guess whether we are in dark mode
bool const in_dark_mode = text_color.black() < bg_color.black();
// if we are in dark mode, check whether we have transparent pixels
if (in_dark_mode && !revert_in_darkmode) {
QImage img = image.convertToFormat(QImage::Format_ARGB32);
for (int x = 0 ; x < img.width() ; x++) {
if (revert_in_darkmode)
break;
for (int y = 0 ; y < img.height() ; y++) {
QRgb currentPixel = (img.pixel(x, y));
if (qAlpha(currentPixel) == 0) {
// we have transparent pixels, revert
// this image in dark mode (#12076)
revert_in_darkmode = true;
break;
}
}
}
}
if (in_dark_mode && revert_in_darkmode)
if (revert_in_darkmode && text_color.black() < bg_color.black())
// FIXME this is only a cheap approximation
// Ideally, replace colors as in GuiApplication::prepareForDarkmode()
image.invertPixels();

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>654</width>
<width>718</width>
<height>512</height>
</rect>
</property>
@ -508,45 +508,6 @@
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="1">
<widget class="QLineEdit" name="latexoptions">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Additional LaTeX options</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="latexoptionsLA">
<property name="toolTip">
<string>Additional LaTeX options</string>
</property>
<property name="text">
<string>LaTeX &amp;options:</string>
</property>
<property name="buddy">
<cstring>latexoptions</cstring>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>354</width>
<height>81</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0" colspan="3">
<widget class="QGroupBox" name="displayGB">
<property name="focusPolicy">
@ -622,9 +583,58 @@
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="darkModeCB">
<property name="toolTip">
<string>If LyX uses a dark theme, revert the colors of this graphics in the workarea</string>
</property>
<property name="text">
<string>Re&amp;vert colors in dark mode</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="5" column="0" colspan="3">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>354</width>
<height>81</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="latexoptionsLA">
<property name="toolTip">
<string>Additional LaTeX options</string>
</property>
<property name="text">
<string>LaTeX &amp;options:</string>
</property>
<property name="buddy">
<cstring>latexoptions</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="latexoptions">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Additional LaTeX options</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QGroupBox" name="graphicsGroupGB">
<property name="toolTip">
@ -652,19 +662,6 @@
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="2" rowspan="2">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>121</width>
<height>51</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
@ -684,6 +681,19 @@
</property>
</widget>
</item>
<item row="0" column="2" rowspan="2">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>121</width>
<height>51</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="newGroupPB">
<property name="toolTip">

View File

@ -74,7 +74,7 @@ void InsetGraphicsParams::init()
bbox = graphics::BoundingBox(); // bounding box
clip = false; // clip image
darkModeSensitive = false; // dark mode dependency (InsetInfo)
darkModeSensitive = false; // dark mode dependency
rotateAngle = "0"; // angle of rotation in degrees
rotateOrigin.erase(); // Origin of rotation
@ -146,6 +146,8 @@ void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
os << "\tlyxscale " << lyxscale << '\n';
if (!display)
os << "\tdisplay false\n";
if (darkModeSensitive)
os << "\tdarkModeSensitive\n";
if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
if (!float_equal(convert<double>(scale), 100.0, 0.05))
os << "\tscale " << scale << '\n';
@ -195,6 +197,8 @@ bool InsetGraphicsParams::Read(Lexer & lex, string const & token,
} else if (token == "display") {
lex.next();
display = lex.getString() != "false";
} else if (token == "darkModeSensitive") {
darkModeSensitive = true;
} else if (token == "scale") {
lex.next();
scale = lex.getString();

View File

@ -32,8 +32,8 @@ extern char const * const lyx_version_info;
// Do not remove the comment below, so we get merge conflict in
// independent branches. Instead add your own.
#define LYX_FORMAT_LYX 602 // spitz: semantic branch colors
#define LYX_FORMAT_TEX2LYX 602
#define LYX_FORMAT_LYX 603 // spitz: InsetGraphicsParam darkModeSensitive
#define LYX_FORMAT_TEX2LYX 603
#if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
#ifndef _MSC_VER