-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathccditemmodel.cpp
More file actions
350 lines (312 loc) · 9.31 KB
/
ccditemmodel.cpp
File metadata and controls
350 lines (312 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* Copyright (C) 2021 Jo2003 (olenka.joerg@gmail.com)
* This file is part of cd2netmd_gui
*
* cd2netmd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cd2netmd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*/
#include "ccditemmodel.h"
#include <QFont>
#include <QIcon>
#include <QMimeData>
#include <cdio/cdio.h>
#include "ccditemmodel.h"
#include "defines.h"
//--------------------------------------------------------------------------
//! @brief Constructs a new instance.
//!
//! @param[in] tracks The audio tracks vector
//! @param parent The parent
//--------------------------------------------------------------------------
CCDItemModel::CCDItemModel(const c2n::AudioTracks &tracks,
QObject *parent)
:QAbstractTableModel(parent), mTracks(tracks)
{
}
CCDItemModel::~CCDItemModel()
{
// cleanup time
}
int CCDItemModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return mTracks.size();
}
int CCDItemModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 2;
}
QVariant CCDItemModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
if (role == Qt::DisplayRole)
{
switch (col)
{
case 0:
return mTracks.at(row).mTitle;
case 1:
{
float secs = static_cast<float>(mTracks.at(row).mLbCount) / static_cast<float>(CDIO_CD_FRAMES_PER_SEC);
int h = static_cast<int>(secs) / 3600;
int m = (static_cast<int>(secs) % 3600) / 60;
float fsec = secs - static_cast<float>(h * 3600 + m * 60);
return QString("%1:%2:%3")
.arg(h, 1, 10, QChar('0'))
.arg(m, 2, 10, QChar('0'))
.arg(fsec, 5, 'f', 2, QChar('0'));
}
default:
return QVariant();
}
}
else if ((role == Qt::EditRole) && (col == 0))
{
return mTracks.at(row).mTitle;
}
else if ((role == Qt::DecorationRole) && (col == 0))
{
return QIcon(":/view/audio");
}
else if ((role == Qt::UserRole) && (col == 1))
{
// return track time in seconds
return static_cast<double>(mTracks.at(row).mLbCount) / static_cast<double>(CDIO_CD_FRAMES_PER_SEC);
}
else if ((role == TSTAMP_ROLE) && (col == 1))
{
return mTracks.at(row).mTStamp;
}
return QVariant();
}
bool CCDItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
int row = index.row();
int col = index.column();
if (role == Qt::EditRole)
{
if (col == 0)
{
mTracks[row].mTitle = value.toString();
return true;
}
}
return QAbstractTableModel::setData(index, value, role);
}
QVariant CCDItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal)
{
switch (section)
{
case 0:
return QString("Title");
case 1:
return QString("Time");
}
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
//--------------------------------------------------------------------------
//! @brief get item flags
//!
//! @param[in] index The index
//!
//! @return item flags
//--------------------------------------------------------------------------
Qt::ItemFlags CCDItemModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
if (index.isValid())
{
return Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
}
else
{
return Qt::ItemIsDropEnabled | defaultFlags;
}
}
//--------------------------------------------------------------------------
//! @brief tells what dropactions are supported
//!
//! @return supported drop actions
//--------------------------------------------------------------------------
Qt::DropActions CCDItemModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
//--------------------------------------------------------------------------
//! @brief export mime types
//--------------------------------------------------------------------------
QStringList CCDItemModel::mimeTypes() const
{
QStringList types;
types << "application/vnd.text.list";
return types;
}
//--------------------------------------------------------------------------
//! @brief remove [count] rows starting from [row]
//!
//! @param[in] row target row
//! @param[in] count row count
//! @param[in] parent parent modell index
//!
//! @return true if handled
//--------------------------------------------------------------------------
bool CCDItemModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent)
if ((row > -1) && (row > -1))
{
beginRemoveRows(parent, row, row + count - 1);
for(int i = (count - 1); i >= 0; i--)
{
if ((i + row) < mTracks.size())
{
mTracks.remove(i + row);
}
}
endRemoveRows();
return true;
}
return false;
}
//--------------------------------------------------------------------------
//! @brief get length of audio in kist
//!
//! @return length in blocks
//--------------------------------------------------------------------------
long CCDItemModel::audioLength() const
{
long length = 0;
for (const auto& t : mTracks)
{
length += t.mLbCount;
}
return length;
}
//--------------------------------------------------------------------------
//! @brief return current audio tracks
//!
//! @return audio tracks
//--------------------------------------------------------------------------
c2n::AudioTracks CCDItemModel::audioTracks() const
{
return mTracks;
}
//--------------------------------------------------------------------------
//! @brief Information carried when dragging
//!
//! @return pointer to mime data
//--------------------------------------------------------------------------
QMimeData *CCDItemModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
for(const auto& index : indexes)
{
if (index.isValid() && (index.column() == 0))
{
if (index.column() == 0)
{
// insert source row
stream << QString::number(index.row());
}
}
}
mimeData->setData("application/vnd.text.list", encodedData);
return mimeData;
}
//--------------------------------------------------------------------------
//! @brief Process the drop according to the information carried by drag
//!
//! @param[in] data mime date needed for drop
//! @param[in] action what needs to be done
//! @param[in] row target row
//! @param[in] column target column
//! @param[in] parent parent modell index
//!
//! @return true if handled
//--------------------------------------------------------------------------
bool CCDItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (action == Qt::IgnoreAction)
{
return true;
}
if (!data->hasFormat("application/vnd.text.list"))
{
return false;
}
if (column > 0)
{
return false;
}
int beginRow;
if (row != -1)
{
beginRow = row;
}
else if (parent.isValid())
{
beginRow = parent.row();
}
else
{
beginRow = rowCount(QModelIndex());
}
QByteArray encodedData = data->data("application/vnd.text.list");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QVector<int> srcRows;
while (!stream.atEnd())
{
QString text;
stream >> text;
srcRows << text.toInt();
}
std::sort(srcRows.begin(), srcRows.end());
qInfo() << "Drop rows" << srcRows << "into row" << beginRow;
// create temp audio tracks vector for sorting
c2n::AudioTracks tmpTracks;
bool tracksHandled = false;
for (int i = 0; i < mTracks.size(); i++)
{
if (i == beginRow)
{
tracksHandled = true;
for (auto t : srcRows)
{
tmpTracks.append(mTracks.at(t));
}
}
if (!srcRows.contains(i))
{
tmpTracks.append(mTracks.at(i));
}
}
if (!tracksHandled)
{
// append
for (auto t : srcRows)
{
tmpTracks.append(mTracks.at(t));
}
}
mTracks = tmpTracks;
return true;
}