Handlers.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. #
  5. # Copyright 2022 Stephen Stengel <stephen.stengel@cwu.edu> and friends
  6. #
  7. #
  8. import multiprocessing
  9. multiprocessing.freeze_support() # this might be needed right after main starts...?
  10. import gi
  11. gi.require_version("Gtk", "3.0")
  12. from gi.repository import Gtk, GLib
  13. from FileChooser import FileChooser
  14. #global variable to kill a sorting process if the user clicks the exit button.
  15. globalSortingProcess = None
  16. class Handlers():
  17. isSortingHappening = False
  18. sortingProcess = None
  19. timeout_id = None
  20. def __init__(self,
  21. source_text_field,
  22. destination_text_field,
  23. progress_bar,
  24. run_button):
  25. # ~ multiprocessing.freeze_support() # this might be needed right after main starts...?
  26. self.source_text_field = source_text_field
  27. self.destination_text_field = destination_text_field
  28. self.progress_bar = progress_bar
  29. self.run_button = run_button
  30. def button_source_onclick(self, button):
  31. print("Source button clicked!")
  32. testChooser = FileChooser("Choose a source folder!")
  33. pathString = testChooser.pathStr
  34. print("Selected folder: " + str(pathString))
  35. if pathString is not None:
  36. self.source_text_field.set_text(pathString)
  37. def button_destination_clicked(self, button):
  38. print("Destination button clicked!")
  39. testChooser = FileChooser("Choose a destination folder!")
  40. pathString = testChooser.pathStr
  41. print("Selected folder: " + str(pathString))
  42. if pathString is not None:
  43. self.destination_text_field.set_text(pathString)
  44. #Need to add a check to make sure fields are not empty
  45. #maybe here, maybe in the sorter. Here makes sense because this can
  46. #get called over and over automatically. And make a popup error
  47. #message.
  48. def button_run_clicked(self, button):
  49. print("Run button clicked!")
  50. sourceStr = self.source_text_field.get_text()
  51. destStr = self.destination_text_field.get_text()
  52. if sourceStr == "":
  53. print("SOURCE STRING NULL!")
  54. print("\a")
  55. elif destStr == "":
  56. print("DEST STRING NULL!")
  57. print("\a")
  58. elif self.sortingProcess is None:
  59. from AnimalSorter import sortAnimalsIntoFolders #import lol
  60. print("Sorting...")
  61. self.sortingProcess = multiprocessing.Process(target=sortAnimalsIntoFolders, args=(sourceStr, destStr,))
  62. global globalSortingProcess
  63. globalSortingProcess = self.sortingProcess ## Tre grava amikoj! Always update together
  64. self.sortingProcess.start()
  65. self.start_timer()
  66. self.progress_bar.pulse()
  67. self.progress_bar.set_text("Processing...")
  68. self.progress_bar.set_show_text(True)
  69. self.run_button.set_sensitive(False)
  70. def myDestroy(self):
  71. global globalSortingProcess
  72. if globalSortingProcess is not None:
  73. if globalSortingProcess.is_alive():
  74. globalSortingProcess.terminate()
  75. def start_timer(self):
  76. self.timeout_id = GLib.timeout_add(1000, self.on_timeout, None)
  77. def stop_timer(self):
  78. if self.timeout_id:
  79. GLib.source_remove(self.timeout_id)
  80. self.timeout_id = None
  81. #This will check the status of the sorting process.
  82. #Reactivate button if done, else, pulse the progress bar.
  83. def on_timeout(self, *args, **kwargs):
  84. if self.timeout_id is not None:
  85. #Check if process is still running
  86. if self.sortingProcess is not None:
  87. if self.sortingProcess.is_alive():
  88. self.progress_bar.pulse()
  89. else:
  90. self.stop_timer()
  91. #reset the progress bar?
  92. self.progress_bar.set_fraction(0.0)
  93. self.progress_bar.set_text("Done!")
  94. self.progress_bar.set_show_text(True)
  95. #Set the run button back to clickable
  96. self.run_button.set_sensitive(True)
  97. self.sortingProcess = None
  98. global globalSortingProcess
  99. globalSortingProcess = self.sortingProcess #always update together.
  100. return True
  101. else:
  102. return False