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)
30  : MolDraw2D(width, height), m_dc(dc) {
31  // m_dc.SetFont(wxFont(10, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL,
32  // wxFONTWEIGHT_NORMAL));
33  }
34 
35  // set font size in molecule coordinate units. That's probably Angstrom for
36  // RDKit. It will turned into drawing units using scale_, which might be
37  // changed as a result, to make sure things still appear in the window.
38 
39  void setFontSize(double new_size) {
40  MolDraw2D::setFontSize(new_size);
41  double font_size_in_points = fontSize() * scale();
42  wxFont font = m_dc.GetFont();
43  // font.SetPointSize(font_size_in_points);
44  font.SetPixelSize(wxSize(0, font_size_in_points));
45  m_dc.SetFont(font);
46  }
47 
48  void setColour(const DrawColour &col) {
50  double r = col.get<0>();
51  double g = col.get<1>();
52  double b = col.get<2>();
53  wxColour colour(r * 255, g * 255, b * 255);
54  m_dc.SetTextForeground(colour);
55  m_dc.SetPen(wxPen(colour));
56  m_dc.SetBrush(wxBrush(colour));
57  }
58 
59  void drawLine(const Point2D &cds1, const Point2D &cds2) {
60  Point2D c1 = getDrawCoords(cds1);
61  Point2D c2 = getDrawCoords(cds2);
62  m_dc.DrawLine(c1.x, c1.y, c2.x, c2.y);
63  }
64 
65  void drawChar(char c, const Point2D &cds) {
66  m_dc.DrawText(wxString(c), cds.x, cds.y);
67  }
68 
69  void drawPolygon(const std::vector<Point2D> &cds) {
70  PRECONDITION(cds.size() >= 3, "must have at least three points");
71  wxPoint lines[cds.size()];
72  for (unsigned int i = 0; i < cds.size(); ++i) {
73  Point2D c1 = getDrawCoords(cds[i]);
74  lines[i] = wxPoint(c1.x, c1.y);
75  }
76  // FIX: deal with toggling fills
77  m_dc.DrawPolygon(cds.size(), lines);
78  };
79 
80  void clearDrawing() {
81  const wxBrush &brush = m_dc.GetBrush();
82  const wxPen &pen = m_dc.GetPen();
84  m_dc.DrawRectangle(0, 0, width(), height());
85  m_dc.SetBrush(brush);
86  m_dc.SetPen(pen);
87  }
88 
89  // using the current scale, work out the size of the label in molecule
90  // coordinates
91  void getStringSize(const std::string &label, double &label_width,
92  double &label_height) const {
93  if (m_dc.CanGetTextExtent()) {
94  wxCoord width, height;
95  m_dc.GetTextExtent(wxString(label), &width, &height);
96  label_width = double(width) / scale();
97  label_height = double(height) / scale();
98  }
99  }
100 
101  private:
102  wxDC &m_dc;
103 };
104 }
105 #endif // MOLDRAW2DWX_H
void setColour(const DrawColour &col)
Definition: MolDraw2Dwx.h:48
virtual void setColour(const DrawColour &col)
Definition: MolDraw2D.h:139
virtual double fontSize() const
Definition: MolDraw2D.h:134
virtual int height() const
Definition: MolDraw2D.h:129
void drawPolygon(const std::vector< Point2D > &cds)
Definition: MolDraw2Dwx.h:69
void setFontSize(double new_size)
Definition: MolDraw2Dwx.h:39
void drawChar(char c, const Point2D &cds)
Definition: MolDraw2Dwx.h:65
double y
Definition: point.h:256
void getStringSize(const std::string &label, double &label_width, double &label_height) const
Definition: MolDraw2Dwx.h:91
MolDraw2Dwx(int width, int height, wxDC &dc)
Definition: MolDraw2Dwx.h:29
virtual DrawColour colour() const
Definition: MolDraw2D.h:140
void drawLine(const Point2D &cds1, const Point2D &cds2)
Definition: MolDraw2Dwx.h:59
MolDrawOptions & drawOptions()
Definition: MolDraw2D.h:185
DrawColour backgroundColour
Definition: MolDraw2D.h:53
virtual void setFontSize(double new_size)
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
virtual double scale() const
Definition: MolDraw2D.h:131
virtual int width() const
Definition: MolDraw2D.h:128
#define PRECONDITION(expr, mess)
Definition: Invariant.h:103
double x
Definition: point.h:256
virtual Point2D getDrawCoords(const Point2D &mol_cds) const
boost::tuple< float, float, float > DrawColour
Definition: MolDraw2D.h:37