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
31 #endif //WIN32
\r setAttribute(Qt::WA_WState_ExplicitShowHide);
\r setVisible(false);
\r theParent->installEventFilter(this);
\r setGeometry(QRect(QPoint(0, 0), theParent->size()));
\r
37 XGUI_AbstractRubberBand::~XGUI_AbstractRubberBand()
\r
41 void XGUI_AbstractRubberBand::clearGeometry()
\r
46 bool XGUI_AbstractRubberBand::isClosed()
\r
51 void XGUI_AbstractRubberBand::paintEvent(QPaintEvent* theEvent)
\r
53 if (!myPoints.empty()) {
\r
54 QPixmap tiledPixmap(16, 16);
\r
56 QPainter pixmapPainter(&tiledPixmap);
\r
57 pixmapPainter.setPen(Qt::NoPen);
\r
58 pixmapPainter.setBrush(QBrush(Qt::black, Qt::Dense4Pattern));
\r
59 pixmapPainter.setBackground(QBrush(Qt::white));
\r
60 pixmapPainter.setBackgroundMode(Qt::OpaqueMode);
\r
61 pixmapPainter.drawRect(0, 0, tiledPixmap.width(), tiledPixmap.height());
\r
62 pixmapPainter.end();
\r
63 // ### workaround for borked XRENDER
\r
64 tiledPixmap = QPixmap::fromImage(tiledPixmap.toImage());
\r
66 QPainter aPainter(this);
\r
67 aPainter.setRenderHint(QPainter::Antialiasing);
\r
68 QRect r = myPoints.boundingRect();
\r
69 aPainter.setClipRegion(r.normalized().adjusted(-1, -1, 2, 2));
\r
70 aPainter.drawTiledPixmap(0, 0, width(), height(), tiledPixmap);
\r
76 void XGUI_AbstractRubberBand::showEvent(QShowEvent* theEvent)
\r
82 void XGUI_AbstractRubberBand::moveEvent(QMoveEvent*)
\r
86 void XGUI_AbstractRubberBand::resizeEvent(QResizeEvent*)
\r
90 bool XGUI_AbstractRubberBand::eventFilter(QObject* obj, QEvent* e)
\r
92 if (obj && obj == parent() && e->type() == QEvent::Resize) {
\r
93 QWidget* p = (QWidget*) parent();
\r
94 setGeometry(QRect(QPoint(0, 0), p->size()));
\r
96 return QWidget::eventFilter(obj, e);
\r
99 QRegion createRegion(const QPointF& p1, const QPointF& p2)
\r
104 QLineF n = QLineF(p1, p2).normalVector(); //.unitVector();
\r
106 n.translate(p1 * -1);
\r
107 QPointF nPoint = n.p2();
\r
110 p << p1 + nPoint << p2 + nPoint << p2 - nPoint << p1 - nPoint << p1 + nPoint;
\r
112 return QRegion(p.toPolygon());
\r
115 void XGUI_AbstractRubberBand::updateMask()
\r
119 QVectorIterator<QPoint> it(myPoints);
\r
120 while (it.hasNext()) {
\r
121 QPoint p = it.next();
\r
125 QPoint np = it.peekNext();
\r
130 r += createRegion(p, np);
\r
134 r += createRegion(myPoints.last(), myPoints.first());
\r
139 //**********************************************************
\r
140 XGUI_RectRubberBand::XGUI_RectRubberBand(QWidget* parent)
\r
141 : XGUI_AbstractRubberBand(parent)
\r
143 myPoints.resize(4);
\r
147 XGUI_RectRubberBand::~XGUI_RectRubberBand()
\r
151 void XGUI_RectRubberBand::initGeometry(const QRect& theRect)
\r
154 myPoints << theRect.topLeft() << theRect.topRight() << theRect.bottomRight()
\r
155 << theRect.bottomLeft();
\r
156 //setMask( QRegion( myPoints ) );
\r
160 void XGUI_RectRubberBand::setStartPoint(const QPoint& thePoint)
\r
162 myPoints[0] = thePoint;
\r
163 myPoints[1].setY(thePoint.y());
\r
164 myPoints[3].setX(thePoint.x());
\r
168 void XGUI_RectRubberBand::setEndPoint(const QPoint& thePoint)
\r
170 myPoints[2] = thePoint;
\r
171 myPoints[1].setX(thePoint.x());
\r
172 myPoints[3].setY(thePoint.y());
\r
176 void XGUI_RectRubberBand::clearGeometry()
\r
178 QMutableVectorIterator<QPoint> i(myPoints);
\r
179 while (i.hasNext()) {
\r
181 i.setValue(QPoint(-1, -1));
\r
185 //**********************************************************
\r
186 XGUI_PolyRubberBand::XGUI_PolyRubberBand(QWidget* parent)
\r
187 : XGUI_AbstractRubberBand(parent)
\r
191 XGUI_PolyRubberBand::~XGUI_PolyRubberBand()
\r
195 void XGUI_PolyRubberBand::initGeometry(const QPolygon& thePoints)
\r
197 myPoints = thePoints;
\r
201 void XGUI_PolyRubberBand::initGeometry(const QPoint& thePoint)
\r
204 myPoints << thePoint;
\r
208 void XGUI_PolyRubberBand::addNode(const QPoint& thePoint)
\r
210 myPoints << thePoint;
\r
214 void XGUI_PolyRubberBand::replaceLastNode(const QPoint& thePoint)
\r
216 if (!myPoints.empty()) {
\r
217 myPoints.pop_back();
\r
218 myPoints << thePoint;
\r
223 void XGUI_PolyRubberBand::removeLastNode()
\r
225 if (!myPoints.empty()) {
\r
226 myPoints.pop_back();
\r
231 void XGUI_PolyRubberBand::setClosed(bool theFlag)
\r
233 if (myIsClosed != theFlag) {
\r
234 myIsClosed = theFlag;
\r