commit 057753dc7549743a826d52b301eb846cb419b2b9
Author: Juergen Spitzmueller <[email protected]>
Date:   Mon Jan 18 09:56:53 2021 +0100

    Allow semantic colors in branches and use semantic background color by 
default
    
    Addresses most crucial part of #12075
    
    File format change
---
 development/FORMAT                |    4 +
 lib/lyx2lyx/lyx_2_4.py            |   49 +++++++++-
 src/BranchList.cpp                |   26 +-----
 src/BranchList.h                  |    6 +-
 src/Buffer.cpp                    |    8 ++-
 src/BufferParams.cpp              |    9 ++-
 src/frontends/qt/GuiBranches.cpp  |   38 +++++++-
 src/frontends/qt/GuiBranches.h    |    1 +
 src/frontends/qt/GuiDocument.cpp  |    8 ++-
 src/frontends/qt/ui/BranchesUi.ui |  184 +++++++++++++++++++++----------------
 src/version.h                     |    4 +-
 11 files changed, 221 insertions(+), 116 deletions(-)

diff --git a/development/FORMAT b/development/FORMAT
index 7e389c2..2caa6d8 100644
--- a/development/FORMAT
+++ b/development/FORMAT
@@ -7,6 +7,10 @@ changes happened in particular if possible. A good example 
would be
 
 -----------------------
 
+2021-01-17 Jürgen Spitzmüller <[email protected]>
+       * Format incremented to 602: Allow semantic branch colors
+         \color can now also take lyx names besides hexnames.
+
 2021-01-05 Richard Kimberly Heck <[email protected]>
        * Format incremented to 601: Add refstyle support for mathed
          Use 'formatted' in the LyX file to indicate formatted references,
diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py
index c82d09f..142a174 100644
--- a/lib/lyx2lyx/lyx_2_4.py
+++ b/lib/lyx2lyx/lyx_2_4.py
@@ -4025,7 +4025,47 @@ def revert_math_refs(document):
             if "\\labelonly" in document.body[i]:
                 document.body[i] = re.sub("\\\\labelonly{([^}]+?)}", "\\1", 
document.body[i])
             i += 1
-        
+
+
+def convert_branch_colors(document):
+    " Convert branch colors to semantic values "
+
+    i = 0
+    while True:
+        i = find_token(document.header, "\\branch", i)
+        if i == -1:
+            break
+        j = find_token(document.header, "\\end_branch", i)
+        if j == -1:
+           document.warning("Malformed LyX document. Can't find end of branch 
definition!")
+           break
+        # We only support the standard LyX background for now
+        k = find_token(document.header, "\\color #faf0e6", i, j)
+        if k != -1:
+           document.header[k] = "\\color background"
+        i += 1
+
+
+def revert_branch_colors(document):
+    " Revert semantic branch colors "
+
+    i = 0
+    while True:
+        i = find_token(document.header, "\\branch", i)
+        if i == -1:
+            break
+        j = find_token(document.header, "\\end_branch", i)
+        if j == -1:
+           document.warning("Malformed LyX document. Can't find end of branch 
definition!")
+           break
+        k = find_token(document.header, "\\color", i, j)
+        if k != -1:
+           bcolor = get_value(document.header, "\\color", k)
+           if bcolor[1] != "#":
+               # this will be read as background by LyX 2.3
+               document.header[k] = "\\color none"
+        i += 1
+
 
 ##
 # Conversion hub
@@ -4089,10 +4129,13 @@ convert = [
            [598, []],
            [599, []],
            [600, []],
-           [601, [convert_math_refs]]
+           [601, [convert_math_refs]],
+           [602, [convert_branch_colors]]
           ]
 
-revert =  [[599, [revert_math_refs]],
+revert =  [[601, [revert_branch_colors]],
+           [600, []],
+           [599, [revert_math_refs]],
            [598, [revert_hrquotes]],
            [598, [revert_nopagebreak]],
            [597, [revert_docbook_table_output]],
diff --git a/src/BranchList.cpp b/src/BranchList.cpp
index 0113552..379b338 100644
--- a/src/BranchList.cpp
+++ b/src/BranchList.cpp
@@ -13,6 +13,7 @@
 
 #include "BranchList.h"
 #include "Color.h"
+#include "ColorSet.h"
 
 #include "frontends/Application.h"
 
@@ -28,11 +29,7 @@ namespace lyx {
 Branch::Branch()
        : selected_(false), filenameSuffix_(false)
 {
-       // no theApp() with command line export
-       if (theApp())
-               theApp()->getRgbColor(Color_background, color_);
-       else
-               frontend::Application::getRgbColorUncached(Color_background, 
color_);
+       color_ = "background";
 }
 
 
@@ -75,30 +72,15 @@ void Branch::setFileNameSuffix(bool b)
 }
 
 
-RGBColor const & Branch::color() const
+string const & Branch::color() const
 {
        return color_;
 }
 
 
-void Branch::setColor(RGBColor const & c)
-{
-       color_ = c;
-}
-
-
 void Branch::setColor(string const & str)
 {
-       if (str.size() == 7 && str[0] == '#')
-               color_ = rgbFromHexName(str);
-       else {
-               // no color set or invalid color - use normal background
-               // no theApp() with command line export
-               if (theApp())
-                       theApp()->getRgbColor(Color_background, color_);
-               else
-                       
frontend::Application::getRgbColorUncached(Color_background, color_);
-       }
+       color_ = str;
 }
 
 
diff --git a/src/BranchList.h b/src/BranchList.h
index 534fb87..c7dd196 100644
--- a/src/BranchList.h
+++ b/src/BranchList.h
@@ -61,9 +61,7 @@ public:
        /// Select/deselect filename suffix property.
        void setFileNameSuffix(bool);
        ///
-       RGBColor const & color() const;
-       ///
-       void setColor(RGBColor const &);
+       std::string const & color() const;
        /**
         * Set color from a string "#rrggbb".
         * Use Color:background if the string is no valid color.
@@ -80,7 +78,7 @@ private:
        ///
        bool filenameSuffix_;
        ///
-       RGBColor color_;
+       std::string color_;
 };
 
 
diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 12af84f..01469db 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -21,6 +21,7 @@
 #include "BufferParams.h"
 #include "Bullet.h"
 #include "Chktex.h"
+#include "ColorSet.h"
 #include "Converter.h"
 #include "Counters.h"
 #include "Cursor.h"
@@ -2938,7 +2939,12 @@ void Buffer::dispatch(FuncRequest const & func, 
DispatchResult & dr)
                                undo().recordUndoBufferParams(CursorData());
                                branch_list.add(branch_name);
                                branch = branch_list.find(branch_name);
-                               string const x11hexname = 
X11hexname(branch->color());
+                               string x11hexname;
+                               string const bcolor = branch->color();
+                               if (bcolor.size() == 7 && bcolor[0] == '#')
+                                       x11hexname = bcolor;
+                               else
+                                       x11hexname = 
lcolor.getX11HexName(lcolor.getFromLyXName(bcolor));
                                docstring const str = branch_name + ' ' + 
from_ascii(x11hexname);
                                lyx::dispatch(FuncRequest(LFUN_SET_COLOR, str));
                                dr.setError(false);
diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index f7e3d2c..93a5398 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -991,11 +991,16 @@ string BufferParams::readToken(Lexer & lex, string const 
& token,
                        if (tok == "\\color") {
                                lex.eatLine();
                                string color = lex.getString();
-                               if (branch_ptr)
+                               if (branch_ptr) {
                                        branch_ptr->setColor(color);
+                                       if (branch_ptr->color() == "none")
+                                               color = 
lcolor.getX11HexName(Color_background);
+                               }
                                // Update also the Color table:
                                if (color == "none")
                                        color = 
lcolor.getX11HexName(Color_background);
+                               else if (color.size() != 7 || color[0] != '#')
+                                       color = lcolor.getFromLyXName(color);
                                // FIXME UNICODE
                                lcolor.setColor(to_utf8(branch), color);
                        }
@@ -1387,7 +1392,7 @@ void BufferParams::writeFile(ostream & os, Buffer const * 
buf) const
                os << "\\branch " << to_utf8(br.branch())
                   << "\n\\selected " << br.isSelected()
                   << "\n\\filename_suffix " << br.hasFileNameSuffix()
-                  << "\n\\color " << lyx::X11hexname(br.color())
+                  << "\n\\color " << br.color()
                   << "\n\\end_branch"
                   << "\n";
        }
diff --git a/src/frontends/qt/GuiBranches.cpp b/src/frontends/qt/GuiBranches.cpp
index b1ef8be..79d1640 100644
--- a/src/frontends/qt/GuiBranches.cpp
+++ b/src/frontends/qt/GuiBranches.cpp
@@ -134,7 +134,13 @@ void GuiBranches::updateView()
                newItem->setText(0, bname);
                newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
 
-               QColor const itemcolor = rgb2qcolor(it->color());
+               std::string bcolor = it->color();
+               RGBColor rgbcol;
+               if (bcolor.size() == 7 && bcolor[0] == '#')
+                       rgbcol = lyx::rgbFromHexName(bcolor);
+               else
+                       guiApp->getRgbColor(lcolor.getFromLyXName(bcolor), 
rgbcol);
+               QColor const itemcolor = rgb2qcolor(rgbcol);
                if (itemcolor.isValid()) {
                        QPixmap coloritem(30, 10);
                        coloritem.fill(itemcolor);
@@ -153,6 +159,7 @@ void GuiBranches::updateView()
        removePB->setEnabled(have_sel);
        renamePB->setEnabled(have_sel);
        colorPB->setEnabled(have_sel);
+       resetColorPB->setEnabled(have_sel);
        activatePB->setEnabled(have_sel);
        suffixPB->setEnabled(have_sel);
        // emit signal
@@ -264,6 +271,7 @@ void GuiBranches::on_branchesTW_itemSelectionChanged()
        removePB->setEnabled(have_sel);
        renamePB->setEnabled(have_sel);
        colorPB->setEnabled(have_sel);
+       resetColorPB->setEnabled(have_sel);
        activatePB->setEnabled(have_sel);
        suffixPB->setEnabled(have_sel);
 }
@@ -292,6 +300,26 @@ void GuiBranches::on_colorPB_clicked()
 }
 
 
+void GuiBranches::on_resetColorPB_clicked()
+{
+       QTreeWidgetItem * item = branchesTW->currentItem();
+       if (item == 0)
+               return;
+
+       QString sel_branch = item->text(0);
+       if (sel_branch.isEmpty())
+               return;
+
+       docstring current_branch = qstring_to_ucs4(sel_branch);
+       Branch * branch = branchlist_.find(current_branch);
+       if (!branch)
+               return;
+       branch->setColor("background");
+       newBranchLE->clear();
+       updateView();
+}
+
+
 void GuiBranches::toggleColor(QTreeWidgetItem * item)
 {
        if (item == 0)
@@ -306,7 +334,13 @@ void GuiBranches::toggleColor(QTreeWidgetItem * item)
        if (!branch)
                return;
 
-       QColor const initial = rgb2qcolor(branch->color());
+       std::string bcolor = branch->color();
+       RGBColor rgbcol;
+       if (bcolor.size() == 7 && bcolor[0] == '#')
+               rgbcol = lyx::rgbFromHexName(bcolor);
+       else
+               guiApp->getRgbColor(lcolor.getFromLyXName(bcolor), rgbcol);
+       QColor const initial = rgb2qcolor(rgbcol);
        QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
        if (!ncol.isValid())
                return;
diff --git a/src/frontends/qt/GuiBranches.h b/src/frontends/qt/GuiBranches.h
index dd12649..64cf83f 100644
--- a/src/frontends/qt/GuiBranches.h
+++ b/src/frontends/qt/GuiBranches.h
@@ -71,6 +71,7 @@ protected Q_SLOTS:
        void on_branchesTW_itemDoubleClicked(QTreeWidgetItem *, int);
        void on_branchesTW_itemSelectionChanged();
        void on_colorPB_clicked();
+       void on_resetColorPB_clicked();
        void on_suffixPB_pressed();
        void on_unknownPB_pressed();
        void addUnknown();
diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp
index 953e51c..538dcbf 100644
--- a/src/frontends/qt/GuiDocument.cpp
+++ b/src/frontends/qt/GuiDocument.cpp
@@ -5008,7 +5008,13 @@ void GuiDocument::dispatchParams()
                for (; it != end; ++it) {
                        docstring const & current_branch = it->branch();
                        Branch const * branch = branchlist.find(current_branch);
-                       string const x11hexname = X11hexname(branch->color());
+                       string const bcolor = branch->color();
+                       RGBColor rgbcol;
+                       if (bcolor.size() == 7 && bcolor[0] == '#')
+                               rgbcol = lyx::rgbFromHexName(bcolor);
+                       else
+                               
guiApp->getRgbColor(lcolor.getFromLyXName(bcolor), rgbcol);
+                       string const x11hexname = X11hexname(rgbcol);
                        // display the new color
                        docstring const str = current_branch + ' ' + 
from_ascii(x11hexname);
                        dispatch(FuncRequest(LFUN_SET_COLOR, str));
diff --git a/src/frontends/qt/ui/BranchesUi.ui 
b/src/frontends/qt/ui/BranchesUi.ui
index 4c83b18..8e6f860 100644
--- a/src/frontends/qt/ui/BranchesUi.ui
+++ b/src/frontends/qt/ui/BranchesUi.ui
@@ -1,53 +1,53 @@
-<ui version="4.0" >
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
  <class>BranchesUi</class>
- <widget class="QWidget" name="BranchesUi" >
-  <property name="geometry" >
+ <widget class="QWidget" name="BranchesUi">
+  <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>401</width>
-    <height>327</height>
+    <height>346</height>
    </rect>
   </property>
-  <property name="windowTitle" >
+  <property name="windowTitle">
    <string/>
   </property>
-  <layout class="QGridLayout" >
-   <property name="margin" >
+  <layout class="QGridLayout">
+   <property name="leftMargin">
     <number>9</number>
    </property>
-   <property name="spacing" >
+   <property name="topMargin">
+    <number>9</number>
+   </property>
+   <property name="rightMargin">
+    <number>9</number>
+   </property>
+   <property name="bottomMargin">
+    <number>9</number>
+   </property>
+   <property name="spacing">
     <number>6</number>
    </property>
-   <item row="0" column="0" >
-    <widget class="QLabel" name="newBranchLA" >
-     <property name="text" >
-      <string>&amp;New:[[branch]]</string>
-     </property>
-     <property name="buddy" >
-      <cstring>newBranchLE</cstring>
-     </property>
-    </widget>
-   </item>
-   <item row="5" column="3" >
-    <widget class="QPushButton" name="suffixPB" >
-     <property name="toolTip" >
+   <item row="6" column="3">
+    <widget class="QPushButton" name="suffixPB">
+     <property name="toolTip">
       <string>Append the name of this branch to the output filename, given the 
branch is active.</string>
      </property>
-     <property name="text" >
+     <property name="text">
       <string>Filename &amp;Suffix</string>
      </property>
     </widget>
    </item>
-   <item row="6" column="3" >
+   <item row="7" column="3">
     <spacer>
-     <property name="orientation" >
+     <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
-     <property name="sizeType" >
+     <property name="sizeType">
       <enum>QSizePolicy::Expanding</enum>
      </property>
-     <property name="sizeHint" >
+     <property name="sizeHint" stdset="0">
       <size>
        <width>83</width>
        <height>51</height>
@@ -55,95 +55,121 @@
      </property>
     </spacer>
    </item>
-   <item row="8" column="2" colspan="2" >
-    <widget class="QPushButton" name="unknownPB" >
-     <property name="toolTip" >
-      <string>Show undefined branches used in this document.</string>
+   <item row="1" column="0" colspan="3">
+    <widget class="QLabel" name="availableLB">
+     <property name="text">
+      <string>A&amp;vailable Branches:</string>
      </property>
-     <property name="text" >
-      <string>&amp;Undefined Branches</string>
+     <property name="buddy">
+      <cstring>branchesTW</cstring>
      </property>
     </widget>
    </item>
-   <item row="8" column="0" colspan="2" >
-    <spacer>
-     <property name="orientation" >
-      <enum>Qt::Horizontal</enum>
+   <item row="2" column="3">
+    <widget class="QPushButton" name="removePB">
+     <property name="toolTip">
+      <string>Remove the selected branch</string>
      </property>
-     <property name="sizeHint" >
-      <size>
-       <width>251</width>
-       <height>20</height>
-      </size>
+     <property name="text">
+      <string>&amp;Remove</string>
      </property>
-    </spacer>
+    </widget>
    </item>
-   <item row="1" column="0" colspan="3" >
-    <widget class="QLabel" name="availableLB" >
-     <property name="text" >
-      <string>A&amp;vailable Branches:</string>
+   <item row="9" column="2" colspan="2">
+    <widget class="QPushButton" name="unknownPB">
+     <property name="toolTip">
+      <string>Show undefined branches used in this document.</string>
      </property>
-     <property name="buddy" >
-      <cstring>branchesTW</cstring>
+     <property name="text">
+      <string>&amp;Undefined Branches</string>
      </property>
     </widget>
    </item>
-   <item row="7" column="3" >
-    <widget class="QPushButton" name="activatePB" >
-     <property name="toolTip" >
+   <item row="2" column="0" rowspan="7" colspan="3">
+    <widget class="QTreeWidget" name="branchesTW">
+     <column>
+      <property name="text">
+       <string notr="true">1</string>
+      </property>
+     </column>
+    </widget>
+   </item>
+   <item row="8" column="3">
+    <widget class="QPushButton" name="activatePB">
+     <property name="toolTip">
       <string>Toggle the selected branch</string>
      </property>
-     <property name="text" >
+     <property name="text">
       <string>(&amp;De)activate</string>
      </property>
     </widget>
    </item>
-   <item rowspan="6" row="2" column="0" colspan="3" >
-    <widget class="QTreeWidget" name="branchesTW" />
-   </item>
-   <item row="0" column="3" >
-    <widget class="QPushButton" name="addBranchPB" >
+   <item row="0" column="3">
+    <widget class="QPushButton" name="addBranchPB">
      <property name="enabled">
       <bool>false</bool>
      </property>
-     <property name="toolTip" >
+     <property name="toolTip">
       <string>Add a new branch to the list</string>
      </property>
-     <property name="text" >
+     <property name="text">
       <string>&amp;Add</string>
      </property>
     </widget>
    </item>
-   <item row="0" column="1" colspan="2" >
-    <widget class="QLineEdit" name="newBranchLE" />
-   </item>
-   <item row="4" column="3" >
-    <widget class="QPushButton" name="colorPB" >
-     <property name="toolTip" >
+   <item row="4" column="3">
+    <widget class="QPushButton" name="colorPB">
+     <property name="toolTip">
       <string>Define or change background color</string>
      </property>
-     <property name="text" >
+     <property name="text">
       <string>Alter Co&amp;lor...</string>
      </property>
     </widget>
    </item>
-   <item row="2" column="3" >
-    <widget class="QPushButton" name="removePB" >
-     <property name="toolTip" >
-      <string>Remove the selected branch</string>
+   <item row="3" column="3">
+    <widget class="QPushButton" name="renamePB">
+     <property name="toolTip">
+      <string>Change the name of the selected branch</string>
      </property>
-     <property name="text" >
-      <string>&amp;Remove</string>
+     <property name="text">
+      <string>Re&amp;name...</string>
      </property>
     </widget>
    </item>
-   <item row="3" column="3" >
-    <widget class="QPushButton" name="renamePB" >
-     <property name="toolTip" >
-      <string>Change the name of the selected branch</string>
+   <item row="0" column="0">
+    <widget class="QLabel" name="newBranchLA">
+     <property name="text">
+      <string>&amp;New:[[branch]]</string>
      </property>
-     <property name="text" >
-      <string>Re&amp;name...</string>
+     <property name="buddy">
+      <cstring>newBranchLE</cstring>
+     </property>
+    </widget>
+   </item>
+   <item row="0" column="1" colspan="2">
+    <widget class="QLineEdit" name="newBranchLE"/>
+   </item>
+   <item row="9" column="0" colspan="2">
+    <spacer>
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>251</width>
+       <height>20</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
+   <item row="5" column="3">
+    <widget class="QPushButton" name="resetColorPB">
+     <property name="statusTip">
+      <string>Reset branch color to default (standard background)</string>
+     </property>
+     <property name="text">
+      <string>R&amp;eset Color</string>
      </property>
     </widget>
    </item>
@@ -161,7 +187,7 @@
   <tabstop>unknownPB</tabstop>
  </tabstops>
  <includes>
-  <include location="local" >qt_i18n.h</include>
+  <include location="local">qt_i18n.h</include>
  </includes>
  <resources/>
  <connections/>
diff --git a/src/version.h b/src/version.h
index 13cf4d0..b4a855e 100644
--- a/src/version.h
+++ b/src/version.h
@@ -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 601 // rkh: refstyle in math
-#define LYX_FORMAT_TEX2LYX 601
+#define LYX_FORMAT_LYX 602 // spitz: semantic branch colors
+#define LYX_FORMAT_TEX2LYX 602
 
 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
 #ifndef _MSC_VER
-- 
lyx-cvs mailing list
[email protected]
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to