annotate demos/shared/hoverpoints.d @ 381:347e4c7a9ba1

make QwtD compile on Windows@
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Sun, 11 Jul 2010 01:59:42 +0100
parents 96a75b1e5b26
children
rev   line source
maxter@41 1 /****************************************************************************
maxter@41 2 **
maxter@41 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
maxter@41 4 ** Contact: Qt Software Information (qt-info@nokia.com)
maxter@41 5 **
maxter@41 6 ** This file is part of the demonstration applications of the Qt Toolkit.
maxter@41 7 **
maxter@41 8 ** $QT_BEGIN_LICENSE:LGPL$
maxter@41 9 ** Commercial Usage
maxter@41 10 ** Licensees holding valid Qt Commercial licenses may use this file in
maxter@41 11 ** accordance with the Qt Commercial License Agreement provided with the
maxter@41 12 ** Software or, alternatively, in accordance with the terms contained in
maxter@41 13 ** a written agreement between you and Nokia.
maxter@41 14 **
maxter@41 15 ** GNU Lesser General Public License Usage
maxter@41 16 ** Alternatively, this file may be used under the terms of the GNU Lesser
maxter@41 17 ** General Public License version 2.1 as published by the Free Software
maxter@41 18 ** Foundation and appearing in the file LICENSE.LGPL included in the
maxter@41 19 ** packaging of this file. Please review the following information to
maxter@41 20 ** ensure the GNU Lesser General Public License version 2.1 requirements
maxter@41 21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
maxter@41 22 **
maxter@41 23 ** In addition, as a special exception, Nokia gives you certain
maxter@41 24 ** additional rights. These rights are described in the Nokia Qt LGPL
maxter@41 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
maxter@41 26 ** package.
maxter@41 27 **
maxter@41 28 ** GNU General Public License Usage
maxter@41 29 ** Alternatively, this file may be used under the terms of the GNU
maxter@41 30 ** General Public License version 3.0 as published by the Free Software
maxter@41 31 ** Foundation and appearing in the file LICENSE.GPL included in the
maxter@41 32 ** packaging of this file. Please review the following information to
maxter@41 33 ** ensure the GNU General Public License version 3.0 requirements will be
maxter@41 34 ** met: http://www.gnu.org/copyleft/gpl.html.
maxter@41 35 **
maxter@41 36 ** If you are unsure which license is appropriate for your use, please
maxter@41 37 ** contact the sales department at qt-sales@nokia.com.
maxter@41 38 ** $QT_END_LICENSE$
maxter@41 39 **
maxter@41 40 ****************************************************************************/
mwarning@162 41 module hoverpoints;
maxter@41 42
maxter@41 43 version (QT_OPENGL_SUPPORT)
maxter@41 44 import qt.opengl.QGLWidget;
maxter@41 45
maxter@41 46 version (D_Version2) {}
maxter@41 47 else
maxter@41 48 import tango.core.Array : sort;
maxter@41 49
maxter@41 50 import
maxter@41 51 qt.gui.QWidget,
maxter@344 52 qtd.Array,
maxter@41 53 arthurwidgets;
maxter@41 54
maxter@41 55 final class HoverPoints : QObject
maxter@41 56 {
maxter@41 57 public:
maxter@41 58 enum PointShape {
maxter@41 59 CircleShape,
maxter@41 60 RectangleShape
maxter@41 61 }
maxter@41 62
maxter@41 63 enum LockType {
maxter@41 64 LockToLeft = 0x01,
maxter@41 65 LockToRight = 0x02,
maxter@41 66 LockToTop = 0x04,
maxter@41 67 LockToBottom = 0x08
maxter@41 68 }
maxter@41 69
maxter@41 70 enum SortType {
maxter@41 71 NoSort,
maxter@41 72 XSort,
maxter@41 73 YSort
maxter@41 74 }
maxter@41 75
maxter@41 76 enum ConnectionType {
maxter@41 77 NoConnection,
maxter@41 78 LineConnection,
maxter@41 79 CurveConnection
maxter@41 80 }
maxter@41 81
maxter@41 82 private:
maxter@41 83 QWidget m_widget;
maxter@41 84
maxter@41 85 QPolygonF m_points;
maxter@41 86 QRectF m_bounds;
maxter@41 87 PointShape m_shape;
maxter@41 88 SortType m_sortType;
maxter@41 89 ConnectionType m_connectionType;
maxter@41 90
maxter@41 91 uint[] m_locks;
maxter@41 92
maxter@41 93 QSizeF m_pointSize;
maxter@41 94 int m_currentIndex;
maxter@41 95 bool m_editable;
maxter@41 96 bool m_enabled;
maxter@41 97
maxter@41 98 QPen m_pointPen;
maxter@41 99 QBrush m_pointBrush;
maxter@41 100 QPen m_connectionPen;
maxter@41 101
maxter@41 102 public:
maxter@41 103 mixin Signal!("pointsChanged", QPolygonF /*points*/);
maxter@41 104
maxter@41 105 this(QWidget widget, PointShape shape)
maxter@41 106 {
maxter@41 107 super(widget);
maxter@41 108
maxter@41 109 m_widget = widget;
maxter@41 110 widget.installEventFilter(this);
maxter@41 111
maxter@41 112 m_connectionType = ConnectionType.CurveConnection;
maxter@41 113 m_sortType = SortType.NoSort;
maxter@41 114 m_shape = shape;
maxter@41 115 m_pointPen = new QPen(new QBrush(new QColor(255, 255, 255, 191)), 1);
maxter@41 116 m_connectionPen = new QPen(new QBrush(new QColor(255, 255, 255, 127)), 2);
maxter@41 117 m_pointBrush = new QBrush(new QColor(191, 191, 191, 127));
maxter@41 118 m_pointSize = QSizeF(11, 11);
maxter@41 119 m_currentIndex = -1;
maxter@41 120 m_editable = true;
maxter@41 121 m_enabled = true;
maxter@41 122
maxter@41 123 pointsChanged.connect(&m_widget.update);
maxter@41 124 }
maxter@41 125
maxter@41 126 void setBoundingRect(QRectF boundingRect) { m_bounds = boundingRect; }
maxter@41 127
maxter@41 128
maxter@41 129 QRectF pointBoundingRect(int i)
maxter@41 130 {
maxter@41 131 QPointF p = m_points.at(i);
maxter@41 132 qreal w = m_pointSize.width();
maxter@41 133 qreal h = m_pointSize.height();
maxter@41 134 qreal x = p.x() - w / 2;
maxter@41 135 qreal y = p.y() - h / 2;
maxter@41 136 return new QRectF(x, y, w, h);
maxter@41 137 }
maxter@41 138
maxter@41 139 QRectF boundingRect()
maxter@41 140 {
maxter@41 141 if (m_bounds.isEmpty())
maxter@41 142 return new QRectF(m_widget.rect());
maxter@41 143 else
maxter@41 144 return m_bounds;
maxter@41 145 }
maxter@41 146
maxter@41 147 QPolygonF points() { return m_points; }
maxter@41 148
maxter@41 149 QSizeF pointSize() { return m_pointSize; }
maxter@41 150 void setPointSize(QSizeF size) { m_pointSize = size; }
maxter@41 151
maxter@41 152 SortType sortType() { return m_sortType; }
maxter@41 153 void setSortType(SortType sortType) { m_sortType = sortType; }
maxter@41 154
maxter@41 155 ConnectionType connectionType() { return m_connectionType; }
maxter@41 156 void setConnectionType(ConnectionType connectionType) { m_connectionType = connectionType; }
maxter@41 157
maxter@41 158 void setConnectionPen(QPen pen) { m_connectionPen = pen; }
maxter@41 159 void setShapePen(QPen pen) { m_pointPen = pen; }
maxter@41 160 void setShapeBrush(QBrush brush) { m_pointBrush = brush; }
maxter@41 161
maxter@41 162 void setPointLock(int pos, LockType lock) { m_locks[pos] = lock; }
maxter@41 163
maxter@41 164 void setEditable(bool editable) { m_editable = editable; }
maxter@41 165 bool editable() { return m_editable; }
maxter@41 166
maxter@41 167 void setEnabled(bool enabled)
maxter@41 168 {
maxter@41 169 if (m_enabled != enabled) {
maxter@41 170 m_enabled = enabled;
maxter@41 171 m_widget.update();
maxter@41 172 }
maxter@41 173 }
maxter@41 174
maxter@41 175
maxter@41 176 override bool eventFilter(QObject object, QEvent event)
maxter@41 177 {
maxter@41 178 if ((object == m_widget) && m_enabled) {
maxter@41 179 switch (event.type()) {
maxter@41 180
maxter@41 181 case QEvent.MouseButtonPress:
maxter@41 182 {
maxter@41 183 QMouseEvent me = cast(QMouseEvent) event;
maxter@41 184
maxter@41 185 QPointF clickPos = me.pos();
maxter@41 186 int index = -1;
maxter@41 187 for (int i=0; i<m_points.size(); ++i) {
maxter@41 188 auto path = new QPainterPath;
maxter@41 189 if (m_shape == PointShape.CircleShape)
maxter@41 190 path.addEllipse(pointBoundingRect(i));
maxter@41 191 else
maxter@41 192 path.addRect(pointBoundingRect(i));
maxter@41 193
maxter@41 194 if (path.contains(clickPos)) {
maxter@41 195 index = i;
maxter@41 196 break;
maxter@41 197 }
maxter@41 198 }
maxter@41 199
maxter@41 200 if (me.button() == Qt.LeftButton) {
maxter@41 201 if (index == -1) {
maxter@41 202 if (!m_editable)
maxter@41 203 return false;
maxter@41 204 int pos = 0;
maxter@41 205 // Insert sort for x or y
maxter@41 206 if (m_sortType == SortType.XSort) {
maxter@41 207 for (int i=0; i<m_points.size(); ++i)
maxter@41 208 if (m_points.at(i).x() > clickPos.x()) {
maxter@41 209 pos = i;
maxter@41 210 break;
maxter@41 211 }
maxter@41 212 } else if (m_sortType == SortType.YSort) {
maxter@41 213 for (int i=0; i<m_points.size(); ++i)
maxter@41 214 if (m_points.at(i).y() > clickPos.y()) {
maxter@41 215 pos = i;
maxter@41 216 break;
maxter@41 217 }
maxter@41 218 }
maxter@41 219
maxter@41 220 // TODO: implement QPoligon(F).insert
maxter@41 221 auto tmpPoints = m_points.toList;
maxter@41 222 tmpPoints.insert(pos, clickPos);
maxter@41 223 m_points = new QPolygonF(tmpPoints);
maxter@41 224
maxter@41 225 m_locks.insert(pos, 0u);
maxter@41 226 m_currentIndex = pos;
maxter@41 227 firePointChange();
maxter@41 228 } else {
maxter@41 229 m_currentIndex = index;
maxter@41 230 }
maxter@41 231 return true;
maxter@41 232
maxter@41 233 } else if (me.button() == Qt.RightButton) {
maxter@41 234 if ((index >= 0) && m_editable) {
maxter@41 235 if (m_locks[index] == 0) {
maxter@41 236 m_locks.removeAt(index);
maxter@41 237 m_points.remove(index);
maxter@41 238 }
maxter@41 239 firePointChange();
maxter@41 240 return true;
maxter@41 241 }
maxter@41 242 }
maxter@41 243
maxter@41 244 }
maxter@41 245 break;
maxter@41 246
maxter@41 247 case QEvent.MouseButtonRelease:
maxter@41 248 m_currentIndex = -1;
maxter@41 249 break;
maxter@41 250
maxter@41 251 case QEvent.MouseMove:
maxter@41 252 if (m_currentIndex >= 0)
maxter@41 253 movePoint(m_currentIndex, QPointF((cast(QMouseEvent)event).pos));
maxter@41 254 break;
maxter@41 255
maxter@41 256 case QEvent.Resize:
maxter@41 257 {
maxter@41 258 QResizeEvent e = cast(QResizeEvent) event;
maxter@41 259 if (e.oldSize().width() == 0 || e.oldSize().height() == 0)
maxter@41 260 break;
maxter@41 261 qreal stretch_x = e.size().width() / cast(qreal)e.oldSize.width;
maxter@41 262 qreal stretch_y = e.size().height() / cast(qreal)e.oldSize.height;
maxter@41 263 for (int i=0; i<m_points.size(); ++i) {
maxter@41 264 QPointF p = m_points.at(i);
maxter@41 265 movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false);
maxter@41 266 }
maxter@41 267
maxter@41 268 firePointChange();
maxter@41 269 break;
maxter@41 270 }
maxter@41 271
maxter@41 272 case QEvent.Paint:
maxter@41 273 {
maxter@41 274 QWidget that_widget = m_widget;
maxter@41 275 m_widget = null;
maxter@41 276 QApplication.sendEvent(object, event);
maxter@41 277 m_widget = that_widget;
maxter@41 278 paintPoints();
maxter@41 279 version (QT_OPENGL_SUPPORT)
maxter@41 280 {
maxter@41 281 ArthurFrame af = cast(ArthurFrame)(that_widget);
maxter@41 282 if (af && af.usesOpenGL())
maxter@41 283 af.glWidget().swapBuffers();
maxter@41 284 }
maxter@41 285
maxter@41 286 return true;
maxter@41 287 }
maxter@41 288 default:
maxter@41 289 break;
maxter@41 290 }
maxter@41 291 }
maxter@41 292
maxter@41 293 return false;
maxter@41 294 }
maxter@41 295
maxter@41 296
maxter@41 297 void paintPoints()
maxter@41 298 {
maxter@41 299 scope p = new QPainter;
maxter@41 300 version (QT_OPENGL_SUPPORT)
maxter@41 301 {
maxter@41 302 ArthurFrame af = cast(ArthurFrame)(m_widget);
maxter@41 303 if (af && af.usesOpenGL())
maxter@41 304 p.begin(af.glWidget());
maxter@41 305 else
maxter@41 306 p.begin(m_widget);
maxter@41 307 }
maxter@41 308 else
maxter@41 309 p.begin(m_widget);
maxter@41 310
maxter@41 311 p.setRenderHint(QPainter.Antialiasing);
maxter@41 312
maxter@41 313 if (m_connectionPen.style() != Qt.NoPen && m_connectionType != ConnectionType.NoConnection) {
maxter@41 314 p.setPen(m_connectionPen);
maxter@41 315
maxter@41 316 if (m_connectionType == ConnectionType.CurveConnection) {
maxter@41 317 auto path = new QPainterPath;
maxter@41 318 path.moveTo(m_points.at(0));
maxter@41 319 for (int i=1; i<m_points.size(); ++i) {
maxter@41 320 QPointF p1 = m_points.at(i-1);
maxter@41 321 QPointF p2 = m_points.at(i);
maxter@41 322 qreal distance = p2.x() - p1.x();
maxter@41 323
maxter@41 324 path.cubicTo(p1.x() + distance / 2, p1.y(),
maxter@41 325 p1.x() + distance / 2, p2.y(),
maxter@41 326 p2.x(), p2.y());
maxter@41 327 }
maxter@41 328 p.drawPath(path);
maxter@41 329 } else {
maxter@41 330 p.drawPolyline(m_points);
maxter@41 331 }
maxter@41 332 }
maxter@41 333
maxter@41 334 p.setPen(m_pointPen);
maxter@41 335 p.setBrush(m_pointBrush);
maxter@41 336
maxter@41 337 for (int i=0; i<m_points.size(); ++i) {
maxter@41 338 QRectF bounds = pointBoundingRect(i);
maxter@41 339 if (m_shape == PointShape.CircleShape)
maxter@41 340 p.drawEllipse(bounds);
maxter@41 341 else
maxter@41 342 p.drawRect(bounds);
maxter@41 343 }
maxter@41 344 }
maxter@41 345
maxter@41 346
maxter@41 347 void setPoints(QPolygonF points)
maxter@41 348 {
maxter@41 349 delete m_points;
maxter@41 350 for (int i=0; i<points.size; ++i)
maxter@41 351 m_points.append(bound_point(points.at(i), boundingRect(), 0));
maxter@41 352
maxter@41 353 delete m_locks;
maxter@41 354 if (m_points.size > 0) {
maxter@41 355 m_locks.length = m_points.size;
maxter@41 356
maxter@41 357 m_locks[] = 0;
maxter@41 358 }
maxter@41 359 }
maxter@41 360
maxter@41 361 void movePoint(int index, QPointF point, bool emitUpdate = true)
maxter@41 362 {
maxter@41 363 m_points.replace(index, bound_point(point, boundingRect(), m_locks[index]));
maxter@41 364 if (emitUpdate)
maxter@41 365 firePointChange();
maxter@41 366 }
maxter@41 367
maxter@41 368 void firePointChange()
maxter@41 369 {
maxter@41 370 // printf("HoverPoints.firePointChange(), current=%d\n", m_currentIndex);
maxter@41 371
maxter@41 372 if (m_sortType != SortType.NoSort) {
maxter@41 373
maxter@41 374 QPointF oldCurrent;
maxter@41 375 if (m_currentIndex != -1) {
maxter@41 376 oldCurrent = m_points.at(m_currentIndex);
maxter@41 377 }
maxter@41 378
maxter@41 379 if (m_sortType == SortType.XSort)
maxter@41 380 {
maxter@41 381 auto tmpPoints = m_points.toList;
maxter@41 382 sort(tmpPoints, &x_less_than);
maxter@41 383 m_points = new QPolygonF(tmpPoints);
maxter@41 384 }
maxter@41 385 else if (m_sortType == SortType.YSort)
maxter@41 386 {
maxter@41 387 auto tmpPoints = m_points.toList;
maxter@41 388 sort(tmpPoints, &y_less_than);
maxter@41 389 m_points = new QPolygonF(tmpPoints);
maxter@41 390 }
maxter@41 391
maxter@41 392 // Compensate for changed order...
maxter@41 393 if (m_currentIndex != -1) {
maxter@41 394 for (int i=0; i<m_points.size; ++i) {
maxter@41 395 if (m_points.at(i) == oldCurrent) {
maxter@41 396 m_currentIndex = i;
maxter@41 397 break;
maxter@41 398 }
maxter@41 399 }
maxter@41 400 }
maxter@41 401
maxter@41 402 // printf(" - firePointChange(), current=%d\n", m_currentIndex);
maxter@41 403 }
maxter@41 404
maxter@41 405 // for (int i=0; i<m_points.size(); ++i) {
maxter@41 406 // printf(" - point(%2d)=[%.2f, %.2f], lock=%d\n",
maxter@41 407 // i, m_points.at(i).x(), m_points.at(i).y(), m_locks.at(i));
maxter@41 408 // }
maxter@41 409
maxter@41 410 pointsChanged.emit(m_points);
maxter@41 411 }
maxter@41 412 }
maxter@41 413
maxter@41 414 private QPointF bound_point(QPointF point, QRectF bounds, int lock)
maxter@41 415 {
maxter@41 416 QPointF p = point;
maxter@41 417
maxter@41 418 qreal left = bounds.left();
maxter@41 419 qreal right = bounds.right();
maxter@41 420 qreal top = bounds.top();
maxter@41 421 qreal bottom = bounds.bottom();
maxter@41 422
maxter@41 423 if (p.x() < left || (lock & HoverPoints.LockType.LockToLeft)) p.x = left;
maxter@41 424 else if (p.x() > right || (lock & HoverPoints.LockType.LockToRight)) p.x = right;
maxter@41 425
maxter@41 426 if (p.y() < top || (lock & HoverPoints.LockType.LockToTop)) p.y = top;
maxter@41 427 else if (p.y() > bottom || (lock & HoverPoints.LockType.LockToBottom)) p.y = bottom;
maxter@41 428
maxter@41 429 return p;
maxter@41 430 }
maxter@41 431
maxter@41 432 private bool x_less_than(QPointF p1, QPointF p2)
maxter@41 433 {
maxter@41 434 return p1.x() < p2.x();
maxter@41 435 }
maxter@41 436
maxter@41 437 private bool y_less_than(QPointF p1, QPointF p2)
maxter@41 438 {
maxter@41 439 return p1.y() < p2.y();
maxter@41 440 }