AnnotationObjectManager.qt.C

00001 #ifndef ANNOTATIONOBJECTMANAGER_C
00002 #define ANNOTATIONOBJECTMANAGER_C
00003 
00004 #include "NeovisionII/NeoAnnotate/AnnotationObjectManager.qt.H"
00005 #include "Util/log.H"
00006 #include "Util/Assert.H"
00007 
00008 //######################################################################
00009 AnnotationObjectManager::AnnotationObjectManager(QObject * parent) :
00010   QAbstractTableModel(parent)
00011 {
00012   itsCurrentSelectedRow = -1;
00013 }
00014 
00015 void AnnotationObjectManager::setAnimationView(QTableView *animationView)
00016 {
00017   itsAnimationView = animationView;
00018 }
00019 
00020 //######################################################################
00021 int AnnotationObjectManager::rowCount(const QModelIndex &parent) const
00022 {
00023   return itsObjects.size();
00024 }
00025 
00026 //######################################################################
00027 int AnnotationObjectManager::columnCount(const QModelIndex &parent) const
00028 {
00029   return 3;
00030 }
00031 
00032 //######################################################################
00033 QVariant AnnotationObjectManager::data(const QModelIndex &index, int role) const
00034 {
00035 
00036   //Make sure we are trying to grab data from a valid table cell
00037   if(role != Qt::DisplayRole || !index.isValid())
00038     return QVariant();
00039   if(index.row() >= itsObjects.size() || index.row() < 0)
00040     return QVariant();
00041 
00042   //Make sure we actually have data to grab
00043   if(itsObjects.size() <= 0) return QVariant();
00044 
00045   //Get a reference to the object in question based on the row
00046   AnnotationObject* obj = itsObjects[index.row()];
00047 
00048   //Grab the proper data from the object based on the indicated column
00049   QVariant ret;
00050   switch(index.column())
00051   {
00052     case 0:
00053       ret = obj->getId();
00054       break;
00055     case 1:
00056       ret = obj->getDescription();
00057       break;
00058     case 2:
00059       ret = obj->getType();
00060       break;
00061   }
00062 
00063   return ret;
00064 }
00065 
00066 //######################################################################
00067 QVariant AnnotationObjectManager::headerData(int section, Qt::Orientation orientation,
00068     int role) const
00069 {
00070   if(role != Qt::DisplayRole)
00071     return QVariant();
00072 
00073   if(orientation == Qt::Horizontal)
00074     switch(section)
00075     {
00076       case 0:
00077         return QVariant("ID");
00078       case 1:
00079         return QVariant("Description");
00080       case 2:
00081         return QVariant("Type");
00082       default:
00083         return QVariant("");
00084 
00085     }
00086   else
00087     return QString();
00088 }
00089 
00090 //######################################################################
00091 bool AnnotationObjectManager::addObject(AnnotationObject * object)
00092 {
00093   //Connect the new object's 'hovered' signal to our own 'itemHovered' slot
00094   connect(object, SIGNAL(objectSelected(int)), this, SLOT(objectSelected(int)));
00095 
00096   connect(object, SIGNAL(rowSelected(int)), this, SLOT(selectAnimationRow(int)));
00097 
00098   //Let Qt know that we are messing with the rows, and insert the object
00099   //into our object list
00100   beginInsertRows(QModelIndex(), rowCount(), rowCount());
00101   itsObjects.insert(rowCount(), object);
00102   endInsertRows();
00103 
00104   object->setOpacity(itsOpacity);
00105 
00106 
00107   selectObject(itsObjects.size()-1);
00108 
00109   return true;
00110 }
00111 
00112 //######################################################################
00113 bool AnnotationObjectManager::setData(const QModelIndex &index, const QVariant &value, int role)
00114 {
00115   //Make sure we're trying to edit a valid table cell
00116   if(!index.isValid() || role != Qt::EditRole)
00117   {
00118     return false;
00119   }
00120 
00121   //If the user didn't enter anything, then let's ignore this to avoid
00122   //having the user accidently clear out useful data
00123   if(value.toString() == "")
00124     return false;
00125 
00126   //Grab a reference to the object that is being edited
00127   AnnotationObject* obj = itsObjects[index.row()];
00128 
00129   //Edit the object's data based on the column
00130   switch(index.column())
00131   {
00132     case 0:
00133       return false;
00134     case 1:
00135       obj->setDescription(value.toString());
00136       break;
00137     case 2:
00138       obj->setType(value.toInt());
00139       break;
00140     default:
00141       return false;
00142   }
00143 
00144   //Let Qt know that we changed some data
00145   emit(dataChanged(index, index));
00146   return true;
00147 }
00148 
00149 //######################################################################
00150 Qt::ItemFlags AnnotationObjectManager::flags(const QModelIndex &index) const
00151 {
00152   //The first column (the id) is selectable, but not editable
00153   if(index.column() == 0)
00154     return QAbstractTableModel::flags(index) | Qt::ItemIsSelectable;
00155 
00156   //All other columns are editable
00157   return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
00158 }
00159 
00160 
00161 //######################################################################
00162 void AnnotationObjectManager::constructAnimationContextMenu(QPoint globalPos, int row, int column)
00163 {
00164   if(itsCurrentSelectedRow != -1)
00165     itsObjects[itsCurrentSelectedRow]->constructAnimationContextMenu(globalPos, row, column);
00166 
00167 }
00168 
00169 //######################################################################
00170 void AnnotationObjectManager::select(const QModelIndex & index)
00171 {
00172   selectObject(index.row());
00173 }
00174 
00175 //######################################################################
00176 void AnnotationObjectManager::objectSelected(int itemId)
00177 {
00178   //Linearly search the items to find which one is being hovered...
00179   //We can do something more efficient later on if need be.
00180   int row = -1;
00181   for(int i=0; i<itsObjects.size(); i++)
00182   {
00183     if(itsObjects[i]->getId() == itemId)
00184       row = i;
00185   }
00186   selectObject(row);
00187 }
00188 
00189 
00190 void AnnotationObjectManager::deselectObject(int rowIdx)
00191 {
00192   itsObjects[rowIdx]->showDeselected();
00193 }
00194 
00195 void AnnotationObjectManager::selectObject(int rowIdx)
00196 {
00197   //Make sure the index is ok
00198   if(rowIdx < 0 || rowIdx >= itsObjects.size())
00199   {
00200     LERROR("Bad ItemIDX: %d", rowIdx);
00201     return;
00202   }
00203 
00204 
00205   if(itsCurrentSelectedRow != -1)
00206   {
00207    deselectObject(itsCurrentSelectedRow);
00208   }
00209 
00210   //Inform the new object that it is selected
00211   itsObjects[rowIdx]->showSelected();
00212 
00213   //Keep track of which row was selected
00214   itsCurrentSelectedRow = rowIdx;
00215 
00216   //ObjectAnimation* animation = itsObjects[rowIdx]->getAnimation();
00217   itsAnimationView->setModel(itsObjects[rowIdx]->getAnimationModel());
00218 
00219   //If there's nothing to do, don't emit the signal to avoid an infinite loop
00220   if(rowIdx != itsCurrentSelectedRow)
00221   {
00222     emit(selectingObject(rowIdx));
00223   }
00224 }
00225 
00226 void AnnotationObjectManager::selectAnimationRow(int rowIdx)
00227 {
00228   AnimationDelegate * del = static_cast<AnimationDelegate*>(itsAnimationView->itemDelegate());
00229   if(del != NULL)
00230     del->setSelectedRow(rowIdx);
00231   else
00232     LINFO("WARNING: Static Cast Returning NULL Pointer! (Line %d)", __LINE__);
00233 
00234   AnimationModel * mod = static_cast<AnimationModel*>(itsAnimationView->model());
00235   if(mod != NULL)
00236     mod->animationChanged();
00237   else
00238     LINFO("WARNING: Static Cast Returning NULL Pointer! (Line %d)", __LINE__);
00239 }
00240 
00241 void AnnotationObjectManager::setLastAnimViewClick(const QModelIndex & pos)
00242 {
00243   selectAnimationRow(pos.row());
00244 
00245   if(itsCurrentSelectedRow != -1)
00246   {
00247     itsObjects[itsCurrentSelectedRow]->setLastClick(pos);
00248   }
00249 
00250 
00251 }
00252 
00253 //######################################################################
00254 void AnnotationObjectManager::addVertex(QPointF point)
00255 {
00256   if(itsCurrentSelectedRow == -1)
00257     return;
00258 
00259   itsObjects[itsCurrentSelectedRow]->insertVertexAtPoint(point);
00260 }
00261 
00262 //######################################################################
00263 void AnnotationObjectManager::removeVertex(QPointF point)
00264 {
00265   if(itsCurrentSelectedRow == -1)
00266     return;
00267 
00268   itsObjects[itsCurrentSelectedRow]->removeVertex(point);
00269 }
00270 
00271 //######################################################################
00272 void AnnotationObjectManager::removeObject()
00273 {
00274 
00275   //Make sure there is a valid object to delete
00276   if(itsObjects.size() <= 0)      return;
00277   if(itsCurrentSelectedRow == -1) return;
00278 
00279   //Let Qt know we are about to start messing with the row structure
00280   beginRemoveRows(QModelIndex(), itsCurrentSelectedRow, itsCurrentSelectedRow);
00281 
00282   //Take the currently selected annotation object out of the object list,
00283   //and destroy it
00284   AnnotationObject * object = itsObjects.takeAt(itsCurrentSelectedRow);
00285   delete object;
00286 
00287   //If we're out of objects, then make sure to invalidate our current selected row
00288   if(itsObjects.size() == 0)
00289   {
00290     itsCurrentSelectedRow = -1;
00291   }
00292   else if(itsCurrentSelectedRow >= itsObjects.size())
00293   {
00294     //If we had selected the last row, we now need to select one less
00295     --itsCurrentSelectedRow;
00296   }
00297 
00298   //Let Qt know we're done messing with the row structure
00299   endRemoveRows();
00300 }
00301 
00302 //######################################################################
00303 void AnnotationObjectManager::frameChanged(int fnum)
00304 {
00305   itsCurrentFrame = fnum;
00306 }
00307 
00308 //######################################################################
00309 void AnnotationObjectManager::setOpacity(int opacity)
00310 {
00311   itsOpacity = opacity;
00312   for(int i=0; i<itsObjects.size(); ++i)
00313   {
00314     itsObjects[i]->setOpacity(opacity);
00315   }
00316 }
00317 
00318 //######################################################################
00319 std::map<int, std::map<int,AnnotationObjectFrame> > 
00320 AnnotationObjectManager::renderAnimations()
00321 {
00322   if(itsObjects.size() == 0)
00323     return std::map<int, std::map<int, AnnotationObjectFrame> >();
00324 
00325   std::map<int, std::map<int, AnnotationObjectFrame > > frames;
00326 
00327   FrameRange range = itsObjects[0]->getAnimation()->getFrameRange();
00328   for(int frameNum = range.getFirst(); frameNum < range.getLast(); frameNum++)
00329   {
00330     for(int objIdx=0; objIdx<itsObjects.size(); objIdx++)
00331     {
00332       AnnotationObjectFrame f;
00333       //Get the state of this object at the current frame
00334       f.VertexFrames      = itsObjects[objIdx]->getVertexStates(frameNum);
00335       f.ObjectDescription = itsObjects[objIdx]->getDescription();
00336       f.ObjectType        = itsObjects[objIdx]->getType();
00337       f.ObjectId          = itsObjects[objIdx]->getId();
00338       f.ObjectFrameState  = itsObjects[objIdx]->getFrameState(frameNum);
00339 
00340       frames[frameNum][f.ObjectId] = f;
00341     }
00342   }
00343 
00344   return frames;
00345 }
00346 
00347 //######################################################################
00348 void AnnotationObjectManager::clear()
00349 {
00350   if(itsObjects.size() > 0)
00351   {
00352     deselectObject(itsCurrentSelectedRow);
00353 
00354     while(itsObjects.size() > 0)
00355       removeObject();
00356   }
00357   itsCurrentSelectedRow = -1;
00358   //itsAnimationView = NULL;
00359 }
00360 
00361 //######################################################################
00362 AnnotationObject* AnnotationObjectManager::getObjectById(int id)
00363 {
00364   for(int i=0; i< itsObjects.size(); i++)
00365   {
00366     if(itsObjects[i]->getId() == id)
00367       return itsObjects[i];
00368   }
00369   return NULL;
00370 }
00371 
00372 
00373 
00374 
00375 #endif //ANNOTATIONOBJECTMANAGER_C
Generated on Sun May 8 08:41:02 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3