File indexing completed on 2024-05-19 04:56:04

0001 /**
0002  * \file frameeditorobject.cpp
0003  * IFrameEditor interface to QObject bridge.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 20 Sep 2014
0008  *
0009  * Copyright (C) 2014-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "frameeditorobject.h"
0028 #include "frameobjectmodel.h"
0029 #include "taggedfile.h"
0030 
0031 /**
0032  * Constructor.
0033  * @param parent parent object
0034  */
0035 FrameEditorObject::FrameEditorObject(QObject* parent) : QObject(parent),
0036   m_selectFrame(nullptr), m_editFrameTaggedFile(nullptr), m_frameObjectModel(nullptr),
0037   m_tagNr(Frame::Tag_2)
0038 {
0039 }
0040 
0041 /**
0042  * Let user edit a frame and then update the fields
0043  * when the edits are accepted.
0044  * frameEdited() is emitted when the edit dialog is closed with the edited
0045  * frame as a parameter if it was accepted.
0046  *
0047  * @param frame frame to edit
0048  * @param taggedFile tagged file where frame has to be set
0049  */
0050 void FrameEditorObject::editFrameOfTaggedFile(const Frame* frame,
0051                                                TaggedFile* taggedFile)
0052 {
0053   if (!frame || !taggedFile) {
0054     emit frameEdited(m_tagNr, nullptr);
0055     return;
0056   }
0057 
0058   m_editFrame = *frame;
0059   m_editFrameTaggedFile = taggedFile;
0060   if (!m_frameObjectModel) {
0061     m_frameObjectModel = new FrameObjectModel(this);
0062   }
0063   m_frameObjectModel->setFrame(m_editFrame);
0064   emit frameEditRequested(m_frameObjectModel);
0065 }
0066 
0067 /**
0068  * Called when the frame edit dialog is closed.
0069  *
0070  * @param frame frame object model, null if canceled
0071  *
0072  * @see frameEditRequested()
0073  */
0074 void FrameEditorObject::onFrameEditFinished(const FrameObjectModel* frame)
0075 {
0076   if (frame) {
0077     m_editFrame = frame->getFrame();
0078     if (m_editFrameTaggedFile->setFrame(m_tagNr, m_editFrame)) {
0079       m_editFrameTaggedFile->markTagChanged(m_tagNr,
0080                                             m_editFrame.getExtendedType());
0081     }
0082     emit frameEdited(m_tagNr, &m_editFrame);
0083   } else {
0084     emit frameEdited(m_tagNr, nullptr);
0085   }
0086 }
0087 
0088 /**
0089  * Let user select a frame type.
0090  * frameSelected() is emitted when the edit dialog is closed with the selected
0091  * frame as a parameter if a frame is selected.
0092  *
0093  * @param frame is filled with the selected frame
0094  * @param taggedFile tagged file for which frame has to be selected
0095  */
0096 void FrameEditorObject::selectFrame(Frame* frame, const TaggedFile* taggedFile)
0097 {
0098   if (taggedFile && frame) {
0099     QStringList frameNames = taggedFile->getFrameIds(m_tagNr);
0100     m_displayNameMap = Frame::getDisplayNameMap(frameNames);
0101     m_selectFrame = frame;
0102     emit frameSelectionRequested(m_displayNameMap.keys());
0103   }
0104 }
0105 
0106 /**
0107  * Called when the frame selection dialog is closed.
0108  *
0109  * @param displayName name of selected frame, empty if canceled
0110  */
0111 void FrameEditorObject::onFrameSelectionFinished(const QString& displayName)
0112 {
0113   if (!displayName.isEmpty()) {
0114     QString name = m_displayNameMap.value(displayName, displayName);
0115     m_displayNameMap.clear();
0116     Frame::Type type = Frame::getTypeFromName(name);
0117     *m_selectFrame = Frame(type, QLatin1String(""), name, -1);
0118     emit frameSelected(m_tagNr, m_selectFrame);
0119   } else {
0120     emit frameSelected(m_tagNr, nullptr);
0121   }
0122 }
0123 
0124 /**
0125  * Return object which emits frameSelected(), frameEdited() signals.
0126  *
0127  * @return object which emits signals.
0128  */
0129 QObject* FrameEditorObject::qobject()
0130 {
0131   return this;
0132 }