cleanups: Sprinkle in a bit unique_ptr

Document memory ownership with unique_ptr's and do a bit of code
cleanups.
This commit is contained in:
Sune Vuorela 2024-03-04 13:22:25 +01:00 committed by Albert Astals Cid
parent 826715100b
commit 56a2a8d3bd
9 changed files with 47 additions and 55 deletions

View file

@ -3624,7 +3624,7 @@ bool DocumentPrivate::canRemoveExternalAnnotations() const
return false;
}
void Document::setPageTextSelection(int page, RegularAreaRect *rect, const QColor &color)
void Document::setPageTextSelection(int page, std::unique_ptr<RegularAreaRect> &&rect, const QColor &color)
{
Page *kp = d->m_pagesVector[page];
if (!d->m_generator || !kp) {
@ -3633,7 +3633,7 @@ void Document::setPageTextSelection(int page, RegularAreaRect *rect, const QColo
// add or remove the selection basing whether rect is null or not
if (rect) {
kp->d->setTextSelections(rect, color);
kp->d->setTextSelections(*rect, color);
} else {
kp->d->deleteTextSelections();
}

View file

@ -592,7 +592,7 @@ public:
* @param rect The rectangle of the selection.
* @param color The color of the selection.
*/
void setPageTextSelection(int page, RegularAreaRect *rect, const QColor &color);
void setPageTextSelection(int page, std::unique_ptr<RegularAreaRect> &&rect, const QColor &color);
/**
* Returns true if there is an undo command available; otherwise returns false.

View file

@ -258,16 +258,16 @@ bool Page::hasTextPage() const
return d->m_text != nullptr;
}
RegularAreaRect *Page::wordAt(const NormalizedPoint &p, QString *word) const
std::unique_ptr<RegularAreaRect> Page::wordAt(const NormalizedPoint &p) const
{
if (d->m_text) {
return d->m_text->wordAt(p, word);
return d->m_text->wordAt(p);
}
return nullptr;
}
RegularAreaRect *Page::textArea(TextSelection *selection) const
std::unique_ptr<RegularAreaRect> Page::textArea(const TextSelection &selection) const
{
if (d->m_text) {
return d->m_text->textArea(selection);
@ -626,16 +626,13 @@ void PagePrivate::setHighlight(int s_id, RegularAreaRect *rect, const QColor &co
m_page->m_highlights.append(hr);
}
void PagePrivate::setTextSelections(RegularAreaRect *r, const QColor &color)
void PagePrivate::setTextSelections(const RegularAreaRect &r, const QColor &color)
{
deleteTextSelections();
if (r) {
HighlightAreaRect *hr = new HighlightAreaRect(r);
hr->s_id = -1;
hr->color = color;
m_textSelections = hr;
delete r;
}
HighlightAreaRect *hr = new HighlightAreaRect(&r);
hr->s_id = -1;
hr->color = color;
m_textSelections = hr;
}
void Page::setSourceReferences(const QList<SourceRefObjectRect *> &refRects)

View file

@ -215,12 +215,12 @@ public:
* @see TextPage::wordAt()
* @since 0.15 (KDE 4.9)
*/
RegularAreaRect *wordAt(const NormalizedPoint &p, QString *word = nullptr) const;
std::unique_ptr<RegularAreaRect> wordAt(const NormalizedPoint &p) const;
/**
* Returns the rectangular area of the given @p selection.
*/
RegularAreaRect *textArea(TextSelection *selection) const;
std::unique_ptr<RegularAreaRect> textArea(const TextSelection &selection) const;
/**
* Returns the object rect of the given @p type which is at point (@p x, @p y) at scale (@p xScale, @p yScale).

View file

@ -95,7 +95,7 @@ public:
* @param r Areas of new text selections.
* @param color Color of new text selections.
*/
void setTextSelections(RegularAreaRect *r, const QColor &color);
void setTextSelections(const RegularAreaRect &r, const QColor &color);
/**
* Sets the @p color and @p rect of the highlight for the observer with

View file

@ -273,10 +273,10 @@ private:
QRect m_area;
};
RegularAreaRect *TextPage::textArea(TextSelection *sel) const
std::unique_ptr<RegularAreaRect> TextPage::textArea(const TextSelection &sel) const
{
if (d->m_words.isEmpty()) {
return new RegularAreaRect();
return std::make_unique<RegularAreaRect>();
}
/**
@ -293,15 +293,15 @@ RegularAreaRect *TextPage::textArea(TextSelection *sel) const
To find the closest rectangle to cursor (cx,cy) we search for a rectangle that either contains the cursor
or that has a left border >= cx and bottom border >= cy.
*/
RegularAreaRect *ret = new RegularAreaRect;
auto ret = std::make_unique<RegularAreaRect>();
PagePrivate *pagePrivate = PagePrivate::get(d->m_page);
const QTransform matrix = pagePrivate ? pagePrivate->rotationMatrix() : QTransform();
const double scaleX = d->m_page->width();
const double scaleY = d->m_page->height();
NormalizedPoint startC = sel->start();
NormalizedPoint endC = sel->end();
NormalizedPoint startC = sel.start();
NormalizedPoint endC = sel.end();
// if startPoint is right to endPoint swap them
if (startC.x > endC.x) {
@ -1689,7 +1689,7 @@ TextEntity::List TextPage::words(const RegularAreaRect *area, TextAreaInclusionB
return ret;
}
RegularAreaRect *TextPage::wordAt(const NormalizedPoint &p, QString *word) const
std::unique_ptr<RegularAreaRect> TextPage::wordAt(const NormalizedPoint &p) const
{
TextEntity::List::ConstIterator itBegin = d->m_words.constBegin(), itEnd = d->m_words.constEnd();
TextEntity::List::ConstIterator it = itBegin;
@ -1729,7 +1729,7 @@ RegularAreaRect *TextPage::wordAt(const NormalizedPoint &p, QString *word) const
break;
}
}
RegularAreaRect *ret = new RegularAreaRect();
auto ret = std::make_unique<RegularAreaRect>();
QString foundWord;
for (; posIt != itEnd; ++posIt) {
const QString itText = posIt->text();
@ -1746,9 +1746,6 @@ RegularAreaRect *TextPage::wordAt(const NormalizedPoint &p, QString *word) const
}
}
if (word) {
*word = foundWord;
}
return ret;
} else {
return nullptr;

View file

@ -186,12 +186,12 @@ public:
* Note that ownership of the returned area belongs to the caller.
* @since 0.15 (KDE 4.9)
*/
RegularAreaRect *wordAt(const NormalizedPoint &p, QString *word = nullptr) const;
std::unique_ptr<RegularAreaRect> wordAt(const NormalizedPoint &p) const;
/**
* Returns the rectangular area of the given @p selection.
*/
RegularAreaRect *textArea(TextSelection *selection) const;
std::unique_ptr<RegularAreaRect> textArea(const TextSelection &selection) const;
private:
TextPagePrivate *const d;

View file

@ -1140,9 +1140,9 @@ void PageView::copyTextSelection() const
void PageView::selectAll()
{
for (const PageViewItem *item : std::as_const(d->items)) {
Okular::RegularAreaRect *area = textSelectionForItem(item);
std::unique_ptr<Okular::RegularAreaRect> area = textSelectionForItem(item);
d->pagesWithTextSelection.insert(item->pageNumber());
d->document->setPageTextSelection(item->pageNumber(), area, palette().color(QPalette::Active, QPalette::Highlight));
d->document->setPageTextSelection(item->pageNumber(), std::move(area), palette().color(QPalette::Active, QPalette::Highlight));
}
}
@ -3230,10 +3230,10 @@ void PageView::mouseDoubleClickEvent(QMouseEvent *e)
if (d->mouseMode == Okular::Settings::EnumMouseMode::TextSelect) {
textSelectionClear();
Okular::RegularAreaRect *wordRect = pageItem->page()->wordAt(Okular::NormalizedPoint(nX, nY));
std::unique_ptr<Okular::RegularAreaRect> wordRect = pageItem->page()->wordAt(Okular::NormalizedPoint(nX, nY));
if (wordRect) {
// TODO words with hyphens across pages
d->document->setPageTextSelection(pageItem->pageNumber(), wordRect, palette().color(QPalette::Active, QPalette::Highlight));
d->document->setPageTextSelection(pageItem->pageNumber(), std::move(wordRect), palette().color(QPalette::Active, QPalette::Highlight));
d->pagesWithTextSelection << pageItem->pageNumber();
if (d->document->isAllowed(Okular::AllowCopy)) {
const QString text = d->selectedText();
@ -3380,10 +3380,10 @@ void PageView::scrollContentsBy(int dx, int dy)
}
// END widget events
QList<Okular::RegularAreaRect *> PageView::textSelections(const QPoint start, const QPoint end, int &firstpage)
std::vector<std::unique_ptr<Okular::RegularAreaRect>> PageView::textSelections(const QPoint start, const QPoint end, int &firstpage)
{
firstpage = -1;
QList<Okular::RegularAreaRect *> ret;
std::vector<std::unique_ptr<Okular::RegularAreaRect>> ret;
QSet<int> affectedItemsSet;
QRect selectionRect = QRect::span(start, end);
for (const PageViewItem *item : std::as_const(d->items)) {
@ -3427,25 +3427,25 @@ QList<Okular::RegularAreaRect *> PageView::textSelections(const QPoint start, co
if (affectedItemsIds.count() == 1) {
PageViewItem *item = d->items[affectedItemsIds.first()];
selectionRect.translate(-item->uncroppedGeometry().topLeft());
ret.append(textSelectionForItem(item, direction_ne_sw ? selectionRect.topRight() : selectionRect.topLeft(), direction_ne_sw ? selectionRect.bottomLeft() : selectionRect.bottomRight()));
ret.push_back(textSelectionForItem(item, direction_ne_sw ? selectionRect.topRight() : selectionRect.topLeft(), direction_ne_sw ? selectionRect.bottomLeft() : selectionRect.bottomRight()));
} else if (affectedItemsIds.count() > 1) {
// first item
PageViewItem *first = d->items[affectedItemsIds.first()];
QRect geom = first->croppedGeometry().intersected(selectionRect).translated(-first->uncroppedGeometry().topLeft());
ret.append(textSelectionForItem(first, selectionRect.bottom() > geom.height() ? (direction_ne_sw ? geom.topRight() : geom.topLeft()) : (direction_ne_sw ? geom.bottomRight() : geom.bottomLeft()), QPoint()));
ret.push_back(textSelectionForItem(first, selectionRect.bottom() > geom.height() ? (direction_ne_sw ? geom.topRight() : geom.topLeft()) : (direction_ne_sw ? geom.bottomRight() : geom.bottomLeft()), QPoint()));
// last item
PageViewItem *last = d->items[affectedItemsIds.last()];
geom = last->croppedGeometry().intersected(selectionRect).translated(-last->uncroppedGeometry().topLeft());
// the last item needs to appended at last...
Okular::RegularAreaRect *lastArea =
std::unique_ptr<Okular::RegularAreaRect> lastArea =
textSelectionForItem(last, QPoint(), selectionRect.bottom() > geom.height() ? (direction_ne_sw ? geom.bottomLeft() : geom.bottomRight()) : (direction_ne_sw ? geom.topLeft() : geom.topRight()));
affectedItemsIds.removeFirst();
affectedItemsIds.removeLast();
// item between the two above
for (const int page : std::as_const(affectedItemsIds)) {
ret.append(textSelectionForItem(d->items[page]));
ret.push_back(textSelectionForItem(d->items[page]));
}
ret.append(lastArea);
ret.push_back(std::move(lastArea));
}
}
return ret;
@ -3770,9 +3770,9 @@ void PageView::updateSelection(const QPoint pos)
} else if (d->mouseTextSelecting) {
scrollPosIntoView(pos);
int first = -1;
const QList<Okular::RegularAreaRect *> selections = textSelections(pos, d->mouseSelectPos.toPoint(), first);
std::vector<std::unique_ptr<Okular::RegularAreaRect>> selections = textSelections(pos, d->mouseSelectPos.toPoint(), first);
QSet<int> pagesWithSelectionSet;
for (int i = 0; i < selections.count(); ++i) {
for (size_t i = 0; i < selections.size(); ++i) {
pagesWithSelectionSet.insert(i + first);
}
@ -3783,7 +3783,7 @@ void PageView::updateSelection(const QPoint pos)
}
// set the new selection for the selected pages
for (int p : std::as_const(pagesWithSelectionSet)) {
d->document->setPageTextSelection(p, selections[p - first], palette().color(QPalette::Active, QPalette::Highlight));
d->document->setPageTextSelection(p, std::move(selections[p - first]), palette().color(QPalette::Active, QPalette::Highlight));
}
d->pagesWithTextSelection = pagesWithSelectionSet;
}
@ -3811,7 +3811,7 @@ static Okular::NormalizedPoint rotateInNormRect(const QPoint rotated, const QRec
return ret;
}
Okular::RegularAreaRect *PageView::textSelectionForItem(const PageViewItem *item, const QPoint startPoint, const QPoint endPoint)
std::unique_ptr<Okular::RegularAreaRect> PageView::textSelectionForItem(const PageViewItem *item, const QPoint startPoint, const QPoint endPoint)
{
const QRect &geometry = item->uncroppedGeometry();
Okular::NormalizedPoint startCursor(0.0, 0.0);
@ -3830,7 +3830,7 @@ Okular::RegularAreaRect *PageView::textSelectionForItem(const PageViewItem *item
d->document->requestTextPage(okularPage->number());
}
Okular::RegularAreaRect *selectionArea = okularPage->textArea(&mouseTextSelectionInfo);
std::unique_ptr<Okular::RegularAreaRect> selectionArea = okularPage->textArea(mouseTextSelectionInfo);
#ifdef PAGEVIEW_DEBUG
qCDebug(OkularUiDebug).nospace() << "text areas (" << okularPage->number() << "): " << (selectionArea ? QString::number(selectionArea->count()) : QStringLiteral("(none)"));
#endif
@ -5400,10 +5400,9 @@ void PageView::slotSpeakDocument()
{
QString text;
for (const PageViewItem *item : std::as_const(d->items)) {
Okular::RegularAreaRect *area = textSelectionForItem(item);
text.append(item->page()->text(area));
std::unique_ptr<Okular::RegularAreaRect> area = textSelectionForItem(item);
text.append(item->page()->text(area.get()));
text.append(QLatin1Char('\n'));
delete area;
}
d->tts()->say(text);
@ -5414,9 +5413,8 @@ void PageView::slotSpeakCurrentPage()
const int currentPage = d->document->viewport().pageNumber;
PageViewItem *item = d->items.at(currentPage);
Okular::RegularAreaRect *area = textSelectionForItem(item);
const QString text = item->page()->text(area);
delete area;
std::unique_ptr<Okular::RegularAreaRect> area = textSelectionForItem(item);
const QString text = item->page()->text(area.get());
d->tts()->say(text);
}
@ -5579,9 +5577,9 @@ void PageView::slotSelectPage()
PageViewItem *item = d->items.at(currentPage);
if (item) {
Okular::RegularAreaRect *area = textSelectionForItem(item);
std::unique_ptr<Okular::RegularAreaRect> area = textSelectionForItem(item);
d->pagesWithTextSelection.insert(currentPage);
d->document->setPageTextSelection(currentPage, area, palette().color(QPalette::Active, QPalette::Highlight));
d->document->setPageTextSelection(currentPage, std::move(area), palette().color(QPalette::Active, QPalette::Highlight));
}
}

View file

@ -93,8 +93,8 @@ public:
QVariant capability(ViewCapability capability) const override;
void setCapability(ViewCapability capability, const QVariant &option) override;
QList<Okular::RegularAreaRect *> textSelections(const QPoint start, const QPoint end, int &firstpage);
Okular::RegularAreaRect *textSelectionForItem(const PageViewItem *item, const QPoint startPoint = QPoint(), const QPoint endPoint = QPoint());
std::vector<std::unique_ptr<Okular::RegularAreaRect>> textSelections(const QPoint start, const QPoint end, int &firstpage);
std::unique_ptr<Okular::RegularAreaRect> textSelectionForItem(const PageViewItem *item, const QPoint startPoint = QPoint(), const QPoint endPoint = QPoint());
void reparseConfig();