346 lines
8.9 KiB
C++
346 lines
8.9 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
|
|
#include "algo.h"
|
|
|
|
#include <QMessageBox>
|
|
#include <QSignalBlocker>
|
|
|
|
#include <algorithm>
|
|
|
|
bool CaseInsensitiveLess(const std::string& lhs, const std::string& rhs) {
|
|
return QString::compare(
|
|
QString::fromStdString(lhs),
|
|
QString::fromStdString(rhs),
|
|
Qt::CaseInsensitive
|
|
) < 0;
|
|
}
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow)
|
|
, m_random_gen_(std::random_device{}()) {
|
|
ui->setupUi(this);
|
|
|
|
ApplyModel();
|
|
}
|
|
|
|
MainWindow::~MainWindow() {
|
|
delete ui;
|
|
}
|
|
|
|
void MainWindow::ApplyModel() {
|
|
const auto it_tmp = deque_model_.iterator;
|
|
const QSignalBlocker blocker(ui->list_widget);
|
|
|
|
ui->list_widget->clear();
|
|
|
|
for (size_t i = 0; i < deque_model_.items.size(); ++i) {
|
|
ui->list_widget->addItem(
|
|
QString::fromStdString(std::to_string(i) + ": " + deque_model_.items[i])
|
|
);
|
|
}
|
|
|
|
ui->list_widget->addItem("end");
|
|
|
|
deque_model_.iterator = it_tmp;
|
|
|
|
ui->txt_size->setText(QString::number(deque_model_.items.size()));
|
|
|
|
const bool is_empty = deque_model_.items.empty();
|
|
ui->btn_pop_back->setDisabled(is_empty);
|
|
ui->btn_pop_front->setDisabled(is_empty);
|
|
ui->btn_erase->setDisabled(is_empty);
|
|
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::ApplyIterator() {
|
|
const QSignalBlocker blocker(ui->list_widget);
|
|
|
|
if (deque_model_.items.empty()) {
|
|
ui->list_widget->setCurrentRow(0);
|
|
ui->txt_elem_content->clear();
|
|
ui->btn_edit->setDisabled(true);
|
|
ui->btn_erase->setDisabled(true);
|
|
ui->btn_inc_iterator->setDisabled(true);
|
|
ui->btn_dec_iterator->setDisabled(true);
|
|
return;
|
|
}
|
|
|
|
const auto distance = std::distance(deque_model_.items.begin(), deque_model_.iterator);
|
|
const bool is_end = deque_model_.iterator == deque_model_.items.end();
|
|
const bool is_begin = deque_model_.iterator == deque_model_.items.begin();
|
|
|
|
ui->list_widget->setCurrentRow(static_cast<int>(distance));
|
|
ui->btn_edit->setDisabled(is_end);
|
|
ui->btn_erase->setDisabled(is_end);
|
|
ui->btn_inc_iterator->setDisabled(is_end);
|
|
ui->btn_dec_iterator->setDisabled(is_begin);
|
|
|
|
if (is_end) {
|
|
ui->txt_elem_content->clear();
|
|
} else {
|
|
ui->txt_elem_content->setText(QString::fromStdString(*deque_model_.iterator));
|
|
}
|
|
}
|
|
|
|
void MainWindow::SetRandomGen(const std::mt19937& random_gen) {
|
|
m_random_gen_ = random_gen;
|
|
}
|
|
|
|
void MainWindow::on_btn_clear_clicked() {
|
|
deque_model_.items.clear();
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_push_front_clicked() {
|
|
deque_model_.items.push_front(ui->txt_elem_content->text().toStdString());
|
|
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_push_back_clicked() {
|
|
deque_model_.items.push_back(ui->txt_elem_content->text().toStdString());
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_pop_back_clicked() {
|
|
if (deque_model_.items.empty()) {
|
|
return;
|
|
}
|
|
|
|
deque_model_.items.pop_back();
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_pop_front_clicked() {
|
|
if (deque_model_.items.empty()) {
|
|
return;
|
|
}
|
|
|
|
deque_model_.items.pop_front();
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_insert_clicked() {
|
|
deque_model_.items.insert(
|
|
deque_model_.iterator,
|
|
ui->txt_elem_content->text().toStdString()
|
|
);
|
|
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_erase_clicked() {
|
|
if (deque_model_.iterator == deque_model_.items.end()) {
|
|
return;
|
|
}
|
|
|
|
deque_model_.items.erase(deque_model_.iterator);
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_begin_clicked() {
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_end_clicked() {
|
|
deque_model_.iterator = deque_model_.items.end();
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_dec_iterator_clicked() {
|
|
if (deque_model_.iterator != deque_model_.items.begin()) {
|
|
--deque_model_.iterator;
|
|
}
|
|
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_inc_iterator_clicked() {
|
|
if (deque_model_.iterator != deque_model_.items.end()) {
|
|
++deque_model_.iterator;
|
|
}
|
|
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_list_widget_currentRowChanged(int currentRow) {
|
|
currentRow = std::clamp(currentRow, 0, static_cast<int>(deque_model_.items.size()));
|
|
deque_model_.iterator = deque_model_.items.begin() + currentRow;
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_tea_clicked() {
|
|
static std::deque<std::string> tea {
|
|
"Чай Лунцзин",
|
|
"Эрл Грей",
|
|
"Сенча",
|
|
"Пуэр",
|
|
"Дарджилинг",
|
|
"Ассам",
|
|
"Матча",
|
|
"Ганпаудер",
|
|
"Оолонг",
|
|
"Лапсанг Сушонг"
|
|
};
|
|
|
|
deque_model_.items = tea;
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_cakes_clicked() {
|
|
static std::deque<std::string> cakes {
|
|
"Красный бархат",
|
|
"Наполеон",
|
|
"Медовик",
|
|
"Тирамису",
|
|
"Прага",
|
|
"Чизкейк",
|
|
"Захер",
|
|
"Эстерхази",
|
|
"Морковный торт",
|
|
"Чёрный лес",
|
|
};
|
|
deque_model_.items = cakes;
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_edit_clicked() {
|
|
if (deque_model_.iterator == deque_model_.items.end()) {
|
|
return;
|
|
}
|
|
|
|
*deque_model_.iterator = ui->txt_elem_content->text().toStdString();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_resize_clicked() {
|
|
bool ok = false;
|
|
const int size = ui->txt_size->text().toInt(&ok);
|
|
|
|
if (!ok || size < 0 || size > 1000) {
|
|
QMessageBox::warning(this, "Error", "Incorrect size");
|
|
return;
|
|
}
|
|
|
|
deque_model_.items.resize(static_cast<size_t>(size));
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_find_clicked() {
|
|
deque_model_.iterator = std::find(
|
|
deque_model_.items.begin(),
|
|
deque_model_.items.end(),
|
|
ui->txt_elem_content->text().toStdString()
|
|
);
|
|
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_count_clicked() {
|
|
const int count = static_cast<int>(std::count(
|
|
deque_model_.items.begin(),
|
|
deque_model_.items.end(),
|
|
ui->le_count->text().toStdString()
|
|
));
|
|
|
|
ui->lbl_count->setText(QString::number(count));
|
|
}
|
|
|
|
void MainWindow::on_btn_min_element_clicked() {
|
|
deque_model_.iterator = std::min_element(
|
|
deque_model_.items.begin(),
|
|
deque_model_.items.end()
|
|
);
|
|
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_max_element_clicked() {
|
|
deque_model_.iterator = std::max_element(
|
|
deque_model_.items.begin(),
|
|
deque_model_.items.end()
|
|
);
|
|
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_sort_clicked() {
|
|
deque_model_.items = MergeSort(
|
|
deque_model_.items,
|
|
[](const std::string& lhs, const std::string& rhs) {
|
|
return lhs < rhs;
|
|
}
|
|
);
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_sOrT_clicked() {
|
|
deque_model_.items = MergeSort(deque_model_.items, CaseInsensitiveLess);
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_shuffle_clicked() {
|
|
std::shuffle(deque_model_.items.begin(), deque_model_.items.end(), m_random_gen_);
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_unique_clicked() {
|
|
if (!std::is_sorted(deque_model_.items.begin(), deque_model_.items.end())) {
|
|
ApplyIterator();
|
|
return;
|
|
}
|
|
|
|
const auto new_end = std::unique(deque_model_.items.begin(), deque_model_.items.end());
|
|
deque_model_.items.erase(new_end, deque_model_.items.end());
|
|
deque_model_.iterator = deque_model_.items.begin();
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_reverse_clicked() {
|
|
std::reverse(deque_model_.items.begin(), deque_model_.items.end());
|
|
ApplyModel();
|
|
}
|
|
|
|
void MainWindow::on_btn_lower_bound_clicked() { if (!std::is_sorted(deque_model_.items.begin(), deque_model_.items.end())) {
|
|
return;
|
|
}
|
|
|
|
deque_model_.iterator = std::lower_bound(
|
|
deque_model_.items.begin(),
|
|
deque_model_.items.end(),
|
|
ui->txt_elem_content->text().toStdString()
|
|
);
|
|
|
|
ApplyIterator();
|
|
}
|
|
|
|
void MainWindow::on_btn_upper_bound_clicked() {
|
|
if (!std::is_sorted(deque_model_.items.begin(), deque_model_.items.end())) {
|
|
return;
|
|
}
|
|
|
|
deque_model_.iterator = std::upper_bound(
|
|
deque_model_.items.begin(),
|
|
deque_model_.items.end(),
|
|
ui->txt_elem_content->text().toStdString()
|
|
);
|
|
|
|
ApplyIterator();
|
|
}
|