Add Lesson 5 Task 2

This commit is contained in:
Stanislav Mykhailenko 2024-07-22 23:58:22 +03:00
parent 571578b022
commit 9a82d4a921
GPG key ID: 1E95E66A9C9D6A36
8 changed files with 473 additions and 0 deletions

74
Lesson_5/Task_2/.gitignore vendored Normal file
View file

@ -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

View file

@ -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

View file

@ -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());
}

View file

@ -0,0 +1,38 @@
#ifndef CARDINDEX_H
#define CARDINDEX_H
#include <QMainWindow>
#include <QListWidget>
#include <record.h>
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<Record *> m_records;
Record *findRecordByTitle(QString title);
void reloadRecords();
void setRecordData(Record *);
};
#endif // CARDINDEX_H

View file

@ -0,0 +1,166 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CardIndex</class>
<widget class="QMainWindow" name="CardIndex">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>CardIndex</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Type</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_type"/>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Title</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_title"/>
</item>
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Author</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_author"/>
</item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Library</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_library"/>
</item>
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>Date</string>
</property>
</widget>
</item>
<item>
<widget class="QDateEdit" name="e_date">
<property name="displayFormat">
<string>yyyy-MM-dd</string>
</property>
<property name="calendarPopup">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb_sortByDate">
<property name="text">
<string>Sort by date</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="b_filter">
<property name="text">
<string>Filter</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_add">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_edit">
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_remove">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Records:</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="l_records"/>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Selected card:</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="e_selected">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

11
Lesson_5/Task_2/main.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "cardindex.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CardIndex w;
w.show();
return a.exec();
}

View file

@ -0,0 +1,5 @@
#include "record.h"
Record::Record(QObject *parent)
: QObject{parent}
{}

33
Lesson_5/Task_2/record.h Normal file
View file

@ -0,0 +1,33 @@
#ifndef RECORD_H
#define RECORD_H
#include <QObject>
#include <QDate>
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