pgconsole/settings.py
changeset 76 3a41b351b122
parent 10 f3a1b9792cc9
equal deleted inserted replaced
75:39f777341db4 76:3a41b351b122
     1 import gtk
     1 from gi.repository import Gtk
       
     2 from gi.repository import Gdk
     2 
     3 
     3 from config import cfg
     4 from pgconsole.config import cfg
     4 
     5 
     5 
     6 
     6 class Settings(gtk.Window):
     7 class Settings(Gtk.Window):
     7     def __init__(self, parent):
     8     def __init__(self, parent):
     8         super(Settings, self).__init__(gtk.WINDOW_TOPLEVEL)
     9         super(Settings, self).__init__()
     9 
    10 
    10         self._parent = parent
    11         self._parent = parent
    11 
    12 
    12         self.set_title('Settings')
    13         self.set_title('Settings')
    13         self.set_modal(True)
    14         self.set_modal(True)
    14         self.set_transient_for(parent.win)
    15         self.set_transient_for(parent.win)
    15         self.set_position(gtk.WIN_POS_CENTER)
    16         self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
    16         #self.set_border_width(10)
    17         #self.set_border_width(10)
    17         self.connect("key_press_event", self.on_keypress)
    18         self.connect("key_press_event", self.on_keypress)
    18         self.connect("destroy", self.on_destroy)
    19         self.connect("destroy", self.on_destroy)
    19 
    20 
    20         self.tabs = gtk.Notebook()
    21         self.tabs = Gtk.Notebook()
    21         self.add(self.tabs)
    22         self.add(self.tabs)
    22 
    23 
    23         ### Servers
    24         ### Servers
    24         vbox = gtk.VBox(spacing=10)
    25         vbox = Gtk.VBox(spacing=10)
    25         vbox.set_border_width(10)
    26         vbox.set_border_width(10)
    26         self.tabs.append_page(vbox, gtk.Label('Servers'))
    27         self.tabs.append_page(vbox, Gtk.Label('Servers'))
    27 
    28 
    28         hbox = gtk.HBox(spacing=10)
    29         hbox = Gtk.HBox(spacing=10)
    29         vbox.pack_start(hbox)
    30         vbox.pack_start(hbox, expand=True, fill=True, padding=0)
    30         sw = gtk.ScrolledWindow()
    31         sw = Gtk.ScrolledWindow()
    31         sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
    32         sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
    32         sw.set_shadow_type(gtk.SHADOW_IN)
    33         sw.set_shadow_type(Gtk.ShadowType.IN)
    33         hbox.pack_start(sw)
    34         hbox.pack_start(sw, expand=True, fill=True, padding=0)
    34         tree = gtk.TreeView()
    35         tree = Gtk.TreeView()
    35         tree.set_headers_visible(False)
    36         tree.set_headers_visible(False)
    36         sw.add(tree)
    37         sw.add(tree)
    37         tree.get_selection().set_mode(gtk.SELECTION_BROWSE)
    38         tree.get_selection().set_mode(Gtk.SelectionMode.BROWSE)
    38 
    39 
    39         model = gtk.ListStore(str, object)
    40         model = Gtk.ListStore(str, object)
    40         tree.set_model(model)
    41         tree.set_model(model)
    41         column = gtk.TreeViewColumn("Server", gtk.CellRendererText(), text=0)
    42         column = Gtk.TreeViewColumn("Server", Gtk.CellRendererText(), text=0)
    42         tree.append_column(column)
    43         tree.append_column(column)
    43         tree.get_selection().connect('changed', self.item_change)
    44         tree.get_selection().connect('changed', self.item_change)
    44         tree.set_size_request(100, -1)
    45         tree.set_size_request(100, -1)
    45 
    46 
    46         try:
    47         try:
    53         except AttributeError:
    54         except AttributeError:
    54             pass
    55             pass
    55 
    56 
    56         self.itemtree = tree
    57         self.itemtree = tree
    57 
    58 
    58         vbox = gtk.VBox(spacing=10)
    59         vbox = Gtk.VBox(spacing=10)
    59         hbox.pack_start(vbox)
    60         hbox.pack_start(vbox, expand=True, fill=True, padding=0)
    60 
    61 
    61         table = gtk.Table(5, 2)
    62         table = Gtk.Table(5, 2)
    62         table.set_row_spacings(4)
    63         table.set_row_spacings(4)
    63         table.set_col_spacings(10)
    64         table.set_col_spacings(10)
    64         vbox.pack_start(table)
    65         vbox.pack_start(table, expand=True, fill=True, padding=0)
    65 
    66 
    66         label = gtk.Label("Name:")
    67         label = Gtk.Label("Name:")
    67         label.set_alignment(0.0, 0.5)
    68         label.set_alignment(0.0, 0.5)
    68         table.attach(label, 0, 1, 0, 1)
    69         table.attach(label, 0, 1, 0, 1)
    69         entry = gtk.Entry()
    70         entry = Gtk.Entry()
    70         table.attach(entry, 1, 2, 0, 1)
    71         table.attach(entry, 1, 2, 0, 1)
    71         self.entry_name = entry
    72         self.entry_name = entry
    72 
    73 
    73         label = gtk.Label("Host:")
    74         label = Gtk.Label("Host:")
    74         label.set_alignment(0.0, 0.5)
    75         label.set_alignment(0.0, 0.5)
    75         table.attach(label, 0, 1, 1, 2)
    76         table.attach(label, 0, 1, 1, 2)
    76         entry = gtk.Entry()
    77         entry = Gtk.Entry()
    77         entry.set_text('127.0.0.1')
    78         entry.set_text('127.0.0.1')
    78         table.attach(entry, 1, 2, 1, 2)
    79         table.attach(entry, 1, 2, 1, 2)
    79         self.entry_host = entry
    80         self.entry_host = entry
    80 
    81 
    81         label = gtk.Label("Port:")
    82         label = Gtk.Label("Port:")
    82         label.set_alignment(0.0, 0.5)
    83         label.set_alignment(0.0, 0.5)
    83         table.attach(label, 0, 1, 2, 3)
    84         table.attach(label, 0, 1, 2, 3)
    84         entry = gtk.Entry()
    85         entry = Gtk.Entry()
    85         entry.set_text('5432')
    86         entry.set_text('5432')
    86         table.attach(entry, 1, 2, 2, 3)
    87         table.attach(entry, 1, 2, 2, 3)
    87         self.entry_port = entry
    88         self.entry_port = entry
    88 
    89 
    89         label = gtk.Label("User:")
    90         label = Gtk.Label("User:")
    90         label.set_alignment(0.0, 0.5)
    91         label.set_alignment(0.0, 0.5)
    91         table.attach(label, 0, 1, 3, 4)
    92         table.attach(label, 0, 1, 3, 4)
    92         entry = gtk.Entry()
    93         entry = Gtk.Entry()
    93         entry.set_text('postgres')
    94         entry.set_text('postgres')
    94         table.attach(entry, 1, 2, 3, 4)
    95         table.attach(entry, 1, 2, 3, 4)
    95         self.entry_user = entry
    96         self.entry_user = entry
    96 
    97 
    97         label = gtk.Label("Password:")
    98         label = Gtk.Label("Password:")
    98         label.set_alignment(0.0, 0.5)
    99         label.set_alignment(0.0, 0.5)
    99         table.attach(label, 0, 1, 4, 5)
   100         table.attach(label, 0, 1, 4, 5)
   100         entry = gtk.Entry()
   101         entry = Gtk.Entry()
   101         table.attach(entry, 1, 2, 4, 5)
   102         table.attach(entry, 1, 2, 4, 5)
   102         self.entry_password = entry
   103         self.entry_password = entry
   103 
   104 
   104         hbox = gtk.HBox()
   105         hbox = Gtk.HBox()
   105         vbox.pack_start(hbox)
   106         vbox.pack_start(hbox, expand=True, fill=True, padding=0)
   106 
   107 
   107         btn = gtk.Button('Add')
   108         btn = Gtk.Button('Add')
   108         btn.connect('clicked', self.item_add)
   109         btn.connect('clicked', self.item_add)
   109         hbox.pack_start(btn)
   110         hbox.pack_start(btn, expand=True, fill=True, padding=0)
   110         btn = gtk.Button('Save')
   111         btn = Gtk.Button('Save')
   111         btn.connect('clicked', self.item_save)
   112         btn.connect('clicked', self.item_save)
   112         hbox.pack_start(btn)
   113         hbox.pack_start(btn, expand=True, fill=True, padding=0)
   113         btn = gtk.Button('Remove')
   114         btn = Gtk.Button('Remove')
   114         btn.connect('clicked', self.item_remove)
   115         btn.connect('clicked', self.item_remove)
   115         hbox.pack_start(btn)
   116         hbox.pack_start(btn, expand=True, fill=True, padding=0)
   116 
   117 
   117         ### Editor
   118         ### Editor
   118         vbox = gtk.VBox(spacing=10)
   119         vbox = Gtk.VBox(spacing=10)
   119         vbox.set_border_width(10)
   120         vbox.set_border_width(10)
   120         self.tabs.append_page(vbox, gtk.Label('Editor'))
   121         self.tabs.append_page(vbox, Gtk.Label('Editor'))
   121 
   122 
   122         self.show_all()
   123         self.show_all()
   123         self.itemtree.grab_focus()
   124         self.itemtree.grab_focus()
   124 
   125 
   125 
   126 
   178         del cfg.servers.server[sel[0][0]]
   179         del cfg.servers.server[sel[0][0]]
   179         cfg.save()
   180         cfg.save()
   180 
   181 
   181 
   182 
   182     def on_keypress(self, w, event):
   183     def on_keypress(self, w, event):
   183         keyname = gtk.gdk.keyval_name(event.keyval)
   184         keyname = Gdk.keyval_name(event.keyval)
   184 
   185 
   185         if keyname == 'Escape':
   186         if keyname == 'Escape':
   186             self.destroy()
   187             self.destroy()
   187             return True
   188             return True
   188         return False
   189         return False
   189 
   190 
   190 
   191 
   191     def on_destroy(self, w):
   192     def on_destroy(self, w):
   192         model, sel = self.itemtree.get_selection().get_selected_rows()
   193         model, sel = self.itemtree.get_selection().get_selected_rows()
   193         if sel:
   194         if sel:
   194             sel = sel[0][0]
   195             sel = sel[0].get_indices()[0]
   195         else:
   196         else:
   196             sel = None
   197             sel = None
   197         self._parent.reload_server_list(sel)
   198         self._parent.reload_server_list(sel)
   198 
   199