Skip to content

Commit

Permalink
Added (Still a dummy) ask a question dialog
Browse files Browse the repository at this point in the history
Slowly but surely, I'm finishing it.

I solved the previous problem by hiding and showing the window, rather than closing it and creating it from scratch.
  • Loading branch information
alekssamos committed Aug 2, 2024
1 parent 1e58519 commit 83627f5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 35 deletions.
30 changes: 24 additions & 6 deletions addon/globalPlugins/CloudVision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import tones
import wx
import config
import globalVars
import globalPluginHandler
import gui
import scriptHandler
Expand Down Expand Up @@ -127,7 +128,7 @@ def postInit(self):
self.sound.SetFocus()

def on_manage_account_button(self, event):
event.Skip()
if event: event.Skip()
account_dialog = bmgui.MainDialog( self.FindWindowByName("cvsettings"), )
account_dialog.ShowModal()

Expand Down Expand Up @@ -208,6 +209,7 @@ def __init__(self):
wx.EVT_MENU, lambda evt: gui.mainFrame._popupSettingsDialog(SettingsDialog), self.CloudVisionSettingsItem)

def terminate(self):
globalVars.cvask = None
try:
gui.mainFrame.sysTrayIcon.preferencesMenu.RemoveItem(
self.CloudVisionSettingsItem)
Expand All @@ -220,11 +222,27 @@ def terminate(self):
isWorking = False

def on_ask_bm(self, evt):
gui.mainFrame.prePopup()
d = bmgui.AskFrame(gui.mainFrame)
d.Show()
gui.mainFrame.postPopup()
#d.postInit()
if not self.last_resp:
queueHandler.queueFunction(queueHandler.eventQueue, ui.message, _("There have been no recognitions yet"))
return
if not getattr(globalVars, "cvask", None):
gui.mainFrame.prePopup()
d = bmgui.AskFrame(gui.mainFrame)
d.Show()
gui.mainFrame.postPopup()
globalVars.cvask = d
else:
d=globalVars.cvask
d.ask_panel.messages_list.ClearAll()
d.ask_panel.question_input.SetValue("")
d.Show()
d.postInit()
if not bmgui.bm().bm_authorized:
d.ask_panel.messages_list.InsertItem(0, _("First you need to log in or register"))
d.ask_panel.messages_list.InsertItem(1, _("Open NVDA Menu, Preferences, CloudVision Settings, Manage Be My Eyes account"))
d.ask_panel.messages_list.SetFocus()
else:
if self.last_resp: d.ask_panel.messages_list.InsertItem(0, self.last_resp)

def getFilePath(self): #For this method thanks to some nvda addon developers ( code snippets and suggestion)
fg = api.getForegroundObject()
Expand Down
88 changes: 59 additions & 29 deletions addon/globalPlugins/CloudVision/bm/account_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import wx


class bm:
url = "https://visionbot.ru/apiv2/"

Expand All @@ -32,40 +33,47 @@ def __init__(self):

@property
def bm_token(self):
with open(bm_token_file) as f: return f.read(90)
with open(bm_token_file) as f:
return f.read(90).strip()

@property
def bm_chat_id(self):
with open(bm_chat_id_file) as f: return int(f.read(90))
with open(bm_chat_id_file) as f:
return int(f.read(90).strip())

@property
def bm_authorized(self):
return len(self.bm_token)>20
return len(self.bm_token) > 20

def ask(self, message, lang):
with open(bm_token_file, "r") as f:
bmtoken = f.read(90).strip()
with open(bm_chat_id_file, "r") as f:
bm_chat_id = f.read(90).strip()
is_chat_id_exists = False
try:
if int(bm_chat_id) != 0:
if int(self.bm_chat_id) != 0:
is_chat_id_exists = True
except ValueError:
raise APIError("chat id not found. First, recognize the picture")
params = {
"action": "ask",
"lang": lang,
"bmtoken": bmtoken,
"bm_chat_id": bm_chat_id,
"bm_token": self.bm_token,
"bm_chat_id": self.bm_chat_id,
"message": message,
}
r1 = ur.urlopen(self.url + "bm.php", data=up.urlencode(params).encode()).read().decode("UTF-8")
r1 = (
ur.urlopen(self.url + "bm.php", data=up.urlencode(params).encode())
.read()
.decode("UTF-8")
)
j1 = json.loads(r1)
for i in range(60):
r2 = ur.urlopen(
self.url + "res.php",
data=up.urlencode({"id": j1["id"]}).encode(),
).read().decode("UTF-8")
r2 = (
ur.urlopen(
self.url + "res.php",
data=up.urlencode({"id": j1["id"]}).encode(),
)
.read()
.decode("UTF-8")
)
j2 = json.loads(r2)
if j2["status"] == "error":
raise APIError(j2["status"])
Expand All @@ -84,10 +92,14 @@ def login(self, email, password, lang="en"):
"email": email,
"password": password,
}
r = ur.urlopen(self.url + "bm.php", data=up.urlencode(params).encode()).read().decode("UTF-8")
r = (
ur.urlopen(self.url + "bm.php", data=up.urlencode(params).encode())
.read()
.decode("UTF-8")
)
j = json.loads(r)
if j["status"] == "ok":
with open(os.path.join(CONFIGDIR, "bm_token.txt"), "w") as f:
with open(bm_token_file, "w") as f:
f.write(j["bmtoken"])
return j

Expand All @@ -100,10 +112,14 @@ def signup(self, first_name, last_name, email, password, lang="en"):
"email": email,
"password": password,
}
r = ur.urlopen(self.url + "bm.php", data=up.urlencode(params).encode()).read().decode("UTF-8")
r = (
ur.urlopen(self.url + "bm.php", data=up.urlencode(params).encode())
.read()
.decode("UTF-8")
)
j = json.loads(r)
if j["status"] == "ok":
with open(os.path.join(CONFIGDIR, "bm_token.txt"), "w") as f:
with open(bm_token_file, "w") as f:
f.write(j["bmtoken"])
return j

Expand Down Expand Up @@ -145,7 +161,7 @@ def on_login(self, event):
password = self.password_input.GetValue()
f = self.FindWindowByName("lrframe1")
b = bm()
res=b.login(email=email, password=password, lang=f.lang)
res = b.login(email=email, password=password, lang=f.lang)
wx.MessageBox(str(res))

def on_show_register_btn(self, event):
Expand Down Expand Up @@ -218,7 +234,7 @@ def on_register(self, event):
password = self.password_input.GetValue()
f = self.FindWindowByName("lrframe1")
b = bm()
res=b.signup(
res = b.signup(
first_name=name,
last_name=surname,
email=email,
Expand All @@ -230,12 +246,13 @@ def on_register(self, event):

class AskPanel(wx.Panel):
ask_tmr = None

def __init__(self, parent):
super().__init__(parent)

main_sizer = wx.BoxSizer(wx.VERTICAL)

messages_sizer = wx.BoxSizer(wx.HORIZONTAL)
messages_sizer = wx.BoxSizer(wx.VERTICAL)
self.messages_list = wx.ListCtrl(self)
messages_sizer.Add(self.messages_list, 1, wx.EXPAND | wx.ALL, 5)

Expand All @@ -260,22 +277,29 @@ def __init__(self, parent):

def on_send(self, event):
event.Skip()
if self.ask_tmr and self.ask_tmr.is_alive(): self.ask_tmr.cancel()
self.ask_tmr = Timer(0.1, self._on_send, [event,])
if self.ask_tmr and self.ask_tmr.is_alive():
self.ask_tmr.cancel()
self.ask_tmr = Timer(
0.1,
self._on_send,
[
event,
],
)
self.ask_tmr.start()

def _on_send(self, event):
message = self.question_input.GetValue()
f = self.FindWindowByName("askframe1")
try:
b = bm()
res=b.ask(message=message, lang=f.lang)
res = b.ask(message=message, lang=f.lang)
except APIError:
wx.MessageBox(str( sys.exc_info()[1] ), style=wx.ICON_ERROR)
wx.MessageBox(str(sys.exc_info()[1]), style=wx.ICON_ERROR)
return False
wx.MessageBox(str(res))

def on_close(self, event):
event.Skip()
f = self.FindWindowByName("askframe1")
f.Hide()

Expand All @@ -295,7 +319,7 @@ def __init__(self, parent=None, lang="en"):
sizer.Add(self.login_panel, 1, wx.EXPAND)
sizer.Add(self.register_panel, 1, wx.EXPAND)

bmtoken=""
bmtoken = ""
if os.path.isfile(bm_token_file):
with open(bm_token_file, "r") as f:
bmtoken = f.read(90).strip()
Expand All @@ -304,6 +328,7 @@ def __init__(self, parent=None, lang="en"):
self.SetSizer(sizer)
self.Layout()


class AskFrame(wx.Frame):
def __init__(self, parent=None, lang="en"):
super().__init__(parent=None)
Expand All @@ -318,9 +343,14 @@ def __init__(self, parent=None, lang="en"):
sizer.Add(self.ask_panel, 1, wx.EXPAND)
self.SetSizer(sizer)
self.Layout()
self.Bind(wx.EVT_CLOSE, self.on_close)

def postInit(self):
self.ask_panel.SetFocus()
self.Layout()
self.ask_panel.question_input.SetFocus()

def on_close(self, event):
self.Hide()


if __name__ == "__main__":
Expand Down

1 comment on commit 83627f5

@amirsol81
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alekssamos Great job! I can't wait to test it whenever it is released.

Please sign in to comment.