00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "omgtextfilesplitter.h"
00021 #include <qfile.h>
00022 #include <qtextstream.h>
00023 #include <qregexp.h>
00024 #include <qstring.h>
00025 #include <qlineedit.h>
00026 #include <qfiledialog.h>
00027 #include <qfileinfo.h>
00028 #include <qsettings.h>
00029 #include <qapplication.h>
00030 #include <qcursor.h>
00031 #include <QRadioButton>
00032 #include <QMessageBox>
00033
00034 OmgTextFileSplitter::OmgTextFileSplitter() : QDialog()
00035 {
00036 setupUi(this);
00037 QSettings myQSettings;
00038 leFileName->setText(myQSettings.value("/TextFileSplitter/leFileName").toString());
00039 leDirName->setText(myQSettings.value("/TextFileSplitter/leDirName").toString());
00040 }
00041
00042
00043 OmgTextFileSplitter::~OmgTextFileSplitter()
00044 {}
00045
00046 void OmgTextFileSplitter::split(QString theFileNameString, QString theOutputDirString, InputType theInputType=Default)
00047 {
00048
00049
00050
00051
00052
00053 QRegExp myQRegExp;
00054 if (theInputType==Default)
00055 {
00056 qDebug( "Genus species headers file" );
00057 myQRegExp = QRegExp("^[^#][a-zA-Z][ a-zA-Z\t]*") ;
00058 }
00059 else
00060 {
00061 qDebug("Comma Delimited File or openModeller format file");
00062 myQRegExp=QRegExp("");
00063 }
00064 QString myTaxonName;
00065 QFile myInputFile (theFileNameString);
00066
00067
00068
00069 QFile myOutputFile (theFileNameString);
00070 QTextStream myOutputTextStream( &myOutputFile );
00071 if ( myInputFile.open( QIODevice::ReadOnly ) )
00072 {
00073
00074 QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
00075
00076
00077 QTextStream myInputTextStream( &myInputFile );
00078 QString myCurrentLineQString;
00079 QString myLastTaxonName = "";
00080 int myFileCount=0;
00081 while ( !myInputTextStream.atEnd() )
00082 {
00083 QString myLong, myLat;
00084 QStringList myList;
00085
00086 myCurrentLineQString = myInputTextStream.readLine();
00087
00088 if (myCurrentLineQString.simplified().isEmpty())
00089 {
00090 continue;
00091 }
00092
00093
00094
00095
00096 QString myLine = myCurrentLineQString;
00097 myLine = myLine.simplified().replace("\t"," ");
00098 myLine = myLine.trimmed();
00099
00100
00101
00102
00103 if (theInputType==Default)
00104 {
00105
00106 myList = myLine.split(" ");
00107 if (myQRegExp.lastIndexIn(myCurrentLineQString) != -1)
00108 {
00109
00110 QStringList myMatchesQStringList = myQRegExp.capturedTexts();
00111 QStringList::Iterator myIterator = myMatchesQStringList.begin();
00112 myTaxonName=*myIterator;
00113
00114 myTaxonName=myTaxonName.simplified();
00115 if (myTaxonName != "" && myMatchesQStringList.size() > 0)
00116 {
00117
00118 myTaxonName=myTaxonName.replace( QRegExp("\t"), " " );
00119
00120 myTaxonName=myTaxonName.replace( QRegExp(" {2,}"), " " );
00121
00122 myTaxonName=myTaxonName.replace( QRegExp("Latitude"), "" );
00123 myTaxonName=myTaxonName.replace( QRegExp("Longitude"), "" );
00124 myTaxonName=myTaxonName.simplified();
00125 }
00126 }
00127 }
00128 else if (theInputType==OpenModeller)
00129 {
00130 myList = myLine.split(" ");
00131 if (myList.size() < 4)
00132 {
00133 continue;
00134 }
00135 myTaxonName=myList.at(0).simplified() + " " + myList.at(1).simplified();
00136 }
00137 else
00138 {
00139 myList = myLine.split(",");
00140 if (myList.size() < 4)
00141 {
00142 continue;
00143 }
00144 myTaxonName=myList.at(0).simplified() + " " + myList.at(1).simplified();
00145 }
00146
00147 if (myLastTaxonName!=myTaxonName)
00148 {
00149 ++myFileCount;
00150
00151 QString myOutputFileName = myTaxonName;
00152 myOutputFileName.replace(" ","_");
00153 myOutputFileName = myOutputFileName+".txt";
00154
00155
00156 myOutputFile.close();
00157 myOutputFile.setFileName(theOutputDirString + QDir::separator() + myOutputFileName);
00158
00159 myOutputFile.open(QIODevice::WriteOnly );
00160 myOutputTextStream.setDevice(&myOutputFile);
00161 }
00162
00163 myLastTaxonName=myTaxonName;
00164 if (theInputType==Default)
00165 {
00166 if (myList.size() < 2 )
00167 {
00168 continue;
00169 }
00170 if (radLongitudeFirst->isChecked())
00171 {
00172 myLong = myList.at(0);
00173 myLat = myList.at(1);
00174 }
00175 else
00176 {
00177 myLong = myList.at(1);
00178 myLat = myList.at(0);
00179 }
00180 }
00181 else
00182 {
00183 if (myList.size() < 4 )
00184 {
00185 continue;
00186 }
00187 if (radLongitudeFirst->isChecked())
00188 {
00189
00190 myLong = myList.at(2);
00191 myLat = myList.at(3);
00192 }
00193 else
00194 {
00195 myLong = myList.at(3);
00196 myLat = myList.at(2);
00197 }
00198
00199 }
00200
00201 myOutputTextStream << myTaxonName.toLocal8Bit() << " " << myLong << " " << myLat << "\r\n";
00202 }
00203 myInputFile.close();
00204 myOutputFile.close();
00205 QApplication::restoreOverrideCursor();
00206 QMessageBox::information( this,tr("openModeller Desktop"),tr("File splitter completed successfully. ") +
00207 QString::number(myFileCount) + tr(" new files were created.") );
00208 }
00209 }
00210
00211
00212
00213
00214
00215
00216
00217 void OmgTextFileSplitter::on_pbnFileSelector_clicked()
00218 {
00219 qDebug("File Selector Button clicked");
00220 QFileInfo myFileInfo(leFileName->text());
00221 QString myDirPath = myFileInfo.dir().dirName();
00222 QString myFileName = QFileDialog::getOpenFileName(
00223 this,
00224 "Choose a file containing occurrence data",
00225 myDirPath,
00226 "Text Files (*.txt *.csv *.asc)");
00227 if (!myFileName.isEmpty())
00228 {
00229 leFileName->setText(myFileName);
00230 }
00231
00232 }
00233
00234
00235
00236
00237
00238 void OmgTextFileSplitter::on_pbnDirectorySelector_clicked()
00239 {
00240 qDebug("Directory Selector Button clicked");
00241 QString myDirPath = QFileDialog::getExistingDirectory(
00242 this,
00243 "Choose a directory for the split files",
00244 leDirName->text()
00245 );
00246 if (!myDirPath.isEmpty())
00247 {
00248 leDirName->setText(myDirPath);
00249 }
00250 }
00251 void OmgTextFileSplitter::accept()
00252 {
00253 QSettings myQSettings;
00254 myQSettings.setValue("/TextFileSplitter/leFileName",leFileName->text());
00255 myQSettings.setValue("/TextFileSplitter/leDirName",leDirName->text());
00256 if (radCommaDelimited->isChecked())
00257 {
00258 split(leFileName->text(),leDirName->text(),CommaDelimited);
00259 }
00260 else if (radOpenModeller->isChecked())
00261 {
00262 split(leFileName->text(),leDirName->text(),OpenModeller);
00263 }
00264 else
00265 {
00266 split(leFileName->text(),leDirName->text(),Default);
00267 }
00268 close();
00269 }