File: pick_widget.py

package info (click to toggle)
cashbox 0.3.7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 388 kB
  • sloc: python: 1,607; makefile: 79; xml: 57
file content (189 lines) | stat: -rwxr-xr-x 6,098 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python3

# pick_widget.py
#
# Copyright:
#   Copyright (C) 2024-2025 Bernd Schumacher <bernd@bschu.de>
#
# License: GPL-3.0+
#   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 package 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 <https://www.gnu.org/licenses/>.
# Comment:
#   On Debian systems, the complete text of the GNU General
#   Public License version 3 can be found in
#   "/usr/share/common-licenses/GPL-3".

""" pick_widget.py provides class PickWidget """

import sys
import os
import gi

dir1 = os.path.dirname(os.path.realpath(__file__))
dir2 = os.path.dirname(dir1)
sys.path.append(dir1)


try:
    from cashbox.utils import reduce_window_size
    from cashbox.article import Article
except ImportError as exc:
    print('Error: cashbox modules not found.', exc)
    sys.exit(1)

if __name__ == '__main__':
    from cashbox.app import App, MinWindow
    from cashbox.article import Sale
    from cashbox.read_appargs import appargs

try:
    gi.require_version(namespace='Adw', version='1')
    from gi.repository import Gtk, GObject
except (ImportError, ValueError) as exc:
    print('Error: Dependencies not met.', exc)
    sys.exit(1)


@Gtk.Template(filename=f'{dir1}/pick_widget_row.ui')
class PickRow(Gtk.Box):
    """ a row of a Pick Widget """
    __gtype_name__ = 'PickRow'
    action_row = Gtk.Template.Child()
    spin_button = Gtk.Template.Child()


@Gtk.Template(filename=f'{dir1}/pick_widget.ui')
class PickWidget(Gtk.Box):
    """ PickWidget to select articles to buy """
    __gtype_name__ = 'PickWidget'
    list_view = Gtk.Template.Child()

    def __init__(self, sale, **kwargs):
        super().__init__(**kwargs)

        # Selection
        ss = Gtk.NoSelection()
        self.list_view.set_model(ss)

        # sale
        self.sale = sale
        ss.set_model(self.sale.picked)

        # factory
        factory = Gtk.SignalListItemFactory()
        self.list_view.set_factory(factory)
        factory.connect("setup", self.factory_setup)
        factory.connect("bind", self.factory_bind)

    def factory_setup(self, _fact, item):
        """ setup factory """
        pick_row = PickRow()
        item.set_child(pick_row)

    def factory_bind(self, _fact, item):
        """ factory bind """
        article = item.get_item()
        pick_row = item.get_child()

        # make sure count is only editable with + and -
        # to avoid opening virtual keyboard on librem5
        c = pick_row.spin_button.get_first_child()
        c.set_sensitive(False)

        article.bind_property("name",
                              pick_row.action_row, "title",
                              GObject.BindingFlags.SYNC_CREATE)

        article.bind_property(
            "count",
            pick_row.spin_button, "value",
            GObject.BindingFlags.SYNC_CREATE |
            GObject.BindingFlags.BIDIRECTIONAL,
            transform_to=self.transform_count_to_spin_button,
            transform_from=self.transform_spin_button_to_count)

    def transform_count_to_spin_button(self, _binding, val):
        """ transform val for widget """
        # print(f"transform_count_to_spin_button val=<{val}> type=<{type(val)}>")
        return val

    def transform_spin_button_to_count(self, _binding, val):
        """ transform val for widget """
        # print(f"transform_spin_button_to_count val=<{val}> type=<{type(val)}>")
        return int(val)

    def output(self, spin_row, item):
        """ get count from widget (article.count may not be updated in
        article.count) """
        article = item.get_item()
        adjustment = spin_row.get_adjustment()
        count = int(adjustment.get_value())
        article.count = count
        # print(f"output: name=<{article.name}> count=<{count}>")
        if count > 0:
            # write price to subtitle
            spin_row.set_subtitle(str(article.price))


if __name__ == '__main__':

    class MyMainWindow(MinWindow):
        """ Test Window """

        def __init__(self, **kwargs):
            super().__init__(**kwargs)

            if appargs.test_small_display:
                reduce_window_size(self)

            self.sale = Sale()

            article = [("Banana", 1.10, 1), ("Apple", 2.0, 2),
                       ("Strawberry", 2.50, 3), ("very long one two three "
                       "four five six seven eight", 9.99, 1),
                       ("Pear", 3.35, 0), ("Watermelon", 1.0, 5),
                       ("Blueberry", 2.0, 6)]
            for f in article:
                self.sale.main_list.append(Article(f[0], f[1], f[2]))

            self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
            self.set_content(self.box)

            list_sale_button = Gtk.Button(label="list")
            self.box.append(list_sale_button)
            list_sale_button.connect('clicked', self.list_sale_clicked)

            self.pick_widget = PickWidget(self.sale)
            self.box.append(self.pick_widget)

            quit_button = Gtk.Button(label="quit")
            self.box.append(quit_button)
            quit_button.connect('clicked', self.quit_clicked)

        def list_sale_clicked(self, _list_button):
            """ test output """
            print(f"list_sale_clicked: sale=<{self.sale}>")

        def quit_clicked(self, _list_button):
            """ end test """
            self.close()

    class MyApp(App):
        """ test class App """

        def on_activate(self, app):
            win = MyMainWindow(application=app)
            win.present()

    myapp = MyApp()
    myapp.run(sys.argv)