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