1 // XDrawChem 2 // Copyright (C) 2004-2005 Bryan Herger <bherger@users.sourceforge.net> 3 // Copyright (C) 2020 Yaman Qalieh <ybq987@gmail.com> 4 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 // render2d.h -- class definition for XDrawChem render widget. 19 20 #ifndef RENDER2D_H 21 #define RENDER2D_H 22 23 #include <QPainterPath> 24 #include <QPrinter> 25 #include <QProcess> 26 #include <QTextCharFormat> 27 #include <QTextEdit> 28 29 #include "clipboard.h" 30 #include "dpoint.h" 31 #include "drawable.h" 32 #include "paintable.h" 33 #include "prefs.h" 34 #include "xdc_event.h" 35 36 class ChemData; 37 class Text; 38 class Molecule; 39 40 extern Preferences preferences; 41 42 class Render2D : public QWidget { 43 Q_OBJECT 44 45 public: setChemData(ChemData * c1)46 void setChemData(ChemData *c1) { c = c1; } 47 // defined in render2d.cpp 48 Render2D(QWidget *parent = 0); 49 void CorrectEndpoint_arrow(); 50 void CorrectEndpoint_bond(); 51 void CleanUpAfterTextTool(); 52 void setClipboard(Clipboard *); 53 Clipboard *getClipboard(); 54 55 public slots: 56 // defined in render2d.cpp 57 void ReactionAnalysis(int); 58 void setMode_Select(); 59 void setMode_Lasso(); 60 void setMode_DrawLine(); 61 void setMode_DrawDashLine(); 62 void setMode_DrawChain(); 63 void setMode_DrawUpLine(); 64 void setMode_DrawDownLine(); 65 void setMode_DrawWavyLine(); 66 void setMode_DrawArrow(regularArrowType); 67 void setMode_DrawCurveArrow(curveArrowType); 68 void setMode_DrawBracket(bracketType); 69 void setMode_Erase(); 70 void setMode_DrawText(); 71 void setMode_DrawSymbol(const QString &); 72 void setMode_DrawGraphicObject(int, int param1 = 0); 73 void setMode_DrawRing(QString, QString, int sp = 0); 74 void setMode_DrawNewmanAnti(); 75 void setMode_DrawNewmanGauche(); 76 void Tool(int); 77 void SelectAll(); 78 void DeselectAll(); 79 void EraseSelected(); 80 void Inserted(); 81 void SetFont(QFont); 82 void SetColor(QColor); 83 QFont GetFont(); 84 QColor GetColor(); 85 void Cut(); 86 void Copy(); 87 void Paste(); 88 void Undo(); 89 void Redo(); 90 void Rotate90(); 91 void Rotate180(); 92 void Rotate270(); 93 void AutoLayout(); 94 void Flip(int); 95 bool SaveEPS(QString); 96 bool SaveSVG(QString); 97 void clearAllGroups(); 98 QPoint zoomCorrect(QPoint); 99 QPoint zoomCorrectReverse(QPoint); 100 // defined in render2d_draw.cpp 101 double getAngle(QPointF, QPointF); 102 void drawBezier(QVector<QPoint>, QColor, bool = false, int = 0); 103 void drawLine(QPointF, QPointF, int, QColor, int = 0); 104 void drawPolyline(QPainterPath, QColor); 105 void drawPolyline(QVector<QPoint> p, QColor c1); 106 void drawUpLine(QPointF, QPointF, QColor); 107 void drawDownLine(QPointF, QPointF, QColor); 108 void drawWavyLine(QPointF, QPointF, QColor); 109 void drawArrow(QPointF, QPointF, QColor, int, int); 110 void drawBracket(QPoint, QPoint, QColor, int, QColor); 111 void drawBox(QPoint, QPoint, QColor); 112 void drawFillBox(QPoint, QPoint, QColor); 113 void drawFillBox(QPoint, QPoint, QColor, bool, QColor, int); 114 void drawCircle(QPoint, int, QColor); 115 void drawEllipse(QPoint, QPoint, bool, QColor, bool, QColor); 116 void drawText(QChar, QPoint, QColor, QFont); 117 void drawString(QString, QPoint, QColor, QFont); 118 void drawTextReverse(QChar, QPoint, QColor, QFont); 119 void drawPixmap(QPoint, QPixmap); 120 void drawCurveArrow(QPointF, QPointF, QColor, QString); 121 QPixmap MakePixmap(bool transp = false); 122 QPixmap MakeFullPixmap(); 123 // defined in render2d_text.cpp 124 void InsertSymbol(); 125 void DrawText_returnPressed(); 126 void DrawText_textChanged(); 127 void DrawText_FinishText(); 128 void DrawText_CVAchanged(QTextCharFormat::VerticalAlignment); 129 QString EditText(QPoint, QString, int, bool); 130 QRect GetTextDimensions(QString, QFont); 131 int GetTextHeight(QFont); 132 int GetTextFullHeight(QFont); 133 int GetCharWidth(QChar, QFont); 134 int GetStringWidth(QString, QFont); 135 void DrawText_mousePressEvent(QMouseEvent *, QPoint); 136 void DrawText_mouseReleaseEvent(QMouseEvent *, QPoint); 137 void DrawText_mouseMoveEvent(QMouseEvent *); 138 void Superscript(); 139 void Subscript(); 140 void Bold(); 141 void Italic(); 142 void Underline(); 143 void JustifyLeft(); 144 void JustifyCenter(); 145 void JustifyRight(); 146 // defined in render2d_select.cpp 147 void Select_mouseMoveEvent(QMouseEvent *); 148 double GetAngle(DPoint *, DPoint *, DPoint *); 149 // defined in render2d_print.cpp 150 void PrintSetup(); 151 void Print(QString epsname = ""); 152 void UpdatePageGeometry(); 153 // defined in render2d_event.cpp: 154 QPoint GridLock(QPoint); 155 void createGrid(); 156 void zoomEvent(); 157 void molInfo(); 158 void molSelect(); 159 void molCut(); 160 void molCopy(); 161 void bondEdit(); 162 void bondInfo(); 163 void textShape(); 164 void bracketFill(); 165 void bracketFillOff(); 166 167 // defined here: setDirectDraw(bool dd1)168 void setDirectDraw(bool dd1) { directdraw = dd1; } DistanceBetween(QPointF a,QPointF b)169 double DistanceBetween(QPointF a, QPointF b) { 170 double dx = a.x() - b.x(); 171 double dy = a.y() - b.y(); 172 return sqrt(dx * dx + dy * dy); 173 } Midpoint(QPointF a,QPointF b)174 QPointF Midpoint(QPointF a, QPointF b) { 175 int ax = (a.x() + b.x()) / 2; 176 int ay = (a.y() + b.y()) / 2; 177 return QPointF(ax, ay); 178 } RoundOff(double x)179 int RoundOff(double x) { 180 int x1 = (int)x; 181 double x2 = x - (double)x1; 182 if (x2 > 0.5) 183 return x1 + 1; 184 else 185 return x1; 186 } SetOutputDevice(int x)187 void SetOutputDevice(int x) { outputDevice = x; } SetThick(int t)188 void SetThick(int t) { thick = t; } resetTextOrigin()189 void resetTextOrigin() { 190 if (mode > 127) { 191 rotateOrigin.setX(0); 192 rotateOrigin.setY(0); 193 } 194 } 195 // now done in prefs.h 196 // int getPageSize() { return page_size; } 197 // int getPageOrientation() { return page_orientation; } setPageSizeAndOrientation()198 void setPageSizeAndOrientation() { UpdatePageGeometry(); } setBGColor(QColor bc1)199 void setBGColor(QColor bc1) { 200 QPalette palette; 201 palette.setColor(backgroundRole(), bc1); 202 setPalette(palette); 203 bgcolor = bc1; 204 } getBGColor()205 QColor getBGColor() { return bgcolor; } setWaitCursor()206 void setWaitCursor() { setCursor(Qt::WaitCursor); } setArrowCursor()207 void setArrowCursor() { setCursor(Qt::ArrowCursor); } getFontKludge()208 bool getFontKludge() { return font_size_kludge; } 209 210 signals: 211 void SignalSetStatusBar(QString); 212 void SignalHelpTopic(QString); 213 void XDCEventSignal(XDC_Event *); 214 void textOn(QFont); 215 void TextOff(); 216 217 protected: 218 // defined in render2d_event.cpp 219 void mouseDoubleClickEvent(QMouseEvent *); 220 void mousePressEvent(QMouseEvent *); 221 void mouseReleaseEvent(QMouseEvent *); 222 void mouseMoveEvent(QMouseEvent *); 223 void paintEvent(QPaintEvent *); 224 void resizeEvent(QResizeEvent *); 225 // defined in render2d_text.cpp 226 void keyPressEvent(QKeyEvent *); 227 228 private slots: 229 void saveEPSFinished(int, QProcess::ExitStatus); 230 231 private: 232 // Qpixmap of double buffer 233 QPixmap dbuffer; 234 QPixmap grid; 235 // height and width of the drawing area 236 int renderHeight, renderWidth; 237 // zoom factor (actually held in prefs.h) 238 float zoomFactor; 239 // cursors 240 QCursor skullcursor, rotatecursor; 241 // the ChemData object this Render2D is linked with 242 ChemData *c; 243 // start and end points of object being drawn, or selection box... 244 DPoint *startpoint, *endpoint; 245 // current drawing font 246 QFont currentFont; 247 // current drawing color 248 QColor currentColor; 249 // current background color 250 QColor bgcolor; 251 // Positions to remember for multiple selection 252 QPoint selectOrigin, selectPrev; 253 // Rotate and resize handles (used in MODE_SELECT_MULTIPLE_SELECTED) 254 QRect selectionBox; 255 QPoint rotateHandle, resizeHandle; 256 // rotate origin 257 QPoint rotateOrigin; 258 // previous angle (for rotating) 259 double prevangle; 260 // previous position (of mouse, usually) 261 DPoint *prevpos; 262 // highlighted point, if any 263 DPoint *highlightpoint; 264 // highlighted object, if any 265 Drawable *highlightobject; 266 // current input/output/drawing mode 267 int mode, prev_mode; 268 // temporary Bezier curve 269 QVector<QPoint> tmp_bezier; 270 int bezier_count; 271 // current button state 272 bool mouse1down, mouse3down, doubleClickFlag; 273 bool moved; // to check validity of points, see render2d_event.cpp 274 // direct drawing flag 275 bool directdraw; 276 // fixed length/angle (now controlled by Preferences class, see prefs.h) 277 // bool fixed_bond, fixed_arrow; 278 // double fixedlength_bond, fixedangle_bond; 279 // double fixedlength_arrow, fixedangle_arrow; 280 // current line thickness and old bond order (for edit) 281 int thick, savedBondOrder; 282 // chain length 283 double chainlength; 284 // Output device (screen, printer, image) 285 int outputDevice; 286 // Output file/textstream 287 QFile output_file; 288 QTextStream output_ts; 289 // Output printer and related classes 290 QList<Paintable *> paintqueue; 291 QPrinter *printer; 292 QPainter *painter; 293 // Text object, temp string to use when creating/editing text 294 Text *localtext; 295 QTextEdit *localtexteditor; 296 QString localstring; 297 bool text_exists; // does this text object exist? 298 bool text_drag, super_set, sub_set; 299 DPoint *start_drag; 300 // Symbol object, which file to use (or ring, if MODE_RING) 301 QString symbolfile; 302 int smartplace; // able to attach ring to a point (e.g., benzene, Fmoc) 303 // Bracket object, which bracket 304 int bracket_type; 305 // page setup options (NOW HELD IN PREFS...) 306 // int page_size; 307 // int page_orientation; 308 // drag object 309 bool dragging; 310 // finished rendering? 311 bool finishedPainting; 312 // set in render2d.cpp::SaveEPS() 313 double eps_dx, eps_dy; 314 // data for lasso 315 QVector<QPoint> curr_lasso; 316 QPoint lasso1, lasso2; 317 // QPopupMenu for right clicks 318 QMenu *rtclickpopup; 319 Molecule *targetmol; 320 // current/temporary Molecule 321 Molecule *tmp_molecule; 322 // kludge for PNG/BMP generation 323 bool font_size_kludge; 324 // track last known pointer position 325 QPoint lastmouse; 326 // remember mode if using "hot key" 327 QString tmpEpsFile; 328 329 bool hotkeymode; 330 int hkprevmode; 331 QPoint startPoint, endPoint; 332 QVector<QPoint> chainPoints; 333 }; 334 335 #endif 336