Tulip 5.7.1
Large graphs analysis and drawing
Loading...
Searching...
No Matches
CSVImportConfigurationWidget.h
1/*
2 *
3 * This file is part of Tulip (https://tulip.labri.fr)
4 *
5 * Authors: David Auber and the Tulip development Team
6 * from LaBRI, University of Bordeaux
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#include <QHeaderView>
28
29#include <tulip/CSVContentHandler.h>
30#include <tulip/CSVGraphImport.h>
31#include <tulip/tulipconf.h>
32
33class QPushButton;
34class Ui_CSVPropertyDialog;
35
36namespace Ui {
37class CSVImportConfigurationWidget;
38}
39namespace tlp {
40class CSVParser;
41class PropertyNameValidator;
42
43/**
44 * @brief Configuration widget for a property.
45 */
46class TLP_QT_SCOPE PropertyConfigurationWidget : public QWidget, public CSVColumn {
47 Q_OBJECT
48public:
49 PropertyConfigurationWidget(unsigned int propertyNumber, const QString &propertyName,
50 bool propertyNameIsEditable, const std::string &PropertyType,
51 PropertyNameValidator *validator, QWidget *parent = nullptr);
52 /**
53 * Return the selected property type.
54 * The property type is not the label displayed in the
55 * combobox but correspond to the Property::propertyTypename
56 * static string variable of the property class.
57 */
58 const std::string &getPropertyType() const;
59 /**
60 * @brief Change the type of the property. Use the PropertyClass::propertyTypename static var.
61 **/
62 void setPropertyType(const std::string &propertyType);
63
64 QString getPropertyName() const;
65
66 void setPropertyName(const QString &name);
67
68 void toggleUsed();
69
70 unsigned int getPropertyNumber() const;
71
72private:
73 PropertyNameValidator *propertyNameValidator;
74 QPushButton *propertyEditButton;
75 Ui_CSVPropertyDialog *ui;
76 bool nameEditable;
77 unsigned int propertyNumber;
78
79private slots:
80 void showPropertyCreationDialog();
81 void typeCBChanged(const QString &index);
82 void addException();
83 void delCurrentException();
84
85signals:
86 void stateChange(bool state);
87};
88
89/**
90 * @brief Check if the property name already exist in the property list.
91 **/
92class TLP_QT_SCOPE PropertyNameValidator : public QValidator {
93public:
94 PropertyNameValidator(const std::vector<PropertyConfigurationWidget *> &widgets,
95 QObject *parent = nullptr)
96 : QValidator(parent), widgets(widgets) {}
97 ~PropertyNameValidator() override {}
98
99 /**
100 * Validate the new property name. Check if any property does not have the same name
101 */
102 QValidator::State validate(QString &input, int &pos) const override;
103
104 // set the index of the column/property currently edited
105 void setCurrentIndex(unsigned int index) {
106 currentIndex = index;
107 }
108
109private:
110 unsigned int currentIndex;
111 const std::vector<PropertyConfigurationWidget *> &widgets;
112};
113
114class CSVTableHeader : public QHeaderView {
115 Q_OBJECT
116
117 const std::vector<PropertyConfigurationWidget *> &widgets;
118
119public:
120 CSVTableHeader(QWidget *parent, std::vector<PropertyConfigurationWidget *> &propertyWidgets);
121
122protected:
123 void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;
124
125protected slots:
126 void checkBoxPressed(int logicalIndex);
127};
128
129/**
130 * @brief Simple table preview of CSV file. Load in a QTableWidget the data send by a
131 *CSVContentHandler.
132 **/
133class TLP_QT_SCOPE CSVTableWidget : public QTableWidget, public CSVContentHandler {
134public:
135 CSVTableWidget(QWidget *parent = nullptr);
136 bool begin() override;
137 bool line(unsigned int row, const std::vector<CSVToken> &lineTokens) override;
138 bool end(unsigned int rowNumber, unsigned int columnNumber) override;
139 /**
140 * @brief Limit the line number of the preview. Need to parse the file again to take this limit
141 *into account.
142 **/
143 void setMaxPreviewLineNumber(unsigned int lineNumber) {
144 // first row is used to display configuration widgets
145 maxLineNumber = lineNumber + 1;
146 }
147
148 unsigned int getFirstLineIndex() {
149 return firstLineIndex;
150 }
151
152 void setFirstLineIndex(unsigned int index) {
153 firstLineIndex = index;
154 }
155
156 int getNbCommentsLines() {
157 return nbCommentsLines;
158 }
159
160private:
161 unsigned int maxLineNumber;
162 unsigned int firstLineIndex;
163 bool checkCommentsLines;
164 int nbCommentsLines;
165};
166
167/**
168 * @brief Widget generating a CSVImportParameters object configuring the CSV import process.
169 *
170 * Use a CSV parser to fill this widget with previews and CSV file statistics like number of rows
171 *and columns.
172 **/
173class TLP_QT_SCOPE CSVImportConfigurationWidget : public QWidget, public CSVContentHandler {
174 Q_OBJECT
175public:
176 CSVImportConfigurationWidget(QWidget *parent = nullptr);
177 ~CSVImportConfigurationWidget() override;
178 bool begin() override;
179 bool line(unsigned int row, const std::vector<CSVToken> &lineTokens) override;
180 bool end(unsigned int rowNumber, unsigned int columnNumber) override;
181 void setFirstLineIndex(int firstLine);
182
183 /**
184 * @brief Update the widget contents with the new file parser.
185 **/
186 void setNewParser(tlp::CSVParser *parser);
187
188 /**
189 * @brief Get the import parameters.
190 *
191 * Use this object to configure import process of the CSVImportGraph object.
192 **/
193 CSVImportParameters getImportParameters() const;
194
195 // return the sorted names of the existing properties of a known typename
196 // see PropertyInterface::getTypename()
197 static const std::set<std::string> &getPropsForTypename(const std::string &type);
198
199protected:
200 void updateWidget(const std::string &title = "Generating preview");
201
202 const std::vector<CSVColumn *> getPropertiesToImport() const;
203
204 void updateLineNumbers(bool resetValues);
205
206 bool useFirstLineAsPropertyName() const;
207 void setUseFirstLineAsPropertyName(bool useFirstLineAsHeader) const;
208 unsigned int rowCount() const;
209 unsigned int columnCount() const;
210
211 /**
212 *@brief The index of the first line to get in the file.
213 *@brief A line number from 0 to LastLineIndex.
214 **/
215 unsigned int getFirstLineIndex() const;
216
217 /**
218 * @brief The index of the last line to take in the file.
219 **/
220 unsigned int getLastLineIndex() const;
221 /**
222 * @brief The index of the first imported line. This index change if user use the first line as
223 *column names.
224 * For example if the user wants to import all lines but uses the first line as column names this
225 *function will return 1 not 0.
226 **/
227 unsigned int getFirstImportedLineIndex() const;
228
229 /**
230 * Empty the properties list.
231 */
232 void clearPropertiesTypeList();
233 /**
234 * Add a property to the current property list.
235 */
236 void addPropertyToPropertyList(const std::string &propertyName, bool isEditable,
237 const std::string &propertyType = std::string(""));
238
239 /**
240 * @brief Creates a property configuration widget.
241 *
242 * @param propertyNumber The property number.
243 * @param propertyName The name of the property.
244 * @param propertyNameIsEditable Whether the property's name is editable.
245 * @param propertyType The type of the property.
246 * @param parent This widget's parent.
247 * @return :PropertyConfigurationWidget*
248 **/
249 virtual PropertyConfigurationWidget *
250 createPropertyConfigurationWidget(unsigned int propertyNumber, const QString &propertyName,
251 bool propertyNameIsEditable, const std::string &propertyType,
252 QWidget *parent);
253
254 /**
255 * @brief Compute the name of the column. Return the first token for the column if the first line
256 *is used as header r Column_x xhere x is the column index.
257 **/
258 QString generateColumnName(unsigned int col) const;
259 /**
260 * @brief Compute the column data type. Take in account the first row only if it is not used as
261 *column label
262 **/
263 std::string getColumnType(unsigned int col) const;
264
265 std::vector<PropertyConfigurationWidget *> propertyWidgets;
266
267protected slots:
268
269 void filterPreviewLineNumber(bool filter);
270 void previewLineNumberChanged(int value);
271
272 void toLineValueChanged(int value);
273
274 void updateTableHeaders();
275
276 void useFirstLineAsHeaderUpdated();
277 void propertyStateChanged(bool activated);
278
279private:
280 /**
281 * @brief Try to guess the property datatype in function of the type of the previous tokens and
282 *the type of the current token.
283 **/
284 const std::string &guessPropertyDataType(const std::string &data,
285 const std::string &previousType) const;
286
287 /**
288 * @brief Return the type of the column in function of the old and new type.
289 **/
290 const std::string &combinePropertyDataType(const std::string &previousType,
291 const std::string &newType) const;
292 /**
293 * @brief Try to guess the type of the data. Can recognize int, double, Boolean or string. If the
294 *type is other return string.
295 * @return The property typename of the type
296 **/
297 const std::string &guessDataType(const std::string &data) const;
298
299 // update the max line number of the preview table
300 void setMaxPreviewLineNumber(unsigned int lineNumber);
301
302 // The data type of the header
303 std::vector<std::string> columnHeaderType;
304 // The data type of the rest of the column;
305 std::vector<std::string> columnType;
306
307 Ui::CSVImportConfigurationWidget *ui;
308 PropertyNameValidator *validator;
309 unsigned int maxLineNumber;
310 unsigned int headerColumnCount;
311 tlp::CSVParser *parser;
312 unsigned int firstLine;
313 bool guessFirstLineIsHeader;
314 bool keepPropertyWidgets;
315};
316} // namespace tlp
317#endif // CSVIMPORTCONFIGURATIONWIDGET_H
318///@endcond