| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <QConnectionsWidget.h>
- #include <ACEEnums.h>
- #include <AudioControl.h>
- #include <AudioControlsEditorPlugin.h>
- #include <IAudioSystemControl.h>
- #include <IAudioSystemEditor.h>
- #include <ImplementationManager.h>
- #include <QDropEvent>
- #include <QEvent>
- #include <QMimeData>
- #include <QMessageBox>
- #include <QMenu>
- namespace AudioControls
- {
- //-------------------------------------------------------------------------------------------//
- QConnectionsWidget::QConnectionsWidget(QWidget* parent)
- : QWidget(parent)
- , m_control(nullptr)
- , m_notFoundColor(QColor(0xf3, 0x81, 0x1d))
- , m_localizedColor(QColor(0x42, 0x85, 0xf4))
- {
- setupUi(this);
- m_connectionPropertiesFrame->setHidden(true);
- m_connectionList->viewport()->installEventFilter(this);
- m_connectionList->installEventFilter(this);
- connect(m_connectionList, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ShowConnectionContextMenu(const QPoint&)));
- connect(m_connectionList, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedConnectionChanged()));
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::CurrentConnectionModified()
- {
- if (m_control)
- {
- m_control->SignalControlModified();
- }
- }
- //-------------------------------------------------------------------------------------------//
- bool QConnectionsWidget::eventFilter(QObject* object, QEvent* event)
- {
- IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl();
- if (audioSystemImpl && m_control && event->type() == QEvent::Drop)
- {
- QDropEvent* dropEvent = static_cast<QDropEvent*>(event);
- const QMimeData* mimeData = dropEvent->mimeData();
- QString format = "application/x-qabstractitemmodeldatalist";
- if (mimeData->hasFormat(format))
- {
- QByteArray encoded = mimeData->data(format);
- QDataStream stream(&encoded, QIODevice::ReadOnly);
- while (!stream.atEnd())
- {
- int row, col;
- QMap<int, QVariant> roleDataMap;
- stream >> row >> col >> roleDataMap;
- if (!roleDataMap.isEmpty())
- {
- if (IAudioSystemControl* middlewareControl = audioSystemImpl->GetControl(roleDataMap[eMDR_ID].toUInt()))
- {
- if (audioSystemImpl->GetCompatibleTypes(m_control->GetType()) & middlewareControl->GetType())
- {
- MakeConnectionTo(middlewareControl);
- }
- }
- }
- }
- }
- return true;
- }
- else if (event->type() == QEvent::KeyPress)
- {
- QKeyEvent* dropEvent = static_cast<QKeyEvent*>(event);
- if (dropEvent && dropEvent->key() == Qt::Key_Delete && object == m_connectionList)
- {
- RemoveSelectedConnection();
- return true;
- }
- }
- return QWidget::eventFilter(object, event);
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::ShowConnectionContextMenu(const QPoint& pos)
- {
- QMenu contextMenu(tr("Context menu"), this);
- contextMenu.addAction(tr("Remove Connection"), this, SLOT(RemoveSelectedConnection()));
- contextMenu.exec(m_connectionList->mapToGlobal(pos));
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::SelectedConnectionChanged()
- {
- TConnectionPtr connection;
- EACEControlType controlType = AudioControls::eACET_NUM_TYPES;
- if (m_control)
- {
- QList<QListWidgetItem*> selected = m_connectionList->selectedItems();
- if (selected.length() == 1)
- {
- QListWidgetItem* currentItem = selected[0];
- if (currentItem)
- {
- const CID externalId = currentItem->data(eMDR_ID).toInt();
- connection = m_control->GetConnection(externalId);
- controlType = m_control->GetType();
- }
- }
- }
- if (m_connectionPropertiesWidget)
- {
- delete m_connectionPropertiesWidget;
- m_connectionPropertiesWidget = nullptr;
- }
- if (connection && connection->HasProperties())
- {
- if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl())
- {
- m_connectionPropertiesWidget = audioSystemImpl->CreateConnectionPropertiesWidget(connection, controlType);
- if (m_connectionPropertiesWidget)
- {
- m_connectionPropertiesWidget->setParent(m_connectionPropertiesFrame);
- m_connectionPropertiesLayout->addWidget(m_connectionPropertiesWidget);
- bool widgetHasChangedSignal = m_connectionPropertiesWidget->metaObject()->indexOfSignal("PropertiesChanged()") != -1;
- AZ_Error(
- "Audio", widgetHasChangedSignal,
- "The widget created by IAudioSystemEditor::CreateConnectionPropertiesWidget() must have a \"PropertiesChanged()\" "
- "signal.");
- if (widgetHasChangedSignal)
- {
- connect(m_connectionPropertiesWidget, SIGNAL(PropertiesChanged()), this, SLOT(CurrentConnectionModified()));
- }
- }
- }
- }
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::MakeConnectionTo(IAudioSystemControl* middlewareControl)
- {
- IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl();
- if (m_control && middlewareControl && audioSystemImpl)
- {
- CUndo undo("Connected Audio Control to Audio System");
- TConnectionPtr connection = m_control->GetConnection(middlewareControl);
- if (connection)
- {
- // connection already exists, select it
- const int size = m_connectionList->count();
- for (int i = 0; i < size; ++i)
- {
- QListWidgetItem* listItem = m_connectionList->item(i);
- if (listItem && listItem->data(eMDR_ID).toInt() == static_cast<int>(middlewareControl->GetId()))
- {
- m_connectionList->clearSelection();
- listItem->setSelected(true);
- m_connectionList->setCurrentItem(listItem);
- m_connectionList->scrollToItem(listItem);
- break;
- }
- }
- }
- else
- {
- if (m_control->GetType() == EACEControlType::eACET_SWITCH_STATE)
- {
- if (!m_control->GetParent()->SwitchStateConnectionCheck(middlewareControl))
- {
- QMessageBox messageBox(this);
- messageBox.setStandardButtons(QMessageBox::Ok);
- messageBox.setDefaultButton(QMessageBox::Ok);
- messageBox.setWindowTitle("Audio Controls Editor");
- messageBox.setText("Not in the same switch group, connection failed.");
- if (messageBox.exec() == QMessageBox::Ok)
- {
- return;
- }
- }
- }
- connection = audioSystemImpl->CreateConnectionToControl(m_control->GetType(), middlewareControl);
- if (connection)
- {
- m_control->AddConnection(connection);
- }
- }
- }
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::RemoveSelectedConnection()
- {
- CUndo undo("Disconnected Audio Control from Audio System");
- if (m_control)
- {
- QMessageBox messageBox(this);
- messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
- messageBox.setDefaultButton(QMessageBox::Yes);
- messageBox.setWindowTitle("Audio Controls Editor");
- QList<QListWidgetItem*> selected = m_connectionList->selectedItems();
- const int size = selected.length();
- if (size > 0)
- {
- if (size == 1)
- {
- messageBox.setText("Are you sure you want to delete the connection between \"" + QString(m_control->GetName().c_str()) + "\" and \"" + selected[0]->text() + "\"?");
- }
- else
- {
- messageBox.setText("Are you sure you want to delete the " + QString::number(size) + " selected connections?");
- }
- if (messageBox.exec() == QMessageBox::Yes)
- {
- if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl())
- {
- AZStd::vector<IAudioSystemControl*> connectedMiddlewareControls;
- connectedMiddlewareControls.reserve(selected.size());
- for (int i = 0; i < size; ++i)
- {
- CID middlewareControlId = selected[i]->data(eMDR_ID).toInt();
- connectedMiddlewareControls.push_back(audioSystemImpl->GetControl(middlewareControlId));
- }
- for (int i = 0; i < size; ++i)
- {
- if (IAudioSystemControl* middlewareControl = connectedMiddlewareControls[i])
- {
- audioSystemImpl->ConnectionRemoved(middlewareControl);
- m_control->RemoveConnection(middlewareControl);
- }
- }
- }
- }
- }
- }
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::UpdateConnections()
- {
- m_connectionList->clear();
- IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl();
- if (audioSystemImpl && m_control)
- {
- m_connectionList->setEnabled(true);
- const size_t size = m_control->ConnectionCount();
- for (size_t i = 0; i < size; ++i)
- {
- TConnectionPtr connection = m_control->GetConnectionAt(i);
- if (connection)
- {
- if (IAudioSystemControl* middlewareControl = audioSystemImpl->GetControl(connection->GetID()))
- {
- CreateItemFromConnection(middlewareControl);
- }
- }
- }
- }
- else
- {
- m_connectionList->setEnabled(false);
- }
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::CreateItemFromConnection(IAudioSystemControl* middlewareControl)
- {
- if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl())
- {
- const TImplControlType type = middlewareControl->GetType();
- QIcon icon = QIcon(QString(audioSystemImpl->GetTypeIcon(type).data()));
- icon.addFile(QString(audioSystemImpl->GetTypeIconSelected(type).data()), QSize(), QIcon::Selected);
- QListWidgetItem* listItem = new QListWidgetItem(icon, QString(middlewareControl->GetName().c_str()));
- listItem->setData(eMDR_ID, middlewareControl->GetId());
- listItem->setData(eMDR_LOCALIZED, middlewareControl->IsLocalized());
- if (middlewareControl->IsPlaceholder())
- {
- listItem->setToolTip(tr("Control not found in currently loaded audio system project"));
- listItem->setForeground(m_notFoundColor);
- }
- else if (middlewareControl->IsLocalized())
- {
- listItem->setForeground(m_localizedColor);
- }
- m_connectionList->insertItem(0, listItem);
- }
- }
- //-------------------------------------------------------------------------------------------//
- void QConnectionsWidget::SetControl(CATLControl* control)
- {
- if (m_control != control)
- {
- m_control = control;
- m_connectionList->clear();
- UpdateConnections();
- }
- else if (m_control->ConnectionCount() != m_connectionList->count())
- {
- UpdateConnections();
- }
- }
- } // namespace AudioControls
- #include <Source/Editor/moc_QConnectionsWidget.cpp>
|