def scroll(self, direction: argtypes.Direction, count=1): """Scroll the library in the given direction. **syntax:** ``:scroll direction`` The behaviour is similar to the file manager ranger. * Scrolling left selects the current file. * Scrolling right selects the parent directory. * Scrolling up and down moves the cursor. positional arguments: * ``direction``: The direction to scroll in (left/right/up/down). **count:** multiplier """ if direction == direction.Right: self.open_selected() elif direction == direction.Left: self.store_position() working_directory.handler.chdir(os.pardir) else: try: row = self.row() # Directory is empty except IndexError: raise api.commands.CommandWarning("Directory is empty") if direction == direction.Up: row -= count else: row += count self._select_row(clamp(row, 0, self.model().rowCount() - 1))
def _scale_to_float(self, level: float) -> None: """Scale image to a defined size. Args: level: Size to scale to. 1 is the original image size. """ level = utils.clamp(level, self.MIN_SCALE, self.MAX_SCALE) super().scale(level / self.zoom_level, level / self.zoom_level)
def _scale_to_float(self, level: float) -> None: """Scale image to a defined size. Args: level: Size to scale to. 1 is the original image size. """ level = utils.clamp(level, self.MIN_SCALE, self.MAX_SCALE) factor = level / self.zoom_level super().scale(factor, factor) if factor < 1: self._update_focalpoint()
def scroll( self, direction: argtypes.DirectionWithPage, open_selected: bool = False, count=1, ): """Scroll the library in the given direction. **syntax:** ``:scroll direction`` The behaviour is similar to the file manager ranger. * Scrolling right selects the current file. * Scrolling left selects the parent directory. * Scrolling up and down moves the cursor. positional arguments: * ``direction``: The direction to scroll in (left/right/up/down/page-up/page-down/half-page-up/half-page-down). optional arguments: * ``--open-selected``: Automatically open any selected image. **count:** multiplier """ _logger.debug("Scrolling in direction '%s'", direction) if direction == direction.Right: current = self.current() # Close library on double selection self._open_path(current, close=current == self._last_selected) elif direction == direction.Left: self.store_position() parent = os.path.abspath(os.pardir) self._positions[parent] = Position(os.getcwd()) api.working_directory.handler.chdir(parent) else: row = self.row() if row == -1: # Directory is empty raise api.commands.CommandWarning("Directory is empty") if direction.is_page_step: n_items = self._n_visible_items() factor = 0.5 if direction.is_half_page_step else 1 stepsize = math.ceil(n_items * factor) count *= stepsize if direction.is_reverse: count *= -1 _logger.debug("Scrolling %d rows", count) row += count self._select_row(clamp(row, 0, self.model().rowCount() - 1), open_selected)
def _select_index(self, index: int, emit: bool = True) -> None: """Select specific item in the ListWidget. Args: index: Number of the current item to select. emit: Emit the new_thumbnail_path_selected signal. """ if not self._paths: raise api.commands.CommandWarning("Thumbnail list is empty") _logger.debug("Selecting thumbnail number %d", index) index = utils.clamp(index, 0, self.count() - 1) self.setCurrentRow(index) if emit: synchronize.signals.new_thumbnail_path_selected.emit(self._paths[index])
def zoom(self, direction: argtypes.Zoom): """Zoom the current widget. **syntax:** ``:zoom direction`` positional arguments: * ``direction``: The direction to zoom in (in/out). **count:** multiplier """ _logger.debug("Zooming in direction '%s'", direction) size = self.iconSize().width() size = size // 2 if direction == direction.Out else size * 2 size = clamp(size, 64, 512) api.settings.thumbnail.size.value = size
def _update_manipulation(self, name, value): """Update the value of one manipulation. Args: name: Name of the manipulation to update. value: Integer value to set the manipulation to. """ self._current = name self.focused.emit(name) if value is not None: value = clamp(value, -127, 127) self.edited.emit(name, value) self.manipulations[name] = value self.thread_id += 1 runnable = ManipulateRunner(self, self.thread_id) self.pool.start(runnable)
def goto(self, row: int, count: Optional[int] = None): """Select specific row in current filelist. **syntax:** ``:goto row`` positional arguments: * ``row``: Number of the row to select. .. hint:: -1 is the last row. **count:** Select [count]th element instead. """ if row == -1: row = self.model().rowCount() row = count if count is not None else row # Prefer count if row > 0: row -= 1 # Start indexing at 1 row = clamp(row, 0, self.model().rowCount() - 1) self._select_row(row)
def scroll(self, direction: argtypes.Direction, open_selected: bool = False, count=1): """Scroll the library in the given direction. **syntax:** ``:scroll direction`` The behaviour is similar to the file manager ranger. * Scrolling right selects the current file. * Scrolling left selects the parent directory. * Scrolling up and down moves the cursor. positional arguments: * ``direction``: The direction to scroll in (left/right/up/down). optional arguments: * ``--open-selected``: Automatically open any selected image. **count:** multiplier """ if direction == direction.Right: current = self.current() # Close library on double selection self._open_path(current, close=current == self._last_selected) elif direction == direction.Left: self.store_position() parent = os.path.abspath(os.pardir) self._positions[parent] = os.getcwd() api.working_directory.handler.chdir(parent) else: try: row = self.row() # Directory is empty except IndexError: raise api.commands.CommandWarning("Directory is empty") if direction == direction.Up: row -= count else: row += count self._select_row(clamp(row, 0, self.model().rowCount() - 1), open_selected)
def test_clamp_with_none(): assert utils.clamp(2, None, None) == 2
def test_clamp_with_min(): assert utils.clamp(2, 0, None) == 2 assert utils.clamp(2, 3, None) == 3
def test_clamp_with_max(): assert utils.clamp(2, None, 5) == 2 assert utils.clamp(2, None, 1) == 1
def test_clamp_with_min_and_max(): assert utils.clamp(2, 0, 5) == 2 assert utils.clamp(2, 3, 5) == 3 assert utils.clamp(2, 0, 1) == 1
def hook(self, value: NumberStr) -> Number: return clamp(self.convert(value), self.min_value, self.max_value)
def step(self, up: bool = True) -> None: """Change thumbnail size by one step up if up else down.""" index = self.ALLOWED_VALUES.index(self.value) + (1 if up else -1) index = clamp(index, 0, len(self.ALLOWED_VALUES) - 1) self.value = self.ALLOWED_VALUES[index]
def convert(self, value: customtypes.NumberStr) -> customtypes.Number: return clamp(super().convert(value), self.min_value, self.max_value)