summaryrefslogtreecommitdiff
path: root/src/mainwindow.cc
blob: 92a91e75c334edf3561874895f1c8a5b14485e2e (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            mainwindow.cc
 *
 *  Tue Nov 10 10:21:04 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 "mainwindow.h"

#include <iostream>

#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QStatusBar>
#include <QApplication>
#include <QMenuBar>
#include <QFileDialog>
#include <QToolBar>
#include <QToolButton>
#include <QMessageBox>

#include "settings.h"
#include "projectdialog.h"
#include "instrumentdialog.h"
#include "projectserialiser.h"
#include "instrumentwidget.h"

#define MAXVAL 10000000L

MainWindow::MainWindow(Settings& settings)
	: settings(settings)
{
	tab_widget = new QTabWidget();
	tab_widget->setTabsClosable(true);
	tab_widget->setMovable(true);
	connect(tab_widget, SIGNAL(tabCloseRequested(int)),
	        this, SLOT(closeTab(int)));

	setCentralWidget(tab_widget);

	QMenu* fileMenu = menuBar()->addMenu(tr("&File"));

	QAction* act_new_project = new QAction(tr("&New Project"), this);
	fileMenu->addAction(act_new_project);
	connect(act_new_project, SIGNAL(triggered()), this, SLOT(newProject()));

	QAction* act_load_project = new QAction(tr("&Load Project..."), this);
	fileMenu->addAction(act_load_project);
	connect(act_load_project, SIGNAL(triggered()), this, SLOT(loadProject()));

	QAction* act_save_project = new QAction(tr("&Save Project"), this);
	fileMenu->addAction(act_save_project);
	connect(act_save_project, SIGNAL(triggered()), this, SLOT(saveProject()));

	QAction* act_save_project_as = new QAction(tr("Save Project As..."), this);
	fileMenu->addAction(act_save_project_as);
	connect(act_save_project_as, SIGNAL(triggered()),
	        this, SLOT(saveProjectAs()));

	QAction* act_quit = new QAction(tr("&Quit"), this);
	fileMenu->addAction(act_quit);
	connect(act_quit, SIGNAL(triggered()), this, SLOT(close()));

	instruments_dock = new QDockWidget(tr("Instruments:"), this);
	instruments_dock->setObjectName("instruments_dock");
	instruments_dock->setAllowedAreas(Qt::LeftDockWidgetArea);
	instruments_dock->setFeatures(QDockWidget::DockWidgetMovable);
	{
		auto w = new QWidget();
		auto l = new QVBoxLayout();
		w->setLayout(l);
		auto tools = new QToolBar();
		auto add = new QToolButton();
		add->setIcon(QPixmap(":icons/add_instrument.png"));
		connect(add, SIGNAL(clicked()), this, SLOT(addInstrument()));
		auto rem = new QToolButton();
		rem->setIcon(QPixmap(":icons/remove_instrument.png"));
		connect(rem, SIGNAL(clicked()), this, SLOT(removeInstrument()));
		auto edt = new QToolButton();
		edt->setIcon(QPixmap(":icons/edit_instrument.png"));
		connect(edt, SIGNAL(clicked()), this, SLOT(editInstrument()));
		tools->addWidget(add);
		tools->addWidget(rem);
		tools->addWidget(edt);
		instrument_list = new QListWidget();
		connect(instrument_list, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
		        this, SLOT(instrumentDoubleClicked(QListWidgetItem*)));

		l->addWidget(tools);
		l->addWidget(instrument_list);

		instruments_dock->setWidget(w);
	}
	addDockWidget(Qt::LeftDockWidgetArea, instruments_dock);

	channels_dock = new QDockWidget(tr("Channels:"), this);
	channels_dock->setObjectName("channels_dock");
	channels_dock->setAllowedAreas(Qt::LeftDockWidgetArea);
	channels_dock->setFeatures(QDockWidget::DockWidgetMovable);
	{
		auto w = new QWidget();
		auto l = new QVBoxLayout();
		w->setLayout(l);
		auto tools = new QToolBar();
		auto add = new QToolButton();
		add->setIcon(QPixmap(":icons/add_channel.png"));
		connect(add, SIGNAL(clicked()), this, SLOT(addChannel()));
		auto rem = new QToolButton();
		rem->setIcon(QPixmap(":icons/remove_channel.png"));
		connect(rem, SIGNAL(clicked()), this, SLOT(removeChannel()));
		tools->addWidget(add);
		tools->addWidget(rem);
		channel_list = new QListWidget();
		connect(channel_list, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
		        this, SLOT(channelDoubleClicked(QListWidgetItem*)));
		channel_list->addItems({"AmbL", "AmbR", "Kdrum_back", "Kdrum_front",
		                        "Hihat", "OHL", "OHR", "Ride","Snare_bottom",
		                        "Snare_top", "Tom1", "Tom2", "Tom3"});
		for(int i = 0; i < channel_list->count(); ++i)
		{
			channel_list->item(i)->setIcon(QPixmap(":icons/channel.png"));
		}

		l->addWidget(tools);
		l->addWidget(channel_list);
		channels_dock->setWidget(w);
		addDockWidget(Qt::LeftDockWidgetArea, channels_dock);
	}

	loadSettings();

	statusBar()->showMessage(tr("Ready"));

	connect(&project, SIGNAL(projectChanged()), this, SLOT(projectChanged()));

	updateWindowTitle();
}

MainWindow::~MainWindow()
{
}

void MainWindow::addInstrument()
{
	auto id = project.createInstrument();
	auto& instrument = project.getInstrument(id);

	InstrumentDialog dlg(this, instrument);
	dlg.show();
	dlg.exec();

	auto item = new QListWidgetItem();
	item->setIcon(QPixmap(":icons/instrument.png"));
	item->setText(instrument.getInstrumentName());
	item->setData(Qt::UserRole, id);
	instrument_list->addItem(item);
}

void MainWindow::editInstrument()
{
	auto items = instrument_list->selectedItems();
	for(auto item : items)
	{
		auto id = item->data(Qt::UserRole).toInt();

		auto& instrument = project.getInstrument(id);

		InstrumentDialog dlg(this, instrument);
		dlg.show();
		dlg.exec();

		item->setText(instrument.getInstrumentName());

		// Also update tab name if open
		for(int i = 0; i < tab_widget->count(); ++i)
		{
			if(tab_widget->widget(i)->property("id").toInt() == id)
			{
				tab_widget->setTabText(i, instrument.getInstrumentName());
			}
		}
	}
}

void MainWindow::removeInstrument()
{
	int ret =
		QMessageBox::question(this, tr("Delete Instrument"),
		                      tr("Are you sure you want to delete the selected "
		                         "instrument?"),
		                      QMessageBox::Yes | QMessageBox::No |
		                      QMessageBox::Cancel);
	if(ret != QMessageBox::Yes)
	{
		return;
	}

	auto items = instrument_list->selectedItems();
	for(auto item : items)
	{
		auto id = item->data(Qt::UserRole).toInt();

		// Also close tab if open.
		for(int i = 0; i < tab_widget->count(); ++i)
		{
			if(tab_widget->widget(i)->property("id").toInt() == id)
			{
				tab_widget->removeTab(i);
			}
		}

		project.deleteInstrument(id);
		delete item;
	}
}

void MainWindow::instrumentDoubleClicked(QListWidgetItem *item)
{
	int id = item->data(Qt::UserRole).toInt();
	auto& instr = project.getInstrument(id);

	for(int i = 0; i < tab_widget->count(); ++i)
	{
		if(tab_widget->widget(i)->property("id").toInt() == id)
		{
			tab_widget->setCurrentIndex(i);
			return;
		}
	}

	// Tab wasn't open already. Create it.
	tab_widget->addTab(new InstrumentWidget(settings, instr),
	                   QPixmap(":icons/instrument.png"),
	                   instr.getInstrumentName());
	// Make new tab active
	tab_widget->setCurrentIndex(tab_widget->count() - 1);
}

void MainWindow::addChannel()
{
	auto item = new QListWidgetItem();
	item->setIcon(QPixmap(":icons/channel.png"));
	item->setText("New Channel");
	//item->setData(Qt::UserRole, id);
	channel_list->addItem(item);
}

void MainWindow::removeChannel()
{
	auto items = channel_list->selectedItems();
	for(auto item : items)
	{
		delete item;
	}
}

void MainWindow::channelDoubleClicked(QListWidgetItem *item)
{
}

void MainWindow::updateWindowTitle()
{
	auto project_string = project.getProjectName();

	if(project_string == "")
	{
		project_string = tr("[Untitled Project]");
	}

	if(project.getProjectFile() != "")
	{
		project_string += " (" + QFileInfo(project.getProjectFile()).fileName() + ")";
	}

	if(project_dirty)
	{
		project_string += "*";
	}

	setWindowTitle("DGEdit - " + project_string);
}

void MainWindow::closeEvent(QCloseEvent*)
{
	saveSettings();
	QApplication::quit();
}

void MainWindow::loadSettings()
{
	QByteArray state;
	QByteArray geometry;
	settings.loadGeometry(state, geometry);
	restoreGeometry(geometry);
	restoreState(state);
	// TODO: lineed_exportp->setText(settings.loadExportPath());
}

void MainWindow::saveSettings()
{
	settings.saveGeometry(saveState(), saveGeometry());
	// TODO: settings.saveExportPath(lineed_exportp->text());
}

void MainWindow::newProject()
{
	ProjectDialog dlg(this, project);
	dlg.show();
	dlg.exec();
}

void MainWindow::loadProject()
{
	QString filename =
		QFileDialog::getOpenFileName(this,
		                             tr("Load DGEdit Project"),
		                             "",
		                             tr("DGEdit Project Files (*.dgedit)"));
	if(filename == "")
	{
		// User clicked cancel
		return;
	}

	QFile file(filename);
	if(!file.open(QIODevice::ReadOnly))
	{
		return;
	}

	QString xml(file.readAll());
	file.close();

	ProjectSerialiser ser;
	ser.deserialise(xml, project);

	project.setProjectFile(filename);

	project_dirty = false;
	updateWindowTitle();

	instrument_list->clear();
	auto instrument_ids = project.getInstrumentList();
	for(auto id : instrument_ids)
	{
		auto& instrument = project.getInstrument(id);
		auto item = new QListWidgetItem();
		item->setIcon(QPixmap(":icons/instrument.png"));
		item->setText(instrument.getInstrumentName());
		item->setData(Qt::UserRole, id);
		instrument_list->addItem(item);
	}

	statusBar()->showMessage(tr("Loaded"));
}

void MainWindow::saveProject()
{
	QString filename = project.getProjectFile();
	if(filename == "")
	{
		saveProjectAs();
		return;
	}

	QFile file(filename);
	if(!file.open(QIODevice::WriteOnly))
	{
		return;
	}

	ProjectSerialiser ser;
	auto xml = ser.serialise(project);

	file.write(xml.toUtf8());

	project_dirty = false;
	updateWindowTitle();

	statusBar()->showMessage(tr("Saved"));
}

void MainWindow::saveProjectAs()
{
	QString filename
		= QFileDialog::getSaveFileName(this, tr("Save DGEdit Project"),
		                               "",
		                               tr("DGEdit Project Files (*.dgedit)"));

	if(filename == "")
	{
		// User clicked cancel
		return;
	}

	project.setProjectFile(filename);

	saveProject();
}

void MainWindow::projectChanged()
{
	project_dirty = true;
	updateWindowTitle();
}

void MainWindow::closeTab(int tab)
{
	tab_widget->removeTab(tab);
}