コード例 #1
0
 def build(self):
     # create a default grid layout with custom width/height
     table = TableView(size=(500,320),
             pos_hint={'x':0.1, 'center_y':.5})
     # columns
     table.add_column(TableColumn("Col1", key="1", hint_text='0'))
     table.add_column(TableColumn("Col2", key="2", hint_text='0'))
     # content
     for i in range(30):
         row = {'1': str(2*i+0), '2': str(2*i+1)}
         table.add_row(row)
     return table
コード例 #2
0
ファイル: main.py プロジェクト: Minfante377/bar_code_scanner
 def refresh(self, ):
     self.layout = BoxLayout(orientation="vertical")
     self.table_layout = BoxLayout(orientation="horizontal",
                                   size_hint=(1.0, 1.0))
     self.article_layout = BoxLayout(orientation="vertical")
     self.delete_layout = BoxLayout(orientation="vertical",
                                    size_hint=(0.2, 1.0))
     self.total_layout = BoxLayout(orientation="horizontal",
                                   size_hint=(1.0, 0.2))
     self.table_layout.clear_widgets()
     self.labels = TableView(size_hint=(0.913, 1.0))
     self.labels.add_column(
         TableColumn("Codigo", key="codigo", hint_text='0'))
     self.labels.add_column(
         TableColumn("Descripcion", key="descripcion", hint_text='0'))
     self.labels.add_column(
         TableColumn("Empresa", key="empresa", hint_text='0'))
     self.labels.add_column(
         TableColumn("Unidades por bulto",
                     key="cantidad_bulto",
                     hint_text='0'))
     self.labels.add_column(
         TableColumn("Precio", key="precio", hint_text='0'))
     self.table = TableView(size_hint=(1.0, 1.0))
     self.table.add_column(
         TableColumn("Codigo", key="codigo", hint_text='0'))
     self.table.add_column(
         TableColumn("Descripcion", key="descripcion", hint_text='0'))
     self.table.add_column(
         TableColumn("Empresa", key="empresa", hint_text='0'))
     self.table.add_column(
         TableColumn("Unidades por bulto",
                     key="cantidad_bulto",
                     hint_text='0'))
     self.table.add_column(
         TableColumn("Precio", key="precio", hint_text='0'))
     row = {
         "codigo": "Codigo",
         "descripcion": "Descripcion",
         "empresa": "Empresa",
         "cantidad_bulto": "Unidades por bulto",
         "precio": "Precio"
     }
     self.labels.add_row(row)
     i = 0
     self.content = BoxLayout(orientation='vertical')
     delete = Button(text="Eliminar", on_release=self.delete)
     delete.bind(on_release=self.delete)
     self.delete_label = Label(text="Esta a punto de eliminar un elemento")
     self.content.add_widget(self.delete_label)
     self.content.add_widget(delete)
     self.delete_popup = Popup(title="Eliminar",
                               content=self.content,
                               size_hint=(0.9, 0.25))
     for key in store.keys():
         row = {
             "codigo": store.get(key)['codigo'],
             "descripcion": store.get(key)['descripcion'],
             "empresa": store.get(key)['empresa'],
             "cantidad_bulto": store.get(key)['unidad_bulto'],
             "precio": store.get(key)['precio']
         }
         self.table.add_row(row)
         self.delete_layout.add_widget(
             Button(text="Eliminar",
                    id=str(i),
                    on_release=self.call_delete_popup))
         i = i + 1
     self.article_layout.add_widget(self.labels)
     self.table_layout.add_widget(self.table)
     self.table_layout.add_widget(self.delete_layout)
     self.article_layout.add_widget(self.table_layout)
     self.clear_content = BoxLayout(orientation='vertical')
     clear_button = Button(text="Eliminar")
     clear_button.bind(on_release=self.clear)
     self.clear_label = Label(
         text="Esta a punto de eliminar todos los elementos")
     self.clear_content.add_widget(self.clear_label)
     self.clear_content.add_widget(clear_button)
     self.clear_popup = Popup(title="Eliminar",
                              content=self.clear_content,
                              size_hint=(0.9, 0.25))
     self.clear_button = Button(text="Borrar todo", size_hint=(1.0, 0.1))
     self.clear_button.bind(on_release=self.clear_popup.open)
     self.content = BoxLayout(orientation="vertical")
     previous_button = Button(text="Volver", size_hint=(1.0, 0.1))
     previous_button.bind(on_release=self.changer)
     self.discount = TextInput(text="0.0")
     self.layout.add_widget(self.article_layout)
     self.layout.add_widget(self.clear_button)
     self.layout.add_widget(previous_button)
     self.clear_widgets()
     self.add_widget(self.layout)
コード例 #3
0
ファイル: main.py プロジェクト: Minfante377/bar_code_scanner
class DbScreen(Screen):
    def __init__(self, **kwargs):
        super(DbScreen, self).__init__(**kwargs)
        self.refresh()

    def on_pre_enter(self):
        self.refresh()

    def changer(self, *args):
        self.manager.current = 'main_screen'

    def refresh(self, ):
        self.layout = BoxLayout(orientation="vertical")
        self.table_layout = BoxLayout(orientation="horizontal",
                                      size_hint=(1.0, 1.0))
        self.article_layout = BoxLayout(orientation="vertical")
        self.delete_layout = BoxLayout(orientation="vertical",
                                       size_hint=(0.2, 1.0))
        self.total_layout = BoxLayout(orientation="horizontal",
                                      size_hint=(1.0, 0.2))
        self.table_layout.clear_widgets()
        self.labels = TableView(size_hint=(0.913, 1.0))
        self.labels.add_column(
            TableColumn("Codigo", key="codigo", hint_text='0'))
        self.labels.add_column(
            TableColumn("Descripcion", key="descripcion", hint_text='0'))
        self.labels.add_column(
            TableColumn("Empresa", key="empresa", hint_text='0'))
        self.labels.add_column(
            TableColumn("Unidades por bulto",
                        key="cantidad_bulto",
                        hint_text='0'))
        self.labels.add_column(
            TableColumn("Precio", key="precio", hint_text='0'))
        self.table = TableView(size_hint=(1.0, 1.0))
        self.table.add_column(
            TableColumn("Codigo", key="codigo", hint_text='0'))
        self.table.add_column(
            TableColumn("Descripcion", key="descripcion", hint_text='0'))
        self.table.add_column(
            TableColumn("Empresa", key="empresa", hint_text='0'))
        self.table.add_column(
            TableColumn("Unidades por bulto",
                        key="cantidad_bulto",
                        hint_text='0'))
        self.table.add_column(
            TableColumn("Precio", key="precio", hint_text='0'))
        row = {
            "codigo": "Codigo",
            "descripcion": "Descripcion",
            "empresa": "Empresa",
            "cantidad_bulto": "Unidades por bulto",
            "precio": "Precio"
        }
        self.labels.add_row(row)
        i = 0
        self.content = BoxLayout(orientation='vertical')
        delete = Button(text="Eliminar", on_release=self.delete)
        delete.bind(on_release=self.delete)
        self.delete_label = Label(text="Esta a punto de eliminar un elemento")
        self.content.add_widget(self.delete_label)
        self.content.add_widget(delete)
        self.delete_popup = Popup(title="Eliminar",
                                  content=self.content,
                                  size_hint=(0.9, 0.25))
        for key in store.keys():
            row = {
                "codigo": store.get(key)['codigo'],
                "descripcion": store.get(key)['descripcion'],
                "empresa": store.get(key)['empresa'],
                "cantidad_bulto": store.get(key)['unidad_bulto'],
                "precio": store.get(key)['precio']
            }
            self.table.add_row(row)
            self.delete_layout.add_widget(
                Button(text="Eliminar",
                       id=str(i),
                       on_release=self.call_delete_popup))
            i = i + 1
        self.article_layout.add_widget(self.labels)
        self.table_layout.add_widget(self.table)
        self.table_layout.add_widget(self.delete_layout)
        self.article_layout.add_widget(self.table_layout)
        self.clear_content = BoxLayout(orientation='vertical')
        clear_button = Button(text="Eliminar")
        clear_button.bind(on_release=self.clear)
        self.clear_label = Label(
            text="Esta a punto de eliminar todos los elementos")
        self.clear_content.add_widget(self.clear_label)
        self.clear_content.add_widget(clear_button)
        self.clear_popup = Popup(title="Eliminar",
                                 content=self.clear_content,
                                 size_hint=(0.9, 0.25))
        self.clear_button = Button(text="Borrar todo", size_hint=(1.0, 0.1))
        self.clear_button.bind(on_release=self.clear_popup.open)
        self.content = BoxLayout(orientation="vertical")
        previous_button = Button(text="Volver", size_hint=(1.0, 0.1))
        previous_button.bind(on_release=self.changer)
        self.discount = TextInput(text="0.0")
        self.layout.add_widget(self.article_layout)
        self.layout.add_widget(self.clear_button)
        self.layout.add_widget(previous_button)
        self.clear_widgets()
        self.add_widget(self.layout)

    def call_delete_popup(self, button):
        i = 0
        for key in store.keys():
            if i == int(button.id):
                break
            i = i + 1
        self.delete_index = i
        self.delete_popup.open()

    def delete(self, button):
        key = store.keys()[self.delete_index]
        store.delete(key)
        self.delete_popup.dismiss()
        self.refresh()

    def clear(self, button):
        for key in store.keys():
            store.delete(key)
        self.clear_popup.dismiss()
        self.refresh()
コード例 #4
0
ファイル: main.py プロジェクト: Minfante377/bar_code_scanner
 def refresh(self, ):
     self.layout = BoxLayout(orientation="vertical")
     self.table_layout = BoxLayout(orientation="horizontal",
                                   size_hint=(1.0, 1.0))
     self.article_layout = BoxLayout(orientation="vertical")
     self.quantity_layout = BoxLayout(orientation="vertical",
                                      size_hint=(0.1, 1.0))
     self.delete_layout = BoxLayout(orientation="vertical",
                                    size_hint=(0.2, 1.0))
     self.total_layout = BoxLayout(orientation="horizontal",
                                   size_hint=(1.0, 0.2))
     self.table_layout.clear_widgets()
     self.labels = TableView(size_hint=(0.877, 1.0))
     self.labels.add_column(
         TableColumn("Codigo", key="codigo", hint_text='0'))
     self.labels.add_column(
         TableColumn("Descripcion", key="descripcion", hint_text='0'))
     self.labels.add_column(
         TableColumn("Empresa", key="empresa", hint_text='0'))
     self.labels.add_column(
         TableColumn("Unidades por bulto",
                     key="cantidad_bulto",
                     hint_text='0'))
     self.labels.add_column(
         TableColumn("Precio", key="precio", hint_text='0'))
     self.table = TableView(size_hint=(1.0, 1.0))
     self.table.add_column(
         TableColumn("Codigo", key="codigo", hint_text='0'))
     self.table.add_column(
         TableColumn("Descripcion", key="descripcion", hint_text='0'))
     self.table.add_column(
         TableColumn("Empresa", key="empresa", hint_text='0'))
     self.table.add_column(
         TableColumn("Unidades por bulto",
                     key="cantidad_bulto",
                     hint_text='0'))
     self.table.add_column(
         TableColumn("Precio", key="precio", hint_text='0'))
     row = {
         "codigo": "Codigo",
         "descripcion": "Descripcion",
         "empresa": "Empresa",
         "cantidad_bulto": "Unidades por bulto",
         "precio": "Precio"
     }
     self.labels.add_row(row)
     i = 0
     self.content = BoxLayout(orientation='vertical')
     delete = Button(text="Eliminar", on_release=self.delete)
     delete.bind(on_release=self.delete)
     self.delete_label = Label(text="Esta a punto de eliminar un elemento")
     self.content.add_widget(self.delete_label)
     self.content.add_widget(delete)
     self.delete_popup = Popup(title="Eliminar",
                               content=self.content,
                               size_hint=(0.9, 0.25))
     for key in shopping_car.keys():
         row = {
             "codigo": shopping_car.get(key)['codigo'],
             "descripcion": shopping_car.get(key)['descripcion'],
             "empresa": shopping_car.get(key)['empresa'],
             "cantidad_bulto": shopping_car.get(key)['unidad_bulto'],
             "precio": shopping_car.get(key)['precio']
         }
         self.table.add_row(row)
         self.quantity_layout.add_widget(
             TextInput(text=shopping_car.get(key)['cantidad'], id=str(i)))
         self.delete_layout.add_widget(
             Button(text="Eliminar",
                    id=str(i),
                    on_release=self.call_delete_popup))
         i = i + 1
     for children in self.quantity_layout.children:
         children.bind(text=self.refresh_total)
     self.article_layout.add_widget(self.labels)
     self.table_layout.add_widget(self.table)
     self.table_layout.add_widget(self.quantity_layout)
     self.table_layout.add_widget(self.delete_layout)
     self.article_layout.add_widget(self.table_layout)
     self.clear_content = BoxLayout(orientation='vertical')
     clear_button = Button(text="Eliminar")
     clear_button.bind(on_release=self.clear)
     self.clear_label = Label(
         text="Esta a punto de eliminar todos los elementos")
     self.clear_content.add_widget(self.clear_label)
     self.clear_content.add_widget(clear_button)
     self.clear_popup = Popup(title="Eliminar",
                              content=self.clear_content,
                              size_hint=(0.9, 0.25))
     self.clear_button = Button(text="Borrar todo", size_hint=(1.0, 0.1))
     self.clear_button.bind(on_release=self.clear_popup.open)
     self.content = BoxLayout(orientation="vertical")
     export = Button(text="Export", on_release=self.export)
     self.export_name = TextInput(hint_text="Nombre del archivo")
     self.content.add_widget(self.export_name)
     self.content.add_widget(export)
     self.export_popup = Popup(title="Exportar archivo",
                               content=self.content,
                               size_hint=(0.9, 0.25))
     export_button = Button(text="Exportar", size_hint=(1.0, 0.1))
     export_button.bind(on_release=self.export_popup.open)
     previous_button = Button(text="Volver", size_hint=(1.0, 0.1))
     previous_button.bind(on_release=self.changer)
     self.discount = TextInput(text="0.0")
     discount_button = Button(text="Aplicar descuento")
     discount_button.bind(on_release=self.apply_discount)
     self.discount_warning = Popup(title="Error!",
                                   content=Label(text="Descuento invalido"),
                                   size_hint=(0.9, 0.25))
     self.succes_export_popup = Popup(
         title="Exportar Archivo",
         content=Label(text="Archivo exportado con exito"),
         size_hint=(0.9, 0.25))
     self.total = Label(text="Total")
     self.calculate_total(0)
     self.total_layout.add_widget(self.discount)
     self.total_layout.add_widget(discount_button)
     self.total_layout.add_widget(self.total)
     self.layout.add_widget(self.article_layout)
     self.layout.add_widget(self.total_layout)
     self.layout.add_widget(export_button)
     self.layout.add_widget(self.clear_button)
     self.layout.add_widget(previous_button)
     self.clear_widgets()
     self.add_widget(self.layout)
コード例 #5
0
ファイル: main.py プロジェクト: Minfante377/bar_code_scanner
class TableScreen(Screen):
    def __init__(self, **kwargs):
        super(TableScreen, self).__init__(**kwargs)
        self.refresh()

    def on_pre_enter(self):
        self.refresh()

    def changer(self, *args):
        self.manager.current = 'main_screen'

    def refresh(self, ):
        self.layout = BoxLayout(orientation="vertical")
        self.table_layout = BoxLayout(orientation="horizontal",
                                      size_hint=(1.0, 1.0))
        self.article_layout = BoxLayout(orientation="vertical")
        self.quantity_layout = BoxLayout(orientation="vertical",
                                         size_hint=(0.1, 1.0))
        self.delete_layout = BoxLayout(orientation="vertical",
                                       size_hint=(0.2, 1.0))
        self.total_layout = BoxLayout(orientation="horizontal",
                                      size_hint=(1.0, 0.2))
        self.table_layout.clear_widgets()
        self.labels = TableView(size_hint=(0.877, 1.0))
        self.labels.add_column(
            TableColumn("Codigo", key="codigo", hint_text='0'))
        self.labels.add_column(
            TableColumn("Descripcion", key="descripcion", hint_text='0'))
        self.labels.add_column(
            TableColumn("Empresa", key="empresa", hint_text='0'))
        self.labels.add_column(
            TableColumn("Unidades por bulto",
                        key="cantidad_bulto",
                        hint_text='0'))
        self.labels.add_column(
            TableColumn("Precio", key="precio", hint_text='0'))
        self.table = TableView(size_hint=(1.0, 1.0))
        self.table.add_column(
            TableColumn("Codigo", key="codigo", hint_text='0'))
        self.table.add_column(
            TableColumn("Descripcion", key="descripcion", hint_text='0'))
        self.table.add_column(
            TableColumn("Empresa", key="empresa", hint_text='0'))
        self.table.add_column(
            TableColumn("Unidades por bulto",
                        key="cantidad_bulto",
                        hint_text='0'))
        self.table.add_column(
            TableColumn("Precio", key="precio", hint_text='0'))
        row = {
            "codigo": "Codigo",
            "descripcion": "Descripcion",
            "empresa": "Empresa",
            "cantidad_bulto": "Unidades por bulto",
            "precio": "Precio"
        }
        self.labels.add_row(row)
        i = 0
        self.content = BoxLayout(orientation='vertical')
        delete = Button(text="Eliminar", on_release=self.delete)
        delete.bind(on_release=self.delete)
        self.delete_label = Label(text="Esta a punto de eliminar un elemento")
        self.content.add_widget(self.delete_label)
        self.content.add_widget(delete)
        self.delete_popup = Popup(title="Eliminar",
                                  content=self.content,
                                  size_hint=(0.9, 0.25))
        for key in shopping_car.keys():
            row = {
                "codigo": shopping_car.get(key)['codigo'],
                "descripcion": shopping_car.get(key)['descripcion'],
                "empresa": shopping_car.get(key)['empresa'],
                "cantidad_bulto": shopping_car.get(key)['unidad_bulto'],
                "precio": shopping_car.get(key)['precio']
            }
            self.table.add_row(row)
            self.quantity_layout.add_widget(
                TextInput(text=shopping_car.get(key)['cantidad'], id=str(i)))
            self.delete_layout.add_widget(
                Button(text="Eliminar",
                       id=str(i),
                       on_release=self.call_delete_popup))
            i = i + 1
        for children in self.quantity_layout.children:
            children.bind(text=self.refresh_total)
        self.article_layout.add_widget(self.labels)
        self.table_layout.add_widget(self.table)
        self.table_layout.add_widget(self.quantity_layout)
        self.table_layout.add_widget(self.delete_layout)
        self.article_layout.add_widget(self.table_layout)
        self.clear_content = BoxLayout(orientation='vertical')
        clear_button = Button(text="Eliminar")
        clear_button.bind(on_release=self.clear)
        self.clear_label = Label(
            text="Esta a punto de eliminar todos los elementos")
        self.clear_content.add_widget(self.clear_label)
        self.clear_content.add_widget(clear_button)
        self.clear_popup = Popup(title="Eliminar",
                                 content=self.clear_content,
                                 size_hint=(0.9, 0.25))
        self.clear_button = Button(text="Borrar todo", size_hint=(1.0, 0.1))
        self.clear_button.bind(on_release=self.clear_popup.open)
        self.content = BoxLayout(orientation="vertical")
        export = Button(text="Export", on_release=self.export)
        self.export_name = TextInput(hint_text="Nombre del archivo")
        self.content.add_widget(self.export_name)
        self.content.add_widget(export)
        self.export_popup = Popup(title="Exportar archivo",
                                  content=self.content,
                                  size_hint=(0.9, 0.25))
        export_button = Button(text="Exportar", size_hint=(1.0, 0.1))
        export_button.bind(on_release=self.export_popup.open)
        previous_button = Button(text="Volver", size_hint=(1.0, 0.1))
        previous_button.bind(on_release=self.changer)
        self.discount = TextInput(text="0.0")
        discount_button = Button(text="Aplicar descuento")
        discount_button.bind(on_release=self.apply_discount)
        self.discount_warning = Popup(title="Error!",
                                      content=Label(text="Descuento invalido"),
                                      size_hint=(0.9, 0.25))
        self.succes_export_popup = Popup(
            title="Exportar Archivo",
            content=Label(text="Archivo exportado con exito"),
            size_hint=(0.9, 0.25))
        self.total = Label(text="Total")
        self.calculate_total(0)
        self.total_layout.add_widget(self.discount)
        self.total_layout.add_widget(discount_button)
        self.total_layout.add_widget(self.total)
        self.layout.add_widget(self.article_layout)
        self.layout.add_widget(self.total_layout)
        self.layout.add_widget(export_button)
        self.layout.add_widget(self.clear_button)
        self.layout.add_widget(previous_button)
        self.clear_widgets()
        self.add_widget(self.layout)

    def call_delete_popup(self, button):
        i = 0
        for key in shopping_car.keys():
            if i == int(button.id):
                break
            i = i + 1
        self.delete_index = i
        self.delete_popup.open()

    def delete(self, button):
        key = shopping_car.keys()[self.delete_index]
        shopping_car.delete(key)
        self.delete_popup.dismiss()
        self.refresh()

    def clear(self, button):
        for key in shopping_car.keys():
            shopping_car.delete(key)
        self.clear_popup.dismiss()
        self.refresh()

    def calculate_total(self, discount):
        i = 0
        total = 0
        for key in shopping_car.keys():
            price = float(shopping_car.get(key)['precio'])
            for children in self.quantity_layout.children:
                if int(children.id) == i:
                    quantity = int(children.text)
            total = total + quantity * price
        total = (1 - discount) * total
        self.total.text = "%.2f" % total

    def apply_discount(self, button):
        try:
            discount = float(self.discount.text)
        except:
            self.discount_warning.open()
            return
        if discount > 100 or discount < 0:
            self.discount_warning.open()
            return
        discount = discount / 100
        self.calculate_total(discount)

    def refresh_total(self, text_input, text):
        try:
            discount = float(self.discount.text)
        except:
            self.discount_warning.open()
            return
        if discount > 100 or discount < 0:
            self.discount_warning.open()
            return
        discount = discount / 100
        self.calculate_total(discount)

    def export(self, button):
        name = self.export_name.text
        i = 0
        f = open("/sdcard/" + name + ".csv", "w+")
        f.write(
            "Codigo,Descripcion,Empresa,Unidades por bulto,Precio unitario,Cantidad,Subtotal\n"
        )
        for key in shopping_car.keys():
            codigo = shopping_car.get(key)['codigo']
            descripcion = shopping_car.get(key)['descripcion']
            empresa = shopping_car.get(key)['empresa']
            cantidad_bulto = shopping_car.get(key)['unidad_bulto']
            precio = shopping_car.get(key)['precio']
            for children in self.quantity_layout.children:
                if int(children.id) == i:
                    quantity = int(children.text)
            subtotal = str(quantity * float(precio))
            f.write("%s,%s,%s,%s,%s,%s,%s\n" %
                    (codigo, descripcion, empresa, cantidad_bulto, precio,
                     str(quantity), subtotal))
        f.write(" , , , , ,Descuento,%s\n" % (self.discount.text))
        f.write(" , , , , ,Total,%s\n" % (self.total.text))
        f.close()
        self.export_popup.dismiss()
        self.succes_export_popup.open()
コード例 #6
0
ファイル: mainwindow.py プロジェクト: linkel/plmeetmgr
    def setup_ui(self):
        # Set title
        self.setWindowTitle('Powerlifting Meet Manager')

        # Setup table model
        self.table_model = TableModel(top=TOP_LIFTERS)

        # Setup the table view
        self.table_view = TableView(self.table_model)

        # Setup the lifter group
        self.pb_lifter_add = QtGui.QPushButton('&Add')
        self.pb_lifter_remove = QtGui.QPushButton('&Remove')

        layout_lifter = QtGui.QHBoxLayout()
        layout_lifter.addWidget(self.pb_lifter_add)
        layout_lifter.addWidget(self.pb_lifter_remove)

        grp_lifter = QtGui.QGroupBox('Lifter')
        grp_lifter.setLayout(layout_lifter)

        # Setup the lifter group signals
        self.pb_lifter_add.clicked.connect(self.add_lifter)
        self.pb_lifter_remove.clicked.connect(self.remove_lifter)

        # Setup the control group
        self.pb_save_results = QtGui.QPushButton('&Save')
        self.pb_load_results = QtGui.QPushButton('&Load')
        self.pb_export_results = QtGui.QPushButton('&Export')

        layout_control = QtGui.QHBoxLayout()
        layout_control.addWidget(self.pb_save_results)
        layout_control.addWidget(self.pb_load_results)
        layout_control.addWidget(self.pb_export_results)

        grp_control = QtGui.QGroupBox('Control')
        grp_control.setLayout(layout_control)

        # Setup the control group signals
        self.pb_load_results.clicked.connect(self.load)
        self.pb_save_results.clicked.connect(self.save)
        self.pb_export_results.clicked.connect(self.export)

        # Set the header layout
        layout_header = QtGui.QHBoxLayout()
        layout_header.addWidget(grp_lifter)
        layout_header.addStretch(1)
        layout_header.addWidget(grp_control)

        # Results group
        grp_results = QtGui.QGroupBox('Results')
        layout_results = QtGui.QHBoxLayout()
        layout_results.addWidget(self.table_view)
        grp_results.setLayout(layout_results)

        # Main layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.addLayout(layout_header)
        main_layout.addWidget(grp_results)

        main_widget = QtGui.QWidget()
        main_widget.setLayout(main_layout)
        self.setCentralWidget(main_widget)
コード例 #7
0
ファイル: mainwindow.py プロジェクト: linkel/plmeetmgr
class MainWindow(QtGui.QMainWindow):
    TEMP_FILENAME = '.powerlifting_temp.dat'
    AUTO_INTERVAL = 600000   # 10 minutes

    def __init__(self, parent=None, flags=QtCore.Qt.Window):
        QtGui.QMainWindow.__init__(self, parent, flags)

        self.setup_ui()

        self.last_dir = '.'

        # Install global exception handler
        sys.excepthook = self.global_exception_handler

        # Initialise autosave
        self.setup_autosave()

        # Set this as the Qt log parent
        log.set_qt_parent(self)

    def setup_ui(self):
        # Set title
        self.setWindowTitle('Powerlifting Meet Manager')

        # Setup table model
        self.table_model = TableModel(top=TOP_LIFTERS)

        # Setup the table view
        self.table_view = TableView(self.table_model)

        # Setup the lifter group
        self.pb_lifter_add = QtGui.QPushButton('&Add')
        self.pb_lifter_remove = QtGui.QPushButton('&Remove')

        layout_lifter = QtGui.QHBoxLayout()
        layout_lifter.addWidget(self.pb_lifter_add)
        layout_lifter.addWidget(self.pb_lifter_remove)

        grp_lifter = QtGui.QGroupBox('Lifter')
        grp_lifter.setLayout(layout_lifter)

        # Setup the lifter group signals
        self.pb_lifter_add.clicked.connect(self.add_lifter)
        self.pb_lifter_remove.clicked.connect(self.remove_lifter)

        # Setup the control group
        self.pb_save_results = QtGui.QPushButton('&Save')
        self.pb_load_results = QtGui.QPushButton('&Load')
        self.pb_export_results = QtGui.QPushButton('&Export')

        layout_control = QtGui.QHBoxLayout()
        layout_control.addWidget(self.pb_save_results)
        layout_control.addWidget(self.pb_load_results)
        layout_control.addWidget(self.pb_export_results)

        grp_control = QtGui.QGroupBox('Control')
        grp_control.setLayout(layout_control)

        # Setup the control group signals
        self.pb_load_results.clicked.connect(self.load)
        self.pb_save_results.clicked.connect(self.save)
        self.pb_export_results.clicked.connect(self.export)

        # Set the header layout
        layout_header = QtGui.QHBoxLayout()
        layout_header.addWidget(grp_lifter)
        layout_header.addStretch(1)
        layout_header.addWidget(grp_control)

        # Results group
        grp_results = QtGui.QGroupBox('Results')
        layout_results = QtGui.QHBoxLayout()
        layout_results.addWidget(self.table_view)
        grp_results.setLayout(layout_results)

        # Main layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.addLayout(layout_header)
        main_layout.addWidget(grp_results)

        main_widget = QtGui.QWidget()
        main_widget.setLayout(main_layout)
        self.setCentralWidget(main_widget)

    # Autosave
    def setup_autosave(self):
        # Initialise mutex
        self.auto_mutex = QtCore.QMutex()

        # Set automatic timer
        self.auto_timer = QtCore.QTimer()
        self.auto_timer.timeout.connect(self.autosave)

        self.auto_timer.setSingleShot(False)
        self.auto_timer.setInterval(self.AUTO_INTERVAL)

        # Enable autosave
        self.set_autosave(True)

    def set_autosave(self, enable):
        if enable:
            # Save on changes to model
            self.table_model.model_changed.connect(self.autosave)

            # Start timer
            self.auto_timer.start()
        else:
            # Lock mutex
            self.auto_mutex.lock()

            # Stop timer
            self.auto_timer.stop()

            # Disconnect on changes to model
            self.table_model.model_changed.disconnect(self.autosave)

            # Unlock mutex
            self.auto_mutex.unlock()

    def autosave(self):
        # Lock mutex
        self.auto_mutex.lock()

        # Save the model to the temp filename
        self.table_model.save(self.TEMP_FILENAME)

        # Unlock mutex
        self.auto_mutex.unlock()

    # Close
    def closeEvent(self, event):
        result = QtGui.QMessageBox.question(self,
            'Exit',
            'Are you sure you want to exit?',
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No ,
            QtGui.QMessageBox.No
        )

        if result != QtGui.QMessageBox.Yes:
            event.ignore()
        else:
            # Disable autosave
            self.set_autosave(False)

            # Unregister exception hook
            sys.excepthook = sys.__excepthook__
            event.accept()

        return

    # Lifter addition/removal slots
    def add_lifter(self):
        # Show a new lifter dialog
        dlg = AddLifterDialog(self)
        dlg.exec_()

        if dlg.result():
            # Get lifter
            lifter = dlg.lifter()
            self.table_model.add(lifter)

    def remove_lifter(self):
        # Get active lifter
        index = self.table_view.currentIndex()

        lifter_info = self.table_model.index_to_lifter(index)
        if lifter_info is None:
            return

        lifter = lifter_info[0]

        # Check to remove lifter
        result = QtGui.QMessageBox.question(self,
            'Remove Lifter',
            "Remove lifter '%s'?" % lifter.name,
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No ,
            QtGui.QMessageBox.No
        )

        if result != QtGui.QMessageBox.Yes:
            return

        # Remove the lifter at index
        self.table_model.remove(index)

    # Control button slots
    def save(self):
        full_path = QtGui.QFileDialog.getSaveFileName(
            self, 'Save', self.last_dir, '*.dat'
        )

        if full_path.isEmpty():
            return

        # Decompose full path (e.g. for checks)
        dir_, filename = os.path.split(str(full_path))
        self.last_dir = dir_

        root, ext = os.path.splitext(filename)

        # Put back together
        full_path = os.path.join(dir_, root + '.dat')

        self.table_model.save(full_path)

    def load(self):
        full_path = QtGui.QFileDialog.getOpenFileName(
            self, 'Load', self.last_dir, '*.dat'
        )

        if full_path.isEmpty():
            return

        # Decompose full path
        full_path = str(full_path)
        dir_, filename = os.path.split(full_path)
        self.last_dir = dir_

        self.table_model.load(full_path)

    def export(self):
        full_path = QtGui.QFileDialog.getSaveFileName(
            self, 'Export', self.last_dir, '*.html'
        )

        if full_path.isEmpty():
            return

        # Decompose full path (e.g. for checks)
        dir_, filename = os.path.split(str(full_path))
        self.last_dir = dir_

        root, ext = os.path.splitext(filename)

        full_path = os.path.join(dir_, root + '.html')

        self.table_model.export(full_path)

    @classmethod
    def global_exception_handler(cls, type_, exception, tb):
        string_buffer = io.StringIO()
        traceback.print_tb(tb, None, string_buffer)

        logger.error(
            'Unhandled Exception\n%s, "%s"\n:: %s',
            type_.__name__,
            exception.message,
            string_buffer.getvalue()
        )

        string_buffer.close()