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 #ifndef QEXTEND_OBJECTLIST_H 00021 #define QEXTEND_OBJECTLIST_H 00022 00023 #include <QtCore/QList> 00024 #include <QtCore/QObject> 00025 #include <QtCore/QVariant> 00026 #include <QExtend/Core/qextend_global> 00027 00028 namespace QExtend 00029 { 00030 00031 00033 00034 00036 00064 class QEXTEND_EXPORT ObjectList 00065 { 00066 public: 00067 typedef QList< QObject* >::const_iterator const_iterator; 00068 typedef QList< QObject* >::iterator iterator; 00069 typedef const_iterator ConstIterator; 00070 typedef iterator Iterator; 00071 // typedef difference_type 00072 // typedef size_type 00073 // typedef value_type 00074 00075 inline explicit ObjectList() : m_objects() {} 00076 ObjectList(const ObjectList & list); 00077 00078 ~ObjectList(); 00079 00080 inline void append(QObject* object) { m_objects.append(object); } 00081 void append(const ObjectList & list); 00082 inline const QObject* at(int i) const { return m_objects.at(i); } 00083 inline QObject* back() { return m_objects.back(); } 00084 inline const QObject* back() const { return m_objects.back(); } 00085 inline iterator begin() { return m_objects.begin(); } 00086 inline const_iterator begin() const { return m_objects.begin(); } 00087 inline void clear() { m_objects.clear(); } 00088 inline const_iterator constBegin() const { return m_objects.constBegin(); } 00089 inline const_iterator constEnd() const { return m_objects.constEnd(); } 00090 //bool contains(const T & value ) const 00091 //inline int count(const T & value ) const 00092 inline int count() const { return m_objects.count(); } 00093 inline bool empty() const { return m_objects.empty(); } 00094 inline iterator end() { return m_objects.end(); } 00095 inline const_iterator end() const { return m_objects.end(); } 00096 // bool endsWith ( const T & value ) const 00097 inline iterator erase(iterator pos) { return m_objects.erase(pos); } 00098 inline iterator erase(iterator begin, iterator end) { return m_objects.erase(begin, end); } 00099 inline QObject* first() { return m_objects.first(); } 00100 inline const QObject* first() const { return m_objects.first(); } 00101 inline QObject* front() { return m_objects.front(); } 00102 inline const QObject* front() const { return m_objects.front(); } 00103 // inline int indexOf(const QObject* object, int from = 0) const 00104 inline void insert(int i, QObject* object) { m_objects.insert(i, object); } 00105 inline iterator insert(iterator before, QObject* object) { return m_objects.insert(before, object); } 00106 inline bool isEmpty() const { return m_objects.isEmpty(); } 00107 inline QObject* last() { return m_objects.last(); } 00108 inline const QObject* last() const { return m_objects.last(); } 00109 // int lastIndexOf ( const T & value, int from = -1 ) const 00110 inline int length() const { return m_objects.length(); } 00111 ObjectList mid(int pos, int length = -1) const; 00112 inline void move(int from, int to) { m_objects.move(from, to); } 00113 inline void pop_back() { m_objects.pop_back(); } 00114 inline void pop_front() { m_objects.pop_front(); } 00115 inline void prepend(QObject* object) { m_objects.prepend(object); } 00116 inline void push_back(QObject* object) { m_objects.push_back(object); } 00117 inline void push_front(QObject* object) { m_objects.push_front(object); } 00118 // inline int removeAll( const T & value ) 00119 inline void removeAt(int i) { m_objects.removeAt(i); } 00120 inline void removeFirst() { m_objects.removeFirst(); } 00121 inline void removeLast() { m_objects.removeLast(); } 00122 // inline bool removeOne(const QObject* object) 00123 // void replace ( int i, const T & value ) { m_objects.removeAt(i); } 00124 void reserve(int alloc); 00125 inline int size() const { return m_objects.size(); } 00126 // bool startsWith ( const T & value ) const 00127 inline void swap(int i, int j) { m_objects.swap(i, j); } 00128 inline QObject* takeAt(int i) { return m_objects.takeAt(i); } 00129 inline QObject* takeFirst() { return m_objects.takeFirst(); } 00130 inline QObject* takeLast() { return m_objects.takeLast(); } 00131 // inline QSet< QObject* > toSet() const { return m_objects.toSet< QObject* >(); } 00132 // inline std::list< QObject* > toStdList() const { return m_objects.toStdList< QObject* >(); } 00133 // inline QVector< QObject* > toVector() const { return m_objects.toVector< QObject* >(); } 00134 inline QObject* value(int i) const { return m_objects.value(i); } 00135 inline QObject* value(int i, QObject* object) const { return m_objects.value(i, object); } 00136 // bool operator!= ( const ObjectList & other ) const 00137 ObjectList operator+ (const ObjectList & list) const; 00138 ObjectList operator+ (QObject* object) const; 00139 inline ObjectList & operator+= (const ObjectList & list) { ObjectList::append(list); return *this; } 00140 inline ObjectList & operator+= (QObject* object) { this->append(object); return *this; } 00141 inline ObjectList & operator<< (ObjectList & list) { append(list); return *this; } 00142 inline ObjectList & operator<< (QObject* object) { append(object); return *this; } 00143 inline ObjectList & operator= (ObjectList & list) 00144 { if (this!=&list) { clear(); append(list); } return *this; } 00145 inline ObjectList & operator= (QObject* object) { clear(); append(object); return *this; } 00146 // bool operator== (const ObjectList & other) const 00147 inline QObject* operator[] (int i) { return m_objects[i]; } 00148 inline const QObject* operator[] (int i) const { return m_objects[i]; } 00149 00150 QObject* getObject(const QString name) const; 00151 QObject* getObject(const char* property, const QVariant value) const; 00152 00153 Iterator getObjectIterator(const QString name); 00154 Iterator getObjectIterator(const char* property, const QVariant value); 00155 00156 ObjectList getObjects(const QString name) const; 00157 ObjectList getObjects(const char* property, const QVariant value) const; 00158 00159 template <typename T> T* getObject() const; 00160 template <typename T> T* getObject(const QString name) const; 00161 template <typename T> T* getObject(const char* property, const QVariant name) const; 00162 00163 template <typename T> QList<T*> getObjects() const; 00164 template <typename T> QList<T*> getObjects(const QString name) const; 00165 template <typename T> QList<T*> getObjects(const char* property, const QVariant name) const; 00166 00167 bool removeObject(Iterator it); 00168 00169 bool removeObject(); 00170 bool removeObject(const QString name); 00171 bool removeObject(const char* property, const QVariant value); 00172 00173 void removeObjects(); 00174 void removeObjects(const QString name); 00175 void removeObjects(const char* property, const QVariant value); 00176 00177 private: 00178 QList< QObject* > m_objects; 00179 }; 00180 00181 00182 //************************************************** 00183 //* template get objects 00184 //************************************************** 00185 00186 template <typename T> T* ObjectList::getObject() const 00187 { 00188 foreach(QObject* object, *this) 00189 { 00190 T* result = qobject_cast<T*>(object); 00191 if(result) 00192 { 00193 return result; 00194 } 00195 } 00196 return NULL; 00197 } 00198 00199 template <typename T> T* ObjectList::getObject(const QString name) const 00200 { 00201 foreach(QObject* object, *this) 00202 { 00203 T* result = qobject_cast<T*>(object); 00204 if(result && result->objectName() == name) 00205 { 00206 return result; 00207 } 00208 } 00209 return NULL; 00210 } 00211 00212 template <typename T> T* ObjectList::getObject(const char* property, const QVariant value) const 00213 { 00214 foreach(QObject* object, *this) 00215 { 00216 T* result = qobject_cast<T*>(object); 00217 if(result) 00218 { 00219 QVariant variant = result->property(property); 00220 if(variant.isValid() && variant==value) 00221 { 00222 return result; 00223 } 00224 } 00225 } 00226 return NULL; 00227 } 00228 00229 00230 00231 template <typename T> QList<T*> ObjectList::getObjects() const 00232 { 00233 QList<T*> results; 00234 T* result; 00235 foreach(QObject* object, *this) 00236 { 00237 result = qobject_cast<T*>(object); 00238 if(result) 00239 { 00240 results << result; 00241 } 00242 } 00243 return results; 00244 } 00245 00246 template <typename T> QList<T*> ObjectList::getObjects(const QString name) const 00247 { 00248 QList<T*> results; 00249 T* result; 00250 foreach(QObject* object, *this) 00251 { 00252 result = qobject_cast<T*>(object); 00253 if(result && result->objectName() == name) 00254 { 00255 results << result; 00256 } 00257 } 00258 return results; 00259 } 00260 00261 template <typename T> QList<T*> ObjectList::getObjects(const char* property, const QVariant value) const 00262 { 00263 QList<T*> results; 00264 T* result; 00265 foreach(QObject* object, *this) 00266 { 00267 result = qobject_cast<T*>(object); 00268 if(result) 00269 { 00270 QVariant variant = result->property(property); 00271 if(variant.isValid() && variant==value) 00272 { 00273 results << result; 00274 } 00275 } 00276 } 00277 return results; 00278 } 00279 00280 } // QExtend namespace 00281 00282 #endif // QEXTEND_OBJECTLIST_H
© 2000-2024 - www.developpez.com