Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ compile_commands.json
# QtCreator local machine specific files for imported projects
*creator.user*

*_qmlcache.qrc
*_qmlcache.qrc.DS_Store
.idea
.DS_Store
8 changes: 8 additions & 0 deletions Application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "Application.h"

namespace mvc {
Application::Application() {
view_.subscribe(controller_.input());
model_.subscribe(view_.input());
}
}// namespace mvc
18 changes: 18 additions & 0 deletions Application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef COURSEPROJECT_APPLICATION_H
#define COURSEPROJECT_APPLICATION_H

#include "Controller.h"

namespace mvc {
class Application {
public:
Application();

private:
AVLTree model_;
View view_;
Controller controller_{&model_};
};
}// namespace mvc

#endif//COURSEPROJECT_APPLICATION_H
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.24)
project(CourseProject)

set(CMAKE_CXX_STANDARD 20)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "/opt/homebrew/opt/qt/lib/cmake")

find_package(Qt6 COMPONENTS
Core
Gui
Widgets
REQUIRED)

add_executable(CourseProject main.cpp mainwindow.cpp mainwindow.ui Model.cpp View.cpp Controller.cpp InfoTree.cpp aboutwindow.cpp Application.cpp)
target_link_libraries(CourseProject
Qt::Core
Qt::Gui
Qt::Widgets
)

29 changes: 29 additions & 0 deletions Controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Controller.h"

namespace mvc {
Controller::Controller(AVLTree *model)
: model_(model) {
}

void Controller::action(const ViewData &data) {
switch (data.operation) {
case View::Operation::Add: {
model_->insert(data.value);
break;
}
case View::Operation::Delete: {
model_->deleteNode(data.value);
break;
}
case View::Operation::Search: {
model_->search(data.value);
break;
}
case View::Operation::DeleteAll: {
model_->deleteAll();
}
default:
break;
}
}
}// namespace mvc
28 changes: 28 additions & 0 deletions Controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef COURSEPROJECT_CONTROLLER_H
#define COURSEPROJECT_CONTROLLER_H

#include <memory>

#include "Model.h"
#include "View.h"

namespace mvc {
class Controller {
private:
using ViewData = View::Command;
using Observer = NSLibrary::CObserver<ViewData>;
using Input = NSLibrary::CColdInput<ViewData>;

public:
Controller(AVLTree *ptr);
Observer *input() { return &observer_; }

private:
void action(const ViewData &data);

AVLTree *model_;
Input observer_ = [this](const ViewData &data) { action(data); };
};
}// namespace mvc

#endif//COURSEPROJECT_CONTROLLER_H
131 changes: 131 additions & 0 deletions InfoTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "InfoTree.h"

namespace mvc {
InfoTree::~InfoTree() {
clear(root_);
}

std::pair<int, bool> InfoTree::findValue(int x, int y) {
if (this == nullptr)
return {0, false};
std::queue<Info *> que;
que.push(root_);
Info *cur;
while (!que.empty()) {
cur = que.front();
que.pop();
if ((x > cur->x - kRadius_ && x < cur->x + kRadius_) && (y > cur->y - kRadius_ && y < cur->y + kRadius_))
return {cur->key, true};
if (cur->left != nullptr)
que.push(cur->left);
if (cur->right != nullptr)
que.push(cur->right);
}
return {0, false};
}

void InfoTree::clear(Info *treeInfo) {
if (treeInfo != nullptr) {
clear(treeInfo->left);
clear(treeInfo->right);
delete treeInfo;
}
}

void InfoTree::setWidth(Info *cur) {
if (cur == nullptr)
return;
setWidth(cur->left);
setWidth(cur->right);

if (cur->right == nullptr && cur->left == nullptr)
cur->width = 2 * kRadius_;
else if (cur->left == nullptr)
cur->width = 2 * cur->right->width + kWidth_;
else if (cur->right == nullptr)
cur->width = 2 * cur->left->width + kWidth_;
else
cur->width = 2 * std::max(cur->left->width, cur->right->width) + kWidth_;
}

void InfoTree::setXCoord(Info *cur, std::queue<Info *> &que, int &count, int &timesNext, int &timesNow) {
if (cur->left != nullptr) {
que.push(cur->left);
++timesNext;
cur->left->x = cur->x - kWidth_ / 2 - cur->left->width / 2;
}
if (cur->right != nullptr) {
que.push(cur->right);
++timesNext;
cur->right->x = cur->x + kWidth_ / 2 + cur->right->width / 2;
}
--timesNow;
if (timesNow == 0) {
++count;
timesNow = timesNext;
timesNext = 0;
}
}

void InfoTree::calcXCoord() {
Info *coord = root_;
setWidth(coord);
Info *root = root_;
if (root == nullptr)
return;
std::queue<Info *> que;
que.push(root);
Info *cur;
int count = 0, timesNow = 1, timesNext = 0;
while (!que.empty()) {
cur = que.front();
que.pop();
setXCoord(cur, que, count, timesNext, timesNow);
}
}

Info *InfoTree::buildInfoTree(const Node *node) {
if (node == nullptr)
return nullptr;
else {
Info *temp = new Info;
temp->key = node->key;
temp->left = buildInfoTree(node->leftCh);
temp->right = buildInfoTree(node->rightCh);
return temp;
}
}

void InfoTree::setYCoord(Info *cur, std::queue<Info *> &que, int &count, int &timesNext, int &timesNow) {
cur->y = count * (2 * kRadius_ + kHeight_);
if (cur->left != nullptr) {
que.push(cur->left);
++timesNext;
}
if (cur->right != nullptr) {
que.push(cur->right);
++timesNext;
}
--timesNow;
if (timesNow == 0) {
++count;
timesNow = timesNext;
timesNext = 0;
}
}

void InfoTree::calcYCoord() {
Info *root = root_;
if (root == nullptr)
return;
std::queue<Info *> que;
que.push(root);
Comment on lines +121 to +122
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это ты от рекурсии избавлялась?

Info *cur;
int count = 0, timesNow = 1, timesNext = 0;
while (!que.empty()) {
cur = que.front();
que.pop();
setYCoord(cur, que, count, timesNext, timesNow);
}
}
}// namespace mvc
49 changes: 49 additions & 0 deletions InfoTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef COURSEPROJECT_INFOTREE_H
#define COURSEPROJECT_INFOTREE_H

#include <queue>

#include "Model.h"

namespace mvc {

struct Info {
int x = 0;
int y = 0;
int key;
Info *left;
Info *right;
int width;
};

class InfoTree {
public:
InfoTree(const Node *rootGet)
: root_(buildInfoTree(rootGet)) {
calcYCoord();
calcXCoord();
}
~InfoTree();
Info *getRoot() { return root_; }
std::pair<int, bool> findValue(int x, int y);
void clear(Info *treeInfo);

private:
static constexpr int kRadius_ = 40;
static constexpr int kHeight_ = 30;
static constexpr int kWidth_ = 15;

private:
void setWidth(Info *cur);
void setXCoord(Info *cur, std::queue<Info *> &que, int &count, int &timesNext, int &timesNow);
void calcXCoord();
Info *buildInfoTree(const Node *node);
void setYCoord(Info *cur, std::queue<Info *> &que, int &count, int &timesNext, int &timesNow);
void calcYCoord();

Info *root_ = nullptr;
};

}// namespace mvc

#endif//COURSEPROJECT_INFOTREE_H
Loading