File indexing completed on 2024-04-28 04:31:59

0001 /*
0002  * Copyright (C) 2010-2022 by Stephen Allewell
0003  * steve.allewell@gmail.com
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  */
0010 
0011 #ifndef Editor_H
0012 #define Editor_H
0013 
0014 #include <QStack>
0015 #include <QWidget>
0016 
0017 #include <KModifierKeyInfo>
0018 
0019 #include "Stitch.h"
0020 
0021 #include "Renderer.h"
0022 #include "StitchData.h"
0023 #include "configuration.h"
0024 
0025 class QUndoCommand;
0026 
0027 class Document;
0028 class LibraryManagerDlg;
0029 class Pattern;
0030 class Preview;
0031 class Renderer;
0032 class Scale;
0033 
0034 class Editor : public QWidget
0035 {
0036     Q_OBJECT
0037 
0038 public:
0039     enum SelectedStitchType { StitchQuarter, StitchHalf, Stitch3Quarter, StitchFull, StitchSmallHalf, StitchSmallFull, StitchFrenchKnot };
0040 
0041     enum ToolMode {
0042         ToolPaint,
0043         ToolDraw,
0044         ToolErase,
0045         ToolRectangle,
0046         ToolFillRectangle,
0047         ToolEllipse,
0048         ToolFillEllipse,
0049         ToolFillPolygon,
0050         ToolText,
0051         ToolAlphabet,
0052         ToolSelect,
0053         ToolBackstitch,
0054         ToolColorPicker,
0055         ToolPaste,
0056         ToolMirror,
0057         ToolRotate
0058     };
0059 
0060     explicit Editor(QWidget *);
0061     virtual ~Editor() = default;
0062 
0063     void setDocument(Document *);
0064     Document *document();
0065 
0066     void setPreview(Preview *);
0067 
0068     void readDocumentSettings();
0069 
0070     Scale *horizontalScale();
0071     Scale *verticalScale();
0072 
0073     QRect selectionArea();
0074     void resetSelectionArea();
0075 
0076 signals:
0077     void selectionMade(bool);
0078     void changedVisibleCells(const QRect &);
0079 
0080 public slots:
0081     void libraryManager();
0082 
0083     void previewClicked(const QPoint &);
0084     void previewClicked(const QRect &);
0085 
0086     void zoomIn();
0087     void zoomOut();
0088     void actualSize();
0089     void fitToPage();
0090     void fitToWidth();
0091     void fitToHeight();
0092 
0093     void editCut();
0094     void editCopy();
0095     void editPaste();
0096 
0097     void mirrorSelection();
0098     void rotateSelection();
0099 
0100     void pastePattern(Editor::ToolMode);
0101 
0102     void formatScalesAsStitches();
0103     void formatScalesAsCentimeters();
0104     void formatScalesAsInches();
0105 
0106     void renderStitches(bool);
0107     void renderBackstitches(bool);
0108     void renderFrenchKnots(bool);
0109     void renderGrid(bool);
0110     void renderBackgroundImages(bool);
0111 
0112     void renderStitchesAs(Configuration::EnumRenderer_RenderStitchesAs::type);
0113     void renderBackstitchesAs(Configuration::EnumRenderer_RenderBackstitchesAs::type);
0114     void renderKnotsAs(Configuration::EnumRenderer_RenderKnotsAs::type);
0115 
0116     void colorHighlight(bool);
0117 
0118     void selectTool(Editor::ToolMode);
0119 
0120     void selectStitch(Editor::SelectedStitchType);
0121 
0122     void setMaskStitch(bool);
0123     void setMaskColor(bool);
0124     void setMaskBackstitch(bool);
0125     void setMaskKnot(bool);
0126     void setMakesCopies(bool);
0127 
0128     void loadSettings();
0129 
0130     void drawContents();
0131     void drawContents(const QPoint &);
0132     void drawContents(const QRect &);
0133 
0134 protected:
0135     virtual bool event(QEvent *) Q_DECL_OVERRIDE;
0136     virtual void contextMenuEvent(QContextMenuEvent *) Q_DECL_OVERRIDE;
0137     virtual void dragEnterEvent(QDragEnterEvent *) Q_DECL_OVERRIDE;
0138     virtual void dragLeaveEvent(QDragLeaveEvent *) Q_DECL_OVERRIDE;
0139     virtual void dragMoveEvent(QDragMoveEvent *) Q_DECL_OVERRIDE;
0140     virtual void dropEvent(QDropEvent *) Q_DECL_OVERRIDE;
0141     virtual void keyPressEvent(QKeyEvent *) Q_DECL_OVERRIDE;
0142     virtual void keyReleaseEvent(QKeyEvent *) Q_DECL_OVERRIDE;
0143     virtual void mousePressEvent(QMouseEvent *) Q_DECL_OVERRIDE;
0144     virtual void mouseMoveEvent(QMouseEvent *) Q_DECL_OVERRIDE;
0145     virtual void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE;
0146     virtual void moveEvent(QMoveEvent *) Q_DECL_OVERRIDE;
0147     virtual void resizeEvent(QResizeEvent *) Q_DECL_OVERRIDE;
0148     virtual void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
0149     virtual void wheelEvent(QWheelEvent *) Q_DECL_OVERRIDE;
0150     virtual bool eventFilter(QObject *, QEvent *) Q_DECL_OVERRIDE;
0151 
0152 private:
0153     bool zoom(double);
0154 
0155     void keyPressPolygon(QKeyEvent *);
0156     void keyPressText(QKeyEvent *);
0157     void keyPressAlphabet(QKeyEvent *);
0158     void keyPressPaste(QKeyEvent *);
0159     void keyPressMirror(QKeyEvent *);
0160     void keyPressRotate(QKeyEvent *);
0161     void keyPressMovePattern(QKeyEvent *);
0162 
0163     void toolInitPolygon();
0164     void toolInitText();
0165     void toolInitAlphabet();
0166 
0167     void toolCleanupPolygon();
0168     void toolCleanupAlphabet();
0169     void toolCleanupSelect();
0170     void toolCleanupMirror();
0171     void toolCleanupRotate();
0172 
0173     void renderBackgroundImages(QPainter &, const QRect &);
0174     void renderStitches(QPainter *, const QRect &);
0175     void renderBackstitches(QPainter *, const QRect &);
0176     void renderFrenchKnots(QPainter *, const QRect &);
0177 
0178     void renderRubberBandLine(QPainter *, const QRect &);
0179     void renderRubberBandRectangle(QPainter *, const QRect &);
0180     void renderRubberBandEllipse(QPainter *, const QRect &);
0181     void renderFillPolygon(QPainter *, const QRect &);
0182     void renderAlphabetCursor(QPainter *, const QRect &);
0183     void renderPasteImage(QPainter *, const QRect &);
0184 
0185     void mousePressEvent_Paint(QMouseEvent *);
0186     void mouseMoveEvent_Paint(QMouseEvent *);
0187     void mouseReleaseEvent_Paint(QMouseEvent *);
0188 
0189     void mousePressEvent_Draw(QMouseEvent *);
0190     void mouseMoveEvent_Draw(QMouseEvent *);
0191     void mouseReleaseEvent_Draw(QMouseEvent *);
0192 
0193     void mousePressEvent_Erase(QMouseEvent *);
0194     void mouseMoveEvent_Erase(QMouseEvent *);
0195     void mouseReleaseEvent_Erase(QMouseEvent *);
0196 
0197     void mousePressEvent_Rectangle(QMouseEvent *);
0198     void mouseMoveEvent_Rectangle(QMouseEvent *);
0199     void mouseReleaseEvent_Rectangle(QMouseEvent *);
0200 
0201     void mousePressEvent_FillRectangle(QMouseEvent *);
0202     void mouseMoveEvent_FillRectangle(QMouseEvent *);
0203     void mouseReleaseEvent_FillRectangle(QMouseEvent *);
0204 
0205     void mousePressEvent_Ellipse(QMouseEvent *);
0206     void mouseMoveEvent_Ellipse(QMouseEvent *);
0207     void mouseReleaseEvent_Ellipse(QMouseEvent *);
0208 
0209     void mousePressEvent_FillEllipse(QMouseEvent *);
0210     void mouseMoveEvent_FillEllipse(QMouseEvent *);
0211     void mouseReleaseEvent_FillEllipse(QMouseEvent *);
0212 
0213     void mousePressEvent_FillPolygon(QMouseEvent *);
0214     void mouseMoveEvent_FillPolygon(QMouseEvent *);
0215     void mouseReleaseEvent_FillPolygon(QMouseEvent *);
0216 
0217     void mousePressEvent_Text(QMouseEvent *);
0218     void mouseMoveEvent_Text(QMouseEvent *);
0219     void mouseReleaseEvent_Text(QMouseEvent *);
0220 
0221     void mousePressEvent_Alphabet(QMouseEvent *);
0222     void mouseMoveEvent_Alphabet(QMouseEvent *);
0223     void mouseReleaseEvent_Alphabet(QMouseEvent *);
0224 
0225     void mousePressEvent_Select(QMouseEvent *);
0226     void mouseMoveEvent_Select(QMouseEvent *);
0227     void mouseReleaseEvent_Select(QMouseEvent *);
0228 
0229     void mousePressEvent_Backstitch(QMouseEvent *);
0230     void mouseMoveEvent_Backstitch(QMouseEvent *);
0231     void mouseReleaseEvent_Backstitch(QMouseEvent *);
0232 
0233     void mousePressEvent_ColorPicker(QMouseEvent *);
0234     void mouseMoveEvent_ColorPicker(QMouseEvent *);
0235     void mouseReleaseEvent_ColorPicker(QMouseEvent *);
0236 
0237     void mousePressEvent_Paste(QMouseEvent *);
0238     void mouseMoveEvent_Paste(QMouseEvent *);
0239     void mouseReleaseEvent_Paste(QMouseEvent *);
0240 
0241     void mousePressEvent_Mirror(QMouseEvent *);
0242     void mouseMoveEvent_Mirror(QMouseEvent *);
0243     void mouseReleaseEvent_Mirror(QMouseEvent *);
0244 
0245     void mousePressEvent_Rotate(QMouseEvent *);
0246     void mouseMoveEvent_Rotate(QMouseEvent *);
0247     void mouseReleaseEvent_Rotate(QMouseEvent *);
0248 
0249     QPoint contentsToCell(const QPoint &) const;
0250     int contentsToZone(const QPoint &) const;
0251     QPoint contentsToSnap(const QPoint &) const;
0252     QRect snapToCells(const QPoint &) const;
0253     QRect cellToRect(const QPoint &) const;
0254     QRect polygonToCells(const QPolygon &) const;
0255     QRect rectToContents(const QRect &) const;
0256 
0257     void processBitmap(QUndoCommand *, const QBitmap &);
0258     QRect visibleCells();
0259     QList<Stitch::Type> maskStitches() const;
0260 
0261     Document *m_document;
0262     Preview *m_preview;
0263 
0264     Renderer m_renderer;
0265 
0266     Scale *m_horizontalScale;
0267     Scale *m_verticalScale;
0268     Configuration::EnumEditor_FormatScalesAs::type m_formatScalesAs;
0269 
0270     LibraryManagerDlg *m_libraryManagerDlg;
0271 
0272     double m_zoomFactor;
0273     double m_cellWidth;
0274     double m_cellHeight;
0275     double m_horizontalClothCount;
0276     double m_verticalClothCount;
0277     int m_cellHorizontalGrouping;
0278     int m_cellVerticalGrouping;
0279 
0280     enum ToolMode m_toolMode;
0281     enum ToolMode m_oldToolMode;
0282 
0283     bool m_renderBackgroundImages;
0284     bool m_renderGrid;
0285     bool m_renderStitches;
0286     bool m_renderBackstitches;
0287     bool m_renderFrenchKnots;
0288 
0289     bool m_maskStitch;
0290     bool m_maskColor;
0291     bool m_maskBackstitch;
0292     bool m_maskKnot;
0293     bool m_makesCopies;
0294 
0295     Qt::Orientation m_orientation;
0296     StitchData::Rotation m_rotation;
0297 
0298     QPoint m_cellStart;
0299     QPoint m_cellTracking;
0300     QPoint m_cellEnd;
0301 
0302     QPoint m_pasteOffset;
0303 
0304     int m_zoneStart;
0305     int m_zoneTracking;
0306     int m_zoneEnd;
0307 
0308     QPolygon m_polygon;
0309 
0310     QUndoCommand *m_activeCommand;
0311 
0312     enum SelectedStitchType m_currentStitchType;
0313 
0314     Configuration::EnumRenderer_RenderStitchesAs::type m_renderStitchesAs;
0315     Configuration::EnumRenderer_RenderBackstitchesAs::type m_renderBackstitchesAs;
0316     Configuration::EnumRenderer_RenderKnotsAs::type m_renderKnotsAs;
0317     bool m_colorHighlight;
0318 
0319     QRect m_rubberBand;
0320     QRect m_selectionArea;
0321 
0322     QByteArray m_pasteData;
0323     Pattern *m_pastePattern;
0324 
0325     QPixmap m_cachedContents;
0326 
0327     QStack<QPoint> m_cursorStack;
0328     QMap<int, int> m_cursorCommands;
0329 
0330     KModifierKeyInfo m_keyInfo;
0331 
0332     typedef void (Editor::*keyPressCallPointer)(QKeyEvent *);
0333     typedef void (Editor::*toolInitCallPointer)();
0334     typedef void (Editor::*toolCleanupCallPointer)();
0335     typedef void (Editor::*mouseEventCallPointer)(QMouseEvent *);
0336     typedef void (Editor::*renderToolSpecificGraphicsCallPointer)(QPainter *, const QRect &);
0337 
0338     static const keyPressCallPointer keyPressCallPointers[];
0339 
0340     static const toolInitCallPointer toolInitCallPointers[];
0341     static const toolCleanupCallPointer toolCleanupCallPointers[];
0342 
0343     static const mouseEventCallPointer mousePressEventCallPointers[];
0344     static const mouseEventCallPointer mouseMoveEventCallPointers[];
0345     static const mouseEventCallPointer mouseReleaseEventCallPointers[];
0346 
0347     static const renderToolSpecificGraphicsCallPointer renderToolSpecificGraphics[];
0348 };
0349 
0350 #endif // Editor_H