Commit 2dad5a14 authored by Philipp Erhardt's avatar Philipp Erhardt

added zoom value and border collision (grid layout)

parent 7589a057
......@@ -49,12 +49,16 @@ int Grid::get_column_count() const {
return column_count;
}
int Grid::get_row_count() const {
return row_count;
}
void Grid::rebuild_cells() {
delete width;
delete height;
// implicit ceil
int row_count = (res->get_page_count() + column_count - 1) / column_count;
row_count = (res->get_page_count() + column_count - 1) / column_count;
width = new float[column_count];
height = new float[row_count];
......
......@@ -14,6 +14,7 @@ public:
float get_width(int page) const;
float get_height(int page) const;
int get_column_count() const;
int get_row_count() const;
private:
void rebuild_cells();
......@@ -21,6 +22,7 @@ private:
ResourceManager *res;
int column_count;
int row_count;
float *width;
float *height;
};
......
......@@ -4,7 +4,7 @@ using namespace std;
// TODO put in a config source file
#define USELESS_GAP 1
#define USELESS_GAP 2
//==[ Layout ]=================================================================
......@@ -16,6 +16,10 @@ int Layout::get_page() {
return page;
}
void Layout::rebuild() {
// does nothing here
}
void Layout::scroll_smooth(int dx, int dy) {
off_x += dx;
off_y += dy;
......@@ -150,20 +154,40 @@ void SequentialLayout::render(QPainter *painter) {
//==[ GridLayout ]=============================================================
GridLayout::GridLayout(ResourceManager *_res, int page, int columns) :
Layout(_res, page) {
grid = new Grid(_res, columns);
horizontal_page = page % grid->get_column_count();
initialize(columns);
}
GridLayout::GridLayout(Layout& old_layout, int columns) :
Layout(old_layout) {
initialize(columns);
}
GridLayout::~GridLayout() {
delete grid;
}
void GridLayout::initialize(int columns) {
grid = new Grid(res, columns);
horizontal_page = page % grid->get_column_count();
zoom = 0.3;
total_height = 0;
for (int i = 0; i < grid->get_row_count(); i++) {
total_height += grid->get_height(i * grid->get_column_count());
}
total_width = 0;
for (int i = 0; i < grid->get_column_count(); i++) {
total_width += grid->get_width(i);
}
}
GridLayout::~GridLayout() {
void GridLayout::rebuild() {
// rebuild non-dynamic data
int columns = grid->get_column_count();
delete grid;
initialize(columns);
}
void GridLayout::scroll_smooth(int dx, int dy) {
......@@ -174,31 +198,79 @@ void GridLayout::scroll_smooth(int dx, int dy) {
page = page / grid->get_column_count() * grid->get_column_count();
// vertical scrolling
while (off_y > 0 && grid->get_height(page - grid->get_column_count()) > 0) {
while (off_y > 0 && grid->get_height(page - grid->get_column_count()) * zoom > 0) {
page -= grid->get_column_count();
off_y -= grid->get_height(page) + USELESS_GAP;
off_y -= grid->get_height(page) * zoom + USELESS_GAP;
}
float h;
while ((h = grid->get_height(page)) > 0 &&
while ((h = grid->get_height(page) * zoom) > 0 &&
off_y <= -h - USELESS_GAP) {
off_y += grid->get_height(page) + USELESS_GAP;
off_y += grid->get_height(page) * zoom + USELESS_GAP;
page += grid->get_column_count();
}
// horizontal scrolling
while (off_x > 0 && grid->get_width(horizontal_page - 1) > 0) {
while (off_x > 0 && grid->get_width(horizontal_page - 1) * zoom > 0) {
horizontal_page--;
off_x -= grid->get_width(horizontal_page) + USELESS_GAP;
off_x -= grid->get_width(horizontal_page) * zoom + USELESS_GAP;
}
float w;
while ((w = grid->get_width(horizontal_page)) > 0 &&
while ((w = grid->get_width(horizontal_page) * zoom) > 0 &&
horizontal_page < grid->get_column_count() - 1 && // TODO not nice
off_x <= -w - USELESS_GAP) {
off_x += grid->get_width(horizontal_page) + USELESS_GAP;
off_x += grid->get_width(horizontal_page) * zoom + USELESS_GAP;
horizontal_page++;
}
// TODO horizontal borders
// vertical borders
// TODO fix rounding errors
int gaps = USELESS_GAP * (grid->get_row_count() - 1);
if (total_height * zoom + gaps < height) {
off_y = (height - total_height * zoom - gaps) / 2;
} else {
if (page == 0 && off_y > 0) {
off_y = 0; // TODO use USELESS_GAP instead?
} else {
float h = off_y;
for (int i = page / grid->get_column_count(); i < grid->get_row_count(); i++) {
h += grid->get_height(i * grid->get_column_count()) * zoom;
if (h >= height) {
break;
}
if (i != grid->get_row_count() - 1) {
h += USELESS_GAP;
}
}
if (h < height) {
off_y += (height - h);
}
}
}
// horizontal borders
// TODO fix rounding errors
gaps = USELESS_GAP * (grid->get_column_count() - 1);
if (total_width * zoom + gaps < width) {
off_x = (width - total_width * zoom - gaps) / 2;
} else {
if (horizontal_page == 0 && off_x > 0) {
off_x = 0; // TODO use USELESS_GAP instead?
} else {
float w = off_x;
for (int i = horizontal_page; i < grid->get_column_count(); i++) {
w += grid->get_width(i) * zoom;
if (w >= width) {
break;
}
if (i != grid->get_column_count() - 1) {
w += USELESS_GAP;
}
}
if (w < width) {
off_x += (width - w);
}
}
}
}
void GridLayout::scroll_page(int new_page, bool relative) {
......@@ -212,24 +284,24 @@ void GridLayout::render(QPainter *painter) {
int cur_page = page;
float h;
int hpos = off_y;
while ((h = grid->get_height(cur_page)) > 0 && hpos < height) {
while ((h = grid->get_height(cur_page) * zoom) > 0 && hpos < height) {
// horizontal
int cur_col = horizontal_page;
float w;
int wpos = off_x;
while ((w = grid->get_width(cur_col)) > 0 &&
while ((w = grid->get_width(cur_col) * zoom) > 0 &&
cur_col < grid->get_column_count() && // TODO not nice
wpos < width) {
int page_width = res->get_page_width(cur_page + cur_col);
int page_width = res->get_page_width(cur_page + cur_col) * zoom;
QImage *img = res->get_page(cur_page + cur_col, page_width);
if (img != NULL) {
painter->drawImage(wpos, hpos, *img);
res->unlock_page(cur_page + cur_col);
}
wpos += grid->get_width(cur_page + cur_col) + USELESS_GAP;
wpos += grid->get_width(cur_page + cur_col) * zoom + USELESS_GAP;
cur_col++;
}
hpos += grid->get_height(cur_page) + USELESS_GAP;
hpos += grid->get_height(cur_page) * zoom + USELESS_GAP;
cur_page += grid->get_column_count();
}
res->collect_garbage(page - 6, cur_page + 6);
......
......@@ -16,6 +16,7 @@ public:
virtual ~Layout() {};
virtual int get_page();
virtual void rebuild();
virtual void scroll_smooth(int dx, int dy);
virtual void scroll_page(int new_page, bool relative = true);
......@@ -52,19 +53,26 @@ public:
class GridLayout : public Layout {
public:
GridLayout(ResourceManager *res, int page = 0, int columns = 2);
GridLayout(Layout& old_layout, int columns = 2);
GridLayout(ResourceManager *res, int page = 0, int columns = 5);
GridLayout(Layout& old_layout, int columns = 5);
~GridLayout();
void rebuild();
void scroll_smooth(int dx, int dy);
void scroll_page(int new_page, bool relative = true);
void render(QPainter *painter);
private:
void initialize(int columns);
Grid *grid;
int horizontal_page;
float zoom;
float total_width;
float total_height;
};
......
......@@ -106,6 +106,7 @@ void Viewer::keyPressEvent(QKeyEvent *event) {
// reload document
case Qt::Key_R:
res->reload_document();
layout->rebuild();
update();
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment