RDKit
Open-source cheminformatics and machine learning.
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 
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) {
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();
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
void setColour(const DrawColour &col)
Definition: MolDraw2Dwx.h:49
virtual void setColour(const DrawColour &col)
Definition: MolDraw2D.h:168
virtual int height() const
Definition: MolDraw2D.h:148
virtual DrawColour colour() const
Definition: MolDraw2D.h:169
void drawPolygon(const std::vector< Point2D > &cds)
Definition: MolDraw2Dwx.h:70
void setFontSize(double new_size)
Definition: MolDraw2Dwx.h:40
void drawChar(char c, const Point2D &cds)
Definition: MolDraw2Dwx.h:66
double y
Definition: point.h:256
virtual int panelHeight() const
Definition: MolDraw2D.h:150
void drawLine(const Point2D &cds1, const Point2D &cds2)
Definition: MolDraw2Dwx.h:60
virtual Point2D getDrawCoords(const Point2D &mol_cds) const
MolDraw2Dwx(int width, int height, wxDC &dc, int panelWidth=-1, int panelHeight=-1)
Definition: MolDraw2Dwx.h:29
MolDrawOptions & drawOptions()
Definition: MolDraw2D.h:217
DrawColour backgroundColour
Definition: MolDraw2D.h:54
virtual void setFontSize(double new_size)
double scale() const
Definition: MolDraw2D.h:152
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
virtual int width() const
Definition: MolDraw2D.h:147
virtual int panelWidth() const
Definition: MolDraw2D.h:149
#define PRECONDITION(expr, mess)
Definition: Invariant.h:107
double x
Definition: point.h:256
virtual double fontSize() const
Definition: MolDraw2D.h:163
boost::tuple< float, float, float > DrawColour
Definition: MolDraw2D.h:37
void getStringSize(const std::string &label, double &label_width, double &label_height) const
Definition: MolDraw2Dwx.h:92