| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 | 
							- /*=============================================================================
 
-   Library: CTK
 
-   Copyright (c) German Cancer Research Center,
 
-     Division of Medical and Biological Informatics
 
-   Licensed under the Apache License, Version 2.0 (the "License");
 
-   you may not use this file except in compliance with the License.
 
-   You may obtain a copy of the License at
 
-     http://www.apache.org/licenses/LICENSE-2.0
 
-   Unless required by applicable law or agreed to in writing, software
 
-   distributed under the License is distributed on an "AS IS" BASIS,
 
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
-   See the License for the specific language governing permissions and
 
-   limitations under the License.
 
- =============================================================================*/
 
- #include "ctkPluginAbstractTracked_p.h"
 
- #include <QDebug>
 
- template<class Item, class Related>
 
- const bool ctkPluginAbstractTracked<Item,Related>::DEBUG	= true;
 
- template<class Item, class Related>
 
- ctkPluginAbstractTracked<Item,Related>::ctkPluginAbstractTracked()
 
- {
 
-   closed = false;
 
- }
 
- template<class Item, class Related>
 
- ctkPluginAbstractTracked<Item,Related>::~ctkPluginAbstractTracked()
 
- {
 
- }
 
- template<class Item, class Related>
 
- bool ctkPluginAbstractTracked<Item,Related>::wait(unsigned long timeout)
 
- {
 
-   return waitCond.wait(this, timeout);
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::wakeAll()
 
- {
 
-   waitCond.wakeAll();
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::setInitial(const QList<Item>& list)
 
- {
 
-   foreach (Item item, list)
 
-   {
 
-     if (DEBUG)
 
-     {
 
-       qDebug() << "ctkPluginAbstractTracked::setInitial:" << item;
 
-     }
 
-     initial.push_back(item);
 
-   }
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::setInitial(const QList<Item*>& list)
 
- {
 
-   foreach (Item* item, list)
 
-   {
 
-     if (item == 0)
 
-     {
 
-       continue;
 
-     }
 
-     if (DEBUG)
 
-     {
 
-       qDebug() << "ctkPluginAbstractTracked::setInitial:" << item;
 
-     }
 
-     initial.push_back(item);
 
-   }
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::trackInitial()
 
- {
 
-   while (true)
 
-   {
 
-     Item item(0);
 
-     {
 
-       QMutexLocker lock(this);
 
-       if (closed || (initial.size() == 0))
 
-       {
 
-         /*
 
-          * if there are no more initial items
 
-          */
 
-         return; /* we are done */
 
-       }
 
-       /*
 
-        * move the first item from the initial list to the adding list
 
-        * within this synchronized block.
 
-        */
 
-       item = initial.takeFirst();
 
-       if (tracked.value(item) != 0)
 
-       {
 
-         /* if we are already tracking this item */
 
-         if (DEBUG)
 
-         {
 
-           qDebug() << "ctkPluginAbstractTracked::trackInitial[already tracked]: " << item;
 
-         }
 
-         continue; /* skip this item */
 
-       }
 
-       if (adding.contains(item))
 
-       {
 
-         /*
 
-          * if this item is already in the process of being added.
 
-          */
 
-         if (DEBUG)
 
-         {
 
-           qDebug() << "ctkPluginAbstractTracked::trackInitial[already adding]: " << item;
 
-         }
 
-         continue; /* skip this item */
 
-       }
 
-       adding.push_back(item);
 
-     }
 
-     if (DEBUG)
 
-     {
 
-       qDebug() << "ctkPluginAbstractTracked::trackInitial: " << item;
 
-     }
 
-     trackAdding(item, Related());
 
-     /*
 
-      * Begin tracking it. We call trackAdding
 
-      * since we have already put the item in the
 
-      * adding list.
 
-      */
 
-   }
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::close()
 
- {
 
-   closed = true;
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::track(Item item, Related related)
 
- {
 
-   QVariant object;
 
-   {
 
-     QMutexLocker lock(this);
 
-     if (closed)
 
-     {
 
-       return;
 
-     }
 
-     object = tracked.value(item);
 
-     if (object.isNull())
 
-     { /* we are not tracking the item */
 
-       if (adding.contains(item))
 
-       {
 
-         /* if this item is already in the process of being added. */
 
-         if (DEBUG)
 
-         {
 
-           qDebug() << "ctkPluginAbstractTracked::track[already adding]: " << item;
 
-         }
 
-         return;
 
-       }
 
-       adding.push_back(item); /* mark this item is being added */
 
-     }
 
-     else
 
-     { /* we are currently tracking this item */
 
-       if (DEBUG)
 
-       {
 
-         qDebug() << "ctkPluginAbstractTracked::track[modified]: " << item;
 
-       }
 
-       modified(); /* increment modification count */
 
-     }
 
-   }
 
-   if (object.isNull())
 
-   { /* we are not tracking the item */
 
-     trackAdding(item, related);
 
-   }
 
-   else
 
-   {
 
-     /* Call customizer outside of synchronized region */
 
-     customizerModified(item, related, object);
 
-     /*
 
-      * If the customizer throws an unchecked exception, it is safe to
 
-      * let it propagate
 
-      */
 
-   }
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::untrack(Item item, Related related)
 
- {
 
-   QVariant object;
 
-   {
 
-     QMutexLocker lock(this);
 
-     if (initial.removeOne(item))
 
-     { /* if this item is already in the list
 
-        * of initial references to process
 
-        */
 
-       if (DEBUG)
 
-       {
 
-         qDebug() << "ctkPluginAbstractTracked::untrack[removed from initial]: " << item;
 
-       }
 
-       return; /* we have removed it from the list and it will not be
 
-                * processed
 
-                */
 
-     }
 
-     if (adding.removeOne(item))
 
-     { /* if the item is in the process of
 
-        * being added
 
-        */
 
-       if (DEBUG)
 
-       {
 
-         qDebug() << "ctkPluginAbstractTracked::untrack[being added]: " << item;
 
-       }
 
-       return; /*
 
-            * in case the item is untracked while in the process of
 
-            * adding
 
-            */
 
-     }
 
-     object = tracked.take(item); /*
 
-                      * must remove from tracker before
 
-                      * calling customizer callback
 
-                      */
 
-     if (object.isNull())
 
-     { /* are we actually tracking the item */
 
-       return;
 
-     }
 
-     modified(); /* increment modification count */
 
-   }
 
-   if (DEBUG)
 
-   {
 
-     qDebug() << "ctkPluginAbstractTracked::untrack[removed]: " << item;
 
-   }
 
-   /* Call customizer outside of synchronized region */
 
-   customizerRemoved(item, related, object);
 
-   /*
 
-    * If the customizer throws an unchecked exception, it is safe to let it
 
-    * propagate
 
-    */
 
- }
 
- template<class Item, class Related>
 
- int ctkPluginAbstractTracked<Item,Related>::size() const
 
- {
 
-   return tracked.size();
 
- }
 
- template<class Item, class Related>
 
- QVariant ctkPluginAbstractTracked<Item,Related>::getCustomizedObject(Item item) const
 
- {
 
-   return tracked.value(item);
 
- }
 
- template<class Item, class Related>
 
- QList<Item> ctkPluginAbstractTracked<Item,Related>::getTracked() const
 
- {
 
-   return tracked.keys();
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::modified()
 
- {
 
-   trackingCount.ref();
 
- }
 
- template<class Item, class Related>
 
- int ctkPluginAbstractTracked<Item,Related>::getTrackingCount() const
 
- {
 
-   return trackingCount;
 
- }
 
- template<class Item, class Related>
 
- bool ctkPluginAbstractTracked<Item,Related>::customizerAddingFinal(Item item, const QVariant& custom)
 
- {
 
-   QMutexLocker lock(this);
 
-   if (adding.removeOne(item) && !closed)
 
-   {
 
-     /*
 
-      * if the item was not untracked during the customizer
 
-      * callback
 
-      */
 
-     if (!custom.isNull())
 
-     {
 
-       tracked.insert(item, custom);
 
-       modified(); /* increment modification count */
 
-       waitCond.wakeAll(); /* notify any waiters */
 
-     }
 
-     return false;
 
-   }
 
-   else
 
-   {
 
-     return true;
 
-   }
 
- }
 
- template<class Item, class Related>
 
- void ctkPluginAbstractTracked<Item,Related>::trackAdding(Item item, Related related)
 
- {
 
-   if (DEBUG)
 
-   {
 
-     qDebug() << "ctkPluginAbstractTracked::trackAdding:" << item;
 
-   }
 
-   QVariant object;
 
-   bool becameUntracked = false;
 
-   /* Call customizer outside of synchronized region */
 
-   try
 
-   {
 
-     object = customizerAdding(item, related);
 
-     becameUntracked = this->customizerAddingFinal(item, object);
 
-   }
 
-   catch (...)
 
-   {
 
-     /*
 
-      * If the customizer throws an exception, it will
 
-      * propagate after the cleanup code.
 
-      */
 
-     this->customizerAddingFinal(item, object);
 
-     throw;
 
-   }
 
-   /*
 
-    * The item became untracked during the customizer callback.
 
-    */
 
-   if (becameUntracked && (!object.isNull()))
 
-   {
 
-     if (DEBUG)
 
-     {
 
-       qDebug() << "ctkPluginAbstractTracked::trackAdding[removed]: " << item;
 
-     }
 
-     /* Call customizer outside of synchronized region */
 
-     customizerRemoved(item, related, object);
 
-     /*
 
-      * If the customizer throws an unchecked exception, it is safe to
 
-      * let it propagate
 
-      */
 
-   }
 
- }
 
 
  |