diff --git a/KShare.pro b/KShare.pro index 43373a7..38a82d5 100644 --- a/KShare.pro +++ b/KShare.pro @@ -39,7 +39,9 @@ SOURCES += main.cpp\ uploaders/customuploader.cpp \ notifications.cpp \ hotkeying.cpp \ - cropeditor/drawing/dotitem.cpp + cropeditor/drawing/dotitem.cpp \ + cropeditor/drawing/lineitem.cpp \ + cropeditor/settings/brushpenselection.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -58,9 +60,12 @@ HEADERS += mainwindow.hpp \ notifications.hpp \ hotkeying.hpp \ cropeditor/drawing/drawitem.hpp \ - cropeditor/drawing/dotitem.hpp + cropeditor/drawing/dotitem.hpp \ + cropeditor/drawing/lineitem.hpp \ + cropeditor/settings/brushpenselection.hpp -FORMS += mainwindow.ui +FORMS += mainwindow.ui \ + cropeditor/settings/brushpenselection.ui DISTFILES += \ README.md \ diff --git a/cropeditor/cropscene.cpp b/cropeditor/cropscene.cpp index f50b213..881d4cd 100644 --- a/cropeditor/cropscene.cpp +++ b/cropeditor/cropscene.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include CropScene::CropScene(QObject *parent) : QGraphicsScene(parent), prevButtons(Qt::NoButton) { @@ -23,12 +25,12 @@ CropScene::CropScene(QObject *parent) : QGraphicsScene(parent), prevButtons(Qt:: }); } -QPen CropScene::pen() +QPen &CropScene::pen() { return _pen; } -QBrush CropScene::brush() +QBrush &CropScene::brush() { return _brush; } @@ -99,6 +101,7 @@ void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) if (drawingSelection != nullptr) { drawingSelection->mouseDragEndEvent(e, this); + delete drawingSelection; drawingSelection = nullptr; } prevButtons = Qt::NoButton; @@ -113,27 +116,24 @@ void CropScene::addDrawingAction(QMenu &menu, DrawItem *item) { QAction *action = new QAction; action->setText(item->name()); - QObject::connect(action, &QAction::triggered, - [&](bool) { QTimer::singleShot(0, [&] { setDrawingSelection(item); }); }); + QObject::connect(action, &QAction::triggered, [this, item](bool) { setDrawingSelection(item); }); menu.addAction(action); } void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) { - // QMenu menu(e->widget()); + QMenu menu(e->widget()); - // addDrawingAction(menu, new DotItem); + addDrawingAction(menu, new DotItem); + addDrawingAction(menu, new LineItem); - // menu.addSeparator(); - // QAction *bColorAction = new QAction; - // bColorAction->setText("Select brush color"); - // connect(bColorAction, &QAction::triggered, [&] { _brush.setColor(QColorDialog::getColor(_brush.color())); }); - // QAction *pColorAction = new QAction; - // pColorAction->setText("Select pen color"); - // connect(pColorAction, &QAction::triggered, [&] { _pen.setColor(QColorDialog::getColor(_pen.color())); }); - // menu.addActions({ pColorAction, bColorAction }); + menu.addSeparator(); + QAction *settings = new QAction; + settings->setText("Settings"); + connect(settings, &QAction::triggered, [&] { BrushPenSelection(this).exec(); }); + menu.addAction(settings); - // menu.exec(e->screenPos()); + menu.exec(e->screenPos()); e->accept(); } diff --git a/cropeditor/cropscene.hpp b/cropeditor/cropscene.hpp index 58bf3ff..eb75e45 100644 --- a/cropeditor/cropscene.hpp +++ b/cropeditor/cropscene.hpp @@ -16,8 +16,8 @@ class CropScene : public QGraphicsScene Q_OBJECT public: CropScene(QObject *parent); - QPen pen(); - QBrush brush(); + QPen &pen(); + QBrush &brush(); void setDrawingSelection(DrawItem *drawAction); signals: diff --git a/cropeditor/drawing/dotitem.cpp b/cropeditor/drawing/dotitem.cpp index dbf8693..b1e77d7 100644 --- a/cropeditor/drawing/dotitem.cpp +++ b/cropeditor/drawing/dotitem.cpp @@ -10,7 +10,7 @@ DotItem::~DotItem() void DotItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) { - scene->addEllipse(e->pos().x() - 1.5, e->pos().y() - 1.5, 3, 3, scene->pen(), scene->brush()); + scene->addEllipse(e->pos().x() - 1.5, e->pos().y() - 1.5, 3, 3, scene->pen(), scene->brush())->setPos(e->scenePos()); } void DotItem::mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) diff --git a/cropeditor/drawing/lineitem.cpp b/cropeditor/drawing/lineitem.cpp new file mode 100644 index 0000000..c253bb6 --- /dev/null +++ b/cropeditor/drawing/lineitem.cpp @@ -0,0 +1,23 @@ +#include "lineitem.hpp" + +LineItem::LineItem() +{ +} + +LineItem::~LineItem() +{ + delete path; +} + +void LineItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *) +{ + if (path == nullptr) + path = new QPainterPath(e->scenePos()); + else + path->lineTo(e->scenePos()); +} + +void LineItem::mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *scene) +{ + scene->addPath(*path, scene->pen(), scene->brush()); +} diff --git a/cropeditor/drawing/lineitem.hpp b/cropeditor/drawing/lineitem.hpp new file mode 100644 index 0000000..9899c6d --- /dev/null +++ b/cropeditor/drawing/lineitem.hpp @@ -0,0 +1,23 @@ +#ifndef LINEITEM_HPP +#define LINEITEM_HPP + +#include "../cropscene.hpp" +#include "drawitem.hpp" + +class LineItem : public DrawItem +{ + public: + LineItem(); + ~LineItem(); + QString name() + { + return "Line"; + } + void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene); + void mouseDragEndEvent(QGraphicsSceneMouseEvent *e, CropScene *scene); + + private: + QPainterPath *path = nullptr; +}; + +#endif // LINEITEM_HPP diff --git a/cropeditor/settings/brushpenselection.cpp b/cropeditor/settings/brushpenselection.cpp new file mode 100644 index 0000000..786047e --- /dev/null +++ b/cropeditor/settings/brushpenselection.cpp @@ -0,0 +1,59 @@ +#include "brushpenselection.hpp" +#include "ui_brushpenselection.h" + +#include +#include +#include +#include +#include +#include + +BrushPenSelection::BrushPenSelection(CropScene *scene) : QDialog(), ui(new Ui::BrushPenSelection) +{ + ui->setupUi(this); + ui->cosmetic->setChecked(scene->pen().isCosmetic()); + ui->widthSlider->setValue(scene->pen().width()); + ui->widthSpinner->setValue(scene->pen().widthF()); + pen = scene->pen().color(); + brush = scene->brush().color(); + this->scene = scene; +} + +BrushPenSelection::~BrushPenSelection() +{ + delete ui; +} + +void BrushPenSelection::on_penColor_clicked(bool) +{ + pen = QColorDialog::getColor(pen, this, "Pen Color"); +} + +void BrushPenSelection::on_brushColor_clicked(bool) +{ + brush = QColorDialog::getColor(brush, this, "Brush Color"); +} + +void BrushPenSelection::on_buttonBox_accepted() +{ + scene->pen().setColor(pen); + scene->pen().setCosmetic(ui->cosmetic->isChecked()); + scene->pen().setWidthF(ui->widthSpinner->value()); + scene->brush().setColor(brush); + close(); +} + +void BrushPenSelection::on_buttonBox_rejected() +{ + close(); +} + +void BrushPenSelection::on_widthSlider_sliderMoved(int position) +{ + ui->widthSpinner->setValue(position / 100.); +} + +void BrushPenSelection::on_widthSpinner_valueChanged(double arg) +{ + ui->widthSlider->setValue(arg * 100); +} diff --git a/cropeditor/settings/brushpenselection.hpp b/cropeditor/settings/brushpenselection.hpp new file mode 100644 index 0000000..8986162 --- /dev/null +++ b/cropeditor/settings/brushpenselection.hpp @@ -0,0 +1,36 @@ +#ifndef BRUSHPENSELECTION_HPP +#define BRUSHPENSELECTION_HPP + +#include +#include + +namespace Ui +{ +class BrushPenSelection; +} + +class BrushPenSelection : public QDialog +{ + Q_OBJECT + + public: + explicit BrushPenSelection(CropScene *scene); + ~BrushPenSelection(); + + private slots: + void on_penColor_clicked(bool); + void on_brushColor_clicked(bool); + + void on_buttonBox_accepted(); + void on_buttonBox_rejected(); + + void on_widthSlider_sliderMoved(int position); + void on_widthSpinner_valueChanged(double arg1); + + private: + Ui::BrushPenSelection *ui; + CropScene *scene; + QColor brush, pen; +}; + +#endif // BRUSHPENSELECTION_HPP diff --git a/cropeditor/settings/brushpenselection.ui b/cropeditor/settings/brushpenselection.ui new file mode 100644 index 0000000..1264b2f --- /dev/null +++ b/cropeditor/settings/brushpenselection.ui @@ -0,0 +1,102 @@ + + + BrushPenSelection + + + + 0 + 0 + 400 + 262 + + + + Dialog + + + + + + Qt::Vertical + + + QSizePolicy::MinimumExpanding + + + + 5 + 5 + + + + + + + + Width + + + + + + + Choose pen color + + + + + + + 2500 + + + Qt::Horizontal + + + + + + + Choose brush color + + + + + + + Brush settings + + + + + + + Cosmetic + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + true + + + + + + + Pen settings + + + + + + + + + + + diff --git a/hotkeying.cpp b/hotkeying.cpp index c8942d3..48e1917 100644 --- a/hotkeying.cpp +++ b/hotkeying.cpp @@ -37,7 +37,7 @@ void hotkeying::load(QString seqName, std::function func) bool hotkeying::valid(QString seq) { - return !QKeySequence(seq).toString().isEmpty(); + return seq.isEmpty() || !QKeySequence(seq).toString().isEmpty(); } QString hotkeying::sequence(QString seqName) diff --git a/main.cpp b/main.cpp index 20549a0..f2e7db9 100644 --- a/main.cpp +++ b/main.cpp @@ -50,6 +50,7 @@ int main(int argc, char *argv[]) verbose = parser.isSet(v); MainWindow w; - if (!parser.isSet(h)) w.show(); + w.show(); + if (parser.isSet(h)) w.hide(); return a.exec(); }