Skip to content

Commit 599f3d4

Browse files
committed
[Qt] Explicitly pass index of clicked item to bboxDrawn instead of relying on selectionModel()->currentIndex()
1 parent 9dde83c commit 599f3d4

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

qt/src/hocr/DisplayerToolHOCR.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,25 @@ void DisplayerToolHOCR::mouseReleaseEvent(QMouseEvent* event) {
7070
clearSelection();
7171
} else {
7272
QRect r = m_selection->rect().translated(-m_displayer->getSceneBoundingRect().toRect().topLeft()).toRect();
73-
emit bboxDrawn(r, m_currentAction);
73+
emit bboxDrawn(r, m_currentAction, m_currentIndex);
7474
}
7575
event->accept();
7676
} else {
7777
QPoint p = m_displayer->mapToSceneClamped(event->pos()).toPoint();
7878
emit positionPicked(p);
7979
}
80-
setAction(ACTION_NONE, false);
80+
setAction(ACTION_NONE, QModelIndex(), false);
8181
}
8282

83-
void DisplayerToolHOCR::setAction(Action action, bool clearSel) {
83+
void DisplayerToolHOCR::setAction(Action action, QModelIndex index, bool clearSel) {
8484
if (action != m_currentAction) {
8585
emit actionChanged(action);
8686
}
8787
if (clearSel) {
8888
clearSelection();
8989
}
9090
m_currentAction = action;
91+
m_currentIndex = index;
9192
if (m_currentAction >= ACTION_DRAW_GRAPHIC_RECT && m_currentAction <= ACTION_DRAW_WORD_RECT) {
9293
m_displayer->setCursor(Qt::CrossCursor);
9394
} else {
@@ -96,7 +97,7 @@ void DisplayerToolHOCR::setAction(Action action, bool clearSel) {
9697
}
9798

9899
void DisplayerToolHOCR::setSelection(const QRect& rect, const QRect& minRect) {
99-
setAction(ACTION_NONE, false);
100+
setAction(ACTION_NONE, QModelIndex(), false);
100101
QRect r = rect.translated(m_displayer->getSceneBoundingRect().toRect().topLeft());
101102
QRect mr = minRect.translated(m_displayer->getSceneBoundingRect().toRect().topLeft());
102103
if (!m_selection) {

qt/src/hocr/DisplayerToolHOCR.hh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef DISPLAYERTOOLHOCR_HH
2121
#define DISPLAYERTOOLHOCR_HH
2222

23+
#include <QModelIndex>
24+
2325
#include "Displayer.hh"
2426

2527
class DisplayerToolHOCR : public DisplayerTool {
@@ -42,28 +44,29 @@ public:
4244
reset();
4345
}
4446
void reset() override {
45-
setAction(ACTION_NONE, true);
47+
setAction(ACTION_NONE, QModelIndex(), true);
4648
}
4749

4850
void mousePressEvent(QMouseEvent* event) override;
4951
void mouseMoveEvent(QMouseEvent* event) override;
5052
void mouseReleaseEvent(QMouseEvent* event) override;
5153

52-
void setAction(Action action, bool clearSel = true);
54+
void setAction(Action action, QModelIndex index, bool clearSel = true);
5355
void setSelection(const QRect& rect, const QRect& minRect);
5456
QImage getSelection(const QRect& rect) const;
5557
void clearSelection();
5658

5759
signals:
5860
void displayedSourceChanged();
59-
void bboxDrawn(QRect rect, int action);
61+
void bboxDrawn(QRect rect, int action, QModelIndex index);
6062
void bboxChanged(QRect rect);
6163
void positionPicked(QPoint pos);
6264
void actionChanged(int action);
6365

6466
private:
6567
DisplayerSelection* m_selection = nullptr;
6668
Action m_currentAction = ACTION_NONE;
69+
QModelIndex m_currentIndex;
6770
bool m_pressed = false;
6871

6972
private slots:

qt/src/hocr/OutputEditorHOCR.cc

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ int OutputEditorHOCR::currentPage() {
540540
}
541541

542542
void OutputEditorHOCR::showItemProperties(const QModelIndex& index, const QModelIndex& prev) {
543-
m_tool->setAction(DisplayerToolHOCR::ACTION_NONE);
543+
m_tool->setAction(DisplayerToolHOCR::ACTION_NONE, QModelIndex());
544544
const HOCRItem* prevItem = m_document->itemAtIndex(prev);
545545
ui.tableWidgetProperties->setRowCount(0);
546546
ui.plainTextEditOutput->setPlainText("");
@@ -685,10 +685,9 @@ void OutputEditorHOCR::itemAttributeChanged(const QModelIndex& itemIndex, const
685685
}
686686
}
687687

688-
void OutputEditorHOCR::bboxDrawn(const QRect& bbox, int action) {
688+
void OutputEditorHOCR::bboxDrawn(const QRect& bbox, int action, QModelIndex index) {
689689
QDomDocument doc;
690-
QModelIndex current = ui.treeViewHOCR->selectionModel()->currentIndex();
691-
const HOCRItem* currentItem = m_document->itemAtIndex(current);
690+
const HOCRItem* currentItem = m_document->itemAtIndex(index);
692691
if (!currentItem) {
693692
return;
694693
}
@@ -746,7 +745,7 @@ void OutputEditorHOCR::bboxDrawn(const QRect& bbox, int action) {
746745
} else {
747746
return;
748747
}
749-
QModelIndex newIndex = m_document->addItem(current, newElement);
748+
QModelIndex newIndex = m_document->addItem(index, newElement);
750749
if (newIndex.isValid()) {
751750
ui.treeViewHOCR->selectionModel()->setCurrentIndex(newIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
752751
}
@@ -864,21 +863,21 @@ void OutputEditorHOCR::showTreeWidgetContextMenu(const QPoint& point) {
864863
return;
865864
}
866865
if (clickedAction == actionAddGraphic) {
867-
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_GRAPHIC_RECT);
866+
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_GRAPHIC_RECT, index);
868867
} else if (clickedAction == actionAddCArea) {
869-
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_CAREA_RECT);
868+
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_CAREA_RECT, index);
870869
} else if (clickedAction == actionAddPar) {
871-
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_PAR_RECT);
870+
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_PAR_RECT, index);
872871
} else if (clickedAction == actionAddLine) {
873-
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_LINE_RECT);
872+
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_LINE_RECT, index);
874873
} else if (clickedAction == actionAddWord) {
875-
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_WORD_RECT);
874+
m_tool->setAction(DisplayerToolHOCR::ACTION_DRAW_WORD_RECT, index);
876875
} else if (clickedAction == actionSplit) {
877876
QModelIndex newIndex = m_document->splitItem(index.parent(), index.row(), index.row());
878877
ui.treeViewHOCR->selectionModel()->setCurrentIndex(newIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
879878
expandCollapseChildren(newIndex, true);
880879
} else if (clickedAction == actionRemove) {
881-
m_document->removeItem(ui.treeViewHOCR->selectionModel()->currentIndex());
880+
m_document->removeItem(index);
882881
} else if (clickedAction == actionExpand) {
883882
expandCollapseChildren(index, true);
884883
} else if (clickedAction == actionCollapse) {

qt/src/hocr/OutputEditorHOCR.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private:
108108
void drawPreview(QPainter& painter, const HOCRItem* item);
109109

110110
private slots:
111-
void bboxDrawn(const QRect& bbox, int action);
111+
void bboxDrawn(const QRect& bbox, int action, QModelIndex index);
112112
void addPage(const QString& hocrText, HOCRReadSessionData data);
113113
void expandItemClass() {
114114
expandCollapseItemClass(true);

0 commit comments

Comments
 (0)