1 /* vi: set sw=4 ts=4:
2  *
3  * Copyright (C) 2015 Christian Hohnstaedt.
4  *
5  * All rights reserved.
6  */
7 
8 #ifndef __ITEMCOMBO_H
9 #define __ITEMCOMBO_H
10 
11 #include <QList>
12 #include <QComboBox>
13 
14 #include "lib/pki_base.h"
15 #include "lib/pki_x509.h"
16 #include "lib/pki_x509req.h"
17 #include "lib/pki_temp.h"
18 
19 template <class T> class itemCombo : public QComboBox
20 {
21     public:
itemCombo(QWidget * parent)22 	itemCombo(QWidget *parent) : QComboBox(parent) { }
insertPkiItems(QList<T * > items)23 	void insertPkiItems(QList<T*> items) {
24 		clear();
25 		foreach(T *p, items) {
26 			addItem(p->comboText(), QVariant::fromValue(p));
27 		}
28 	}
currentPkiItem()29 	T *currentPkiItem() {
30 		return itemData(currentIndex()).template value<T*>();
31 	}
setNullItem(QString text)32 	void setNullItem(QString text) {
33 		if (itemData(0).template value<T*>() == NULL)
34 			removeItem(0);
35 		insertItem(0, text, QVariant());
36 	}
setCurrentPkiItem(T * p)37 	int setCurrentPkiItem(T *p) {
38 		int idx = findData(QVariant::fromValue(p));
39 		setCurrentIndex(idx);
40 		return idx;
41 	}
42 };
43 
44 typedef class itemCombo<pki_temp> itemComboTemp;
45 typedef class itemCombo<pki_x509req> itemComboReq;
46 typedef class itemCombo<pki_x509> itemComboCert;
47 typedef class itemCombo<pki_key> itemComboKey;
48 #endif
49