Handlers.py 3.5 KB

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