From 9356a0544f74072116d08e845058b4b172bc65c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Tue, 31 Mar 2015 17:46:15 -0600 Subject: [PATCH] A gtk view for session manager. Key libraries for linux need to be compiled --- src/sessionmanager/gtkUI.py | 54 ++++++++++++++++ src/sessionmanager/sessionManager.py | 6 +- src/widgetUtils/__init__.py | 11 ++-- src/widgetUtils/baseDialog.py | 28 -------- src/widgetUtils/gtkUtils.py | 32 ++++++++++ src/widgetUtils/wxUtils.py | 95 ++++++++++++++++++++++++---- 6 files changed, 177 insertions(+), 49 deletions(-) create mode 100644 src/sessionmanager/gtkUI.py delete mode 100644 src/widgetUtils/baseDialog.py diff --git a/src/sessionmanager/gtkUI.py b/src/sessionmanager/gtkUI.py new file mode 100644 index 00000000..8b016d18 --- /dev/null +++ b/src/sessionmanager/gtkUI.py @@ -0,0 +1,54 @@ +from gi.repository import Gtk +import widgetUtils + +class sessionManagerWindow(Gtk.Dialog): + def __init__(self): + super(sessionManagerWindow, self).__init__("Session Manager", None, 0, (Gtk.STOCK_OK, widgetUtils.OK, Gtk.STOCK_CANCEL, widgetUtils.CANCEL)) + box = self.get_content_area() + self.list = widgetUtils.list("Session") + box.add(self.list.list) + btnBox = Gtk.Box(spacing=6) + self.new = Gtk.Button("New account") + self.remove = Gtk.Button("Remove account") + btnBox.add(self.new) + btnBox.add(self.remove) + box.add(btnBox) + self.show_all() + + def fill_list(self, sessionsList): + for i in sessionsList: + self.list.insert_item(i) + if self.list.get_count() > 0: + self.list.select_item(0) + + def get_response(self): + return self.run() + + def new_account_dialog(self): + dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "Authorization") + dialog.format_secondary_text("The request for the required Twitter authorization to continue will be opened on your browser. You only need to do it once. Would you like to autorhise a new account now?") + return dialog.run() + + def add_new_session_to_list(self): + total = self.list.get_count() + name = "Authorised account %d" % (total+1) + self.list.insert_item(name) + if self.list.get_count() == 1: + self.list.select_item(0) + + def show_unauthorised_error(self): + dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CANCEL, "Invalid user token") + dialog.format_secondary_text("Your access token is invalid or the authorisation has failed. Please try again.") + return dialog.run() + + def remove_account_dialog(self): + dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "Remove account") + dialog.format_secondary_text("Do you really want delete this account?") + return dialog.run() + + def get_selected(self): + return self.list.get_selected() + + def remove_session(self, sessionID): + self.list.remove_item(sessionID) + diff --git a/src/sessionmanager/sessionManager.py b/src/sessionmanager/sessionManager.py index 8a8e1419..2c9e1a84 100644 --- a/src/sessionmanager/sessionManager.py +++ b/src/sessionmanager/sessionManager.py @@ -1,7 +1,11 @@ # -*- coding: utf-8 -*- import shutil import widgetUtils -import wxUI as view +import platform +if platform.system() == "Windows": + import wxUI as view +elif platform.system() == "Linux": + import gtkUI as view import paths import time import os diff --git a/src/widgetUtils/__init__.py b/src/widgetUtils/__init__.py index 28902ae3..c413d4f8 100644 --- a/src/widgetUtils/__init__.py +++ b/src/widgetUtils/__init__.py @@ -1,6 +1,5 @@ -#import platform -#if platform.system() == "Windows": -from wxUtils import * -from baseDialog import * -#elif platform.system() == "Linux": -# from gtkUtils import * +import platform +if platform.system() == "Windows": + from wxUtils import * +elif platform.system() == "Linux": + from gtkUtils import * diff --git a/src/widgetUtils/baseDialog.py b/src/widgetUtils/baseDialog.py deleted file mode 100644 index ac09f797..00000000 --- a/src/widgetUtils/baseDialog.py +++ /dev/null @@ -1,28 +0,0 @@ -import wx - -class BaseDialog(wx.Dialog): - def __init__(self, *args, **kwargs): - super(BaseDialog, self).__init__(*args, **kwargs) - - def get_response(self): - return self.ShowModal() - - def get(self, control): - if hasattr(self, control): - control = getattr(self, control) - if hasattr(control, "GetValue"): return getattr(control, "GetValue")() - elif hasattr(control, "GetLabel"): return getattr(control, "GetLabel")() - else: return -1 - else: return 0 - - def set(self, control, text): - if hasattr(self, control): - control = getattr(self, control) - if hasattr(control, "SetValue"): return getattr(control, "SetValue")(text) - elif hasattr(control, "SetLabel"): return getattr(control, "SetLabel")(text) - elif hasattr(control, "ChangeValue"): return getattr(control, "ChangeValue")(text) - else: return -1 - else: return 0 - - def destroy(self): - self.Destroy() \ No newline at end of file diff --git a/src/widgetUtils/gtkUtils.py b/src/widgetUtils/gtkUtils.py index 4dc203e2..ef5cf83e 100644 --- a/src/widgetUtils/gtkUtils.py +++ b/src/widgetUtils/gtkUtils.py @@ -37,3 +37,35 @@ def connect_event(parent, event, func, menuitem=None, *args, **kwargs): return getattr(parent, "connect")(event, func, *args, **kwargs) else: return getattr(menuitem, "connect")(event, func, *args, **kwargs) + +class list(object): + def __init__(self, *columns, **listArguments): + self.columns = columns + self.list_arguments = listArguments + self.create_list() + + def create_list(self): + columns = [] + [columns.append(str) for i in self.columns] + self.store = Gtk.ListStore(*columns) + self.list = Gtk.TreeView(model=self.store) + renderer = Gtk.CellRendererText() + for i in range(0, len(self.columns)): + column = Gtk.TreeViewColumn(self.columns[i], renderer, text=0) + column.set_sort_column_id(i) + self.list.append_column(column) + + def insert_item(self, *item): + self.store.append(item) + + def get_selected(self): + tree_selection = self.list.get_selection() + (model, pathlist) = tree_selection.get_selected_rows() + return pathlist[0] + + def select_item(self, item): + tree_selection = self.list.get_selection() + tree_selection.select_path(item) + + def get_count(self): + return len(self.store) diff --git a/src/widgetUtils/wxUtils.py b/src/widgetUtils/wxUtils.py index ff3669b8..b871258b 100644 --- a/src/widgetUtils/wxUtils.py +++ b/src/widgetUtils/wxUtils.py @@ -1,47 +1,114 @@ import wx toolkit = "wx" -# Code responses for WX dialogs. + +### Code responses for WX dialogs. + # this is when an user presses OK on a dialogue. OK = wx.ID_OK + # This is when an user presses cancel on a dialogue. CANCEL = wx.ID_CANCEL + # This is when an user closes the dialogue or an id to create the close button. CLOSE = wx.ID_CLOSE + # The response for a "yes" Button pressed on a dialogue. YES = wx.ID_YES + # This is when the user presses No on a default dialogue. NO = wx.ID_NO -#events +###events + # This is raised when the application must be closed. CLOSE_EVENT = wx.EVT_CLOSE + # This is activated when a button is pressed. BUTTON_PRESSED = wx.EVT_BUTTON + +# This is raised when a checkbox changes its status. CHECKBOX = wx.EVT_CHECKBOX + # This is activated when an user enter text on an edit box. ENTERED_TEXT = wx.EVT_TEXT + +# This is raised when a user activates a menu. MENU = wx.EVT_MENU + +# This is raised when a user presses any key in the control. KEYPRESS = wx.EVT_CHAR_HOOK + +# This is raised when a user releases a key in the control. KEYUP = wx.EVT_KEY_UP + +# This happens when a notebook tab is changed, It is used in Treebooks too. NOTEBOOK_PAGE_CHANGED = wx.EVT_TREEBOOK_PAGE_CHANGED + +# This happens when a radiobutton group changes its status. RADIOBUTTON = wx.EVT_RADIOBUTTON + +# Taskbar mouse clicks. TASKBAR_RIGHT_CLICK = wx.EVT_TASKBAR_RIGHT_DOWN TASKBAR_LEFT_CLICK = wx.EVT_TASKBAR_LEFT_DOWN + def exit_application(): - """ Closes the current window cleanly. """ - wx.GetApp().ExitMainLoop() + """ Closes the current window cleanly. """ + wx.GetApp().ExitMainLoop() def connect_event(parent, event, func, menuitem=None, *args, **kwargs): - """ Connects an event to a function. - parent wx.window: The widget that will listen for the event. - event widgetUtils.event: The event that will be listened for the parent. The event should be one of the widgetUtils events. - function func: The function that will be connected to the event.""" - if menuitem == None: - return getattr(parent, "Bind")(event, func, *args, **kwargs) - else: - return getattr(parent, "Bind")(event, func, menuitem, *args, **kwargs) + """ Connects an event to a function. + parent wx.window: The widget that will listen for the event. + event widgetUtils.event: The event that will be listened for the parent. The event should be one of the widgetUtils events. + function func: The function that will be connected to the event.""" + if menuitem == None: + return getattr(parent, "Bind")(event, func, *args, **kwargs) + else: + return getattr(parent, "Bind")(event, func, menuitem, *args, **kwargs) def connectExitFunction(exitFunction): - wx.GetApp().Bind(wx.EVT_QUERY_END_SESSION, exitFunction) - wx.GetApp().Bind(wx.EVT_END_SESSION, exitFunction) \ No newline at end of file + """ This connect the events in WX when an user is turning off the machine.""" + wx.GetApp().Bind(wx.EVT_QUERY_END_SESSION, exitFunction) + wx.GetApp().Bind(wx.EVT_END_SESSION, exitFunction) + +class BaseDialog(wx.Dialog): + def __init__(self, *args, **kwargs): + super(BaseDialog, self).__init__(*args, **kwargs) + + def get_response(self): + return self.ShowModal() + + def get(self, control): + if hasattr(self, control): + control = getattr(self, control) + if hasattr(control, "GetValue"): return getattr(control, "GetValue")() + elif hasattr(control, "GetLabel"): return getattr(control, "GetLabel")() + else: return -1 + else: return 0 + + def set(self, control, text): + if hasattr(self, control): + control = getattr(self, control) + if hasattr(control, "SetValue"): return getattr(control, "SetValue")(text) + elif hasattr(control, "SetLabel"): return getattr(control, "SetLabel")(text) + elif hasattr(control, "ChangeValue"): return getattr(control, "ChangeValue")(text) + else: return -1 + else: return 0 + + def destroy(self): + self.Destroy() + + def set_title(self, title): + self.SetTitle(title) + + def get_title(self): + return self.GetTitle() + +class mainloopObject(wx.App): + + def __init__(self): + self.app = wx.App() + + def run_mainloop(self): + self.app.MainLoop() +