Minsky: 3.17.0
minsky::CSVDialog Class Reference

#include <CSVDialog.h>

Inheritance diagram for minsky::CSVDialog:
Inheritance graph
Collaboration diagram for minsky::CSVDialog:
Collaboration graph

Public Member Functions

double tableWidth () const
 width of table (in pixels) More...
 
void loadFile ()
 loads an initial sequence of lines from url. If fname contains "://", is is treated as a URL, and downloaded from the web. More...
 
void guessSpecAndLoadFile ()
 guess the spec, then load an initial sequence of like loadFile() More...
 
void loadFileFromName (const std::string &fname)
 common implementation of loading the initial sequence of lines More...
 
void reportFromFile (const std::string &input, const std::string &output) const
 
void requestRedraw ()
 
std::size_t columnOver (double x) const
 return column mouse is over More...
 
std::size_t rowOver (double y) const
 return row mouse is over More...
 
std::vector< std::vector< std::string > > parseLines (size_t maxColumn=std::numeric_limits< size_t >::max())
 
void populateHeaders ()
 populate all column names from the headers row More...
 
void populateHeader (size_t col)
 populate the name of column col with the data from the header row More...
 
void classifyColumns ()
 try to classify axis,data,ignore columns based on read in data More...
 
std::vector< size_t > correctedUniqueValues ()
 unique values in each column, ignoring initial header lines could slightly underestimate the value, and is never less than 1, even for empty columns More...
 
- Public Member Functions inherited from minsky::RenderNativeWindow
const RenderFrameArgsframeArgs () const
 
 ~RenderNativeWindow () override
 
virtual void init ()
 perform any initialisation of any subclasses of this More...
 
void renderFrame (const RenderFrameArgs &args)
 
void destroyFrame ()
 
void draw ()
 
void requestRedraw ()
 
RenderNativeWindowoperator= (const RenderNativeWindow &x)
 
 RenderNativeWindow ()=default
 
 RenderNativeWindow (const RenderNativeWindow &)=default
 
virtual bool hasScrollBars () const
 return whether this window has scrollbars (needed for MacOSX). More...
 
- Public Member Functions inherited from minsky::EventInterface
virtual void mouseDown (float x, float y)
 
virtual void controlMouseDown (float x, float y)
 
virtual void mouseUp (float x, float y)
 
virtual void mouseMove (float x, float y)
 
virtual void zoom (double x, double y, double z)
 
virtual void moveTo (float x, float y)
 move this so that (x,y) is centred More...
 
virtual std::vector< float > position () const
 current centre coordinates More...
 
virtual double zoomFactor () const
 
virtual bool keyPress (const KeyPressArgs &)
 handle key press over current itemFocus, More...
 
virtual bool getItemAt (float x, float y)
 item or wire obtained by get*At() calls More...
 
virtual bool getWireAt (float x, float y)
 item or wire obtained by get*At() calls More...
 

Public Attributes

double xoffs =80
 
double colWidth =50
 
bool flashNameRow =false
 
DataSpec spec
 
std::string url
 filename, or web url More...
 
- Public Attributes inherited from minsky::EventInterface
ItemPtr item
 item or wire obtained by get*At() calls More...
 
WirePtr wire
 item or wire obtained by get*At() calls More...
 

Static Public Attributes

static const unsigned numInitialLines =100
 
- Static Public Attributes inherited from minsky::RenderNativeWindow
static ecolab::cairo::Colour backgroundColour {0.8,0.8,0.8,1}
 

Private Member Functions

 CLASSDESC_ACCESS (DataSpec)
 
bool redraw (int, int, int width, int height) override
 

Private Attributes

std::vector< std::string > initialLines
 initial lines of file More...
 
double rowHeight =0
 
double m_tableWidth
 

Additional Inherited Members

- Static Public Member Functions inherited from minsky::RenderNativeWindow
static double scaleFactor ()
 

Detailed Description

Definition at line 35 of file CSVDialog.h.

Member Function Documentation

◆ CLASSDESC_ACCESS()

minsky::CSVDialog::CLASSDESC_ACCESS ( DataSpec  )
private

◆ classifyColumns()

void CSVDialog::classifyColumns ( )

try to classify axis,data,ignore columns based on read in data

Definition at line 345 of file CSVDialog.cc.

References minsky::isNumerical(), and parseLines().

346 {
347  auto parsedLines=parseLines();
348  spec.dimensionCols.clear();
349  spec.dataCols.clear();
350  spec.dimensions.resize(min(spec.numCols,spec.maxColumn));
351  for (size_t col=0; col<spec.numCols; ++col)
352  {
353  bool entryFound=false, timeFound=true, numberFound=true;
354  for (size_t row=spec.nRowAxes(); row<parsedLines.size(); ++row)
355  if (col<parsedLines[row].size() && !parsedLines[row][col].empty())
356  {
357  entryFound=true;
358  if (numberFound && !isNumerical(parsedLines[row][col]))
359  numberFound=false;
360  static const AnyVal any(Dimension(Dimension::time,""));
361  if (timeFound)
362  try
363  {any(parsedLines[row][col]);}
364  catch (...)
365  {timeFound=false;}
366  }
367  if (entryFound && col<spec.maxColumn)
368  {
369  if (numberFound)
370  spec.dataCols.insert(col);
371  else
372  {
373  spec.dimensionCols.insert(col);
374  if (timeFound)
375  spec.dimensions[col].type=Dimension::time;
376  else
377  spec.dimensions[col].type=Dimension::string;
378  spec.dimensions[col].units.clear();
379  }
380  }
381  else if (col>=spec.nColAxes() && col<spec.maxColumn)
382  spec.dataCols.insert(col);
383  }
384 }
std::size_t nColAxes() const
start column of the data area
Definition: CSVParser.h:48
std::vector< std::vector< std::string > > parseLines(size_t maxColumn=std::numeric_limits< size_t >::max())
Definition: CSVDialog.cc:301
std::size_t numCols
number of columns in CSV. Must be > dataColOffset
std::vector< civita::Dimension > dimensions
bool isNumerical(const std::string &s)
Definition: CSVParser.cc:338
DataSpec spec
Definition: CSVDialog.h:48
std::size_t maxColumn
maximum number of columns that can be configured independently. Columns after this limit are treated ...
Definition: CSVParser.h:43
std::size_t nRowAxes() const
start row of the data area
Definition: CSVParser.h:46
std::set< unsigned > dataCols
std::set< unsigned > dimensionCols
rows and columns that are comment lines to be ignored
Here is the call graph for this function:

◆ columnOver()

size_t CSVDialog::columnOver ( double  x) const

return column mouse is over

Definition at line 291 of file CSVDialog.cc.

292 {
293  return size_t((x-xoffs)/(colWidth+5));
294 }

◆ correctedUniqueValues()

std::vector< size_t > CSVDialog::correctedUniqueValues ( )

unique values in each column, ignoring initial header lines could slightly underestimate the value, and is never less than 1, even for empty columns

Definition at line 386 of file CSVDialog.cc.

References parseLines().

387 {
388  auto r=spec.uniqueValues();
389  // apply a correction by removing the values in the header rows
390  vector<set<size_t>> correction(r.size());
391  auto parsedLines=parseLines();
392  const hash<string> h;
393  for (size_t row=0; row<parsedLines.size() && row<spec.nRowAxes(); ++row)
394  for (size_t col=0; col<correction.size() && col<parsedLines[row].size(); ++col)
395  correction[col].insert(h(parsedLines[row][col]));
396  for (size_t i=0; i<r.size(); ++i)
397  if (r[i]>correction[i].size())
398  r[i]-=correction[i].size();
399  else
400  r[i]=1;
401  return r;
402 }
std::vector< std::vector< std::string > > parseLines(size_t maxColumn=std::numeric_limits< size_t >::max())
Definition: CSVDialog.cc:301
const std::vector< size_t > & uniqueValues() const
number of unique values in each column corrected for header row, so may be slightly inaccurate if hea...
Definition: CSVParser.h:92
DataSpec spec
Definition: CSVDialog.h:48
std::size_t nRowAxes() const
start row of the data area
Definition: CSVParser.h:46
Here is the call graph for this function:

◆ guessSpecAndLoadFile()

void CSVDialog::guessSpecAndLoadFile ( )

guess the spec, then load an initial sequence of like loadFile()

Definition at line 103 of file CSVDialog.cc.

104 {
105  spec=DataSpec();
108  populateHeaders();
109  classifyColumns();
110 }
void guessFromFile(const std::string &fileName)
initial stab at dataspec from examining file
Definition: CSVParser.h:80
void classifyColumns()
try to classify axis,data,ignore columns based on read in data
Definition: CSVDialog.cc:345
void populateHeaders()
populate all column names from the headers row
Definition: CSVDialog.cc:328
std::string url
filename, or web url
Definition: CSVDialog.h:50
DataSpec spec
Definition: CSVDialog.h:48
void loadFileFromName(const std::string &fname)
common implementation of loading the initial sequence of lines
Definition: CSVDialog.cc:112

◆ loadFile()

void CSVDialog::loadFile ( )

loads an initial sequence of lines from url. If fname contains "://", is is treated as a URL, and downloaded from the web.

Definition at line 98 of file CSVDialog.cc.

99 {
101 }
std::string url
filename, or web url
Definition: CSVDialog.h:50
void loadFileFromName(const std::string &fname)
common implementation of loading the initial sequence of lines
Definition: CSVDialog.cc:112

◆ loadFileFromName()

void CSVDialog::loadFileFromName ( const std::string &  fname)

common implementation of loading the initial sequence of lines

Definition at line 112 of file CSVDialog.cc.

References minsky::stripByteOrderingMarker().

113 {
114  ifstream is(fname);
116  initialLines.clear();
117  for (size_t i=0; i<numInitialLines && is; ++i)
118  {
119  initialLines.emplace_back();
120  getline(is, initialLines.back());
121  // chomp any final '\r' character (DOS files)
122  if (!initialLines.back().empty() && initialLines.back().back()=='\r')
123  initialLines.back().erase(initialLines.back().end()-1);
124  }
125  // Ensure dimensions.size() is the same as nColAxes() upon first load of a CSV file. For ticket 974.
127 }
std::size_t nColAxes() const
start column of the data area
Definition: CSVParser.h:48
std::vector< civita::Dimension > dimensions
DataSpec spec
Definition: CSVDialog.h:48
std::size_t nRowAxes() const
start row of the data area
Definition: CSVParser.h:46
void stripByteOrderingMarker(std::istream &s)
checks if the input stream has the UTF-8 byte ordering marker, and removes it if present ...
Definition: str.h:147
std::vector< std::string > initialLines
initial lines of file
Definition: CSVDialog.h:37
static const unsigned numInitialLines
Definition: CSVDialog.h:44
void setDataArea(std::size_t row, std::size_t col)
set top left cell of the data area
Definition: CSVParser.cc:370
Here is the call graph for this function:

◆ parseLines()

std::vector< std::vector< std::string > > CSVDialog::parseLines ( size_t  maxColumn = std::numeric_limits<size_t>::max())

Definition at line 301 of file CSVDialog.cc.

References parseLines().

302 {
303  vector<vector<string>> parsedLines;
304  if (spec.mergeDelimiters)
305  if (spec.separator==' ')
306  parsedLines=::parseLines(boost::char_separator<char>(), initialLines, maxColumn);
307  else
308  {
309  char separators[]={spec.separator,'\0'};
310  parsedLines=::parseLines
311  (boost::char_separator<char>(separators,""),initialLines, maxColumn);
312  }
313  else
314  parsedLines=::parseLines
315  (boost::escaped_list_separator<char>(spec.escape,spec.separator,spec.quote),
316  initialLines, maxColumn);
317 
318  // update numCols iff maxColumn unrestricted
319  if (maxColumn==numeric_limits<size_t>::max())
320  {
321  spec.numCols=0;
322  for (auto& i: parsedLines)
323  spec.numCols=std::max(spec.numCols, i.size());
324  }
325  return parsedLines;
326 }
std::vector< std::vector< std::string > > parseLines(size_t maxColumn=std::numeric_limits< size_t >::max())
Definition: CSVDialog.cc:301
std::size_t numCols
number of columns in CSV. Must be > dataColOffset
DataSpec spec
Definition: CSVDialog.h:48
std::vector< std::string > initialLines
initial lines of file
Definition: CSVDialog.h:37
Here is the call graph for this function:

◆ populateHeader()

void CSVDialog::populateHeader ( size_t  col)

populate the name of column col with the data from the header row

Definition at line 336 of file CSVDialog.cc.

References parseLines().

337 {
338  auto parsedLines=parseLines();
339  if (spec.headerRow>=parsedLines.size()) return;
340  auto& headers=parsedLines[spec.headerRow];
341  if (col<headers.size() && col<spec.maxColumn)
342  spec.dimensionNames[col]=headers[col];
343 }
std::vector< std::vector< std::string > > parseLines(size_t maxColumn=std::numeric_limits< size_t >::max())
Definition: CSVDialog.cc:301
DataSpec spec
Definition: CSVDialog.h:48
std::size_t headerRow
number of header rows
std::vector< std::string > dimensionNames
std::size_t maxColumn
maximum number of columns that can be configured independently. Columns after this limit are treated ...
Definition: CSVParser.h:43
Here is the call graph for this function:

◆ populateHeaders()

void CSVDialog::populateHeaders ( )

populate all column names from the headers row

Definition at line 328 of file CSVDialog.cc.

References parseLines().

329 {
330  auto parsedLines=parseLines();
331  if (spec.headerRow>=parsedLines.size()) return;
332  auto& hr=parsedLines[spec.headerRow];
333  spec.dimensionNames={hr.begin(), min(hr.end(), hr.begin()+spec.maxColumn)};
334 }
std::vector< std::vector< std::string > > parseLines(size_t maxColumn=std::numeric_limits< size_t >::max())
Definition: CSVDialog.cc:301
DataSpec spec
Definition: CSVDialog.h:48
std::size_t headerRow
number of header rows
std::vector< std::string > dimensionNames
std::size_t maxColumn
maximum number of columns that can be configured independently. Columns after this limit are treated ...
Definition: CSVParser.h:43
Here is the call graph for this function:

◆ redraw()

bool CSVDialog::redraw ( int  ,
int  ,
int  width,
int  height 
)
overrideprivate

Definition at line 169 of file CSVDialog.cc.

References parseLines().

170 {
171  cairo_t* cairo=surface->cairo();
172  rowHeight=15;
173  vector<vector<string>> parsedLines=parseLines();
174 
175  // LHS row labels
176  {
177  Pango pango(cairo);
178  pango.setText("Dimension");
179  cairo_move_to(cairo,xoffs-pango.width()-5,0);
180  pango.show();
181  pango.setText("Type");
182  cairo_move_to(cairo,xoffs-pango.width()-5,rowHeight);
183  pango.show();
184  pango.setText("Format");
185  cairo_move_to(cairo,xoffs-pango.width()-5,2*rowHeight);
186  pango.show();
187  if (flashNameRow)
188  pango.setMarkup("<b>Name</b>");
189  else
190  pango.setText("Name");
191  cairo_move_to(cairo,xoffs-pango.width()-5,3*rowHeight);
192  pango.show();
193  pango.setText("Header");
194  cairo_move_to(cairo,xoffs-pango.width()-5,(4+spec.headerRow)*rowHeight);
195  pango.show();
196 
197  }
198 
199  CroppedPango pango(cairo, colWidth);
200  pango.setFontSize(0.8*rowHeight);
201 
202  set<size_t> done;
203  double x=xoffs, y=0;
204  size_t col=0;
205  for (; done.size()<parsedLines.size(); ++col)
206  {
207  if (col<spec.nColAxes())
208  {// dimension check boxes
209  const CairoSave cs(cairo);
210  const double cbsz=5;
211  cairo_set_line_width(cairo,1);
212  cairo_translate(cairo,x+0.5*colWidth,y+0.5*rowHeight);
213  cairo_rectangle(cairo,-cbsz,-cbsz,2*cbsz,2*cbsz);
214  if (spec.dimensionCols.contains(col))
215  {
216  cairo_move_to(cairo,-cbsz,-cbsz);
217  cairo_line_to(cairo,cbsz,cbsz);
218  cairo_move_to(cairo,cbsz,-cbsz);
219  cairo_line_to(cairo,-cbsz,cbsz);
220  }
221  cairo_stroke(cairo);
222  }
223  y+=rowHeight;
224  // type
225  if (spec.dimensionCols.contains(col) && col<spec.dimensions.size() && col<spec.nColAxes())
226  {
227  pango.setText(classdesc::enumKey<Dimension::Type>(spec.dimensions[col].type));
228  pango.setxy(x,y);
229  pango.show();
230  }
231  y+=rowHeight;
232  if (spec.dimensionCols.contains(col) && col<spec.dimensions.size() && col<spec.nColAxes())
233  {
234  pango.setText(spec.dimensions[col].units);
235  pango.setxy(x,y);
236  pango.show();
237  }
238  y+=rowHeight;
239  if (spec.dimensionCols.contains(col) && col<spec.dimensionNames.size() && col<spec.nColAxes())
240  {
241  pango.setText(spec.dimensionNames[col]);
242  pango.setxy(x,y);
243  pango.show();
244  }
245  y+=rowHeight;
246  for (size_t row=0; row<parsedLines.size(); ++row)
247  {
248  auto& line=parsedLines[row];
249  if (col<line.size())
250  {
251  const CairoSave cs(cairo);
252  pango.setText(line[col]);
253  pango.setxy(x, y);
254  if (row==spec.headerRow)
255  if (col<spec.nColAxes())
256  cairo_set_source_rgb(surface->cairo(),0,0.7,0);
257  else
258  cairo_set_source_rgb(surface->cairo(),0,0,1);
259  else if (row<spec.nRowAxes() || (col<spec.nColAxes() && !spec.dimensionCols.contains(col)))
260  cairo_set_source_rgb(surface->cairo(),1,0,0);
261  else if (col<spec.nColAxes())
262  cairo_set_source_rgb(surface->cairo(),0,0,1);
263  pango.show();
264  }
265  else
266  done.insert(row);
267  y+=rowHeight;
268  }
269  {
270  const CairoSave cs(cairo);
271  cairo_set_source_rgb(cairo,.5,.5,.5);
272  cairo_move_to(cairo,x-2.5,0);
273  cairo_rel_line_to(cairo,0,(parsedLines.size()+4)*rowHeight);
274  cairo_stroke(cairo);
275  }
276  x+=colWidth+5;
277  y=0;
278  }
279  m_tableWidth=(col-1)*(colWidth+5);
280  for (size_t row=0; row<parsedLines.size()+5; ++row)
281  {
282  const CairoSave cs(cairo);
283  cairo_set_source_rgb(cairo,.5,.5,.5);
284  cairo_move_to(cairo,xoffs-2.5,row*rowHeight);
285  cairo_rel_line_to(cairo,m_tableWidth,0);
286  cairo_stroke(cairo);
287  }
288  return true;
289 }
std::size_t nColAxes() const
start column of the data area
Definition: CSVParser.h:48
std::vector< std::vector< std::string > > parseLines(size_t maxColumn=std::numeric_limits< size_t >::max())
Definition: CSVDialog.cc:301
double rowHeight
Definition: CSVDialog.h:38
std::vector< civita::Dimension > dimensions
DataSpec spec
Definition: CSVDialog.h:48
std::size_t headerRow
number of header rows
std::vector< std::string > dimensionNames
std::size_t nRowAxes() const
start row of the data area
Definition: CSVParser.h:46
double m_tableWidth
Definition: CSVDialog.h:39
std::set< unsigned > dimensionCols
rows and columns that are comment lines to be ignored
Here is the call graph for this function:

◆ reportFromFile()

void CSVDialog::reportFromFile ( const std::string &  input,
const std::string &  output 
) const

Definition at line 51 of file CSVDialog.cc.

References minsky::reportFromCSVFile(), and minsky::stripByteOrderingMarker().

52 {
53  ifstream is(input);
55  ofstream of(output);
56  reportFromCSVFile(is,of,spec,file_size(input));
57 }
void reportFromCSVFile(istream &input, ostream &output, const DataSpec &spec, uintmax_t fileSize)
creates a report CSV file from input, with errors sorted at begining of file, with a column for error...
Definition: CSVParser.cc:1161
Definition: input.py:1
DataSpec spec
Definition: CSVDialog.h:48
void stripByteOrderingMarker(std::istream &s)
checks if the input stream has the UTF-8 byte ordering marker, and removes it if present ...
Definition: str.h:147
Here is the call graph for this function:

◆ requestRedraw()

void minsky::CSVDialog::requestRedraw ( )
inline

Definition at line 63 of file CSVDialog.h.

63 {if (surface.get()) surface->requestRedraw();}

◆ rowOver()

size_t CSVDialog::rowOver ( double  y) const

return row mouse is over

Definition at line 296 of file CSVDialog.cc.

297 {
298  return size_t(y/rowHeight);
299 }
double rowHeight
Definition: CSVDialog.h:38

◆ tableWidth()

double minsky::CSVDialog::tableWidth ( ) const
inline

width of table (in pixels)

Definition at line 52 of file CSVDialog.h.

References m_tableWidth.

52 {return m_tableWidth;}
double m_tableWidth
Definition: CSVDialog.h:39

Member Data Documentation

◆ colWidth

double minsky::CSVDialog::colWidth =50

Definition at line 46 of file CSVDialog.h.

◆ flashNameRow

bool minsky::CSVDialog::flashNameRow =false

Definition at line 47 of file CSVDialog.h.

◆ initialLines

std::vector<std::string> minsky::CSVDialog::initialLines
private

initial lines of file

Definition at line 37 of file CSVDialog.h.

◆ m_tableWidth

double minsky::CSVDialog::m_tableWidth
private

Definition at line 39 of file CSVDialog.h.

Referenced by tableWidth().

◆ numInitialLines

const unsigned CSVDialog::numInitialLines =100
static

◆ rowHeight

double minsky::CSVDialog::rowHeight =0
private

Definition at line 38 of file CSVDialog.h.

◆ spec

DataSpec minsky::CSVDialog::spec

Definition at line 48 of file CSVDialog.h.

◆ url

std::string minsky::CSVDialog::url

filename, or web url

Definition at line 50 of file CSVDialog.h.

◆ xoffs

double minsky::CSVDialog::xoffs =80

Definition at line 45 of file CSVDialog.h.


The documentation for this class was generated from the following files: