Text widget

This commit is contained in:
ArsenArsen 2017-05-05 23:59:39 +02:00
parent b506a5d92c
commit 905f9b27d1
5 changed files with 98 additions and 51 deletions

View File

@ -44,7 +44,8 @@ SOURCES += main.cpp\
cropeditor/drawing/bluritem.cpp \ cropeditor/drawing/bluritem.cpp \
cropeditor/settings/blurdialog.cpp \ cropeditor/settings/blurdialog.cpp \
cropeditor/drawing/pathitem.cpp \ cropeditor/drawing/pathitem.cpp \
cropeditor/drawing/lineitem.cpp cropeditor/drawing/lineitem.cpp \
cropeditor/drawing/textitem.cpp
HEADERS += mainwindow.hpp \ HEADERS += mainwindow.hpp \
cropeditor/cropeditor.hpp \ cropeditor/cropeditor.hpp \
@ -68,7 +69,8 @@ HEADERS += mainwindow.hpp \
cropeditor/drawing/bluritem.hpp \ cropeditor/drawing/bluritem.hpp \
cropeditor/settings/blurdialog.hpp \ cropeditor/settings/blurdialog.hpp \
cropeditor/drawing/pathitem.hpp \ cropeditor/drawing/pathitem.hpp \
cropeditor/drawing/lineitem.hpp cropeditor/drawing/lineitem.hpp \
cropeditor/drawing/textitem.hpp
FORMS += mainwindow.ui \ FORMS += mainwindow.ui \
cropeditor/settings/brushpenselection.ui \ cropeditor/settings/brushpenselection.ui \

View File

@ -1,6 +1,7 @@
#include "cropscene.hpp" #include "cropscene.hpp"
#include <QColorDialog> #include <QColorDialog>
#include <QDebug> #include <QDebug>
#include <QFontDialog>
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsView> #include <QGraphicsView>
@ -10,13 +11,14 @@
#include <cropeditor/drawing/dotitem.hpp> #include <cropeditor/drawing/dotitem.hpp>
#include <cropeditor/drawing/lineitem.hpp> #include <cropeditor/drawing/lineitem.hpp>
#include <cropeditor/drawing/pathitem.hpp> #include <cropeditor/drawing/pathitem.hpp>
#include <cropeditor/drawing/textitem.hpp>
#include <cropeditor/settings/brushpenselection.hpp> #include <cropeditor/settings/brushpenselection.hpp>
#include <functional> #include <functional>
#include <settings.hpp> #include <settings.hpp>
CropScene::CropScene(QObject *parent, QPixmap *pixmap) CropScene::CropScene(QObject *parent, QPixmap *pixmap)
: QGraphicsScene(parent), prevButtons(Qt::NoButton), drawingSelectionMaker([] { return nullptr; }) : QGraphicsScene(parent), prevButtons(Qt::NoButton), drawingSelectionMaker([] { return nullptr; }),
{ _font(settings::settings().value("font", QFont()).value<QFont>()) {
pen().setColor(settings::settings().value("penColor", pen().color()).value<QColor>()); pen().setColor(settings::settings().value("penColor", pen().color()).value<QColor>());
pen().setCosmetic(settings::settings().value("penCosmetic", pen().isCosmetic()).toBool()); pen().setCosmetic(settings::settings().value("penCosmetic", pen().isCosmetic()).toBool());
pen().setWidthF(settings::settings().value("penWidth", pen().widthF()).toFloat()); pen().setWidthF(settings::settings().value("penWidth", pen().widthF()).toFloat());
@ -26,6 +28,7 @@ CropScene::CropScene(QObject *parent, QPixmap *pixmap)
addDrawingAction(menu, "Path", [] { return new PathItem; }); addDrawingAction(menu, "Path", [] { return new PathItem; });
addDrawingAction(menu, "Blur", [] { return new BlurItem; }); addDrawingAction(menu, "Blur", [] { return new BlurItem; });
addDrawingAction(menu, "Straight line", [] { return new LineItem; }); addDrawingAction(menu, "Straight line", [] { return new LineItem; });
addDrawingAction(menu, "Text", [] { return new TextItem; });
QAction *reset = menu.addAction("Reset"); QAction *reset = menu.addAction("Reset");
connect(reset, &QAction::triggered, [&] { setDrawingSelection("None", [] { return nullptr; }); }); connect(reset, &QAction::triggered, [&] { setDrawingSelection("None", [] { return nullptr; }); });
@ -39,6 +42,8 @@ CropScene::CropScene(QObject *parent, QPixmap *pixmap)
connect(settings, &QAction::triggered, [&] { BrushPenSelection(this).exec(); }); connect(settings, &QAction::triggered, [&] { BrushPenSelection(this).exec(); });
menu.addAction(settings); menu.addAction(settings);
connect(menu.addAction("Set Font"), &QAction::triggered, this, &CropScene::fontAsk);
_pixmap = pixmap; _pixmap = pixmap;
QTimer::singleShot(0, [&] { QTimer::singleShot(0, [&] {
QPolygonF poly; QPolygonF poly;
@ -53,59 +58,54 @@ CropScene::CropScene(QObject *parent, QPixmap *pixmap)
}); });
} }
CropScene::~CropScene() CropScene::~CropScene() {
{
delete drawingSelection; delete drawingSelection;
} }
QPen &CropScene::pen() QPen &CropScene::pen() {
{
return _pen; return _pen;
} }
QBrush &CropScene::brush() QBrush &CropScene::brush() {
{
return _brush; return _brush;
} }
void CropScene::setDrawingSelection(QString name, std::function<DrawItem *()> drawAction) QFont &CropScene::font() {
{ return _font;
}
void CropScene::setDrawingSelection(QString name, std::function<DrawItem *()> drawAction) {
drawingSelectionMaker = drawAction; drawingSelectionMaker = drawAction;
drawingSelection = drawAction(); drawingSelection = drawAction();
drawingName = name; drawingName = name;
if (drawingSelection) drawingSelection->init(this); if (drawingSelection) drawingSelection->init(this);
} }
void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) void CropScene::fontAsk() {
{ bool ok = false;
auto buttons = e->buttons(); QFont font = QFontDialog::getFont(&ok, this->font(), nullptr, "Font to use");
if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton) if (ok) _font = font;
{
if (drawingSelection)
{
drawingSelection->mouseDragEvent(e, this);
} }
else
{ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
auto buttons = e->buttons();
if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton) {
if (drawingSelection) {
drawingSelection->mouseDragEvent(e, this);
} else {
QPointF p = e->scenePos(); QPointF p = e->scenePos();
if (rect == nullptr) if (rect == nullptr) {
{
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1); rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1);
initPos = p; initPos = p;
QPen pen(Qt::NoBrush, 1); QPen pen(Qt::NoBrush, 1);
pen.setColor(Qt::cyan); pen.setColor(Qt::cyan);
rect->setPen(pen); rect->setPen(pen);
addItem(rect); addItem(rect);
} } else {
else if (prevButtons == Qt::NoButton) {
{
if (prevButtons == Qt::NoButton)
{
initPos = p; initPos = p;
rect->setRect(p.x(), p.y(), 1, 1); rect->setRect(p.x(), p.y(), 1, 1);
} } else {
else
{
rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()), rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()),
qAbs(initPos.y() - p.y()))); qAbs(initPos.y() - p.y())));
} }
@ -132,11 +132,9 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
prevButtons = buttons; prevButtons = buttons;
} }
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
{
qDebug() << "release"; qDebug() << "release";
if (drawingSelection) if (drawingSelection) {
{
drawingSelection->mouseDragEndEvent(e, this); drawingSelection->mouseDragEndEvent(e, this);
delete drawingSelection; delete drawingSelection;
drawingSelection = drawingSelectionMaker(); drawingSelection = drawingSelectionMaker();
@ -145,30 +143,25 @@ void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
prevButtons = Qt::NoButton; prevButtons = Qt::NoButton;
} }
void CropScene::addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item) void CropScene::addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item) {
{
QAction *action = new QAction; QAction *action = new QAction;
action->setText(name); action->setText(name);
connect(action, &QAction::triggered, [this, item, name](bool) { setDrawingSelection(name, item); }); connect(action, &QAction::triggered, [this, item, name](bool) { setDrawingSelection(name, item); });
menu.addAction(action); menu.addAction(action);
} }
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) {
{
display->setText(drawingName); display->setText(drawingName);
menu.exec(e->screenPos()); menu.exec(e->screenPos());
e->accept(); e->accept();
} }
void CropScene::keyReleaseEvent(QKeyEvent *event) void CropScene::keyReleaseEvent(QKeyEvent *event) {
{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) done(); // Segfault if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) done(); // Segfault
} }
void CropScene::done() void CropScene::done() {
{ if (rect) {
if (rect)
{
rect->setPen(QPen(Qt::NoPen)); rect->setPen(QPen(Qt::NoPen));
emit closedWithRect(rect->rect().toRect()); emit closedWithRect(rect->rect().toRect());
} }

View File

@ -1,6 +1,7 @@
#ifndef CROPSCENE_HPP #ifndef CROPSCENE_HPP
#define CROPSCENE_HPP #define CROPSCENE_HPP
#include <QFont>
#include <QGraphicsRectItem> #include <QGraphicsRectItem>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
@ -12,20 +13,22 @@ class CropScene;
#include <cropeditor/drawing/drawitem.hpp> #include <cropeditor/drawing/drawitem.hpp>
class CropScene : public QGraphicsScene class CropScene : public QGraphicsScene {
{
Q_OBJECT Q_OBJECT
public: public:
CropScene(QObject *parent, QPixmap *pixmap); CropScene(QObject *parent, QPixmap *pixmap);
~CropScene(); ~CropScene();
QPen &pen(); QPen &pen();
QBrush &brush(); QBrush &brush();
QFont &font();
void setDrawingSelection(QString name, std::function<DrawItem *()> drawAction); void setDrawingSelection(QString name, std::function<DrawItem *()> drawAction);
QPixmap *pixmap() QPixmap *pixmap() {
{
return _pixmap; return _pixmap;
} }
public slots:
void fontAsk();
signals: signals:
void closedWithRect(QRect rect); void closedWithRect(QRect rect);
@ -39,14 +42,15 @@ class CropScene : public QGraphicsScene
private: private:
void addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item); void addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item);
void done(); void done();
QPixmap *_pixmap; std::function<DrawItem *()> drawingSelectionMaker;
QFlags<Qt::MouseButton> prevButtons; QFlags<Qt::MouseButton> prevButtons;
QPixmap *_pixmap;
QGraphicsRectItem *rect = nullptr; QGraphicsRectItem *rect = nullptr;
QPointF initPos; QPointF initPos;
QPen _pen; QPen _pen;
QBrush _brush; QBrush _brush;
QFont _font;
QGraphicsPolygonItem *polyItem = nullptr; QGraphicsPolygonItem *polyItem = nullptr;
std::function<DrawItem *()> drawingSelectionMaker;
DrawItem *drawingSelection = nullptr; DrawItem *drawingSelection = nullptr;
QMenu menu; QMenu menu;
QString drawingName = "None"; QString drawingName = "None";

View File

@ -0,0 +1,29 @@
#include "textitem.hpp"
#include <QDebug>
#include <QInputDialog>
#include <QtMath>
void TextItem::init(CropScene *) {
text = QInputDialog::getText(nullptr, "Text to add", "Input");
}
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());
} else {
auto ee
= 180 + qRadiansToDegrees(qAtan2((textItem->pos().y() - e->scenePos().y()), (textItem->pos().x() - e->scenePos().x())));
textItem->setRotation(ee);
qDebug() << ee;
}
}
void TextItem::mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) {
}
QString TextItem::name() {
return "Text";
}

View File

@ -0,0 +1,19 @@
#ifndef TEXTITEM_HPP
#define TEXTITEM_HPP
#include "drawitem.hpp"
#include <QGraphicsSimpleTextItem>
class TextItem : public DrawItem {
public:
QString name() override;
void init(CropScene *) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override;
private:
QGraphicsSimpleTextItem *textItem = nullptr;
QString text;
};
#endif // TEXTITEM_HPP