diff -r 165777a20dc7 -r d9a3b153f679 src/main.h --- a/src/main.h Sun Jan 10 15:28:44 2021 +0200 +++ b/src/main.h Sun Jan 10 17:21:32 2021 +0200 @@ -153,3 +153,62 @@ .arg(toDouble(vec.z)) .arg(toDouble(vec.w)); } + +template +struct KeyValuePair +{ + K key; + V value; +}; + +template +struct MapItemsIterator : IteratorType +{ + template + MapItemsIterator(Ts&&... args) : IteratorType{args...} {} + auto operator*() const + { + return KeyValuePair{this->key(), this->value()}; + } +}; + +template +struct MapItems +{ + MapType& map; + IteratorType begin() + { + return IteratorType(this->map.begin()); + } + + IteratorType end() + { + return IteratorType(this->map.end()); + } +}; + +/* + * Python's dict.items for QMap: use in a for loop to iterate a map to + * get both keys and values. Iteration yields KeyValuePairs. + */ +template +auto items(const QMap& map) +{ + return MapItems< + const K&, + const V&, + const QMap, + MapItemsIterator::const_iterator> + >{map}; +} + +template +auto items(QMap& map) +{ + return MapItems< + const K&, + V&, + QMap, + MapItemsIterator::iterator> + >{map}; +}