| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- """
- This file is part of RuneOptimizer.
- RuneOptimizer is free software: you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the Free
- Software Foundation, either version 3 of the License, or (at your option)
- any later version.
- RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- more details.
- You should have received a copy of the GNU General Public License along with
- RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
- """
- class DialogUpdateJson(wx.Dialog):
- """
- The JSON data updater dialog.
-
- Allows for JSON file selection and control options. Runs a update command.
- Parameters
- ----------
- update_done : boolean
- Indicates if the update process has been run (default False)
- update_error : boolean
- Indicates if the update process has been run with errors (default False)
- """
- _form_sizer = None
- _progress_sizer = None
- _message_sizer = None
- _button_sizer = None
- _file_selector = None
- _stars_check = None
- _runes_check = None
- _clear_teams_check = None
- update_done = False
- update_error = False
- _message_label = None
- _timer = None
- _process = None
- def __init__(self, parent, id=wx.ID_ANY,):
- """Initializes the panel.
- Sets upt all the widgets.
- Parameters
- ----------
- parent : wx.*
- Dialog parent.
- id : int, optional
- ID for the dialig (default wx.ID_ANY).
- """
- # Parent constructor
- wx.Dialog.__init__(
- self, parent=parent, id=id, pos=(50, 50), size=(400, 290),
- title="Update from JSON file", style=wx.DEFAULT_DIALOG_STYLE
- )
- self._form_sizer = wx.BoxSizer(wx.VERTICAL)
- self._progress_sizer = wx.BoxSizer(wx.VERTICAL)
- self._message_sizer = wx.BoxSizer(wx.VERTICAL)
- self._button_sizer = wx.BoxSizer(wx.VERTICAL)
- file_selector_label = wx.StaticText(
- parent=self, id=wx.ID_ANY, label="Select a JSON file",
- pos=(30, 30), size=(130, 30)
- )
- self._form_sizer.Add(file_selector_label)
- self._file_selector = wx.FilePickerCtrl(parent=self,
- id=wx.ID_ANY, path="",
- message="Select JSON file", wildcard="JSON files (*.json)|*.json",
- style=wx.FC_DEFAULT_STYLE, pos=(140, 20), size=(250, 40)
- )
- self._form_sizer.Add(self._file_selector)
- self._stars_check = wx.CheckBox(
- parent=self, id=wx.ID_ANY, label="Only import units at with 6 stars.",
- pos=(30, 80), size=(230, 20)
- )
- self._form_sizer.Add(self._stars_check)
- self._runes_check = wx.CheckBox(
- parent=self, id=wx.ID_ANY, label="Only import units with runes.",
- pos=(30, 110), size=(230, 20)
- )
- self._form_sizer.Add(self._runes_check)
- self._clear_teams_check = wx.CheckBox(
- parent=self, id=wx.ID_ANY, label="Clear team data",
- pos=(30, 140), size=(230, 20)
- )
- self._form_sizer.Add(self._clear_teams_check)
- self._accept_button = wx.Button(
- parent=self, id=wx.ID_ANY, pos=(100, 180),
- size=(100, 40), style=wx.LC_REPORT, label="Accept"
- )
- wx.Button(
- parent=self, id=wx.ID_OK, pos=(210, 180),
- size=(100, 40), style=wx.LC_REPORT, label="Close"
- )
- self._button_sizer.Add(self._accept_button)
- self._accept_button.Bind(wx.EVT_BUTTON, self._accept)
- anim = wx.adv.Animation(
- os.path.dirname(os.path.realpath(__file__)) + '/res/icon/progress.gif'
- )
- progress_ctrl = wx.adv.AnimationCtrl(
- parent=self, id=wx.ID_ANY, anim=anim, pos=(126, 176), size=(48, 48)
- )
- progress_ctrl.Play()
- self._progress_sizer.Add(progress_ctrl)
- # Progress message
- self._message_label = wx.StaticText(
- parent=self, id=wx.ID_ANY, label="",
- pos=(20, 20), size=(340, 460)
- )
- self._message_sizer.Add(self._message_label)
- # Hide progress bar and messages
- self._progress_sizer.ShowItems(False)
- self._message_sizer.ShowItems(False)
- def _check_process(self, event):
- """Checks the process output and updates the progress message.
- Parameters
- ----------
- event : wx.Event, optional
- The event that triggered the call.
- """
- if self._process is not None:
- stream = self._process.GetInputStream()
- if stream.CanRead():
- text = bytes.decode(stream.read())
- #text = text[:-1] # Remove the last newline
- self._message_label.SetLabel(text)
- else:
- self._timer.Stop()
- def _update_complete(self, event):
- """Called when the update process is complete.
- Hiddes the progress image, checks for errors in the output, prints a
- message annd sets self.update_done and self.update_error.
- Parameters
- ----------
- event : wx.Event, optional
- The event that triggered the call.
- """
- self._timer.Stop()
- self._progress_sizer.ShowItems(False)
- errStream = bytes.decode(self._process.GetErrorStream().read())
- if errStream == "":
- self._message_label.SetLabel(
- self._message_label.GetLabel() + "\nAll done!"
- )
- self.update_done = True
- self.update_error = False
- else:
- self._message_label.SetLabel(
- self._message_label.GetLabel() +
- "\nUpdate didnt' complete succesfully"
- )
- self.update_done = True
- self.update_error = True
- def _accept(self, event):
- """Called when the accept button is clicked.
- Hiddes the form and shows the progress image and message box, composes
- the command and launchs it
- Parameters
- ----------
- event : wx.Event, optional
- The event that triggered the call.
- """
- self._form_sizer.ShowItems(False)
- self._progress_sizer.ShowItems(True)
- self._message_sizer.ShowItems(True)
- self._button_sizer.ShowItems(False)
- # TODO: Executable name for windows
- command = "runeoptimizer update "
- command += pipes.quote(self._file_selector.GetPath())
- if self._stars_check.GetValue():
- command += " --six-stars"
- if self._runes_check.GetValue():
- command += " --with-runes"
- if self._clear_teams_check.GetValue():
- command += " --clear-teams"
- command += " --gui"
- print("Command: " + command)
- # Timer ot periodically check on the process
- self._timer = wx.Timer(self)
- self._timer.Start(1000)
- self._timer.Bind(wx.EVT_TIMER, self._check_process)
- # Create the process
- self.Bind(wx.EVT_END_PROCESS, self._update_complete)
- self._process = wx.Process(self)
- self._process.Redirect()
- wx.Execute(command, wx.EXEC_ASYNC, self._process)
|