Changeset View
Changeset View
Standalone View
Standalone View
src/ksaneviewer.cpp
Show First 20 Lines • Show All 79 Lines • ▼ Show 20 Line(s) | 79 | { | |||
---|---|---|---|---|---|
80 | d->img = img; | 80 | d->img = img; | ||
81 | 81 | | |||
82 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); | 82 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); | ||
83 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); | 83 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); | ||
84 | setMouseTracking(true); | 84 | setMouseTracking(true); | ||
85 | 85 | | |||
86 | // Init the scene | 86 | // Init the scene | ||
87 | d->scene = new QGraphicsScene(this); | 87 | d->scene = new QGraphicsScene(this); | ||
88 | d->scene->setSceneRect(0, 0, img->width(), img->height()); | 88 | const auto dpr = img->devicePixelRatio(); | ||
89 | d->scene->setSceneRect(0, 0, img->width() / dpr, img->height() / dpr); | ||||
89 | setScene(d->scene); | 90 | setScene(d->scene); | ||
90 | 91 | | |||
91 | d->selection = new SelectionItem(QRectF()); | 92 | d->selection = new SelectionItem(QRectF()); | ||
92 | d->selection->setZValue(10); | 93 | d->selection->setZValue(10); | ||
93 | d->selection->setSaved(false); | 94 | d->selection->setSaved(false); | ||
94 | d->selection->setMaxRight(img->width()); | 95 | d->selection->setMaxRight(img->width()); | ||
95 | d->selection->setMaxBottom(img->height()); | 96 | d->selection->setMaxBottom(img->height()); | ||
96 | d->selection->setRect(d->scene->sceneRect()); | 97 | d->selection->setRect(d->scene->sceneRect()); | ||
Show All 39 Lines | |||||
136 | addAction(d->clrSelAction); | 137 | addAction(d->clrSelAction); | ||
137 | setContextMenuPolicy(Qt::ActionsContextMenu); | 138 | setContextMenuPolicy(Qt::ActionsContextMenu); | ||
138 | } | 139 | } | ||
139 | 140 | | |||
140 | // ------------------------------------------------------------------------ | 141 | // ------------------------------------------------------------------------ | ||
141 | void KSaneViewer::drawBackground(QPainter *painter, const QRectF &rect) | 142 | void KSaneViewer::drawBackground(QPainter *painter, const QRectF &rect) | ||
142 | { | 143 | { | ||
143 | painter->fillRect(rect, QColor(0x70, 0x70, 0x70)); | 144 | painter->fillRect(rect, QColor(0x70, 0x70, 0x70)); | ||
144 | painter->drawImage(rect, *d->img, rect); | 145 | QRectF r = rect & sceneRect(); | ||
146 | const qreal dpr = d->img->devicePixelRatio(); | ||||
147 | QRectF srcRect = QRectF(r.topLeft() * dpr, r.size() * dpr); | ||||
148 | painter->drawImage(r, *d->img, srcRect); | ||||
145 | } | 149 | } | ||
146 | 150 | | |||
147 | // ------------------------------------------------------------------------ | 151 | // ------------------------------------------------------------------------ | ||
148 | KSaneViewer::~KSaneViewer() | 152 | KSaneViewer::~KSaneViewer() | ||
149 | { | 153 | { | ||
150 | // first remove any old saved selections | 154 | // first remove any old saved selections | ||
151 | clearSavedSelections(); | 155 | clearSavedSelections(); | ||
152 | 156 | | |||
153 | delete d; | 157 | delete d; | ||
154 | } | 158 | } | ||
155 | 159 | | |||
156 | // ------------------------------------------------------------------------ | 160 | // ------------------------------------------------------------------------ | ||
157 | void KSaneViewer::setQImage(QImage *img) | 161 | void KSaneViewer::setQImage(QImage *img) | ||
158 | { | 162 | { | ||
159 | if (img == nullptr) { | 163 | if (img == nullptr) { | ||
160 | return; | 164 | return; | ||
161 | } | 165 | } | ||
162 | 166 | | |||
163 | // remove selections | 167 | // remove selections | ||
164 | clearSelections(); | 168 | clearSelections(); | ||
165 | 169 | | |||
166 | // clear zoom | 170 | // clear zoom | ||
167 | setMatrix(QMatrix()); | 171 | setMatrix(QMatrix()); | ||
168 | 172 | | |||
169 | d->scene->setSceneRect(0, 0, img->width(), img->height()); | 173 | const auto dpr = img->devicePixelRatio(); | ||
174 | d->scene->setSceneRect(0, 0, img->width() / dpr, img->height() / dpr); | ||||
170 | d->selection->setMaxRight(img->width()); | 175 | d->selection->setMaxRight(img->width()); | ||
171 | d->selection->setMaxBottom(img->height()); | 176 | d->selection->setMaxBottom(img->height()); | ||
177 | | ||||
178 | d->selection->setDevicePixelRatio(dpr); | ||||
179 | d->hideTop->setDevicePixelRatio(dpr); | ||||
180 | d->hideBottom->setDevicePixelRatio(dpr); | ||||
181 | d->hideRight->setDevicePixelRatio(dpr); | ||||
182 | d->hideLeft->setDevicePixelRatio(dpr); | ||||
183 | d->hideArea->setDevicePixelRatio(dpr); | ||||
184 | | ||||
172 | d->img = img; | 185 | d->img = img; | ||
173 | } | 186 | } | ||
174 | 187 | | |||
175 | // ------------------------------------------------------------------------ | 188 | // ------------------------------------------------------------------------ | ||
176 | void KSaneViewer::updateImage() | 189 | void KSaneViewer::updateImage() | ||
177 | { | 190 | { | ||
178 | setCacheMode(QGraphicsView::CacheNone); | 191 | setCacheMode(QGraphicsView::CacheNone); | ||
179 | repaint(); | 192 | repaint(); | ||
▲ Show 20 Lines • Show All 342 Lines • ▼ Show 20 Line(s) | |||||
522 | } | 535 | } | ||
523 | 536 | | |||
524 | // ------------------------------------------------------------------------ | 537 | // ------------------------------------------------------------------------ | ||
525 | void KSaneViewer::mousePressEvent(QMouseEvent *e) | 538 | void KSaneViewer::mousePressEvent(QMouseEvent *e) | ||
526 | { | 539 | { | ||
527 | if (e->button() == Qt::LeftButton) { | 540 | if (e->button() == Qt::LeftButton) { | ||
528 | d->m_left_last_x = e->x(); | 541 | d->m_left_last_x = e->x(); | ||
529 | d->m_left_last_y = e->y(); | 542 | d->m_left_last_y = e->y(); | ||
530 | QPointF scenePoint = mapToScene(e->pos()); | 543 | QPointF scenePoint = scenePos(e) * d->selection->devicePixelRatio(); | ||
531 | d->lastSPoint = scenePoint; | 544 | d->lastSPoint = scenePoint; | ||
532 | if (e->modifiers() != Qt::ControlModifier) { | 545 | if (e->modifiers() != Qt::ControlModifier) { | ||
533 | if (!d->selection->isVisible()) { | 546 | if (!d->selection->isVisible()) { | ||
534 | d->selection->setVisible(true); | 547 | d->selection->setVisible(true); | ||
535 | d->selection->setRect(QRectF(scenePoint, QSizeF(0, 0))); | 548 | d->selection->setRect(QRectF(scenePoint, QSizeF(0, 0))); | ||
536 | d->selection->intersects(scenePoint); // just to disable add/remove | 549 | d->selection->intersects(scenePoint); // just to disable add/remove | ||
537 | d->change = SelectionItem::BottomRight; | 550 | d->change = SelectionItem::BottomRight; | ||
538 | } else if (d->selection->intersects(scenePoint) == SelectionItem::None) { | 551 | } else if (d->selection->intersects(scenePoint) == SelectionItem::None) { | ||
Show All 12 Lines | 563 | { | |||
551 | bool removed = false; | 564 | bool removed = false; | ||
552 | if (e->button() == Qt::LeftButton) { | 565 | if (e->button() == Qt::LeftButton) { | ||
553 | if ((d->selection->rect().width() < 0.001) || | 566 | if ((d->selection->rect().width() < 0.001) || | ||
554 | (d->selection->rect().height() < 0.001)) { | 567 | (d->selection->rect().height() < 0.001)) { | ||
555 | emit newSelection(0.0, 0.0, 0.0, 0.0); | 568 | emit newSelection(0.0, 0.0, 0.0, 0.0); | ||
556 | clearActiveSelection(); | 569 | clearActiveSelection(); | ||
557 | } | 570 | } | ||
558 | 571 | | |||
559 | QPointF scenePoint = mapToScene(e->pos()); | 572 | QPointF scenePoint = scenePos(e) * d->selection->devicePixelRatio(); | ||
560 | for (int i = 0; i < d->selectionList.size(); i++) { | 573 | for (int i = 0; i < d->selectionList.size(); i++) { | ||
561 | if (d->selectionList[i]->intersects(scenePoint) == SelectionItem::AddRemove) { | 574 | if (d->selectionList[i]->intersects(scenePoint) == SelectionItem::AddRemove) { | ||
562 | d->scene->removeItem(d->selectionList[i]); | 575 | d->scene->removeItem(d->selectionList[i]); | ||
563 | SelectionItem *tmp = d->selectionList[i]; | 576 | SelectionItem *tmp = d->selectionList[i]; | ||
564 | d->selectionList.removeAt(i); | 577 | d->selectionList.removeAt(i); | ||
565 | d->selection->setVisible(true); | 578 | d->selection->setVisible(true); | ||
566 | d->selection->setRect(tmp->rect()); | 579 | d->selection->setRect(tmp->rect()); | ||
567 | d->selection->intersects(scenePoint); // just to enable add/remove | 580 | d->selection->intersects(scenePoint); // just to enable add/remove | ||
568 | delete tmp; | 581 | delete tmp; | ||
569 | removed = true; | 582 | removed = true; | ||
570 | break; | 583 | break; | ||
571 | } | 584 | } | ||
572 | } | 585 | } | ||
573 | if (!removed && (d->selection->intersects(scenePoint) == SelectionItem::AddRemove)) { | 586 | if (!removed && (d->selection->intersects(scenePoint) == SelectionItem::AddRemove)) { | ||
574 | // add the current selection | 587 | // add the current selection | ||
575 | SelectionItem *tmp = new SelectionItem(d->selection->rect()); | 588 | SelectionItem *tmp = new SelectionItem(d->selection->rect()); | ||
589 | tmp->setDevicePixelRatio(d->img->devicePixelRatio()); | ||||
576 | d->selectionList.push_back(tmp); | 590 | d->selectionList.push_back(tmp); | ||
577 | d->selectionList.back()->setSaved(true); | 591 | d->selectionList.back()->setSaved(true); | ||
578 | d->selectionList.back()->saveZoom(transform().m11()); | 592 | d->selectionList.back()->saveZoom(transform().m11()); | ||
579 | d->scene->addItem(d->selectionList.back()); | 593 | d->scene->addItem(d->selectionList.back()); | ||
580 | d->selectionList.back()->setZValue(9); | 594 | d->selectionList.back()->setZValue(9); | ||
581 | d->selectionList.back()->intersects(scenePoint); | 595 | d->selectionList.back()->intersects(scenePoint); | ||
582 | 596 | | |||
583 | // clear the old one | 597 | // clear the old one | ||
Show All 15 Lines | |||||
599 | } | 613 | } | ||
600 | updateHighlight(); | 614 | updateHighlight(); | ||
601 | QGraphicsView::mouseReleaseEvent(e); | 615 | QGraphicsView::mouseReleaseEvent(e); | ||
602 | } | 616 | } | ||
603 | 617 | | |||
604 | // ------------------------------------------------------------------------ | 618 | // ------------------------------------------------------------------------ | ||
605 | void KSaneViewer::mouseMoveEvent(QMouseEvent *e) | 619 | void KSaneViewer::mouseMoveEvent(QMouseEvent *e) | ||
606 | { | 620 | { | ||
607 | QPointF scenePoint = mapToScene(e->pos()); | 621 | QPointF scenePoint = scenePos(e) * d->selection->devicePixelRatio(); | ||
608 | 622 | | |||
609 | if (e->buttons()&Qt::LeftButton) { | 623 | if (e->buttons()&Qt::LeftButton) { | ||
610 | if (e->modifiers() == Qt::ControlModifier) { | 624 | if (e->modifiers() == Qt::ControlModifier) { | ||
611 | int dx = e->x() - d->m_left_last_x; | 625 | int dx = e->x() - d->m_left_last_x; | ||
612 | int dy = e->y() - d->m_left_last_y; | 626 | int dy = e->y() - d->m_left_last_y; | ||
613 | verticalScrollBar()->setValue(verticalScrollBar()->value() - dy); | 627 | verticalScrollBar()->setValue(verticalScrollBar()->value() - dy); | ||
614 | horizontalScrollBar()->setValue(horizontalScrollBar()->value() - dx); | 628 | horizontalScrollBar()->setValue(horizontalScrollBar()->value() - dx); | ||
615 | d->m_left_last_x = e->x(); | 629 | d->m_left_last_x = e->x(); | ||
▲ Show 20 Lines • Show All 292 Lines • ▼ Show 20 Line(s) | 920 | if ((wSelEnd - wSelStart) < width) { | |||
908 | // calculate the coordinates in the original size | 922 | // calculate the coordinates in the original size | ||
909 | int x1 = wSelStart / multiplier; | 923 | int x1 = wSelStart / multiplier; | ||
910 | int y1 = hSelStart / multiplier; | 924 | int y1 = hSelStart / multiplier; | ||
911 | int x2 = wSelEnd / multiplier; | 925 | int x2 = wSelEnd / multiplier; | ||
912 | int y2 = hSelEnd / multiplier; | 926 | int y2 = hSelEnd / multiplier; | ||
913 | float selArea = (float)(wSelEnd - wSelStart) * (float)(hSelEnd - hSelStart); | 927 | float selArea = (float)(wSelEnd - wSelStart) * (float)(hSelEnd - hSelStart); | ||
914 | if (selArea > (area * MIN_AREA_SIZE)) { | 928 | if (selArea > (area * MIN_AREA_SIZE)) { | ||
915 | SelectionItem *tmp = new SelectionItem(QRect(QPoint(x1, y1), QPoint(x2, y2))); | 929 | SelectionItem *tmp = new SelectionItem(QRect(QPoint(x1, y1), QPoint(x2, y2))); | ||
930 | tmp->setDevicePixelRatio(d->img->devicePixelRatio()); | ||||
916 | d->selectionList.push_back(tmp); | 931 | d->selectionList.push_back(tmp); | ||
917 | d->selectionList.back()->setSaved(true); | 932 | d->selectionList.back()->setSaved(true); | ||
918 | d->selectionList.back()->saveZoom(transform().m11()); | 933 | d->selectionList.back()->saveZoom(transform().m11()); | ||
919 | d->scene->addItem(d->selectionList.back()); | 934 | d->scene->addItem(d->selectionList.back()); | ||
920 | d->selectionList.back()->setZValue(9); | 935 | d->selectionList.back()->setZValue(9); | ||
921 | } | 936 | } | ||
922 | } | 937 | } | ||
923 | wSelStart = -1; | 938 | wSelStart = -1; | ||
▲ Show 20 Lines • Show All 213 Lines • ▼ Show 20 Line(s) | 1144 | if (colTrigger > AVERAGE_TRIGGER) { | |||
1137 | } | 1152 | } | ||
1138 | return col; | 1153 | return col; | ||
1139 | } | 1154 | } | ||
1140 | col += addSub; | 1155 | col += addSub; | ||
1141 | } | 1156 | } | ||
1142 | return col; | 1157 | return col; | ||
1143 | } | 1158 | } | ||
1144 | 1159 | | |||
1160 | QPointF KSaneViewer::scenePos(QMouseEvent *e) const | ||||
1161 | { | ||||
1162 | // QGraphicsView::mapToScene() maps only QPoints, but in highdpi mode we want | ||||
1163 | // to deal with non-rounded coordinates, that's why QPainterPath wrapper is used. | ||||
1164 | // QMouseEvent::localPos() currently returns a rounded QPointF | ||||
1165 | // (https://codereview.qt-project.org/259785), so we have to extract a fractional | ||||
1166 | // part from QMouseEvent::screenPos(). | ||||
1167 | const QPointF screenPos = e->screenPos(); | ||||
1168 | QPointF delta = screenPos - screenPos.toPoint(); | ||||
1169 | return mapToScene(QPainterPath(e->pos() + delta)).currentPosition(); | ||||
1170 | } | ||||
1171 | | ||||
1145 | } // NameSpace KSaneIface | 1172 | } // NameSpace KSaneIface |