|
| 1 | +// SPDX-FileCopyrightText: Team OpenVPI |
| 2 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 3 | + |
| 4 | +#include "AudioFileDropAddOn.h" |
| 5 | + |
| 6 | +#include <memory> |
| 7 | +#include <utility> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include <QDir> |
| 11 | +#include <QFileInfo> |
| 12 | +#include <QLoggingCategory> |
| 13 | +#include <QMetaObject> |
| 14 | +#include <QScopeGuard> |
| 15 | +#include <QSet> |
| 16 | +#include <QStringList> |
| 17 | + |
| 18 | +#include <CoreApi/objectpool.h> |
| 19 | + |
| 20 | +#include <opendspx/track.h> |
| 21 | + |
| 22 | +#include <SVSCraftCore/SVSCraftNamespace.h> |
| 23 | + |
| 24 | +#include <TalcsFormat/FormatManager.h> |
| 25 | + |
| 26 | +#include <dspxmodelORM/AudioClip.h> |
| 27 | +#include <dspxmodelORM/Model.h> |
| 28 | +#include <dspxmodelORM/Track.h> |
| 29 | +#include <dspxmodelORM/TrackList.h> |
| 30 | +#include <dspxmodelSelectionModel/SelectionModel.h> |
| 31 | + |
| 32 | +#include <coreplugin/CoreInterface.h> |
| 33 | +#include <coreplugin/DspxDocument.h> |
| 34 | +#include <coreplugin/ProjectDocumentContext.h> |
| 35 | +#include <coreplugin/ProjectWindowInterface.h> |
| 36 | +#include <coreplugin/TrackColorSchema.h> |
| 37 | + |
| 38 | +#include <transactional/TransactionController.h> |
| 39 | + |
| 40 | +#include <audio/AudioClipImporter.h> |
| 41 | +#include <audio/GlobalAudioContext.h> |
| 42 | + |
| 43 | +namespace AudioVisualizer::Internal { |
| 44 | + |
| 45 | + Q_STATIC_LOGGING_CATEGORY(lcAudioFileDropAddOn, "diffscope.audiovisualizer.audiofiledropaddon") |
| 46 | + |
| 47 | + AudioFileDropAddOnAttachedType::AudioFileDropAddOnAttachedType(QObject *parent) : QObject(parent) { |
| 48 | + if (auto window = qobject_cast<Core::ProjectWindowInterface *>(parent)) { |
| 49 | + connect(window, &Core::ObjectPool::objectAdded, this, [this](const QString &, QObject *object) { |
| 50 | + if (qobject_cast<AudioFileDropAddOn *>(object)) { |
| 51 | + Q_EMIT addOnChanged(); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + AudioFileDropAddOn *AudioFileDropAddOnAttachedType::addOn() const { |
| 58 | + const auto window = qobject_cast<Core::ProjectWindowInterface *>(parent()); |
| 59 | + return window ? window->getFirstObject<AudioFileDropAddOn>() : nullptr; |
| 60 | + } |
| 61 | + |
| 62 | + AudioFileDropAddOn::AudioFileDropAddOn(QObject *parent) : WindowInterfaceAddOn(parent) { |
| 63 | + } |
| 64 | + |
| 65 | + void AudioFileDropAddOn::initialize() { |
| 66 | + windowHandle()->addObject(this); |
| 67 | + } |
| 68 | + |
| 69 | + void AudioFileDropAddOn::extensionsInitialized() { |
| 70 | + } |
| 71 | + |
| 72 | + AudioFileDropAddOnAttachedType *AudioFileDropAddOn::qmlAttachedProperties(QObject *object) { |
| 73 | + return new AudioFileDropAddOnAttachedType(object); |
| 74 | + } |
| 75 | + |
| 76 | + bool AudioFileDropAddOn::isImporting() const { |
| 77 | + return m_importing; |
| 78 | + } |
| 79 | + |
| 80 | + bool AudioFileDropAddOn::acceptsUrls(const QList<QUrl> &urls) const { |
| 81 | + if (m_importing || urls.isEmpty()) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + QSet<QString> extensions; |
| 85 | + for (const auto &extension : Audio::GlobalAudioContext::formatManager()->extensionHints()) { |
| 86 | + extensions.insert(extension.toLower()); |
| 87 | + } |
| 88 | + for (const auto &url : urls) { |
| 89 | + if (!url.isValid() || !url.isLocalFile() || !extensions.contains(QFileInfo(url.toLocalFile()).suffix().toLower())) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + bool AudioFileDropAddOn::importFiles(const QList<QUrl> &urls, dspx::Track *firstTrack, int position) { |
| 97 | + if (!acceptsUrls(urls)) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + const auto window = windowHandle()->cast<Core::ProjectWindowInterface>(); |
| 101 | + QPointer<Core::DspxDocument> document = window->projectDocumentContext()->document(); |
| 102 | + if (!document || document->transactionController()->isExclusiveToTransaction() || (firstTrack && firstTrack->trackList() != document->model()->tracks())) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + m_importing = true; |
| 106 | + Q_EMIT importingChanged(); |
| 107 | + QMetaObject::invokeMethod(this, [this, urls, document, firstTrack = QPointer<dspx::Track>(firstTrack), hadFirstTrack = firstTrack != nullptr, position] { |
| 108 | + importFilesNow(urls, document, firstTrack, hadFirstTrack, qMax(0, position)); |
| 109 | + }, Qt::QueuedConnection); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + void AudioFileDropAddOn::importFilesNow(const QList<QUrl> &urls, QPointer<Core::DspxDocument> document, QPointer<dspx::Track> firstTrack, bool hadFirstTrack, int position) { |
| 114 | + QPointer<AudioFileDropAddOn> guard(this); |
| 115 | + const auto resetImporting = qScopeGuard([guard] { |
| 116 | + if (guard) { |
| 117 | + guard->m_importing = false; |
| 118 | + Q_EMIT guard->importingChanged(); |
| 119 | + } |
| 120 | + }); |
| 121 | + const QPointer<Core::ProjectWindowInterface> window = windowHandle()->cast<Core::ProjectWindowInterface>(); |
| 122 | + if (!document || window->projectDocumentContext()->document() != document || (hadFirstTrack && !firstTrack)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + const auto model = document->model(); |
| 126 | + const auto trackList = model->tracks(); |
| 127 | + QList<QPointer<dspx::Track>> targetTracks; |
| 128 | + if (firstTrack) { |
| 129 | + const auto tracks = trackList->items(); |
| 130 | + const auto index = tracks.indexOf(firstTrack.data()); |
| 131 | + if (index < 0) { |
| 132 | + return; |
| 133 | + } |
| 134 | + for (auto i = index; i < tracks.size(); ++i) { |
| 135 | + targetTracks.append(tracks.at(i)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + std::vector<std::unique_ptr<Audio::AudioClipImporter>> importers; |
| 140 | + QStringList failedFiles; |
| 141 | + qCDebug(lcAudioFileDropAddOn) << "Preparing dropped audio files" << urls.size(); |
| 142 | + for (const auto &url : urls) { |
| 143 | + auto importer = std::make_unique<Audio::AudioClipImporter>(window); |
| 144 | + const auto opened = importer->openFile(url.toLocalFile()); |
| 145 | + if (!guard || !window || !document || window->projectDocumentContext()->document() != document) { |
| 146 | + return; |
| 147 | + } |
| 148 | + if (opened) { |
| 149 | + importers.push_back(std::move(importer)); |
| 150 | + } else { |
| 151 | + failedFiles.append(QDir::toNativeSeparators(url.toLocalFile())); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (!importers.empty()) { |
| 156 | + for (const auto &track : targetTracks) { |
| 157 | + if (!track || track->trackList() != trackList) { |
| 158 | + return; |
| 159 | + } |
| 160 | + } |
| 161 | + const auto transaction = document->transactionController()->beginTransaction(); |
| 162 | + if (transaction == Core::TransactionController::TransactionId::Invalid) { |
| 163 | + window->sendNotification(SVS::SVSCraft::Warning, tr("Audio import failed"), tr("Audio files cannot be imported while another edit is active.")); |
| 164 | + return; |
| 165 | + } |
| 166 | + QList<dspx::AudioClip *> clips; |
| 167 | + bool succeeded = true; |
| 168 | + qCDebug(lcAudioFileDropAddOn) << "Inserting dropped audio files" << importers.size() << "at" << position; |
| 169 | + for (std::size_t i = 0; i < importers.size(); ++i) { |
| 170 | + auto &importer = importers[i]; |
| 171 | + const auto name = QFileInfo(importer->fileName()).completeBaseName(); |
| 172 | + dspx::Track *track; |
| 173 | + if (i < static_cast<std::size_t>(targetTracks.size())) { |
| 174 | + track = targetTracks.at(static_cast<qsizetype>(i)); |
| 175 | + } else { |
| 176 | + track = model->createTrack(); |
| 177 | + track->fromOpenDSPX(opendspx::Track{}); |
| 178 | + track->setName(name); |
| 179 | + const auto colors = Core::CoreInterface::trackColorSchema()->colors(); |
| 180 | + if (!colors.isEmpty()) { |
| 181 | + const auto index = trackList->size() % colors.size(); |
| 182 | + track->setColorId(index % 2 ? (colors.size() + index) / 2 : index / 2); |
| 183 | + } |
| 184 | + if (!trackList->insertItem(trackList->size(), track)) { |
| 185 | + model->destroyItem(track); |
| 186 | + succeeded = false; |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + auto clip = importer->insertClip(track, position, name); |
| 191 | + if (!clip) { |
| 192 | + succeeded = false; |
| 193 | + break; |
| 194 | + } |
| 195 | + clips.append(clip); |
| 196 | + } |
| 197 | + if (!succeeded) { |
| 198 | + document->transactionController()->abortTransaction(transaction); |
| 199 | + qCWarning(lcAudioFileDropAddOn) << "Failed to insert dropped audio clips"; |
| 200 | + window->sendNotification(SVS::SVSCraft::Critical, tr("Audio import failed"), tr("The audio clips could not be inserted.")); |
| 201 | + return; |
| 202 | + } |
| 203 | + document->transactionController()->commitTransaction(transaction, tr("Importing audio files")); |
| 204 | + qCDebug(lcAudioFileDropAddOn) << "Committed dropped audio clips" << clips.size(); |
| 205 | + bool first = true; |
| 206 | + for (auto clip : std::as_const(clips)) { |
| 207 | + auto command = dspx::SelectionModel::Select | dspx::SelectionModel::SetCurrentItem; |
| 208 | + if (first) { |
| 209 | + command |= dspx::SelectionModel::ClearPreviousSelection; |
| 210 | + first = false; |
| 211 | + } |
| 212 | + document->selectionModel()->select(clip, command); |
| 213 | + } |
| 214 | + } |
| 215 | + if (!failedFiles.isEmpty()) { |
| 216 | + window->sendNotification(SVS::SVSCraft::Warning, tr("Audio files skipped"), tr("The following audio files could not be imported:\n%1").arg(failedFiles.join('\n'))); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | +} |
| 221 | + |
| 222 | +#include "moc_AudioFileDropAddOn.cpp" |
0 commit comments