Shape2D.cpp 20.9 KB
Newer Older
1
2
3
4
5
6
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
//     NScD Oak Ridge National Laboratory, European Spallation Source
//     & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
Roman Tolchenov's avatar
Roman Tolchenov committed
7
#include "MantidQtWidgets/InstrumentView/Shape2D.h"
8

LamarMoore's avatar
LamarMoore committed
9
#include <QMouseEvent>
10
11
12
13
14
15
16
17
18
#include <QPainter>
#include <QPainterPath>
#include <QWheelEvent>

#include <QLine>
#include <QMap>

#include <algorithm>
#include <cmath>
LamarMoore's avatar
LamarMoore committed
19
#include <stdexcept>
20

21
22
23
24
25
26
27
28
29
namespace MantidQt {
namespace MantidWidgets {

// number of control points common for all shapes
const size_t Shape2D::NCommonCP = 4;
// size (== width/2 == height/2) of each control point
const double Shape2D::sizeCP = 3;

/**
LamarMoore's avatar
LamarMoore committed
30
31
32
 * Set default border color to red and fill color to default Qt color
 * (==QColor()).
 */
33
34
35
36
37
Shape2D::Shape2D()
    : m_color(Qt::red), m_fill_color(QColor()), m_scalable(true),
      m_editing(false), m_selected(false), m_visible(true) {}

/**
LamarMoore's avatar
LamarMoore committed
38
39
40
41
42
 * Calls virtual drawShape() method to draw the actial shape.
 * Draws bounding rect and control points if the shape is selected.
 *
 * @param painter :: QPainter used for drawing.
 */
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
void Shape2D::draw(QPainter &painter) const {
  if (!m_visible)
    return;
  painter.setPen(m_color);
  this->drawShape(painter);
  if (m_editing || m_selected) {
    QColor c(255, 255, 255, 100);
    painter.setPen(c);
    painter.drawRect(m_boundingRect.toQRectF());
    size_t np = NCommonCP;
    double rsize = 2;
    int alpha = 100;
    if (m_editing) {
      // if editing show all CP, make them bigger and opaque
      np = getNControlPoints();
      rsize = sizeCP;
      alpha = 255;
    }
    for (size_t i = 0; i < np; ++i) {
      QPointF p = painter.transform().map(getControlPoint(i));
      QRectF r(p - QPointF(rsize, rsize), p + QPointF(rsize, rsize));
      painter.save();
      painter.resetTransform();
      QColor c(255, 255, 255, alpha);
      painter.fillRect(r, c);
      r.adjust(-1, -1, 0, 0);
      painter.setPen(QColor(0, 0, 0, alpha));
      painter.drawRect(r);
      painter.restore();
    }
  }
}

/**
LamarMoore's avatar
LamarMoore committed
77
78
 * Return total number of control points for this shape.
 */
79
80
81
82
83
size_t Shape2D::getNControlPoints() const {
  return NCommonCP + this->getShapeNControlPoints();
}

/**
LamarMoore's avatar
LamarMoore committed
84
85
86
87
 * Return coordinates of i-th control point.
 *
 * @param i :: Index of a control point. 0 <= i < getNControlPoints().
 */
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
QPointF Shape2D::getControlPoint(size_t i) const {
  if (i >= getNControlPoints()) {
    throw std::range_error("Control point index is out of range");
  }

  if (i < 4)
    return m_boundingRect.vertex(i);

  return getShapeControlPoint(i - NCommonCP);
}

void Shape2D::setControlPoint(size_t i, const QPointF &pos) {
  if (i >= getNControlPoints()) {
    throw std::range_error("Control point index is out of range");
  }

  if (i < 4) {
    m_boundingRect.setVertex(i, pos);
    refit();
  }
  // else ?
  else
    setShapeControlPoint(i - NCommonCP, pos);
  resetBoundingRect();
}

/**
LamarMoore's avatar
LamarMoore committed
115
116
117
118
 * Move the shape.
 *
 * @param dp :: The shift vector.
 */
119
120
121
122
123
124
void Shape2D::moveBy(const QPointF &dp) {
  m_boundingRect.translate(dp);
  refit();
}

/**
LamarMoore's avatar
LamarMoore committed
125
126
127
 * Adjust the bound of the bounding rect. Calls virtual method refit()
 * to resize the shape in order to fit into the new bounds.
 */
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
void Shape2D::adjustBoundingRect(double dx1, double dy1, double dx2,
                                 double dy2) {
  double dwidth = dx2 - dx1;
  if (dwidth <= -m_boundingRect.xSpan()) {
    double mu = m_boundingRect.xSpan() / fabs(dwidth);
    dx1 *= mu;
    dx2 *= mu;
  }
  double dheight = dy2 - dy1;
  if (dheight <= -m_boundingRect.ySpan()) {
    double mu = m_boundingRect.ySpan() / fabs(dheight);
    dy1 *= mu;
    dy2 *= mu;
  }
  m_boundingRect.adjust(QPointF(dx1, dy1), QPointF(dx2, dy2));
  refit();
}

/**
LamarMoore's avatar
LamarMoore committed
147
148
149
 * Assign new bounding rect. Calls virtual method refit()
 * to resize the shape in order to fit into the new bounds.
 */
150
151
152
153
154
155
void Shape2D::setBoundingRect(const RectF &rect) {
  m_boundingRect = rect;
  refit();
}

/**
LamarMoore's avatar
LamarMoore committed
156
157
158
159
 * Check if the shape masks a point.
 *
 * @param p :: Point to check.
 */
160
161
162
163
bool Shape2D::isMasked(const QPointF &p) const {
  return m_fill_color != QColor() && contains(p);
}

164
165
166
167
/** Load shape 2D state from a Mantid project file
 * @param lines :: lines from the project file to load state from
 * @return a new shape2D with old state applied
 */
168
Shape2D *Shape2D::loadFromProject(const std::string &lines) {
169
  API::TSVSerialiser tsv(lines);
Samuel Jackson's avatar
Samuel Jackson committed
170

171
172
  if (!tsv.selectLine("Type"))
    return nullptr;
Samuel Jackson's avatar
Samuel Jackson committed
173

174
175
  std::string type;
  tsv >> type;
Samuel Jackson's avatar
Samuel Jackson committed
176

177
178
179
  Shape2D *shape = loadShape2DFromType(type, lines);
  if (!shape)
    return nullptr;
Samuel Jackson's avatar
Samuel Jackson committed
180

181
182
183
  if (tsv.selectLine("Properties")) {
    bool scalable, editing, selected, visible;
    tsv >> scalable >> editing >> selected >> visible;
Samuel Jackson's avatar
Samuel Jackson committed
184

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    shape->setScalable(scalable);
    shape->edit(editing);
    shape->setSelected(selected);
    shape->setVisible(visible);
  }

  if (tsv.selectLine("Color")) {
    QColor color;
    tsv >> color;
    shape->setColor(color);
  }

  if (tsv.selectLine("FillColor")) {
    QColor color;
    tsv >> color;
    shape->setFillColor(color);
Samuel Jackson's avatar
Samuel Jackson committed
201
  }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

  return shape;
}

/**
 * Instantiate different types of Shape2D from a string
 *
 * @param type :: a string representing the type e.g. ellipse
 * @param lines :: Mantid project lines to parse state from
 * @return a new instance of a Shape2D
 */
Shape2D *Shape2D::loadShape2DFromType(const std::string &type,
                                      const std::string &lines) {
  Shape2D *shape = nullptr;

  if (type == "ellipse") {
    shape = Shape2DEllipse::loadFromProject(lines);
  } else if (type == "rectangle") {
    shape = Shape2DRectangle::loadFromProject(lines);
  } else if (type == "ring") {
    shape = Shape2DRing::loadFromProject(lines);
  } else if (type == "free") {
    shape = Shape2DFree::loadFromProject(lines);
  }

Samuel Jackson's avatar
Samuel Jackson committed
227
228
229
  return shape;
}

230
231
232
/** Save the state of the shape 2D to a Mantid project file
 * @return a string representing the state of the shape 2D
 */
233
std::string Shape2D::saveToProject() const {
234
  API::TSVSerialiser tsv;
235
  bool props[]{m_scalable, m_editing, m_selected, m_visible};
Samuel Jackson's avatar
Samuel Jackson committed
236
237

  tsv.writeLine("Properties");
238
  for (auto prop : props) {
Samuel Jackson's avatar
Samuel Jackson committed
239
240
241
242
    tsv << prop;
  }

  auto color = getColor();
243
  tsv.writeLine("Color") << color;
Samuel Jackson's avatar
Samuel Jackson committed
244
245

  auto fillColor = getFillColor();
246
  tsv.writeLine("FillColor") << fillColor;
Samuel Jackson's avatar
Samuel Jackson committed
247
248
249
250

  return tsv.outputLines();
}

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// --- Shape2DEllipse --- //

Shape2DEllipse::Shape2DEllipse(const QPointF &center, double radius1,
                               double radius2)
    : Shape2D() {
  if (radius2 == 0) {
    radius2 = radius1;
  }
  QPointF dr(radius1, radius2);
  m_boundingRect = RectF(center - dr, center + dr);
}

void Shape2DEllipse::drawShape(QPainter &painter) const {
  QRectF drawRect = m_boundingRect.toQRectF();
  painter.drawEllipse(drawRect);
  if (m_fill_color != QColor()) {
    QPainterPath path;
    path.addEllipse(drawRect);
    painter.fillPath(path, m_fill_color);
  }
}

void Shape2DEllipse::addToPath(QPainterPath &path) const {
  path.addEllipse(m_boundingRect.toQRectF());
}

bool Shape2DEllipse::selectAt(const QPointF &p) const {
  if (m_fill_color != QColor()) { // filled ellipse
    return contains(p);
  }

  double a = m_boundingRect.xSpan() / 2;
  if (a == 0.0)
    a = 1.0;
  double b = m_boundingRect.ySpan() / 2;
  if (b == 0.0)
    b = 1.0;
  double xx = m_boundingRect.x0() + a - double(p.x());
  double yy = m_boundingRect.y0() + b - double(p.y());

  double f = fabs(xx * xx / (a * a) + yy * yy / (b * b) - 1);

  return f < 0.1;
}

bool Shape2DEllipse::contains(const QPointF &p) const {
  if (m_boundingRect.isEmpty())
    return false;
  QPointF pp = m_boundingRect.center() - p;
  double a = m_boundingRect.xSpan() / 2;
  if (a == 0.0)
    a = 1.0;
  double b = m_boundingRect.ySpan() / 2;
  if (b == 0.0)
    b = 1.0;
  double xx = pp.x();
  double yy = pp.y();

  double f = xx * xx / (a * a) + yy * yy / (b * b);

  return f <= 1.0;
}

QStringList Shape2DEllipse::getDoubleNames() const {
  QStringList res;
  res << "radius1"
      << "radius2";
  return res;
}

double Shape2DEllipse::getDouble(const QString &prop) const {
  if (prop == "radius1") {
    return m_boundingRect.width() / 2;
  } else if (prop == "radius2") {
    return m_boundingRect.height() / 2;
  }
  return 0.0;
}

void Shape2DEllipse::setDouble(const QString &prop, double value) {
  if (prop == "radius1") {
    if (value <= 0.0)
      value = 1.0;
    double d = value - m_boundingRect.width() / 2;
    adjustBoundingRect(-d, 0, d, 0);
  } else if (prop == "radius2") {
    if (value <= 0.0)
      value = 1.0;
    double d = value - m_boundingRect.height() / 2;
    adjustBoundingRect(0, -d, 0, d);
  }
}

QPointF Shape2DEllipse::getPoint(const QString &prop) const {
  if (prop == "center" || prop == "centre") {
    return m_boundingRect.center();
  }
  return QPointF();
}

void Shape2DEllipse::setPoint(const QString &prop, const QPointF &value) {
  if (prop == "center" || prop == "centre") {
    m_boundingRect.moveCenter(value);
  }
}

357
358
359
360
/** Load shape 2D state from a Mantid project file
 * @param lines :: lines from the project file to load state from
 * @return a new shape2D in the shape of a ellipse
 */
361
Shape2D *Shape2DEllipse::loadFromProject(const std::string &lines) {
362
  API::TSVSerialiser tsv(lines);
Samuel Jackson's avatar
Samuel Jackson committed
363
364
365
366
367
368
  tsv.selectLine("Parameters");
  double radius1, radius2, x, y;
  tsv >> radius1 >> radius2 >> x >> y;
  return new Shape2DEllipse(QPointF(x, y), radius1, radius2);
}

369
370
371
/** Save the state of the shape 2D ellipe to a Mantid project file
 * @return a string representing the state of the shape 2D
 */
372
std::string Shape2DEllipse::saveToProject() const {
373
  API::TSVSerialiser tsv;
Samuel Jackson's avatar
Samuel Jackson committed
374
375
376
377
378
379
380
381
382
  double radius1 = getDouble("radius1");
  double radius2 = getDouble("radius2");
  auto centre = getPoint("centre");

  tsv.writeLine("Type") << "ellipse";
  tsv.writeLine("Parameters") << radius1 << radius2 << centre.x(), centre.y();
  tsv.writeRaw(Shape2D::saveToProject());
  return tsv.outputLines();
}
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// --- Shape2DRectangle --- //

Shape2DRectangle::Shape2DRectangle() { m_boundingRect = RectF(); }

Shape2DRectangle::Shape2DRectangle(const QPointF &p0, const QPointF &p1) {
  m_boundingRect = RectF(p0, p1);
}

Shape2DRectangle::Shape2DRectangle(const QPointF &p0, const QSizeF &size) {
  m_boundingRect = RectF(p0, size);
}

bool Shape2DRectangle::selectAt(const QPointF &p) const {
  if (m_fill_color != QColor()) { // filled rectangle
    return contains(p);
  }

  RectF outer(m_boundingRect);
  outer.adjust(QPointF(-2, -2), QPointF(2, 2));
  RectF inner(m_boundingRect);
  inner.adjust(QPointF(2, 2), QPointF(-2, -2));
  return outer.contains(p) && !inner.contains(p);
}

void Shape2DRectangle::drawShape(QPainter &painter) const {
  QRectF drawRect = m_boundingRect.toQRectF();
  painter.drawRect(drawRect);
  if (m_fill_color != QColor()) {
    QPainterPath path;
    path.addRect(drawRect);
    painter.fillPath(path, m_fill_color);
  }
}

void Shape2DRectangle::addToPath(QPainterPath &path) const {
  path.addRect(m_boundingRect.toQRectF());
}

421
422
423
424
/** Load shape 2D state from a Mantid project file
 * @param lines :: lines from the project file to load state from
 * @return a new shape2D in the shape of a rectangle
 */
425
Shape2D *Shape2DRectangle::loadFromProject(const std::string &lines) {
426
  API::TSVSerialiser tsv(lines);
Samuel Jackson's avatar
Samuel Jackson committed
427
428
429
430
431
432
433
434
  tsv.selectLine("Parameters");
  double x0, y0, x1, y1;
  tsv >> x0 >> y0 >> x1 >> y1;
  QPointF point1(x0, y0);
  QPointF point2(x1, y1);
  return new Shape2DRectangle(point1, point2);
}

435
436
437
/** Save the state of the shape 2D rectangle to a Mantid project file
 * @return a string representing the state of the shape 2D
 */
438
std::string Shape2DRectangle::saveToProject() const {
439
  API::TSVSerialiser tsv;
Samuel Jackson's avatar
Samuel Jackson committed
440
441
442
443
444
445
446
447
448
449
450
  auto x0 = m_boundingRect.x0();
  auto x1 = m_boundingRect.x1();
  auto y0 = m_boundingRect.y0();
  auto y1 = m_boundingRect.y1();

  tsv.writeLine("Type") << "rectangle";
  tsv.writeLine("Parameters") << x0 << y0 << x1 << y1;
  tsv.writeRaw(Shape2D::saveToProject());
  return tsv.outputLines();
}

451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// --- Shape2DRing --- //

Shape2DRing::Shape2DRing(Shape2D *shape, double xWidth, double yWidth)
    : m_outer_shape(shape), m_xWidth(xWidth), m_yWidth(yWidth) {
  m_inner_shape = m_outer_shape->clone();
  m_inner_shape->getBoundingRect();
  m_inner_shape->adjustBoundingRect(m_xWidth, m_yWidth, -m_xWidth, -m_yWidth);
  resetBoundingRect();
  m_outer_shape->setFillColor(QColor());
  m_inner_shape->setFillColor(QColor());
}

Shape2DRing::Shape2DRing(const Shape2DRing &ring)
    : Shape2D(), m_outer_shape(ring.m_outer_shape->clone()),
      m_inner_shape(ring.m_inner_shape->clone()), m_xWidth(ring.m_xWidth),
      m_yWidth(ring.m_yWidth) {
  resetBoundingRect();
}

bool Shape2DRing::selectAt(const QPointF &p) const { return contains(p); }

bool Shape2DRing::contains(const QPointF &p) const {
  return m_outer_shape->contains(p) && !m_inner_shape->contains(p);
}

void Shape2DRing::drawShape(QPainter &painter) const {
  m_outer_shape->draw(painter);
  m_inner_shape->draw(painter);
  if (m_fill_color != QColor()) {
    QPainterPath path;
    m_outer_shape->addToPath(path);
    m_inner_shape->addToPath(path);
    painter.fillPath(path, m_fill_color);
  }
}

void Shape2DRing::refit() {
  if (m_xWidth <= 0)
    m_xWidth = 0.000001;
  if (m_yWidth <= 0)
    m_yWidth = 0.000001;
  double xWidth = m_xWidth;
  double yWidth = m_yWidth;
  double max_width = m_boundingRect.width() / 2;
  if (xWidth > max_width)
    xWidth = max_width;
  double max_height = m_boundingRect.height() / 2;
  if (yWidth > max_height)
    yWidth = max_height;
  m_outer_shape->setBoundingRect(m_boundingRect);
  m_inner_shape->setBoundingRect(m_boundingRect);
  m_inner_shape->adjustBoundingRect(xWidth, yWidth, -xWidth, -yWidth);
}

void Shape2DRing::resetBoundingRect() {
  m_boundingRect = m_outer_shape->getBoundingRect();
}

QPointF Shape2DRing::getShapeControlPoint(size_t i) const {
  RectF rect = m_inner_shape->getBoundingRect();
  switch (i) {
  case 0:
    return QPointF(rect.center().x(), rect.y1());
  case 1:
    return QPointF(rect.center().x(), rect.y0());
  case 2:
    return QPointF(rect.x0(), rect.center().y());
  case 3:
    return QPointF(rect.x1(), rect.center().y());
  }
  return QPointF();
}

void Shape2DRing::setShapeControlPoint(size_t i, const QPointF &pos) {
  QPointF dp = pos - getShapeControlPoint(i);

  switch (i) {
  case 0:
    m_yWidth -= dp.y();
    break;
  case 1:
    m_yWidth += dp.y();
    break;
  case 2:
    m_xWidth += dp.x();
    break;
  case 3:
    m_xWidth -= dp.x();
    break;
  }
  refit();
}

QStringList Shape2DRing::getDoubleNames() const {
  QStringList res;
  res << "xwidth"
      << "ywidth";
  return res;
}

double Shape2DRing::getDouble(const QString &prop) const {
  if (prop == "xwidth") {
    return m_xWidth;
  }
  if (prop == "ywidth") {
    return m_yWidth;
  }
  return 0.0;
}

void Shape2DRing::setDouble(const QString &prop, double value) {
  if (prop == "xwidth") {
    m_xWidth = value;
    refit();
  }
  if (prop == "ywidth") {
    m_yWidth = value;
    refit();
  }
}

QPointF Shape2DRing::getPoint(const QString &prop) const {
  if (prop == "center") {
    return m_boundingRect.center();
  }
  return QPointF();
}

void Shape2DRing::setPoint(const QString &prop, const QPointF &value) {
  if (prop == "center") {
    m_boundingRect.moveCenter(value);
  }
}

void Shape2DRing::setColor(const QColor &color) {
  m_inner_shape->setColor(color);
  m_outer_shape->setColor(color);
}

590
591
592
593
/** Load shape 2D state from a Mantid project file
 * @param lines :: lines from the project file to load state from
 * @return a new shape2D in the shape of a ring
 */
594
Shape2D *Shape2DRing::loadFromProject(const std::string &lines) {
595
  API::TSVSerialiser tsv(lines);
Samuel Jackson's avatar
Samuel Jackson committed
596
597
598
599
600
601
602
603
604
605
606
607
  tsv.selectLine("Parameters");
  double xWidth, yWidth;
  tsv >> xWidth >> yWidth;

  tsv.selectSection("shape");
  std::string baseShapeLines;
  tsv >> baseShapeLines;

  auto baseShape = Shape2D::loadFromProject(baseShapeLines);
  return new Shape2DRing(baseShape, xWidth, yWidth);
}

608
609
610
/** Save the state of the shape 2D ring to a Mantid project file
 * @return a string representing the state of the shape 2D
 */
611
std::string Shape2DRing::saveToProject() const {
612
  API::TSVSerialiser tsv;
Samuel Jackson's avatar
Samuel Jackson committed
613
614
615
616
617
618
619
620
621
622
623
  auto xWidth = getDouble("xwidth");
  auto yWidth = getDouble("ywidth");
  auto baseShape = getOuterShape();

  tsv.writeLine("Type") << "ring";
  tsv.writeLine("Parameters") << xWidth << yWidth;
  tsv.writeSection("shape", baseShape->saveToProject());
  tsv.writeRaw(Shape2D::saveToProject());
  return tsv.outputLines();
}

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//------------------------------------------------------------------------------

/// Construct a zero-sized shape.
Shape2DFree::Shape2DFree(const QPointF &p) : m_polygon(QRectF(p, p)) {
  resetBoundingRect();
}

/// The shape can be selected if it contains the point.
bool Shape2DFree::selectAt(const QPointF &p) const { return contains(p); }

/// Check if a point is inside the shape.
bool Shape2DFree::contains(const QPointF &p) const {
  return m_polygon.containsPoint(p, Qt::OddEvenFill);
}

/// Add to a larger shape.
void Shape2DFree::addToPath(QPainterPath &path) const {
  path.addPolygon(m_polygon);
}

/// Draw.
void Shape2DFree::drawShape(QPainter &painter) const {
  QPainterPath path;
  path.addPolygon(m_polygon);
  painter.fillPath(path, m_fill_color);
  painter.drawPath(m_outline);
}

/// Rescale polygon's verices to fit to the new bounding rect.
void Shape2DFree::refit() {
  auto brOld = getPolygonBoundingRect();
  auto &brNew = m_boundingRect;
  if (brNew.xSpan() < 0.0)
    brNew.xFlip();
  if (brNew.ySpan() < 0.0)
    brNew.yFlip();

  auto xs0 = brNew.x0();
  auto x0 = brOld.x0();
  auto xScale = brNew.width() / brOld.width();

  auto ys0 = brNew.y0();
  auto y0 = brOld.y0();
  auto yScale = brNew.height() / brOld.height();
  for (int i = 0; i < m_polygon.size(); ++i) {
    auto &p = m_polygon[i];
    p.rx() = xs0 + xScale * (p.x() - x0);
    p.ry() = ys0 + yScale * (p.y() - y0);
  }
  resetBoundingRect();
}

/// Recalculate the bounding rect.
/// Also make the new border outline.
/// QPolygonF cannot have holes or disjointed parts,
/// it's a single closed line. The outline (implemented as a QPainterPath)
/// makes it look like it have holes.
void Shape2DFree::resetBoundingRect() {
  m_boundingRect = getPolygonBoundingRect();
  // Clear the outline path.
  m_outline = QPainterPath();
  if (m_polygon.isEmpty())
    return;

  // If the polygon has apparent holes/discontinuities
  // it will have extra pairs of edges which we don't want
  // to draw.
  auto n = m_polygon.size() - 1;
  // Find those vertices at which we must break the polygon
  // to get rid of these extra edges.
  QList<int> breaks;
  breaks.push_back(0);
  for (int i = 1; i < m_polygon.size() - 1; ++i) {
    auto p = m_polygon[i];
    auto j = m_polygon.indexOf(p, i + 1);
    if (j != -1) {
      auto i1 = i + 1;
      auto j1 = j - 1;
      if (m_polygon[i1] == m_polygon[j1]) {
        breaks.push_back(i);
        breaks.push_back(i1);
        breaks.push_back(j1);
        breaks.push_back(j);
      }
    }
  }
  if (breaks.back() != n) {
    breaks.push_back(n);
  }
  qSort(breaks);

  m_outline.moveTo(m_polygon[0]);
  int j1 = 0;
  // Add contiguous portions of the polygon to the outline
  // and break at points from breaks list.
  for (int i = 0; i < breaks.size(); ++i) {
    auto j = breaks[i];
    if (j == j1 + 1) {
      m_outline.moveTo(m_polygon[j]);
    } else {
      for (auto k = j1; k <= j; ++k) {
        m_outline.lineTo(m_polygon[k]);
      }
    }
    j1 = j;
  }
}

/// Convert the bounding rect computed by QPolygonF to RectF
RectF Shape2DFree::getPolygonBoundingRect() const {
  auto br = m_polygon.boundingRect();
  auto x0 = br.left();
  auto x1 = br.right();
  if (x0 > x1)
    std::swap(x0, x1);
  auto y0 = br.bottom();
  auto y1 = br.top();
  if (y0 > y1)
    std::swap(y0, y1);
  return RectF(QPointF(x0, y0), QPointF(x1, y1));
}

/// Add a polygon to this shape.
void Shape2DFree::addPolygon(const QPolygonF &polygon) {
  m_polygon = m_polygon.united(polygon);
  resetBoundingRect();
}

/// Subtract a polygon from this shape.
void Shape2DFree::subtractPolygon(const QPolygonF &polygon) {
  m_polygon = m_polygon.subtracted(polygon);
  resetBoundingRect();
}
Samuel Jackson's avatar
Samuel Jackson committed
757

758
759
760
761
/** Load shape 2D state from a Mantid project file
 * @param lines :: lines from the project file to load state from
 * @return a new freefrom shape2D
 */
762
Shape2D *Shape2DFree::loadFromProject(const std::string &lines) {
763
  API::TSVSerialiser tsv(lines);
Samuel Jackson's avatar
Samuel Jackson committed
764
765
  QPolygonF polygon;

766
  size_t paramCount = tsv.values("Parameters").size() - 1;
Samuel Jackson's avatar
Samuel Jackson committed
767
768

  tsv.selectLine("Parameters");
769
  for (size_t i = 0; i < paramCount; i += 2) {
Samuel Jackson's avatar
Samuel Jackson committed
770
771
772
773
774
775
776
777
    double x, y;
    tsv >> x >> y;
    polygon << QPointF(x, y);
  }

  return new Shape2DFree(polygon);
}

778
779
780
/** Save the state of the shape 2D to a Mantid project file
 * @return a string representing the state of the shape 2D
 */
781
std::string Shape2DFree::saveToProject() const {
782
  API::TSVSerialiser tsv;
Samuel Jackson's avatar
Samuel Jackson committed
783
784
785

  tsv.writeLine("Type") << "free";
  tsv.writeLine("Parameters");
786
  for (auto &point : m_polygon) {
Samuel Jackson's avatar
Samuel Jackson committed
787
788
789
790
791
792
    tsv << point.x() << point.y();
  }
  tsv.writeRaw(Shape2D::saveToProject());
  return tsv.outputLines();
}

793
Shape2DFree::Shape2DFree(const QPolygonF &polygon) : m_polygon(polygon) {
Samuel Jackson's avatar
Samuel Jackson committed
794
795
796
  resetBoundingRect();
}

LamarMoore's avatar
LamarMoore committed
797
798
} // namespace MantidWidgets
} // namespace MantidQt