~mhall119/ubuntu-app-reviews/qtranscribe

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *  Copyright © 2012 Matt Pharoah (mr.exuberant@gmail.com)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef FOOTPEDALWIDGETMODELS_HH_
#define FOOTPEDALWIDGETMODELS_HH_

#include <QAbstractListModel>
#include <QAbstractTableModel>
#include <QVariant>
#include <QString>
#include <mutex>
#include <stdexcept>
#include <cstdio>
#include <cstring>
#include <bitset>
#include "Joystick.hh"
#include "Actions.hh"

class FootPedalListModel : public QAbstractListModel {
  private:
	char *pedals[10]; //first character of each entry is joystick port + 1, 2nd is number of pedals, remaining characters are device name string
	unsigned char numPedals;
	mutable mutex safety;

  public:
	FootPedalListModel() : QAbstractListModel() {
		numPedals = 0;
		recheck();
	}

	void recheck() {
		safety.lock();
		//clear old data
		for (int i = 0; i < numPedals; i++) {
			delete[] pedals[i];
		}
		numPedals = 0;
		//add new data
		for (unsigned char i = 0; i <= 9; i++) {
			char *info = new char[258];
			info[0] = i + 1;
			try {
				Joystick::getNameAndButtons(i, &info[2], 256, (unsigned char *) &info[1]);
				pedals[numPedals++] = info;
			} catch (invalid_argument &e) {
				delete[] info;
			}
		}
		safety.unlock();
		emit dataChanged(index(0,0), index(9,0));
	}

	QModelIndex find(char *ID) {
		for (short i = 0; i < numPedals; i++) {
			if (strcmp(pedals[i], ID) == 0) return index(i, 0);
		}
		return QModelIndex();
	}

	short getButtons(int index) {
		safety.lock();
		short ret;
		if (index >= 0 && index < numPedals) {
			ret = (short) (unsigned char) pedals[index][1]; //double cast so it's unsigned... not that there will ever be 128 or more pedals anyways
		} else {
			ret = -1;
		}
		safety.unlock();
		return ret;
	}

	unsigned char getPort(int index) {
		safety.lock();
		unsigned char ret;
		if (index >= 0 && index < numPedals) {
			ret = ((unsigned char) pedals[index][0]) - 1; //double cast so it's unsigned... not that there will ever be 128 or more pedals anyways
		} else {
			ret = 0xff;
		}
		safety.unlock();
		return ret;
	}

	void getStringID(int index, char *dest) {
		safety.lock();
		if (index >= 0 && index < numPedals) {
			strcpy(dest, pedals[index]);
		} else {
			strcpy(dest, "");
		}
		safety.unlock();
	}

	bool stringIDequals(int index, char *compareTo) {
		bool ret;
		safety.lock();
		if (index >= 0 && index < numPedals) {
			ret = (strcmp(pedals[index], compareTo) == 0);
		} else {
			ret = (strcmp(pedals[index], "") == 0);
		}
		safety.unlock();
		return ret;
	}

	int rowCount(const QModelIndex &parent) const {
		return numPedals;
	}

	QVariant data(const QModelIndex &index, int role) const {
		QVariant ret;
		safety.lock();
		if (role == Qt::DisplayRole) {
			const register int i = index.row();
			if (i >= 0 && i < numPedals) {
				ret = QVariant(QString(&pedals[i][2]));
			} else {
				ret = QVariant(QString());
			}
		} else {
			ret = QVariant();
		}
		safety.unlock();
		return ret;
	}

	~FootPedalListModel() {
		safety.lock(); //shouldn't be accessing it after delete anyways
		for (int i = 0; i < numPedals; i++) {
			delete[] pedals[i];
		}
	}
  protected:
	void timerEvent(QTimerEvent *ev) {
		recheck();
	}
};

class PedalCommandTable : public QAbstractTableModel {
  private:
	Joystick *currentPedal;
	unsigned char numButtons;
	unsigned char pedalID;
	mutable mutex safety;
	bitset<256> pedalState;
	Action commands[256];

	void updateCell(unsigned char row, unsigned char col) {
		emit(dataChanged(index(row, col), index(row, col)));
	}

	static void pedalEventHandler(const JSEvent &ev, void *ptr) {
		PedalCommandTable *me = (PedalCommandTable *) ptr;
		if (ev.type == Joystick::BUTTON_EVENT && (ev.value == Joystick::PRESSED || ev.value == Joystick::RELEASED)) {
			me->safety.lock();
			me->pedalState[ev.button] = (ev.value == Joystick::PRESSED);
			me->safety.unlock();
			me->updateCell(ev.button, 2);
		}
	}

  public:
	PedalCommandTable() : QAbstractTableModel() {
		currentPedal = NULL;
		pedalID = 0xff;
		numButtons = 0;
		for (short i = 0; i < 256; i++) commands[i] = A_NOOP;
	}

	void changeJoystick(unsigned char jsnum, unsigned char nb) {
		safety.lock();
		bool REMOVE = (numButtons > 0);
		if (REMOVE) beginRemoveRows(QModelIndex(), 0, numButtons - 1);
		pedalID = jsnum;
		numButtons = nb;
		if (currentPedal != NULL) {
			delete currentPedal;
			currentPedal = NULL;
		}
		pedalState.reset();
		if (REMOVE) endRemoveRows();
		if (numButtons > 0 && jsnum <= 9) beginInsertRows(QModelIndex(), 0, numButtons - 1);
		if (jsnum <= 9) currentPedal = new Joystick(jsnum, pedalEventHandler, NULL, this);
		if (numButtons > 0 && jsnum <= 9) endInsertRows();
		safety.unlock();
		emit(dataChanged(index(0,0), index(255, 2)));
	}

	void changeCommand(const QModelIndex &index, Action cmd) {
		commands[index.row()] = cmd;
		updateCell(index.row(), 1);
	}

	void changeCommand(int index, Action cmd) {
		commands[index] = cmd;
		updateCell(index, 1);
	}

	inline Action getCommand(const QModelIndex &index) {
		return commands[index.row()];
	}

	inline Action getCommand(int index) {
		return commands[index];
	}

	int rowCount(const QModelIndex &parent = QModelIndex()) const {
		return numButtons;
	}

	int columnCount(const QModelIndex &parent) const {
		return 3;
	}

	QVariant headerData(int col, Qt::Orientation orientation, int role) const {
		if (role != Qt::DisplayRole || orientation != Qt::Orientation::Horizontal) return QVariant();
		switch (col) {
			case 0: return QVariant(QString("Button"));
			case 1: return QVariant(QString("Command"));
			case 2: return QVariant(QString("Pressed?"));
			default: return QVariant();
		}
	}

	QVariant data(const QModelIndex &index, int role) const {
		if (role != Qt::DisplayRole) return QVariant();
		safety.lock();
		QVariant ret;
		if (pedalID <= 9 && index.row() >= 0 && index.row() < rowCount() && index.column() <= 256) {
			switch (index.column()) {
				case 0: {
					char str[10];
					sprintf(str, "Pedal %d", index.row() + 1);
					ret = QVariant(QString(str));
				}; break;
				case 1: {
					ret = QVariant(QString(ACTION_STRING(commands[index.row()])));
				}; break;
				case 2: {
					if (pedalState[index.row()]) ret = QVariant(QString("YES"));
				}; break;
			}
		}
		safety.unlock();
		return ret;
	}

	~PedalCommandTable() {
		safety.lock(); //shouldn't be accessing it after delete anyways
		if (currentPedal != NULL) delete currentPedal;
	}
};


#endif /* FOOTPEDALWIDGETMODELS_HH_ */