summaryrefslogtreecommitdiff
path: root/lsseteditor.h
blob: 0b09ca63b84c6cf1d543bf337af55760f9e98d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef LSSETEDITOR_H
#define LSSETEDITOR_H

#include <QWidget>
#include <QSpinBox>
#include <QComboBox>

class _LSSetEntryEditorBaseBase : public QWidget {
	Q_OBJECT
protected:
	explicit _LSSetEntryEditorBaseBase(QWidget *parent = 0) :
		QWidget(parent) { }

signals:
	void dataEdited();
};

// class prototype
template <typename TData, typename TWidget>
class LSSetEditor;

template <typename TData>
class LSSetEntryEditorBase : public _LSSetEntryEditorBaseBase {
protected:
	explicit LSSetEntryEditorBase(QWidget *parent = 0) :
		_LSSetEntryEditorBaseBase(parent) { }

public:
	void setCurrentEntry(TData &entry) {
		m_currentEntry = &entry;
		loadEntryFrom(entry);
	}

private:
	TData *m_currentEntry;

protected:
	virtual void loadEntryFrom(const TData &entry) = 0;
	TData *currentEntry() const { return m_currentEntry; }
};




class _LSSetEditorBase : public QWidget {
	Q_OBJECT
public:
	explicit _LSSetEditorBase(int maxEntries, QWidget *parent = 0);

protected:
	int m_maxEntries;
	QSpinBox *m_entryCount;
	QComboBox *m_chooser;

	QWidget *m_setEditorWidget;

	int m_loadingThings;

	void setup(QWidget *eWidget);

	virtual void changeEntryCountTo(int count) = 0;
	virtual void showEntry(int index) = 0;
	virtual void resizeDataListTo(int count) = 0;

private slots:
	void handleEntryCountChanged(int count);
	void handleEntrySelected(int index);

signals:
	void dataEdited();

};

template <typename TData, typename TWidget>
class LSSetEditor : public _LSSetEditorBase {
public:
	explicit LSSetEditor(int maxEntries, QWidget *parent = 0) :
		_LSSetEditorBase(maxEntries, parent) {

		TWidget *w = new TWidget(this);
		setup(w);

		m_setEditorWidget = w;
		m_typedSetEditorWidget = w;

		LSSetEntryEditorBase<TData> *checkMe = w;
		connect(checkMe, SIGNAL(dataEdited()), SIGNAL(dataEdited()));
	}

	void setData(QList<TData> *newData) {
		m_loadingThings++;

		m_data = newData;
		m_entryCount->setValue(newData->count());
		changeEntryCountTo(newData->count());

		m_chooser->setCurrentIndex(newData->count() ? 0 : -1);
		showEntry(newData->count() ? 0 : -1);

		m_loadingThings--;
	}

protected:
	QList<TData> *m_data;
	TWidget *m_typedSetEditorWidget;

	void changeEntryCountTo(int count) {
		m_loadingThings++;

		int existingCount = m_chooser->count();

		if (existingCount > count) {
			// remove something
			int nowSelected = m_chooser->currentIndex();

			if (nowSelected >= count) {
				// oops, we'll need to select something else
				showEntry(count - 1);
				m_chooser->setCurrentIndex(count - 1);
			}

			for (int i = (existingCount - 1); i >= count; i--)
				m_chooser->removeItem(i);

		} else if (count > existingCount) {
			// add something

			for (int i = existingCount; i < count; i++)
				m_chooser->addItem(QString("Set %1").arg(i + 1));
		}

		m_loadingThings--;
	}

	void resizeDataListTo(int count) {
		m_data->reserve(count);

		while (m_data->count() < count)
			m_data->append(TData());
		while (m_data->count() > count)
			m_data->removeLast();
	}

	void showEntry(int index) {
		m_loadingThings++;

		if (index == -1) {
			m_setEditorWidget->setEnabled(false);
		} else {
			m_setEditorWidget->setEnabled(true);
			TData &entry = (*m_data)[index];
			m_typedSetEditorWidget->setCurrentEntry(entry);
		}
	}
};




#endif // LSSETEDITOR_H