kblackbox
kbbgraphicsitemset.cpp
Go to the documentation of this file.00001 // 00002 // KBlackbox 00003 // 00004 // A simple game inspired by an emacs module 00005 // 00006 /*************************************************************************** 00007 * Copyright (c) 1999-2000, Robert Cimrman * 00008 * cimrman3@students.zcu.cz * 00009 * * 00010 * Copyright (c) 2007, Nicolas Roffet * 00011 * nicolas-kde@roffet.com * 00012 * * 00013 * * 00014 * This program is free software; you can redistribute it and/or modify * 00015 * it under the terms of the GNU General Public License as published by * 00016 * the Free Software Foundation; either version 2 of the License, or * 00017 * (at your option) any later version. * 00018 * * 00019 * This program is distributed in the hope that it will be useful, * 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00022 * GNU General Public License for more details. * 00023 * * 00024 * You should have received a copy of the GNU General Public License * 00025 * along with this program; if not, write to the * 00026 * Free Software Foundation, Inc., * 00027 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * 00028 ***************************************************************************/ 00029 00030 #include "kbbgraphicsitemset.h" 00031 00032 00033 00034 #include <QGraphicsScene> 00035 00036 00037 #include "kbbgraphicsitem.h" 00038 #include "kbbitemwithposition.h" 00039 00040 00041 00042 // 00043 // Constructor / Destructor 00044 // 00045 00046 KBBGraphicsItemSet::KBBGraphicsItemSet(QGraphicsScene* scene) 00047 { 00048 m_scene = scene; 00049 } 00050 00051 00052 KBBGraphicsItemSet::~KBBGraphicsItemSet() 00053 { 00054 clear(); 00055 } 00056 00057 00058 00059 // 00060 // Public 00061 // 00062 00063 int KBBGraphicsItemSet::anyItemPosition() 00064 { 00065 if (m_items.count()>0) 00066 return m_items.last()->position(); 00067 else 00068 return NO_INDEX; 00069 } 00070 00071 00072 int KBBGraphicsItemSet::count() const 00073 { 00074 return m_items.count(); 00075 } 00076 00077 00078 void KBBGraphicsItemSet::clear() 00079 { 00080 while (m_items.count()>0) { 00081 remove(m_items.last()->position()); 00082 } 00083 } 00084 00085 00086 bool KBBGraphicsItemSet::contains(int position) 00087 { 00088 return (indexOf(position)!=NO_INDEX); 00089 } 00090 00091 00092 bool KBBGraphicsItemSet::containsVisible(int position) 00093 { 00094 int i = indexOf(position); 00095 00096 if (i!=NO_INDEX) { 00097 if(dynamic_cast<KBBGraphicsItem*>(m_items[i])) 00098 return ((dynamic_cast<KBBGraphicsItem*>(m_items[i]))->isVisible()); 00099 else 00100 return true; 00101 } else 00102 return false; 00103 } 00104 00105 00106 void KBBGraphicsItemSet::insert(KBBItemWithPosition* item) 00107 { 00108 if (contains(item->position())) 00109 // We want to avoid duplicated item on a given position. 00110 item->cleanDelete(); 00111 else 00112 m_items.append(item); 00113 } 00114 00115 00116 KBBItemWithPosition* KBBGraphicsItemSet::item(int position) 00117 { 00118 KBBItemWithPosition* r = NULL; 00119 00120 for (int i=0;i<m_items.count();i++) 00121 if (m_items[i]->position()==position) 00122 r = m_items[i]; 00123 return r; 00124 } 00125 00126 00127 void KBBGraphicsItemSet::remove(int position) 00128 { 00129 int i = indexOf(position); 00130 00131 if (i!=NO_INDEX) { 00132 m_items[i]->cleanDelete(); 00133 m_scene->update(); 00134 m_items.removeAt(i); 00135 } 00136 } 00137 00138 00139 void KBBGraphicsItemSet::setVisible(int position, const bool visible) 00140 { 00141 int i = indexOf(position); 00142 00143 if (i!=NO_INDEX) { 00144 if(dynamic_cast<KBBGraphicsItem*>(m_items[i])) 00145 (dynamic_cast<KBBGraphicsItem*>(m_items[i]))->setVisible(visible); 00146 } 00147 } 00148 00149 00150 00151 // 00152 // Private 00153 // 00154 00155 int KBBGraphicsItemSet::indexOf(int position) 00156 { 00157 for(int i=0;i<m_items.count();i++) 00158 if (m_items[i]->position()==position) 00159 return i; 00160 00161 return NO_INDEX; 00162 }
KDE 4.0 API Reference