RDKit: MolDraw2Dwx.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

MolDraw2Dwx.h
Go to the documentation of this file.
1 //
2 // @@ All Rights Reserved @@
3 // This file is part of the RDKit.
4 // The contents are covered by the terms of the BSD license
5 // which is included in the file license.txt, found at the root
6 // of the RDKit source tree.
7 //
8 // Author: Igor Filippov based on the work of David Cosgrove (AstraZeneca)
9 //
10 // This is a concrete class derived from MolDraw2D that uses RDKit to draw a
11 // molecule into a wxDC
12
13 #ifndef MOLDRAW2DWX_H
14 #define MOLDRAW2DWX_H
15
16 #include <GraphMol/MolDraw2D/MolDraw2D.h>
17 #include <wx/dc.h>
18 #include <wx/font.h>
19 #include <wx/pen.h>
20 #include <wx/colour.h>
21 #include <wx/brush.h>
22
23 // ****************************************************************************
24
25 namespace RDKit {
26
27 class MolDraw2Dwx : public MolDraw2D {
28 public:
29 MolDraw2Dwx(int width, int height, wxDC &dc, int panelWidth = -1,
30 int panelHeight = -1)
31 : MolDraw2D(width, height, panelWidth, panelHeight), m_dc(dc) {
32 // m_dc.SetFont(wxFont(10, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL,
33 // wxFONTWEIGHT_NORMAL));
34 }
35
36 // set font size in molecule coordinate units. That's probably Angstrom for
37 // RDKit. It will turned into drawing units using scale_, which might be
38 // changed as a result, to make sure things still appear in the window.
39
40 void setFontSize(double new_size) {
41 MolDraw2D::setFontSize(new_size);
42 double font_size_in_points = fontSize() * scale();
43 wxFont font = m_dc.GetFont();
44 // font.SetPointSize(font_size_in_points);
45 font.SetPixelSize(wxSize(0, font_size_in_points));
46 m_dc.SetFont(font);
47 }
48
49 void setColour(const DrawColour &col) {
50 MolDraw2D::setColour(col);
51 double r = col.get<0>();
52 double g = col.get<1>();
53 double b = col.get<2>();
54 wxColour colour(r * 255, g * 255, b * 255);
55 m_dc.SetTextForeground(colour);
56 m_dc.SetPen(wxPen(colour));
57 m_dc.SetBrush(wxBrush(colour));
58 }
59
60 void drawLine(const Point2D &cds1, const Point2D &cds2) {
61 Point2D c1 = getDrawCoords(cds1);
62 Point2D c2 = getDrawCoords(cds2);
63 m_dc.DrawLine(c1.x, c1.y, c2.x, c2.y);
64 }
65
66 void drawChar(char c, const Point2D &cds) {
67 m_dc.DrawText(wxString(c), cds.x, cds.y);
68 }
69
70 void drawPolygon(const std::vector<Point2D> &cds) {
71 PRECONDITION(cds.size() >= 3, "must have at least three points");
72 wxPoint lines[cds.size()];
73 for (unsigned int i = 0; i < cds.size(); ++i) {
74 Point2D c1 = getDrawCoords(cds[i]);
75 lines[i] = wxPoint(c1.x, c1.y);
76 }
77 // FIX: deal with toggling fills
78 m_dc.DrawPolygon(cds.size(), lines);
79 };
80
81 void clearDrawing() {
82 const wxBrush &brush = m_dc.GetBrush();
83 const wxPen &pen = m_dc.GetPen();
84 setColour(drawOptions.backgroundColour);
85 m_dc.DrawRectangle(0, 0, width(), height());
86 m_dc.SetBrush(brush);
87 m_dc.SetPen(pen);
88 }
89
90 // using the current scale, work out the size of the label in molecule
91 // coordinates
92 void getStringSize(const std::string &label, double &label_width,
93 double &label_height) const {
94 if (m_dc.CanGetTextExtent()) {
95 wxCoord width, height;
96 m_dc.GetTextExtent(wxString(label), &width, &height);
97 label_width = double(width) / scale();
98 label_height = double(height) / scale();
99 }
100 }
101
102 private:
103 wxDC &m_dc;
104 };
105 }
106 #endif // MOLDRAW2DWX_H
RDKit::MolDraw2Dwx::setColour
void setColour(const DrawColour &col)
sets the current draw color
Definition: MolDraw2Dwx.h:49
RDKit::MolDraw2D::setColour
virtual void setColour(const DrawColour &col)
sets the current draw color
Definition: MolDraw2D.h:315
RDKit::MolDraw2D::height
virtual int height() const
return the height of the drawing area.
Definition: MolDraw2D.h:284
RDKit::MolDraw2D::colour
virtual DrawColour colour() const
returns the current draw color
Definition: MolDraw2D.h:317
RDKit::MolDraw2Dwx::drawPolygon
void drawPolygon(const std::vector< Point2D > &cds)
draw a polygon
Definition: MolDraw2Dwx.h:70
RDKit::MolDraw2Dwx::setFontSize
void setFontSize(double new_size)
Definition: MolDraw2Dwx.h:40
RDKit::MolDraw2Dwx::drawChar
void drawChar(char c, const Point2D &cds)
Definition: MolDraw2Dwx.h:66
RDGeom::Point2D::y
double y
Definition: point.h:256
RDGeom::Point2D
Definition: point.h:254
RDKit::MolDraw2D::panelHeight
virtual int panelHeight() const
return the height of the drawing panels.
Definition: MolDraw2D.h:288
MolDraw2D.h
RDKit::MolDraw2Dwx
Definition: MolDraw2Dwx.h:27
RDKit::MolDraw2Dwx::drawLine
void drawLine(const Point2D &cds1, const Point2D &cds2)
draws a line from cds1 to cds2 using the current drawing style
Definition: MolDraw2Dwx.h:60
RDKit::MolDraw2D::getDrawCoords
virtual Point2D getDrawCoords(const Point2D &mol_cds) const
RDKit::MolDraw2Dwx::MolDraw2Dwx
MolDraw2Dwx(int width, int height, wxDC &dc, int panelWidth=-1, int panelHeight=-1)
Definition: MolDraw2Dwx.h:29
RDKit::MolDraw2D::drawOptions
MolDrawOptions & drawOptions()
returns our current drawing options
Definition: MolDraw2D.h:379
RDKit::MolDrawOptions::backgroundColour
DrawColour backgroundColour
Definition: MolDraw2D.h:78
RDKit::MolDraw2Dwx::clearDrawing
void clearDrawing()
clears the contes of the drawingd]
Definition: MolDraw2Dwx.h:81
RDKit::MolDraw2D::setFontSize
virtual void setFontSize(double new_size)
RDKit::MolDraw2D::scale
double scale() const
returns the drawing scale (conversion from molecular coords -> drawing
Definition: MolDraw2D.h:292
RDKit
Std stuff.
Definition: Atom.h:29
RDKit::MolDraw2D::width
virtual int width() const
return the width of the drawing area.
Definition: MolDraw2D.h:282
RDKit::MolDraw2D::panelWidth
virtual int panelWidth() const
return the width of the drawing panels.
Definition: MolDraw2D.h:286
RDKit::MolDraw2D
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:132
PRECONDITION
#define PRECONDITION(expr, mess)
Definition: Invariant.h:107
RDGeom::Point2D::x
double x
Definition: point.h:256
RDKit::MolDraw2D::fontSize
virtual double fontSize() const
returns the font size (in nolecule units)
Definition: MolDraw2D.h:309
RDKit::DrawColour
boost::tuple< float, float, float > DrawColour
Definition: MolDraw2D.h:38
RDKit::MolDraw2Dwx::getStringSize
void getStringSize(const std::string &label, double &label_width, double &label_height) const
Definition: MolDraw2Dwx.h:92