sortercontainer.cpp
1 #include "sortercontainer.h" 2 #include "sorter.h" 3 #include <QtQml> 4 5 namespace qqsfpm { 6 7 /*! 8 \qmltype SorterContainer 9 \qmlabstract 10 \inqmlmodule SortFilterProxyModel 11 \ingroup SorterAttached 12 \brief Abstract interface for types containing \l {Sorter}{Sorters}. 13 14 \section2 Types implementing this interface: 15 \annotatedlist SorterContainer 16 */ 17 18 QList<Sorter*> SorterContainer::sorters() const 19 { 20 return m_sorters; 21 } 22 23 void SorterContainer::appendSorter(Sorter* sorter) 24 { 25 m_sorters.append(sorter); 26 onSorterAppended(sorter); 27 } 28 29 void SorterContainer::removeSorter(Sorter *sorter) 30 { 31 m_sorters.removeOne(sorter); 32 onSorterRemoved(sorter); 33 } 34 35 void SorterContainer::clearSorters() 36 { 37 m_sorters.clear(); 38 onSortersCleared(); 39 } 40 41 QQmlListProperty<Sorter> SorterContainer::sortersListProperty() 42 { 43 return QQmlListProperty<Sorter>(reinterpret_cast<QObject*>(this), &m_sorters, 44 &SorterContainer::append_sorter, 45 &SorterContainer::count_sorter, 46 &SorterContainer::at_sorter, 47 &SorterContainer::clear_sorters); 48 } 49 50 void SorterContainer::append_sorter(QQmlListProperty<Sorter>* list, Sorter* sorter) 51 { 52 if (!sorter) 53 return; 54 55 SorterContainer* that = reinterpret_cast<SorterContainer*>(list->object); 56 that->appendSorter(sorter); 57 } 58 59 SorterContainer::sizetype SorterContainer::count_sorter(QQmlListProperty<Sorter>* list) 60 { 61 QList<Sorter*>* sorters = static_cast<QList<Sorter*>*>(list->data); 62 return sorters->count(); 63 } 64 65 Sorter* SorterContainer::at_sorter(QQmlListProperty<Sorter>* list, qqsfpm::SorterContainer::sizetype index) 66 { 67 QList<Sorter*>* sorters = static_cast<QList<Sorter*>*>(list->data); 68 return sorters->at(index); 69 } 70 71 void SorterContainer::clear_sorters(QQmlListProperty<Sorter> *list) 72 { 73 SorterContainer* that = reinterpret_cast<SorterContainer*>(list->object); 74 that->clearSorters(); 75 } 76 77 SorterContainerAttached::SorterContainerAttached(QObject* object) : QObject(object), 78 m_sorter(qobject_cast<Sorter*>(object)) 79 { 80 if (!m_sorter) 81 qmlWarning(object) << "SorterContainerAttached must be attached to a Sorter"; 82 } 83 84 SorterContainerAttached::~SorterContainerAttached() 85 { 86 if (m_sorter && m_container) { 87 SorterContainer* container = qobject_cast<SorterContainer*>(m_container.data()); 88 container->removeSorter(m_sorter); 89 } 90 } 91 92 /*! 93 \qmlattachedproperty bool SorterContainer::container 94 This attached property allows you to include in a \l SorterContainer a \l Sorter that 95 has been instantiated outside of the \l SorterContainer, for example in an Instantiator. 96 */ 97 QObject* SorterContainerAttached::container() const 98 { 99 return m_container; 100 } 101 102 void SorterContainerAttached::setContainer(QObject* object) 103 { 104 if (m_container == object) 105 return; 106 107 SorterContainer* container = qobject_cast<SorterContainer*>(object); 108 if (object && !container) 109 qmlWarning(parent()) << "container must inherits from SorterContainer, " << object->metaObject()->className() << " provided"; 110 111 if (m_container && m_sorter) 112 qobject_cast<SorterContainer*>(m_container.data())->removeSorter(m_sorter); 113 114 m_container = container ? object : nullptr; 115 if (container && m_sorter) 116 container->appendSorter(m_sorter); 117 118 Q_EMIT containerChanged(); 119 } 120 121 SorterContainerAttached* SorterContainerAttached::qmlAttachedProperties(QObject* object) 122 { 123 return new SorterContainerAttached(object); 124 } 125 126 }