gaupol/dialogs/encoding.py¶
Source code for module gaupol.dialogs.encoding from file gaupol/dialogs/encoding.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | # -*- coding: utf-8 -*-
# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Dialogs for selecting character encodings."""
import aeidon
import gaupol
_ = aeidon.i18n._
from gi.repository import Gtk
__all__ = ("EncodingDialog", "MenuEncodingDialog")
class EncodingDialog(gaupol.BuilderDialog):
"""Dialog for selecting a character encoding."""
_widgets = ("tree_view",)
def __init__(self, parent):
"""Initialize an :class:`EncodingDialog` instance."""
gaupol.BuilderDialog.__init__(self, "encoding-dialog.ui")
self._init_tree_view()
gaupol.util.scale_to_content(self._tree_view,
min_nchar=50,
max_nchar=100,
min_nlines=10,
max_nlines=20)
self._dialog.set_transient_for(parent)
self._dialog.set_default_response(Gtk.ResponseType.OK)
def get_encoding(self):
"""Return the selected encoding or ``None``."""
selection = self._tree_view.get_selection()
store, itr = selection.get_selected()
if itr is None: return
return store.get_value(itr, 0)
def _init_tree_view(self):
"""Initialize the tree view."""
selection = self._tree_view.get_selection()
selection.set_mode(Gtk.SelectionMode.SINGLE)
store = Gtk.ListStore(str, str, str)
for item in aeidon.encodings.get_valid():
store.append((item[0], item[2], item[1]))
store.set_sort_column_id(1, Gtk.SortType.ASCENDING)
self._tree_view.set_model(store)
column = Gtk.TreeViewColumn(_("Description"),
Gtk.CellRendererText(),
text=1)
column.set_clickable(True)
column.set_sort_column_id(1)
self._tree_view.append_column(column)
column = Gtk.TreeViewColumn(_("Encoding"),
Gtk.CellRendererText(),
text=2)
column.set_clickable(True)
column.set_sort_column_id(2)
self._tree_view.append_column(column)
def _on_tree_view_row_activated(self, *args):
"""Send response to select activated character encoding."""
self.response(Gtk.ResponseType.OK)
class MenuEncodingDialog(EncodingDialog):
"""Dialog for selecting character encodings."""
def get_visible_encodings(self):
"""Return encodings chosen to be visible."""
store = self._tree_view.get_model()
return [store[i][0] for i in range(len(store)) if store[i][3]]
def _init_tree_view(self):
"""Initialize the tree view."""
selection = self._tree_view.get_selection()
selection.set_mode(Gtk.SelectionMode.SINGLE)
store = Gtk.ListStore(str, str, str, bool)
visible = gaupol.conf.encoding.visible
for item in aeidon.encodings.get_valid():
store.append((item[0], item[2], item[1], item[0] in visible))
store.set_sort_column_id(1, Gtk.SortType.ASCENDING)
self._tree_view.set_model(store)
column = Gtk.TreeViewColumn(_("Description"),
Gtk.CellRendererText(),
text=1)
column.set_clickable(True)
column.set_sort_column_id(1)
self._tree_view.append_column(column)
column = Gtk.TreeViewColumn(_("Encoding"),
Gtk.CellRendererText(),
text=2)
column.set_clickable(True)
column.set_sort_column_id(2)
self._tree_view.append_column(column)
renderer = Gtk.CellRendererToggle()
renderer.connect("toggled", self._on_tree_view_cell_toggled)
column = Gtk.TreeViewColumn(_("Show in Menu"),
renderer,
active=3)
column.set_sort_column_id(3)
self._tree_view.append_column(column)
def _on_tree_view_cell_toggled(self, renderer, path):
"""Toggle the value of the "Show in Menu" column."""
store = self._tree_view.get_model()
store[path][3] = not store[path][3]
|