{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Selections\n",
    "DataGrid cells can be selected using mouse by simply clicking and dragging over the cells. Pressing Cmd / Ctrl key during selection will add to existing selections. Pressing Shift key allows selection of regions between two clicks.\n",
    "\n",
    "DataGrid supports three modes of selection 'cell', 'row', 'column'. In order to disable selections, selection mode can be set to 'none' which is the default setting.\n",
    "#### Selection Modes\n",
    "- **'cell'**: Clicking on grid will select only the cell under mouse cursor\n",
    "- **'row'**: Clicking on grid will select all the cells on the row under mouse cursor\n",
    "- **'column'**: Clicking on grid will select all the cells on the column under mouse cursor\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "from ipydatagrid import DataGrid\n",
    "from json import load\n",
    "import pandas as pd\n",
    "\n",
    "with open(\"./cars.json\") as fobj:\n",
    "    data = load(fobj)\n",
    "\n",
    "df = pd.DataFrame(data[\"data\"]).drop(\"index\", axis=1)\n",
    "\n",
    "datagrid = DataGrid(df, selection_mode=\"cell\")\n",
    "datagrid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Select a rectangular region programmatically. 'row1' & 'column1' correspond to top-left corner of selected region, and 'row2'& 'column2' correspond to bottom-right corner of selected region. Row and column values start from 0 and from top-left corner of **grid body**. clear_mode specifies how new selection should be applied in the presence of other selections. possible values are `'none'`: to keep previous selections (default), `'all'`: to clear previous selections, `'current'`: to remove only the last selection pre-existing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.select(row1=1, column1=1, row2=2, column2=2, clear_mode=\"all\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Clear existing selections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.clear_selection()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.select(1, 1, 2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.select(2, 2, 3, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Select individual cells by using only row1 and column1 parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.select(4, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.select(6, 1, clear_mode=\"current\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Selections are stored as rectangular regions internally and can be accessed using:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.selections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Access cells selected. Cells in overlapped regions are listed once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.selected_cells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Cell values for the selected cells can also be accessed with single method call"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.selected_cell_values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For large DataGrids and large selection regions, it is more optimal to use iterator and access selected cells one by one. **selected_cell_iterator** can be used to loop through selected cells without need to store them into a list in advance internally."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for cell in datagrid.selected_cell_iterator:\n",
    "    print(cell)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Iterator also provides the total selection count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(datagrid.selected_cell_iterator)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DataGrid selections can also be modified with a single assignment statement by passing in selection regions as a list of rectangles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagrid.selections = [\n",
    "    {\"r1\": 22, \"r2\": 20, \"c1\": 0, \"c2\": 2},\n",
    "    {\"r1\": 6, \"r2\": 6, \"c1\": 2, \"c2\": 2},\n",
    "    {\"r1\": 10, \"r2\": 10, \"c1\": 3, \"c2\": 3},\n",
    "    {\"r1\": 13, \"r2\": 13, \"c1\": 2, \"c2\": 2},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}