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