Return to repo list

opet

Oblivion Proton .esp Tool
Return to HMagellan.com

opet.py (9682B)


      1 import wx, os
      2 from . import filer, tools, wxcustom, datas
      3 
      4 ###########
      5 # opet.py #
      6 ###########
      7 
      8 # This file contains:
      9 #   1. The OPET class, which is the main application object
     10 #   2. The PrimeFrame class, which represents the dominant OPET frame and is managed by OPET
     11 
     12 ##########################
     13 # Section 1 - OPET class #
     14 ##########################
     15 
     16 class OPET(object):
     17     """
     18     OPET is the main application object. It
     19     manages the other constituent objects
     20     and controls the application's entire
     21     operation.
     22     """
     23     def __init__(self):
     24 
     25         # Various tools
     26         self.filer = filer.Filer(self, "environment.json")
     27         self.environment = self.filer.get_environment()
     28         self.esp_tool = tools.DataPackTool(self)
     29 
     30         # Data
     31         rel = self.filer.read_raw_file(os.path.join(os.path.expanduser(self.environment["OblivionPrefixAppdata"]), "Plugins.txt"))
     32         enabled_list = []
     33         for l in rel:
     34             if l[0] not in ['#', '\n']:
     35                 if l[len(l) - 1] == '\n':
     36                     l = l[0:len(l) - 1]
     37                 enabled_list.append(l)
     38         self.esp_data = datas.OrderList(self, self.esp_tool.get_load_order_list(os.path.expanduser(self.environment["OblivionDataPath"])), enabled_list)
     39 
     40         # Load up app and frame components
     41         self.app = wx.App()
     42         self.app_frame = OPETFrame(self, None, title = "OPET", size = (1200, 800))
     43         self.app_frame.Show()
     44         self.app.MainLoop()
     45 
     46 ################################
     47 # Section 2 - PrimeFrame class #
     48 ################################
     49 
     50 class OPETFrame(wx.Frame):
     51     """
     52     The wxWidget frame representing the
     53     application's main window.
     54     """
     55 
     56     def __init__(self, manager, *args, **kwargs):
     57 
     58         # Parent initialization
     59         super(OPETFrame, self).__init__(*args, **kwargs)
     60 
     61         # Important vals
     62         self.manager = manager
     63 
     64         # Frame geometry
     65         self.main_panel = wx.Panel(self)
     66         self.main_sizer = wx.BoxSizer(wx.VERTICAL)
     67         self.main_panel.SetSizer(self.main_sizer)
     68         
     69         # Construct menu bar
     70         # .. File menu
     71         self.file_menu = wx.Menu()
     72         self.new_profile_item = self.file_menu.Append(-1, "&New Profile...\tCtrl-N", "Create a new empty mod list profile")
     73         self.Bind(wx.EVT_MENU, self.on_new_profile, self.new_profile_item)
     74         self.open_profile_item = self.file_menu.Append(-1, "&Open Profile...\tCtrl-O", "Open an existing mod list profile")
     75         self.Bind(wx.EVT_MENU, self.on_open_profile, self.open_profile_item)
     76         self.close_profile_item = self.file_menu.Append(-1, "Close Profile...\tCtrl-Q", "Close current mod list profile")
     77         self.Bind(wx.EVT_MENU, self.on_close_profile, self.close_profile_item)
     78         self.file_menu.AppendSeparator()
     79         self.save_profile_item = self.file_menu.Append(-1, "&Save Profile...\tCtrl-S", "Save current mod list")
     80         self.Bind(wx.EVT_MENU, self.on_save_profile, self.save_profile_item)
     81         self.save_profile_as_item = self.file_menu.Append(-1, "&Save Profile As...\tCtrl-Shift-S", "Save current mod list as...")
     82         self.Bind(wx.EVT_MENU, self.on_save_profile_as, self.save_profile_as_item)
     83         self.file_menu.AppendSeparator()
     84         self.exit_item = self.file_menu.Append(wx.ID_EXIT)
     85         self.Bind(wx.EVT_MENU, self.on_exit, self.exit_item)
     86         # .. Edit menu
     87         self.edit_menu = wx.Menu()
     88         # .. Tools menu
     89         self.tools_menu = wx.Menu()
     90         # .. Help menu
     91         self.help_menu = wx.Menu()
     92         self.about_item = self.help_menu.Append(wx.ID_ABOUT)
     93         self.Bind(wx.EVT_MENU, self.on_about, self.about_item)
     94         # Menu bar itself
     95         self.menu_bar = wx.MenuBar()
     96         self.menu_bar.Append(self.file_menu, "&File")
     97         self.menu_bar.Append(self.edit_menu, "&Edit")
     98         self.menu_bar.Append(self.tools_menu, "&Tools")
     99         self.menu_bar.Append(self.help_menu, "&Help")
    100         self.SetMenuBar(self.menu_bar)
    101 
    102         # Construct sub-panel
    103         self.sub_panel = wx.Panel(self.main_panel)
    104         self.sub_panel_sizer = wx.BoxSizer(wx.VERTICAL)
    105         self.sub_panel.SetSizer(self.sub_panel_sizer)
    106         self.sub_panel.SetSize(4, 4, 1200, 800)
    107 
    108         # Construct esp list
    109         self.esp_list_panel = wx.Panel(self.sub_panel)
    110         self.esp_list_sizer = wx.BoxSizer(wx.VERTICAL)
    111         self.esp_list_panel.SetSizer(self.esp_list_sizer)
    112         self.esp_list_ctrl = wxcustom.ESPListCtrl(self.esp_list_panel, size = (800, 500))
    113         self.esp_list_ctrl.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginLabelEdit)
    114         self.esp_list_ctrl.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEndLabelEdit)
    115         self.update_esp_list(self.manager.esp_data.get_data())
    116         self.esp_buttons_panel = wx.Panel(self.sub_panel)
    117         self.esp_buttons_panel_sizer = wx.BoxSizer(wx.HORIZONTAL)
    118         self.esp_buttons_panel.SetSizer(self.esp_buttons_panel_sizer)
    119         self.esp_apply_button = wxcustom.ESPApplyButton(self.esp_buttons_panel)
    120         self.esp_apply_button.Bind(wx.EVT_BUTTON, self.OnApplyChanges)
    121         self.esp_list_panel.SetSize(8, 8, 800, 600)
    122         self.esp_buttons_panel.SetSize(8, 600, 800, 100)
    123 
    124     def update_esp_list(self, data):
    125         self.esp_list_ctrl.DeleteAllItems()
    126         for e in data:
    127             entry = ["", e["order"], e["name"], e["comments"]]
    128             self.esp_list_ctrl.Append(entry)
    129             if e["enabled"]:
    130                 self.esp_list_ctrl.CheckItem(int(e["order"]), True)
    131         self.manager.esp_data.set_data(data)
    132 
    133     def sort_esp_data_by_order(self, data):
    134         # TODO: This is terrible and inefficient
    135         simsor = sorted(data,  key=lambda k: k["order"])
    136         j = None
    137         for i in simsor:
    138             if i["just_changed"]:
    139                 if j["order"] == i["order"]:
    140                     j["order"] += 1
    141             elif j != None and j["just_changed"]:
    142                 if i["order"] == j["order"]:
    143                     i["order"] -= 1
    144             j = i
    145         simsor = sorted(data,  key=lambda k: k["order"])
    146         oldor = -1
    147         for i in simsor:
    148             if i["order"] != oldor + 1:
    149                 i["order"] = oldor + 1
    150             oldor = i["order"]
    151         return simsor
    152 
    153     # TODO: Cross-object binding issues indicate larger program construction problems???
    154     def OnApplyChanges(self, event):
    155         data = []
    156         for row in range(self.esp_list_ctrl.GetItemCount()):
    157             n = self.esp_list_ctrl.GetItem(row, col = 2).GetText()
    158             o = int(self.esp_list_ctrl.GetItem(row, col = 1).GetText())
    159             e = self.esp_list_ctrl.IsItemChecked(row)
    160             c = self.esp_list_ctrl.GetItem(row, col = 3).GetText()
    161             dd = { "name" : n, "order" : o, "enabled" : e, "comments" : c, "just_changed" : False }
    162             data.append(dd)
    163         self.update_esp_list(self.sort_esp_data_by_order(data))
    164         self.manager.esp_tool.assign_load_order(self.manager.esp_data.get_data(), self.manager.environment)
    165         self.manager.esp_tool.enable_esps(self.manager.esp_data.get_data(), self.manager.environment)
    166     def OnBeginLabelEdit(self, event):
    167         col = event.GetColumn()
    168         fi = self.esp_list_ctrl.GetFocusedItem()
    169         if col == 0:
    170             event.Veto()
    171             if fi != -1:
    172                 self.esp_list_ctrl.CheckItem(fi, not self.esp_list_ctrl.IsItemChecked(fi))
    173         elif col == 1:
    174             if fi != -1:
    175                 self.esp_list_ctrl.last_order_value = int(self.esp_list_ctrl.GetItem(fi, 1).GetText())
    176             event.Skip()
    177         elif col == 2:
    178             event.Veto()
    179         else:
    180             event.Skip()
    181     def OnEndLabelEdit(self, event):
    182         if event.GetColumn() == 1:
    183             try:
    184                 lbint = int(event.GetLabel())
    185             except ValueError:
    186                 lbint = int(self.esp_list_ctrl.GetItem(event.GetItem(), event.GetColumn())) 
    187             if lbint < 0:
    188                 lbint = 0
    189             if self.esp_list_ctrl.last_order_value != None and lbint != self.esp_list_ctrl.last_order_value:
    190                 self.esp_list_ctrl.SetItem(event.GetIndex(), event.GetColumn(), str(lbint))
    191                 # TODO: This is a bit fragile and relies on file names...
    192                 entry = self.esp_list_ctrl.GetItem(event.GetIndex(), col = 2).GetText()
    193                 data = []
    194                 for row in range(self.esp_list_ctrl.GetItemCount()):
    195                     n = self.esp_list_ctrl.GetItem(row, col = 2).GetText()
    196                     o = int(self.esp_list_ctrl.GetItem(row, col = 1).GetText())
    197                     e = self.esp_list_ctrl.IsItemChecked(row)
    198                     c = self.esp_list_ctrl.GetItem(row, col = 3).GetText()
    199                     j = True if n == entry else False
    200                     dd = { "name" : n, "order" : o, "enabled" : e, "comments" : c, "just_changed" : j }
    201                     data.append(dd)
    202                 self.update_esp_list(self.sort_esp_data_by_order(data))
    203                 for i in self.manager.esp_data.get_data():
    204                     if i["just_changed"]:
    205                         #self.esp_list_ctrl.EnsureVisible(int(i["order"]))
    206                         self.esp_list_ctrl.Focus(int(i["order"]))
    207                         break
    208                 event.Veto()
    209 
    210     def on_new_profile(self, event):
    211         pass
    212     def on_open_profile(self, event):
    213         pass
    214     def on_close_profile(self, event):
    215         pass
    216     def on_save_profile(self, event):
    217         pass
    218     def on_save_profile_as(self, event):
    219         pass
    220     def on_exit(self, event):
    221         self.Close(True)
    222     def on_about(self, event):
    223         wx.MessageBox("Test", "Test test", wx.OK|wx.ICON_INFORMATION)