Handlers.py 3.7 KB

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