Ejemplo n.º 1
0
class SimpleListDialog(BasicDialog):
    size = (500, 400)

    def __init__(self,
                 columns,
                 objects,
                 hide_cancel_btn=True,
                 title='',
                 multiple=True,
                 header_text=""):
        """
        Create a new SimpleListDialog.
        :param columns:
        :param objects:
        :param hide_cancel_btn:
        :param title:
        :param multiple: if we're allowed to select multiple items
        :type multiple: boolean
        """

        BasicDialog.__init__(self,
                             size=self.size,
                             title=title,
                             header_text=header_text)
        if hide_cancel_btn:
            self.cancel_button.hide()

        if multiple:
            selection_mode = gtk.SELECTION_MULTIPLE
        else:
            selection_mode = gtk.SELECTION_BROWSE
        self.setup_slave(columns, objects, selection_mode)

    def setup_slave(self, columns, objects, selection_mode):
        self.main.remove(self.main_label)
        self._klist = ObjectList(columns, objects, selection_mode)
        self.main.add(self._klist)
        self._klist.show()

    def get_selection(self):
        mode = self._klist.get_selection_mode()
        if mode == gtk.SELECTION_MULTIPLE:
            return self._klist.get_selected_rows()
        selection = self._klist.get_selected()
        if not selection:
            return []
        return [selection]

    #
    #  BasicDialog
    #

    def confirm(self):
        super(SimpleListDialog, self).confirm()
        self.retval = self.retval and self.get_selection()
Ejemplo n.º 2
0
class SimpleListDialog(BasicDialog):
    size = (500, 400)

    def __init__(self, columns, objects, hide_cancel_btn=True,
                 title='', multiple=True, header_text=""):
        """
        Create a new SimpleListDialog.
        :param columns:
        :param objects:
        :param hide_cancel_btn:
        :param title:
        :param multiple: if we're allowed to select multiple items
        :type multiple: boolean
        """

        BasicDialog.__init__(self, size=self.size, title=title,
                             header_text=header_text)
        if hide_cancel_btn:
            self.cancel_button.hide()

        if multiple:
            selection_mode = gtk.SELECTION_MULTIPLE
        else:
            selection_mode = gtk.SELECTION_BROWSE
        self.setup_slave(columns, objects, selection_mode)

    def setup_slave(self, columns, objects, selection_mode):
        self.main.remove(self.main_label)
        self._klist = ObjectList(columns, objects, selection_mode)
        self.main.add(self._klist)
        self._klist.show()

    def get_selection(self):
        mode = self._klist.get_selection_mode()
        if mode == gtk.SELECTION_MULTIPLE:
            return self._klist.get_selected_rows()
        selection = self._klist.get_selected()
        if not selection:
            return []
        return [selection]

    #
    #  BasicDialog
    #

    def confirm(self):
        super(SimpleListDialog, self).confirm()
        self.retval = self.retval and self.get_selection()
Ejemplo n.º 3
0
class MethodTest(unittest.TestCase):
    def setUp(self):
        self.klist = ObjectList([Column('name', sorted=True)],
                                [Settable(name='first')])

    def testNonZero(self):
        self.assertEqual(self.klist.__nonzero__(), True)
        self.klist.remove(self.klist[0])
        self.assertEqual(self.klist.__nonzero__(), True)
        if not self.klist:
            raise AssertionError

    def testIter(self):
        for item1 in self.klist:
            pass
        for item2 in iter(self.klist):
            self.assertEqual(item1, item2)

    def testGetItem(self):
        self.klist.append(Settable(name='second'))
        model = self.klist.get_model()
        item1 = model[0][0]
        item2 = model[1][0]
        self.assertEqual(self.klist[0], item1)
        self.assertEqual(self.klist[:1], [item1])
        self.assertEqual(self.klist[-1:], [item2])
        self.assertRaises(TypeError, self.klist.__getitem__, None)

    def testSetItem(self):
        self.klist[0] = Settable(name='second')
        self.assertRaises(NotImplementedError, self.klist.__setitem__,
                          slice(0), None)
        self.assertRaises(TypeError, self.klist.__setitem__, None, None)

    def testIndex(self):
        self.assertRaises(NotImplementedError, self.klist.index, 0, start=0)
        self.assertRaises(NotImplementedError, self.klist.index, 0, stop=0)

        self.assertEqual(self.klist.index(self.klist[0]), 0)
        self.assertRaises(ValueError, self.klist.index, None)

    def testCount(self):
        item = self.klist[0]
        self.assertEqual(self.klist.count(item), 1)
        self.klist.append(item)
        self.assertEqual(self.klist.count(item), 2)
        self.klist.clear()
        self.assertEqual(self.klist.count(item), 0)

    def testPop(self):
        self.assertRaises(NotImplementedError, self.klist.pop, None)

    def testReverse(self):
        self.assertRaises(NotImplementedError, self.klist.reverse, 1, 2)

    def testSort(self):
        self.assertRaises(NotImplementedError, self.klist.sort, 1, 2)

    def testSelectPath(self):
        self.klist.get_treeview().get_selection().set_mode(gtk.SELECTION_NONE)
        self.assertRaises(TypeError, self.klist.select_paths, (0,))
        self.klist.get_treeview().get_selection().set_mode(gtk.SELECTION_SINGLE)
        self.klist.select_paths((0,))

    def testSelect(self):
        self.klist.get_treeview().get_selection().set_mode(gtk.SELECTION_NONE)
        self.assertRaises(TypeError, self.klist.select, None)
        self.klist.get_treeview().get_selection().set_mode(gtk.SELECTION_SINGLE)

    def testGetSelected(self):
        item = self.klist[0]
        self.klist.select(item)
        self.klist.get_treeview().get_selection().set_mode(gtk.SELECTION_SINGLE)
        self.assertEqual(self.klist.get_selected(), item)

    def testGetSelectedRows(self):
        self.klist.get_treeview().get_selection().set_mode(gtk.SELECTION_MULTIPLE)
        item = self.klist[0]
        self.klist.select(item)
        self.assertEqual(self.klist.get_selected_rows(), [item])

    def testGetNextAndPrevious(self):
        self.klist.append(Settable(name='second'))
        self.klist.append(Settable(name='third'))
        item1, item2, item3 = self.klist

        self.assertEqual(self.klist.get_next(item1), item2)
        self.assertEqual(self.klist.get_next(item2), item3)
        self.assertEqual(self.klist.get_next(item3), item1)
        self.assertRaises(ValueError, self.klist.get_next, None)

        self.assertEqual(self.klist.get_previous(item1), item3)
        self.assertEqual(self.klist.get_previous(item2), item1)
        self.assertEqual(self.klist.get_previous(item3), item2)
        self.assertRaises(ValueError, self.klist.get_previous, None)

    def testInsert(self):
        self.klist = ObjectList([Column('name')])
        self.assertEqual(list(self.klist), [])

        self.klist.insert(0, Settable(name='one'))
        self.assertEqual(self.klist[0].name, 'one')

        self.klist.insert(0, Settable(name='two'))
        self.assertEqual(self.klist[0].name, 'two')
        self.assertEqual(self.klist[1].name, 'one')

        self.klist.insert(1, Settable(name='three'))
        self.assertEqual(self.klist[0].name, 'two')
        self.assertEqual(self.klist[1].name, 'three')
        self.assertEqual(self.klist[2].name, 'one')
Ejemplo n.º 4
0
class MethodTest(unittest.TestCase):
    def setUp(self):
        self.klist = ObjectList([Column('name', sorted=True)],
                                [Settable(name='first')])

    def testNonZero(self):
        self.assertEqual(self.klist.__nonzero__(), True)
        self.klist.remove(self.klist[0])
        self.assertEqual(self.klist.__nonzero__(), True)
        if not self.klist:
            raise AssertionError

    def testIter(self):
        for item1 in self.klist:
            pass
        for item2 in iter(self.klist):
            self.assertEqual(item1, item2)

    def testGetItem(self):
        self.klist.append(Settable(name='second'))
        model = self.klist.get_model()
        item1 = model[0][0]
        item2 = model[1][0]
        self.assertEqual(self.klist[0], item1)
        self.assertEqual(self.klist[:1], [item1])
        self.assertEqual(self.klist[-1:], [item2])
        self.assertRaises(TypeError, self.klist.__getitem__, None)

    def testSetItem(self):
        self.klist[0] = Settable(name='second')
        self.assertRaises(NotImplementedError, self.klist.__setitem__,
                          slice(0), None)
        self.assertRaises(TypeError, self.klist.__setitem__, None, None)

    def testIndex(self):
        self.assertRaises(NotImplementedError, self.klist.index, 0, start=0)
        self.assertRaises(NotImplementedError, self.klist.index, 0, stop=0)

        self.assertEqual(self.klist.index(self.klist[0]), 0)
        self.assertRaises(ValueError, self.klist.index, None)

    def testCount(self):
        item = self.klist[0]
        self.assertEqual(self.klist.count(item), 1)
        self.klist.append(item)
        self.assertEqual(self.klist.count(item), 2)
        self.klist.clear()
        self.assertEqual(self.klist.count(item), 0)

    def testPop(self):
        self.assertRaises(NotImplementedError, self.klist.pop, None)

    def testReverse(self):
        self.assertRaises(NotImplementedError, self.klist.reverse, 1, 2)

    def testSort(self):
        self.assertRaises(NotImplementedError, self.klist.sort, 1, 2)

    def testSelectPath(self):
        self.klist.get_treeview().get_selection().set_mode(
            Gtk.SelectionMode.NONE)
        self.assertRaises(TypeError, self.klist.select_paths, (0, ))
        self.klist.get_treeview().get_selection().set_mode(
            Gtk.SelectionMode.SINGLE)
        self.klist.select_paths((0, ))

    def testSelect(self):
        self.klist.get_treeview().get_selection().set_mode(
            Gtk.SelectionMode.NONE)
        self.assertRaises(TypeError, self.klist.select, None)
        self.klist.get_treeview().get_selection().set_mode(
            Gtk.SelectionMode.SINGLE)

    def testGetSelected(self):
        item = self.klist[0]
        self.klist.select(item)
        self.klist.get_treeview().get_selection().set_mode(
            Gtk.SelectionMode.SINGLE)
        self.assertEqual(self.klist.get_selected(), item)

    def testGetSelectedRows(self):
        self.klist.get_treeview().get_selection().set_mode(
            Gtk.SelectionMode.MULTIPLE)
        item = self.klist[0]
        self.klist.select(item)
        self.assertEqual(self.klist.get_selected_rows(), [item])

    def testGetNextAndPrevious(self):
        self.klist.append(Settable(name='second'))
        self.klist.append(Settable(name='third'))
        item1, item2, item3 = self.klist

        self.assertEqual(self.klist.get_next(item1), item2)
        self.assertEqual(self.klist.get_next(item2), item3)
        self.assertEqual(self.klist.get_next(item3), item1)
        self.assertRaises(ValueError, self.klist.get_next, None)

        self.assertEqual(self.klist.get_previous(item1), item3)
        self.assertEqual(self.klist.get_previous(item2), item1)
        self.assertEqual(self.klist.get_previous(item3), item2)
        self.assertRaises(ValueError, self.klist.get_previous, None)

    def testInsert(self):
        self.klist = ObjectList([Column('name')])
        self.assertEqual(list(self.klist), [])

        self.klist.insert(0, Settable(name='one'))
        self.assertEqual(self.klist[0].name, 'one')

        self.klist.insert(0, Settable(name='two'))
        self.assertEqual(self.klist[0].name, 'two')
        self.assertEqual(self.klist[1].name, 'one')

        self.klist.insert(1, Settable(name='three'))
        self.assertEqual(self.klist[0].name, 'two')
        self.assertEqual(self.klist[1].name, 'three')
        self.assertEqual(self.klist[2].name, 'one')