AudioBook Plugin: Move selection up/down

This commit is contained in:
Jakub Melka
2021-08-26 21:09:26 +02:00
parent 7525265233
commit 3318a2a6d7
6 changed files with 123 additions and 0 deletions

View File

@@ -905,6 +905,66 @@ void PDFDocumentTextFlowEditor::restoreOriginalTexts()
}
}
void PDFDocumentTextFlowEditor::moveSelectionUp()
{
size_t originalSize = m_editedTextFlow.size();
size_t firstSelectedIndex = originalSize;
EditedItems selectedItems;
for (auto it = m_editedTextFlow.begin(); it != m_editedTextFlow.end();)
{
if (it->editedItemFlags.testFlag(Selected))
{
if (firstSelectedIndex == originalSize)
{
firstSelectedIndex = std::distance(m_editedTextFlow.begin(), it);
}
selectedItems.emplace_back(std::move(*it));
it = m_editedTextFlow.erase(it);
}
else
{
++it;
}
}
if (firstSelectedIndex > 0)
{
--firstSelectedIndex;
}
m_editedTextFlow.insert(std::next(m_editedTextFlow.begin(), firstSelectedIndex), std::make_move_iterator(selectedItems.begin()), std::make_move_iterator(selectedItems.end()));
}
void PDFDocumentTextFlowEditor::moveSelectionDown()
{
size_t originalSize = m_editedTextFlow.size();
size_t lastSelectedIndex = originalSize;
EditedItems selectedItems;
for (auto it = m_editedTextFlow.begin(); it != m_editedTextFlow.end();)
{
if (it->editedItemFlags.testFlag(Selected))
{
lastSelectedIndex = std::distance(m_editedTextFlow.begin(), it);
selectedItems.emplace_back(std::move(*it));
it = m_editedTextFlow.erase(it);
}
else
{
++it;
}
}
if (lastSelectedIndex < m_editedTextFlow.size())
{
++lastSelectedIndex;
}
m_editedTextFlow.insert(std::next(m_editedTextFlow.begin(), lastSelectedIndex), std::make_move_iterator(selectedItems.begin()), std::make_move_iterator(selectedItems.end()));
}
PDFDocumentTextFlowEditor::PageIndicesMappingRange PDFDocumentTextFlowEditor::getItemsForPageIndex(PDFInteger pageIndex) const
{
auto comparator = [](const auto& l, const auto& r)

View File

@@ -244,6 +244,16 @@ public:
/// Restores original texts in selected items
void restoreOriginalTexts();
/// Move all selected items one position up. If multiple non-consecutive
/// items are selected, they are grouped into one group and move one item
/// up above first selected item.
void moveSelectionUp();
/// Move all selected items one position down. If multiple non-consecutive
/// items are selected, they are grouped into one group and move one item
/// down above first selected item.
void moveSelectionDown();
/// Returns item indices for a given page index, i.e.
/// index of items which are lying on a page.
/// \param pageIndex Page index

View File

@@ -309,6 +309,28 @@ void PDFDocumentTextFlowEditorModel::restoreOriginalTexts()
emit dataChanged(index(0, 0), index(rowCount(QModelIndex()) - 1, ColumnLast));
}
void PDFDocumentTextFlowEditorModel::moveSelectionUp()
{
if (!m_editor || m_editor->isEmpty())
{
return;
}
m_editor->moveSelectionUp();
notifyDataChanged();
}
void PDFDocumentTextFlowEditorModel::moveSelectionDown()
{
if (!m_editor || m_editor->isEmpty())
{
return;
}
m_editor->moveSelectionDown();
notifyDataChanged();
}
void PDFDocumentTextFlowEditorModel::notifyDataChanged()
{
if (!m_editor || m_editor->isEmpty())

View File

@@ -69,6 +69,8 @@ public:
void selectByRegularExpression(const QRegularExpression& expression);
void selectByPageIndices(const pdf::PDFClosedIntervalSet& indices);
void restoreOriginalTexts();
void moveSelectionUp();
void moveSelectionDown();
void notifyDataChanged();
private: