📄 dataconvertor.cpp
字号:
//Data Convertor v0.1 Copyright (C) 2009 Dan
#include<QtGui>
#include<QProgressDialog>
#include"dataconvertor.h"
DataConvertor::DataConvertor(QWidget *parent):QDialog(parent)
{
setupUi(this);
okButton->setEnabled(false); // disable OK button
// set default settings for the output.
fourInFourOut->setChecked(true);
cloudCheckBox->setChecked(true);
windSpeedCheckBox->setChecked(true);
connect(inputFileBrowseButton,SIGNAL(clicked()),this,SLOT(inputFileNameBrowse()));
connect(outputFileBrowseButton,SIGNAL(clicked()),this,SLOT(outputFileDirectoryBrowse()));
connect(aboutButton,SIGNAL(clicked()),this,SLOT(aboutinfo()));
connect(okButton,SIGNAL(clicked()),this,SLOT(processFile()));
}
void DataConvertor::inputFileNameBrowse()
{
QString initialName=inputFileNameComboBox->currentText();
if(initialName.isEmpty())
initialName=QDir::homePath();
QString inputFileName=QFileDialog::getOpenFileName(this,
tr("Open file"), initialName);
inputFileName=QDir::toNativeSeparators(inputFileName);
if(!inputFileName.isEmpty())
{
inputFileNameComboBox->addItem(inputFileName);
//when it's empty, the index is 0.
inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->count() - 1);
// set output file the same location with the inputFile if it is empty.
if(outputFileDirectoryComboBox->currentText().isEmpty())
{
outputFileDirectoryComboBox->addItem(QDir::toNativeSeparators(QFileInfo(inputFileName).path()));
outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->count() - 1);
}
okButton->setEnabled(true); // enable the ok button
}
}
void DataConvertor::outputFileDirectoryBrowse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Open file"), QDir::currentPath());
if (!directory.isEmpty())
{
outputFileDirectoryComboBox->addItem(QDir::toNativeSeparators(directory));
outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->currentIndex() + 1);
}
}
void DataConvertor::processFile()
{
okButton->setEnabled(false); // disable OK button
QString fileName=inputFileNameComboBox->currentText();
if(fileName.isEmpty())
{
QMessageBox::information(this,tr("Data Convertor"),tr("Not such a file, Please reselect a file."),QMessageBox::Ok);
}
else
{
QFile file(fileName);
//Open read file.
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(this,tr("Data Convertor"),tr("Cann't read file %1:\n%2.").arg(file.fileName()).arg(file.errorString()),QMessageBox::Cancel);
okButton->setEnabled(false);
return;
}
//Open write directory.
QString outDirName=outputFileDirectoryComboBox->currentText();
QDir outDir(outDirName);
///
//if the directory is not existed, create one.
if(!outDir.exists())
{
outDir.mkpath(outDirName);
}
QTextStream in(&file);
// form outfile name for all the year.
QString path=outDirName+QDir::separator()+"year.txt";
QFile yearFile(path);
if(!yearFile.open(QIODevice::WriteOnly|QIODevice::Text))
{
QMessageBox::information(this,tr("Data Convertor"),tr("Cann't open write file %1:\n%2.").arg(yearFile.fileName()).arg(yearFile.errorString()),QMessageBox::Cancel);
okButton->setEnabled(true);
return;
}
QTextStream yearOut(&yearFile);
// setup QProgressDailog
QProgressDialog progressDialog(this);
progressDialog.setCancelButtonText(tr("&Cancel"));
progressDialog.setRange(0, file.size());
progressDialog.setWindowTitle(tr("Processing file..."));
QString line;
QFile *monthFile = NULL;
QTextStream monthOut;
while(in.pos()<file.size())
{
progressDialog.setValue(file.pos());
progressDialog.setLabelText(tr("Processing file now %1 of %2. Please wait...")
.arg(file.pos()).arg(file.size()));
qApp->processEvents();
if (progressDialog.wasCanceled())
break;
line=in.readLine();
QString monthFileName = line.left(6) + ".txt";
QStringList list=line.split(QRegExp("\\s+"));
// Validate the options if it conform the input Data
if(fourInFourOut->isChecked()||fourInThreeOut->isChecked())
{
if(list.count()!=17)
{
QMessageBox::critical(this,tr("Data Convertor"),tr("You must be select a wrong file or select the wrong options!"),QMessageBox::Cancel);
return;
}
}
else
{
if(list.count()!=13)
{
QMessageBox::critical(this,tr("Data Convertor"),tr("You must be select a wrong file or select the wrong options!"),QMessageBox::Cancel);
return;
}
}
// in this way to write data to every month.
if(monthFile == NULL || monthFile->fileName() != monthFileName)
{
if(monthFile!=NULL) // first time should not delete
delete monthFile;
monthFile = new QFile(outDirName+QDir::separator()+monthFileName);
monthFile->open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text);
monthOut.setDevice(monthFile);
}
// Data configuration area for output. here will process the
if(fourInFourOut->isChecked())
fourInFourOutFile(list,monthOut,yearOut);
else
{
if(threeInThreeOut->isChecked())
threeInThreeOutFile(list,monthOut,yearOut);
else
{
if(fourInThreeOut->isChecked())
fourInThreeOutFile(list,monthOut,yearOut);
}
}
}
delete monthFile;
}
okButton->setEnabled(true); // enable the ok button
}
void DataConvertor::aboutinfo()
{
QMessageBox::information(this,tr("About DataConvertor"),tr(" Copyright (C) 2009 Dan \n"
" Contact: fightfortomorrow@yahoo.cn\n"
" This application is generated from the OPEN SOURCE\n"
"VERSION OF Qt under the GNU General Public Licens. \n"
"This program is distributed in the hope that it will be useful."),QMessageBox::Ok);
}
//trim cloud, make it between 0~10
void DataConvertor::trimCloud(QString& cloud)
{
if(cloud<"0")
{
cloud="0";
}
else
{
if(cloud>"10")
cloud="10";
}
}
//windspeed divide by ten.
void DataConvertor::divideByTen(QString& windspeed)
{
double val=windspeed.toDouble();
val/=10.0;
windspeed=QString::number(val);
}
void DataConvertor::fourInFourOutFile(QStringList& list,QTextStream& monthOut,QTextStream& yearOut)
{
if(cloudCheckBox->isChecked())
{
for(size_t i=9;i<17;++i)
{
trimCloud(list[i]);
}
}
if(windSpeedCheckBox->isChecked())
{
for(size_t i=1;i<5;++i)
{
divideByTen(list[2*i]);
}
}
// output stream monthOut and then yearOut
monthOut<<list[1]<<'\t'<<list[2]<<'\t'<<list[9]<<'\t'<<list[13]<<"\n";
monthOut<<list[3]<<'\t'<<list[4]<<'\t'<<list[10]<<'\t'<<list[14]<<"\n";
monthOut<<list[5]<<'\t'<<list[6]<<'\t'<<list[11]<<'\t'<<list[15]<<"\n";
monthOut<<list[7]<<'\t'<<list[8]<<'\t'<<list[12]<<'\t'<<list[16]<<"\n";
yearOut<<list[0]<<'\t'<<"2 "<<'\t'<<list[1]<<'\t'<<list[2]<<'\t'<<list[9]<<'\t'<<list[13]<<"\n";
yearOut<<list[0]<<'\t'<<"8 "<<'\t'<<list[3]<<'\t'<<list[4]<<'\t'<<list[10]<<'\t'<<list[14]<<"\n";
yearOut<<list[0]<<'\t'<<"14"<<'\t'<<list[5]<<'\t'<<list[6]<<'\t'<<list[11]<<'\t'<<list[15]<<"\n";
yearOut<<list[0]<<'\t'<<"20"<<'\t'<<list[7]<<'\t'<<list[8]<<'\t'<<list[12]<<'\t'<<list[16]<<"\n";
}
void DataConvertor::threeInThreeOutFile(QStringList& list,QTextStream& monthOut,QTextStream& yearOut)
{
if(cloudCheckBox->isChecked())
{
for(size_t i=7;i<13;++i)
{
trimCloud(list[i]);
}
}
if(windSpeedCheckBox->isChecked())
{
divideByTen(list[2]);
divideByTen(list[4]);
divideByTen(list[6]);
}
// output stream monthOut and then yearOut
monthOut<<list[1]<<'\t'<<list[2]<<'\t'<<list[7]<<'\t'<<list[10]<<"\n";
monthOut<<list[3]<<'\t'<<list[4]<<'\t'<<list[8]<<'\t'<<list[11]<<"\n";
monthOut<<list[5]<<'\t'<<list[6]<<'\t'<<list[9]<<'\t'<<list[12]<<"\n";
yearOut<<list[0]<<'\t'<<"8 "<<'\t'<<list[1]<<'\t'<<list[2]<<'\t'<<list[7]<<'\t'<<list[10]<<"\n";
yearOut<<list[0]<<'\t'<<"14"<<'\t'<<list[3]<<'\t'<<list[4]<<'\t'<<list[8]<<'\t'<<list[11]<<"\n";
yearOut<<list[0]<<'\t'<<"20"<<'\t'<<list[5]<<'\t'<<list[6]<<'\t'<<list[9]<<'\t'<<list[12]<<"\n";
}
void DataConvertor::fourInThreeOutFile(QStringList& list,QTextStream& monthOut,QTextStream& yearOut)
{
if(cloudCheckBox->isChecked())
{
for(size_t i=9;i<17;++i)
{
trimCloud(list[i]);
}
}
if(windSpeedCheckBox->isChecked())
{
for(size_t i=1;i<5;++i)
{
divideByTen(list[2*i]);
}
}
// output stream monthOut and then yearOut
//monthOut<<list[1]<<'\t'<<list[2]<<'\t'<<list[9]<<'\t'<<list[13]<<"\n";
monthOut<<list[3]<<'\t'<<list[4]<<'\t'<<list[10]<<'\t'<<list[14]<<"\n";
monthOut<<list[5]<<'\t'<<list[6]<<'\t'<<list[11]<<'\t'<<list[15]<<"\n";
monthOut<<list[7]<<'\t'<<list[8]<<'\t'<<list[12]<<'\t'<<list[16]<<"\n";
//yearOut<<list[0]<<'\t'<<"2 "<<list[1]<<'\t'<<list[2]<<'\t'<<list[9]<<'\t'<<list[13]<<"\n";
yearOut<<list[0]<<'\t'<<"8 "<<'\t'<<list[3]<<'\t'<<list[4]<<'\t'<<list[10]<<'\t'<<list[14]<<"\n";
yearOut<<list[0]<<'\t'<<"14"<<'\t'<<list[5]<<'\t'<<list[6]<<'\t'<<list[11]<<'\t'<<list[15]<<"\n";
yearOut<<list[0]<<'\t'<<"20"<<'\t'<<list[7]<<'\t'<<list[8]<<'\t'<<list[12]<<'\t'<<list[16]<<"\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -