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),
\r
28 setAttribute(Qt::WA_TransparentForMouseEvents);
\r
30 setAttribute(Qt::WA_NoSystemBackground);
\r
32 setAttribute(Qt::WA_WState_ExplicitShowHide);
\r
34 theParent->installEventFilter(this);
\r
35 setGeometry(QRect(QPoint(0, 0), theParent->size()));
\r
41 XGUI_AbstractRubberBand::~XGUI_AbstractRubberBand()
\r
45 void XGUI_AbstractRubberBand::clearGeometry()
\r
50 bool XGUI_AbstractRubberBand::isClosed()
\r
55 void XGUI_AbstractRubberBand::paintEvent(QPaintEvent* theEvent)
\r
57 if (!myPoints.empty()) {
\r
58 QPixmap tiledPixmap(16, 16);
\r
60 QPainter pixmapPainter(&tiledPixmap);
\r
61 pixmapPainter.setPen(Qt::NoPen);
\r
62 pixmapPainter.setBrush(QBrush(Qt::black, Qt::Dense4Pattern));
\r
63 pixmapPainter.setBackground(QBrush(Qt::white));
\r
64 pixmapPainter.setBackgroundMode(Qt::OpaqueMode);
\r
65 pixmapPainter.drawRect(0, 0, tiledPixmap.width(), tiledPixmap.height());
\r
66 pixmapPainter.end();
\r
67 // ### workaround for borked XRENDER
\r
68 tiledPixmap = QPixmap::fromImage(tiledPixmap.toImage());
\r
70 QPainter aPainter(this);
\r
71 aPainter.setRenderHint(QPainter::Antialiasing);
\r
72 QRect r = myPoints.boundingRect();
\r
73 aPainter.setClipRegion(r.normalized().adjusted(-1, -1, 2, 2));
\r
74 aPainter.drawTiledPixmap(0, 0, width(), height(), tiledPixmap);
\r
80 void XGUI_AbstractRubberBand::showEvent(QShowEvent* theEvent)
\r
86 void XGUI_AbstractRubberBand::moveEvent(QMoveEvent*)
\r
90 void XGUI_AbstractRubberBand::resizeEvent(QResizeEvent*)
\r
94 bool XGUI_AbstractRubberBand::eventFilter(QObject* obj, QEvent* e)
\r
96 if (obj && obj == parent() && e->type() == QEvent::Resize) {
\r
97 QWidget* p = (QWidget*) parent();
\r
98 setGeometry(QRect(QPoint(0, 0), p->size()));
\r
100 return QWidget::eventFilter(obj, e);
\r
103 QRegion createRegion(const QPointF& p1, const QPointF& p2)
\r
108 QLineF n = QLineF(p1, p2).normalVector(); //.unitVector();
\r
110 n.translate(p1 * -1);
\r
111 QPointF nPoint = n.p2();
\r
114 p << p1 + nPoint << p2 + nPoint << p2 - nPoint << p1 - nPoint << p1 + nPoint;
\r
116 return QRegion(p.toPolygon());
\r
119 void XGUI_AbstractRubberBand::updateMask()
\r
123 QVectorIterator<QPoint> it(myPoints);
\r
124 while (it.hasNext()) {
\r
125 QPoint p = it.next();
\r
129 QPoint np = it.peekNext();
\r
134 r += createRegion(p, np);
\r
138 r += createRegion(myPoints.last(), myPoints.first());
\r
144 //**********************************************************
\r
145 XGUI_RectRubberBand::XGUI_RectRubberBand(QWidget* parent)
\r
146 : XGUI_AbstractRubberBand(parent)
\r
148 myPoints.resize(4);
\r
152 XGUI_RectRubberBand::~XGUI_RectRubberBand()
\r
156 void XGUI_RectRubberBand::initGeometry(const QRect& theRect)
\r
159 myPoints << theRect.topLeft() << theRect.topRight() << theRect.bottomRight()
\r
160 << theRect.bottomLeft();
\r
161 //setMask( QRegion( myPoints ) );
\r
165 void XGUI_RectRubberBand::setStartPoint(const QPoint& thePoint)
\r
167 myPoints[0] = thePoint;
\r
168 myPoints[1].setY(thePoint.y());
\r
169 myPoints[3].setX(thePoint.x());
\r
173 void XGUI_RectRubberBand::setEndPoint(const QPoint& thePoint)
\r
175 myPoints[2] = thePoint;
\r
176 myPoints[1].setX(thePoint.x());
\r
177 myPoints[3].setY(thePoint.y());
\r
181 void XGUI_RectRubberBand::clearGeometry()
\r
183 QMutableVectorIterator<QPoint> i(myPoints);
\r
184 while (i.hasNext()) {
\r
186 i.setValue(QPoint(-1, -1));
\r
190 //**********************************************************
\r
191 XGUI_PolyRubberBand::XGUI_PolyRubberBand(QWidget* parent)
\r
192 : XGUI_AbstractRubberBand(parent)
\r
196 XGUI_PolyRubberBand::~XGUI_PolyRubberBand()
\r
200 void XGUI_PolyRubberBand::initGeometry(const QPolygon& thePoints)
\r
202 myPoints = thePoints;
\r
206 void XGUI_PolyRubberBand::initGeometry(const QPoint& thePoint)
\r
209 myPoints << thePoint;
\r
213 void XGUI_PolyRubberBand::addNode(const QPoint& thePoint)
\r
215 myPoints << thePoint;
\r
219 void XGUI_PolyRubberBand::replaceLastNode(const QPoint& thePoint)
\r
221 if (!myPoints.empty()) {
\r
222 myPoints.pop_back();
\r
223 myPoints << thePoint;
\r
228 void XGUI_PolyRubberBand::removeLastNode()
\r
230 if (!myPoints.empty()) {
\r
231 myPoints.pop_back();
\r
236 void XGUI_PolyRubberBand::setClosed(bool theFlag)
\r
238 if (myIsClosed != theFlag) {
\r
239 myIsClosed = theFlag;
\r