commit d5e76100f6767b8482aa3dbb2b295ac1066a8dee
Author: Koji Yokota <yok...@lyx.org>
Date:   Thu Feb 27 16:59:24 2025 +0900

    Adding export and import buttons to PrefColors
---
 src/frontends/qt/GuiPrefs.cpp       | 140 ++++++++++++++-----
 src/frontends/qt/GuiPrefs.h         |  15 +-
 src/frontends/qt/ui/PrefColorsUi.ui | 268 ++++++++++++++++++++----------------
 3 files changed, 264 insertions(+), 159 deletions(-)

diff --git a/src/frontends/qt/GuiPrefs.cpp b/src/frontends/qt/GuiPrefs.cpp
index c4198e7209..970598d2d0 100644
--- a/src/frontends/qt/GuiPrefs.cpp
+++ b/src/frontends/qt/GuiPrefs.cpp
@@ -1056,35 +1056,41 @@ PrefColors::PrefColors(GuiPreferences * form)
        // End initialization
 
        connect(autoapplyCB, SIGNAL(toggled(bool)),
-               this, SLOT(changeAutoapply()));
+               this, SLOT(changeAutoapply()));
        connect(colorChangePB, SIGNAL(clicked(bool)),
-               this, SLOT(changeLightColor()));
+               this, SLOT(changeLightColor()));
        connect(colorDarkChangePB, SIGNAL(clicked(bool)),
-               this, SLOT(changeDarkColor()));
+               this, SLOT(changeDarkColor()));
        connect(colorResetPB, SIGNAL(clicked()),
-               this, SLOT(resetColor()));
+               this, SLOT(resetColor()));
        connect(colorResetAllPB, SIGNAL(clicked()),
-               this, SLOT(resetAllColor()));
+               this, SLOT(resetAllColor()));
+       connect(exportPB, SIGNAL(clicked()),
+               this, SLOT(exportThemeInterface()));
+       connect(importPB, SIGNAL(clicked()),
+               this, SLOT(importThemeInterface()));
        connect(loadThemeCO, SIGNAL(activated(int)),
-               this, SLOT(loadTheme(int)));
+               this, SLOT(loadThemeInterface(int)));
        connect(lyxObjectsLW, SIGNAL(focusChanged()),
-               this, SLOT(changeFocus()));
+               this, SLOT(changeFocus()));
        connect(lyxObjectsLW, SIGNAL(itemActivated(QListWidgetItem*)),
-               this, SLOT(changeColor()));
+               this, SLOT(changeColor()));
        connect(lyxObjectsLW, SIGNAL(itemSelectionChanged()),
-               this, SLOT(changeLyxObjectsSelection()));
+               this, SLOT(changeLyxObjectsSelection()));
        connect(lyxObjectsLW,
                SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
                this, SLOT(moveCurrentItem(QListWidgetItem*,QListWidgetItem*)));
        connect(lyxObjectsLW, SIGNAL(itemPressed(QListWidgetItem*)),
                this, SLOT(pressCurrentItem(QListWidgetItem*)));
-       connect(redoColorPB, SIGNAL(clicked()), undo_stack_, SLOT(redo()));
+       connect(redoColorPB, SIGNAL(clicked()),
+               undo_stack_, SLOT(redo()));
        connect(removeThemePB, SIGNAL(clicked()),
                this, SLOT(removeTheme()));
        connect(saveThemePB, SIGNAL(clicked()),
-               this, SLOT(saveTheme()));
+               this, SLOT(saveThemeInterface()));
 #if !defined(Q_OS_MAC)
-       connect(sc_load_theme, SIGNAL(activated()), loadThemeCO, 
SLOT(setFocus()));
+       connect(sc_load_theme, SIGNAL(activated()),
+               loadThemeCO, SLOT(setFocus()));
 #endif
        connect(sc_search, SIGNAL(activated()),
                searchStringEdit, SLOT(setFocus()));
@@ -1092,8 +1098,10 @@ PrefColors::PrefColors(GuiPreferences * form)
                this, SLOT(searchNextColorItem()));
        connect(sc_search_backward, SIGNAL(activated()),
                this, SLOT(searchPreviousColorItem()));
-       connect(sc_redo, SIGNAL(activated()), undo_stack_, SLOT(redo()));
-       connect(sc_undo, SIGNAL(activated()), undo_stack_, SLOT(undo()));
+       connect(sc_redo, SIGNAL(activated()),
+               undo_stack_, SLOT(redo()));
+       connect(sc_undo, SIGNAL(activated()),
+               undo_stack_, SLOT(undo()));
        connect(searchBackwardPB, SIGNAL(clicked()),
                this, SLOT(searchPreviousColorItem()));
        connect(searchForwardPB, SIGNAL(clicked()),
@@ -1101,10 +1109,11 @@ PrefColors::PrefColors(GuiPreferences * form)
        connect(searchStringEdit, SIGNAL(returnPressed()),
                this, SLOT(searchNextColorItem()));
        connect(syscolorsCB, SIGNAL(toggled(bool)),
-               this, SIGNAL(changed()));
+               this, SIGNAL(changed()));
        connect(syscolorsCB, SIGNAL(toggled(bool)),
-               this, SLOT(changeSysColor()));
-       connect(undoColorPB, SIGNAL(clicked()), undo_stack_, SLOT(undo()));
+               this, SLOT(changeSysColor()));
+       connect(undoColorPB, SIGNAL(clicked()),
+               undo_stack_, SLOT(undo()));
 }
 
 
@@ -1303,29 +1312,68 @@ void PrefColors::setDisabledResets()
 }
 
 
-void PrefColors::saveTheme()
+void PrefColors::exportThemeInterface()
+{
+       if (themeNameInterface(true))
+               return;
+
+       // ask for directory to export
+       QString home_dir = toqstr(package().get_home_dir().absFileName());
+       FileDialog dialog("Export a color theme");
+       FileDialog::Result result =
+               dialog.save((home_dir == "") ? "/" : home_dir, {"*.theme", 
"*.*"},
+                           theme_file_name_);
+       QString file_path;
+       if (result.first == FileDialog::Chosen)
+               file_path = result.second;
+
+       saveTheme(file_path);
+
+       return;
+}
+
+
+void PrefColors::saveThemeInterface()
 {
+       if (themeNameInterface(false))
+               return;
+
+       QString file_path = toqstr(package().user_support().absFileName())
+               + "/themes/" + theme_file_name_;
+       saveTheme(file_path);
+
+       return;
+}
+
+
+bool PrefColors::themeNameInterface(bool exporting)
+{
+       QString prompt = exporting ?
+                   qt_("What is the name of the color theme to export?") :
+                   qt_("What is the name of the new color theme to save?");
        bool ok;
-       QString theme_name =
+       theme_name_ =
                QInputDialog::getText(this, qt_("Name the color theme"),
-                       qt_("What is the name of the new color theme to be 
saved?"),
-                       QLineEdit::Normal, qt_("New theme name"), &ok);
-       QString file_name;
-       if (ok && !theme_name.isEmpty()) {
+                                     prompt, QLineEdit::Normal,
+                                     (theme_name_ == "") ? qt_("New theme 
name") : theme_name_, &ok);
+
+       if (ok && !theme_name_.isEmpty()) {
                // Makefile cannot handle spaces in filenames directly
                // so replace it with an underscore same as other places
-               file_name = theme_name;
-               file_name.replace(' ', '_');
-               file_name += ".theme";
+               theme_file_name_ = theme_name_;
+               theme_file_name_.replace(' ', '_');
+               theme_file_name_ += ".theme";
        } else {
                activatePrefsWindow(form_);
-               return;
+               return true;
        }
 
-       QString full_path = toqstr(package().user_support().absFileName())
-               + "/themes/" + file_name;
+       return false;
+}
 
-       QFile file_to_save(full_path);
+void PrefColors::saveTheme(QString file_path)
+{
+       QFile file_to_save(file_path);
        if (file_to_save.exists()) {
                QMessageBox msgBox;
                msgBox.setIcon(QMessageBox::Warning);
@@ -1338,11 +1386,11 @@ void PrefColors::saveTheme()
                      return;
        }
 
-       ofstream ofs(fromqstr(full_path));
+       ofstream ofs(fromqstr(file_path));
 
        ofs << "#\n" <<
               "#   This is a definition file of a color theme \"" <<
-                   fromqstr(theme_name) << "\" of LyX\n" <<
+                   fromqstr(theme_name_) << "\" of LyX\n" <<
               "#\n" <<
               "#   Author: " << form_->rc().user_name <<
                        " (" << form_->rc().user_email << ")\n" <<
@@ -1362,11 +1410,33 @@ void PrefColors::saveTheme()
 }
 
 
-void PrefColors::loadTheme(int index)
+void PrefColors::importThemeInterface()
+{
+       QString home_dir = toqstr(package().get_home_dir().absFileName());
+       FileDialog dialog("Import a color theme");
+       FileDialog::Result result =
+               dialog.open((home_dir == "") ? "/" : home_dir, {"*.theme", 
"*.*"});
+       QString file_path;
+       if (result.first == FileDialog::Chosen)
+               file_path = result.second;
+       else
+               return;
+
+       loadTheme(FileName(fromqstr(file_path)));
+
+       return;
+}
+
+
+void PrefColors::loadThemeInterface(int index)
 {
-       QString filename = loadThemeCO->itemData(index).toString();
+       loadTheme(FileName(fromqstr(loadThemeCO->itemData(index).toString())));
+}
 
-       form_->rc().read(FileName(fromqstr(filename)), true);
+
+void PrefColors::loadTheme(FileName filename)
+{
+       form_->rc().read(filename, true);
        for (size_type row = 0; row < lcolors_.size(); ++row) {
                newcolors_[size_t(row)] =
                    {QString(lcolor.getX11HexName(lcolors_[row], 
false).c_str()),
diff --git a/src/frontends/qt/GuiPrefs.h b/src/frontends/qt/GuiPrefs.h
index e10b5b406f..e21acbc14d 100644
--- a/src/frontends/qt/GuiPrefs.h
+++ b/src/frontends/qt/GuiPrefs.h
@@ -267,9 +267,11 @@ private Q_SLOTS:
                      QString const & old_color);
        bool isDefaultColor(int const row, std::pair<QString, QString> const & 
color);
        void setDisabledResets();
-       void saveTheme();
-       void loadTheme(int index);
+       void saveThemeInterface();
+       void loadThemeInterface(int index);
        void removeTheme();
+       void exportThemeInterface();
+       void importThemeInterface();
 
        void moveCurrentItem(QListWidgetItem *cur = nullptr,
                             QListWidgetItem *prev = nullptr);
@@ -296,6 +298,12 @@ private:
        ///
        void initializeLoadThemeCO();
        ///
+       void saveTheme(QString file_path);
+       ///
+       void loadTheme(support::FileName filename);
+       ///
+       bool themeNameInterface(bool exporting);
+       ///
        std::vector<ColorCode> lcolors_;
        ///
        std::vector<std::pair<QString, QString>> curcolors_;
@@ -311,7 +319,8 @@ private:
        int const spacer_width_ = 6;
 
        bool autoapply_ = false;
-
+       QString theme_name_ = "";
+       QString theme_file_name_;
        QUndoStack * undo_stack_;
 
        friend class SetColor;
diff --git a/src/frontends/qt/ui/PrefColorsUi.ui 
b/src/frontends/qt/ui/PrefColorsUi.ui
index cbf1fe6bf2..9e1c261fa2 100644
--- a/src/frontends/qt/ui/PrefColorsUi.ui
+++ b/src/frontends/qt/ui/PrefColorsUi.ui
@@ -60,33 +60,7 @@
     </widget>
    </item>
    <item row="1" column="0" colspan="3">
-    <layout class="QGridLayout" name="gridLayout" 
rowstretch="0,0,0,0,0,0,0,0,0" columnstretch="0,0,0,0,0,0,0">
-     <item row="4" column="6">
-      <widget class="QComboBox" name="loadThemeCO">
-       <property name="sizePolicy">
-        <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
-         <horstretch>0</horstretch>
-         <verstretch>0</verstretch>
-        </sizepolicy>
-       </property>
-       <property name="toolTip">
-        <string>Load preset color theme</string>
-       </property>
-       <property name="placeholderText">
-        <string>    Load Theme</string>
-       </property>
-      </widget>
-     </item>
-     <item row="5" column="6">
-      <widget class="QPushButton" name="saveThemePB">
-       <property name="toolTip">
-        <string>Save current color setting as a color theme</string>
-       </property>
-       <property name="text">
-        <string>&amp;Save Theme...</string>
-       </property>
-      </widget>
-     </item>
+    <layout class="QGridLayout" name="gridLayout" 
rowstretch="0,0,0,0,0,0,0,0,0,0,0,0,0" columnstretch="0,0,0,0,0,0,0">
      <item row="0" column="2">
       <layout class="QHBoxLayout" name="horizontalLayout">
        <property name="spacing">
@@ -135,39 +109,30 @@
        </item>
       </layout>
      </item>
-     <item row="0" column="6">
-      <widget class="QPushButton" name="colorChangePB">
-       <property name="sizePolicy">
-        <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
-         <horstretch>0</horstretch>
-         <verstretch>0</verstretch>
-        </sizepolicy>
-       </property>
+     <item row="4" column="6">
+      <widget class="QPushButton" name="colorResetAllPB">
        <property name="toolTip">
-        <string>Change the selected light-mode color</string>
+        <string>Reset all colors to their original value</string>
        </property>
        <property name="text">
-        <string>Alter &amp;Light Color...</string>
+        <string>Restore &amp;All</string>
        </property>
       </widget>
      </item>
-     <item row="0" column="5">
-      <spacer name="hsTitleRight">
+     <item row="5" column="6">
+      <spacer name="vsRightMiddleBelow">
        <property name="orientation">
-        <enum>Qt::Orientation::Horizontal</enum>
-       </property>
-       <property name="sizeType">
-        <enum>QSizePolicy::Policy::Fixed</enum>
+        <enum>Qt::Orientation::Vertical</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
-         <width>13</width>
-         <height>20</height>
+         <width>20</width>
+         <height>40</height>
         </size>
        </property>
       </spacer>
      </item>
-     <item row="1" column="0" rowspan="8" colspan="6">
+     <item row="1" column="0" rowspan="12" colspan="6">
       <widget class="lyx::frontend::ColorListWidget" name="lyxObjectsLW">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
@@ -180,47 +145,25 @@
        </property>
       </widget>
      </item>
-     <item row="3" column="6">
-      <spacer name="vsRightMiddle">
-       <property name="orientation">
-        <enum>Qt::Orientation::Vertical</enum>
-       </property>
-       <property name="sizeType">
-        <enum>QSizePolicy::Policy::Maximum</enum>
-       </property>
-       <property name="sizeHint" stdset="0">
-        <size>
-         <width>20</width>
-         <height>32</height>
-        </size>
+     <item row="7" column="6">
+      <widget class="QPushButton" name="saveThemePB">
+       <property name="toolTip">
+        <string>Save current color setting as a color theme</string>
        </property>
-      </spacer>
-     </item>
-     <item row="6" column="6">
-      <widget class="QPushButton" name="removeThemePB">
        <property name="text">
-        <string>Re&amp;move Theme...</string>
+        <string>&amp;Save Theme...</string>
        </property>
       </widget>
      </item>
-     <item row="0" column="3">
-      <spacer name="hsTitleCenter">
-       <property name="orientation">
-        <enum>Qt::Orientation::Horizontal</enum>
-       </property>
-       <property name="sizeType">
-        <enum>QSizePolicy::Policy::Maximum</enum>
-       </property>
-       <property name="sizeHint" stdset="0">
-        <size>
-         <width>30</width>
-         <height>13</height>
-        </size>
+     <item row="11" column="6">
+      <widget class="QPushButton" name="importPB">
+       <property name="text">
+        <string>Import...</string>
        </property>
-      </spacer>
+      </widget>
      </item>
-     <item row="1" column="6">
-      <widget class="QPushButton" name="colorDarkChangePB">
+     <item row="0" column="6">
+      <widget class="QPushButton" name="colorChangePB">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
          <horstretch>0</horstretch>
@@ -228,46 +171,13 @@
         </sizepolicy>
        </property>
        <property name="toolTip">
-        <string>Change the selected dark-mode color</string>
-       </property>
-       <property name="text">
-        <string>Alter &amp;Dark Color...</string>
-       </property>
-      </widget>
-     </item>
-     <item row="8" column="6">
-      <widget class="QPushButton" name="colorResetAllPB">
-       <property name="toolTip">
-        <string>Reset all colors to their original value</string>
-       </property>
-       <property name="text">
-        <string>Restore &amp;All</string>
-       </property>
-      </widget>
-     </item>
-     <item row="2" column="6">
-      <widget class="QPushButton" name="colorResetPB">
-       <property name="toolTip">
-        <string>Reset the selected color to its original value</string>
+        <string>Change the selected light-mode color</string>
        </property>
        <property name="text">
-        <string>&amp;Restore Default</string>
+        <string>Alter &amp;Light Color...</string>
        </property>
       </widget>
      </item>
-     <item row="0" column="1">
-      <spacer name="hsTitleLeft">
-       <property name="orientation">
-        <enum>Qt::Orientation::Horizontal</enum>
-       </property>
-       <property name="sizeHint" stdset="0">
-        <size>
-         <width>40</width>
-         <height>20</height>
-        </size>
-       </property>
-      </spacer>
-     </item>
      <item row="0" column="4">
       <layout class="QHBoxLayout" name="searchLayout">
        <property name="spacing">
@@ -358,19 +268,39 @@
        </item>
       </layout>
      </item>
-     <item row="7" column="6">
-      <spacer name="vsRightBottom">
+     <item row="2" column="6">
+      <widget class="QPushButton" name="colorResetPB">
+       <property name="toolTip">
+        <string>Reset the selected color to its original value</string>
+       </property>
+       <property name="text">
+        <string>&amp;Restore Theme</string>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="3">
+      <spacer name="hsTitleCenter">
        <property name="orientation">
-        <enum>Qt::Orientation::Vertical</enum>
+        <enum>Qt::Orientation::Horizontal</enum>
+       </property>
+       <property name="sizeType">
+        <enum>QSizePolicy::Policy::Maximum</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
-         <width>20</width>
-         <height>40</height>
+         <width>30</width>
+         <height>13</height>
         </size>
        </property>
       </spacer>
      </item>
+     <item row="10" column="6">
+      <widget class="QPushButton" name="exportPB">
+       <property name="text">
+        <string>Export...</string>
+       </property>
+      </widget>
+     </item>
      <item row="0" column="0">
       <layout class="QGridLayout" name="labelLayout">
        <property name="leftMargin">
@@ -450,6 +380,103 @@
        </item>
       </layout>
      </item>
+     <item row="3" column="6">
+      <spacer name="vsRightMiddle">
+       <property name="orientation">
+        <enum>Qt::Orientation::Vertical</enum>
+       </property>
+       <property name="sizeType">
+        <enum>QSizePolicy::Policy::Maximum</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>20</width>
+         <height>32</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="1" column="6">
+      <widget class="QPushButton" name="colorDarkChangePB">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="toolTip">
+        <string>Change the selected dark-mode color</string>
+       </property>
+       <property name="text">
+        <string>Alter &amp;Dark Color...</string>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="5">
+      <spacer name="hsTitleRight">
+       <property name="orientation">
+        <enum>Qt::Orientation::Horizontal</enum>
+       </property>
+       <property name="sizeType">
+        <enum>QSizePolicy::Policy::Fixed</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>13</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="9" column="6">
+      <spacer name="vsRightBottom">
+       <property name="orientation">
+        <enum>Qt::Orientation::Vertical</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>20</width>
+         <height>40</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="0" column="1">
+      <spacer name="hsTitleLeft">
+       <property name="orientation">
+        <enum>Qt::Orientation::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="8" column="6">
+      <widget class="QPushButton" name="removeThemePB">
+       <property name="text">
+        <string>Re&amp;move Theme...</string>
+       </property>
+      </widget>
+     </item>
+     <item row="6" column="6">
+      <widget class="QComboBox" name="loadThemeCO">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="toolTip">
+        <string>Load preset color theme</string>
+       </property>
+       <property name="placeholderText">
+        <string>    Load Theme</string>
+       </property>
+      </widget>
+     </item>
     </layout>
    </item>
   </layout>
@@ -472,7 +499,6 @@
   <tabstop>loadThemeCO</tabstop>
   <tabstop>saveThemePB</tabstop>
   <tabstop>removeThemePB</tabstop>
-  <tabstop>colorResetAllPB</tabstop>
   <tabstop>syscolorsCB</tabstop>
   <tabstop>autoapplyCB</tabstop>
   <tabstop>undoColorPB</tabstop>
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to