8000 Simplify layout get view at by JJones780 · Pull Request #1443 · xournalpp/xournalpp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Simplify layout get view at #1443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2019
Merged
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
46 changes: 9 additions & 37 deletions src/gui/Layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ void Layout::layoutPages(int width, int height)
auto const rows = this->heightRows.size();
auto const columns = this->widthCols.size();

this->lastGetViewAtRow = rows / 2; //reset to middle
this->lastGetViewAtCol = columns / 2;

//add space around the entire page area to accomodate older Wacom tablets with limited sense area.
int64_t const v_padding =
Expand Down Expand Up @@ -340,49 +338,23 @@ XojPageView* Layout::getViewAt(int x, int y)

XOJ_CHECK_TYPE(Layout);

// No need to check page cache as the Linear search below starts at cached position.
// Keep Binary search handy to check against.
//
// //Binary Search ... too much overhead makes this a slower option in our use case.
// auto rit = std::lower_bound( this->sizeRow.begin(), this->sizeRow.end(), y);
// int rb = rit - this->sizeRow.begin(); //get index
// auto cit = std::lower_bound( this->sizeCol.begin(), this->sizeCol.end(), x);
// int cb = cit - this->sizeCol.begin();

//Binary Search:
auto rit = std::lower_bound(this->heightRows.begin(), this->heightRows.end(), y);
int const foundRow = std::distance(this->heightRows.begin(), rit);
auto cit = std::lower_bound(this->widthCols.begin(), this->widthCols.end(), x);
int const foundCol = std::distance(this->widthCols.begin(), cit);

auto fast_linear_search = [](std::vector<unsigned> const& container, size_t start, size_t value) {
auto row_i = std::next(begin(container), start);
if (row_i != begin(container) && value < *row_i)
{
//Todo: replace with c++14
//Todo: rend(this->heightRows)
//Todo: std::make_reverse_iterator(c_i)
auto rbeg_i = std::reverse_iterator<decltype(row_i)>(row_i);
return std::find_if(rbeg_i, container.rend(), [value](int item) { return value > item; }).base();
}
return std::find_if(row_i, end(container), [value](int item) { return value <= item; });
};

auto const& row_i = fast_linear_search(this->heightRows, this->lastGetViewAtRow, y);
auto const& col_i = fast_linear_search(this->widthCols, this->lastGetViewAtCol, x);

auto optionalPage = this->mapper.at({foundCol, foundRow});

if (col_i != end(this->widthCols) && row_i != end(this->heightRows))
if (optionalPage && this->view->viewPages[*optionalPage]->containsPoint(x, y, false))
{
//Todo c++14+ cbegin(...);
this->lastGetViewAtRow = std::distance(this->heightRows.cbegin(), row_i);
this->lastGetViewAtCol = std::distance(this->widthCols.cbegin(), col_i);
auto optionalPage = this->mapper.at({this->lastGetViewAtCol, this->lastGetViewAtRow});

if (optionalPage && this->view->viewPages[*optionalPage]->containsPoint(x, y, false))
{
return this->view->viewPages[*optionalPage];
}

return this->view->viewPages[*optionalPage];
}

return nullptr;
}

// Todo replace with boost::optional<size_t> Layout::getIndexAtGridMap(size_t row, size_t col)
// or std::optional<size_t> Layout::getIndexAtGridMap(size_t row, size_t col)
LayoutMapper::optional_size_t Layout::getIndexAtGridMap(size_t row, size_t col)
Expand Down
6 changes: 0 additions & 6 deletions src/gui/Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ class Layout
size_t minWidth = 0;
size_t minHeight = 0;

/**
* cache the last GetViewAt() row and column.
*/
size_t lastGetViewAtRow = 0;
size_t lastGetViewAtCol = 0;

/**
* layoutPages invalidates the precalculation of recalculate
* this bool prevents that layotPages can be called without a previously call to recalculate
Expand Down
0