Autocompletion works for the recipient in a direct message

This commit is contained in:
Manuel Cortez 2015-03-12 12:24:34 -06:00
parent 4f4b8c6447
commit 4465f6e8aa
4 changed files with 52 additions and 18 deletions

View File

@ -141,6 +141,11 @@ class reply(tweet):
class dm(basicTweet): class dm(basicTweet):
def __init__(self, session, title, caption, text): def __init__(self, session, title, caption, text):
super(dm, self).__init__(session, title, caption, text, messageType="dm") super(dm, self).__init__(session, title, caption, text, messageType="dm")
widgetUtils.connect_event(self.message.autocompletionButton, widgetUtils.BUTTON_PRESSED, self.autocomplete_users)
def autocomplete_users(self, *args, **kwargs):
c = autocompletionUsers.completion.autocompletionUsers(self.message, self.session.session_id)
c.show_menu("dm")
class viewTweet(basicTweet): class viewTweet(basicTweet):
def __init__(self, tweet, is_tweet=True): def __init__(self, tweet, is_tweet=True):

View File

@ -9,23 +9,39 @@ class autocompletionUsers(object):
self.window = window self.window = window
self.db = storage.storage(session_id) self.db = storage.storage(session_id)
def show_menu(self): def show_menu(self, mode="tweet"):
position = self.window.get_position() position = self.window.get_position()
text = self.window.get_text() if mode == "tweet":
text = text[:position] text = self.window.get_text()
try: text = text[:position]
pattern = text.split()[-1] try:
except IndexError: pattern = text.split()[-1]
output.speak(_(u"You have to start writing")) except IndexError:
return output.speak(_(u"You have to start writing"))
if pattern.startswith("@") == True: return
menu = wx_menu.menu(self.window.text, pattern[1:]) if pattern.startswith("@") == True:
users = self.db.get_users(pattern[1:]) menu = wx_menu.menu(self.window.text, pattern[1:], mode=mode)
users = self.db.get_users(pattern[1:])
if len(users) > 0:
menu.append_options(users)
self.window.popup_menu(menu)
menu.destroy()
else:
output.speak(_(u"There are no results in your users database"))
else:
output.speak(_(u"Autocompletion only works for users."))
elif mode == "dm":
text = self.window.get_user()
try:
pattern = text.split()[-1]
except IndexError:
output.speak(_(u"You have to start writing"))
return
menu = wx_menu.menu(self.window.cb, pattern, mode=mode)
users = self.db.get_users(pattern)
if len(users) > 0: if len(users) > 0:
menu.append_options(users) menu.append_options(users)
self.window.popup_menu(menu) self.window.popup_menu(menu)
menu.destroy() menu.destroy()
else: else:
output.speak(_(u"There are no results in your users database")) output.speak(_(u"There are no results in your users database"))
else:
output.speak(_(u"Autocompletion only works for users."))

View File

@ -2,10 +2,11 @@
import wx import wx
class menu(wx.Menu): class menu(wx.Menu):
def __init__(self, window, pattern): def __init__(self, window, pattern, mode):
super(menu, self).__init__() super(menu, self).__init__()
self.window = window self.window = window
self.pattern = pattern self.pattern = pattern
self.mode = mode
def append_options(self, options): def append_options(self, options):
for i in options: for i in options:
@ -14,7 +15,10 @@ class menu(wx.Menu):
self.Bind(wx.EVT_MENU, lambda evt, temp=i[0]: self.select_text(evt, temp), item) self.Bind(wx.EVT_MENU, lambda evt, temp=i[0]: self.select_text(evt, temp), item)
def select_text(self, ev, text): def select_text(self, ev, text):
self.window.ChangeValue(self.window.GetValue().replace("@"+self.pattern, "@"+text+" ")) if self.mode == "tweet":
self.window.ChangeValue(self.window.GetValue().replace("@"+self.pattern, "@"+text+" "))
elif self.mode == "dm":
self.window.SetValue(self.window.GetValue().replace(self.pattern, text))
self.window.SetInsertionPointEnd() self.window.SetInsertionPointEnd()
def destroy(self): def destroy(self):

View File

@ -114,8 +114,14 @@ class dm(textLimited):
self.mainBox = wx.BoxSizer(wx.VERTICAL) self.mainBox = wx.BoxSizer(wx.VERTICAL)
label = wx.StaticText(self.panel, -1, _(u"Recipient")) label = wx.StaticText(self.panel, -1, _(u"Recipient"))
self.cb = wx.ComboBox(self.panel, -1, choices=users, value=users[0], size=wx.DefaultSize) self.cb = wx.ComboBox(self.panel, -1, choices=users, value=users[0], size=wx.DefaultSize)
self.autocompletionButton = wx.Button(self.panel, -1, _(u"&Autocomplete users"))
self.createTextArea(message, text="") self.createTextArea(message, text="")
self.mainBox.Add(self.cb, 0, wx.ALL, 5) userBox = wx.BoxSizer(wx.HORIZONTAL)
userBox.Add(label, 0, wx.ALL, 5)
userBox.Add(self.cb, 0, wx.ALL, 5)
userBox.Add(self.autocompletionButton, 0, wx.ALL, 5)
self.mainBox.Add(userBox, 0, wx.ALL, 5)
# self.mainBox.Add(self.cb, 0, wx.ALL, 5)
self.mainBox.Add(self.textBox, 0, wx.ALL, 5) self.mainBox.Add(self.textBox, 0, wx.ALL, 5)
self.spellcheck = wx.Button(self.panel, -1, _("Spelling correction"), size=wx.DefaultSize) self.spellcheck = wx.Button(self.panel, -1, _("Spelling correction"), size=wx.DefaultSize)
self.attach = wx.Button(self.panel, -1, _(u"Attach audio"), size=wx.DefaultSize) self.attach = wx.Button(self.panel, -1, _(u"Attach audio"), size=wx.DefaultSize)
@ -150,7 +156,10 @@ class dm(textLimited):
def get_user(self): def get_user(self):
return self.cb.GetValue() return self.cb.GetValue()
def set_user(self, user):
return self.cb.SetValue(user)
class reply(tweet): class reply(tweet):
def __init__(self, title, message, text): def __init__(self, title, message, text):
super(reply, self).__init__(message, title, text) super(reply, self).__init__(message, title, text)