/* * 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 #include #include #include #include #include #include "GraphTabBar.h" #include #include #include #include namespace ScriptCanvasEditor { namespace Widget { //////////////// // GraphTabBar //////////////// GraphTabBar::GraphTabBar(QWidget* parent /*= nullptr*/) : AzQtComponents::TabBar(parent) { setTabsClosable(true); setMovable(true); setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); connect(this, &QTabBar::currentChanged, this, &GraphTabBar::currentChangedTab); setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &QTabBar::customContextMenuRequested, this, &GraphTabBar::OnContextMenu); } void GraphTabBar::AddGraphTab(ScriptCanvasEditor::SourceHandle assetId) { InsertGraphTab(count(), assetId); } int GraphTabBar::InsertGraphTab([[maybe_unused]] int tabIndex, [[maybe_unused]] ScriptCanvasEditor::SourceHandle assetId) { // #sc_editor_asset // if (!SelectTab(assetId)) // { // AZStd::shared_ptr memoryAsset; // AssetTrackerRequestBus::BroadcastResult(memoryAsset, &AssetTrackerRequests::GetAsset, assetId); // // if (memoryAsset) // { // ScriptCanvas::AssetDescription assetDescription = memoryAsset->GetAsset().Get()->GetAssetDescription(); // // QIcon tabIcon = QIcon(assetDescription.GetIconPathImpl()); // int newTabIndex = qobject_cast(parent())->insertTab(tabIndex, new QWidget(), tabIcon, ""); // // CanvasWidget* canvasWidget = memoryAsset->CreateView(this); // // canvasWidget->SetDefaultBorderColor(assetDescription.GetDisplayColorImpl()); // // AZStd::string tabName; // AzFramework::StringFunc::Path::GetFileName(memoryAsset->GetAbsolutePath().c_str(), tabName); // // ConfigureTab(newTabIndex, assetId, tabName); // // // new graphs will need to use their in-memory assetid which we'll need to update // // upon saving the asset // if (!memoryAsset->GetFileAssetId().IsValid()) // { // setTabData(newTabIndex, QVariant::fromValue(memoryAsset->GetId())); // } // // return newTabIndex; // } // } return -1; } bool GraphTabBar::SelectTab(ScriptCanvasEditor::SourceHandle assetId) { int tabIndex = FindTab(assetId); if (-1 != tabIndex) { setCurrentIndex(tabIndex); return true; } return false; } void GraphTabBar::ConfigureTab(int tabIndex, ScriptCanvasEditor::SourceHandle fileAssetId, const AZStd::string& tabName) { if (fileAssetId.IsValid()) { QVariant tabDataVariant = tabData(tabIndex); if (tabDataVariant.isValid()) { // #se_editor_asset // auto tabAssetId = tabDataVariant.value(); // MemoryAssetNotificationBus::MultiHandler::BusDisconnect(tabAssetId); } setTabData(tabIndex, QVariant::fromValue(fileAssetId)); // #se_editor_asset // MemoryAssetNotificationBus::MultiHandler::BusConnect(fileAssetId); } // #se_editor_asset Tracker::ScriptCanvasFileState fileState = Tracker::ScriptCanvasFileState::INVALID; // AssetTrackerRequestBus::BroadcastResult(fileState, &AssetTrackerRequests::GetFileState, fileAssetId); SetTabText(tabIndex, tabName.c_str(), fileState); } int GraphTabBar::FindTab(ScriptCanvasEditor::SourceHandle assetId) const { for (int tabIndex = 0; tabIndex < count(); ++tabIndex) { QVariant tabDataVariant = tabData(tabIndex); if (tabDataVariant.isValid()) { auto tabAssetId = tabDataVariant.value(); if (tabAssetId == assetId) { return tabIndex; } } } return -1; } ScriptCanvasEditor::SourceHandle GraphTabBar::FindAssetId(int tabIndex) { QVariant dataVariant = tabData(tabIndex); if (dataVariant.isValid()) { auto tabAssetId = dataVariant.value(); return tabAssetId; } return ScriptCanvasEditor::SourceHandle(); } void GraphTabBar::CloseTab(int index) { if (index >= 0 && index < count()) { QVariant tabdata = tabData(index); if (tabdata.isValid()) { auto tabAssetId = tabdata.value(); MemoryAssetNotificationBus::MultiHandler::BusDisconnect(tabAssetId); AssetTrackerRequestBus::Broadcast(&AssetTrackerRequests::ClearView, tabAssetId); } qobject_cast(parent())->removeTab(index); } } void GraphTabBar::CloseAllTabs() { for (int i = count() - 1; i >= 0; --i) { Q_EMIT TabCloseNoButton(i); } MemoryAssetNotificationBus::MultiHandler::BusDisconnect(); } void GraphTabBar::OnContextMenu(const QPoint& point) { QPoint screenPoint = mapToGlobal(point); int tabIndex = tabAt(point); bool hasValidTab = (tabIndex >= 0); bool isModified = false; QVariant tabdata = tabData(tabIndex); if (tabdata.isValid()) { auto tabAssetId = tabdata.value(); Tracker::ScriptCanvasFileState fileState; AssetTrackerRequestBus::BroadcastResult(fileState , &AssetTrackerRequests::GetFileState, tabAssetId); isModified = fileState == Tracker::ScriptCanvasFileState::NEW || fileState == Tracker::ScriptCanvasFileState::MODIFIED; } QMenu menu; QAction* saveAction = menu.addAction("Save"); saveAction->setEnabled(hasValidTab && isModified); QAction* closeAction = menu.addAction("Close"); closeAction->setEnabled(hasValidTab); QAction* closeAllAction = menu.addAction("Close All"); QAction* closeAllButThis = menu.addAction("Close All But This"); closeAllButThis->setEnabled(hasValidTab); menu.addSeparator(); QAction* fullPathAction = menu.addAction("Copy Source Path To Clipboard"); fullPathAction->setEnabled(hasValidTab); QAction* action = menu.exec(screenPoint); if (action) { if (action == saveAction) { if (tabIndex != currentIndex()) { m_signalSaveOnChangeTo = tabIndex; setCurrentIndex(tabIndex); } else { Q_EMIT SaveTab(tabIndex); } } else if (action == closeAction) { tabCloseRequested(tabIndex); } else if (action == closeAllAction) { Q_EMIT CloseAllTabsSignal(); } else if (action == closeAllButThis) { Q_EMIT CloseAllTabsButSignal(tabIndex); } else if (action == fullPathAction) { Q_EMIT CopyPathToClipboard(tabIndex); } } } void GraphTabBar::mouseReleaseEvent(QMouseEvent* event) { if (event->button() == Qt::MidButton) { int tabIndex = tabAt(event->localPos().toPoint()); if (tabIndex >= 0) { tabCloseRequested(tabIndex); return; } } AzQtComponents::TabBar::mouseReleaseEvent(event); } void GraphTabBar::OnFileStateChanged(Tracker::ScriptCanvasFileState ) { // #se_editor_asset // const AZ::Data::AssetId* fileAssetId = MemoryAssetNotificationBus::GetCurrentBusId(); // if (fileAssetId) // { // SetFileState((*fileAssetId), fileState); // // if (FindTab((*fileAssetId)) == currentIndex()) // { // Q_EMIT OnActiveFileStateChanged(); // } // } } void GraphTabBar::SetTabText(int tabIndex, const QString& path, Tracker::ScriptCanvasFileState fileState) { if (tabIndex >= 0 && tabIndex < count()) { const char* fileStateTag = ""; switch (fileState) { case Tracker::ScriptCanvasFileState::NEW: fileStateTag = "^"; break; case Tracker::ScriptCanvasFileState::SOURCE_REMOVED: case Tracker::ScriptCanvasFileState::MODIFIED: fileStateTag = "*"; break; default: break; } setTabText(tabIndex, QString("%1%2").arg(path).arg(fileStateTag)); } } void GraphTabBar::tabInserted(int index) { AzQtComponents::TabBar::tabInserted(index); Q_EMIT TabInserted(index); } void GraphTabBar::tabRemoved(int index) { AzQtComponents::TabBar::tabRemoved(index); Q_EMIT TabRemoved(index); } void GraphTabBar::currentChangedTab(int index) { if (index < 0) { return; } QVariant tabdata = tabData(index); if (!tabdata.isValid()) { return; } auto assetId = tabdata.value(); // #sc_editor_asset // ScriptCanvasEditor::GeneralRequestBus::Broadcast(&ScriptCanvasEditor::GeneralRequests::OnChangeActiveGraphTab, assetId); if (m_signalSaveOnChangeTo >= 0 && m_signalSaveOnChangeTo == index) { m_signalSaveOnChangeTo = -1; Q_EMIT SaveTab(m_signalSaveOnChangeTo); } } void GraphTabBar::SetFileState(ScriptCanvasEditor::SourceHandle, Tracker::ScriptCanvasFileState ) { // #se_editor_asset // int index = FindTab(assetId); // if (index >= 0 && index < count()) // { // QVariant tabdata = tabData(index); // if (tabdata.isValid()) // { // auto tabAssetId = tabdata.value(); // // AZStd::string tabName; // AssetTrackerRequestBus::BroadcastResult(tabName, &AssetTrackerRequests::GetTabName, tabAssetId); // SetTabText(index, tabName.c_str(), fileState); // } // } } #include } }