-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagcliprectitem.cpp
349 lines (297 loc) · 14.5 KB
/
agcliprectitem.cpp
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
#include "agcliprectitem.h"
#include "aglobal.h"
#include "agtagtextitem.h"
#include <QDir>
#include <QStyle>
AGClipRectItem::AGClipRectItem(QGraphicsItem *parent, AGMediaFileRectItem *mediaItem, QFileInfo fileInfo, int duration, int clipIn, int clipOut) :
AGViewRectItem(parent, fileInfo)
{
// changed = true;
this->mediaType = "Clip";
this->itemType = "Base";
this->clipIn = clipIn;
this->clipOut = clipOut;
this->duration = duration;
this->mediaItem = mediaItem;
this->mediaItem->clips << this;
// this->timelineGroupItem = mediaItem->groupItem->timelineGroupItem;
mediaItem->groupItem->timelineGroupItem->clips<<this;
// qDebug()<<"AGClipRectItem addclip"<<this->timelineGroupItem->fileInfo.fileName()<<this->timelineGroupItem->clips.count();
// setFocusProxy(mediaItem);
int alpha = 125;
if (AGlobal().audioExtensions.contains(fileInfo.suffix(), Qt::CaseInsensitive))
setBrush(QColor(0, 128, 0, alpha)); //darkgreen
else if (AGlobal().imageExtensions.contains(fileInfo.suffix(), Qt::CaseInsensitive))
setBrush(QColor(218, 130, 42, alpha));
else if (AGlobal().projectExtensions.contains(fileInfo.suffix(), Qt::CaseInsensitive))
setBrush(QColor(130, 218, 42, alpha));
else
setBrush(QColor(42, 130, 218, alpha)); //#2a82da blue-ish
setData(itemTypeIndex, itemType);
setData(mediaTypeIndex, mediaType);
updateToolTip();
QGraphicsRectItem *durationLine = new QGraphicsRectItem(this);
durationLine->setBrush(Qt::darkGreen);
durationLine->setData(itemTypeIndex, "SubDurationLine");
durationLine->setData(mediaTypeIndex, "Clip");
// durationLine->setData(folderNameIndex, fileInfo.absolutePath());
// durationLine->setData(fileNameIndex, fileInfo.fileName());
}
AGClipRectItem::~AGClipRectItem()
{
// this->mediaItem->clips.rem
}
QString AGClipRectItem::itemToString()
{
return AGViewRectItem::itemToString() + " " + QString::number(clipIn) + " " + QString::number(clipOut);// + " " + QString::number(item->zValue());
}
void AGClipRectItem::processAction(QString action)
{
// qDebug()<<"AGClipRectItem::processAction"<<fileInfo.fileName()<<clipIn<<clipOut<<action;
if (action == "actionIn")
{
if (mediaItem->m_player->position() > clipOut)
mediaItem->processAction(action); //add a new clip in mediaItem
else
{
emit addUndo(true, "Update", "Clip", this, "clipIn", QString::number(clipIn), QString::number(mediaItem->m_player->position()));
clipIn = mediaItem->m_player->position();
duration = clipOut - clipIn;
}
}
else if (action == "actionOut")
{
emit addUndo(true, "Update", "Clip", this, "clipOut", QString::number(clipOut), QString::number(mediaItem->m_player->position()));
clipOut = mediaItem->m_player->position();
duration = clipOut - clipIn;
// qDebug()<<"AGClipRectItem processAction"<<action<<duration<<clipIn<<clipOut;
}
else if (action.contains("Key_"))
{
//if key is * to ****: check if exists * and replace, otherwise add. if 0* then remove tag
//if key is ✔: check if exists then remove (toggle), otherwise add
//else if keybuffer not empty: check if (old) keybuffer exists (always the case...)
// if key is \r: empty keybuffer (tag will be red)
// if key is \b: remove last character from tag. If tag is empty, remove tag
// else update tag by adding key and update keybuffer
//else if empty keybuffer: add key
if (action == "Key_\r") //return
keyBuffer = "";
else
{
//loop over tags
QString searchTag = "";
if (action.contains("Key_*"))
searchTag = "*";
else if (action == "Key_✔")
searchTag = "✔";
else if (keyBuffer != "")
searchTag = keyBuffer;
bool found = false;
if (searchTag != "")
{
foreach (AGTagTextItem *tagItem, tags)
{
if ((action.contains("Key_*") && tagItem->tagName.contains(searchTag)) || tagItem->tagName == searchTag) //contains because of *(*(*(*(*)))))
{
found = true;
if (action == "Key_✔")
emit deleteItem(true, "Tag", fileInfo, clipIn, "✔");
else if (action == "Key_\b" && tagItem->tagName.length() == 1)
emit deleteItem(true, "Tag", fileInfo, clipIn, tagItem->tagName);
else if (action == "Key_*0")
emit deleteItem(true, "Tag", fileInfo, clipIn, tagItem->tagName);
else
{
QString oldValue = tagItem->tagName;
if (action == "Key_\b")
{
tagItem->tagName = tagItem->tagName.left(tagItem->tagName.length()-1);
keyBuffer = tagItem->tagName;
}
else if (action.contains("Key_*"))
tagItem->tagName = action.mid(4);
else
{
tagItem->tagName += action.mid(4);
keyBuffer = tagItem->tagName;
}
emit addUndo(true, "Update", "Tag", tagItem, "tagName", oldValue, keyBuffer);
tagItem->setHtml(QString("<div align=\"center\">") + tagItem->tagName + "</div>");
}
}
}
}
if (!found && !action.contains("Key_\b"))
{
//add tag
if (!action.contains("Key_*") && action != "Key_✔")
keyBuffer += action.mid(4);
emit addItem(true, "Clip", "Tag", fileInfo, duration, clipIn, clipOut, action.mid(4));
}
}
}
else
{
mediaItem->processAction(action);
}
}
void AGClipRectItem::onItemRightClicked(QPoint pos)
{
QGraphicsView *view = scene()->views().first();
fileContextMenu->clear();
fileContextMenu->addAction(new QAction("Zoom to item",fileContextMenu));
connect(fileContextMenu->actions().last(), &QAction::triggered, [=]()
{
view->fitInView(boundingRect()|childrenBoundingRect(), Qt::KeepAspectRatio);
});
fileContextMenu->actions().last()->setToolTip(tr("<p><b>%1</b></p>"
"<p><i>Zooms in to %2 and it's details</i></p>"
).arg(fileContextMenu->actions().last()->text(), fileInfo.fileName()));
fileContextMenu->addSeparator();
fileContextMenu->addAction(new QAction("Delete clip",fileContextMenu));
fileContextMenu->actions().last()->setIcon(qApp->style()->standardIcon(QStyle::SP_TrashIcon));
connect(fileContextMenu->actions().last(), &QAction::triggered, [=]()
{
emit deleteItem(true, "Clip", fileInfo, clipIn);
});
fileContextMenu->addAction(new QAction("Archive clips",fileContextMenu));
fileContextMenu->actions().last()->setIcon(qApp->style()->standardIcon(QStyle::SP_TrashIcon));
connect(fileContextMenu->actions().last(), &QAction::triggered, [=]()
{
AGProcessAndThread *process = new AGProcessAndThread(this);
process->command("Archive clips", [=]
{
QString recycleFolder = fileInfo.absolutePath() + "/" + "MSKRecycleBin/";
bool success = true;
QDir dir(recycleFolder);
if (!dir.exists())
{
success = dir.mkpath(".");
if (success)
process->addProcessLog("output", tr("%1 created").arg(recycleFolder));
}
if (success)
{
//first supporting files
if (success)
{
//srt file
QString srtFileName = fileInfo.completeBaseName() + ".srt";
QFile *file = new QFile(fileInfo.absolutePath() + "/" + srtFileName);
if (file->exists())
{
recursiveFileRenameCopyIfExists(recycleFolder, srtFileName);
success = file->rename(recycleFolder + srtFileName);
if (success)
{
// qDebug()<<"Undo - archive srt"<<srtFileName <<recycleFolder;
emit addUndo(false, "Archive", "Clips", this->mediaItem);
process->addProcessLog("output", tr("%1 moved to recycle folder").arg(srtFileName));
}
}
//txt file
if (success)
{
srtFileName = fileInfo.completeBaseName() + ".txt";
file = new QFile(fileInfo.absolutePath() + "/" + srtFileName);
if (file->exists())
{
recursiveFileRenameCopyIfExists(recycleFolder, srtFileName);
success = file->rename(recycleFolder + srtFileName);
if (success)
process->addProcessLog("output", tr("%1 moved to recycle folder").arg(srtFileName));
}
}
else
process->addProcessLog("error", QString("-3, could not rename to " + recycleFolder + srtFileName));
}
else
process->addProcessLog("error", QString("-2, could not rename to " + recycleFolder + fileInfo.fileName()));
}
else
process->addProcessLog("error", QString("-1, could not create folder " + recycleFolder));
});
process->start();
});
fileContextMenu->actions().last()->setToolTip(tr("<p><b>%1</b></p>"
"<p><i>Move the clips of %2 (.srt file) and supporting files (.txt) to the Media Sidekick recycle bin folder</i></p>"
).arg(fileContextMenu->actions().last()->text(), fileInfo.fileName()));
QPointF map = view->mapToGlobal(QPoint(pos.x()+10, pos.y()));
fileContextMenu->popup(QPoint(map.x(), map.y()));
}
QGraphicsItem * AGClipRectItem::drawPoly()
{
// prepareGeometryChange();
// qDebug()<<"AGView::drawPoly"<<itemToString(clipItem);
QGraphicsPolygonItem *polyItem = nullptr;
//find poly
foreach (QGraphicsItem *item, childItems())
{
if (item->data(itemTypeIndex) == "poly")
polyItem = (QGraphicsPolygonItem *)item;
}
bool shouldDraw = true;
if (parentItem() != mediaItem) //if clip part of timeline
{
//if mediafile not in same FileGroup as clip then do not draw (in case of filtering where the clip is in parking and the mediafile not)
QGraphicsItem *clipFileGroup = parentItem()->parentItem();
QGraphicsItem *mediaFileGroup = mediaItem->parentItem();
if (clipFileGroup != mediaFileGroup)
{
shouldDraw = false;
}
}
if (shouldDraw)
{
if (polyItem == nullptr)
{
// if (fileInfo.fileName().contains("Blindfold"))
// qDebug()<<"new poly" << itemToString(clipItem);
// QBrush brush;
// brush.setColor(Qt::lightGray);
// brush.setStyle(Qt::SolidPattern);
QPen pen(Qt::transparent);
polyItem = new QGraphicsPolygonItem(this);
polyItem->setPen(pen);
QColor color = Qt::lightGray;
color.setAlpha(127);
polyItem->setBrush(color);
polyItem->setZValue(99999); //to put polys after tags in arrangeitem ordering
polyItem->setData(mediaTypeIndex, "Poly");
polyItem->setData(itemTypeIndex, "poly");
}
if (polyItem != nullptr) //should always be the case here
{
// if (fileInfo.fileName().contains("Blindfold"))
// qDebug()<<"draw poly" << itemToString(clipItem) << itemToString(parentMediaFile);
if (mediaItem != nullptr) //should always be the case
{
int duration = this->mediaItem->duration;
// qDebug()<<"AGView::drawPoly inout"<<clipIn<<clipOut<<duration<<itemToString(parentMediaFile);
QPointF parentPointLeft = mapFromItem(mediaItem, QPoint(duration==0?0:mediaItem->rect().width() * clipIn / duration, mediaItem->rect().height()));
QPointF parentPointRight = mapFromItem(mediaItem, QPoint(duration==0?0:mediaItem->rect().width() * clipOut / duration, mediaItem->rect().height()));
// if (fileInfo.fileName().contains("Blindfold"))
// qDebug()<<" "<<parentMediaFile->rect()<<clipIn<<duration<<parentPointLeft<<QPoint(duration==0?0:parentMediaFile->rect().width() * clipIn / duration, parentMediaFile->rect().height());
//all points relative to clip
QPolygonF polyGon;
polyGon.append(QPointF(parentPointLeft.x(), parentPointLeft.y()+1));
polyGon.append(QPointF(-1,-1));
polyGon.append(QPointF(boundingRect().width(),-1));
polyGon.append(QPointF(parentPointRight.x(), parentPointRight.y()+1));
polyItem->setPolygon(polyGon);
// QGraphicsOpacityEffect *bef = new QGraphicsOpacityEffect();
// polyItem->setGraphicsEffect(bef); //make it significantly slower
}
}
}
else //should not draw
{
if (polyItem != nullptr)
{
delete polyItem;
polyItem = nullptr;
}
}
return polyItem;
}