Skip to content

Commit

Permalink
display: Added table content drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Laštovička committed Apr 27, 2022
1 parent 1892f9f commit fbd3917
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 39 deletions.
9 changes: 5 additions & 4 deletions Semestralni_prace/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ CXX = g++

# C++ compiler options
DEBUG = true
ifeq ($(DEBUG),true)
CXXFLAGSDEBUG =
else
ifeq ($(DEBUG), true)
CXXFLAGSDEBUG = -g3 -fsanitize=address
endif
CXXFLAGS = -std=c++17 -pedantic -Wall -Wextra -O3 $(CXXFLAGSDEBUG)

# External libraries used
LIBS = -lncurses
# Linker options
LFLAGS =
ifeq ($(DEBUG), true)
LFLAGSDEBUG = -fsanitize=address
endif
LFLAGS = $(LFLAGSDEBUG)
# My username, used as output filename
USERNAME = lastope2

Expand Down
47 changes: 42 additions & 5 deletions Semestralni_prace/src/cz/lastaapps/vimxel/app.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
#include "app.hpp"
#include "table/cell.hpp"

namespace cz::lastaapps::vimxel {
int App::run(vector<string> args) {
display::Display().draw();
return 0;
}
}
int App::run(vector<string> args) {
initNCurses();
try {
shared_ptr<table::Table> table = loadTable();
display::Display display(table->createCellContract());
display.draw();
} catch(...) {
destroyNCurses();
return 1;
}
destroyNCurses();
return 0;
}

shared_ptr<table::Table> App::loadTable() {
auto table = shared_ptr<table::Table>(new table::Table);
for (int i = 0; i < 42; i++)
for (int j = 0; j < 69; j++)
table->updateCell(Coordinates(i, j), TextCell(to_string(i + 1) + ", " + to_string(j + 1)));
return table;
}

void App::initNCurses() {
initscr();
// Ctr+C gets ignored
// raw();
// Ctr+C works as normal
cbreak();
// pressed characters wouldn't be printed
noecho();
// arrows, F keys and others enabled
keypad(stdscr, TRUE);
// hide cursor
curs_set(false);
}

void App::destroyNCurses() {
endwin();
}

} // namespace cz::lastaapps::vimxel
8 changes: 8 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/app.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef APP_H
#define APP_H
#include <ncurses.h>
#include <vector>
#include <string>
#include "display/display.hpp"
#include "table/table.hpp"

using namespace std;
namespace cz::lastaapps::vimxel {
Expand All @@ -11,6 +13,12 @@ class App final {
public:
int run(vector<string> args);
private:

shared_ptr<table::Table> loadTable();
void initNCurses();
void destroyNCurses();
void setupDisplay();

// Storage engine
// NCurses Init
// Table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,41 @@ void ContentDrawer::init() {
mCntr->registerCallback(
[this](const Coordinates& coord) { renderItem(coord); });
}
void ContentDrawer::selectPos(
const table::Coordinates& pos,
const table::Coordinates& viewPort) {
if (mViewPort != viewPort) {
mPos = pos;
mViewPort = viewPort;
draw();
} else {
Coordinates old = mPos;
mPos = pos;
renderItem(old);
renderItem(mPos);
}
wrefresh(mWin);
}
void ContentDrawer::draw() {
const size_t vX = mViewPort.x();
const size_t vY = mViewPort.y();
for (size_t x = 0; x < cellCount.x(); x++)
for (size_t y = 0; y < cellCount.y(); y++)
renderItem(Coordinates(x + vX, y + vY));
wrefresh(mWin);
}
inline Coordinates ContentDrawer::getCellsCount() const {
Coordinates ContentDrawer::getCellsCount() const {
return Coordinates(mWinSize.x() / mColWidth, mWinSize.y() / mRowHeight);
}
inline void ContentDrawer::renderItem(const Coordinates& coord) {
void ContentDrawer::renderItem(const Coordinates& coord) {
const Coordinates point = getRealPoint(coord);
const string content = mCntr -> getDataAt(coord);
const bool selected = isSelected(coord);
if (selected) wattron(mWin, A_STANDOUT);
mvwprintw(mWin, point.y(), point.x(), "%.*s", (int)mColWidth, content.c_str());
mvwprintw(mWin, point.y(), point.x(), "%*.*s", (int)mColWidth, (int)mColWidth, content.c_str());
wattroff(mWin, A_STANDOUT);
}
inline bool ContentDrawer::isSelected(const Coordinates& coord) {
bool ContentDrawer::isSelected(const Coordinates& coord) {
return mPos.x() == coord.x() && mPos.y() == coord.y();
}
Coordinates ContentDrawer::getRealPoint(const Coordinates& coord) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace cz::lastaapps::vimxel::display {
class ContentDrawer final : public Drawer {
const size_t mColWidth, mRowHeight;
const shared_ptr<CellContract> mCntr;
Coordinates cellCount = getCellsCount();
const Coordinates cellCount = getCellsCount();

public:
ContentDrawer(WINDOW * w, const size_t colW, const size_t rowH, shared_ptr<CellContract> cnt);
Expand All @@ -23,12 +23,15 @@ class ContentDrawer final : public Drawer {
table::Coordinates pos, table::Coordinates viewPort,
shared_ptr<CellContract> cnt);
~ContentDrawer();
void selectPos(
const table::Coordinates& pos,
const table::Coordinates& viewPort) override;
void draw() override;

private:
void init();
inline Coordinates getCellsCount() const;
inline void renderItem(const Coordinates &coord);
Coordinates getCellsCount() const;
void renderItem(const Coordinates &coord);
inline bool isSelected(const Coordinates &coord);
Coordinates getRealPoint(const Coordinates& coord);
};
Expand Down
27 changes: 6 additions & 21 deletions Semestralni_prace/src/cz/lastaapps/vimxel/display/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,22 @@ using namespace std;
using namespace cz::lastaapps::vimxel;
namespace cz::lastaapps::vimxel::display {

Display::Display() {
initscr();
// Ctr+C gets ignored
// raw();
// Ctr+C works as normal
cbreak();
// pressed characters wouldn't be printed
noecho();
// arrows, F keys and others enabled
keypad(stdscr, TRUE);
// hide cursor
curs_set(false);

Display::Display(shared_ptr<CellContract> contract)
: mContract(contract) {
// initial refresh
refresh();
updateDisplayConfig();
}

Display::~Display() {
delWindows();
endwin();
}

void Display::delWindows() {
delete posDrawer;
delete rowDrawer;
delete colDrawer;
delete contentDrawer;
delwin(colNamesWin);
delwin(rowNamesWin);
delwin(vimWin);
Expand Down Expand Up @@ -65,14 +54,14 @@ void Display::updateDisplayConfig() {
posDrawer = new PosDrawer(posWin, mPos, mViewPort);
rowDrawer = new RowDrawer(rowNamesWin, mPos, mViewPort);
colDrawer = new ColDrawer(colNamesWin, cellWidth, mPos, mViewPort);
contentDrawer = new ContentDrawer(contentWin, cellWidth, cellHeight, mPos, mViewPort, mContract);
}
void Display::draw() {
const table::Coordinates scrSize = getTerminalSize();

drawSeparatingLines();
posDrawer->draw();
rowDrawer->draw();
colDrawer->draw();
contentDrawer->draw();

while (true) {
int ch = getch();
Expand Down Expand Up @@ -105,11 +94,6 @@ void Display::draw() {
break;
}
}

delwin(colNamesWin);
delwin(rowNamesWin);
delwin(vimWin);
delwin(contentWin);
}
void Display::setPosition(const table::Coordinates& coord) {
mPos = coord;
Expand Down Expand Up @@ -166,5 +150,6 @@ void Display::refreshWindows() {
posDrawer->selectPos(mPos, mViewPort);
rowDrawer->selectPos(mPos, mViewPort);
colDrawer->selectPos(mPos, mViewPort);
contentDrawer->selectPos(mPos, mViewPort);
}
} // namespace cz::lastaapps::vimxel::display
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <memory>

#include "../table/coordinate.hpp"
#include "../table/cellContract.hpp"
#include "colDrawer.hpp"
#include "posDrawer.hpp"
#include "rowDrawer.hpp"
#include "contentDrawer.hpp"
using namespace std;
using namespace cz::lastaapps::vimxel;
namespace cz::lastaapps::vimxel::display {
Expand All @@ -19,10 +21,12 @@ class Display final {
PosDrawer* posDrawer = nullptr;
RowDrawer* rowDrawer = nullptr;
ColDrawer* colDrawer = nullptr;
ContentDrawer* contentDrawer = nullptr;
table::Coordinates mPos, mViewPort;
shared_ptr<CellContract> mContract;

public:
Display();
Display(shared_ptr<CellContract> contract);
~Display();
void draw();
void setPosition(const table::Coordinates& corrd);
Expand Down
6 changes: 6 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ TextCell::TextCell(string content) : mContent(move(content)) {}
const string& TextCell::getContent() const {
return mContent;
}
Cell* TextCell::clone() const {
return new TextCell(*this);
}

// --- EmptyCell --------------------------------------------------------------
const string& EmptyCell::getContent() const { return emptyString; }
Cell* EmptyCell::clone() const {
return new EmptyCell(*this);
}

} // namespace cz::lastaapps::vimxel::table
3 changes: 3 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Cell {
public:
virtual ~Cell() {}
virtual const string& getContent() const = 0;
virtual Cell* clone() const = 0;
};

class TextCell : public Cell {
Expand All @@ -18,6 +19,7 @@ class TextCell : public Cell {
TextCell(string);
~TextCell() = default;
const string& getContent() const override;
virtual Cell* clone() const override;
};

class EmptyCell : public Cell {
Expand All @@ -26,6 +28,7 @@ class EmptyCell : public Cell {
public:
~EmptyCell() = default;
const string& getContent() const override;
virtual Cell* clone() const override;
};

} // namespace cz::lastaapps::vimexel::table
Expand Down
2 changes: 1 addition & 1 deletion Semestralni_prace/src/cz/lastaapps/vimxel/table/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void Table::updateCell(const Coordinates& coord, const Cell& cell) {
mMap.erase(coord);
else
// insert/update
mMap.insert_or_assign(coord, unique_ptr<const Cell>(&cell));
mMap.insert_or_assign(coord, unique_ptr<const Cell>(cell.clone()));

updateContracts(coord);
}
Expand Down
1 change: 1 addition & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Table final {
vector<shared_ptr<CellContract>> mContracts;

public:
Table() = default;
Table(const Table& other) = delete;
Table(const Table&& other) = delete;
Table& operator=(const Table& other) = delete;
Expand Down

0 comments on commit fbd3917

Please sign in to comment.