Example #1
1
    def createOrdersTab(self):
        """Create the page to view the Orders table from the database."""
        ord_sql_model = QSqlRelationalTableModel()
        ord_sql_model.setTable("Orders")
        ord_sql_model.setRelation(
            ord_sql_model.fieldIndex("product_id"),
            QSqlRelation("Products", "product_id", "product_name"))
        ord_sql_model.setRelation(
            ord_sql_model.fieldIndex("customer_id"),
            QSqlRelation("Customers", "customer_id", "first_name"))
        ord_sql_model.setHeaderData(ord_sql_model.fieldIndex("customer_id"),
                                    Qt.Orientation.Horizontal, "customer_name")
        ord_sql_model.select()  # Populate the model with data

        ord_proxy_model = QSortFilterProxyModel()
        ord_proxy_model.setSourceModel(ord_sql_model)

        ord_table = QTableView()
        ord_table.setSortingEnabled(True)
        ord_table.setModel(ord_proxy_model)
        ord_table.setItemDelegate(SqlProxyDelegate(ord_table))
        ord_table.setItemDelegateForColumn(
            ord_sql_model.fieldIndex("date_of_order"), DateDelegate())
        ord_table.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeMode.Stretch)

        ord_h_box = QHBoxLayout()
        ord_h_box.addWidget(ord_table)
        self.orders_tab.setLayout(ord_h_box)
    def setUpMainWindow(self):
        """Set up the GUI's main window."""
        headers, data = self.loadCSVData()

        # Create QTableView object and set up its behavior
        table_view = QTableView()
        table_view.setSelectionMode(
            QAbstractItemView.SelectionMode.ExtendedSelection)
        # Set up the horizontal header so that cells resize to fit
        # contents, and so that the last column stretches to take up empty space
        table_view.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeMode.ResizeToContents)
        table_view.horizontalHeader().setStretchLastSection(True)

        self.model = TableModel(headers=headers, data=data)
        table_view.setModel(self.model)

        self.setCentralWidget(table_view)
    def setUpMainWindow(self):
        """Set up the GUI's main window."""
        header_label = QLabel("List of Users")

        # Create model and table objects
        model = QStandardItemModel()
        model.setColumnCount(3)
        model.setHorizontalHeaderLabels(["Name", "Birthdate", "Actions"])

        table_view = QTableView()
        # NOTE: setEditTriggers() is not used so that the user
        # can double-click and edit cells
        table_view.setModel(model)
        table_view.horizontalHeader().setSectionResizeMode(
            0, QHeaderView.ResizeMode.Stretch)
        # Set the item delegate for a specific column, in this case column 1
        table_view.setItemDelegateForColumn(1, DateEditDelegate())

        names_list = ["Willman, Joshua", "Davis, Scott", "Garcia, Sky"]

        # Add items to each row in the table by looping over
        # the names_list and adding the date edit and button widgets
        for row, name in enumerate(names_list):
            model.setItem(row, QStandardItem(name))
            # Create an item and set the initial value for the second column.
            # Here the QDate values are converted to strings to make it easier
            # to align the text without having to subclass a model class
            date_item = QStandardItem(
                QDate.currentDate().toString("MM/dd/yyyy"))
            date_item.setTextAlignment(Qt.AlignmentFlag.AlignVCenter
                                       | Qt.AlignmentFlag.AlignRight)
            model.setItem(row, 1, date_item)
            # Set the widgets in the final column for each row
            index = model.index(row, 2, QModelIndex())
            table_view.setIndexWidget(index, EditCellWidget(table_view))

        # Set up main layout and container object for main window
        main_v_box = QVBoxLayout()
        main_v_box.addWidget(header_label)
        main_v_box.addWidget(table_view)

        container = QWidget()
        container.setLayout(main_v_box)
        self.setCentralWidget(container)
Example #4
0
    def createCategoriesTab(self):
        """Create the page to view the Categories table from the database."""
        cat_sql_model = QSqlRelationalTableModel()
        cat_sql_model.setTable("Categories")
        cat_sql_model.select()  # Populate the model with data

        cat_proxy_model = QSortFilterProxyModel()
        cat_proxy_model.setSourceModel(cat_sql_model)

        cat_table = QTableView()
        cat_table.setSortingEnabled(True)
        cat_table.setModel(cat_proxy_model)
        cat_table.setItemDelegateForColumn(
            cat_sql_model.fieldIndex("category_id"), ReadOnlyDelegate())
        cat_table.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeMode.Stretch)

        cat_h_box = QHBoxLayout()
        cat_h_box.addWidget(cat_table)
        self.category_tab.setLayout(cat_h_box)
Example #5
0
    def createStaffTab(self):
        """Create the page to view the Staff table from the database."""
        staff_sql_model = QSqlRelationalTableModel()
        staff_sql_model.setTable("Staff")
        staff_sql_model.select()  # Populate the model with data

        staff_proxy_model = QSortFilterProxyModel()
        staff_proxy_model.setSourceModel(staff_sql_model)

        staff_table = QTableView()
        staff_table.setSortingEnabled(True)
        staff_table.setModel(staff_proxy_model)
        staff_table.setItemDelegateForColumn(
            staff_sql_model.fieldIndex("staff_id"), ReadOnlyDelegate())
        staff_table.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeMode.Stretch)

        staff_h_box = QHBoxLayout()
        staff_h_box.addWidget(staff_table)
        self.staff_tab.setLayout(staff_h_box)
Example #6
0
 def setupUI(self):
     """Setup the main window's GUI."""
     # Create the table view widget
     self.table = QTableView()
     self.table.setModel(self.contactsModel.model)
     self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
     self.table.resizeColumnsToContents()
     # Create buttons
     self.addButton = QPushButton("Add...")
     self.addButton.clicked.connect(self.openAddDialog)
     self.deleteButton = QPushButton("Delete")
     self.deleteButton.clicked.connect(self.deleteContact)
     self.clearAllButton = QPushButton("Clear All")
     self.clearAllButton.clicked.connect(self.clearContacts)
     # Lay out the GUI
     layout = QVBoxLayout()
     layout.addWidget(self.addButton)
     layout.addWidget(self.deleteButton)
     layout.addStretch()
     layout.addWidget(self.clearAllButton)
     self.layout.addWidget(self.table)
     self.layout.addLayout(layout)
Example #7
0
    def createProductsTab(self):
        """Create the page to view the Products table from the database."""
        prod_sql_model = QSqlRelationalTableModel()
        prod_sql_model.setTable("Products")
        prod_sql_model.setRelation(
            prod_sql_model.fieldIndex("category_id"),
            QSqlRelation("Categories", "category_id", "category_name"))
        prod_sql_model.select()  # Populate the model with data

        prod_proxy_model = QSortFilterProxyModel()
        prod_proxy_model.setSourceModel(prod_sql_model)

        prod_table = QTableView()
        prod_table.setSortingEnabled(True)
        prod_table.setModel(prod_proxy_model)
        prod_table.setItemDelegate(SqlProxyDelegate(prod_table))
        prod_table.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeMode.Stretch)

        prod_h_box = QHBoxLayout()
        prod_h_box.addWidget(prod_table)
        self.products_tab.setLayout(prod_h_box)
Example #8
0
class Window(QMainWindow):
    """ Main Window."""

    def __init__(self, parent=None):
        """Initializer."""
        super().__init__(parent)
        self.setWindowTitle("RP Contacts")
        self.resize(550, 250)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.layout = QHBoxLayout()
        self.centralWidget.setLayout(self.layout)

        self.ContactsModel = ContactsModel()
        self.setupUI()

    def setupUI(self):
        """Setup the main window's GUI."""
        # Create the table view widget
        self.table = QTableView()
        self.table.setModel(self.contactsModel.model)
        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.table.resizeColumnsToContents()
        # Create buttons
        self.addButton = QPushButton("Add...")
        self.addButton.clicked.connect(self.openAddDialog)
        self.deleteButton = QPushButton("Delete")
        self.deleteButton.clicked.connect(self.deleteContact)
        self.clearAllButton = QPushButton("Clear All")
        self.clearAllButton.clicked.connect(self.clearContacts)
        # Lay out the GUI
        layout = QVBoxLayout()
        layout.addWidget(self.addButton)
        layout.addWidget(self.deleteButton)
        layout.addStretch()
        layout.addWidget(self.clearAllButton)
        self.layout.addWidget(self.table)
        self.layout.addLayout(layout)

    def openAddDialog(self):
        """Open the Add Contact dialog."""
        dialog = AddDialog(self)
        if dialog.exec() == QDialog.Accepted:
            self.ContactsModel.addContact(dialog.data)
            self.table.resizeColumnsToContents()

    def deleteContact(self):
        row = self.table.currentIndex().row()
        if row < 0:
            return
        messageBox = QMessageBox.warning(
            self,
            "Warning!",
            "Do you want to remove the selected contact?",
            QMessageBox.Ok | QMessageBox.Cancel,
        )
        if messageBox == QMessageBox.Ok:
            self.contactsModel.deleteContact(row)

    def clearContacts(self):
        """Remove all contacts from the database."""
        messageBox = QMessageBox.warning(
            self,
            "Warning!",
            "Do you want to remove all your contacts?"
            QMessageBox.Ok | QMessageBox.Cancel,
        )
Example #9
0
    def createCustomersTab(self):
        """Create the page to view the Customers table from the database."""
        cust_sql_model = QSqlRelationalTableModel()
        cust_sql_model.setTable("Customers")
        cust_sql_model.setRelation(
            cust_sql_model.fieldIndex("staff_id"),
            QSqlRelation("Staff", "staff_id", "username"))
        cust_sql_model.setHeaderData(cust_sql_model.fieldIndex("staff_id"),
                                     Qt.Orientation.Horizontal,
                                     "staff_username")
        cust_sql_model.select()  # Populate the model with data

        cust_proxy_model = QSortFilterProxyModel()
        cust_proxy_model.setSourceModel(cust_sql_model)

        cust_table = QTableView()
        cust_table.setSortingEnabled(True)
        cust_table.setModel(cust_proxy_model)
        cust_table.setItemDelegate(SqlProxyDelegate(cust_table))
        cust_table.setItemDelegateForColumn(cust_sql_model.fieldIndex("phone"),
                                            PhoneDelegate())
        cust_table.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeMode.Stretch)

        cust_h_box = QHBoxLayout()
        cust_h_box.addWidget(cust_table)
        self.customer_tab.setLayout(cust_h_box)
    def setUpMainWindow(self):
        """Set up the GUI's main window."""
        header_label = QLabel("List of Users")

        # Create model and table objects
        model = QStandardItemModel()
        model.setColumnCount(3)
        model.setHorizontalHeaderLabels(["Name", "Birthdate", "Actions"])

        table_view = QTableView()
        table_view.setEditTriggers(
            QAbstractItemView.EditTrigger.NoEditTriggers)
        # NOTE: Uncomment for table cells to be unselectable
        #table_view.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
        table_view.setModel(model)
        table_view.horizontalHeader().setSectionResizeMode(
            0, QHeaderView.ResizeMode.Stretch)

        names_list = ["Willman, Joshua", "Davis, Scott", "Garcia, Sky"]

        # Add items to each row in the table by looping over
        # the names_list and adding the date edit and button widgets
        for row, name in enumerate(names_list):
            model.setItem(row, QStandardItem(name))
            # Setting the widget at an index in a QTableView involves
            # acquiring the QModelIndex values of the current position.
            # One way to do this is to use the QAbstractItemModel.sibling()
            # method to retrieve the QModelIndex index from the specified
            # row and column (here the column is 1)
            index = table_view.model().sibling(row, 1, QModelIndex())
            date_edit = QDateEdit(QDate.currentDate(
            ))  # Create QDateEdit object that starts at current date
            date_edit.setDateRange(QDate(1900, 1, 1), QDate.currentDate())
            date_edit.setDisplayFormat("MM/dd/yyyy")
            date_edit.setAlignment(
                Qt.AlignmentFlag.AlignRight)  # Align the text
            date_edit.setAutoFillBackground(True)
            table_view.setIndexWidget(index, date_edit)
            # Set the widgets in the final column for each row
            index = table_view.model().sibling(row, 2, QModelIndex())
            table_view.setIndexWidget(index, EditCellWidget(table_view))

        # Set up main layout and container object for main window
        main_v_box = QVBoxLayout()
        main_v_box.addWidget(header_label)
        main_v_box.addWidget(table_view)

        container = QWidget()
        container.setLayout(main_v_box)
        self.setCentralWidget(container)