core: implement an enumerating iterator

In the printing-template code, we loop through a vector and
then determine the index of the current element by searching
the vector. This irks me.

Since looping over a collection with an index is a rather
common theme, implement an enumerating iterator that can
be used as in:
	for (auto [idx, item]: enumerated_range(v)) {
		...
	}

For now, use it for the above vexing case. Convert other
iterations of this theme later.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-09-24 19:08:05 +02:00
parent 94641c510f
commit cea171ffd4
2 changed files with 56 additions and 2 deletions

View file

@ -3,6 +3,7 @@
#include "templateedit.h"
#include "templatelayout.h"
#include "core/qthelper.h"
#include "core/range.h"
#include <QDebug>
#include <QFileDialog>
@ -63,10 +64,10 @@ void PrintOptions::setupTemplates()
currList.sort();
int current_index = 0;
ui.printTemplate->clear();
Q_FOREACH(const QString& theme, currList) {
for (auto [idx, theme]: enumerated_range(currList)) {
// find the stored template in the list
if (theme == storedTemplate || theme == lastImportExportTemplate)
current_index = currList.indexOf(theme);
current_index = idx;
ui.printTemplate->addItem(theme.split('.')[0], theme);
}
ui.printTemplate->setCurrentIndex(current_index);