OSDN Git Service

change skill2 table to wx.Grid and implement auto column size of Grid
[amulettoolsmh4/main.git] / amulettool.py
index 565d39d..b95ae2c 100644 (file)
@@ -1,3 +1,4 @@
+#!/usr/bin/python
 # -*- coding: utf-8 -*-
 
 # お守りスキルのSeed特定ツールのコントロールクラス
@@ -14,9 +15,11 @@ import wx
 
 import view
 import model
+import convertcoding
 
 SETTING_FILE = u"settings"
-SETTING_THRESHOLD = u"threshold"
+SETTING_THRESHOLD1 = u"threshold1"
+SETTING_THRESHOLD2 = u"threshold2"
 SETTING_SKILLS = u"skills"
 
 def _get_base_dir():
@@ -26,14 +29,16 @@ def _get_base_dir():
         basedir = sys._MEIPASS
     else:
         # we are running in a normal Python environment
-        basedir = os.path.dirname(__file__)
-    return basedir
+        #1basedir = os.path.dirname(__file__)
+        basedir = os.path.dirname(os.path.abspath(__file__))
+    return convertcoding.convert_unicode(basedir)
+
 
 class AmuletToolController(wx.App):
     u""" アプリケーションの制御クラス """
     def OnInit(self):
         self._read_settings()
-        self.frame_view = view.MainFrameView(os.path.join(_get_base_dir(), "view", view.XRC_MAIN_FRAME))
+        self.frame_view = view.MainFrameView(os.path.join(_get_base_dir(), u"view", view.XRC_MAIN_FRAME))
 
         self._init_events()
 
@@ -87,7 +92,7 @@ class AmuletToolController(wx.App):
                 generator = model.DataBaseGenerator(model.DB_FILE_NAME)
                 #generator = model.DataBaseGenerator()
 
-                dlg_view = view.GaugeDialogView(os.path.join(_get_base_dir(), "view", view.XRC_GAUGE_DIALOG))
+                dlg_view = view.GaugeDialogView(os.path.join(_get_base_dir(), u"view", view.XRC_GAUGE_DIALOG))
                 def _loop():
                     while t1.is_alive():
                         dlg_view.gauge.Pulse()
@@ -136,13 +141,10 @@ class AmuletToolController(wx.App):
                 [view.NAME_AMULET1, view.NAME_AMULET2, view.NAME_AMULET3]):
             amu_id = self._amulet_name2id_dict[amu_name]
             skill_ids = self._amulet_id2skill2_id_dict[amu_id]
-            skill_names = [self._skill_id2name_dict[x] for x in skill_ids]
+            skill_names = [view.VAL_NO_SKILL] + [self._skill_id2name_dict[x] for x in skill_ids]
             for combo in combo_dict[amu_key]:
-                combo.Clear()
-                combo.Append(view.VAL_NO_SKILL)
-                for name in skill_names:
-                    combo.Append(name)
-                combo.SetSelection(0)
+               combo.SetItems(skill_names)
+               combo.SetSelection(0)
 
         # amulet search
         amu_id = self._amulet_name2id_dict[view.NAME_AMULET1]
@@ -151,28 +153,32 @@ class AmuletToolController(wx.App):
 
     def _set_skill_list_from_amulet(self, amulet_id):
         u""" お守り種類の選択が変わった時の動作 """
-        skill_selected = []
+
         for i, (combo, skill_dict) in enumerate(zip(
                 [self.frame_view.combo_box_amulet_search_skill1, 
                     self.frame_view.combo_box_amulet_search_skill2], 
                 [self._amulet_id2skill1_id_dict, self._amulet_id2skill2_id_dict]
                 )):
-            combo.Clear()
-            if i == 1:
-                combo.Append(view.VAL_NO_SKILL)
             skill_ids = skill_dict[amulet_id]
-            for skill_id in skill_ids:
-                combo.Append(self._skill_id2name_dict[skill_id])
-            skill_selected.append(skill_ids[i])
+            skill_names = [self._skill_id2name_dict[_id] for _id in skill_ids]
+            if i == 0:
+                combo.SetItems(skill_names)
+                skill1_selected = skill_ids[0]
+            elif i == 1:
+                combo.SetItems([view.VAL_NO_SKILL] + skill_names)
+            else:
+                raise NotImplementedError(u"this iteration must be conducted only twice!")
             combo.SetSelection(0)   # combo1 = skill1, combo2 = No Skill
-        self._set_spin_range(amulet_id, skill_selected[0], skill_selected[1])
+        self._set_spin_range(amulet_id, skill1_selected, None)
 
     def _set_spin_views(self):
         u""" Spin Ctrlの最大最小をセット"""
         min1, max1, min2, max2 = self.db_accessor.get_skill_minmax()
         self.frame_view.spin_ctrl_amulet_search_slot_num.SetRange(view.SLOT_MIN, view.SLOT_MAX)
-        self.frame_view.spin_ctrl_highlight.SetRange(view.THRESHOLD2_MIN, view.THRESHOLD2_MAX)
-        self.frame_view.spin_ctrl_highlight.SetValue(self._highlight_threshold)
+        self.frame_view.spin_ctrl_highlight1.SetRange(view.THRESHOLD1_MIN, view.THRESHOLD1_MAX)
+        self.frame_view.spin_ctrl_highlight2.SetRange(view.THRESHOLD2_MIN, view.THRESHOLD2_MAX)
+        self.frame_view.spin_ctrl_highlight1.SetValue(self._highlight_threshold1)
+        self.frame_view.spin_ctrl_highlight2.SetValue(self._highlight_threshold2)
 
     def _set_check_list_views(self):
         u""" CheckListBoxの値のセット """
@@ -211,10 +217,12 @@ class AmuletToolController(wx.App):
         self.frame_view.text_ctrl_seed_result.SetValue(u"""Seedの候補は{0}個です。""".format(len(seed_sets)))
 
         if len(seed_sets) > 0:
-            for seed in seed_sets:
-                self.frame_view.list_box_seed.Append(u"{0}".format(seed))
+            self.frame_view.list_box_seed.SetItems([u"{0}".format(seed) for seed in seed_sets])
             self.frame_view.list_box_seed.SetSelection(0)
             self.frame_view.button_skill_from_seed_search.Enable()
+        else:
+            self.frame_view.list_box_seed.Clear()
+
 
     def OnClickSeedClear(self, evt):
         u""" reset seed search settings of combo box"""
@@ -222,6 +230,7 @@ class AmuletToolController(wx.App):
         for amu_key in [view.KEY_AMULET1, view.KEY_AMULET2, view.KEY_AMULET3]:
             for combo in combo_dict[amu_key]:
                 combo.SetSelection(0)
+        self.frame_view.text_ctrl_seed_result.SetValue(u"")
         self.frame_view.button_skill_from_seed_search.Disable()
         self.frame_view.list_box_seed.Clear()
 
@@ -238,17 +247,18 @@ class AmuletToolController(wx.App):
         seed = self.frame_view.text_ctrl_seed_select.GetValue()
         if seed.isdigit():
             seed = int(seed)
-            skill_dict, threshold2_dict = self.db_accessor.select_skills_from_seeds([seed])
-
+            skill_dict, threshold1_dict = self.db_accessor.select_skills_from_seeds([seed])
+            grid = self.frame_view.skill2_grid
             try:
-                text_ctrl_dict = self.frame_view.text_ctrl_seed_skill_dict
                 for amu_key, amu_name in zip([view.KEY_AMULET1, view.KEY_AMULET2, view.KEY_AMULET3], 
                     [view.NAME_AMULET1, view.NAME_AMULET2, view.NAME_AMULET3]):
                     amu_id = self._amulet_name2id_dict[amu_name]
-                    for txt_ctr, skill_id in zip(text_ctrl_dict[amu_key], skill_dict[amu_id][seed]):
-                        txt_ctr.SetValue(self._skill_id2name_dict[skill_id])
-                for txt_ctr, threshold2 in zip(text_ctrl_dict[view.KEY_THRESHOLD2], threshold2_dict[seed]):
-                    txt_ctr.SetValue(u"{0}".format(threshold2))
+                    amu_col = view.DICT_SKILL2_GRID_COL[amu_key]
+                    for row, skill_id in enumerate(skill_dict[amu_id][seed]):
+                        grid.SetCellValue(row, amu_col, self._skill_id2name_dict[skill_id])
+                th1_col = view.DICT_SKILL2_GRID_COL[view.KEY_THRESHOLD1]
+                for row, threshold1 in enumerate(threshold1_dict[seed]):
+                    grid.SetCellValue(row, th1_col, u"{0}".format(threshold1))
                 self._update_highlight()
 
             except KeyError, e:
@@ -258,15 +268,13 @@ class AmuletToolController(wx.App):
 
     def OnClickSkillClear(self, evt):
         u""" clear skills from seed """
-        text_ctrl_dict = self.frame_view.text_ctrl_seed_skill_dict
-        for amu_key, amu_name in zip([view.KEY_AMULET1, view.KEY_AMULET2, view.KEY_AMULET3], 
-            [view.NAME_AMULET1, view.NAME_AMULET2, view.NAME_AMULET3]):
-            amu_id = self._amulet_name2id_dict[amu_name]
-            for txt_ctr in text_ctrl_dict[amu_key]:
-                txt_ctr.Clear()
-        for txt_ctr in text_ctrl_dict[view.KEY_THRESHOLD2]:
-            txt_ctr.Clear()
+
+        grid = self.frame_view.skill2_grid
+        for row in range(view.NUM_SKILL2_GRID_ROW):
+            for col in range(view.NUM_SKILL2_GRID_COL):
+                grid.SetCellValue(row, col, "")
         self.frame_view.list_box_seed_skill_amulet_prospect.Clear() 
+        self._update_highlight()
 
     def OnClickAmuletRadio(self, evt):
         u""" switch skill lists by amulet id """
@@ -300,7 +308,6 @@ class AmuletToolController(wx.App):
             raise IndexError(u"amulet id is unknown")
         self._set_spin_range(amu_id, skill1_id, skill2_id)
 
-
     def OnClickAmuletSearch(self, evt):
         u""" search seeds from amulet condition """
         skill1_name = self.frame_view.combo_box_amulet_search_skill1.GetStringSelection()
@@ -337,9 +344,7 @@ class AmuletToolController(wx.App):
                 if len(seeds_set) > 0:
                     self.frame_view.text_ctrl_amulet_search_result.SetValue(
                         u"{0}個のSeedで出現するお守りです".format(len(seeds_set)))
-                    list_box.Clear()
-                    for seed in seeds_set:
-                        list_box.Append(u"{0}".format(seed))
+                    list_box.SetItems([u"{0}".format(seed) for seed in seeds_set])
                     list_box.SetSelection(0)
                     self.frame_view.button_amulet_search_skill.Enable()
                 else:
@@ -349,7 +354,7 @@ class AmuletToolController(wx.App):
                     list_box.Clear()
             else:
                 self.frame_view.text_ctrl_amulet_search_result.SetValue(
-                    u"指定されたお守りは見つかりませんでした")
+                    u"エラー。充足値が計算できません")
                 self.frame_view.button_amulet_search_skill.Disable()
                 list_box.Clear()
 
@@ -372,7 +377,8 @@ class AmuletToolController(wx.App):
 
     def OnClickSettingOK(self, evt):
         u""" get settings of setting tab """
-        self._highlight_threshold = self.frame_view.spin_ctrl_highlight.GetValue()
+        self._highlight_threshold1 = self.frame_view.spin_ctrl_highlight1.GetValue()
+        self._highlight_threshold2 = self.frame_view.spin_ctrl_highlight2.GetValue()
         self._highlight_skills = set([self._skill_name2id_dict[x] for x in 
                 self.frame_view.check_list_box_highlight_skill.GetCheckedStrings()
                 if x in self._skill_name2id_dict])
@@ -380,8 +386,10 @@ class AmuletToolController(wx.App):
 
     def OnClickSettingClear(self, evt):
         u""" reset settings of setting tab """
-        self._highlight_threshold = view.HIGHLIGHT_THRESHOLD
-        self.frame_view.spin_ctrl_highlight.SetValue(view.HIGHLIGHT_THRESHOLD)
+        self._highlight_threshold1 = view.HIGHLIGHT_THRESHOLD1
+        self._highlight_threshold2 = view.HIGHLIGHT_THRESHOLD2
+        self.frame_view.spin_ctrl_highlight1.SetValue(view.HIGHLIGHT_THRESHOLD1)
+        self.frame_view.spin_ctrl_highlight2.SetValue(view.HIGHLIGHT_THRESHOLD2)
         self._highlight_skills = set()
         for idx in self.frame_view.check_list_box_highlight_skill.GetChecked():
             self.frame_view.check_list_box_highlight_skill.Check(idx, False)
@@ -389,21 +397,23 @@ class AmuletToolController(wx.App):
 
     def _update_highlight(self):
         u""" update highlight cells """
-        text_dict = self.frame_view.text_ctrl_seed_skill_dict
+        grid = self.frame_view.skill2_grid
         for key in [view.KEY_AMULET1, view.KEY_AMULET2, view.KEY_AMULET3]:
-            for text_ctrl in text_dict[key]:
-                val = text_ctrl.GetValue()
+            col = view.DICT_SKILL2_GRID_COL[key]
+            for row in range(view.NUM_SKILL2_GRID_ROW):
+                val = grid.GetCellValue(row, col)
                 if (val in self._skill_name2id_dict and
                         self._skill_name2id_dict[val] in self._highlight_skills):
-                    text_ctrl.SetBackgroundColour("Yellow")
+                    grid.SetCellBackgroundColour(row, col, "Yellow")
                 else:
-                    text_ctrl.SetBackgroundColour(wx.NullColor)
-        for text_ctrl in text_dict[view.KEY_THRESHOLD2]:
-            val = text_ctrl.GetValue()
-            if val.isdigit() and int(val) >= self._highlight_threshold:
-                text_ctrl.SetBackgroundColour("Yellow")
+                    grid.SetCellBackgroundColour(row, col, wx.NullColor)
+        col = view.DICT_SKILL2_GRID_COL[view.KEY_THRESHOLD1]
+        for row in range(view.NUM_SKILL2_GRID_ROW):
+            val = grid.GetCellValue(row, col)
+            if val.isdigit() and int(val) >= self._highlight_threshold1:
+                grid.SetCellBackgroundColour(row, col, "Yellow")
             else:
-                text_ctrl.SetBackgroundColour(wx.NullColor)
+                grid.SetCellBackgroundColour(row, col, wx.NullColor)
 
 
     def _show_error_dialog(self, message=u"予期せぬエラーが発生しました", caption=u"エラー"):
@@ -446,7 +456,8 @@ class AmuletToolController(wx.App):
 
     def _write_settings(self):
         with open(SETTING_FILE, mode="w") as f:
-            data = {SETTING_THRESHOLD:self._highlight_threshold, 
+            data = {SETTING_THRESHOLD1:self._highlight_threshold1, 
+                    SETTING_THRESHOLD2:self._highlight_threshold2, 
                     SETTING_SKILLS:self._highlight_skills}
             pickle.dump(data, f)
 
@@ -455,13 +466,16 @@ class AmuletToolController(wx.App):
             with open(SETTING_FILE, mode="r") as f:
                 try:
                     data = pickle.load(f)
-                    self._highlight_threshold = data[SETTING_THRESHOLD] 
+                    self._highlight_threshold1 = data[SETTING_THRESHOLD1] 
+                    self._highlight_threshold2 = data[SETTING_THRESHOLD2] 
                     self._highlight_skills = data[SETTING_SKILLS]
                 except EOFError, e:
-                    self._highlight_threshold = view.HIGHLIGHT_THRESHOLD
+                    self._highlight_threshold1 = view.HIGHLIGHT_THRESHOLD1
+                    self._highlight_threshold2 = view.HIGHLIGHT_THRESHOLD2
                     self._highlight_skills = set()
         else:
-            self._highlight_threshold = view.HIGHLIGHT_THRESHOLD
+            self._highlight_threshold1 = view.HIGHLIGHT_THRESHOLD1
+            self._highlight_threshold2 = view.HIGHLIGHT_THRESHOLD2
             self._highlight_skills = set()
 
 if __name__ == "__main__":