00001 00002 00003 00004 00005 #ifndef FILTERMANAGER_H 00006 #define FILTERMANAGER_H 00007 00008 #include <algorithm> 00009 00010 #include <QObject> 00011 #include <QList> 00012 #include <QHash> 00013 #include <QtDebug> 00014 #include <QMetaType> 00015 #include <QVariant> 00016 00017 #include "filter/filter.h" 00018 00019 class QActionGroup; 00020 class Filter; 00021 00023 00027 class FilterManager : public QObject 00028 { 00029 Q_OBJECT 00030 00031 typedef QList<Filter*> FilterList; 00032 typedef QHash<int, FilterList> FilterMap; 00033 typedef QList<int> FilterTypeList; 00034 00035 FilterMap m_filters; 00036 FilterTypeList m_types; 00037 00038 public: 00040 explicit FilterManager(QObject *parent = 0); 00041 00044 template <typename T> void registerFilterableType(); 00045 00048 void registerFilterableType(int type); 00049 00053 bool isTypeRegistered(int type) const; 00054 00057 void addFilter(Filter* filter); 00058 00062 template <typename T> bool checkValue(const T& t) const; 00063 00067 template <typename T> void filterList(QList<T>& container) const; 00068 00072 template <typename T> QList<QActionGroup*> createActions(); 00073 00076 template <typename T> void connectType(const QObject* object, const char* method, Qt::ConnectionType connType = Qt::AutoConnection); 00077 00078 signals: 00079 void stateChanged(int type = -1); 00080 }; 00081 00082 template <typename T> 00083 void FilterManager::registerFilterableType() 00084 { 00085 registerFilterableType(qMetaTypeId<T>()); 00086 } 00087 00088 template <typename T> 00089 bool FilterManager::checkValue(const T& t) const 00090 { 00091 int type = qMetaTypeId<T>(); 00092 if (!isTypeRegistered(type)) { 00093 qWarning() << "Type " << QMetaType::typeName(type) << " not registered"; 00094 return true; 00095 } 00096 00097 bool ret = true; 00098 QVariant var = QVariant::fromValue(t); 00099 const FilterList& filters = m_filters[type]; 00100 foreach (const Filter* filter, filters) { 00101 if (filter->isFiltered(var)) { 00102 ret = false; 00103 break; 00104 } 00105 } 00106 return ret; 00107 } 00108 00109 template <typename T> void FilterManager::filterList(QList<T>& container) const 00110 { 00111 if (!container.empty()) { 00112 QMutableListIterator<T> iter(container); 00113 while (iter.hasNext()) { 00114 if (!checkValue(iter.next())) 00115 iter.remove(); 00116 } 00117 } 00118 } 00119 00120 template <typename T> 00121 QList<QActionGroup*> FilterManager::createActions() 00122 { 00123 int type = qMetaTypeId<T>(); 00124 Q_ASSERT(isTypeRegistered(type)); 00125 00126 FilterList& filters = m_filters[type]; 00127 QList<QActionGroup*> ret; 00128 00129 foreach (Filter* filter, filters) { 00130 ret.append(filter->createActions()); 00131 } 00132 00133 return ret; 00134 } 00135 00136 template <typename T> 00137 void FilterManager::connectType(const QObject* object, const char* method, Qt::ConnectionType connType) 00138 { 00139 int type = qMetaTypeId<T>(); 00140 Q_ASSERT(isTypeRegistered(type)); 00141 00142 FilterList& filters = m_filters[type]; 00143 foreach (Filter* filter, filters) { 00144 connect(filter, SIGNAL(stateChanged()), object, method, connType); 00145 } 00146 } 00147 00148 #endif // FILTERMANAGER_H