Changeset View
Changeset View
Standalone View
Standalone View
src/selectionitem.cpp
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Line(s) | 42 | struct SelectionItem::Private { | |||
---|---|---|---|---|---|
49 | bool hasMaxX; | 49 | bool hasMaxX; | ||
50 | bool hasMaxY; | 50 | bool hasMaxY; | ||
51 | bool hasMax; | 51 | bool hasMax; | ||
52 | bool isSaved; | 52 | bool isSaved; | ||
53 | bool showAddRem; | 53 | bool showAddRem; | ||
54 | qreal invZoom; | 54 | qreal invZoom; | ||
55 | qreal selMargin; | 55 | qreal selMargin; | ||
56 | QRectF addRemRect; | 56 | QRectF addRemRect; | ||
57 | qreal devicePixelRatio; | ||||
57 | }; | 58 | }; | ||
58 | 59 | | |||
59 | SelectionItem::SelectionItem(const QRectF &rect) : QGraphicsItem(), d(new Private) | 60 | SelectionItem::SelectionItem(const QRectF &rect) : QGraphicsItem(), d(new Private) | ||
60 | { | 61 | { | ||
61 | d->hasMaxX = false; | 62 | d->hasMaxX = false; | ||
62 | d->hasMaxY = false; | 63 | d->hasMaxY = false; | ||
63 | d->hasMax = false; | 64 | d->hasMax = false; | ||
64 | setRect(rect); | 65 | setRect(rect); | ||
65 | 66 | | |||
66 | d->penDark.setColor(Qt::black); | 67 | d->penDark.setColor(Qt::black); | ||
67 | d->penDark.setStyle(Qt::SolidLine); | 68 | d->penDark.setStyle(Qt::SolidLine); | ||
69 | d->penDark.setWidth(0); | ||||
68 | d->penLight.setColor(Qt::white); | 70 | d->penLight.setColor(Qt::white); | ||
69 | d->penLight.setStyle(Qt::DashLine); | 71 | d->penLight.setStyle(Qt::DashLine); | ||
72 | d->penLight.setWidth(0); | ||||
70 | 73 | | |||
71 | // FIXME We should probably use some standard KDE color here and not hard code it | 74 | // FIXME We should probably use some standard KDE color here and not hard code it | ||
72 | d->penAddRemFg.setColor(Qt::darkGreen); | 75 | d->penAddRemFg.setColor(Qt::darkGreen); | ||
73 | d->penAddRemFg.setStyle(Qt::SolidLine); | 76 | d->penAddRemFg.setStyle(Qt::SolidLine); | ||
74 | d->penAddRemFg.setWidth(3); | 77 | d->penAddRemFg.setWidth(3); | ||
75 | 78 | | |||
76 | d->isSaved = false; | 79 | d->isSaved = false; | ||
77 | d->showAddRem = false; | 80 | d->showAddRem = false; | ||
78 | d->invZoom = 1; | 81 | d->invZoom = 1; | ||
79 | d->selMargin = selMargin; | 82 | d->selMargin = selMargin; | ||
80 | 83 | | |||
81 | d->addRemRect = QRectF(0, 0, 0, 0); | 84 | d->addRemRect = QRectF(0, 0, 0, 0); | ||
85 | | ||||
86 | d->devicePixelRatio = 1.0; | ||||
82 | } | 87 | } | ||
83 | 88 | | |||
84 | SelectionItem::~SelectionItem() | 89 | SelectionItem::~SelectionItem() | ||
85 | { | 90 | { | ||
86 | delete d; | 91 | delete d; | ||
87 | } | 92 | } | ||
88 | 93 | | |||
89 | void SelectionItem::saveZoom(qreal zoom) | 94 | void SelectionItem::saveZoom(qreal zoom) | ||
90 | { | 95 | { | ||
91 | if (zoom < 0.00001) { | 96 | if (zoom < 0.00001) { | ||
92 | zoom = 0.00001; | 97 | zoom = 0.00001; | ||
93 | } | 98 | } | ||
94 | d->invZoom = 1 / zoom; | 99 | d->invZoom = 1 / zoom; | ||
95 | 100 | | |||
96 | d->selMargin = selMargin * d->invZoom; | 101 | d->selMargin = selMargin * d->invZoom; | ||
97 | 102 | | |||
98 | qreal margin = addRemMargin * d->invZoom; | 103 | qreal margin = addRemMargin * d->invZoom; | ||
99 | QPointF pMargin = addRemMarginPoint * d->invZoom; | 104 | QPointF pMargin = addRemMarginPoint * d->invZoom; | ||
100 | d->addRemRect = QRectF(d->rect.center() - pMargin, QSizeF(margin * 2.0, margin * 2.0)); | 105 | d->addRemRect = QRectF(d->rect.center() / d->devicePixelRatio - pMargin, QSizeF(margin * 2.0, margin * 2.0)); | ||
101 | d->penAddRemFg.setWidthF(3.0 * d->invZoom); | 106 | d->penAddRemFg.setWidthF(3.0 * d->invZoom); | ||
102 | } | 107 | } | ||
103 | 108 | | |||
104 | void SelectionItem::setSaved(bool isSaved) | 109 | void SelectionItem::setSaved(bool isSaved) | ||
105 | { | 110 | { | ||
106 | if (isSaved) { | 111 | if (isSaved) { | ||
107 | d->penDark.setColor(Qt::darkBlue); | 112 | d->penDark.setColor(Qt::darkBlue); | ||
108 | d->penLight.setColor(Qt::red); | 113 | d->penLight.setColor(Qt::red); | ||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Line(s) | 189 | if (point.y() > (d->rect.bottom() - d->selMargin)) { | |||
188 | return Bottom; | 193 | return Bottom; | ||
189 | } | 194 | } | ||
190 | 195 | | |||
191 | d->showAddRem = true; | 196 | d->showAddRem = true; | ||
192 | if (oldState != d->showAddRem) { | 197 | if (oldState != d->showAddRem) { | ||
193 | update(); | 198 | update(); | ||
194 | } | 199 | } | ||
195 | 200 | | |||
196 | if ((point.x() > d->addRemRect.left()) && | 201 | if (d->addRemRect.contains(point / d->devicePixelRatio)) { | ||
197 | (point.x() < d->addRemRect.right()) && | | |||
198 | (point.y() > d->addRemRect.top()) && | | |||
199 | (point.y() < d->addRemRect.bottom())) { | | |||
200 | return AddRemove; | 202 | return AddRemove; | ||
201 | } | 203 | } | ||
202 | return Move; | 204 | return Move; | ||
203 | } | 205 | } | ||
204 | 206 | | |||
205 | void SelectionItem::setRect(const QRectF &rect) | 207 | void SelectionItem::setRect(const QRectF &rect) | ||
206 | { | 208 | { | ||
207 | prepareGeometryChange(); | 209 | prepareGeometryChange(); | ||
Show All 12 Lines | 212 | if (d->hasMax) { | |||
220 | if (d->rect.bottom() > d->maxY) { | 222 | if (d->rect.bottom() > d->maxY) { | ||
221 | d->rect.setBottom(d->maxY); | 223 | d->rect.setBottom(d->maxY); | ||
222 | } | 224 | } | ||
223 | } | 225 | } | ||
224 | 226 | | |||
225 | // calculate the add/remove rectangle | 227 | // calculate the add/remove rectangle | ||
226 | qreal margin = addRemMargin * d->invZoom; | 228 | qreal margin = addRemMargin * d->invZoom; | ||
227 | QPointF pMargin = addRemMarginPoint * d->invZoom; | 229 | QPointF pMargin = addRemMarginPoint * d->invZoom; | ||
228 | d->addRemRect = QRectF(d->rect.center() - pMargin, QSizeF(margin * 2, margin * 2)); | 230 | d->addRemRect = QRectF(d->rect.center() / d->devicePixelRatio - pMargin, QSizeF(margin * 2, margin * 2)); | ||
229 | } | 231 | } | ||
230 | 232 | | |||
231 | QPointF SelectionItem::fixTranslation(QPointF dp) | 233 | QPointF SelectionItem::fixTranslation(QPointF dp) | ||
232 | { | 234 | { | ||
233 | if ((d->rect.left() + dp.x()) < 0) { | 235 | if ((d->rect.left() + dp.x()) < 0) { | ||
234 | dp.setX(-d->rect.left()); | 236 | dp.setX(-d->rect.left()); | ||
235 | } | 237 | } | ||
236 | if ((d->rect.top() + dp.y()) < 0) { | 238 | if ((d->rect.top() + dp.y()) < 0) { | ||
237 | dp.setY(-d->rect.top()); | 239 | dp.setY(-d->rect.top()); | ||
238 | } | 240 | } | ||
239 | if ((d->rect.right() + dp.x()) > d->maxX) { | 241 | if ((d->rect.right() + dp.x()) > d->maxX) { | ||
240 | dp.setX(d->maxX - d->rect.right()); | 242 | dp.setX(d->maxX - d->rect.right()); | ||
241 | } | 243 | } | ||
242 | if ((d->rect.bottom() + dp.y()) > d->maxY) { | 244 | if ((d->rect.bottom() + dp.y()) > d->maxY) { | ||
243 | dp.setY(d->maxY - d->rect.bottom()); | 245 | dp.setY(d->maxY - d->rect.bottom()); | ||
244 | } | 246 | } | ||
245 | return dp; | 247 | return dp; | ||
246 | } | 248 | } | ||
247 | 249 | | |||
248 | QRectF SelectionItem::rect() | 250 | QRectF SelectionItem::rect() | ||
249 | { | 251 | { | ||
250 | return d->rect; | 252 | return d->rect; | ||
251 | } | 253 | } | ||
252 | 254 | | |||
253 | QRectF SelectionItem::boundingRect() const | 255 | qreal SelectionItem::devicePixelRatio() const | ||
254 | { | 256 | { | ||
255 | QRectF tmp(d->rect.topLeft() - boundMargin, d->rect.bottomRight() + boundMargin); | 257 | return d->devicePixelRatio; | ||
256 | if (tmp.top() > d->addRemRect.top()) { | | |||
257 | tmp.setTop(d->addRemRect.top()); | | |||
258 | } | | |||
259 | if (tmp.left() > d->addRemRect.left()) { | | |||
260 | tmp.setLeft(d->addRemRect.left()); | | |||
261 | } | | |||
262 | | ||||
263 | if (tmp.bottom() < d->addRemRect.bottom()) { | | |||
264 | tmp.setBottom(d->addRemRect.bottom()); | | |||
265 | } | 258 | } | ||
266 | 259 | | |||
267 | if (tmp.right() < d->addRemRect.right()) { | 260 | void SelectionItem::setDevicePixelRatio(qreal dpr) | ||
268 | tmp.setRight(d->addRemRect.right()); | 261 | { | ||
262 | d->devicePixelRatio = dpr; | ||||
269 | } | 263 | } | ||
sars: Do we want to divide the width of the selection-item border also? might it not become a bit… | |||||
Qt scales width of a pen by devicePixelRatio, and this is the inverse transform to make it again 1 pixel. volkov: Qt scales width of a pen by devicePixelRatio, and this is the inverse transform to make it… | |||||
use pen->setWidth(0) instead;
davidedmundson: use pen->setWidth(0) instead;
> A line width of zero indicates a cosmetic pen. This means that… | |||||
270 | 264 | | |||
271 | return tmp; | 265 | QRectF SelectionItem::boundingRect() const | ||
266 | { | ||||
267 | const auto dpr = d->devicePixelRatio; | ||||
268 | QRectF tmp(d->rect.topLeft() / dpr - boundMargin, d->rect.bottomRight() / dpr + boundMargin); | ||||
269 | return tmp.united(d->addRemRect); | ||||
272 | } | 270 | } | ||
273 | 271 | | |||
274 | void SelectionItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | 272 | void SelectionItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | ||
275 | { | 273 | { | ||
274 | const auto dpr = d->devicePixelRatio; | ||||
275 | QRectF rect(d->rect.topLeft() / dpr, d->rect.size() / dpr); | ||||
276 | | ||||
276 | painter->setPen(d->penDark); | 277 | painter->setPen(d->penDark); | ||
277 | painter->drawRect(d->rect); | 278 | painter->drawRect(rect); | ||
278 | 279 | | |||
279 | painter->setPen(d->penLight); | 280 | painter->setPen(d->penLight); | ||
280 | painter->drawRect(d->rect); | 281 | painter->drawRect(rect); | ||
281 | 282 | | |||
282 | if (d->showAddRem) { | 283 | if (d->showAddRem) { | ||
283 | painter->fillRect(d->addRemRect, QBrush(Qt::white)); | 284 | painter->fillRect(d->addRemRect, QBrush(Qt::white)); | ||
284 | QLineF minus(d->addRemRect.left() + 3 * d->invZoom, d->rect.center().y(), | 285 | QLineF minus(d->addRemRect.left() + 3 * d->invZoom, d->addRemRect.center().y(), | ||
285 | d->addRemRect.right() - 3 * d->invZoom, d->rect.center().y()); | 286 | d->addRemRect.right() - 3 * d->invZoom, d->addRemRect.center().y()); | ||
286 | painter->setPen(d->penAddRemFg); | 287 | painter->setPen(d->penAddRemFg); | ||
287 | 288 | | |||
288 | painter->drawLine(minus); | 289 | painter->drawLine(minus); | ||
289 | 290 | | |||
290 | if (!d->isSaved) { | 291 | if (!d->isSaved) { | ||
291 | QLineF plus(d->rect.center().x(), d->addRemRect.top() + 3 * d->invZoom, | 292 | QLineF plus(d->addRemRect.center().x(), d->addRemRect.top() + 3 * d->invZoom, | ||
292 | d->rect.center().x(), d->addRemRect.bottom() - 3 * d->invZoom); | 293 | d->addRemRect.center().x(), d->addRemRect.bottom() - 3 * d->invZoom); | ||
293 | painter->drawLine(plus); | 294 | painter->drawLine(plus); | ||
294 | } | 295 | } | ||
295 | } | 296 | } | ||
296 | } | 297 | } | ||
297 | 298 | | |||
298 | } // NameSpace KSaneIface | 299 | } // NameSpace KSaneIface |
Do we want to divide the width of the selection-item border also? might it not become a bit narrow?