File indexing completed on 2022-11-23 10:43:25

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 1999-2000 Robert Cimrman <cimrman3@students.zcu.cz>
0005     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kbbgraphicsitemset.h"
0011 
0012 
0013 
0014 #include <QGraphicsScene>
0015 
0016 
0017 #include "kbbgraphicsitem.h"
0018 #include "kbbitemwithposition.h"
0019 
0020 
0021 
0022 //
0023 // Constructor / Destructor
0024 //
0025 
0026 KBBGraphicsItemSet::KBBGraphicsItemSet(QGraphicsScene* scene)
0027 {
0028         m_scene = scene;
0029 }
0030 
0031 
0032 KBBGraphicsItemSet::~KBBGraphicsItemSet()
0033 {
0034     clear();
0035 }
0036 
0037 
0038 
0039 //
0040 // Public
0041 //
0042 
0043 int KBBGraphicsItemSet::anyItemPosition()
0044 {
0045         if (!m_items.isEmpty())
0046         return m_items.last()->position();
0047     else
0048         return NO_INDEX;
0049 }
0050 
0051 
0052 int KBBGraphicsItemSet::count() const
0053 {
0054     return m_items.count();
0055 }
0056 
0057 
0058 void KBBGraphicsItemSet::clear()
0059 {
0060     while (m_items.count()>0) {
0061         remove(m_items.last()->position());
0062     }
0063 }
0064 
0065 
0066 bool KBBGraphicsItemSet::contains(int position)
0067 {
0068     return (indexOf(position)!=NO_INDEX);
0069 }
0070 
0071 
0072 bool KBBGraphicsItemSet::containsVisible(int position)
0073 {
0074     int i = indexOf(position);
0075 
0076     if (i!=NO_INDEX) {
0077         if(dynamic_cast<KBBGraphicsItem*>(m_items[i]))
0078             return ((dynamic_cast<KBBGraphicsItem*>(m_items[i]))->isVisible());
0079         else
0080             return true;
0081     } else
0082         return false;
0083 }
0084 
0085 
0086 void KBBGraphicsItemSet::insert(KBBItemWithPosition* item)
0087 {
0088     if (contains(item->position()))
0089         // We want to avoid duplicated item on a given position.
0090         item->cleanDelete();
0091     else
0092         m_items.append(item);
0093 }
0094 
0095 
0096 KBBItemWithPosition* KBBGraphicsItemSet::item(int position)
0097 {
0098     KBBItemWithPosition* r = nullptr;
0099 
0100     for (int i=0;i<m_items.count();i++)
0101         if (m_items[i]->position()==position)
0102             r = m_items[i];
0103     return r;
0104 }
0105 
0106 
0107 void KBBGraphicsItemSet::remove(int position)
0108 {
0109     int i = indexOf(position);
0110 
0111     if (i!=NO_INDEX) {
0112         m_items[i]->cleanDelete();
0113         m_scene->update();
0114         m_items.removeAt(i);
0115     }
0116 }
0117 
0118 
0119 void KBBGraphicsItemSet::setVisible(const int position, const bool visible)
0120 {
0121     int i = indexOf(position);
0122 
0123     if (i!=NO_INDEX) {
0124         if(dynamic_cast<KBBGraphicsItem*>(m_items[i]))
0125             (dynamic_cast<KBBGraphicsItem*>(m_items[i]))->setVisible(visible);
0126     }
0127 }
0128 
0129 
0130 
0131 //
0132 // Private
0133 //
0134 
0135 int KBBGraphicsItemSet::indexOf(int position)
0136 {
0137     for(int i=0;i<m_items.count();i++)
0138         if (m_items[i]->position()==position)
0139             return i;
0140 
0141     return NO_INDEX;
0142 }