00001 /* 00002 Copyright © 2010 guillaume belz 00003 00004 This file is part of QExtend. 00005 00006 QExtend is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 any later version. 00010 00011 QExtend is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with QExtend. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "objectlist.hpp" 00021 00022 using namespace QExtend; 00023 00024 ObjectList::ObjectList(const ObjectList & list) 00025 { 00026 foreach(QObject* object, list) 00027 { 00028 m_objects.append(object); 00029 } 00030 } 00031 00032 ObjectList::~ObjectList() 00033 { 00034 foreach(QObject* object, m_objects) 00035 { 00036 delete object; 00037 } 00038 clear(); 00039 } 00040 00041 void ObjectList::append(const ObjectList & list) 00042 { 00043 foreach(QObject* object, list) 00044 { 00045 m_objects.append(object); 00046 } 00047 } 00048 00049 ObjectList ObjectList::mid(const int pos, const int length) const 00050 { 00051 ObjectList l; 00052 int end = (length == -1) ? m_objects.size() : length; 00053 00054 for(int i=pos; i<m_objects.size() && i<end; ++i) 00055 { 00056 l.append(m_objects[i]); 00057 } 00058 return l; 00059 } 00060 00061 void ObjectList::reserve(int alloc) 00062 { 00063 #if QT_VERSION >= 0x040700 00064 m_objects.reserve(alloc); 00065 #endif 00066 } 00067 00068 ObjectList ObjectList::operator+ (const ObjectList & list) const 00069 { 00070 ObjectList l; 00071 l.append(*this); 00072 l.append(list); 00073 return l; 00074 } 00075 00076 ObjectList ObjectList::operator+ (QObject* object) const 00077 { 00078 ObjectList l; 00079 l.append(*this); 00080 l.append(object); 00081 return l; 00082 } 00083 00084 //************************************************** 00085 //* get object 00086 //************************************************** 00087 00088 QObject* ObjectList::getObject(const QString name) const 00089 { 00090 foreach(QObject* object, *this) 00091 { 00092 if(object->objectName() == name) 00093 { 00094 return object; 00095 } 00096 } 00097 return NULL; 00098 } 00099 00100 QObject* ObjectList::getObject(const char* property, const QVariant value) const 00101 { 00102 foreach(QObject* object, *this) 00103 { 00104 QVariant variant = object->property(property); 00105 if(variant.isValid() && variant==value) 00106 { 00107 return object; 00108 } 00109 } 00110 return NULL; 00111 } 00112 00113 00114 //************************************************** 00115 //* iterator get object 00116 //************************************************** 00117 00118 ObjectList::Iterator ObjectList::getObjectIterator(const QString name) 00119 { 00120 for(Iterator it=begin(); it!=end(); ++it) 00121 { 00122 if((*it)->objectName() == name) 00123 { 00124 return it; 00125 } 00126 } 00127 return end(); 00128 } 00129 00130 ObjectList::Iterator ObjectList::getObjectIterator(const char* property, const QVariant value) 00131 { 00132 for(Iterator it=begin(); it!=end(); ++it) 00133 { 00134 QVariant variant = (*it)->property(property); 00135 if(variant.isValid() && variant==value) 00136 { 00137 return it; 00138 } 00139 } 00140 return end(); 00141 } 00142 00143 00144 //************************************************** 00145 //* get objects 00146 //************************************************** 00147 00148 ObjectList ObjectList::getObjects(const QString name) const 00149 { 00150 ObjectList results; 00151 foreach(QObject* object, *this) 00152 { 00153 if(object->objectName() == name) 00154 { 00155 results << object; 00156 } 00157 } 00158 return results; 00159 } 00160 00161 ObjectList ObjectList::getObjects(const char* property, const QVariant value) const 00162 { 00163 ObjectList results; 00164 foreach(QObject* object, *this) 00165 { 00166 QVariant variant = object->property(property); 00167 if(variant.isValid() && variant==value) 00168 { 00169 results << object; 00170 } 00171 } 00172 return results; 00173 } 00174 00175 00176 //************************************************** 00177 //* remove objects 00178 //************************************************** 00179 00180 bool ObjectList::removeObject(Iterator it) 00181 { 00182 if(it != end()) 00183 { 00184 delete *it; 00185 erase(it); 00186 return true; 00187 } 00188 else 00189 return false; 00190 } 00191 00192 bool ObjectList::removeObject() 00193 { 00194 return removeObject(this->begin()); 00195 } 00196 00197 bool ObjectList::removeObject(const QString name) 00198 { 00199 return removeObject(getObjectIterator(name)); 00200 } 00201 00202 bool ObjectList::removeObject(const char* property, const QVariant value) 00203 { 00204 return removeObject(getObjectIterator(property, value)); 00205 } 00206 00207 00208 void ObjectList::removeObjects() 00209 { 00210 while(removeObject()) 00211 { 00212 } 00213 } 00214 00215 void ObjectList::removeObjects(const QString name) 00216 { 00217 while(removeObject(name)) 00218 { 00219 } 00220 } 00221 00222 void ObjectList::removeObjects(const char* property, const QVariant value) 00223 { 00224 while(removeObject(property, value)) 00225 { 00226 } 00227 }
Les sources présentées sur cette page sont libres de droits et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation constitue une œuvre intellectuelle protégée par les droits d'auteur. Copyright © 2010 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.