|
| 1 | +from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QScrollArea, |
| 2 | + QLineEdit, QFormLayout, QHBoxLayout, QFrame, QDateEdit, |
| 3 | + QPushButton, QLabel, QListWidget, QDialog, QAction, QToolBar) |
| 4 | +from PyQt5.QtCore import Qt |
| 5 | + |
| 6 | +from datetime import datetime |
| 7 | +from db import (get_all_books, create_table, insert_book, delete_book) |
| 8 | + |
| 9 | + |
| 10 | +class CreateRecord(QFrame): |
| 11 | + def __init__(self, main_window): |
| 12 | + super().__init__() |
| 13 | + self.main_window = main_window # Pass a reference to the main window |
| 14 | + |
| 15 | + self.date_entry = QDateEdit() |
| 16 | + self.book_name = QLineEdit() |
| 17 | + self.book_name.setPlaceholderText('Book name') |
| 18 | + self.add_button = QPushButton(text="Add Book") |
| 19 | + # Connect the button to add_book function |
| 20 | + self.add_button.clicked.connect(self.add_book) |
| 21 | + |
| 22 | + layout = QVBoxLayout(self) |
| 23 | + layout.addWidget(QLabel('Book Name:')) |
| 24 | + layout.addWidget(self.book_name) |
| 25 | + layout.addWidget(QLabel('Completed Date:')) |
| 26 | + layout.addWidget(self.date_entry) |
| 27 | + layout.addWidget(self.add_button) |
| 28 | + |
| 29 | + def add_book(self): |
| 30 | + book_name = self.book_name.text() |
| 31 | + completed_date = self.date_entry.date().toString("yyyy-MM-dd") |
| 32 | + |
| 33 | + if book_name: |
| 34 | + insert_book(book_name, completed_date) |
| 35 | + # Reload the book collection after adding a book |
| 36 | + self.main_window.load_collection() |
| 37 | + self.book_name.clear() # Clear the input field |
| 38 | + |
| 39 | + |
| 40 | +class BookCard(QFrame): |
| 41 | + def __init__(self, book_id, bookname, completed_date): |
| 42 | + super().__init__() |
| 43 | + self.setStyleSheet( |
| 44 | + 'background:white; border-radius:4px; color:black;' |
| 45 | + ) |
| 46 | + self.setFixedHeight(110) |
| 47 | + self.book_id = book_id |
| 48 | + layout = QVBoxLayout() |
| 49 | + label = QLabel(f'<strong>{bookname}</strong>') |
| 50 | + |
| 51 | + # Update the format string here |
| 52 | + parsed_datetime = datetime.strptime(completed_date, "%Y-%m-%d") |
| 53 | + formatted_datetime = parsed_datetime.strftime("%Y-%m-%d") |
| 54 | + |
| 55 | + date_completed = QLabel(f"Completed {formatted_datetime}") |
| 56 | + delete_button = QPushButton( |
| 57 | + text='Delete', clicked=self.delete_book_click) |
| 58 | + # delete_button.setFixedWidth(60) |
| 59 | + delete_button.setStyleSheet('background:red; padding:4px;') |
| 60 | + |
| 61 | + layout.addWidget(label) |
| 62 | + layout.addWidget(date_completed) |
| 63 | + layout.addWidget(delete_button) |
| 64 | + layout.addStretch() |
| 65 | + self.setLayout(layout) |
| 66 | + |
| 67 | + def delete_book_click(self): |
| 68 | + delete_book(self.book_id) |
| 69 | + self.close() |
| 70 | + |
| 71 | + |
| 72 | +class Main(QMainWindow): |
| 73 | + def __init__(self): |
| 74 | + super().__init__() |
| 75 | + self.initUI() |
| 76 | + self.load_collection() |
| 77 | + |
| 78 | + def initUI(self): |
| 79 | + self.main_frame = QFrame() |
| 80 | + self.main_layout = QVBoxLayout(self.main_frame) |
| 81 | + |
| 82 | + # add register widget |
| 83 | + # Pass a reference to the main window |
| 84 | + self.register_widget = CreateRecord(self) |
| 85 | + self.main_layout.addWidget(self.register_widget) |
| 86 | + |
| 87 | + books_label = QLabel('Completed Books') |
| 88 | + books_label.setStyleSheet('font-size:18px;') |
| 89 | + self.main_layout.addWidget(books_label) |
| 90 | + self.book_collection_area() |
| 91 | + |
| 92 | + self.setCentralWidget(self.main_frame) |
| 93 | + |
| 94 | + def book_collection_area(self): |
| 95 | + scroll_frame = QFrame() |
| 96 | + self.book_collection_layout = QVBoxLayout(scroll_frame) |
| 97 | + |
| 98 | + scroll = QScrollArea() |
| 99 | + scroll.setWidgetResizable(True) |
| 100 | + scroll.setWidget(scroll_frame) |
| 101 | + scroll.setStyleSheet('QScrollArea{border:0px}') |
| 102 | + |
| 103 | + self.book_collection_layout.addStretch() |
| 104 | + self.main_layout.addWidget(scroll) |
| 105 | + |
| 106 | + def load_collection(self): |
| 107 | + # Clear existing book cards before reloading |
| 108 | + for i in reversed(range(self.book_collection_layout.count())): |
| 109 | + widget = self.book_collection_layout.itemAt(i).widget() |
| 110 | + if widget is not None: |
| 111 | + widget.deleteLater() |
| 112 | + |
| 113 | + collections = get_all_books() |
| 114 | + for collection in collections: |
| 115 | + frame = BookCard(*collection) |
| 116 | + self.book_collection_layout.insertWidget(0, frame) |
| 117 | + |
| 118 | + |
| 119 | +def main(): |
| 120 | + app = QApplication([]) |
| 121 | + app.setStyle('fusion') |
| 122 | + win = Main() |
| 123 | + win.show() |
| 124 | + app.exec_() |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + main() |
0 commit comments