2009-07-23 13:11:51 -04:00
|
|
|
/****************************************************************
|
|
|
|
* RetroShare is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 - 2009 RetroShare Team
|
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, write to the Free Software
|
2009-08-19 18:15:16 -04:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2009-07-23 13:11:51 -04:00
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#include "SoundPage.h"
|
2010-05-11 16:02:52 -04:00
|
|
|
#include "rsharesettings.h"
|
2012-01-22 17:58:23 -05:00
|
|
|
#include "util/misc.h"
|
2015-06-07 08:40:25 -04:00
|
|
|
#include "gui/SoundManager.h"
|
2012-10-21 17:38:55 -04:00
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
#define COLUMN_NAME 0
|
|
|
|
#define COLUMN_FILENAME 1
|
|
|
|
#define COLUMN_COUNT 2
|
|
|
|
#define COLUMN_DATA COLUMN_NAME
|
2009-07-23 13:11:51 -04:00
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
#define ROLE_EVENT Qt::UserRole
|
|
|
|
|
|
|
|
#define TYPE_GROUP 0
|
|
|
|
#define TYPE_ITEM 1
|
2009-07-23 13:11:51 -04:00
|
|
|
|
|
|
|
/** Constructor */
|
2013-10-18 17:10:33 -04:00
|
|
|
SoundPage::SoundPage(QWidget * parent, Qt::WindowFlags flags)
|
2009-08-19 18:15:16 -04:00
|
|
|
: ConfigPage(parent, flags)
|
2009-07-23 13:11:51 -04:00
|
|
|
{
|
2012-01-22 17:58:23 -05:00
|
|
|
/* Invoke the Qt Designer generated object setup routine */
|
|
|
|
ui.setupUi(this);
|
|
|
|
|
|
|
|
connect(ui.eventTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(eventChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
|
|
|
|
connect(ui.filenameEdit, SIGNAL(textChanged(QString)), this, SLOT(filenameChanged(QString)));
|
2015-06-07 08:40:25 -04:00
|
|
|
connect(ui.defaultButton, SIGNAL(clicked()), this, SLOT(defaultButtonClicked()));
|
2012-01-22 17:58:23 -05:00
|
|
|
connect(ui.browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
|
|
|
|
connect(ui.playButton, SIGNAL(clicked()), this, SLOT(playButtonClicked()));
|
2017-01-30 15:38:06 -05:00
|
|
|
connect(ui.eventTreeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(updateSounds()));
|
2012-01-22 17:58:23 -05:00
|
|
|
|
|
|
|
ui.eventTreeWidget->setColumnCount(COLUMN_COUNT);
|
|
|
|
|
|
|
|
QTreeWidgetItem *headerItem = ui.eventTreeWidget->headerItem();
|
|
|
|
headerItem->setText(COLUMN_NAME, tr("Event"));
|
|
|
|
headerItem->setText(COLUMN_FILENAME, tr("Filename"));
|
|
|
|
|
2013-10-19 09:25:06 -04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
|
|
|
|
ui.eventTreeWidget->header()->setSectionResizeMode(QHeaderView::Fixed);
|
|
|
|
#else
|
2012-01-22 17:58:23 -05:00
|
|
|
ui.eventTreeWidget->header()->setResizeMode(QHeaderView::Fixed);
|
2013-10-19 09:25:06 -04:00
|
|
|
#endif
|
2012-01-22 17:58:23 -05:00
|
|
|
ui.eventTreeWidget->setTextElideMode(Qt::ElideMiddle);
|
|
|
|
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
|
|
|
|
2010-05-07 19:58:17 -04:00
|
|
|
SoundPage::~SoundPage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
QTreeWidgetItem *SoundPage::addGroup(const QString &name)
|
2009-07-23 13:11:51 -04:00
|
|
|
{
|
2012-10-21 17:38:55 -04:00
|
|
|
QTreeWidgetItem *item = NULL;
|
|
|
|
|
|
|
|
int count = ui.eventTreeWidget->topLevelItemCount();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
item = ui.eventTreeWidget->topLevelItem(i);
|
|
|
|
if (item->text(COLUMN_NAME) == name) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item = new QTreeWidgetItem(TYPE_GROUP);
|
2012-01-22 17:58:23 -05:00
|
|
|
item->setText(COLUMN_NAME, name);
|
|
|
|
ui.eventTreeWidget->insertTopLevelItem(ui.eventTreeWidget->topLevelItemCount(), item);
|
|
|
|
ui.eventTreeWidget->expandItem(item);
|
2009-07-23 13:11:51 -04:00
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
return item;
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 17:38:55 -04:00
|
|
|
QTreeWidgetItem *SoundPage::addItem(QTreeWidgetItem *groupItem, const QString &name, const QString &event)
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
|
|
|
QTreeWidgetItem *item = new QTreeWidgetItem(TYPE_ITEM);
|
|
|
|
item->setData(COLUMN_DATA, ROLE_EVENT, event);
|
|
|
|
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
2015-06-07 13:35:58 -04:00
|
|
|
item->setCheckState(COLUMN_NAME, SoundManager::eventEnabled(event) ? Qt::Checked : Qt::Unchecked);
|
2012-01-22 17:58:23 -05:00
|
|
|
item->setText(COLUMN_NAME, name);
|
2015-06-07 13:35:58 -04:00
|
|
|
item->setText(COLUMN_FILENAME, SoundManager::eventFilename(event));
|
2012-01-22 17:58:23 -05:00
|
|
|
groupItem->addChild(item);
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Saves the changes on this page */
|
2017-01-30 15:38:06 -05:00
|
|
|
void SoundPage::updateSounds()
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
|
|
|
QTreeWidgetItemIterator itemIterator(ui.eventTreeWidget);
|
|
|
|
QTreeWidgetItem *item = NULL;
|
|
|
|
while ((item = *itemIterator) != NULL) {
|
2014-10-21 18:33:02 -04:00
|
|
|
++itemIterator;
|
2012-01-22 17:58:23 -05:00
|
|
|
|
|
|
|
if (item->type() == TYPE_ITEM) {
|
2012-10-21 17:38:55 -04:00
|
|
|
const QString event = item->data(COLUMN_DATA, ROLE_EVENT).toString();
|
2015-06-07 13:35:58 -04:00
|
|
|
SoundManager::setEventEnabled(event, item->checkState(COLUMN_NAME) == Qt::Checked);
|
|
|
|
SoundManager::setEventFilename(event, item->text(COLUMN_FILENAME));
|
2012-01-22 17:58:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-23 13:11:51 -04:00
|
|
|
|
|
|
|
/** Loads the settings for this page */
|
2012-01-22 17:58:23 -05:00
|
|
|
void SoundPage::load()
|
2009-07-23 13:11:51 -04:00
|
|
|
{
|
2012-01-22 17:58:23 -05:00
|
|
|
ui.eventTreeWidget->clear();
|
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
/* add sound events */
|
|
|
|
SoundEvents events;
|
2015-06-07 13:35:58 -04:00
|
|
|
SoundManager::soundEvents(events);
|
2012-10-23 16:54:07 -04:00
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
QString event;
|
|
|
|
foreach (event, events.mEventInfos.keys()) {
|
|
|
|
SoundEvents::SoundEventInfo &eventInfo = events.mEventInfos[event];
|
2012-10-21 17:38:55 -04:00
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
QTreeWidgetItem *groupItem = addGroup(eventInfo.mGroupName);
|
|
|
|
addItem(groupItem, eventInfo.mEventName, event);
|
2012-10-21 17:38:55 -04:00
|
|
|
}
|
2012-01-22 17:58:23 -05:00
|
|
|
|
|
|
|
ui.eventTreeWidget->resizeColumnToContents(COLUMN_NAME);
|
2015-06-07 08:40:25 -04:00
|
|
|
ui.eventTreeWidget->sortByColumn(COLUMN_NAME, Qt::AscendingOrder);
|
2012-01-22 17:58:23 -05:00
|
|
|
|
|
|
|
eventChanged(NULL, NULL);
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
|
|
|
|
2012-01-22 18:29:11 -05:00
|
|
|
void SoundPage::eventChanged(QTreeWidgetItem *current, QTreeWidgetItem */*previous*/)
|
2009-07-23 13:11:51 -04:00
|
|
|
{
|
2012-01-22 17:58:23 -05:00
|
|
|
if (!current || current->type() != TYPE_ITEM) {
|
|
|
|
ui.eventGroup->setEnabled(false);
|
|
|
|
ui.eventName->clear();
|
|
|
|
ui.filenameEdit->clear();
|
|
|
|
ui.playButton->setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2009-07-23 13:11:51 -04:00
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
ui.eventGroup->setEnabled(true);
|
|
|
|
ui.filenameEdit->setText(current->text(COLUMN_FILENAME));
|
|
|
|
|
|
|
|
QString eventName;
|
|
|
|
if (current->parent()) {
|
|
|
|
eventName = current->parent()->text(COLUMN_NAME) + ": ";
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
2012-01-22 17:58:23 -05:00
|
|
|
eventName += current->text(COLUMN_NAME);
|
|
|
|
ui.eventName->setText(eventName);
|
2015-06-07 08:40:25 -04:00
|
|
|
|
|
|
|
QString event = current->data(COLUMN_DATA, ROLE_EVENT).toString();
|
2015-06-07 13:35:58 -04:00
|
|
|
ui.defaultButton->setDisabled(SoundManager::defaultFilename(event, true).isEmpty());
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
void SoundPage::filenameChanged(QString filename)
|
2009-07-23 13:11:51 -04:00
|
|
|
{
|
2012-01-22 17:58:23 -05:00
|
|
|
ui.playButton->setEnabled(!filename.isEmpty());
|
2009-07-23 13:11:51 -04:00
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
QTreeWidgetItem *item = ui.eventTreeWidget->currentItem();
|
|
|
|
if (item) {
|
|
|
|
item->setText(COLUMN_FILENAME, filename);
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
|
|
|
}
|
2012-01-22 17:58:23 -05:00
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
void SoundPage::defaultButtonClicked()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem *item = ui.eventTreeWidget->currentItem();
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString event = item->data(COLUMN_DATA, ROLE_EVENT).toString();
|
2015-06-07 13:35:58 -04:00
|
|
|
ui.filenameEdit->setText(SoundManager::defaultFilename(event, true));
|
2015-06-07 08:40:25 -04:00
|
|
|
}
|
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
void SoundPage::browseButtonClicked()
|
2009-07-23 13:11:51 -04:00
|
|
|
{
|
2012-01-22 17:58:23 -05:00
|
|
|
QString filename;
|
|
|
|
if (!misc::getOpenFileName(this, RshareSettings::LASTDIR_SOUNDS, tr("Open File"), "wav (*.wav)", filename)) {
|
|
|
|
return;
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
2015-06-07 08:40:25 -04:00
|
|
|
ui.filenameEdit->setText(SoundManager::convertFilename(filename));
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
2012-01-22 17:58:23 -05:00
|
|
|
|
|
|
|
void SoundPage::playButtonClicked()
|
2009-07-23 13:11:51 -04:00
|
|
|
{
|
2012-01-22 17:58:23 -05:00
|
|
|
QTreeWidgetItem *item = ui.eventTreeWidget->currentItem();
|
|
|
|
if (!item) {
|
|
|
|
return;
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|
2009-08-19 18:15:16 -04:00
|
|
|
|
2012-01-22 17:58:23 -05:00
|
|
|
QString filename = item->text(COLUMN_FILENAME);
|
2015-06-07 13:35:58 -04:00
|
|
|
SoundManager::playFile(filename);
|
2009-07-23 13:11:51 -04:00
|
|
|
}
|