2023-03-03 17:18:56 +01:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
|
|
#include "celliterator.h"
|
|
|
|
|
|
|
|
|
|
#include "terminalsurface.h"
|
|
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
|
|
namespace Terminal::Internal {
|
|
|
|
|
|
|
|
|
|
CellIterator::CellIterator(const TerminalSurface *surface, QPoint pos)
|
2023-05-09 07:37:30 +02:00
|
|
|
: CellIterator(surface, pos.x() + (pos.y() * surface->liveSize().width()))
|
|
|
|
|
{}
|
2023-03-03 17:18:56 +01:00
|
|
|
|
|
|
|
|
CellIterator::CellIterator(const TerminalSurface *surface, int pos)
|
|
|
|
|
: m_state(State::INSIDE)
|
|
|
|
|
, m_surface(surface)
|
|
|
|
|
{
|
|
|
|
|
m_maxpos = surface->fullSize().width() * (surface->fullSize().height()) - 1;
|
2023-04-06 11:26:05 +02:00
|
|
|
m_pos = qMax(0, qMin(m_maxpos + 1, pos));
|
2023-05-09 07:37:30 +02:00
|
|
|
|
|
|
|
|
if (m_pos == 0)
|
2023-03-09 22:30:47 +01:00
|
|
|
m_state = State::BEGIN;
|
2023-05-09 07:37:30 +02:00
|
|
|
else if (m_pos == m_maxpos + 1)
|
2023-03-09 22:30:47 +01:00
|
|
|
m_state = State::END;
|
2023-05-09 07:37:30 +02:00
|
|
|
|
2023-03-03 17:18:56 +01:00
|
|
|
updateChar();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 07:37:30 +02:00
|
|
|
CellIterator::CellIterator(const TerminalSurface *surface)
|
|
|
|
|
: m_state(State::END)
|
2023-03-03 17:18:56 +01:00
|
|
|
, m_surface(surface)
|
|
|
|
|
{
|
|
|
|
|
m_maxpos = surface->fullSize().width() * (surface->fullSize().height()) - 1;
|
2023-05-09 07:37:30 +02:00
|
|
|
m_pos = m_maxpos + 1;
|
2023-03-03 17:18:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QPoint CellIterator::gridPos() const
|
|
|
|
|
{
|
|
|
|
|
return m_surface->posToGrid(m_pos);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 12:53:21 +01:00
|
|
|
bool CellIterator::updateChar()
|
2023-03-03 17:18:56 +01:00
|
|
|
{
|
|
|
|
|
QPoint cell = m_surface->posToGrid(m_pos);
|
|
|
|
|
m_char = m_surface->fetchCharAt(cell.x(), cell.y());
|
2023-03-24 12:53:21 +01:00
|
|
|
return m_char != 0;
|
2023-03-03 17:18:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CellIterator &CellIterator::operator-=(int n)
|
|
|
|
|
{
|
|
|
|
|
if (n == 0)
|
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
|
|
if (m_pos - n < 0)
|
|
|
|
|
throw new std::runtime_error("-= n too big!");
|
|
|
|
|
|
|
|
|
|
m_pos -= n;
|
2023-03-24 12:53:21 +01:00
|
|
|
|
|
|
|
|
while (!updateChar() && m_pos > 0 && m_skipZeros)
|
|
|
|
|
m_pos--;
|
2023-03-03 17:18:56 +01:00
|
|
|
|
|
|
|
|
m_state = State::INSIDE;
|
|
|
|
|
|
|
|
|
|
if (m_pos == 0) {
|
|
|
|
|
m_state = State::BEGIN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CellIterator &CellIterator::operator+=(int n)
|
|
|
|
|
{
|
|
|
|
|
if (n == 0)
|
|
|
|
|
return *this;
|
|
|
|
|
|
2023-03-24 12:53:21 +01:00
|
|
|
if (m_pos + n < m_maxpos + 1) {
|
2023-03-03 17:18:56 +01:00
|
|
|
m_state = State::INSIDE;
|
|
|
|
|
m_pos += n;
|
2023-03-24 12:53:21 +01:00
|
|
|
while (!updateChar() && m_pos < (m_maxpos + 1) && m_skipZeros)
|
|
|
|
|
m_pos++;
|
|
|
|
|
|
|
|
|
|
if (m_pos == m_maxpos + 1)
|
|
|
|
|
m_state = State::END;
|
2023-03-03 17:18:56 +01:00
|
|
|
} else {
|
|
|
|
|
*this = m_surface->end();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Terminal::Internal
|