summaryrefslogtreecommitdiff
path: root/src/filelist.cc
blob: e1467e3efcde38cfc5dc240beddac921984b9528 (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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            filelist.cc
 *
 *  Mon Nov 30 15:35:52 CET 2009
 *  Copyright 2009 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo 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 DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "filelist.h"

#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>

#include <iostream>

#include "itemeditor.h"
#include "project.h"

#include <QAbstractItemModel>
#include <QTreeView>

#include <iostream>

#include <QComboBox>
#include <QLineEdit>
#include <QCheckBox>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QHeaderView>
#include <QStyleOptionButton>
#include <QApplication>

#include <sndfile.h>

class ChannelMapDeligate
	: public QStyledItemDelegate
{
public:
	ChannelMapDeligate(Instrument& instrument)
		: instrument(instrument)
	{
	}

	QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option,
	                      const QModelIndex &index) const override
	{
		// Main channel:
		if(index.column() == 2)
		{
			auto w = new QCheckBox(parent);
			auto audiofile_ids = instrument.getAudioFileList();
			auto audiofile_id = audiofile_ids.begin() + index.row();
			auto& audiofile = instrument.getAudioFile(*audiofile_id);

			connect(w, &QCheckBox::stateChanged,
			        [&audiofile](int state)
			        {
				        audiofile.setMainChannel(state == Qt::Checked);
			        });

			return w;
		}

		// Name:
		if(index.column() == 3)
		{
			auto w = new QLineEdit(parent);

			auto audiofile_ids = instrument.getAudioFileList();
			auto audiofile_id = audiofile_ids.begin() + index.row();
			auto& audiofile = instrument.getAudioFile(*audiofile_id);

			connect(w, &QLineEdit::textEdited,
			        [&audiofile](const QString& name)
			        {
				        audiofile.setName(name);
			        });
			return w;
		}

		// Channel Map ID:
		if(index.column() == 4)
		{
			auto w = new QComboBox(parent);
			w->addItem(tr("<None>"), -1);
			auto channel_ids = instrument.getProject().getChannelList();
			for(auto channel_id : channel_ids)
			{
				const auto& channel = instrument.getProject().getChannel(channel_id);
				w->addItem(channel.getChannelName(), channel.getId());
			}

			auto audiofile_ids = instrument.getAudioFileList();
			auto audiofile_id = audiofile_ids.begin() + index.row();
			auto& audiofile = instrument.getAudioFile(*audiofile_id);

			connect(w,
			        // This wierd line points the compiler to the correct overloaded
			        // version of QComboBox::currentIndexChanged
			        // ie. chooses the (int) version over the (const QString&) version
			        QOverload<int>::of(&QComboBox::currentIndexChanged),
			        [w, &audiofile](int idx)
			        {
				        audiofile.setChannelMapId(w->itemData(idx).toInt());
			        });
			return w;
		}

		return QStyledItemDelegate::createEditor(parent, option, index);
	}

	void setEditorData(QWidget *editor, const QModelIndex &index) const override
	{
		// Main channel:
		if(index.column() == 2)
		{
			auto w = static_cast<QCheckBox*>(editor);
			auto b = index.data(Qt::EditRole).toBool();
			w->setCheckState(b ? Qt::Checked : Qt::Unchecked);
		}

		// Name:
		if(index.column() == 3)
		{
			auto w = static_cast<QLineEdit*>(editor);
			auto s = index.data(Qt::EditRole).toString();
			w->setText(s);
		}

		// Channel Map ID:
		if(index.column() == 4)
		{
			auto w = static_cast<QComboBox*>(editor);
			auto i = w->findData(index.data(Qt::EditRole).toInt());
			w->setCurrentIndex(i);
		}
}

	void setModelData(QWidget *editor, QAbstractItemModel *model,
	                  const QModelIndex &index) const override
	{
		// Main channel:
		if(index.column() == 2)
		{
			model->setData(index,
			               static_cast<QCheckBox*>(editor)->checkState() == Qt::Checked,
			               Qt::EditRole);
		}

		// Name:
		if(index.column() == 3)
		{
			model->setData(index, static_cast<QLineEdit*>(editor)->text(),
			               Qt::EditRole);
		}

		// Channel Map ID:
		if(index.column() == 4)
		{
			model->setData(index, static_cast<QComboBox*>(editor)->currentData(),
			               Qt::EditRole);
		}
	}

	void paint(QPainter* painter, const QStyleOptionViewItem& option,
	           const QModelIndex& index) const override
	{
		if(index.column() == 2) // Main Channel
		{
			auto audiofile_ids = instrument.getAudioFileList();
			auto audiofile_id = audiofile_ids.begin() + index.row();
			const auto& audiofile = instrument.getAudioFile(*audiofile_id);

			bool checked = audiofile.getMainChannel();
			QStyleOptionButton butOpt;
			butOpt.state = QStyle::State_Enabled;
			butOpt.state |= checked ? QStyle::State_On : QStyle::State_Off;
			butOpt.rect = option.rect;
			QApplication::style()->drawControl( QStyle::CE_CheckBox,
			                                    &butOpt, painter );
		}
		else
		{
			QStyledItemDelegate::paint(painter, option, index);
		}
	}

private:
	Instrument& instrument;
};

class FileDataModel
	: public QAbstractItemModel
{
public:
	FileDataModel(Instrument& instrument)
		: instrument(instrument)
	{
	}

	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
	{
		if(!hasIndex(row, column, parent))
		{
			return QModelIndex();
		}

		auto audiofile_ids = instrument.getAudioFileList();
		if(!parent.isValid())
		{
			if(row < audiofile_ids.size())
			{
				return createIndex(row, column, &instrument);
			}
			else
			{
				return QModelIndex(); // row is out of bounds.
			}
		}

		return QModelIndex();
	}

	QModelIndex parent(const QModelIndex &index) const override
	{
		return QModelIndex(); // no parent
	}

	int rowCount(const QModelIndex &parent = QModelIndex()) const override
	{
		if(parent.column() > 0) // only return row count on first column
		{
			return 0;
		}

		if(!parent.isValid())
		{
			auto audiofile_ids = instrument.getAudioFileList();
			return audiofile_ids.size(); // root level
		}

		return 0; // no children
	}

	int columnCount(const QModelIndex &parent = QModelIndex()) const override
	{
		return 5;
	}

	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
	{
		if(!index.isValid())
		{
			return QVariant();
		}

		auto audiofile_ids = instrument.getAudioFileList();

		auto audiofile_id = audiofile_ids.begin() + index.row();
		const auto& audiofile = instrument.getAudioFile(*audiofile_id);

		if(role == Qt::DecorationRole )
		{
			if(index.column() == 0)
			{
				if(audiofile.getAbsoluteFile() == instrument.getMasterFile())
				{
					return QPixmap(":/icons/master.png");
				}
				else
				{
					return QPixmap(":/icons/file.png");
				}
			}
		}

		if(role == Qt::DisplayRole)
		{
			switch(index.column())
			{
			case 0: return QVariant(); // Master
			case 1: return audiofile.getFile();
			case 2: return audiofile.getMainChannel() ? "x" : " ";
			case 3: return audiofile.getName();
			case 4:
				{
					auto channelMapId = audiofile.getChannelMapId();
					if(channelMapId == -1)
					{
						return tr("<None>");
					}
					return instrument.getProject().getChannel(channelMapId).getChannelName();
				}
			default: return QVariant();
			}
		}
		else if(role == Qt::EditRole)
		{
			switch(index.column())
			{
			case 0: return QVariant(); // Master
			case 1: return audiofile.getFile();
			case 2: return audiofile.getMainChannel();
			case 3: return audiofile.getName();
			case 4: return audiofile.getChannelMapId();
			default: return QVariant();
			}
		}
		else

		{
			return QVariant();
		}
	}

	QVariant headerData(int section, Qt::Orientation orientation, int role) const override
	{
		if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
		{
			switch(section)
			{
			case 0: return tr("M");
			case 1: return tr("Filename");
			case 2: return tr("m");
			case 3: return tr("Name");
			case 4: return tr("Kit Channel");
			default: return QVariant();
			}
		}

		return QVariant();
	}

	Qt::ItemFlags flags(const QModelIndex &index) const override
	{
		if(!index.isValid())
		{
			return 0;
		}

		switch(index.column())
		{
		case 0: // Master
			return QAbstractItemModel::flags(index);
		case 1: // File
			return QAbstractItemModel::flags(index);
		case 2: // Main channel
			return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
		case 3: // Name
			return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
		case 4: // Channel map id
			return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
		}
	}

	bool setData(const QModelIndex &index, const QVariant &value,
	             int role = Qt::EditRole) override
	{
		auto audiofile_ids = instrument.getAudioFileList();

		if(index.row() > audiofile_ids.size() ||
		   index.column() > (columnCount() - 1))
		{
			return false;
		}

		auto audiofile_id = audiofile_ids.begin() + index.row();
		auto& audiofile = instrument.getAudioFile(*audiofile_id);

		switch(index.column())
		{
		case 0: // Master
			break;
		case 1: // File
			break;
		case 2: // Main Channel
			audiofile.setMainChannel(value.toBool());
			break;
		case 3: // Name
			audiofile.setName(value.toString());
			break;
		case 4: // Channel map id
			audiofile.setChannelMapId(value.toInt());
			break;
		default: break;
		}

		return true;
	}

	void refresh()
	{
		beginResetModel();
		endResetModel();
	}

private:
	Instrument& instrument;
};

FileList::FileList(Instrument& instrument)
	: instrument(instrument)
{
	model = new FileDataModel(instrument);
	setModel(model);
	setItemDelegate(new ChannelMapDeligate(instrument));
	setEditTriggers(QAbstractItemView::AllEditTriggers); // show list on click

	setContextMenuPolicy(Qt::CustomContextMenu); // enable context menu
	connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
	        this, SLOT(onCustomContextMenu(const QPoint &)));
	setRootIsDecorated(false);
	header()->resizeSection(0, 24);
	header()->resizeSection(2, 24);

	connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
	        this, SLOT(selectionChanged(const QModelIndex&)));

	createMenus();
}

static double getSamplerate(const QString& file)
{
	SF_INFO sf_info;
	SNDFILE *fh = sf_open(file.toStdString().c_str(), SFM_READ, &sf_info);
	if(!fh)
	{
		return -1.0;
	}

	return sf_info.samplerate;
}

void FileList::addFiles()
{
	auto root = instrument.getProject().getRawFileRoot();
	if(path.isEmpty())
	{
		path = root;
	}

	QStringList files =
		QFileDialog::getOpenFileNames(this, tr("Open file"),
		                              path, tr("Audio Files (*.wav)"));
	QStringList::Iterator i = files.begin();
	while(i != files.end())
	{
		QString file = *i;
		QFileInfo fi(file);
		QString name = fi.baseName();
		path = fi.absolutePath();

		double samplerate = getSamplerate(file);
		if(samplerate == -1.0)
		{
			// Error reading file. Skip it.
			std::cout << "Error reading file. Skip it.\n";
			++i;
			continue;
		}

		if(instrument.getProject().getProjectSamplerate() == -1.0)
		{
			// Samplerate not yet set
			instrument.getProject().setProjectSamplerate(samplerate);
		}

		if(instrument.getProject().getProjectSamplerate() != samplerate)
		{
			// Samplerate of file differs from the projeft samplerate
			std::cout << "Samplerate of file differs from the project samplerate:\n";
			std::cout << " project: " << instrument.getProject().getProjectSamplerate() << std::endl;
			std::cout << " file: " << samplerate << std::endl;
		}

		if(root == file.left(root.length()))
		{
			file = file.mid(root.length() + 1);
		}

		auto id = instrument.createAudioFile();
		auto& audiofile = instrument.getAudioFile(id);
		audiofile.setName(name);
		audiofile.setFile(file);
		audiofile.setChannelMapId(-1);
		audiofile.setMainChannel(false);

		i++;
	}

	model->refresh();
}

void FileList::selectionChanged(const QModelIndex &index)
{
	auto audiofile_ids = instrument.getAudioFileList();
	auto audiofile_id = audiofile_ids.begin() + index.row();
	const auto& audiofile = instrument.getAudioFile(*audiofile_id);

	instrument.setMasterFile(audiofile.getFile());

	emit masterFileChanged(audiofile.getAbsoluteFile());
	//setMasterFile(i);
	model->refresh();
}

void FileList::createMenus()
{
	menu = new QMenu();

	setMasterAction = new QAction(tr("Set as Master (dbl-click)"), this);
	connect(setMasterAction, SIGNAL(triggered()), this, SLOT(setMaster()));

	removeAction = new QAction(tr("Remove"), this);
	connect(removeAction, SIGNAL(triggered()), this, SLOT(removeFile()));

	removeAllAction = new QAction(tr("Remove all"), this);
	connect(removeAllAction, SIGNAL(triggered()), this, SLOT(removeAllFiles()));

	menu->addAction(setMasterAction);
	menu->addAction(removeAction);
	menu->addSeparator();
	menu->addAction(removeAllAction);
}

void FileList::onCustomContextMenu(const QPoint &point)
{
	QModelIndex index = indexAt(point);
	if(index.isValid())
	{
		menu->popup(viewport()->mapToGlobal(point));
	}
}

void FileList::setMaster()
{
//	setMasterFile(activeItem);
}

void FileList::removeFile()
{
	auto audiofile_ids = instrument.getAudioFileList();
	auto audiofile_id = audiofile_ids.begin() + currentIndex().row();
	auto& audiofile = instrument.getAudioFile(*audiofile_id);
	if(instrument.getMasterFile() == audiofile.getAbsoluteFile())
	{
		emit masterFileChanged(""); // Clear canvas
	}

	instrument.deleteAudioFile(*audiofile_id);

	//emit fileRemoved(file, name);
}

void FileList::removeAllFiles()
{
	instrument.setMasterFile("");
	auto audiofile_ids = instrument.getAudioFileList();
	for(auto audiofile_id : audiofile_ids)
	{
		instrument.deleteAudioFile(audiofile_id);
	}
	reset();

	emit masterFileChanged(""); // Clear canvas
	emit allFilesRemoved();
}