• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegraphics API Reference
  • KDE Home
  • Contact Us
 

libs/libksane/libksane

  • sources
  • kde-4.14
  • kdegraphics
  • libs
  • libksane
  • libksane
selectionitem.cpp
Go to the documentation of this file.
1 /* ============================================================
2 *
3 * This file is part of the KDE project
4 *
5 * Date : 2008-11-15
6 * Description :Selection QGraphicsItem for the image viewer.
7 *
8 * Copyright (C) 2008 by Kare Sars <kare dot sars at iki dot fi>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) version 3, or any
14 * later version accepted by the membership of KDE e.V. (or its
15 * successor approved by the membership of KDE e.V.), which shall
16 * act as a proxy defined in Section 6 of version 3 of the license.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 * ============================================================ */
27 
28 #include "selectionitem.h"
29 #include <QGraphicsView>
30 #include <QGraphicsScene>
31 #include <QCursor>
32 #include <QList>
33 
34 namespace KSaneIface
35 {
36 
37 
38 static const qreal selMargin = 4.0;
39 static const QPointF boundMargin(selMargin,selMargin);
40 static const qreal addRemMargin = 8.0;
41 static const QPointF addRemMarginPoint(addRemMargin, addRemMargin);
42 
43 struct SelectionItem::Private
44 {
45  QPen penDark;
46  QPen penLight;
47  QPen penAddRemFg;
48  QRectF rect;
49  qreal maxX;
50  qreal maxY;
51  bool hasMaxX;
52  bool hasMaxY;
53  bool hasMax;
54  bool isSaved;
55  bool showAddRem;
56  qreal invZoom;
57  qreal selMargin;
58  QRectF addRemRect;
59 };
60 
61 
62 SelectionItem::SelectionItem(QRectF rect) : QGraphicsItem(), d(new Private)
63 {
64  d->hasMaxX = false;
65  d->hasMaxY = false;
66  d->hasMax = false;
67  setRect(rect);
68 
69  d->penDark.setColor(Qt::black);
70  d->penDark.setStyle(Qt::SolidLine);
71  d->penLight.setColor(Qt::white);
72  d->penLight.setStyle(Qt::DashLine);
73 
74  // FIXME We should probably use some standard KDE color here and not hard code it
75  d->penAddRemFg.setColor(Qt::darkGreen);
76  d->penAddRemFg.setStyle(Qt::SolidLine);
77  d->penAddRemFg.setWidth(3);
78 
79  d->isSaved = false;
80  d->showAddRem = false;
81  d->invZoom = 1;
82  d->selMargin = selMargin;
83 
84  d->addRemRect = QRectF(0,0,0,0);
85 }
86 
87 SelectionItem::~SelectionItem()
88 {
89  delete d;
90 }
91 
92 void SelectionItem::saveZoom(qreal zoom)
93 {
94  if (zoom < 0.00001) zoom = 0.00001;
95  d->invZoom = 1/zoom;
96 
97  d->selMargin = selMargin * d->invZoom;
98 
99  qreal margin = addRemMargin * d->invZoom;
100  QPointF pMargin = addRemMarginPoint * d->invZoom;
101  d->addRemRect = QRectF(d->rect.center()-pMargin, QSizeF(margin*2.0, margin*2.0));
102  d->penAddRemFg.setWidthF(3.0 * d->invZoom);
103 }
104 
105 void SelectionItem::setSaved(bool isSaved)
106 {
107  if (isSaved) {
108  d->penDark.setColor(Qt::darkBlue);
109  d->penLight.setColor(Qt::red);
110  d->penAddRemFg.setColor(Qt::darkRed);
111  d->isSaved = true;
112  }
113  else {
114  d->penDark.setColor(Qt::black);
115  d->penLight.setColor(Qt::white);
116  d->penAddRemFg.setColor(Qt::darkGreen);
117  d->isSaved = false;
118  }
119 }
120 
121 void SelectionItem::setMaxRight(qreal maxX)
122 {
123  d->maxX = maxX;
124  d->hasMaxX = true;
125  if (d->hasMaxY) d->hasMax = true;
126 }
127 
128 void SelectionItem::setMaxBottom(qreal maxY)
129 {
130  d->maxY = maxY;
131  d->hasMaxY = true;
132  if (d->hasMaxX) d->hasMax = true;
133 }
134 
135 SelectionItem::Intersects SelectionItem::intersects(QPointF point)
136 {
137  bool oldState = d->showAddRem;
138  d->showAddRem = false;
139 
140  if ((point.x() < (d->rect.left()-d->selMargin)) ||
141  (point.x() > (d->rect.right()+d->selMargin)) ||
142  (point.y() < (d->rect.top()-d->selMargin)) ||
143  (point.y() > (d->rect.bottom()+d->selMargin)))
144  {
145  if (oldState != d->showAddRem) update();
146  return None;
147  }
148 
149  if (point.x() < (d->rect.left()+d->selMargin)) {
150  if (oldState != d->showAddRem) update();
151  if (point.y() < (d->rect.top()+d->selMargin)) return TopLeft;
152  if (point.y() > (d->rect.bottom()-d->selMargin)) return BottomLeft;
153  return Left;
154  }
155 
156  if (point.x() > (d->rect.right()-d->selMargin)) {
157  if (oldState != d->showAddRem) update();
158  if (point.y() < (d->rect.top()+d->selMargin)) return TopRight;
159  if (point.y() > (d->rect.bottom()-d->selMargin)) return BottomRight;
160  return Right;
161  }
162 
163  if (point.y() < (d->rect.top()+d->selMargin)) {
164  if (oldState != d->showAddRem) update();
165  return Top;
166  }
167  if (point.y() > (d->rect.bottom()-d->selMargin)) {
168  if (oldState != d->showAddRem) update();
169  return Bottom;
170  }
171 
172  d->showAddRem = true;
173  if (oldState != d->showAddRem) update();
174 
175  if ((point.x() > d->addRemRect.left()) &&
176  (point.x() < d->addRemRect.right()) &&
177  (point.y() > d->addRemRect.top()) &&
178  (point.y() < d->addRemRect.bottom()))
179  {
180  return AddRemove;
181  }
182  return Move;
183 }
184 
185 void SelectionItem::setRect(QRectF rect)
186 {
187  prepareGeometryChange();
188  d->rect = rect;
189  d->rect = d->rect.normalized();
190  if (d->hasMax) {
191  if (d->rect.top() < 0) d->rect.setTop(0);
192  if (d->rect.left() < 0) d->rect.setLeft(0);
193  if (d->rect.right() > d->maxX) d->rect.setRight(d->maxX);
194  if (d->rect.bottom() > d->maxY) d->rect.setBottom(d->maxY);
195  }
196 
197  // calculate the add/remove rectangle
198  qreal margin = addRemMargin * d->invZoom;
199  QPointF pMargin = addRemMarginPoint * d->invZoom;
200  d->addRemRect = QRectF(d->rect.center()-pMargin, QSizeF(margin*2, margin*2));
201 }
202 
203 QPointF SelectionItem::fixTranslation(QPointF dp)
204 {
205  if ((d->rect.left() + dp.x()) < 0) dp.setX(-d->rect.left());
206  if ((d->rect.top() + dp.y()) < 0) dp.setY(-d->rect.top());
207  if ((d->rect.right() + dp.x()) > d->maxX) dp.setX(d->maxX - d->rect.right());
208  if ((d->rect.bottom() + dp.y()) > d->maxY) dp.setY(d->maxY - d->rect.bottom());
209  return dp;
210 }
211 
212 QRectF SelectionItem::rect()
213 {
214  return d->rect;
215 }
216 
217 QRectF SelectionItem::boundingRect() const
218 {
219  QRectF tmp(d->rect.topLeft()-boundMargin, d->rect.bottomRight()+boundMargin);
220  if (tmp.top() > d->addRemRect.top()) {
221  tmp.setTop(d->addRemRect.top());
222  }
223  if (tmp.left() > d->addRemRect.left()) {
224  tmp.setLeft(d->addRemRect.left());
225  }
226 
227  if (tmp.bottom() < d->addRemRect.bottom()) {
228  tmp.setBottom(d->addRemRect.bottom());
229  }
230 
231  if (tmp.right() < d->addRemRect.right()) {
232  tmp.setRight(d->addRemRect.right());
233  }
234 
235  return tmp;
236 }
237 
238 void SelectionItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
239 {
240  painter->setPen(d->penDark);
241  painter->drawRect(d->rect);
242 
243  painter->setPen(d->penLight);
244  painter->drawRect(d->rect);
245 
246  if (d->showAddRem) {
247  painter->fillRect(d->addRemRect, QBrush(Qt::white));
248  QLineF minus(d->addRemRect.left()+3*d->invZoom, d->rect.center().y(),
249  d->addRemRect.right()-3*d->invZoom, d->rect.center().y());
250  painter->setPen(d->penAddRemFg);
251 
252  painter->drawLine(minus);
253 
254  if (!d->isSaved) {
255  QLineF plus(d->rect.center().x(), d->addRemRect.top()+3*d->invZoom,
256  d->rect.center().x(), d->addRemRect.bottom()-3*d->invZoom);
257  painter->drawLine(plus);
258  }
259  }
260 }
261 
262 } // NameSpace KSaneIface
KSaneIface::addRemMarginPoint
static const QPointF addRemMarginPoint(addRemMargin, addRemMargin)
QWidget
KSaneIface::SelectionItem::None
Definition: selectionitem.h:41
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
KSaneIface::SelectionItem::Right
Definition: selectionitem.h:44
KSaneIface::SelectionItem::saveZoom
void saveZoom(qreal zoom)
Definition: selectionitem.cpp:92
KSaneIface::addRemMargin
static const qreal addRemMargin
Definition: selectionitem.cpp:40
KSaneIface::SelectionItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: selectionitem.cpp:238
QBrush
QGraphicsItem
KSaneIface::SelectionItem::rect
QRectF rect()
Definition: selectionitem.cpp:212
QPainter::drawLine
void drawLine(const QLineF &line)
KSaneIface::SelectionItem::Move
Definition: selectionitem.h:50
KSaneIface::SelectionItem::Intersects
Intersects
Definition: selectionitem.h:39
KSaneIface::boundMargin
static const QPointF boundMargin(selMargin, selMargin)
QGraphicsItem::update
void update(const QRectF &rect)
KSaneIface::SelectionItem::SelectionItem
SelectionItem(QRectF rect)
Definition: selectionitem.cpp:62
KSaneIface::SelectionItem::Top
Definition: selectionitem.h:42
QPointF
QPainter::drawRect
void drawRect(const QRectF &rectangle)
QPointF::x
qreal x() const
QPointF::y
qreal y() const
KSaneIface::SelectionItem::setMaxBottom
void setMaxBottom(qreal maxBottom)
Definition: selectionitem.cpp:128
KSaneIface::SelectionItem::Left
Definition: selectionitem.h:48
QPainter::setPen
void setPen(const QColor &color)
KSaneIface::selMargin
static const qreal selMargin
Definition: selectionitem.cpp:38
QPainter
KSaneIface::SelectionItem::intersects
Intersects intersects(QPointF point)
Definition: selectionitem.cpp:135
QLineF
KSaneIface::SelectionItem::setSaved
void setSaved(bool isSaved)
Definition: selectionitem.cpp:105
KSaneIface::SelectionItem::TopLeft
Definition: selectionitem.h:49
QGraphicsItem::prepareGeometryChange
void prepareGeometryChange()
KSaneIface::SelectionItem::setMaxRight
void setMaxRight(qreal maxRight)
Definition: selectionitem.cpp:121
KSaneIface::SelectionItem::TopRight
Definition: selectionitem.h:43
QSizeF
QRectF
QRectF::setTop
void setTop(qreal y)
QPointF::setX
void setX(qreal x)
QPointF::setY
void setY(qreal y)
selectionitem.h
KSaneIface::SelectionItem::BottomLeft
Definition: selectionitem.h:47
QPen
QStyleOptionGraphicsItem
KSaneIface::SelectionItem::~SelectionItem
~SelectionItem()
Definition: selectionitem.cpp:87
KSaneIface::SelectionItem::AddRemove
Definition: selectionitem.h:51
KSaneIface::SelectionItem::boundingRect
QRectF boundingRect() const
Definition: selectionitem.cpp:217
KSaneIface::SelectionItem::Bottom
Definition: selectionitem.h:46
KSaneIface::SelectionItem::setRect
void setRect(QRectF rect)
Definition: selectionitem.cpp:185
KSaneIface::SelectionItem::fixTranslation
QPointF fixTranslation(QPointF dp)
Definition: selectionitem.cpp:203
KSaneIface::SelectionItem::BottomRight
Definition: selectionitem.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:19:47 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libs/libksane/libksane

Skip menu "libs/libksane/libksane"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegraphics API Reference

Skip menu "kdegraphics API Reference"
  •     libkdcraw
  •     libkexiv2
  •     libkipi
  •     libksane
  • okular

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal