~ubuntu-branches/ubuntu/wily/kid3/wily

« back to all changes in this revision

Viewing changes to src/core/model/frameobjectmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2015-05-12 18:56:41 UTC
  • mfrom: (1.1.19) (2.3.3 sid)
  • Revision ID: package-import@ubuntu.com-20150512185641-hgeys2pingwq9mwn
Tags: 3.2.1-1
* New upstream release.
  - Add new build dependency qt4-dev-tools.
* Uploading to unstable.
* Switch to DEP5 debian/copyright format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file frameobjectmodel.cpp
 
3
 * Object model with frame information.
 
4
 *
 
5
 * \b Project: Kid3
 
6
 * \author Urs Fleisch
 
7
 * \date 02 Sep 2014
 
8
 *
 
9
 * Copyright (C) 2014  Urs Fleisch
 
10
 *
 
11
 * This file is part of Kid3.
 
12
 *
 
13
 * Kid3 is free software; you can redistribute it and/or modify
 
14
 * it under the terms of the GNU General Public License as published by
 
15
 * the Free Software Foundation; either version 2 of the License, or
 
16
 * (at your option) any later version.
 
17
 *
 
18
 * Kid3 is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
 * GNU General Public License for more details.
 
22
 *
 
23
 * You should have received a copy of the GNU General Public License
 
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
25
 */
 
26
 
 
27
#include "frameobjectmodel.h"
 
28
#include <QCoreApplication>
 
29
 
 
30
/**
 
31
 * Constructor.
 
32
 * @param parent parent object
 
33
 */
 
34
FrameObjectModel::FrameObjectModel(QObject* parent) : QObject(parent)
 
35
{
 
36
}
 
37
 
 
38
/**
 
39
 * Destructor.
 
40
 */
 
41
FrameObjectModel::~FrameObjectModel()
 
42
{
 
43
}
 
44
 
 
45
/**
 
46
 * Get frame name.
 
47
 * @return translated frame name.
 
48
 */
 
49
QString FrameObjectModel::name() const
 
50
{
 
51
  return m_frame.getExtendedType().getTranslatedName();
 
52
}
 
53
 
 
54
/**
 
55
 * Get internal frame name.
 
56
 * @return internal frame name, e.g. "TXXX - User defined text information"
 
57
 */
 
58
QString FrameObjectModel::internalName() const
 
59
{
 
60
  QString name(m_frame.getInternalName());
 
61
  if (name.isEmpty()) {
 
62
    name = m_frame.getName();
 
63
  }
 
64
  if (!name.isEmpty()) {
 
65
    int nlPos = name.indexOf(QLatin1Char('\n'));
 
66
    if (nlPos > 0) {
 
67
      // probably "TXXX - User defined text information\nDescription" or
 
68
      // "WXXX - User defined URL link\nDescription"
 
69
      name.truncate(nlPos);
 
70
    }
 
71
    name = QCoreApplication::translate("@default", name.toLatin1().data());
 
72
  }
 
73
  return name;
 
74
}
 
75
 
 
76
/**
 
77
 * Get frame type.
 
78
 * @return type, type Frame::Type.
 
79
 */
 
80
int FrameObjectModel::type() const
 
81
{
 
82
  return m_frame.getType();
 
83
}
 
84
 
 
85
/**
 
86
 * Get frame value.
 
87
 * @return frame value.
 
88
 */
 
89
QString FrameObjectModel::value() const
 
90
{
 
91
  return m_frame.getValue();
 
92
}
 
93
 
 
94
/**
 
95
 * Set frame value.
 
96
 * @param value value
 
97
 */
 
98
void FrameObjectModel::setValue(const QString& value)
 
99
{
 
100
  if (m_frame.getValue() != value) {
 
101
    m_frame.setValueIfChanged(value);
 
102
    emit valueChanged(m_frame.getValue());
 
103
  }
 
104
}
 
105
 
 
106
/**
 
107
 * Get field list.
 
108
 * @return fields.
 
109
 */
 
110
QList<QObject*> FrameObjectModel::fields()
 
111
{
 
112
  QList<QObject*> lst;
 
113
  const int numFields = m_frame.getFieldList().size();
 
114
  if (numFields > 0) {
 
115
    for (int i = 0; i < numFields; ++i) {
 
116
      FrameFieldObject* fieldObj = new FrameFieldObject(i, this);
 
117
      connect(fieldObj, SIGNAL(valueChanged(QVariant)),
 
118
              this, SIGNAL(fieldsChanged()));
 
119
      lst.append(fieldObj);
 
120
    }
 
121
  } else {
 
122
    FrameFieldObject* fieldObj = new FrameFieldObject(-1, this);
 
123
    connect(fieldObj, SIGNAL(valueChanged(QVariant)),
 
124
            this, SIGNAL(fieldsChanged()));
 
125
    lst.append(fieldObj);
 
126
  }
 
127
  return lst;
 
128
}
 
129
 
 
130
/**
 
131
 * Set from frame.
 
132
 * @param frame frame
 
133
 */
 
134
void FrameObjectModel::setFrame(const Frame& frame)
 
135
{
 
136
  m_frame = frame;
 
137
}
 
138
 
 
139
/**
 
140
 * Get frame from object information.
 
141
 * @return frame.
 
142
 */
 
143
Frame FrameObjectModel::getFrame() const
 
144
{
 
145
  return m_frame;
 
146
}
 
147
 
 
148
/**
 
149
 * Get binary data from data field.
 
150
 * @return binary data, empty if not available.
 
151
 */
 
152
QByteArray FrameObjectModel::getBinaryData() const
 
153
{
 
154
  QVariant var(Frame::getField(m_frame, Frame::ID_Data));
 
155
  if (var.isValid()) {
 
156
    return var.toByteArray();
 
157
  }
 
158
  return QByteArray();
 
159
}
 
160
 
 
161
 
 
162
/**
 
163
 * Constructor.
 
164
 * @param index index in field list
 
165
 * @param parent parent object
 
166
 */
 
167
FrameFieldObject::FrameFieldObject(int index, FrameObjectModel* parent) :
 
168
  QObject(parent), m_index(index)
 
169
{
 
170
}
 
171
 
 
172
/**
 
173
 * Destructor.
 
174
 */
 
175
FrameFieldObject::~FrameFieldObject()
 
176
{
 
177
}
 
178
 
 
179
/**
 
180
 * Get field name.
 
181
 * @return translated field name.
 
182
 */
 
183
QString FrameFieldObject::name() const
 
184
{
 
185
  if (FrameObjectModel* fom = frameObject()) {
 
186
    const Frame::FieldList& fields = fom->m_frame.getFieldList();
 
187
    if (m_index >= 0 && m_index < fields.size()) {
 
188
      return Frame::Field::getFieldIdName(
 
189
            static_cast<Frame::FieldId>(fields.at(m_index).m_id));
 
190
    }
 
191
  }
 
192
  return tr("Text");
 
193
}
 
194
 
 
195
/**
 
196
 * Get field ID.
 
197
 * @return id, type Frame::FieldId.
 
198
 */
 
199
int FrameFieldObject::id() const {
 
200
  if (FrameObjectModel* fom = frameObject()) {
 
201
    const Frame::FieldList& fields = fom->m_frame.getFieldList();
 
202
    if (m_index >= 0 && m_index < fields.size()) {
 
203
      return fields.at(m_index).m_id;
 
204
    }
 
205
  }
 
206
  return 0;
 
207
}
 
208
 
 
209
/**
 
210
 * Get field value.
 
211
 * @return field value.
 
212
 */
 
213
QVariant FrameFieldObject::value() const {
 
214
  if (FrameObjectModel* fom = frameObject()) {
 
215
    const Frame::FieldList& fields = fom->m_frame.getFieldList();
 
216
    if (m_index >= 0 && m_index < fields.size()) {
 
217
      return fields.at(m_index).m_value;
 
218
    } else {
 
219
      return fom->value();
 
220
    }
 
221
  }
 
222
  return QVariant();
 
223
}
 
224
 
 
225
/**
 
226
 * Set field value.
 
227
 * @param value value
 
228
 */
 
229
void FrameFieldObject::setValue(const QVariant& value)
 
230
{
 
231
  if (FrameObjectModel* fom = frameObject()) {
 
232
    Frame::FieldList& fields = fom->m_frame.fieldList();
 
233
    if (m_index >= 0 && m_index < fields.size()) {
 
234
      Frame::Field& fld = fields[m_index];
 
235
      if (fld.m_value != value) {
 
236
        fld.m_value = value;
 
237
        emit valueChanged(fld.m_value);
 
238
      }
 
239
    } else {
 
240
      fom->setValue(value.toString());
 
241
    }
 
242
  }
 
243
}
 
244
 
 
245
/**
 
246
 * Get frame type.
 
247
 * @return type, type Frame::Type.
 
248
 */
 
249
int FrameFieldObject::type() const
 
250
{
 
251
  if (FrameObjectModel* fom = frameObject()) {
 
252
    return fom->type();
 
253
  }
 
254
  return Frame::FT_UnknownFrame;
 
255
}