1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/****************************************************************************
** Copyright (c) 2016, Adel Kara Slimane <adel.ks@zegrapher.com>
**
** This file is part of ZeGrapher's source code.
**
** ZeGrapher is free software: you may copy, redistribute and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This file is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "csvhandler.h"
CSVhandler::CSVhandler(QWidget *parent): QDialog(parent), ui(new Ui::CSVconfig)
{
ui->setupUi(this);
fileDialog = new QFileDialog(this);
fileDialog->setNameFilter(tr("Data (*.csv)"));
job = CSV_NO_FILE;
setWindowIcon(QIcon(":/icons/csv.png"));
connect(ui->apply, SIGNAL(released()), this, SLOT(apply()));
connect(ui->askForFileLocation, SIGNAL(released()), this, SLOT(askForFileLocation()));
connect(ui->cancel, SIGNAL(released()), this, SLOT(close()));
}
void CSVhandler::askForFileLocation()
{
if(fileDialog->exec())
{
ui->fileLocation->setText(fileDialog->selectedFiles().first());
if(job == CSV_FILE_SAVE && !ui->fileLocation->text().endsWith(".csv"))
ui->fileLocation->setText(ui->fileLocation->text()+".csv");
}
}
void CSVhandler::getDataFromCSV()
{
setWindowTitle(tr("Open data from file"));
ui->apply->setText(tr("Open"));
ui->fileLocation->clear();
job = CSV_FILE_OPEN;
fileDialog->setAcceptMode(QFileDialog::AcceptOpen);
exec();
}
void CSVhandler::saveCSV(QList<QStringList> data)
{
values = data;
removeUnnecessaryCells();
setWindowTitle(tr("Save data"));
ui->apply->setText(tr("Save"));
ui->fileLocation->clear();
job = CSV_FILE_SAVE;
fileDialog->setAcceptMode(QFileDialog::AcceptSave);
exec();
}
void CSVhandler::removeUnnecessaryCells()
{
bool unnecessary = true;
int col = 0, row = 1;
for(row = 0 ; row < values.size(); row++)
for(col = 0 ; col < values[row].size(); col++)
if(values[row][col] == " ")
values[row][col].clear();
//start by removing unnecessary columns, at first, "values" is a square matrix, that's why it's possbile to ask for values[0].size()
col = 0;
row = 1;
while(col < values[0].size())
{
unnecessary = true;
for(row = 1 ; row < values.size() && unnecessary ; row++)
unnecessary = values[row][col].isEmpty();
if(unnecessary)
for(row = 0 ; row < values.size() ; row++)
values[row].removeAt(col);
else col++;
}
//removing unnecessary rows
row = 0;
while(row < values.size())
{
unnecessary = true;
for(col = 0 ; col < values[0].size() && unnecessary ; col++)
unnecessary = values[row][col].isEmpty();
if(unnecessary)
values.removeAt(row);
else row++;
}
//removing empty cells at the end of each line
for(row = 0 ; row < values.size(); row++)
while(values[row].last().isEmpty()) {values[row].removeLast();}
}
void CSVhandler::apply()
{
if(ui->fileLocation->text().isEmpty())
{
QMessageBox::warning(this, tr("Error"), tr("Target file was not specified."));
return;
}
if(ui->customSeparator->isChecked() and ui->delimiter->text().isEmpty())
{
QMessageBox::warning(this, tr("Error"), tr("Separator was not specified."));
return;
}
if(job == CSV_FILE_SAVE)
{
QFile file(ui->fileLocation->text());
if(file.open(QFile::WriteOnly | QFile::Truncate | QFile::Text))
{
QTextStream out(&file);
QString delimiter = ui->tabSeparator->isChecked() ? "\t" : ui->delimiter->text();
for(int i = 0 ; i < values.size(); i++)
out << values[i].join(delimiter) << '\n';
file.close();
}
}
else if(job == CSV_FILE_OPEN)
{
QFile file(ui->fileLocation->text());
values.clear();
if(file.open(QFile::ReadOnly | QFile::Text))
{
QTextStream in(&file);
QString delimiter = ui->tabSeparator->isChecked() ? "\t" : ui->delimiter->text();
while(!in.atEnd())
values << in.readLine().split(delimiter);
file.close();
emit dataFromCSV(values);
}
}
close();
}
|