From 9a82d4a921e705f921694bf2490ad8bc8983752c Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Mon, 22 Jul 2024 23:58:22 +0300 Subject: [PATCH] Add Lesson 5 Task 2 --- Lesson_5/Task_2/.gitignore | 74 +++++++++++++++ Lesson_5/Task_2/Task_2.pro | 26 ++++++ Lesson_5/Task_2/cardindex.cpp | 120 ++++++++++++++++++++++++ Lesson_5/Task_2/cardindex.h | 38 ++++++++ Lesson_5/Task_2/cardindex.ui | 166 ++++++++++++++++++++++++++++++++++ Lesson_5/Task_2/main.cpp | 11 +++ Lesson_5/Task_2/record.cpp | 5 + Lesson_5/Task_2/record.h | 33 +++++++ 8 files changed, 473 insertions(+) create mode 100644 Lesson_5/Task_2/.gitignore create mode 100644 Lesson_5/Task_2/Task_2.pro create mode 100644 Lesson_5/Task_2/cardindex.cpp create mode 100644 Lesson_5/Task_2/cardindex.h create mode 100644 Lesson_5/Task_2/cardindex.ui create mode 100644 Lesson_5/Task_2/main.cpp create mode 100644 Lesson_5/Task_2/record.cpp create mode 100644 Lesson_5/Task_2/record.h diff --git a/Lesson_5/Task_2/.gitignore b/Lesson_5/Task_2/.gitignore new file mode 100644 index 0000000..4a0b530 --- /dev/null +++ b/Lesson_5/Task_2/.gitignore @@ -0,0 +1,74 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/Lesson_5/Task_2/Task_2.pro b/Lesson_5/Task_2/Task_2.pro new file mode 100644 index 0000000..aac6372 --- /dev/null +++ b/Lesson_5/Task_2/Task_2.pro @@ -0,0 +1,26 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + cardindex.cpp \ + record.cpp + +HEADERS += \ + cardindex.h \ + record.h + +FORMS += \ + cardindex.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/Lesson_5/Task_2/cardindex.cpp b/Lesson_5/Task_2/cardindex.cpp new file mode 100644 index 0000000..f9a29b0 --- /dev/null +++ b/Lesson_5/Task_2/cardindex.cpp @@ -0,0 +1,120 @@ +#include "cardindex.h" +#include "ui_cardindex.h" + +CardIndex::CardIndex(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::CardIndex) +{ + ui->setupUi(this); + connect(ui->b_add, &QPushButton::clicked, this, &CardIndex::add); + connect(ui->b_edit, &QPushButton::clicked, this, &CardIndex::edit); + connect(ui->b_remove, &QPushButton::clicked, this, &CardIndex::remove); + connect(ui->b_filter, &QPushButton::clicked, this, &CardIndex::filter); + connect(ui->l_records, &QListWidget::itemClicked, this, &CardIndex::select); +} + +CardIndex::~CardIndex() +{ + delete ui; +} + +void CardIndex::filter() +{ + + ui->l_records->clear(); + ui->e_selected->clear(); + bool typeEmpty = ui->e_type->text().isEmpty(); + bool titleEmpty = ui->e_title->text().isEmpty(); + bool authorEmpty = ui->e_author->text().isEmpty(); + bool libraryEmpty = ui->e_library->text().isEmpty(); + bool dateEmpty = !ui->cb_sortByDate->isChecked(); + for (int record = 0; record < m_records.size(); ++record) { + bool typeMatch = m_records[record]->getType().contains(ui->e_type->text()); + bool titleMatch = m_records[record]->getTitle().contains(ui->e_title->text()); + bool authorMatch = m_records[record]->getAuthor().contains(ui->e_author->text()); + bool libraryMatch = m_records[record]->getLibrary().contains(ui->e_library->text()); + bool dateMatch = m_records[record]->getDate() == ui->e_date->date(); + if ( + (typeEmpty || typeMatch) + && (titleEmpty || titleMatch) + && (authorEmpty || authorMatch) + && (libraryEmpty || libraryMatch) + && (dateEmpty || dateMatch) + ) + ui->l_records->addItem(m_records[record]->getTitle()); + + } +} + +void CardIndex::add() +{ + if (findRecordByTitle(ui->e_title->text()) != NULL) + return; + Record* record = new Record(); + setRecordData(record); + m_records.append(record); + reloadRecords(); +} + +void CardIndex::edit() +{ + QListWidgetItem* item = ui->l_records->currentItem(); + if (item == NULL) + return; + Record* record = findRecordByTitle(item->text()); + setRecordData(record); + reloadRecords(); +} + +void CardIndex::remove() +{ + QListWidgetItem* item = ui->l_records->currentItem(); + if (item == NULL) + return; + for (int record = 0; record < m_records.size(); ++record) + if (item->text() == m_records[record]->getTitle()) { + delete m_records[record]; + m_records.removeAt(record); + } + reloadRecords(); + +} + +void CardIndex::select(QListWidgetItem * item) +{ + for (int record = 0; record < m_records.size(); ++record) { + Record* currentRecord = m_records[record]; + if (item->text() == currentRecord->getTitle()) { + ui->e_selected->setText("Type: " + currentRecord->getType() + "\n" + + "Title: " + currentRecord->getTitle() + "\n" + + "Author: " + currentRecord->getAuthor() + "\n" + + "Library: " + currentRecord->getLibrary() + "\n" + + "Date: " + currentRecord->getDate().toString()); + break; + } + } +} + +Record *CardIndex::findRecordByTitle(QString title) +{ + for (int record = 0; record < m_records.size(); ++record) + if (title == m_records[record]->getTitle()) + return m_records[record]; + return nullptr; +} + +void CardIndex::reloadRecords() +{ + ui->l_records->clear(); + for (int record = 0; record < m_records.size(); ++record) + ui->l_records->addItem(m_records[record]->getTitle()); +} + +void CardIndex::setRecordData(Record* record) +{ + record->setType(ui->e_type->text()); + record->setTitle(ui->e_title->text()); + record->setAuthor(ui->e_author->text()); + record->setLibrary(ui->e_library->text()); + record->setDate(ui->e_date->date()); +} diff --git a/Lesson_5/Task_2/cardindex.h b/Lesson_5/Task_2/cardindex.h new file mode 100644 index 0000000..a31e447 --- /dev/null +++ b/Lesson_5/Task_2/cardindex.h @@ -0,0 +1,38 @@ +#ifndef CARDINDEX_H +#define CARDINDEX_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { +class CardIndex; +} +QT_END_NAMESPACE + +class CardIndex : public QMainWindow +{ + Q_OBJECT + +public: + CardIndex(QWidget *parent = nullptr); + ~CardIndex(); + +private slots: + void filter(); + void add(); + void edit(); + void remove(); + void select(QListWidgetItem *); + + +private: + Ui::CardIndex *ui; + QVector m_records; + + Record *findRecordByTitle(QString title); + void reloadRecords(); + void setRecordData(Record *); +}; +#endif // CARDINDEX_H diff --git a/Lesson_5/Task_2/cardindex.ui b/Lesson_5/Task_2/cardindex.ui new file mode 100644 index 0000000..3cc7bc5 --- /dev/null +++ b/Lesson_5/Task_2/cardindex.ui @@ -0,0 +1,166 @@ + + + CardIndex + + + + 0 + 0 + 800 + 600 + + + + CardIndex + + + + + + + + + Type + + + + + + + + + + Title + + + + + + + + + + Author + + + + + + + + + + Library + + + + + + + + + + Date + + + + + + + yyyy-MM-dd + + + true + + + + + + + Sort by date + + + + + + + + + Filter + + + + + + + Add + + + + + + + Edit + + + + + + + Remove + + + + + + + + + + + + + Records: + + + + + + + + + + + + + + Selected card: + + + + + + + true + + + + + + + + + + + 0 + 0 + 800 + 25 + + + + + + + + diff --git a/Lesson_5/Task_2/main.cpp b/Lesson_5/Task_2/main.cpp new file mode 100644 index 0000000..c89256e --- /dev/null +++ b/Lesson_5/Task_2/main.cpp @@ -0,0 +1,11 @@ +#include "cardindex.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + CardIndex w; + w.show(); + return a.exec(); +} diff --git a/Lesson_5/Task_2/record.cpp b/Lesson_5/Task_2/record.cpp new file mode 100644 index 0000000..ce9f696 --- /dev/null +++ b/Lesson_5/Task_2/record.cpp @@ -0,0 +1,5 @@ +#include "record.h" + +Record::Record(QObject *parent) + : QObject{parent} +{} diff --git a/Lesson_5/Task_2/record.h b/Lesson_5/Task_2/record.h new file mode 100644 index 0000000..12480ea --- /dev/null +++ b/Lesson_5/Task_2/record.h @@ -0,0 +1,33 @@ +#ifndef RECORD_H +#define RECORD_H + +#include +#include + +class Record : public QObject +{ + Q_OBJECT +public: + explicit Record(QObject *parent = nullptr); + QString getType() { return m_type; } + void setType(QString type) { m_type = type; } + QString getTitle() { return m_title; } + void setTitle(QString title) { m_title = title; } + QString getAuthor() { return m_author; } + void setAuthor(QString author) { m_author = author; } + QString getLibrary() { return m_library; } + void setLibrary(QString library) { m_library = library; } + QDate getDate() { return m_date; } + void setDate(QDate date) { m_date = date; } + +signals: + +private: + QString m_type; + QString m_title; + QString m_author; + QString m_library; + QDate m_date; +}; + +#endif // RECORD_H