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

0001 /**
0002  * \file frameobjectmodel.cpp
0003  * Object model with frame information.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 02 Sep 2014
0008  *
0009  * Copyright (C) 2014-2018  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 "frameobjectmodel.h"
0028 #include <QCoreApplication>
0029 
0030 /**
0031  * Constructor.
0032  * @param parent parent object
0033  */
0034 FrameObjectModel::FrameObjectModel(QObject* parent) : QObject(parent)
0035 {
0036 }
0037 
0038 /**
0039  * Get frame name.
0040  * @return translated frame name.
0041  */
0042 QString FrameObjectModel::name() const
0043 {
0044   return m_frame.getExtendedType().getTranslatedName();
0045 }
0046 
0047 /**
0048  * Get internal frame name.
0049  * @return internal frame name, e.g. "TXXX - User defined text information"
0050  */
0051 QString FrameObjectModel::internalName() const
0052 {
0053   QString name(m_frame.getInternalName());
0054   if (name.isEmpty()) {
0055     name = m_frame.getName();
0056   }
0057   if (!name.isEmpty()) {
0058     if (int nlPos = name.indexOf(QLatin1Char('\n')); nlPos > 0) {
0059       // probably "TXXX - User defined text information\nDescription" or
0060       // "WXXX - User defined URL link\nDescription"
0061       name.truncate(nlPos);
0062     }
0063     name = QCoreApplication::translate("@default", name.toLatin1().data());
0064   }
0065   return name;
0066 }
0067 
0068 /**
0069  * Get frame type.
0070  * @return type, type Frame::Type.
0071  */
0072 int FrameObjectModel::type() const
0073 {
0074   return m_frame.getType();
0075 }
0076 
0077 /**
0078  * Get frame value.
0079  * @return frame value.
0080  */
0081 QString FrameObjectModel::value() const
0082 {
0083   return m_frame.getValue();
0084 }
0085 
0086 /**
0087  * Set frame value.
0088  * @param value value
0089  */
0090 void FrameObjectModel::setValue(const QString& value)
0091 {
0092   if (m_frame.getValue() != value) {
0093     m_frame.setValueIfChanged(value);
0094     emit valueChanged(m_frame.getValue());
0095   }
0096 }
0097 
0098 /**
0099  * Get field list.
0100  * @return fields.
0101  */
0102 QList<QObject*> FrameObjectModel::fields()
0103 {
0104   QList<QObject*> lst;
0105   if (const int numFields = m_frame.getFieldList().size(); numFields > 0) {
0106     for (int i = 0; i < numFields; ++i) {
0107       auto fieldObj = new FrameFieldObject(i, this);
0108       connect(fieldObj, &FrameFieldObject::valueChanged,
0109               this, &FrameObjectModel::fieldsChanged);
0110       lst.append(fieldObj);
0111     }
0112   } else {
0113     auto fieldObj = new FrameFieldObject(-1, this);
0114     connect(fieldObj, &FrameFieldObject::valueChanged,
0115             this, &FrameObjectModel::fieldsChanged);
0116     lst.append(fieldObj);
0117   }
0118   return lst;
0119 }
0120 
0121 /**
0122  * Set from frame.
0123  * @param frame frame
0124  */
0125 void FrameObjectModel::setFrame(const Frame& frame)
0126 {
0127   m_frame = frame;
0128 }
0129 
0130 /**
0131  * Get frame from object information.
0132  * @return frame.
0133  */
0134 Frame FrameObjectModel::getFrame() const
0135 {
0136   return m_frame;
0137 }
0138 
0139 /**
0140  * Get binary data from data field.
0141  * @return binary data, empty if not available.
0142  */
0143 QByteArray FrameObjectModel::getBinaryData() const
0144 {
0145   if (QVariant var(Frame::getField(m_frame, Frame::ID_Data)); var.isValid()) {
0146     return var.toByteArray();
0147   }
0148   return QByteArray();
0149 }
0150 
0151 
0152 /**
0153  * Constructor.
0154  * @param index index in field list
0155  * @param parent parent object
0156  */
0157 FrameFieldObject::FrameFieldObject(int index, FrameObjectModel* parent)
0158   : QObject(parent), m_index(index)
0159 {
0160 }
0161 
0162 /**
0163  * Get field name.
0164  * @return translated field name.
0165  */
0166 QString FrameFieldObject::name() const
0167 {
0168   if (FrameObjectModel* fom = frameObject()) {
0169     if (const Frame::FieldList& fields = fom->m_frame.getFieldList();
0170         m_index >= 0 && m_index < fields.size()) {
0171       return Frame::Field::getFieldIdName(
0172             static_cast<Frame::FieldId>(fields.at(m_index).m_id));
0173     }
0174   }
0175   return tr("Text");
0176 }
0177 
0178 /**
0179  * Get field ID.
0180  * @return id, type Frame::FieldId.
0181  */
0182 int FrameFieldObject::id() const {
0183   if (FrameObjectModel* fom = frameObject()) {
0184     if (const Frame::FieldList& fields = fom->m_frame.getFieldList();
0185         m_index >= 0 && m_index < fields.size()) {
0186       return fields.at(m_index).m_id;
0187     }
0188   }
0189   return 0;
0190 }
0191 
0192 /**
0193  * Get field value.
0194  * @return field value.
0195  */
0196 QVariant FrameFieldObject::value() const {
0197   if (FrameObjectModel* fom = frameObject()) {
0198     if (const Frame::FieldList& fields = fom->m_frame.getFieldList();
0199         m_index >= 0 && m_index < fields.size()) {
0200       return fields.at(m_index).m_value;
0201     }
0202     return fom->value();
0203   }
0204   return QVariant();
0205 }
0206 
0207 /**
0208  * Set field value.
0209  * @param value value
0210  */
0211 void FrameFieldObject::setValue(const QVariant& value)
0212 {
0213   if (FrameObjectModel* fom = frameObject()) {
0214     if (Frame::FieldList& fields = fom->m_frame.fieldList();
0215         m_index >= 0 && m_index < fields.size()) {
0216       Frame::Field& fld = fields[m_index];
0217       if (fld.m_value != value) {
0218         fld.m_value = value;
0219         emit valueChanged(fld.m_value);
0220       }
0221     } else {
0222       fom->setValue(value.toString());
0223     }
0224   }
0225 }
0226 
0227 /**
0228  * Get frame type.
0229  * @return type, type Frame::Type.
0230  */
0231 int FrameFieldObject::type() const
0232 {
0233   if (FrameObjectModel* fom = frameObject()) {
0234     return fom->type();
0235   }
0236   return Frame::FT_UnknownFrame;
0237 }