From 7284a3bbeb0f864fa6cb98f087083fb8295875e5 Mon Sep 17 00:00:00 2001 From: mitchcamza <192mitch88@gmail.com> Date: Mon, 5 Aug 2024 11:20:24 -0700 Subject: [PATCH] Implement BookTableView in MainWindow; Implement clearFilter() method --- COS3711-03-01/src/CMakeLists.txt | 6 +++--- COS3711-03-01/src/mainwindow.cpp | 34 ++++++++++++++++++++------------ COS3711-03-01/src/mainwindow.h | 15 +++++++------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/COS3711-03-01/src/CMakeLists.txt b/COS3711-03-01/src/CMakeLists.txt index 0319ee8..9180d0e 100644 --- a/COS3711-03-01/src/CMakeLists.txt +++ b/COS3711-03-01/src/CMakeLists.txt @@ -18,12 +18,12 @@ set(PROJECT_SOURCES author.h author.cpp book.h book.cpp bookinput.h bookinput.cpp + booktablemodel.h booktablemodel.cpp + bookproxymodel.h bookproxymodel.cpp ) # Create library -add_library(MyProjectLib ${PROJECT_SOURCES} - booktablemodel.h booktablemodel.cpp - bookproxymodel.h bookproxymodel.cpp) +add_library(MyProjectLib ${PROJECT_SOURCES}) # Include directories for the library target_include_directories(MyProjectLib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/COS3711-03-01/src/mainwindow.cpp b/COS3711-03-01/src/mainwindow.cpp index 644379c..736288f 100644 --- a/COS3711-03-01/src/mainwindow.cpp +++ b/COS3711-03-01/src/mainwindow.cpp @@ -6,10 +6,10 @@ */ -#include "mainwindow.h" -// #include "bookproxymodel.h" #include "bookinput.h" -// #include "bookview.h" +#include "bookproxymodel.h" +#include "booktablemodel.h" +#include "mainwindow.h" #include #include @@ -20,23 +20,27 @@ #include #include #include -#include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - // menuBar(new QMenuBar(this)), - // statusBar(new QStatusBar(this)), - // toolBar(new QToolBar(this)), - // bookProxyModel(new BookProxyModel(this)), - // bookView(new BookView(this)), + bookTableModel(new BookTableModel(this)), + bookProxyModel(new BookProxyModel(this)), + bookTableView(new QTableView(this)), actionAddBook(new QAction(QIcon(":/icons/addBook"), tr("New Book"), this)), actionExportBooks(new QAction(QIcon(":/icons/export"), tr("Export Books"), this)), actionClose(new QAction(QIcon(":/icons/exit"), tr("Exit Application"), this)), lineEditSearch(new QLineEdit(this)), pushButtonClear(new QPushButton("Clear", this)) { + // Proxy Model + bookProxyModel->setSourceModel(bookTableModel); + bookProxyModel->setFilterKeyColumn(0); + bookProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); + + // Signals and Slots connections connect(actionAddBook, &QAction::triggered, this, &MainWindow::addBook); connect(actionExportBooks, &QAction::triggered, this, &MainWindow::exportBooks); // connect(lineEditSearch, &QLineEdit::textEdited, bookProxyModel, &BookProxyModel::setFilterText); @@ -93,9 +97,11 @@ void MainWindow::setupUI() gridLayout->addWidget(lineEditSearch, 0, 0, 1, 3); gridLayout->addWidget(pushButtonClear, 0, 3, 1, 1); - // Proxy Model - // TODO: Replace tableView with bookView - gridLayout->addWidget(new QTableView(), 1, 0, 1, 4); + // Book table view + bookTableView->setModel(bookProxyModel); + bookTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); + bookTableView->setSortingEnabled(true); + gridLayout->addWidget(bookTableView, 1, 0, 1, 4); } void MainWindow::addBook() @@ -111,6 +117,8 @@ void MainWindow::exportBooks() void MainWindow::clearFilter() { - // TODO + lineEditSearch->clear(); + lineEditSearch->setFocus(); + bookProxyModel->setFilter(lineEditSearch->text()); } diff --git a/COS3711-03-01/src/mainwindow.h b/COS3711-03-01/src/mainwindow.h index 175245f..ea31d90 100644 --- a/COS3711-03-01/src/mainwindow.h +++ b/COS3711-03-01/src/mainwindow.h @@ -9,10 +9,12 @@ #define MAINWINDOW_H +class BookProxyModel; +class BookTableModel; +class BookView; class QLineEdit; class QPushButton; -class BookView; -class BookProxyModel; +class QTableView; #include @@ -38,12 +40,9 @@ private slots: void clearFilter(); private: - // BookView *bookView; - // BookProxyModel *bookProxyModel; - - // QMenuBar *menuBar; - // QStatusBar *statusBar; - // QToolBar *toolBar; + BookTableModel *bookTableModel; + BookProxyModel *bookProxyModel; + QTableView *bookTableView; QAction *actionAddBook; QAction *actionExportBooks; QAction *actionClose;