Text widget
This commit is contained in:
parent
b506a5d92c
commit
905f9b27d1
@ -44,7 +44,8 @@ SOURCES += main.cpp\
|
||||
cropeditor/drawing/bluritem.cpp \
|
||||
cropeditor/settings/blurdialog.cpp \
|
||||
cropeditor/drawing/pathitem.cpp \
|
||||
cropeditor/drawing/lineitem.cpp
|
||||
cropeditor/drawing/lineitem.cpp \
|
||||
cropeditor/drawing/textitem.cpp
|
||||
|
||||
HEADERS += mainwindow.hpp \
|
||||
cropeditor/cropeditor.hpp \
|
||||
@ -68,7 +69,8 @@ HEADERS += mainwindow.hpp \
|
||||
cropeditor/drawing/bluritem.hpp \
|
||||
cropeditor/settings/blurdialog.hpp \
|
||||
cropeditor/drawing/pathitem.hpp \
|
||||
cropeditor/drawing/lineitem.hpp
|
||||
cropeditor/drawing/lineitem.hpp \
|
||||
cropeditor/drawing/textitem.hpp
|
||||
|
||||
FORMS += mainwindow.ui \
|
||||
cropeditor/settings/brushpenselection.ui \
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "cropscene.hpp"
|
||||
#include <QColorDialog>
|
||||
#include <QDebug>
|
||||
#include <QFontDialog>
|
||||
#include <QGraphicsPolygonItem>
|
||||
#include <QGraphicsSceneContextMenuEvent>
|
||||
#include <QGraphicsView>
|
||||
@ -10,13 +11,14 @@
|
||||
#include <cropeditor/drawing/dotitem.hpp>
|
||||
#include <cropeditor/drawing/lineitem.hpp>
|
||||
#include <cropeditor/drawing/pathitem.hpp>
|
||||
#include <cropeditor/drawing/textitem.hpp>
|
||||
#include <cropeditor/settings/brushpenselection.hpp>
|
||||
#include <functional>
|
||||
#include <settings.hpp>
|
||||
|
||||
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().setCosmetic(settings::settings().value("penCosmetic", pen().isCosmetic()).toBool());
|
||||
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, "Blur", [] { return new BlurItem; });
|
||||
addDrawingAction(menu, "Straight line", [] { return new LineItem; });
|
||||
addDrawingAction(menu, "Text", [] { return new TextItem; });
|
||||
|
||||
QAction *reset = menu.addAction("Reset");
|
||||
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(); });
|
||||
menu.addAction(settings);
|
||||
|
||||
connect(menu.addAction("Set Font"), &QAction::triggered, this, &CropScene::fontAsk);
|
||||
|
||||
_pixmap = pixmap;
|
||||
QTimer::singleShot(0, [&] {
|
||||
QPolygonF poly;
|
||||
@ -53,59 +58,54 @@ CropScene::CropScene(QObject *parent, QPixmap *pixmap)
|
||||
});
|
||||
}
|
||||
|
||||
CropScene::~CropScene()
|
||||
{
|
||||
CropScene::~CropScene() {
|
||||
delete drawingSelection;
|
||||
}
|
||||
|
||||
QPen &CropScene::pen()
|
||||
{
|
||||
QPen &CropScene::pen() {
|
||||
return _pen;
|
||||
}
|
||||
|
||||
QBrush &CropScene::brush()
|
||||
{
|
||||
QBrush &CropScene::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;
|
||||
drawingSelection = drawAction();
|
||||
drawingName = name;
|
||||
if (drawingSelection) drawingSelection->init(this);
|
||||
}
|
||||
|
||||
void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
|
||||
{
|
||||
auto buttons = e->buttons();
|
||||
if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton)
|
||||
{
|
||||
if (drawingSelection)
|
||||
{
|
||||
drawingSelection->mouseDragEvent(e, this);
|
||||
void CropScene::fontAsk() {
|
||||
bool ok = false;
|
||||
QFont font = QFontDialog::getFont(&ok, this->font(), nullptr, "Font to use");
|
||||
if (ok) _font = font;
|
||||
}
|
||||
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();
|
||||
if (rect == nullptr)
|
||||
{
|
||||
if (rect == nullptr) {
|
||||
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1);
|
||||
initPos = p;
|
||||
QPen pen(Qt::NoBrush, 1);
|
||||
pen.setColor(Qt::cyan);
|
||||
rect->setPen(pen);
|
||||
addItem(rect);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (prevButtons == Qt::NoButton)
|
||||
{
|
||||
} else {
|
||||
if (prevButtons == Qt::NoButton) {
|
||||
initPos = p;
|
||||
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()),
|
||||
qAbs(initPos.y() - p.y())));
|
||||
}
|
||||
@ -132,11 +132,9 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
|
||||
prevButtons = buttons;
|
||||
}
|
||||
|
||||
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
|
||||
{
|
||||
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
|
||||
qDebug() << "release";
|
||||
if (drawingSelection)
|
||||
{
|
||||
if (drawingSelection) {
|
||||
drawingSelection->mouseDragEndEvent(e, this);
|
||||
delete drawingSelection;
|
||||
drawingSelection = drawingSelectionMaker();
|
||||
@ -145,30 +143,25 @@ void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
|
||||
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;
|
||||
action->setText(name);
|
||||
connect(action, &QAction::triggered, [this, item, name](bool) { setDrawingSelection(name, item); });
|
||||
menu.addAction(action);
|
||||
}
|
||||
|
||||
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
|
||||
{
|
||||
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) {
|
||||
display->setText(drawingName);
|
||||
menu.exec(e->screenPos());
|
||||
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
|
||||
}
|
||||
|
||||
void CropScene::done()
|
||||
{
|
||||
if (rect)
|
||||
{
|
||||
void CropScene::done() {
|
||||
if (rect) {
|
||||
rect->setPen(QPen(Qt::NoPen));
|
||||
emit closedWithRect(rect->rect().toRect());
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#ifndef CROPSCENE_HPP
|
||||
#define CROPSCENE_HPP
|
||||
|
||||
#include <QFont>
|
||||
#include <QGraphicsRectItem>
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsSceneContextMenuEvent>
|
||||
@ -12,20 +13,22 @@ class CropScene;
|
||||
|
||||
#include <cropeditor/drawing/drawitem.hpp>
|
||||
|
||||
class CropScene : public QGraphicsScene
|
||||
{
|
||||
class CropScene : public QGraphicsScene {
|
||||
Q_OBJECT
|
||||
public:
|
||||
CropScene(QObject *parent, QPixmap *pixmap);
|
||||
~CropScene();
|
||||
QPen &pen();
|
||||
QBrush &brush();
|
||||
QFont &font();
|
||||
void setDrawingSelection(QString name, std::function<DrawItem *()> drawAction);
|
||||
QPixmap *pixmap()
|
||||
{
|
||||
QPixmap *pixmap() {
|
||||
return _pixmap;
|
||||
}
|
||||
|
||||
public slots:
|
||||
void fontAsk();
|
||||
|
||||
signals:
|
||||
void closedWithRect(QRect rect);
|
||||
|
||||
@ -39,14 +42,15 @@ class CropScene : public QGraphicsScene
|
||||
private:
|
||||
void addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item);
|
||||
void done();
|
||||
QPixmap *_pixmap;
|
||||
std::function<DrawItem *()> drawingSelectionMaker;
|
||||
QFlags<Qt::MouseButton> prevButtons;
|
||||
QPixmap *_pixmap;
|
||||
QGraphicsRectItem *rect = nullptr;
|
||||
QPointF initPos;
|
||||
QPen _pen;
|
||||
QBrush _brush;
|
||||
QFont _font;
|
||||
QGraphicsPolygonItem *polyItem = nullptr;
|
||||
std::function<DrawItem *()> drawingSelectionMaker;
|
||||
DrawItem *drawingSelection = nullptr;
|
||||
QMenu menu;
|
||||
QString drawingName = "None";
|
||||
|
29
cropeditor/drawing/textitem.cpp
Normal file
29
cropeditor/drawing/textitem.cpp
Normal 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";
|
||||
}
|
19
cropeditor/drawing/textitem.hpp
Normal file
19
cropeditor/drawing/textitem.hpp
Normal 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
|
Loading…
Reference in New Issue
Block a user