1 #include "XGUI_RubberBand.h"
\r
5 #include <QPaintEvent>
\r
8 #include <QShowEvent>
\r
9 #include <QVectorIterator>
\r
12 \class QtxAbstractRubberBand
\r
13 \brief Analog of class QRubberBand with possibility of creation non-rectangular contour for selection.
\r
15 Currently this class does not support Style functionality in full.
\r
20 \param theParent parent widget
\r
23 XGUI_AbstractRubberBand::XGUI_AbstractRubberBand(QWidget* theParent)
\r
24 : QWidget(theParent), myPoints(), myIsClosed(false)
\r
26 setAttribute(Qt::WA_TransparentForMouseEvents);
\r
28 setAttribute(Qt::WA_NoSystemBackground);
\r
29 #endif //WIN32
\r setAttribute(Qt::WA_WState_ExplicitShowHide);
\r setVisible(false);
\r theParent->installEventFilter(this);
\r
30 setGeometry(QRect(QPoint(0, 0), theParent->size()));
\r
36 XGUI_AbstractRubberBand::~XGUI_AbstractRubberBand()
\r
40 void XGUI_AbstractRubberBand::clearGeometry()
\r
45 bool XGUI_AbstractRubberBand::isClosed()
\r
50 void XGUI_AbstractRubberBand::paintEvent(QPaintEvent* theEvent)
\r
52 if (!myPoints.empty()) {
\r
53 QPixmap tiledPixmap(16, 16);
\r
55 QPainter pixmapPainter(&tiledPixmap);
\r
56 pixmapPainter.setPen(Qt::NoPen);
\r
57 pixmapPainter.setBrush(QBrush(Qt::black, Qt::Dense4Pattern));
\r
58 pixmapPainter.setBackground(QBrush(Qt::white));
\r
59 pixmapPainter.setBackgroundMode(Qt::OpaqueMode);
\r
60 pixmapPainter.drawRect(0, 0, tiledPixmap.width(), tiledPixmap.height());
\r
61 pixmapPainter.end();
\r
62 // ### workaround for borked XRENDER
\r
63 tiledPixmap = QPixmap::fromImage(tiledPixmap.toImage());
\r
65 QPainter aPainter(this);
\r
66 aPainter.setRenderHint(QPainter::Antialiasing);
\r
67 QRect r = myPoints.boundingRect();
\r
68 aPainter.setClipRegion(r.normalized().adjusted(-1, -1, 2, 2));
\r
69 aPainter.drawTiledPixmap(0, 0, width(), height(), tiledPixmap);
\r
75 void XGUI_AbstractRubberBand::showEvent(QShowEvent* theEvent)
\r
81 void XGUI_AbstractRubberBand::moveEvent(QMoveEvent*)
\r
85 void XGUI_AbstractRubberBand::resizeEvent(QResizeEvent*)
\r
89 bool XGUI_AbstractRubberBand::eventFilter(QObject* obj, QEvent* e)
\r
91 if (obj && obj == parent() && e->type() == QEvent::Resize) {
\r
92 QWidget* p = (QWidget*) parent();
\r
93 setGeometry(QRect(QPoint(0, 0), p->size()));
\r
95 return QWidget::eventFilter(obj, e);
\r
98 QRegion createRegion(const QPointF& p1, const QPointF& p2)
\r
103 QLineF n = QLineF(p1, p2).normalVector(); //.unitVector();
\r
105 n.translate(p1 * -1);
\r
106 QPointF nPoint = n.p2();
\r
109 p << p1 + nPoint << p2 + nPoint << p2 - nPoint << p1 - nPoint << p1 + nPoint;
\r
111 return QRegion(p.toPolygon());
\r
114 void XGUI_AbstractRubberBand::updateMask()
\r
118 QVectorIterator<QPoint> it(myPoints);
\r
119 while(it.hasNext()) {
\r
120 QPoint p = it.next();
\r
124 QPoint np = it.peekNext();
\r
129 r += createRegion(p, np);
\r
133 r += createRegion(myPoints.last(), myPoints.first());
\r
138 //**********************************************************
\r
139 XGUI_RectRubberBand::XGUI_RectRubberBand(QWidget* parent)
\r
140 : XGUI_AbstractRubberBand(parent)
\r
142 myPoints.resize(4);
\r
146 XGUI_RectRubberBand::~XGUI_RectRubberBand()
\r
150 void XGUI_RectRubberBand::initGeometry(const QRect& theRect)
\r
153 myPoints << theRect.topLeft() << theRect.topRight() << theRect.bottomRight()
\r
154 << theRect.bottomLeft();
\r
155 //setMask( QRegion( myPoints ) );
\r
159 void XGUI_RectRubberBand::setStartPoint(const QPoint& thePoint)
\r
161 myPoints[0] = thePoint;
\r
162 myPoints[1].setY(thePoint.y());
\r
163 myPoints[3].setX(thePoint.x());
\r
167 void XGUI_RectRubberBand::setEndPoint(const QPoint& thePoint)
\r
169 myPoints[2] = thePoint;
\r
170 myPoints[1].setX(thePoint.x());
\r
171 myPoints[3].setY(thePoint.y());
\r
175 void XGUI_RectRubberBand::clearGeometry()
\r
177 QMutableVectorIterator<QPoint> i(myPoints);
\r
178 while(i.hasNext()) {
\r
180 i.setValue(QPoint(-1, -1));
\r
184 //**********************************************************
\r
185 XGUI_PolyRubberBand::XGUI_PolyRubberBand(QWidget* parent)
\r
186 : XGUI_AbstractRubberBand(parent)
\r
190 XGUI_PolyRubberBand::~XGUI_PolyRubberBand()
\r
194 void XGUI_PolyRubberBand::initGeometry(const QPolygon& thePoints)
\r
196 myPoints = thePoints;
\r
200 void XGUI_PolyRubberBand::initGeometry(const QPoint& thePoint)
\r
203 myPoints << thePoint;
\r
207 void XGUI_PolyRubberBand::addNode(const QPoint& thePoint)
\r
209 myPoints << thePoint;
\r
213 void XGUI_PolyRubberBand::replaceLastNode(const QPoint& thePoint)
\r
215 if (!myPoints.empty()) {
\r
216 myPoints.pop_back();
\r
217 myPoints << thePoint;
\r
222 void XGUI_PolyRubberBand::removeLastNode()
\r
224 if (!myPoints.empty()) {
\r
225 myPoints.pop_back();
\r
230 void XGUI_PolyRubberBand::setClosed(bool theFlag)
\r
232 if (myIsClosed != theFlag) {
\r
233 myIsClosed = theFlag;
\r