00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kitmodel.hpp"
00024 #include <algorithm>
00025 #include <cassert>
00026 #include <glib.h>
00027 #include <iostream>
00028 #include <iterator>
00029
00030
00036 void GuiState::reset() {
00037 m_dirty = false;
00038 m_deleted = false;
00039 m_new = false;
00040 }
00041
00042
00043 ModelCategory::ModelCategory() : Category(), GuiState() {
00044 m_removed_children = new ItemMap;
00045 m_added_children = new ItemMap;
00046 }
00047
00048
00049 ModelCategory::~ModelCategory() {
00050 delete m_removed_children;
00051 delete m_added_children;
00052 }
00053
00054
00061 void ModelCategory::reset() {
00062 GuiState::reset();
00063 m_removed_children->clear();
00064 m_added_children->clear();
00065 }
00066
00067
00068 void ModelCategory::purge() {
00069 ItemIter i = m_items.begin();
00070 while (i != m_items.end()) {
00071 Item* item = *i;
00072 if (((ModelItem*) item)->is_deleted()) {
00073 ItemIter iterTemp = i;
00074 m_items.erase(i);
00075 i = iterTemp++;
00076 } else {
00077 i++;
00078 }
00079 }
00080 }
00081
00082
00088 void ModelCategory::add_item(Item* item) {
00089 Category::add_item(item);
00090
00091
00092
00093 ItemMap::size_type n = m_removed_children->erase(item->get_id());
00094 if (n == 0) {
00095
00096 m_added_children->insert(std::make_pair(item->get_id(), (ModelItem*) item));
00097 }
00098 }
00099
00100
00106 void ModelCategory::remove_item(Item* item) {
00107 Category::remove_item(item);
00108
00109
00110 ItemMap::size_type n = m_added_children->erase(item->get_id());
00111 if (n == 0) {
00112 m_removed_children->insert(std::make_pair(item->get_id(), (ModelItem*) item));
00113 }
00114 }
00115
00116
00122 void ModelCategory::remove_items(ModelItemContainer* items) {
00123 for (ModelItemIter i = items->begin(); i != items->end(); ++i) {
00124 remove_item(*i);
00125 }
00126 }
00127
00128
00134 ModelItemContainer* ModelCategory::get_model_items() {
00135 ModelItemContainer* retval = new ModelItemContainer;
00136 for (ItemIter i = m_items.begin(); i != m_items.end(); ++i) {
00137 ModelItem* mi = (ModelItem*)*i;
00138 if (!mi->is_deleted())
00139 retval->push_back(mi);
00140 }
00141 return retval;
00142 }
00143
00144
00151 ItemContainer* ModelCategory::get_items() {
00152 ItemContainer* retval = new ItemContainer;
00153 for (ItemIter i = m_items.begin(); i != m_items.end(); ++i) {
00154 ModelItem* mi = (ModelItem*)*i;
00155 if (!mi->is_deleted()) {
00156 retval->push_back(mi);
00157 }
00158 }
00159 return retval;
00160 }
00161
00162
00172 ItemContainer* ModelCategory::get_items(ItemFunctor& functor) {
00173 ItemContainer* retval = new ItemContainer;
00174 for (ItemIter i = m_items.begin(); i != m_items.end(); ++i) {
00175 ModelItem* mi = (ModelItem*)*i;
00176 if (!mi->is_deleted() && functor(**i)) {
00177 retval->push_back(mi);
00178 }
00179 }
00180 return retval;
00181 }
00182
00183
00187 KitModel::KitModel() : m_dirty(false), m_item_filter(ALL) {
00188 m_item_map = new ItemMap;
00189 m_category_map = new CategoryMap;
00190 }
00191
00192
00198 KitModel::~KitModel() {
00199 for (ItemMapIter i = m_item_map->begin(); i != m_item_map->end(); ++i) {
00200 delete (*i).second;
00201 (*i).second = 0;
00202 }
00203 m_item_map->clear();
00204 for (CategoryMapIter i = m_category_map->begin(); i != m_category_map->end(); ++i) {
00205 delete (*i).second;
00206 (*i).second = 0;
00207 }
00208 m_category_map->clear();
00209 delete m_item_map;
00210 delete m_category_map;
00211 }
00212
00213
00218 void KitModel::foreach_item_iter(const SlotForeachModelItemIter& slot) {
00219 for (ItemMapIter i = m_item_map->begin(); i != m_item_map->end(); ++i) {
00220 if (slot(i))
00221 break;
00222 }
00223 }
00224
00225
00230 void KitModel::foreach_item(const SlotForeachModelItem& slot) {
00231 for (ItemMapIter i = m_item_map->begin(); i != m_item_map->end(); ++i) {
00232 if (slot(*(*i).second))
00233 break;
00234 }
00235 }
00236
00237
00242 void KitModel::foreach_category_iter(const SlotForeachCategoryIter& slot) {
00243 for (CategoryMapIter i = m_category_map->begin(); i != m_category_map->end(); ++i) {
00244 if (slot(i))
00245 break;
00246 }
00247 }
00248
00249
00254 void KitModel::foreach_category(const SlotForeachCategory& slot) {
00255 for (CategoryMapIter i = m_category_map->begin(); i != m_category_map->end(); ++i) {
00256 if (slot(*(*i).second))
00257 break;
00258 }
00259 }
00260
00261
00265 ModelCategory* KitModel::find_category(long id) {
00266 ModelCategory* retval = NULL;
00267 CategoryMapIter i = m_category_map->find(id);
00268 if (i != m_category_map->end()) {
00269 retval = (*i).second;
00270 }
00271 return retval;
00272 }
00273
00274
00278 ModelItem* KitModel::find_item(long id) {
00279 ModelItem* retval = NULL;
00280 ItemMapIter i = m_item_map->find(id);
00281 if (i != m_item_map->end()) {
00282 retval = (*i).second;
00283 }
00284 return retval;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00300 CategoryContainer* KitModel::get_categories() {
00301 CategoryContainer* retval = new CategoryContainer;
00302 for (CategoryMapIter i = m_category_map->begin(); i != m_category_map->end(); ++i) {
00303 ModelCategory* cat = (*i).second;
00304 if (!cat->is_deleted()) {
00305 retval->push_back(cat);
00306 }
00307 }
00308 return retval;
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00327 ItemContainer* KitModel::get_all_items() {
00328 ItemContainer* retval = new ItemContainer;
00329 for (ItemMapIter i = m_item_map->begin(); i != m_item_map->end(); ++i) {
00330 if (!((*i).second)->is_deleted()) {
00331 retval->push_back((*i).second);
00332 }
00333 }
00334 return retval;
00335 }
00336
00337
00345 ItemContainer* KitModel::get_all_items(ItemFunctor& functor) {
00346 ItemContainer* retval = new ItemContainer;
00347 for (ItemMapIter i = m_item_map->begin(); i != m_item_map->end(); ++i) {
00348 if (!((*i).second)->is_deleted() && functor(*(*i).second)) {
00349 retval->push_back((*i).second);
00350 }
00351 }
00352 return retval;
00353 }
00354
00355
00361 void KitModel::add_category(ModelCategory* category) {
00362 m_category_map->insert(std::make_pair(category->get_id(), category));
00363 }
00364
00365
00371 void KitModel::add_item(ModelItem* item) {
00372 m_item_map->insert(std::make_pair(item->get_id(), item));
00373 }
00374
00375
00382 void KitModel::add_item(ModelItem* item, long cat_id) {
00383 std::pair<long, ModelItem*> pair = std::make_pair(item->get_id(), item);
00384 m_item_map->insert(pair);
00385 ModelCategory* c = find_category(cat_id);
00386 if (c) {
00387 c->add_item(item);
00388 } else
00389 g_warning("category id %d was not found - unable to associate item", cat_id);
00390 }
00391
00392
00399 void KitModel::copy_items(const ModelItemContainer& items, long cat_id) {
00400 assert(cat_id != -1);
00401 ModelCategory* c = find_category(cat_id);
00402 if (c) {
00403 c->set_dirty(true);
00404
00405
00406 ModelItemContainer sourceItems(items);
00407
00408
00409 ModelItemContainer targetItems;
00410 for (ItemIter i = c->m_items.begin(); i != c->m_items.end(); ++i) {
00411 targetItems.push_back((ModelItem*) (*i));
00412 }
00413
00414
00415 sort(sourceItems.begin(), sourceItems.end());
00416
00417 ModelItemContainer newItems;
00418 sort(targetItems.begin(), targetItems.end());
00419 std::set_difference(
00420 sourceItems.begin(),
00421 sourceItems.end(),
00422 targetItems.begin(),
00423 targetItems.end(),
00424 std::back_insert_iterator<ModelItemContainer>(newItems)
00425 );
00426
00427
00428
00429
00430 for (ModelItemIter i = newItems.begin(); i != newItems.end(); ++i) {
00431 c->add_item(*i);
00432 }
00433 } else {
00434 g_error("Warning: category id %d was not found - unable to copy items",
00435 cat_id);
00436 }
00437 }
00438
00439
00446 bool KitModel::filter(bool checked) {
00447 switch (m_item_filter) {
00448 case ALL : return true;
00449 case CHECKED : return checked;
00450 case UNCHECKED : return !checked;
00451 }
00452 return true;
00453 }
00454
00455
00466 void KitModel::reset() {
00467 for (CategoryMapIter i = m_category_map->begin(); i != m_category_map->end(); ++i) {
00468 ModelCategory* cat = (*i).second;
00469 if (!cat->is_deleted())
00470 cat->reset();
00471 }
00472 for (ItemMapIter i = m_item_map->begin(); i != m_item_map->end(); ++i) {
00473 ModelItem* item = (*i).second;
00474 if (!item->is_deleted())
00475 item->reset();
00476 }
00477 m_dirty = false;
00478 }
00479
00486 void KitModel::purge() {
00487 CategoryMapIter c = m_category_map->begin();
00488 while (c != m_category_map->end()) {
00489 ModelCategory* cat = (*c).second;
00490 if (cat->is_deleted()) {
00491 CategoryMapIter iterTemp = c;
00492 iterTemp++;
00493 m_category_map->erase(c);
00494 c = iterTemp;
00495 delete cat;
00496 } else {
00497 cat->purge();
00498 c++;
00499 }
00500 }
00501 ItemMapIter i = m_item_map->begin();
00502 while (i != m_item_map->end()) {
00503 ModelItem* item = (*i).second;
00504 if (item->is_deleted()) {
00505 ItemMapIter iterTemp = i;
00506 iterTemp++;
00507 m_item_map->erase(i);
00508 i = iterTemp;
00509 delete item;
00510 } else {
00511 i++;
00512 }
00513 }
00514 }