aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/screenrecorder/cropandtrim.cpp
blob: 819f0e285b60d95e75e2988551885b08b8a5c5b3 (plain)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "cropandtrim.h"

#include "ffmpegutils.h"
#include "screenrecordersettings.h"
#include "screenrecordertr.h"

#include <utils/fileutils.h>
#include <utils/layoutbuilder.h>
#include <utils/process.h>
#include <utils/qtcsettings.h>
#include <utils/styledbar.h>
#include <utils/stylehelper.h>
#include <utils/utilsicons.h>

#include <coreplugin/icore.h>

#include <QAction>
#include <QClipboard>
#include <QDialog>
#include <QDialogButtonBox>
#include <QGuiApplication>
#include <QLabel>
#include <QMouseEvent>
#include <QPainter>
#include <QPushButton>
#include <QScrollArea>
#include <QSlider>
#include <QSpinBox>
#include <QStyleOptionSlider>
#include <QToolButton>

using namespace Utils;

namespace ScreenRecorder {

CropScene::CropScene(QWidget *parent)
    : QWidget(parent)
{
    setMouseTracking(true);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

void CropScene::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter p(this);
    p.drawImage(QPointF(), m_buffer);
}

void CropScene::initMouseInteraction(const QPoint &imagePos)
{
    static const auto inGripRange = [](int grip, int pos, int &clickOffset) -> bool {
        const bool inRange = pos - m_gripWidth <= grip && pos + m_gripWidth >= grip;
        if (inRange)
            clickOffset = grip - pos;
        return inRange;
    };

    m_mouse.clickOffset = {};
    if (inGripRange(imagePos.x(), m_cropRect.left(), m_mouse.clickOffset.rx())) {
        m_mouse.margin = EdgeLeft;
        m_mouse.cursorShape = Qt::SizeHorCursor;
    } else if (inGripRange(imagePos.x(), m_cropRect.right(), m_mouse.clickOffset.rx())) {
        m_mouse.margin = EdgeRight;
        m_mouse.cursorShape = Qt::SizeHorCursor;
    } else if (inGripRange(imagePos.y(), m_cropRect.top(), m_mouse.clickOffset.ry())) {
        m_mouse.margin = EdgeTop;
        m_mouse.cursorShape = Qt::SizeVerCursor;
    } else if (inGripRange(imagePos.y(), m_cropRect.bottom(), m_mouse.clickOffset.ry())) {
        m_mouse.margin = EdgeBottom;
        m_mouse.cursorShape = Qt::SizeVerCursor;
    } else if (!fullySelected() && activeMoveArea().contains(imagePos)) {
        m_mouse.margin = Move;
        m_mouse.cursorShape = Qt::SizeAllCursor;
        m_mouse.clickOffset = imagePos - m_cropRect.topLeft();
    } else {
        m_mouse.margin = Free;
        m_mouse.cursorShape = Qt::ArrowCursor;
    }
}

void CropScene::updateBuffer()
{
    if (m_buffer.isNull())
        return;

    m_buffer.fill(palette().window().color());
    const qreal dpr = m_image->devicePixelRatioF();
    QPainter p(&m_buffer);
    p.drawImage(lineWidth, lineWidth, *m_image);

    const qreal lineOffset = lineWidth / 2.0;
    const QRectF r = {
        m_cropRect.x() / dpr + lineOffset,
        m_cropRect.y() / dpr + lineOffset,
        m_cropRect.width() / dpr + lineWidth,
        m_cropRect.height() / dpr + lineWidth
    };

    p.save();
    p.setClipRegion(QRegion(m_buffer.rect()).subtracted(QRegion(r.toRect())));
    p.setOpacity(0.85);
    p.fillRect(m_buffer.rect(), qRgb(0x30, 0x30, 0x30));
    p.restore();

    const auto paintLine = [&p](const QLineF &line)
    {
        const QPen blackPen(Qt::black, lineWidth);
        p.setPen(blackPen);
        p.drawLine(line);
        const QPen whiteDotPen(Qt::white, lineWidth, Qt::DotLine);
        p.setPen(whiteDotPen);
        p.drawLine(line);
    };
    paintLine(QLineF(r.left(), 0, r.left(), m_buffer.height()));
    paintLine(QLineF(0, r.top(), m_buffer.width(), r.top()));
    paintLine(QLineF(r.right(), 0, r.right(), m_buffer.height()));
    paintLine(QLineF(0, r.bottom(), m_buffer.width(), r.bottom()));

    update();
}

QPoint CropScene::toImagePos(const QPoint &widgetPos) const
{
    const int dpr = int(m_image->devicePixelRatio());
    return {(widgetPos.x() - lineWidth) * dpr, (widgetPos.y() - lineWidth) * dpr};
}

QRect CropScene::activeMoveArea() const
{
    const qreal minRatio = 0.22;  // At least 22% width and height of current selection
    const int minAbsoluteSize = 40;
    QRect result(0, 0, qMax(int(m_cropRect.width() * minRatio), minAbsoluteSize),
                 qMax(int(m_cropRect.height() * minRatio), minAbsoluteSize));
    result.moveCenter(m_cropRect.center());
    return result;
}

QRect CropScene::cropRect() const
{
    return m_cropRect;
}

void CropScene::setCropRect(const QRect &rect)
{
    m_cropRect = rect;
    updateBuffer();
    emit cropRectChanged(m_cropRect);
}

bool CropScene::fullySelected() const
{
    return m_image && m_image->rect() == m_cropRect;
}

void CropScene::setFullySelected()
{
    if (!m_image)
        return;
    setCropRect(m_image->rect());
}

QRect CropScene::fullRect() const
{
    return m_image ? m_image->rect() : QRect();
}

void CropScene::setImage(const QImage &image)
{
    m_image = &image;
    const QSize sceneSize = m_image->deviceIndependentSize().toSize()
                                .grownBy({lineWidth, lineWidth, lineWidth, lineWidth});
    const QSize sceneSizeDpr = sceneSize * m_image->devicePixelRatio();
    m_buffer = QImage(sceneSizeDpr, QImage::Format_RGB32);
    m_buffer.setDevicePixelRatio(m_image->devicePixelRatio());
    updateBuffer();
    resize(sceneSize);
}

QImage CropScene::croppedImage() const
{
    if (!m_image)
        return {};

    return m_image->copy(m_cropRect);
}

void CropScene::mouseMoveEvent(QMouseEvent *event)
{
    const QPoint imagePos = toImagePos(event->pos());

    if (m_mouse.dragging) {
        switch (m_mouse.margin) {
        case EdgeLeft:
            m_cropRect.setLeft(qBound(0, imagePos.x() - m_mouse.clickOffset.x(),
                                      m_cropRect.right()));
            break;
        case EdgeTop:
            m_cropRect.setTop(qBound(0, imagePos.y() - m_mouse.clickOffset.y(),
                                     m_cropRect.bottom()));
            break;
        case EdgeRight:
            m_cropRect.setRight(qBound(m_cropRect.left(), imagePos.x() - m_mouse.clickOffset.x(),
                                       m_image->width() - 1));
            break;
        case EdgeBottom:
            m_cropRect.setBottom(qBound(m_cropRect.top(), imagePos.y() - m_mouse.clickOffset.y(),
                                        m_image->height() - 1));
            break;
        case Free:
            m_cropRect = QRect(m_mouse.startImagePos, imagePos).normalized()
                             .intersected(m_image->rect());
            break;
        case Move: {
            const QPoint topLeft(qBound(0, imagePos.x() - m_mouse.clickOffset.x(),
                                        m_image->width() - m_cropRect.width()),
                                 qBound(0, imagePos.y() - m_mouse.clickOffset.y(),
                                        m_image->height() - m_cropRect.height()));
            m_cropRect = QRect(topLeft, m_cropRect.size());
            break;
        }
        }
        emit cropRectChanged(m_cropRect);
        updateBuffer();
    } else {
        initMouseInteraction(imagePos);
        setCursor(m_mouse.cursorShape);
    }

    QWidget::mouseMoveEvent(event);
}

void CropScene::mousePressEvent(QMouseEvent *event)
{
    const QPoint imagePos = toImagePos(event->pos());

    m_mouse.dragging = true;
    m_mouse.startImagePos = imagePos;

    QWidget::mousePressEvent(event);
}

void CropScene::mouseReleaseEvent(QMouseEvent *event)
{
    m_mouse.dragging = false;
    setCursor(Qt::ArrowCursor);

    QWidget::mouseReleaseEvent(event);
}

class CropWidget : public QWidget
{
public:
    explicit CropWidget(QWidget *parent = nullptr);

    QRect cropRect() const;
    void setCropRect(const QRect &rect);

    void setImage(const QImage &image);

private:
    void updateWidgets();
    void onSpinBoxChanged();
    void onCropRectChanged();

    CropScene *m_cropScene;

    QSpinBox *m_xSpinBox;
    QSpinBox *m_ySpinBox;
    QSpinBox *m_widthSpinBox;
    QSpinBox *m_heightSpinBox;

    CropSizeWarningIcon *m_warningIcon;
    QToolButton *m_resetButton;
};

CropWidget::CropWidget(QWidget *parent)
    : QWidget(parent)
{
    m_cropScene = new CropScene;

    auto scrollArea = new QScrollArea;
    scrollArea->setWidget(m_cropScene);

    for (auto s : {&m_xSpinBox, &m_ySpinBox, &m_widthSpinBox, &m_heightSpinBox}) {
        *s = new QSpinBox;
        (*s)->setMaximum(99999); // Will be adjusted in CropWidget::setImage
        (*s)->setSuffix(" px");
    }
    m_widthSpinBox->setMinimum(1);
    m_heightSpinBox->setMinimum(1);

    m_resetButton = new QToolButton;
    m_resetButton->setIcon(Icons::RESET.icon());

    m_warningIcon = new CropSizeWarningIcon(CropSizeWarningIcon::StandardVariant);

    auto saveImageButton = new QToolButton;
    saveImageButton->setToolTip(Tr::tr("Save current, cropped frame as image file."));
    saveImageButton->setIcon(Icons::SAVEFILE.icon());

    auto copyImageToClipboardAction = new QAction(Tr::tr("Copy current, cropped frame as image "
                                                         "to the clipboard."), this);
    copyImageToClipboardAction->setIcon(Icons::SNAPSHOT.icon());
    copyImageToClipboardAction->setShortcut(QKeySequence::Copy);

    auto copyImageToClipboardButton = new QToolButton;
    copyImageToClipboardButton->setDefaultAction(copyImageToClipboardAction);

    using namespace Layouting;
    Column {
        scrollArea,
        Row {
            Tr::tr("X:"), m_xSpinBox,
            Space(4), Tr::tr("Y:"), m_ySpinBox,
            Space(16), Tr::tr("Width:"), m_widthSpinBox,
            Space(4), Tr::tr("Height:"), m_heightSpinBox,
            m_resetButton,
            m_warningIcon,
            st,
            saveImageButton,
            copyImageToClipboardButton,
        },
        noMargin(),
    }.attachTo(this);

    connect(m_xSpinBox, &QSpinBox::valueChanged, this, &CropWidget::onSpinBoxChanged);
    connect(m_ySpinBox, &QSpinBox::valueChanged, this, &CropWidget::onSpinBoxChanged);
    connect(m_widthSpinBox, &QSpinBox::valueChanged, this, &CropWidget::onSpinBoxChanged);
    connect(m_heightSpinBox, &QSpinBox::valueChanged, this, &CropWidget::onSpinBoxChanged);
    connect(m_cropScene, &CropScene::cropRectChanged, this, &CropWidget::onCropRectChanged);
    connect(m_resetButton, &QToolButton::pressed, this, [this] {
        m_cropScene->setFullySelected();
    });
    connect(saveImageButton, &QToolButton::clicked, this, [this] {
        FilePathAspect &lastDir = Internal::settings().lastSaveImageDirectory;
        const QString ext(".png");
        FilePath file = FileUtils::getSaveFilePath(nullptr, Tr::tr("Save Current Frame As"),
                                                   lastDir(), "*" + ext);
        if (!file.isEmpty()) {
            if (!file.endsWith(ext))
                file = file.stringAppended(ext);
            lastDir.setValue(file.parentDir());
            lastDir.writeToSettingsImmediatly();
            const QImage image = m_cropScene->croppedImage();
            image.save(file.toString());
        }
    });
    connect(copyImageToClipboardAction, &QAction::triggered, this, [this] {
        const QImage image = m_cropScene->croppedImage();
        QClipboard *clipboard = QGuiApplication::clipboard();
        clipboard->setImage(image);
    });

    updateWidgets();
}

QRect CropWidget::cropRect() const
{
    return m_cropScene->cropRect();
}

void CropWidget::setCropRect(const QRect &rect)
{
    m_cropScene->setCropRect(rect);
}

void CropWidget::setImage(const QImage &image)
{
    const QRect rBefore = m_cropScene->fullRect();
    m_cropScene->setImage(image);
    const QRect rAfter = m_cropScene->fullRect();
    if (rBefore != rAfter) {
        m_xSpinBox->setMaximum(rAfter.width() - 1);
        m_ySpinBox->setMaximum(rAfter.height() - 1);
        m_widthSpinBox->setMaximum(rAfter.width());
        m_heightSpinBox->setMaximum(rAfter.height());
    }
    updateWidgets();
}

void CropWidget::updateWidgets()
{
    m_resetButton->setEnabled(!m_cropScene->fullySelected());
    const QRect r = m_cropScene->cropRect();
    m_warningIcon->setCropSize(r.size());
}

void CropWidget::onSpinBoxChanged()
{
    const QRect spinBoxRect = {
        m_xSpinBox->value(),
        m_ySpinBox->value(),
        m_widthSpinBox->value(),
        m_heightSpinBox->value()
    };
    m_cropScene->setCropRect(spinBoxRect.intersected(m_cropScene->fullRect()));
}

void CropWidget::onCropRectChanged()
{
    const QRect rect = m_cropScene->cropRect();
    const struct { QSpinBox *spinBox; int value; } updates[] = {
                   {m_xSpinBox, rect.x()},
                   {m_ySpinBox, rect.y()},
                   {m_widthSpinBox, rect.width()},
                   {m_heightSpinBox, rect.height()},
                   };
    for (const auto &update : updates) {
        update.spinBox->blockSignals(true);
        update.spinBox->setValue(update.value);
        update.spinBox->blockSignals(false);
    }
    updateWidgets();
}

class SelectionSlider : public QSlider
{
public:
    explicit SelectionSlider(QWidget *parent = nullptr);

    void setSelectionRange(FrameRange range);

protected:
    void paintEvent(QPaintEvent *) override;

private:
    FrameRange m_range;
};

SelectionSlider::SelectionSlider(QWidget *parent)
    : QSlider(Qt::Horizontal, parent)
    , m_range({-1, -1})
{
    setPageStep(50);
}

void SelectionSlider::setSelectionRange(FrameRange range)
{
    m_range = range;
    update();
}

void SelectionSlider::paintEvent(QPaintEvent *)
{
    QStyleOptionSlider opt;
    initStyleOption(&opt);
    opt.subControls = QStyle::SC_SliderHandle; // Draw only the handle. We draw the rest, here.

    const int tickOffset = style()->pixelMetric(QStyle::PM_SliderTickmarkOffset, &opt, this);
    QRect grooveRect = style()->subControlRect(QStyle::CC_Slider, &opt,
                                               QStyle::SC_SliderGroove, this)
                           .adjusted(tickOffset, 0, -tickOffset, 0);
    grooveRect.setTop(rect().top());
    grooveRect.setBottom(rect().bottom());
    const QColor bgColor = palette().window().color();
    const QColor fgColor = palette().text().color();
    const QColor grooveColor = StyleHelper::mergedColors(bgColor, fgColor, 80);
    const QColor selectionColor = StyleHelper::mergedColors(bgColor, fgColor, 45);
    QPainter p(this);
    p.fillRect(grooveRect, grooveColor);
    const qreal pixelsPerFrame = grooveRect.width() / qreal(maximum());
    const int startPixels = int(m_range.first * pixelsPerFrame);
    const int endPixels = int((maximum() - m_range.second) * pixelsPerFrame);
    p.fillRect(grooveRect.adjusted(startPixels, 0, -endPixels, 0), selectionColor);
    style()->drawComplexControl(QStyle::CC_Slider, &opt, &p, this);
}

class TrimWidget : public QWidget
{
    Q_OBJECT

public:
    explicit TrimWidget(const ClipInfo &clip, QWidget *parent = nullptr);

    void setCurrentFrame(int frame);
    int currentFrame() const;
    void setTrimRange(FrameRange range);
    FrameRange trimRange() const;

signals:
    void positionChanged();
    void trimRangeChanged(FrameRange range);

private:
    void resetTrimRange();
    void updateTrimWidgets();
    void emitTrimRangeChange();

    ClipInfo m_clipInfo;
    SelectionSlider *m_frameSlider;
    TimeLabel *m_currentTime;
    TimeLabel *m_clipDuration;

    struct {
        QPushButton *button;
        TimeLabel *timeLabel;
    } m_trimStart, m_trimEnd;
    TimeLabel *m_trimRange;
    QToolButton *m_trimResetButton;
};

TrimWidget::TrimWidget(const ClipInfo &clip, QWidget *parent)
    : QWidget(parent)
    , m_clipInfo(clip)
{
    m_frameSlider = new SelectionSlider;

    m_currentTime = new TimeLabel(m_clipInfo);

    m_clipDuration = new TimeLabel(m_clipInfo);

    m_trimStart.button = new QPushButton(Tr::tr("Start:"));
    m_trimStart.timeLabel = new TimeLabel(m_clipInfo);

    m_trimEnd.button = new QPushButton(Tr::tr("End:"));
    m_trimEnd.timeLabel = new TimeLabel(m_clipInfo);

    m_trimRange = new TimeLabel(m_clipInfo);

    m_trimResetButton = new QToolButton;
    m_trimResetButton->setIcon(Icons::RESET.icon());

    using namespace Layouting;
    Column {
        Row { m_frameSlider, m_currentTime, "/", m_clipDuration },
        Group {
            title(Tr::tr("Trimming")),
            Row {
                m_trimStart.button, m_trimStart.timeLabel,
                Space(20),
                m_trimEnd.button, m_trimEnd.timeLabel,
                Stretch(), Space(20),
                Tr::tr("Range:"), m_trimRange,
                m_trimResetButton,
            },
        },
        noMargin(),
    }.attachTo(this);

    connect(m_frameSlider, &QSlider::valueChanged, this, [this]() {
        m_currentTime->setFrame(currentFrame());
        updateTrimWidgets();
        emit positionChanged();
    });
    connect(m_trimStart.button, &QPushButton::clicked, this, [this] (){
        m_trimStart.timeLabel->setFrame(currentFrame());
        updateTrimWidgets();
        emitTrimRangeChange();
    });
    connect(m_trimEnd.button, &QPushButton::clicked, this, [this] (){
        m_trimEnd.timeLabel->setFrame(currentFrame());
        updateTrimWidgets();
        emitTrimRangeChange();
    });
    connect(m_trimResetButton, &QToolButton::clicked, this, &TrimWidget::resetTrimRange);

    m_frameSlider->setMaximum(m_clipInfo.framesCount());
    m_currentTime->setFrame(currentFrame());
    m_clipDuration->setFrame(m_clipInfo.framesCount());
    resetTrimRange();
}

void TrimWidget::setCurrentFrame(int frame)
{
    m_frameSlider->setValue(frame);
}

int TrimWidget::currentFrame() const
{
    return m_frameSlider->value();
}

void TrimWidget::setTrimRange(FrameRange range)
{
    m_trimStart.timeLabel->setFrame(range.first);
    m_trimEnd.timeLabel->setFrame(range.second);
    m_frameSlider->setSelectionRange(trimRange());
}

FrameRange TrimWidget::trimRange() const
{
    return { m_trimStart.timeLabel->frame(), m_trimEnd.timeLabel->frame() };
}

void TrimWidget::resetTrimRange()
{
    setTrimRange({0, m_clipInfo.framesCount()});
    emitTrimRangeChange();
    updateTrimWidgets();
}

void TrimWidget::updateTrimWidgets()
{
    const int current = currentFrame();
    const int trimStart = m_trimStart.timeLabel->frame();
    const int trimEnd = m_trimEnd.timeLabel->frame();
    m_trimStart.button->setEnabled(current < m_clipInfo.framesCount() && current < trimEnd);
    m_trimEnd.button->setEnabled(current > 0 && current > trimStart);
    m_trimRange->setFrame(trimEnd - trimStart);
    m_frameSlider->setSelectionRange(trimRange());
    m_trimResetButton->setEnabled(!m_clipInfo.isCompleteRange(trimRange()));
}

void TrimWidget::emitTrimRangeChange()
{
    emit trimRangeChanged(trimRange());
}

class CropAndTrimDialog : public QDialog
{
public:
    explicit CropAndTrimDialog(const ClipInfo &clip, QWidget *parent = nullptr);

    void setCropRect(const QRect &rect);
    QRect cropRect() const;
    void setTrimRange(FrameRange range);
    FrameRange trimRange() const;

    int currentFrame() const;
    void setCurrentFrame(int frame);

private:
    void onSeekPositionChanged();
    void startFrameFetch();

    ClipInfo m_clipInfo;
    CropWidget *m_cropWidget;
    TrimWidget *m_trimWidget;
    QImage m_previewImage;

    Process *m_process;
    int m_nextFetchFrame = -1;
};

CropAndTrimDialog::CropAndTrimDialog(const ClipInfo &clip, QWidget *parent)
    : QDialog(parent, Qt::Window)
    , m_clipInfo(clip)
{
    setWindowTitle(Tr::tr("Crop and Trim"));

    m_cropWidget = new CropWidget;

    m_trimWidget = new TrimWidget(m_clipInfo);

    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    using namespace Layouting;
    Column {
        Group {
            title("Cropping"),
            Column { m_cropWidget },
        },
        Space(16),
        m_trimWidget,
        buttonBox,
    }.attachTo(this);

    m_process = new Process(this);
    connect(m_process, &Process::done, this, [this] {
        if (m_process->exitCode() != 0) {
            FFmpegUtils::reportError(m_process->commandLine(),
                                     m_process->readAllRawStandardError());
            return;
        }
        const QByteArray &imageData = m_process->rawStdOut();
        startFrameFetch();
        if (imageData.isEmpty())
            return;
        m_previewImage = QImage(reinterpret_cast<const uchar*>(imageData.constData()),
                                m_clipInfo.dimensions.width(), m_clipInfo.dimensions.height(),
                                QImage::Format_RGB32);
        m_previewImage.detach();
        m_cropWidget->setImage(m_previewImage);
    });
    connect(m_trimWidget, &TrimWidget::positionChanged,
            this, &CropAndTrimDialog::onSeekPositionChanged);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    onSeekPositionChanged();
    resize(1000, 800);
}

void CropAndTrimDialog::onSeekPositionChanged()
{
    // -1, because frame numbers are 0-based
    m_nextFetchFrame = qMin(m_trimWidget->currentFrame(), m_clipInfo.framesCount() - 1);
    if (!m_process->isRunning())
        startFrameFetch();
}

void CropAndTrimDialog::startFrameFetch()
{
    if (m_nextFetchFrame == -1)
        return;

    const CommandLine cl = {
        Internal::settings().ffmpegTool(),
        {
            "-v", "error",
            "-ss", m_clipInfo.timeStamp(m_nextFetchFrame),
            "-i", m_clipInfo.file.toUserOutput(),
            "-threads", "1",
            "-frames:v", "1",
            "-f", "rawvideo",
            "-pix_fmt", "bgra",
            "-"
        }
    };
    m_process->close();
    m_nextFetchFrame = -1;
    m_process->setCommand(cl);
    m_process->setWorkingDirectory(Internal::settings().ffmpegTool().parentDir());
    m_process->start();
}

void CropAndTrimDialog::setCropRect(const QRect &rect)
{
    m_cropWidget->setCropRect(rect);
}

QRect CropAndTrimDialog::cropRect() const
{
    return m_cropWidget->cropRect();
}

void CropAndTrimDialog::setTrimRange(FrameRange range)
{
    m_trimWidget->setTrimRange(range);
}

FrameRange CropAndTrimDialog::trimRange() const
{
    return m_trimWidget->trimRange();
}

int CropAndTrimDialog::currentFrame() const
{
    return m_trimWidget->currentFrame();
}

void CropAndTrimDialog::setCurrentFrame(int frame)
{
    m_trimWidget->setCurrentFrame(frame);
}

CropAndTrimWidget::CropAndTrimWidget(QWidget *parent)
    : StyledBar(parent)
{
    m_button = new QToolButton;
    m_button->setText(Tr::tr("Crop and Trim..."));

    m_cropSizeWarningIcon = new CropSizeWarningIcon(CropSizeWarningIcon::ToolBarVariant);

    using namespace Layouting;
    Row {
        m_button,
        m_cropSizeWarningIcon,
        noMargin(), spacing(0),
    }.attachTo(this);

    connect(m_button, &QPushButton::clicked, this, [this] {
        CropAndTrimDialog dlg(m_clipInfo, Core::ICore::dialogParent());
        dlg.setCropRect(m_cropRect);
        dlg.setTrimRange(m_trimRange);
        dlg.setCurrentFrame(m_currentFrame);
        if (dlg.exec() == QDialog::Accepted) {
            m_cropRect = dlg.cropRect();
            m_trimRange = dlg.trimRange();
            m_currentFrame = dlg.currentFrame();
            emit cropRectChanged(m_cropRect);
            emit trimRangeChanged(m_trimRange);
            updateWidgets();
        }
    });

    updateWidgets();
}

void CropAndTrimWidget::setClip(const ClipInfo &clip)
{
    if (clip.dimensions != m_clipInfo.dimensions)
        m_cropRect = {QPoint(), clip.dimensions}; // Reset only if clip size changed
    m_clipInfo = clip;
    m_currentFrame = 0;
    m_trimRange = {m_currentFrame, m_clipInfo.framesCount()};
    updateWidgets();
}

void CropAndTrimWidget::updateWidgets()
{
    if (!m_clipInfo.isNull()) {
        const QString cropText =
            !m_clipInfo.isCompleteArea(m_cropRect)
                ? Tr::tr("Crop to %1x%2px.").arg(m_cropRect.width()).arg(m_cropRect.height())
                : Tr::tr("Complete area.");

        const QString trimText =
            !m_clipInfo.isCompleteRange(m_trimRange)
                ? Tr::tr("Frames %1 to %2.").arg(m_trimRange.first).arg(m_trimRange.second)
                : Tr::tr("Complete clip.");

        m_button->setToolTip(cropText + " " + trimText);
    }

    m_cropSizeWarningIcon->setCropSize(m_cropRect.size());
}

} // namespace ScreenRecorder

#include "cropandtrim.moc"