#include "grid_model.h" namespace roro { void GridModel::setCount(int count) { count_ = count < 0 ? 0 : count; if (count_ == 0) { selected_ = -1; first_ = 0; return; } select(selected_ < 0 ? 0 : selected_); } void GridModel::select(int index) { if (count_ == 0) return; selected_ = index < 0 ? 0 : index >= count_ ? count_ - 1 : index; int row = selected_ / cols_; if (row < first_) first_ = row; if (row >= first_ + rows_) first_ = row - rows_ + 1; int maxFirst = rows() > rows_ ? rows() - rows_ : 0; if (first_ > maxFirst) first_ = maxFirst; } void GridModel::left() { if (count_) select(selected_ == 0 ? count_ - 1 : selected_ - 1); } void GridModel::right() { if (count_) select(selected_ == count_ - 1 ? 0 : selected_ + 1); } void GridModel::up() { if (!count_) return; int col = selected_ % cols_, row = selected_ / cols_; row = row == 0 ? rows() - 1 : row - 1; select(row * cols_ + col); // past the end of a short last row: the last item } void GridModel::down() { if (!count_) return; int col = selected_ % cols_, row = selected_ / cols_; row = row == rows() - 1 ? 0 : row + 1; select(row * cols_ + col); } } // namespace roro