Skip to content
Snippets Groups Projects
Commit a05c1bec authored by Juan Manuel Carmona Loaiza's avatar Juan Manuel Carmona Loaiza :ghost:
Browse files

Functionality implemented: creating pointwise axis from data columns

parent f34b62d9
No related branches found
No related tags found
No related merge requests found
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
namespace namespace
{ {
const QSize default_dialog_size(600, 800); const QSize default_dialog_size(300, 400);
} }
CsvImportAssistant::CsvImportAssistant(QString dir, QString file, QWidget* parent): CsvImportAssistant::CsvImportAssistant(QString dir, QString file, QWidget* parent):
...@@ -40,7 +40,6 @@ CsvImportAssistant::CsvImportAssistant(QString dir, QString file, QWidget* paren ...@@ -40,7 +40,6 @@ CsvImportAssistant::CsvImportAssistant(QString dir, QString file, QWidget* paren
m_coordinateName(""), m_coordinateName(""),
m_singleCol(0), m_singleCol(0),
m_tableWidget(nullptr), m_tableWidget(nullptr),
m_filePathField(nullptr),
m_separatorField(nullptr), m_separatorField(nullptr),
m_firstDataRowSpinBox(nullptr), m_firstDataRowSpinBox(nullptr),
m_singleDataColSpinBox(nullptr) m_singleDataColSpinBox(nullptr)
...@@ -85,8 +84,10 @@ QBoxLayout* CsvImportAssistant::createLayout() ...@@ -85,8 +84,10 @@ QBoxLayout* CsvImportAssistant::createLayout()
result->setMargin(10); result->setMargin(10);
result->addWidget(new QLabel("Right clicking on the table below allows you to modify what will be imported"));
result->addWidget(m_tableWidget); result->addWidget(m_tableWidget);
result->addSpacing(20);
result->addWidget(new QLabel("Use the controls below to select what will be imported"));
result->addLayout(CsvImportAssistant::createFileDetailsLayout()); result->addLayout(CsvImportAssistant::createFileDetailsLayout());
...@@ -100,94 +101,64 @@ QBoxLayout* CsvImportAssistant::createLayout() ...@@ -100,94 +101,64 @@ QBoxLayout* CsvImportAssistant::createLayout()
} }
QBoxLayout* CsvImportAssistant::createFileDetailsLayout(){ QBoxLayout* CsvImportAssistant::createFileDetailsLayout(){
auto result = new QVBoxLayout; auto result = new QHBoxLayout;
result->setMargin(10); result->setMargin(10);
auto labelFilepath = new QLabel("File to import: ");
m_filePathField = new QLineEdit(m_fileName,this);
auto browseButton = new QPushButton("browse");
connect(browseButton, &QPushButton::clicked, this, &CsvImportAssistant::onBrowseButton);
auto lay1 = new QHBoxLayout;
lay1->addWidget(labelFilepath);
lay1->addWidget(m_filePathField);
lay1->addWidget(browseButton);
result->addLayout(lay1);
auto labelSeparator = new QLabel("Separator: "); auto labelSeparator = new QLabel("Separator: ");
m_separatorField = new QLineEdit(QString(guessSeparator())); m_separatorField = new QLineEdit(QString(guessSeparator()));
m_separatorField->setMaxLength(1); m_separatorField->setMaxLength(1);
auto lay2 = new QHBoxLayout; m_separatorField->setMaximumWidth(50);
auto lay2 = new QVBoxLayout;
lay2->addWidget(labelSeparator); lay2->addWidget(labelSeparator);
lay2->addWidget(m_separatorField); lay2->addWidget(m_separatorField);
result->addLayout(lay2); //result->addLayout(lay2);
connect(m_separatorField, &QLineEdit::editingFinished, this, &CsvImportAssistant::Reload); connect(m_separatorField, &QLineEdit::editingFinished, this, &CsvImportAssistant::Reload);
auto lay3 = new QVBoxLayout;
auto labelFirstDataRow = new QLabel("First data row: "); auto labelFirstDataRow = new QLabel("First data row: ");
m_firstDataRowSpinBox = new QSpinBox(); m_firstDataRowSpinBox = new QSpinBox();
m_firstDataRowSpinBox->setMinimum(1); m_firstDataRowSpinBox->setMinimum(1);
m_firstDataRowSpinBox->setMaximum(1); m_firstDataRowSpinBox->setMaximum(1);
m_firstDataRowSpinBox->setValue(1); m_firstDataRowSpinBox->setValue(1);
auto lay4 = new QHBoxLayout; m_firstDataRowSpinBox->setMaximumWidth(50);
lay4->addWidget(labelFirstDataRow); lay3->addWidget(labelFirstDataRow);
lay4->addWidget(m_firstDataRowSpinBox); lay3->addWidget(m_firstDataRowSpinBox);
result->addLayout(lay4);
connect(m_firstDataRowSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), connect(m_firstDataRowSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
[=](int i){ onIntChanged(i); }); [=](int i){ onIntChanged(i); });
auto lay4 = new QVBoxLayout;
auto labelSingleColImport = new QLabel("Import Single Column (zero to import all): "); auto labelSingleColImport = new QLabel("Import Single Column (zero to import all): ");
m_singleDataColSpinBox = new QSpinBox(); m_singleDataColSpinBox = new QSpinBox();
m_singleDataColSpinBox->setMinimum(0); m_singleDataColSpinBox->setMinimum(0);
m_singleDataColSpinBox->setMaximum(0); m_singleDataColSpinBox->setMaximum(0);
m_singleDataColSpinBox->setValue(0); m_singleDataColSpinBox->setValue(0);
auto laySingleDataCol = new QHBoxLayout; m_singleDataColSpinBox->setMaximumWidth(50);
laySingleDataCol->addWidget(labelSingleColImport); lay4->addWidget(labelSingleColImport);
laySingleDataCol->addWidget(m_singleDataColSpinBox); lay4->addWidget(m_singleDataColSpinBox);
result->addLayout(laySingleDataCol);
connect(m_singleDataColSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), connect(m_singleDataColSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
[=](int i){ m_singleCol = i; onIntChanged(i); }); [=](int i){ m_singleCol = i; onIntChanged(i); });
auto lay6 = new QVBoxLayout;
auto reloadButton = new QPushButton("Reload");
lay6->addWidget(reloadButton);
reloadButton->setDefault(true);
connect(reloadButton, &QPushButton::clicked, this, &CsvImportAssistant::onReloadButton);
result->addLayout(lay1);
result->addLayout(lay2); result->addLayout(lay2);
result->addSpacing(20);
result->addLayout(lay3);
result->addSpacing(20);
result->addLayout(lay4); result->addLayout(lay4);
result->addLayout(lay6);
return result; return result;
} }
void CsvImportAssistant::onBrowseButton()
{
const QString filter_string = "Intensity File (*.txt *.dat *.ascii *.int *.gz *.tif *.tiff *.csv);;All files (*.*)";
QString fileName = QFileDialog::getOpenFileName(nullptr, QStringLiteral("Open Intensity File"),
this->m_dirName, filter_string);
if(fileName != nullptr){
m_filePathField->setText(fileName);
m_fileName = fileName;
CsvImportAssistant::Reload();
}
}
void CsvImportAssistant::Reload() void CsvImportAssistant::Reload()
{ {
ifstream f(filepath().toStdString()); ifstream f(m_fileName.toStdString());
if(f.good()){ if(f.good()){
generate_table(); generate_table();
}else{ }else{
QMessageBox msgBox; QMessageBox msgBox;
string message = "There was a problem opening the file \"" + filepath().toStdString() + "\""; string message = "There was a problem opening the file \"" + m_fileName.toStdString() + "\"";
message += "\n Check for any errors in the path and try again."; message += "\n Check for any errors in the path and try again.";
msgBox.setText(QString::fromStdString(message)); msgBox.setText(QString::fromStdString(message));
msgBox.setIcon(msgBox.Critical); msgBox.setIcon(msgBox.Critical);
...@@ -257,6 +228,31 @@ unique_ptr<OutputData<double>> CsvImportAssistant::getData() ...@@ -257,6 +228,31 @@ unique_ptr<OutputData<double>> CsvImportAssistant::getData()
} }
} }
} }
else if(m_coordinateName != ""){
//Fill intensity values and coordinate values:
int intensityCol = 1;
int coordinateCol = 0;
vector<double> coordValues;
vector<double> intensityValues;
for(unsigned row=0; row < nDataRows; row++) {
string string_to_parse;
vector<double> parsed_doubles;
string_to_parse = StringVectorVector[row][coordinateCol];
parsed_doubles = DataFormatUtils::parse_doubles(string_to_parse);
coordValues.push_back(parsed_doubles[0]);
string_to_parse = StringVectorVector[row][intensityCol];
parsed_doubles = DataFormatUtils::parse_doubles(string_to_parse);
intensityValues.push_back(parsed_doubles[0]);
}
auto axisName = m_coordinateName.toStdString();
PointwiseAxis coordAxis(axisName, coordValues);
result->addAxis(coordAxis);
for(unsigned i = 0; i < intensityValues.size(); i++)
(*result)[i] = intensityValues[i];
}
else{ else{
result->addAxis("x", nDataCols, 0.0, double(nDataCols)); result->addAxis("x", nDataCols, 0.0, double(nDataCols));
result->addAxis("y", nDataRows, 0.0, double(nDataRows)); result->addAxis("y", nDataRows, 0.0, double(nDataRows));
...@@ -282,11 +278,11 @@ void CsvImportAssistant::generate_table() ...@@ -282,11 +278,11 @@ void CsvImportAssistant::generate_table()
{ {
unique_ptr<CSVFile> csvFile; unique_ptr<CSVFile> csvFile;
try{ try{
csvFile.reset(new CSVFile(filepath().toStdString(), separator())); csvFile.reset(new CSVFile(m_fileName.toStdString(), separator()));
} }
catch(...){ catch(...){
QMessageBox msgBox; QMessageBox msgBox;
string message = "There was a problem opening the file \"" + filepath().toStdString() + "\""; string message = "There was a problem opening the file \"" + m_fileName.toStdString() + "\"";
msgBox.setText(QString::fromStdString(message)); msgBox.setText(QString::fromStdString(message));
msgBox.setIcon(msgBox.Critical); msgBox.setIcon(msgBox.Critical);
msgBox.exec(); msgBox.exec();
...@@ -402,29 +398,6 @@ void CsvImportAssistant::setRowNumbering(){ ...@@ -402,29 +398,6 @@ void CsvImportAssistant::setRowNumbering(){
} }
bool CsvImportAssistant::cell_is_blank(int iRow, int jCol){
if ( m_tableWidget->item(iRow,jCol) == nullptr)
return true;
if ( m_tableWidget->item(iRow,jCol)->text() == nullptr)
return true;
if( m_tableWidget->item(iRow,jCol)->text().trimmed() == "")
return true;
return false;
}
QString CsvImportAssistant::filepath() const{
return m_filePathField->text().trimmed();
}
void CsvImportAssistant::setFilepath(QString fpath){
m_fileName = fpath;
m_filePathField->setText(m_fileName);
}
char CsvImportAssistant::separator() const{ char CsvImportAssistant::separator() const{
char separator; char separator;
QString tmpstr = m_separatorField->text(); QString tmpstr = m_separatorField->text();
...@@ -510,24 +483,21 @@ unsigned CsvImportAssistant::lastLine() const{ ...@@ -510,24 +483,21 @@ unsigned CsvImportAssistant::lastLine() const{
return m_lastDataRow; return m_lastDataRow;
} }
unsigned CsvImportAssistant::singleColumnImport() const{
return unsigned(m_singleDataColSpinBox->value());
}
void CsvImportAssistant::OnColumnClicked(int row, int column) void CsvImportAssistant::OnColumnClicked(int row, int column)
{ {
if(column < 0) return; if(column < -1) return;
if(row < 0) return; if(row < -1) return;
m_tableWidget->clearSelection(); //m_tableWidget->clearSelection();
QModelIndex left = m_tableWidget->model()->index(row, 0); //m_tableWidget->selectionModel()->select
QModelIndex right = m_tableWidget->model()->index(row, m_tableWidget->columnCount() - 1); // QModelIndex left = m_tableWidget->model()->index(row, 0);
QModelIndex top = m_tableWidget->model()->index(0, column); // QModelIndex right = m_tableWidget->model()->index(row, m_tableWidget->columnCount() - 1);
QModelIndex bottom = m_tableWidget->model()->index(m_tableWidget->rowCount() - 1, column); // QModelIndex top = m_tableWidget->model()->index(0, column);
// QModelIndex bottom = m_tableWidget->model()->index(m_tableWidget->rowCount() - 1, column);
QItemSelection selection(left, right);
selection.merge(QItemSelection(top, bottom), QItemSelectionModel::Select); // QItemSelection selection(left, right);
m_tableWidget->selectionModel()->select(selection, QItemSelectionModel::Select); // selection.merge(QItemSelection(top, bottom), QItemSelectionModel::Select);
// m_tableWidget->selectionModel()->select(selection, QItemSelectionModel::Select);
} }
void CsvImportAssistant::onColumnRightClick(const QPoint position) void CsvImportAssistant::onColumnRightClick(const QPoint position)
...@@ -591,6 +561,8 @@ void CsvImportAssistant::onColumnRightClick(const QPoint position) ...@@ -591,6 +561,8 @@ void CsvImportAssistant::onColumnRightClick(const QPoint position)
QMenu *coordMenu = menu.addMenu("Set as coordinate column"); QMenu *coordMenu = menu.addMenu("Set as coordinate column");
coordMenu->setDisabled(m_coordinateCol>0 || m_singleCol > 0); coordMenu->setDisabled(m_coordinateCol>0 || m_singleCol > 0);
//Set column as "Theta" //Set column as "Theta"
QAction setAsTheta("Set as " + relevantHeaders[_theta_],nullptr); QAction setAsTheta("Set as " + relevantHeaders[_theta_],nullptr);
coordMenu->addAction(&setAsTheta); coordMenu->addAction(&setAsTheta);
......
...@@ -37,12 +37,10 @@ class BA_CORE_API_ CsvImportAssistant : public QDialog ...@@ -37,12 +37,10 @@ class BA_CORE_API_ CsvImportAssistant : public QDialog
public: public:
CsvImportAssistant(QString dir = "./", QString file = "", QWidget* parent = nullptr); CsvImportAssistant(QString dir = "./", QString file = "", QWidget* parent = nullptr);
QString filepath() const; QString filepath() const;
void setFilepath(QString fpath);
char separator() const; char separator() const;
void setHeaders(); void setHeaders();
unsigned firstLine() const; unsigned firstLine() const;
unsigned lastLine() const; unsigned lastLine() const;
unsigned singleColumnImport() const;
void Reload(); void Reload();
unique_ptr<OutputData<double>> getData(); unique_ptr<OutputData<double>> getData();
QStringList relevantHeaders = {"Intensity","theta","2theta","q"}; QStringList relevantHeaders = {"Intensity","theta","2theta","q"};
...@@ -52,7 +50,6 @@ public slots: ...@@ -52,7 +50,6 @@ public slots:
void onImportButton(); void onImportButton();
void onReloadButton(); void onReloadButton();
void onRejectButton(); void onRejectButton();
void onBrowseButton();
void onIntChanged(int _ = 0); void onIntChanged(int _ = 0);
void OnColumnClicked(int row, int column); void OnColumnClicked(int row, int column);
void onColumnRightClick(const QPoint position); void onColumnRightClick(const QPoint position);
...@@ -67,9 +64,7 @@ private: ...@@ -67,9 +64,7 @@ private:
void removeBlankColumns(vector<vector<string>> &dataArray); void removeBlankColumns(vector<vector<string>> &dataArray);
void extractDesiredColumns(vector<vector<string>> &dataArray); void extractDesiredColumns(vector<vector<string>> &dataArray);
bool hasEqualLengthLines(vector<vector<string> > &dataArray); bool hasEqualLengthLines(vector<vector<string> > &dataArray);
void convert_table();
void setRowNumbering(); void setRowNumbering();
bool cell_is_blank(int iRow, int jCol);
...@@ -82,7 +77,6 @@ private: ...@@ -82,7 +77,6 @@ private:
unsigned m_singleCol; unsigned m_singleCol;
QTableWidget* m_tableWidget; QTableWidget* m_tableWidget;
QLineEdit* m_filePathField;
QLineEdit* m_separatorField; QLineEdit* m_separatorField;
QSpinBox* m_firstDataRowSpinBox; QSpinBox* m_firstDataRowSpinBox;
QSpinBox* m_singleDataColSpinBox; QSpinBox* m_singleDataColSpinBox;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment