Engauge Digitizer  2
MimePointsDetector.cpp
1 /******************************************************************************************************
2  * (C) 2017 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "MimePointsDetector.h"
8 #include <QApplication>
9 #include <QClipboard>
10 #include <QSize>
11 #include <QStringList>
12 #include "Transformation.h"
13 
15 {
16 }
17 
19 {
20 }
21 
23  const QSize &screenSize) const
24 {
25  // A major goal is for this to return as quickly as possible so user interface is not visibly slowed down
26  //
27  // Expected format is:
28  // x Curve1
29  // # #
30  // # #
31  // x Curve 2
32  // # #
33  //
34  // Tests are, ordered from fastest/easiest to slowest/hardest are:
35  // 1) Transformation must be defined
36  // 2) Need at least two lines (one header plus one data point)
37  // 3) Always two tab-delimited columns
38  // 4) Skip lines that have other than 2 numbers
39  // 5) Skip if numbers correspond to points outside of the X/Y coordinates since user will never be able to see them
40 
41  const QString TAB_DELIMITER ("\t");
42 
43  if (!transformation.transformIsDefined()) {
44  return false;
45  }
46 
47  const QClipboard *clipboard = QApplication::clipboard();
48  QString text = clipboard->text ();
49  QStringList lines = text.split ("\n");
50  int i;
51 
52  // Check for two lines
53  if (lines.count () < 2) {
54  return false;
55  }
56 
57  // Check for two columns
58  for (i = 0; i < lines.count(); i++) {
59 
60  // Skip empty lines
61  QString line = lines.at (i);
62  if (!line.trimmed ().isEmpty ()) {
63 
64  QStringList fields = line.split (TAB_DELIMITER);
65  if (fields.count () != 2) {
66  return false;
67  }
68  }
69  }
70 
71  // Check for numbers outside of the legal range
72  for (i = 0; i < lines.count (); i++) {
73 
74  // Skip empty lines
75  QString line = lines.at (i);
76  if (!line.trimmed ().isEmpty ()) {
77 
78  QStringList fields = line.split (TAB_DELIMITER);
79  QString field0 = fields [0];
80  QString field1 = fields [1];
81  bool ok0, ok1;
82  double value0 = field0.toDouble (&ok0);
83  double value1 = field1.toDouble (&ok1);
84  if (ok0 && ok1) {
85 
86  // This is a data point. Check against legal range
87  QPointF pointScreen;
88  transformation.transformRawGraphToScreen (QPointF (value0, value1),
89  pointScreen);
90  if (pointScreen.x() < 0 ||
91  pointScreen.y() < 0 ||
92  pointScreen.x() > screenSize.width() ||
93  pointScreen.y() > screenSize.height ()) {
94 
95  return false;
96  }
97  }
98  }
99  }
100 
101  return true;
102 }
void transformRawGraphToScreen(const QPointF &pointRaw, QPointF &pointScreen) const
Transform from raw graph coordinates to linear cartesian graph coordinates, then to screen coordinate...
bool isMimePointsData(const Transformation &transforation, const QSize &screenSize) const
Returns true if text is acceptable mime data.
Affine transformation between screen and graph coordinates, based on digitized axis points...
bool transformIsDefined() const
Transform is defined when at least three axis points have been digitized.
virtual ~MimePointsDetector()
Destructor.
MimePointsDetector()
Default constructofr.