From 93b0b19a75a476bda44ce0690f0bfaf898906845 Mon Sep 17 00:00:00 2001
From: Saara Saaninkoski <saara.saaninkoski@tuni.fi>
Date: Mon, 25 Jan 2021 12:38:05 +0200
Subject: [PATCH] Fix disappearing ui elements

---
 kvalikirstu2/header_panel.py | 84 ++++++++++++++++++++----------------
 requirements.txt             |  2 +-
 requirements_test.txt        |  4 +-
 3 files changed, 49 insertions(+), 41 deletions(-)

diff --git a/kvalikirstu2/header_panel.py b/kvalikirstu2/header_panel.py
index a0eb635..6cb6e8e 100644
--- a/kvalikirstu2/header_panel.py
+++ b/kvalikirstu2/header_panel.py
@@ -285,12 +285,9 @@ class HeaderPanel(wx.Panel):
     """A panel with header editing controls.
 
     """
-    ID_SHOW_BUILTIN = wx.Window.NewControlId()
-    ID_HIDE_BUILTIN = wx.Window.NewControlId()
-    ID_SHOW_NONHEADER = wx.Window.NewControlId()
-    ID_HIDE_NONHEADER = wx.Window.NewControlId()
-    ID_MOVE_OFF = wx.Window.NewControlId()
-    ID_MOVE_ON = wx.Window.NewControlId()
+    ID_TOGGLE_SHOW_BUILTIN = wx.Window.NewControlId()
+    ID_TOGGLE_SHOW_NONHEADER = wx.Window.NewControlId()
+    ID_MOVE_HEADERS = wx.Window.NewControlId()
 
     def __init__(self, parent, main_win, model, style=wx.TAB_TRAVERSAL):
         super().__init__(parent, style=style)
@@ -342,7 +339,7 @@ class HeaderPanel(wx.Panel):
         """Inits the side ribbon bar with header edit controls.
 
         """
-        btn_id = self.ID_HIDE_NONHEADER + 1
+        btn_id = self.ID_TOGGLE_SHOW_NONHEADER + 1
         new_buttonbar = gui_utils.new_buttonbar
 
         self.undo_redo_button_bar.AddButton(wx.ID_UNDO, "", wx.ArtProvider.GetBitmap(wx.ART_UNDO, wx.ART_TOOLBAR),
@@ -383,14 +380,14 @@ class HeaderPanel(wx.Panel):
         gui_utils.add_separator_to_ribbon_page(ribbon_page, False, wx.LI_HORIZONTAL)
 
         builtin_tb = new_buttonbar(ribbon_page)
-        self.add_show_builtin_button(builtin_tb)
+        self.add_or_refresh_show_builtin_button(builtin_tb)
 
         nonheader_tb = new_buttonbar(ribbon_page)
-        self.add_show_nonheader_button(nonheader_tb)
+        self.add_or_refresh_show_nonheader_button(nonheader_tb)
 
         gui_utils.add_separator_to_ribbon_page(ribbon_page, False, wx.LI_HORIZONTAL)
         move_tb = gui_utils.new_buttonbar(ribbon_page)
-        self.add_move_headers_button(move_tb)
+        self.add_or_refresh_move_headers_button(move_tb)
 
     def _init_listctrl(self):
         """Inits the listctrl.
@@ -482,18 +479,21 @@ class HeaderPanel(wx.Panel):
 
         self.list_ctrl.PopupMenu(self.menu, evt.GetPoint())
 
-    def add_move_headers_button(self, parent_tb):
+    def add_or_refresh_move_headers_button(self, parent_tb):
         """Removes and readds the move headers button with an icon
         and label according to if moving rows is activated.
         """
         active = self.model.move_rows_active
-        btn_id = self.ID_MOVE_ON if active else self.ID_MOVE_OFF
+        btn_id = self.ID_MOVE_HEADERS
         title = _("Header ordering")
-        parent_tb.ClearButtons()
-        icon_id = "move_off" if btn_id == self.ID_MOVE_OFF else "move_on"
-        parent_tb.AddButton(
-            btn_id, title, gui_utils.create_bitmap(icon_id),
-            _("Enables or disables moving rows with up/down keys."))
+        icon_id = "move_on" if active else "move_off"
+        icon = gui_utils.create_bitmap(icon_id)
+        if parent_tb.GetButtonCount() < 1:
+            parent_tb.AddButton(
+                btn_id, title, icon,
+                _("Enables or disables moving rows with up/down keys."))
+        else:
+            parent_tb.SetButtonIcon(btn_id, icon)
         parent_tb.Bind(
             RB.EVT_RIBBONBUTTONBAR_CLICKED, self.toggle_move_rows, id=btn_id)
         self.main_win.add_button_exists_hook(parent_tb, btn_id, gui_model.Messages.STUDY_EXISTS)
@@ -503,33 +503,41 @@ class HeaderPanel(wx.Panel):
         """
         ribbon_btn_bar = evt.GetBar()
         self.model.toggle_move_rows()
-        self.add_move_headers_button(ribbon_btn_bar)
+        self.add_or_refresh_move_headers_button(ribbon_btn_bar)
 
-    def add_show_builtin_button(self, parent_tb):
+    def add_or_refresh_show_builtin_button(self, parent_tb):
         """Clears the button bar with the show builtin button and adds a new button with an
         icon and label according to the value of show_builtin_headers."""
-        btn_id = self.ID_HIDE_BUILTIN if self.show_builtin_headers else self.ID_SHOW_BUILTIN
-        parent_tb.ClearButtons()
-        btn_title = _("Hide builtin headers") if btn_id == self.ID_HIDE_BUILTIN else _("Show builtin headers")
-        icon_id = "hide" if btn_id == self.ID_HIDE_BUILTIN else "show"
-        tooltip = (_("Hide builtin headers in the list") if btn_id == self.ID_HIDE_BUILTIN else
-                   _("Show builtin headers in the list"))
-
-        parent_tb.AddButton(btn_id, btn_title, gui_utils.create_bitmap(icon_id), tooltip)
+        btn_id = self.ID_TOGGLE_SHOW_BUILTIN
+        btn_title = _("Hide builtin headers") if self.show_builtin_headers else _("Show builtin headers")
+        icon_id = "hide" if self.show_builtin_headers else "show"
+        icon = gui_utils.create_bitmap(icon_id)
+        tooltip = (_("Show builtin headers in the list"))
+
+        if parent_tb.GetButtonCount() < 1:
+            parent_tb.AddButton(btn_id, btn_title, icon, tooltip)
+        else:
+            parent_tb.SetButtonIcon(btn_id, icon)
+            parent_tb.SetButtonText(btn_id, btn_title)
+
         parent_tb.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.toggle_show_builtin_headers, id=btn_id)
         self.main_win.add_button_exists_hook(parent_tb, btn_id, gui_model.Messages.STUDY_EXISTS)
 
-    def add_show_nonheader_button(self, parent_tb):
+    def add_or_refresh_show_nonheader_button(self, parent_tb):
         """Clears the button bar with the show non headers button and adds a new button with an
         icon and label according to the value of show_non_headers."""
-        btn_id = self.ID_HIDE_NONHEADER if self.show_non_headers else self.ID_SHOW_NONHEADER
-        parent_tb.ClearButtons()
-        btn_title = _("Hide non-headers") if btn_id == self.ID_HIDE_NONHEADER else _("Show non-headers")
-        icon_id = "hide" if btn_id == self.ID_HIDE_NONHEADER else "show"
-        tooltip = (_("Hide lines marked as not headers") if btn_id == self.ID_HIDE_NONHEADER else
-                   _("Show lines marked as not headers"))
-
-        parent_tb.AddButton(btn_id, btn_title, gui_utils.create_bitmap(icon_id), tooltip)
+        btn_id = self.ID_TOGGLE_SHOW_NONHEADER
+        btn_title = _("Hide non-headers") if self.show_non_headers else _("Show non-headers")
+        icon_id = "hide" if self.show_non_headers else "show"
+        icon = gui_utils.create_bitmap(icon_id)
+        tooltip = (_("Show lines marked as not headers"))
+
+        if parent_tb.GetButtonCount() < 1:
+            parent_tb.AddButton(btn_id, btn_title, icon, tooltip)
+        else:
+            parent_tb.SetButtonIcon(btn_id, icon)
+            parent_tb.SetButtonText(btn_id, btn_title)
+
         parent_tb.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.toggle_show_non_headers, id=btn_id)
         self.main_win.add_button_exists_hook(parent_tb, btn_id, gui_model.Messages.STUDY_EXISTS)
 
@@ -539,7 +547,7 @@ class HeaderPanel(wx.Panel):
         """
         ribbon_btn_bar = evt.GetBar()
         self.show_builtin_headers = not self.show_builtin_headers
-        self.add_show_builtin_button(ribbon_btn_bar)
+        self.add_or_refresh_show_builtin_button(ribbon_btn_bar)
         self.model.set_type_shown(gui_model.HeaderType.BUILTIN, self.show_builtin_headers)
 
     def toggle_show_non_headers(self, evt):
@@ -548,7 +556,7 @@ class HeaderPanel(wx.Panel):
         """
         ribbon_btn_bar = evt.GetBar()
         self.show_non_headers = not self.show_non_headers
-        self.add_show_nonheader_button(ribbon_btn_bar)
+        self.add_or_refresh_show_nonheader_button(ribbon_btn_bar)
         self.model.set_type_shown(gui_model.HeaderType.NOT_HEADER, self.show_non_headers)
 
     def open_edit_value_dlg(self):
diff --git a/requirements.txt b/requirements.txt
index 7dd4087..7fc8b60 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -9,5 +9,5 @@ natsort==6.2.0
 odfpy==1.4.0
 Pillow>=6.0.0
 six>=1.14.0
-wxPython>=4.1;platform_system=="Windows"
+wxPython==4.1.1;platform_system=="Windows"
 pypubsub===4.0.3
\ No newline at end of file
diff --git a/requirements_test.txt b/requirements_test.txt
index 3974055..9250700 100644
--- a/requirements_test.txt
+++ b/requirements_test.txt
@@ -9,8 +9,8 @@ natsort==6.2.0
 odfpy==1.4.0
 Pillow>=6.0.0
 six>=1.14.0
-wxPython>=4.1;platform_system=="Windows"
-https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04/wxPython-4.1.0-cp38-cp38-linux_x86_64.whl; sys_platform == "linux"
+wxPython==4.1.1;platform_system=="Windows"
+https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04/wxPython-4.1.1-cp38-cp38-linux_x86_64.whl; sys_platform == "linux"
 pytest<=4.0
 pytest-cov<=2.8
 pyvirtualdisplay==0.2.1
-- 
GitLab