Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omgdatafetcherwizardsearchwidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Tim Sutton tim@linfiniti.com   *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU General Public License     *
00015  *   along with this program; if not, write to the                         *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   59 Temple Place - Suite 330, Boston, MA  02222-2307, USA.             *
00018  ***************************************************************************/
00019 
00020 #include <omgui.h> //ancilliary helper functions
00021 #include <omgdatafetcherwizardsearchwidget.h>
00022 #include <omgscraperpluginregistry.h>
00023 #include <QSettings>
00024 #include <QStringList>
00025 #include <QFileDialog>
00026 #include <QTextStream>
00027 #include <QMessageBox>
00028 OmgDataFetcherWizardSearchWidget::OmgDataFetcherWizardSearchWidget(QWidget* parent , Qt::WFlags fl )
00029 {
00030   setupUi(this);
00031   QSettings mySettings;
00032   //set the gui defaults
00033   QString myPluginName = mySettings.value("locScraper/scraperPluginName", "GBIF REST Web Service Plugin").toString();
00034   QStringList myList = OmgScraperPluginRegistry::instance()->names();
00035   cboScraperPluginName->addItems(myList);
00036   if (myList.contains(myPluginName))
00037   {
00038     cboScraperPluginName->setCurrentIndex(cboScraperPluginName->findText(myPluginName));
00039   }
00040 
00041   leSearchString->setText(mySettings.value("locScraper/searchString", "").toString());
00042   leOutputPath->setText(mySettings.value("locScraper/outputPath", "").toString());
00043   spinMinimumRecords->setValue(mySettings.value("locScraper/minimumRecords", 20).toInt());
00044   connect(leOutputPath, SIGNAL(editingFinished()), 
00045       this, SLOT(outputPathChanged()));
00046 }
00047 OmgDataFetcherWizardSearchWidget::~OmgDataFetcherWizardSearchWidget()
00048 {
00049   QSettings mySettings;
00050   mySettings.setValue("locScraper/searchString",leSearchString->text());
00051   mySettings.setValue("locScraper/outputPath",leOutputPath->text());
00052   mySettings.setValue("locScraper/scraperPluginName",cboScraperPluginName->currentText() );
00053   mySettings.setValue("locScraper/minimumRecords",spinMinimumRecords->value());
00054 }
00055 QListWidget * OmgDataFetcherWizardSearchWidget::getLstSearch()
00056 {
00057   return lstSearch;
00058 }
00059 QLineEdit * OmgDataFetcherWizardSearchWidget::getLeOutputPath()
00060 {
00061   return leOutputPath;
00062 }
00063 void OmgDataFetcherWizardSearchWidget::on_toolSelectSearchFileName_clicked()
00064 {
00065   QSettings mySettings;
00066   QString myFileNameQString = QFileDialog::getOpenFileName(
00067       this,
00068       tr("Choose a file to load"),
00069       mySettings.value("locScraper/searchFilePath", ".").toString(),
00070       "Text files (*.txt)");
00071   setSearchList(myFileNameQString);
00072   mySettings.setValue("locScraper/searchFilePath",myFileNameQString);
00073   emit dataChanged();
00074 }
00075 
00076 void OmgDataFetcherWizardSearchWidget::setSearchList(QString theFileName)
00077 {
00078   //
00079   // Add all search strings from text file
00080   //
00081   //first build a regex to match text at the beginning of the line
00082   QRegExp myQRegExp( "^[^#][ a-zA-Z\\-]*" ); //seconf caret means 'not'
00083   QStringList myTaxonQStringList;;
00084   QFile myQFile( theFileName );
00085   if ( myQFile.open( QIODevice::ReadOnly ) )
00086   {
00087     //now we parse the file, checking each line for its taxon string
00088     QTextStream myQTextStream( &myQFile );
00089     QString myCurrentLineQString;
00090     while ( !myQTextStream.atEnd() )
00091     {
00092       myCurrentLineQString = myQTextStream.readLine(); // line of text excluding '\n'
00093       int  myPosInt = myQRegExp.indexIn( myCurrentLineQString,0 );
00094       if (myPosInt<0) continue;
00095       QStringList myMatchesQStringList = myQRegExp.capturedTexts();
00096       QStringList::Iterator myIterator = myMatchesQStringList.begin();
00097       QString myTaxonQString=*myIterator;
00098       myTaxonQString=myTaxonQString.simplified();
00099       if (myTaxonQString != "")
00100       {
00101         //make sure there are only single spaces separating words.
00102         myTaxonQString=myTaxonQString.replace( QRegExp(" {2,}"), " " );
00103         myTaxonQStringList.append(myTaxonQString);
00104       }
00105     }
00106     myQFile.close();
00107     //sort the taxon list alpabetically
00108     myTaxonQStringList.sort();
00109     //now find the uniqe entries in the qstringlist and
00110     //add each entry to the list
00111     QString myLastTaxon="";
00112     QStringList::Iterator myIterator= myTaxonQStringList.begin();
00113     while( myIterator!= myTaxonQStringList.end() )
00114     {
00115       QString myCurrentTaxon=*myIterator;
00116       if (myCurrentTaxon!=myLastTaxon)
00117       {
00118         lstSearch->addItem(myCurrentTaxon);
00119       }
00120       myLastTaxon=*myIterator;
00121       ++myIterator;
00122     }
00123   }
00124   else
00125   {
00126     QMessageBox::warning( this,QString(tr("Localities Search  Error")),QString(tr("The file is not readable. Check you have the neccessary file permissions and try again.")));
00127     return;
00128   }
00129 
00130 }
00131 
00132 void OmgDataFetcherWizardSearchWidget::on_toolAddSearchItem_clicked()
00133 {
00134   lstSearch->addItem(leSearchString->text());
00135   emit dataChanged();
00136 }
00137 
00138 void OmgDataFetcherWizardSearchWidget::on_toolDeleteItem_clicked()
00139 {
00140 
00141   unsigned int myItemsCount = static_cast<unsigned int>(lstSearch->count());
00142   for ( unsigned int myInt = 0; myInt < myItemsCount; myInt++ )
00143   {
00144     QListWidgetItem *myItem = lstSearch->item( myInt );
00145     // if the item is selected...
00146     if ( lstSearch->isItemSelected(myItem) )
00147     {
00148       //remove the item if it is selected
00149       //this method is described in the qt docs but gives a compile errror saying the error is non existant!
00150       //lstProjectionLayers->removeItem(myInt);
00151       //so we kludge it for now!
00152       lstSearch->takeItem(myInt);
00153       myInt--;
00154       myItemsCount--;
00155     }
00156   }
00157   emit dataChanged();
00158 }
00159 
00160 void OmgDataFetcherWizardSearchWidget::on_toolSelectOutputPath_clicked()
00161 {
00162   QString myOutputPathQString = QFileDialog::getExistingDirectory(
00163       this,
00164       tr("Choose an output folder"),
00165       "ShowDirsOnly");
00166   leOutputPath->setText(myOutputPathQString);
00167   emit dataChanged();
00168 
00169 }
00170 
00171 void OmgDataFetcherWizardSearchWidget::outputPathChanged()
00172 {
00173   emit dataChanged();
00174 }
00175 

Generated on Mon Apr 28 15:07:01 2008 for openModellerDesktop by  doxygen 1.4.1-20050210