Browse Source

Fix the encoder, add rectangles, and add undraw

undraw = way to delete drawings
tags/v5.0.0
ArsenArsen 5 years ago
parent
commit
d0eaec047a
  1. 8
      KShare.pro
  2. 14
      cropeditor/cropscene.cpp
  3. 6
      cropeditor/cropscene.hpp
  4. 16
      cropeditor/drawing/eraseritem.cpp
  5. 18
      cropeditor/drawing/eraseritem.hpp
  6. 23
      cropeditor/drawing/rectitem.cpp
  7. 21
      cropeditor/drawing/rectitem.hpp
  8. 4
      cropeditor/drawing/textitem.cpp
  9. 10
      recording/encoders/encoder.cpp
  10. 3
      recording/encoders/encoder.hpp
  11. 6
      recording/recordingformats.cpp

8
KShare.pro

@ -59,7 +59,9 @@ SOURCES += main.cpp\
recording/encoders/encodersettings.cpp \
recording/encoders/encodersettingsdialog.cpp \
settingsdialog.cpp \
aboutbox.cpp
aboutbox.cpp \
cropeditor/drawing/eraseritem.cpp \
cropeditor/drawing/rectitem.cpp
HEADERS += mainwindow.hpp \
cropeditor/cropeditor.hpp \
@ -97,7 +99,9 @@ HEADERS += mainwindow.hpp \
recording/encoders/encodersettings.hpp \
recording/encoders/encodersettingsdialog.hpp \
settingsdialog.hpp \
aboutbox.hpp
aboutbox.hpp \
cropeditor/drawing/eraseritem.hpp \
cropeditor/drawing/rectitem.hpp
LIBS += -lavcodec -lavformat -lavutil -lswscale -lavutil

14
cropeditor/cropscene.cpp

@ -8,8 +8,10 @@
#include <QTimer>
#include <cropeditor/drawing/bluritem.hpp>
#include <cropeditor/drawing/dotitem.hpp>
#include <cropeditor/drawing/eraseritem.hpp>
#include <cropeditor/drawing/lineitem.hpp>
#include <cropeditor/drawing/pathitem.hpp>
#include <cropeditor/drawing/rectitem.hpp>
#include <cropeditor/drawing/textitem.hpp>
#include <cropeditor/settings/brushpenselection.hpp>
#include <functional>
@ -28,10 +30,22 @@ CropScene::CropScene(QObject *parent, QPixmap *pixmap)
addDrawingAction(menu, "Blur", [] { return new BlurItem; });
addDrawingAction(menu, "Straight line", [] { return new LineItem; });
addDrawingAction(menu, "Text", [] { return new TextItem; });
addDrawingAction(menu, "Rectangle", [] { return new RectItem; });
menu.addSeparator();
addDrawingAction(menu, "Eraser", [] { return new EraserItem; });
QAction *reset = menu.addAction("Reset");
connect(reset, &QAction::triggered, [&] { setDrawingSelection("None", [] { return nullptr; }); });
QAction *clear = menu.addAction("Clear all drwawing");
connect(clear, &QAction::triggered, [&] {
auto its = items();
for (auto i : its) {
if (i != rect && i != polyItem && i->zValue() != -1) removeItem(i);
}
});
menu.addSeparator();
QAction *settings = new QAction;
settings->setText("Settings");

6
cropeditor/cropscene.hpp

@ -25,6 +25,12 @@ public:
QPixmap *pixmap() {
return _pixmap;
}
QGraphicsPolygonItem *polyItm() {
return polyItem;
}
QGraphicsRectItem *selRect() {
return rect;
}
public slots:
void fontAsk();

16
cropeditor/drawing/eraseritem.cpp

@ -0,0 +1,16 @@
#include "eraseritem.hpp"
EraserItem::EraserItem() {
}
EraserItem::~EraserItem() {
}
void EraserItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
for (auto i : scene->items()) {
if (i->contains(e->scenePos()) && i->zValue() != -1 && i != scene->polyItm() && i != scene->selRect()) {
scene->removeItem(i);
break;
}
}
}

18
cropeditor/drawing/eraseritem.hpp

@ -0,0 +1,18 @@
#ifndef ERASERITEM_HPP
#define ERASERITEM_HPP
#include "drawitem.hpp"
class EraserItem : public DrawItem {
public:
EraserItem();
~EraserItem();
QString name() override {
return "Eraser";
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override {
}
};
#endif // ERASERITEM_HPP

23
cropeditor/drawing/rectitem.cpp

@ -0,0 +1,23 @@
#include "rectitem.hpp"
RectItem::RectItem() {
}
RectItem::~RectItem() {
}
void RectItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
if (!rect) {
rect = scene->addRect(e->scenePos().x(), e->scenePos().y(), 0, 0);
rect->setBrush(scene->brush());
rect->setPen(scene->pen());
initPos = e->scenePos();
} else {
auto p = e->scenePos();
rect->setRect(
QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()), qAbs(initPos.y() - p.y())));
}
}
void RectItem::mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) {
}

21
cropeditor/drawing/rectitem.hpp

@ -0,0 +1,21 @@
#ifndef RECTITEM_HPP
#define RECTITEM_HPP
#include "drawitem.hpp"
class RectItem : public DrawItem {
public:
RectItem();
~RectItem();
QString name() override {
return "Rectangle";
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override;
private:
QGraphicsRectItem *rect = nullptr;
QPointF initPos;
};
#endif // RECTITEM_HPP

4
cropeditor/drawing/textitem.cpp

@ -12,8 +12,8 @@ void TextItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
if (!textItem) {
textItem = scene->addSimpleText(text, scene->font());
textItem->setPos(e->scenePos());
textItem->setPen(scene->pen());
textItem->setBrush(scene->brush());
textItem->setPen(scene->pen().color());
textItem->setBrush(scene->pen().color());
} else {
auto ee
= 180 + qRadiansToDegrees(qAtan2((textItem->pos().y() - e->scenePos().y()), (textItem->pos().x() - e->scenePos().x())));

10
recording/encoders/encoder.cpp

@ -87,10 +87,11 @@ Encoder::Encoder(QString &targetFile, QSize res, CodecSettings *settings) {
success = true;
}
void Encoder::setFrameRGB(uint8_t *rgb) {
void Encoder::setFrameRGB(QImage img) {
uint8_t *rgb = (uint8_t *)img.bits();
int ret = av_frame_make_writable(out->frame);
if (ret < 0) throwAVErr(ret, "setFrameRGB");
int lineSize[1] = { 3 * out->enc->width };
int lineSize[1] = { img.bytesPerLine() };
out->sws = sws_getCachedContext(out->sws, out->enc->width, out->enc->height, AV_PIX_FMT_RGB24, out->enc->width,
out->enc->height, (AVPixelFormat)out->frame->format, 0, 0, 0, 0);
sws_scale(out->sws, (const uint8_t *const *)&rgb, lineSize, 0, out->enc->height, out->frame->data, out->frame->linesize);
@ -105,8 +106,7 @@ bool Encoder::addFrame(QImage frm) {
if (!success) return false;
if (frm.size() != size) frm = frm.copy(QRect(QPoint(0, 0), size));
if (frm.format() != QImage::Format_RGB888) frm = frm.convertToFormat(QImage::Format_RGB888);
uint8_t *frameData = (uint8_t *)frm.bits();
setFrameRGB(frameData);
setFrameRGB(frm);
AVPacket pkt;
pkt.size = 0;
pkt.data = NULL;
@ -140,6 +140,8 @@ bool Encoder::isRunning() {
}
bool Encoder::end() {
if (ended) return false;
ended = true;
if (!success) {
goto cleanup;
}

3
recording/encoders/encoder.hpp

@ -45,10 +45,11 @@ private:
AVFormatContext *fc = NULL;
bool success = false;
bool ended = false;
QSize size;
void setFrameRGB(uint8_t *rgb);
void setFrameRGB(QImage img);
};
#endif // ENCODER_HPP

6
recording/recordingformats.cpp

@ -30,6 +30,12 @@ RecordingFormats::RecordingFormats(formats::Recording f) {
iFormat = QImage::Format_RGB888;
path = tmpDir.absoluteFilePath("res." + formats::recordingFormatName(f).toLower());
finalizer = [&] {
qDebug() << "end";
try {
enc->end();
} catch (std::runtime_error e) {
qCritical() << "Encoder error: " << e.what();
}
delete enc;
if (interrupt) {
tmpDir.removeRecursively();

Loading…
Cancel
Save