Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
CSVImportConfigurationWidget.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef CSVIMPORTCONFIGURATIONWIDGET_H
22 #define CSVIMPORTCONFIGURATIONWIDGET_H
23 
24 #include <QWidget>
25 #include <QValidator>
26 #include <QTableWidget>
27 
28 #include <tulip/CSVContentHandler.h>
29 #include <tulip/tulipconf.h>
30 
31 class QComboBox;
32 class QCheckBox;
33 class QValidator;
34 class QLineEdit;
35 
36 namespace Ui {
37 class CSVImportConifgurationWidget;
38 }
39 
40 namespace tlp {
41 class CSVParser;
42 class CSVImportParameters;
43 class CSVColumn;
44 
45 /**
46  * @brief Configuration widget for a property.
47  */
48 class TLP_QT_SCOPE PropertyConfigurationWidget: public QWidget {
49  Q_OBJECT
50 public:
51  PropertyConfigurationWidget(unsigned int propertyNumber, const QString& propertyName, bool propertyNameIsEditable,
52  const std::string& PropertyType, QWidget* parent = NULL);
53  /**
54  * Return the selected property type. The property type is not the label displayed in the combobox but correspond to the Property::propertyTypename static string variable of the property class.
55  */
56  std::string getPropertyType() const;
57  /**
58  * @brief Change the type of the property. Use the PropertyClass::propertyTypename static var.
59  **/
60  void setPropertyType(const std::string& propertyType);
61 
62  QString getPropertyName() const;
63  bool getPropertyUsed() const;
64  /**
65  * @brief Set the property name validator. Use to chek if entered graph name is valid.
66  */
67  void setPropertyNameValidator(QValidator* validator);
68  unsigned int getPropertyNumber() const;
69 
70  QLineEdit *getNameLineEdit() {
71  return propertyNameLineEdit;
72  }
73  QComboBox *getTypeComboBox() {
74  return propertyTypeComboBox;
75  }
76 
77  QCheckBox *getCheckBox() {
78  return usedCheckBox;
79  }
80 
81 private:
82  void fillPropertyTypeComboBox();
83  QLineEdit *propertyNameLineEdit;
84  QComboBox *propertyTypeComboBox;
85  QCheckBox *usedCheckBox;
86  bool nameEditable;
87  unsigned int propertyNumber;
88 
89 private slots:
90  void nameEditFinished();
91  void useStateChanged(int state);
92 
93 signals:
94  void propertyNameChange(QString newName);
95  void stateChange(bool state);
96 };
97 
98 /**
99  * @brief Check if the property name already exist in the property list.
100  **/
101 class TLP_QT_SCOPE PropertyNameValidator: public QValidator {
102 public:
103  PropertyNameValidator(const std::vector<PropertyConfigurationWidget*>& widgets,QObject*parent=NULL) :
104  QValidator(parent), widgets(widgets) {
105  }
106  virtual ~PropertyNameValidator() {
107 
108  }
109 
110  /**
111  * Validate the new property name. Check if any property does not have the same name
112  */
113  QValidator::State validate(QString & input, int & pos) const;
114 
115 private:
116  const std::vector<PropertyConfigurationWidget*>& widgets;
117 };
118 
119 /**
120 * @brief Simple table preview of CSV file. Load in a QTableWidget the data send by a CSVContentHandler.
121 **/
122 class TLP_QT_SCOPE CSVTableWidget : public QTableWidget, public CSVContentHandler {
123 public:
124  CSVTableWidget(QWidget* parent=NULL);
125  void begin();
126  void line(unsigned int row,const std::vector<std::string>& lineTokens);
127  void end(unsigned int rowNumber, unsigned int columnNumber);
128  /**
129  * @brief Limit the line number of the preview. Need to parse the file again to take this limit in account.
130  **/
131  void setMaxPreviewLineNumber(unsigned int lineNumber) {
132  maxLineNumber = lineNumber;
133  }
134 
135  /**
136  * @brief Get the preview line number.
137  **/
138  unsigned int getMaxPreviewLineNumber()const {
139  return maxLineNumber;
140  }
141 
142  unsigned int getFirstLineIndex() {
143  return firstLineIndex;
144  }
145 
146  void setFirstLineIndex(unsigned int index) {
147  firstLineIndex = index;
148  }
149 
150 private:
151  unsigned int maxLineNumber;
152  unsigned int firstLineIndex;
153 };
154 
155 
156 /**
157  * @brief Widget generating a CSVImportParameters object configuring the CSV import process.
158  *
159  * Use a CSV parser to fill this widget with previews and CSV file statistics like number of rows and columns.
160  **/
161 class TLP_QT_SCOPE CSVImportConfigurationWidget : public QWidget, public CSVContentHandler {
162  Q_OBJECT
163 public:
164  CSVImportConfigurationWidget(QWidget *parent = NULL);
165  ~CSVImportConfigurationWidget();
166  void begin();
167  void line(unsigned int row,const std::vector<std::string>& lineTokens);
168  void end(unsigned int rowNumber, unsigned int columnNumber);
169 
170  /**
171  * @brief Update the widget contents with the new file parser.
172  **/
173  void setNewParser(tlp::CSVParser *parser);
174 
175  /**
176  * @brief Get the import parameters.
177  *
178  * Use this object to configure import process of the CSVImportGraph object.
179  **/
180  CSVImportParameters getImportParameters()const;
181 
182  bool eventFilter(QObject *, QEvent *);
183 
184 
185 protected:
186 
187  void updateWidget();
188 
189  std::vector<CSVColumn> getPropertiesToImport()const;
190 
191  void updateLineNumbers(bool resetValues);
192 
193  bool useFirstLineAsPropertyName()const;
194  void setUseFirstLineAsPropertyName(bool useFirstLineAsHeader)const;
195  unsigned int rowCount()const;
196  unsigned int columnCount()const;
197 
198 
199  /**
200  *@brief The index of the first line to get in the file.
201  *@brief A line number from 0 to LastLineIndex.
202  **/
203  unsigned int getFirstLineIndex()const;
204 
205  /**
206  * @brief The index of the last line to take in the file.
207  **/
208  unsigned int getLastLineIndex()const;
209  /**
210  * @brief The index of the first imported line. This index change if user use the first line as column names.
211  * The first imported line is the firstLineIndex but with the
212  * By example if user want to import all lines but use the first line as column names this funnction will return 1 not 0.
213  **/
214  unsigned int getFirstImportedLineIndex()const;
215 
216  /**
217  * Empty the properties list.
218  */
219  void clearPropertiesTypeList();
220  /**
221  * Add a property to the current property list.
222  */
223  void addPropertyToPropertyList(const std::string& propertyName, bool isEditable, const std::string& propertyType=std::string(""));
224 
225  /**
226  * @brief Creates a property configuration widget.
227  *
228  * @param propertyNumber The property number.
229  * @param propertyName The name of the property.
230  * @param propertyNameIsEditable Whether the property's name is editable.
231  * @param propertyType The type of the property.
232  * @param parent This widget's parent.
233  * @return :PropertyConfigurationWidget*
234  **/
235  virtual PropertyConfigurationWidget *createPropertyConfigurationWidget(unsigned int propertyNumber,
236  const QString& propertyName, bool propertyNameIsEditable, const std::string& propertyType, QWidget* parent);
237 
238  /**
239  * @brief Compute the name of the column. Return the first token fo the column if the first lline is used as header r Column_x xhere x is the column index.
240  **/
241  QString genrateColumnName(unsigned int col)const;
242  /**
243  * @brief Compute the column data type. Take in account the first row only if it is not used as column label
244  **/
245  std::string getColumnType(unsigned int col)const;
246 
247  std::vector<PropertyConfigurationWidget*> propertyWidgets;
248 
249 protected slots:
250 
251  void filterPreviewLineNumber(bool filter);
252  void previewLineNumberChanged(int value);
253 
254  void fromLineValueChanged(int value);
255  void toLineValueChanged(int value);
256 
257  void updateTableHeaders();
258 
259  void useFirstLineAsHeaderUpdated();
260  void propertyNameChanged(QString propertyName);
261  void propertyStateChanged(bool activated);
262 
263 signals:
264  void fileInfoChanged();
265 
266 private:
267 
268  /**
269  * @brief Try to guess the property datatype in function of the type of the previous tokens and the type of the current token.
270  **/
271  std::string guessPropertyDataType(const std::string data,const std::string previousType)const;
272 
273  /**
274  * @brief Return the type of the column in function of the old and new type.
275  **/
276  std::string combinePropertyDataType(const std::string previousType,const std::string newType)const;
277  /**
278  * @brief Try to guess the type of the data. Can recognize int, double, boolean or string. If the type is other return string.
279  * @return The property typename of the type
280  **/
281  std::string guessDataType(const std::string data)const;
282 
283  void columnSizeChanged(unsigned int i);
284 
285  //The data type of the header
286  std::vector<std::string> columnHeaderType;
287  //The data type of the rest of the column;
288  std::vector<std::string> columnType;
289 
290  Ui::CSVImportConifgurationWidget *ui;
291  PropertyNameValidator* validator;
292  unsigned int maxLineNumber;
293  tlp::CSVParser* parser;
294 };
295 
296 }
297 #endif // CSVIMPORTCONFIGURATIONWIDGET_H
298 ///@endcond