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

omglocalitiesmodel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Tim Sutton   *
00003  *   tim@linfiniti.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "omglocality.h"  //provides omglocalities, omgsamples
00022 #include "omglocalitiesmodel.h" 
00023 #include <QStringList>
00024 #include <QFileInfo>
00025 
00026 OmgLocalitiesModel::OmgLocalitiesModel()
00027 {
00028 
00029 }
00030 OmgLocalitiesModel::~OmgLocalitiesModel()
00031 {
00032 
00033 }
00034 void OmgLocalitiesModel::setModel(OmgModel * const thepNicheModel)
00035 {
00036   if (rowCount() > 0)
00037   {
00038     //first clear the table
00039     beginRemoveRows(QModelIndex(),0,rowCount()-1);
00040     endRemoveRows();
00041   }
00042   if (columnCount() > 0)
00043   {
00044     beginRemoveColumns(QModelIndex(),0,columnCount()-1);
00045     endRemoveColumns();
00046   }
00047   //update the localities
00048   mpNicheModel = thepNicheModel;;
00049   //now repopulate it - dont remove the if statements even if they look
00050   //unintuitive...
00051   if (rowCount() > 1)
00052   {
00053     beginInsertRows(QModelIndex(),0,rowCount()-1); //notify any views of data updates
00054     endInsertRows(); //notify any views of data updates
00055   }
00056   if (columnCount() > 1)
00057   {
00058     beginInsertColumns(QModelIndex(),0,columnCount()-1); //notify any views of data updates
00059     endInsertColumns(); //notify any views of data updates
00060   }
00061 }
00062 int OmgLocalitiesModel::columnCount ( const QModelIndex & theParent /*= QModelIndex()*/ ) const
00063 {
00064   //to calculate we get the first locality element,
00065   //then find out how meny samples it contains and add 4 for
00066   // - id
00067   // - lat
00068   // - long
00069   // - abundance
00070   // if there are no localities simlpy return 0
00071   if (!mpNicheModel)
00072   {
00073     return 0;
00074   }
00075 
00076   if (mpNicheModel->localities().count() < 1)
00077   {
00078     return 0;
00079   }
00080   else
00081   {
00082     int myCount = mpNicheModel->localities().at(0).samples().count() + 4;
00083     return myCount;
00084   }
00085 }
00086 int OmgLocalitiesModel::rowCount ( const QModelIndex & theParent /*= QModelIndex()*/ ) const
00087 {
00088   if (!mpNicheModel)
00089   {
00090     return 0;
00091   }
00092   if (!mpNicheModel->localities().count())
00093   {
00094     return 0;
00095   }
00096   return mpNicheModel->localities().count();
00097 }
00098 QVariant OmgLocalitiesModel::data ( const QModelIndex & theIndex, 
00099     int theRole /*= Qt::DisplayRole*/ ) const
00100 {
00101   if (!mpNicheModel)
00102   {
00103     return QVariant();
00104   }
00105   if (!theIndex.isValid())
00106   {
00107     return QVariant();
00108   }
00109   if (theRole != Qt::DisplayRole)
00110   {
00111     return QVariant();
00112   }
00113   //to get teh data we need to first check if the 
00114   //theIndex.row() is valid and then if the column specified
00115   //in the index is valid.
00116   //The column is mapped to the locality like this
00117   //0 - id
00118   //1 - lon
00119   //2 - lat
00120   //3 - abundance
00121   //>3 - samples.at(column-4)
00122   if (!mpNicheModel->localities().count())
00123   {
00124     return QVariant();
00125   }
00126   if (theIndex.row() > mpNicheModel->localities().count())
00127   {
00128     return QVariant();
00129   }
00130   if (theIndex.column() == 0 ) //no need to get the samples
00131   {
00132     // Id
00133     return QVariant(mpNicheModel->localities().at(theIndex.row()).id());
00134   }
00135   if (theIndex.column() == 1 ) //no need to get the samples
00136   {
00137     //long
00138     return QVariant(mpNicheModel->localities().at(theIndex.row()).longitude());
00139   }
00140   if (theIndex.column() == 2 ) //no need to get the samples
00141   {
00142     //lat
00143     return QVariant(mpNicheModel->localities().at(theIndex.row()).latitude());
00144   }
00145   if (theIndex.column() == 3 ) //no need to get the samples
00146   {
00147     //abundance
00148     return QVariant(mpNicheModel->localities().at(theIndex.row()).abundance());
00149   }
00150   //must be looking for the actual sample data so 
00151   //subtract 3 to offset the above 3 recs
00152   unsigned int mySampleNumber = theIndex.column() - 4;
00153   return QVariant(mpNicheModel->localities().at(theIndex.row()).samples().at(mySampleNumber));
00154 }
00155 
00156 
00157 QVariant OmgLocalitiesModel::headerData(int theSection, Qt::Orientation theOrientation,
00158                                            int theRole) const
00159 {
00160   if (!mpNicheModel)
00161   {
00162     return QVariant();
00163   }
00164   if (theRole != Qt::DisplayRole)
00165   {
00166     return QVariant();
00167   }
00168   if (theOrientation == Qt::Horizontal)
00169   {
00170     //first three cols have fixed names...
00171     if (theSection==0)
00172     {
00173       return QString(tr("ID"));
00174     }
00175     if (theSection==1)
00176     {
00177       return QString(tr("Lon"));
00178     }
00179     if (theSection==2)
00180     {
00181       return QString(tr("Lat"));
00182     }
00183     if (theSection==3)
00184     {
00185       return QString(tr("Count"));
00186     }
00187 
00188     QStringList myList = mpNicheModel->creationLayerNames();
00189     //we offset the section no by 3 to take into account
00190     //the three non layer name headers above
00191     if (myList.count() > (theSection-4))
00192     {
00193       QFileInfo myFileInfo(myList.at(theSection-4));
00194       return QString(myFileInfo.baseName());
00195     }
00196     else
00197     {
00198       //final stopgap measure
00199       return QString("Layer %1").arg(theSection);
00200     }
00201   }
00202   else
00203   {
00204     // do nothing for row headers
00205     return QString("%1").arg(theSection);
00206   }
00207   return QVariant();
00208 }

Generated on Mon Apr 28 15:08:46 2008 for openModellerDesktop by  doxygen 1.4.1-20050210