|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""This module provides views to manage the contacts table.""" |
| 4 | + |
| 5 | +from PyQt5.QtCore import Qt |
| 6 | +from PyQt5.QtWidgets import ( |
| 7 | + QAbstractItemView, |
| 8 | + QHBoxLayout, |
| 9 | + QDialog, |
| 10 | + QDialogButtonBox, |
| 11 | + QFormLayout, |
| 12 | + QLineEdit, |
| 13 | + QMessageBox, |
| 14 | + QMainWindow, |
| 15 | + QPushButton, |
| 16 | + QTableView, |
| 17 | + QVBoxLayout, |
| 18 | + QWidget, |
| 19 | +) |
| 20 | + |
| 21 | +from .model import ContactsModel |
| 22 | + |
| 23 | + |
| 24 | +class Window(QMainWindow): |
| 25 | + """Main Window.""" |
| 26 | + |
| 27 | + def __init__(self, parent=None): |
| 28 | + """Initializer.""" |
| 29 | + # Snip... |
| 30 | + self.contactsModel = ContactsModel() |
| 31 | + self.setupUI() |
| 32 | + |
| 33 | + def setupUI(self): |
| 34 | + """Setup the main window's GUI.""" |
| 35 | + # Create the table view widget |
| 36 | + self.table = QTableView() |
| 37 | + self.table.setModel(self.contactsModel.model) |
| 38 | + self.deleteButton.clicked.connect(self.deleteContact) |
| 39 | + self.clearAllButton.clicked.connect(self.clearContacts) |
| 40 | + self.table.setSelectionBehavior(QAbstractItemView.SelectRows) |
| 41 | + self.table.resizeColumnsToContents() |
| 42 | + # Create buttons |
| 43 | + self.addButton = QPushButton("Add...") |
| 44 | + self.deleteButton = QPushButton("Delete") |
| 45 | + self.clearAllButton = QPushButton("Clear All") |
| 46 | + # Lay out the GUI |
| 47 | + layout = QVBoxLayout() |
| 48 | + layout.addWidget(self.addButton) |
| 49 | + layout.addWidget(self.deleteButton) |
| 50 | + layout.addStretch() |
| 51 | + layout.addWidget(self.clearAllButton) |
| 52 | + self.layout.addWidget(self.table) |
| 53 | + self.layout.addLayout(layout) |
| 54 | + |
| 55 | + |
| 56 | +class AddDialog(QDialog): |
| 57 | + """Add Contact dialog.""" |
| 58 | + |
| 59 | + def __init__(self, parent=None): |
| 60 | + """Initializer.""" |
| 61 | + super().__init__(parent=parent) |
| 62 | + self.setWindowTitle("Add Contact") |
| 63 | + self.layout = QVBoxLayout() |
| 64 | + self.setLayout(self.layout) |
| 65 | + self.data = None |
| 66 | + |
| 67 | + self.setupUI() |
| 68 | + |
| 69 | + def setupUI(self): |
| 70 | + """Setup the Add Contact dialog's GUI.""" |
| 71 | + # Create line edits for data fields |
| 72 | + self.nameField = QLineEdit() |
| 73 | + self.nameField.setObjectName("Name") |
| 74 | + self.addButton.clicked.connect(self.openAddDialog) |
| 75 | + self.jobField = QLineEdit() |
| 76 | + self.jobField.setObjectName("Job") |
| 77 | + self.emailField = QLineEdit() |
| 78 | + self.emailField.setObjectName("Email") |
| 79 | + # Lay out the data fields |
| 80 | + layout = QFormLayout() |
| 81 | + layout.addRow("Name:", self.nameField) |
| 82 | + layout.addRow("Job:", self.jobField) |
| 83 | + layout.addRow("Email:", self.emailField) |
| 84 | + self.layout.addLayout(layout) |
| 85 | + # Add standard buttons to the dialog and connect them |
| 86 | + self.buttonsBox = QDialogButtonBox(self) |
| 87 | + self.buttonsBox.setOrientation(Qt.Horizontal) |
| 88 | + self.buttonsBox.setStandardButtons( |
| 89 | + QDialogButtonBox.Ok | QDialogButtonBox.Cancel |
| 90 | + ) |
| 91 | + self.buttonsBox.accepted.connect(self.accept) |
| 92 | + self.buttonsBox.rejected.connect(self.reject) |
| 93 | + self.layout.addWidget(self.buttonsBox) |
| 94 | + |
| 95 | + def accept(self): |
| 96 | + """Accept the data provided through the dialog.""" |
| 97 | + self.data = [] |
| 98 | + for field in (self.nameField, self.jobField, self.emailField): |
| 99 | + if not field.text(): |
| 100 | + QMessageBox.critical( |
| 101 | + self, |
| 102 | + "Error!", |
| 103 | + f"You must provide a contact's {field.objectName()}", |
| 104 | + ) |
| 105 | + self.data = None # Reset .data |
| 106 | + return |
| 107 | + |
| 108 | + self.data.append(field.text()) |
| 109 | + |
| 110 | + if not self.data: |
| 111 | + return |
| 112 | + |
| 113 | + super().accept() |
| 114 | + |
| 115 | + def openAddDialog(self): |
| 116 | + """Open the Add Contact dialog.""" |
| 117 | + dialog = AddDialog(self) |
| 118 | + if dialog.exec() == QDialog.Accepted: |
| 119 | + self.contactsModel.addContact(dialog.data) |
| 120 | + self.table.resizeColumnsToContents() |
| 121 | + |
| 122 | + def deleteContact(self): |
| 123 | + """Delete the selected contact from the database.""" |
| 124 | + row = self.table.currentIndex().row() |
| 125 | + if row < 0: |
| 126 | + return |
| 127 | + |
| 128 | + messageBox = QMessageBox.warning( |
| 129 | + self, |
| 130 | + "Warning!", |
| 131 | + "Do you want to remove the selected contact?", |
| 132 | + QMessageBox.Ok | QMessageBox.Cancel, |
| 133 | + ) |
| 134 | + |
| 135 | + if messageBox == QMessageBox.Ok: |
| 136 | + self.contactsModel.deleteContact(row) |
| 137 | + |
| 138 | + def clearContacts(self): |
| 139 | + """Remove all contacts from the database.""" |
| 140 | + messageBox = QMessageBox.warning( |
| 141 | + self, |
| 142 | + "Warning!", |
| 143 | + "Do you want to remove all your contacts?", |
| 144 | + QMessageBox.Ok | QMessageBox.Cancel, |
| 145 | + ) |
| 146 | + |
| 147 | + if messageBox == QMessageBox.Ok: |
| 148 | + self.contactsModel.clearContacts() |
0 commit comments