merchants.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # GPL3 or any later version
  2. import os
  3. import json
  4. import time
  5. import gi
  6. import threading
  7. gi.require_version('Gtk', '3.0')
  8. from gi.repository import Gtk
  9. from gi.repository import Gdk
  10. from gi.repository import GLib
  11. from modules import wallet
  12. from modules import images
  13. from modules import ui
  14. def select_dialogue(win, mode="normal"):
  15. # This is an item selector.
  16. dialogWindow = Gtk.Dialog("Select Merchant",
  17. buttons=(),
  18. )
  19. dialogWindow.set_size_request(400, 400)
  20. r = Gtk.VBox()
  21. r.ret = None
  22. def todo(w, win, item):
  23. r.ret = item
  24. dialogWindow.destroy()
  25. box = dialogWindow.get_content_area()
  26. select_widget(win, box, todo, mode=mode)
  27. box.show_all()
  28. response = dialogWindow.run()
  29. dialogWindow.destroy()
  30. return r.ret
  31. def select_widget(win, box, todo, mode="normal"):
  32. # Draws widget for searching and selecting items.
  33. search_box = Gtk.HBox()
  34. box.pack_start(search_box, 0,0,1)
  35. def on_changed(w):
  36. win.search_item = search.get_text()
  37. def on_search(w):
  38. refresh_items(win, pack, todo, search=search.get_text())
  39. search = Gtk.Entry()
  40. search.connect("changed", on_changed)
  41. search.connect("activate", on_search)
  42. search_box.pack_start(search, 1,1,0)
  43. scrl = Gtk.ScrolledWindow()
  44. scrl.set_vexpand(0)
  45. box.pack_start(scrl, 1,1,1)
  46. pack = Gtk.VBox()
  47. scrl.add(pack)
  48. refresh_items(win, pack, todo)
  49. return (win, pack, todo)
  50. def refresh_items(win, pack, todo, search=""):
  51. for i in pack.get_children():
  52. i.destroy()
  53. flowbox = Gtk.FlowBox()
  54. flowbox.set_valign(Gtk.Align.START)
  55. flowbox.set_max_children_per_line(30)
  56. flowbox.set_selection_mode(Gtk.SelectionMode.NONE)
  57. def on_add_item(w):
  58. editor(win, new=True)
  59. refresh_items(win, pack, todo, search="")
  60. add_item = Gtk.Button()
  61. add_item.add(ui.icon(win, "list-add"))
  62. add_item.set_relief(Gtk.ReliefStyle.NONE)
  63. flowbox.add(add_item)
  64. add_item.connect("clicked", on_add_item)
  65. folder = wallet.save_folder()+"merchants/"
  66. try:
  67. items = list(os.walk(folder))[0][2]
  68. items = sorted(items)
  69. except:
  70. items = []
  71. for i in items:
  72. with open(folder+i) as json_file:
  73. idata = json.load(json_file)
  74. if search:
  75. if search.lower() not in idata.get("name").lower() and search.lower() not in i.lower():
  76. continue
  77. item_button = Gtk.Button()
  78. item_button.connect("clicked", todo, win, i)
  79. item_button.set_relief(Gtk.ReliefStyle.NONE)
  80. item_box = Gtk.VBox()
  81. item_button.add(item_box)
  82. save_as = idata.get("image", "")
  83. if os.path.exists(save_as):
  84. image = ui.load(win, ui.net_image_calculation, ui.net_image_render, "", 100 , save_as, False)
  85. else:
  86. image = ui.icon(win, "go-home")
  87. item_box.pack_start(image, 0,0,0)
  88. item_box.pack_start(Gtk.Label(str(idata.get("name", ""))), 0,0,0)
  89. item_box.pack_start(Gtk.Label(str(idata.get("address", ""))),0,0,0)
  90. flowbox.add(item_button)
  91. flowbox.show_all()
  92. pack.pack_start(flowbox, 1,1,1)
  93. def editor(win, item="", new=False):
  94. # editor
  95. folder = wallet.save_folder()+"merchants/"
  96. try:
  97. os.makedirs(folder)
  98. except:
  99. pass
  100. data = {}
  101. edit_id = True
  102. try:
  103. search = win.search_item
  104. except:
  105. search = ""
  106. if not item:
  107. item = search
  108. if item:
  109. try:
  110. if not item.endswith(".json"):
  111. item = item+".json"
  112. with open(folder+item) as json_file:
  113. data = json.load(json_file)
  114. item = item.replace(".json", "")
  115. edit_id = False
  116. except:
  117. pass
  118. if not data:
  119. data = {
  120. "name":"",
  121. "image":"",
  122. "address":"",
  123. "phone":"",
  124. "email":""}
  125. try:
  126. item = str(int(search))
  127. except:
  128. data["name"] = search
  129. dialogWindow = Gtk.Dialog("Merchant",
  130. buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  131. Gtk.STOCK_OK, Gtk.ResponseType.OK),
  132. )
  133. dialogWindow.set_size_request(200, 200)
  134. box = dialogWindow.get_content_area()
  135. main_data_box = Gtk.HBox()
  136. box.pack_start(main_data_box, 1,1,1)
  137. # IMAGE
  138. def image_edit(w):
  139. search = address_entry.get_text()+" "+name_entry.get_text()
  140. if search:
  141. images.select_image(win, search)
  142. save_as = win.return_search
  143. data["image"] = save_as
  144. add_image.get_children()[0].destroy()
  145. image = ui.load(win, ui.net_image_calculation, ui.net_image_render, "", 100 , save_as, False)
  146. add_image.add(image)
  147. add_image.show_all()
  148. add_image = Gtk.Button()
  149. main_data_box.pack_start(add_image, 0,0,0)
  150. add_image.connect("clicked", image_edit)
  151. if not data.get("image"):
  152. add_image.add(ui.icon(win, "image-x-generic"))
  153. add_image.set_relief(Gtk.ReliefStyle.NONE)
  154. else:
  155. save_as = data.get("image")
  156. image = ui.load(win, ui.net_image_calculation, ui.net_image_render, "", 100 , save_as, False)
  157. add_image.add(image)
  158. text_data_box = Gtk.VBox()
  159. main_data_box.pack_start(text_data_box, 1,1,1)
  160. # ID
  161. def on_entry(w, d):
  162. data[d] = w.get_text()
  163. id_box = Gtk.HBox()
  164. text_data_box.pack_start(id_box, 0,0,5)
  165. id_box.pack_start(Gtk.Label(" ID: "), 0,0,0)
  166. id_entry = Gtk.Entry()
  167. id_entry.grab_focus()
  168. id_entry.set_sensitive(edit_id)
  169. id_box.pack_end(id_entry, 0,0,0)
  170. # NAME
  171. name_box = Gtk.HBox()
  172. text_data_box.pack_start(name_box, 0,0,5)
  173. name_box.pack_start(Gtk.Label(" Name: "), 0,0,0)
  174. name_entry = Gtk.Entry()
  175. name_entry.connect("changed", on_entry, "name")
  176. name_box.pack_end(name_entry, 0,0,0)
  177. # Address
  178. address_box = Gtk.HBox()
  179. text_data_box.pack_start(address_box, 0,0,5)
  180. address_box.pack_start(Gtk.Label(" Address: "), 0,0,0)
  181. address_entry = Gtk.Entry()
  182. address_entry.connect("changed", on_entry, "address")
  183. address_box.pack_end(address_entry, 0,0,0)
  184. # Phone
  185. phone_box = Gtk.HBox()
  186. text_data_box.pack_start(phone_box, 0,0,5)
  187. phone_box.pack_start(Gtk.Label(" Phone: "), 0,0,0)
  188. phone_entry = Gtk.Entry()
  189. phone_entry.connect("changed", on_entry, "phone")
  190. phone_box.pack_end(phone_entry, 0,0,0)
  191. # Email
  192. email_box = Gtk.HBox()
  193. text_data_box.pack_start(email_box, 0,0,5)
  194. email_box.pack_start(Gtk.Label(" Email: "), 0,0,0)
  195. email_entry = Gtk.Entry()
  196. email_entry.connect("changed", on_entry, "email")
  197. email_box.pack_end(email_entry, 0,0,0)
  198. # Auto filling
  199. if item:
  200. id_entry.set_text( str(item).replace(".json", "") )
  201. name_entry.grab_focus()
  202. name_entry.set_text(str(data.get("name", "")))
  203. address_entry.set_text(str(data.get("address", "")))
  204. phone_entry.set_text(str(data.get("phone", "")))
  205. email_entry.set_text(str(data.get("email", "")))
  206. box.show_all()
  207. response = dialogWindow.run()
  208. if response == Gtk.ResponseType.OK:
  209. item = id_entry.get_text()
  210. if new: # Failsafe for in case the user will accidentaly overwrite
  211. n = 0 # an existing item by mistake.
  212. nn = item
  213. while os.path.exists(folder+str(item)+".json"):
  214. n = n + 1
  215. item = nn+"_"+str(n)
  216. filename = folder+str(item)+".json"
  217. with open(filename, "w") as fp:
  218. json.dump(data, fp, indent=4)
  219. dialogWindow.destroy()