summaryrefslogtreecommitdiff
path: root/src/renderdialog.cc
blob: 758f99ec5b8be6ed9f2f19a36dd79268bcc5ce2e (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            renderdialog.cc
 *
 *  Thu Sep 20 17:32:11 CEST 2018
 *  Copyright 2018 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 "renderdialog.h"

#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QFileDialog>
#include <QLabel>
#include <QAbstractItemModel>
#include <QHeaderView>
#include <QStyledItemDelegate>
#include <QStyleOptionProgressBar>
#include <QApplication>

#include "project.h"

class ProgressDelegate
	: public QStyledItemDelegate
{
//	Q_OBJECT

public:
	ProgressDelegate(Project& project, QObject *parent)
		: QStyledItemDelegate(parent)
		, project(project)
	{
	}

	void paint(QPainter *painter,
	           const QStyleOptionViewItem &option,
	           const QModelIndex &index ) const override
	{
		if(/*index.row() == current_row &&*/
		   index.column() == 2 &&
		   index.parent() == QModelIndex())
		{
			QStyleOptionProgressBar progressBarOption;
			progressBarOption.state = QStyle::State_Enabled;
			progressBarOption.direction = QApplication::layoutDirection();
			progressBarOption.rect = option.rect;
			progressBarOption.fontMetrics = QApplication::fontMetrics();
			progressBarOption.minimum = 0;
			progressBarOption.textAlignment = Qt::AlignCenter;
			progressBarOption.textVisible = true;

			if(index.row() == current_row)
			{
				// Current task
				progressBarOption.maximum = progress_max;
				progressBarOption.progress = progress;
			}
			else if(index.row() < current_row)
			{
				// Finished task
				progressBarOption.maximum = 100;
				progressBarOption.progress = 100;
			}
			else
			{
				// Waiting task
				progressBarOption.maximum = 100;
				progressBarOption.progress = 0;
			}
			QApplication::style()->drawControl(QStyle::CE_ProgressBar,
			                                   &progressBarOption, painter);
		}
		else
		{
			QStyledItemDelegate::paint(painter, option, index);
		}
	}

	void setActiveTask(int current_instrument_id)
	{
		auto instrument_ids = project.getInstrumentList();
		current_row = 0;
		for(auto instrument_id : instrument_ids)
		{
			if(current_instrument_id == instrument_id)
			{
				return;
			}
			current_row++;
		}

		// current_row will be one above the row count here
	}

	void setProgressMaximum(int progress_max)
	{
		this->progress_max = progress_max;
	}

	void setProgress(int progress)
	{
		this->progress = progress;
	}

private:
	Project& project;
	int progress{0};
	int progress_max{100};
	int current_row{-1};
};

class RenderDataModel
	: public QAbstractItemModel
{
public:
	RenderDataModel(Project& project)
		: project(project)
	{
	}

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

		if(!parent.isValid())
		{
			auto instrument_ids = project.getInstrumentList();
			if(row < instrument_ids.size())
			{
				return createIndex(row, column, (void*)42);
			}
			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 instrument_ids = project.getInstrumentList();
			return instrument_ids.size();
		}

		return 0; // no children
	}

	int columnCount(const QModelIndex &parent = QModelIndex()) const override
	{
		return 3; // icon, text, progressbar
	}

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

		if(role == Qt::DecorationRole )
		{
			if(index.column() == 0)
			{
				if(index.row() < current_row)
				{
					return QPixmap(":/icons/task_done.png");
				}
				else if(index.row() == current_row)
				{
					//return QPixmap(":/icons/task_error.png");
					return QPixmap(":/icons/task_running.png");
				}
				else
				{
					return QPixmap(":/icons/task_waiting.png");
				}
			}
		}

		if(role == Qt::DisplayRole)
		{
			auto instrument_ids = project.getInstrumentList();
			auto instrument_id = instrument_ids.begin() + index.row();
			const auto& instrument = project.getInstrument(*instrument_id);
			switch(index.column())
			{
			case 1: return instrument.getInstrumentName();
			default: return QVariant();
			}
		}

		return QVariant();
	}

	QVariant headerData(int section, Qt::Orientation orientation, int role) const override
	{
		return QVariant();
	}

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

	void setActiveTask(int current_instrument_id)
	{
		auto instrument_ids = project.getInstrumentList();
		current_row = 0;
		for(auto instrument_id : instrument_ids)
		{
			if(current_instrument_id == instrument_id)
			{
				return;
			}
			current_row++;
		}

		// current_row will be one above the row count here
	}

private:
	Project& project;
	int current_row{-1};
};

RenderDialog::RenderDialog(QWidget* parent, Project& project)
	: QDialog(parent)
	, project(project)
	, renderer(project)
{
	setWindowTitle(tr("Export drumkit"));
	model = new RenderDataModel(project);

	auto vl = new QVBoxLayout();
	setLayout(vl);

	layout()->addWidget(new QLabel(tr("Export path:")));

	{
		auto hl = new QHBoxLayout();
		export_path = new QLineEdit(this);
		export_path->setText(project.getExportPath());
		auto btn = new QPushButton(tr("..."));
		btn->setMaximumWidth(32);
		connect(btn, SIGNAL(clicked()), this, SLOT(chooseExportPath()));
		hl->addWidget(export_path);
		hl->addWidget(btn);
		vl->addLayout(hl);
	}

	export_btn = new QPushButton(this);
	export_btn->setText(tr("Export"));
	connect(export_btn, SIGNAL(clicked()), this, SLOT(render()));
	layout()->addWidget(export_btn);

	bar = new QProgressBar(this);
	connect(&renderer, SIGNAL(progressStart(int)),
	        bar, SLOT(setMaximum(int)));
	layout()->addWidget(bar);

	connect(&renderer, SIGNAL(progressRenderStart(int)),
	        this, SLOT(progressRenderStart(int)));
	connect(&renderer, SIGNAL(progressRenderTask(int)),
	        this, SLOT(progressRenderTask(int)));

	tasks = new QTreeView(this);
	tasks->setModel(model);
	tasks->setHeaderHidden(true);
	tasks->setRootIsDecorated(false);
	tasks->header()->resizeSection(0, 24);
	delegate = new ProgressDelegate(project, this);
	tasks->setItemDelegate(delegate);

	connect(&renderer, SIGNAL(progressStart(int)),
	        this, SLOT(progressStart(int)));
	connect(&renderer, SIGNAL(progressFinished(int)),
	        this, SLOT(progressFinished(int)));
	layout()->addWidget(tasks);

	connect(&renderer, SIGNAL(progressTask(QString)),
	        this, SLOT(progressTask(QString)));
	connect(&renderer, SIGNAL(progressRenderFinished(int)),
	        this, SLOT(progressRenderFinished(int)));
}

void RenderDialog::render()
{
	bar->setValue(1);
	renderer.render();
}

void RenderDialog::progressStart(int total)
{
	export_btn->setEnabled(false);
}

void RenderDialog::progressTask(QString text)
{
	//tasks->addItem(text);
}

void RenderDialog::progressRenderStart(int tasktotal)
{
	auto task = bar->value();

	auto instrument_ids = project.getInstrumentList();
	auto instrument_id = instrument_ids.begin() + (task - 1);
	delegate->setActiveTask(*instrument_id);
	model->setActiveTask(*instrument_id);

	delegate->setProgressMaximum(tasktotal);
	model->refresh();
}

void RenderDialog::progressRenderTask(int subtask)
{
	delegate->setProgress(subtask);
	model->refresh();
}

void RenderDialog::progressRenderFinished(int success)
{
	auto task = bar->value();

	// Search past the last instriment id to mark all tasks as finished
	auto instrument_ids = project.getInstrumentList();
	auto instrument_id = instrument_ids.begin() + task;
	delegate->setActiveTask(*instrument_id); // note this id is invalid
	model->setActiveTask(*instrument_id); // note this id is invalid

	bar->setValue(task + 1);
	model->refresh();
}

void RenderDialog::progressFinished(int success)
{
	export_btn->setEnabled(true);
}

void RenderDialog::chooseExportPath()
{
	QString path =
		QFileDialog::getExistingDirectory(
			this, tr("Choose export directory."),
			project.getExportPath(), QFileDialog::ShowDirsOnly);

	if(path != "")
	{
		export_path->setText(path);
		project.setExportPath(path);
	}
}