Start making a menu bar

I need someone to draw icons or some public domains one
This commit is contained in:
ArsenArsen 2017-07-24 20:17:10 +02:00
parent 3ba317c6e2
commit bf0bed2e29
2 changed files with 48 additions and 28 deletions

View File

@ -3,9 +3,11 @@
#include <QDebug> #include <QDebug>
#include <QFontDialog> #include <QFontDialog>
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include <QGraphicsProxyWidget>
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsView> #include <QGraphicsView>
#include <QMenu> #include <QMenu>
#include <QMenuBar>
#include <QTimer> #include <QTimer>
#include <cropeditor/drawing/arrowitem.hpp> #include <cropeditor/drawing/arrowitem.hpp>
#include <cropeditor/drawing/bluritem.hpp> #include <cropeditor/drawing/bluritem.hpp>
@ -26,10 +28,12 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap)
_pixmap = pixmap; _pixmap = pixmap;
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()).toReal());
brush().setColor(settings::settings().value("brushColor", brush().color()).value<QColor>()); brush().setColor(settings::settings().value("brushColor", brush().color()).value<QColor>());
brush().setStyle((Qt::BrushStyle)settings::settings().value("brushStyle", (int)Qt::SolidPattern).toInt()); brush().setStyle(
static_cast<Qt::BrushStyle>(settings::settings().value("brushStyle", static_cast<int>(Qt::SolidPattern)).toInt()));
menu = new QMenuBar;
addDrawingAction(menu, "Dot", [] { return new DotItem; }); addDrawingAction(menu, "Dot", [] { return new DotItem; });
addDrawingAction(menu, "Path", [] { return new PathItem; }); addDrawingAction(menu, "Path", [] { return new PathItem; });
addDrawingAction(menu, "Blur", [] { return new BlurItem; }); addDrawingAction(menu, "Blur", [] { return new BlurItem; });
@ -39,31 +43,39 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap)
addDrawingAction(menu, "Ellipse", [] { return new EllipseItem; }); addDrawingAction(menu, "Ellipse", [] { return new EllipseItem; });
addDrawingAction(menu, "Arrow", [] { return new ArrowItem; }); addDrawingAction(menu, "Arrow", [] { return new ArrowItem; });
menu.addSeparator(); menu->addSeparator();
addDrawingAction(menu, "Eraser", [] { return new EraserItem; }); addDrawingAction(menu, "Eraser", [] { return new EraserItem; });
QAction *clear = menu.addAction("Clear all drawing"); QAction *clear = menu->addAction("Clear all drawing");
connect(clear, &QAction::triggered, [&] { connect(clear, &QAction::triggered, [&] {
auto its = items(); auto its = items();
for (auto i : its) { for (auto i : its) {
if (i != rect && i != polyItem && i->zValue() != -1) removeItem(i); if (i != polyItem && i != rect && i != cursorItem && i->zValue() != -1 && i != proxyMenu && i != hint
&& i != magnifier && i != magnifierBox && i != magnifierHint && i != magnifierHintBox
&& gridRectsX.contains(dynamic_cast<QGraphicsRectItem *>(i))
&& gridRectsY.contains(dynamic_cast<QGraphicsRectItem *>(i))) {
removeItem(i);
}
} }
}); });
QAction *reset = menu.addAction("Reset pen selection"); QAction *reset = menu->addAction("Reset pen selection");
connect(reset, &QAction::triggered, [&] { setDrawingSelection("None", [] { return nullptr; }); }); connect(reset, &QAction::triggered, [this] {
menu->setActiveAction(0);
setDrawingSelection("None", [] { return nullptr; });
});
menu.addSeparator(); menu->addSeparator();
QAction *settings = new QAction; QAction *settings = new QAction;
settings->setText("Settings"); settings->setText("Settings");
menu.addSeparator(); menu->addSeparator();
display = menu.addAction(drawingName); display = menu->addAction(drawingName);
display->setDisabled(true); display->setDisabled(true);
connect(settings, &QAction::triggered, [&] { connect(settings, &QAction::triggered, [&] {
hide(); hide();
BrushPenSelection(this).exec(); BrushPenSelection(this).exec();
show(); show();
}); });
menu.addAction(settings); menu->addAction(settings);
QPolygonF cursorPoly; QPolygonF cursorPoly;
cursorPoly << QPoint(-10, 0) // cursorPoly << QPoint(-10, 0) //
<< QPoint(10, 0) // << QPoint(10, 0) //
@ -96,7 +108,7 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap)
hint->setPos(5, 5); hint->setPos(5, 5);
hint->setZValue(2); hint->setZValue(2);
hint->setVisible(settings::settings().value("crophint", true).toBool()); hint->setVisible(settings::settings().value("crophint", true).toBool());
connect(menu.addAction("Set Font"), &QAction::triggered, this, &CropScene::fontAsk); connect(menu->addAction("Set Font"), &QAction::triggered, this, &CropScene::fontAsk);
QPolygonF poly; QPolygonF poly;
QRect prect = pixmap.rect(); QRect prect = pixmap.rect();
@ -109,6 +121,12 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap)
polyItem->setPen(QPen(Qt::NoPen)); polyItem->setPen(QPen(Qt::NoPen));
polyItem->setZValue(1); polyItem->setZValue(1);
addItem(polyItem); addItem(polyItem);
auto widget = addWidget(menu);
widget->setZValue(100);
widget->setPos(100, 100);
proxyMenu = widget;
QTimer::singleShot(0, [&] { QTimer::singleShot(0, [&] {
auto pf = views()[0]->mapFromGlobal(QCursor::pos()); auto pf = views()[0]->mapFromGlobal(QCursor::pos());
cursorPos = QPoint(pf.x(), pf.y()); cursorPos = QPoint(pf.x(), pf.y());
@ -145,7 +163,8 @@ void CropScene::setDrawingSelection(QString name, std::function<DrawItem *()> dr
QGraphicsItem *CropScene::whichItem(QPointF scenePos) { QGraphicsItem *CropScene::whichItem(QPointF scenePos) {
for (auto item : items()) { for (auto item : items()) {
if (item->sceneBoundingRect().contains(scenePos)) if (item->sceneBoundingRect().contains(scenePos))
if (item != polyItem && item != rect && item != cursorItem && item->zValue() != -1) return item; if (item != polyItem && item != rect && item != cursorItem && item->zValue() != -1 && item != proxyMenu)
return item;
} }
return nullptr; return nullptr;
} }
@ -241,6 +260,8 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
} }
} }
prevButtons = buttons; prevButtons = buttons;
QGraphicsScene::mouseMoveEvent(e);
} }
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
@ -253,6 +274,8 @@ void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
} else if (settings::settings().value("quickMode", false).toBool()) } else if (settings::settings().value("quickMode", false).toBool())
done(true); done(true);
prevButtons = Qt::NoButton; prevButtons = Qt::NoButton;
QGraphicsScene::mouseReleaseEvent(e);
} }
void CropScene::mousePressEvent(QGraphicsSceneMouseEvent *e) { void CropScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
@ -260,6 +283,8 @@ void CropScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
auto item = whichItem(cursorItem->scenePos()); auto item = whichItem(cursorItem->scenePos());
if (item) removeItem(item); if (item) removeItem(item);
} }
QGraphicsScene::mousePressEvent(e);
} }
void CropScene::wheelEvent(QGraphicsSceneWheelEvent *event) { void CropScene::wheelEvent(QGraphicsSceneWheelEvent *event) {
@ -278,22 +303,16 @@ void CropScene::wheelEvent(QGraphicsSceneWheelEvent *event) {
initMagnifierGrid(); initMagnifierGrid();
updateMag(); updateMag();
QGraphicsScene::wheelEvent(event);
} }
void CropScene::addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item) { void CropScene::addDrawingAction(QMenuBar *menu, QString name, std::function<DrawItem *()> item) {
QAction *action = new QAction; QAction *action = menu->addAction(name);
action->setText(name); connect(action, &QAction::triggered, [this, &menu, action, item, name](bool) { setDrawingSelection(name, item); });
connect(action, &QAction::triggered, [this, item, name](bool) { setDrawingSelection(name, item); });
menu.addAction(action);
} }
QPoint contextOffset(5, 5); static QPoint contextOffset(5, 5);
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) {
display->setText(drawingName);
menu.exec((cursorPos + contextOffset).toPoint());
e->accept();
}
void CropScene::keyReleaseEvent(QKeyEvent *event) { void CropScene::keyReleaseEvent(QKeyEvent *event) {
if (((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !drawingSelection) || event->key() == Qt::Key_Escape) if (((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !drawingSelection) || event->key() == Qt::Key_Escape)

View File

@ -8,6 +8,7 @@
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QKeyEvent> #include <QKeyEvent>
#include <QMenu> #include <QMenu>
#include <QMenuBar>
#include <functional> #include <functional>
#include <screenshotutil.hpp> #include <screenshotutil.hpp>
class CropScene; class CropScene;
@ -51,13 +52,12 @@ protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *e) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *e) override;
void mousePressEvent(QGraphicsSceneMouseEvent *e) override; void mousePressEvent(QGraphicsSceneMouseEvent *e) override;
void wheelEvent(QGraphicsSceneWheelEvent *event) override; // WHEEEEEEL void wheelEvent(QGraphicsSceneWheelEvent *event) override; // WHEEEEEEL
void contextMenuEvent(QGraphicsSceneContextMenuEvent *e) override;
void keyReleaseEvent(QKeyEvent *e) override; void keyReleaseEvent(QKeyEvent *e) override;
private: private:
void updateMag(); void updateMag();
void initMagnifierGrid(); void initMagnifierGrid();
void addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item); void addDrawingAction(QMenuBar *menu, QString name, std::function<DrawItem *()> item);
void done(bool notEsc); void done(bool notEsc);
bool fullscreen; bool fullscreen;
QPointF cursorPos; QPointF cursorPos;
@ -75,7 +75,8 @@ private:
QFont _font; QFont _font;
QGraphicsPolygonItem *polyItem = nullptr; QGraphicsPolygonItem *polyItem = nullptr;
DrawItem *drawingSelection = nullptr; DrawItem *drawingSelection = nullptr;
QMenu menu; QMenuBar *menu = nullptr;
QGraphicsProxyWidget *proxyMenu = nullptr;
QString drawingName = "None"; QString drawingName = "None";
QAction *display; QAction *display;
QList<QGraphicsRectItem *> gridRectsX; QList<QGraphicsRectItem *> gridRectsX;