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 
29  public :
30 
31  MolDraw2Dwx( int width , int height , wxDC &dc ) : MolDraw2D( width , height ), m_dc(dc)
32  {
33  // m_dc.SetFont(wxFont(10, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, 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  {
42  MolDraw2D::setFontSize( new_size );
43  double font_size_in_points = fontSize() * scale();
44  wxFont font = m_dc.GetFont();
45  //font.SetPointSize(font_size_in_points);
46  font.SetPixelSize(wxSize(0,font_size_in_points));
47  m_dc.SetFont(font);
48  }
49 
50  void setColour( const DrawColour &col )
51  {
52  MolDraw2D::setColour( col );
53  double r = col.get<0>();
54  double g = col.get<1>();
55  double b = col.get<2>();
56  wxColour colour(r * 255, g * 255, b * 255);
57  m_dc.SetTextForeground(colour);
58  m_dc.SetPen(wxPen(colour));
59  m_dc.SetBrush(wxBrush(colour));
60  }
61 
62  void drawLine( const Point2D &cds1, const Point2D &cds2 )
63  {
64  Point2D c1 = getDrawCoords( cds1 );
65  Point2D c2 = getDrawCoords( cds2 );
66  m_dc.DrawLine(c1.x,c1.y,c2.x,c2.y);
67  }
68 
69  void drawChar( char c , const Point2D &cds )
70  {
71  m_dc.DrawText(wxString(c),cds.x,cds.y);
72  }
73 
74  void drawPolygon( const std::vector<Point2D > &cds ){
75  PRECONDITION(cds.size()>=3,"must have at least three points");
76  wxPoint lines[cds.size()];
77  for(unsigned int i=0;i<cds.size();++i){
78  Point2D c1 = getDrawCoords( cds[i] );
79  lines[i] = wxPoint(c1.x,c1.y);
80  }
81  // FIX: deal with toggling fills
82  m_dc.DrawPolygon(cds.size(),lines);
83  };
84 
85  void clearDrawing()
86  {
87  wxColour backgroundColour = m_dc.GetTextBackground();
88  if (!backgroundColour.Ok())
89  backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
90  const wxBrush &brush = m_dc.GetBrush();
91  const wxPen &pen = m_dc.GetPen();
92  m_dc.SetBrush(wxBrush(backgroundColour));
93  m_dc.SetPen(wxPen(backgroundColour, 1));
94  m_dc.DrawRectangle(0,0, width(), height());
95  m_dc.SetBrush(brush);
96  m_dc.SetPen(pen);
97  }
98 
99  // using the current scale, work out the size of the label in molecule coordinates
100  void getStringSize( const std::string &label, double &label_width, double &label_height ) const
101  {
102  if (m_dc.CanGetTextExtent())
103  {
104  wxCoord width, height;
105  m_dc.GetTextExtent(wxString(label), &width, &height);
106  label_width = double(width) / scale();
107  label_height = double(height) / scale();
108  }
109  }
110 
111  private :
112 
113  wxDC &m_dc;
114 
115  };
116 
117 }
118 #endif // MOLDRAW2DWX_H
void setColour(const DrawColour &col)
Definition: MolDraw2Dwx.h:50
virtual void setColour(const DrawColour &col)
Definition: MolDraw2D.h:105
virtual int height() const
Definition: MolDraw2D.h:95
virtual DrawColour colour() const
Definition: MolDraw2D.h:106
void drawPolygon(const std::vector< Point2D > &cds)
Definition: MolDraw2Dwx.h:74
void setFontSize(double new_size)
Definition: MolDraw2Dwx.h:40
void drawChar(char c, const Point2D &cds)
Definition: MolDraw2Dwx.h:69
double y
Definition: point.h:265
MolDraw2Dwx(int width, int height, wxDC &dc)
Definition: MolDraw2Dwx.h:31
boost::tuple< float, float, float > DrawColour
Definition: MolDraw2D.h:37
void drawLine(const Point2D &cds1, const Point2D &cds2)
Definition: MolDraw2Dwx.h:62
virtual Point2D getDrawCoords(const Point2D &mol_cds) const
virtual void setFontSize(double new_size)
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
virtual int width() const
Definition: MolDraw2D.h:94
virtual double scale() const
Definition: MolDraw2D.h:97
#define PRECONDITION(expr, mess)
Definition: Invariant.h:119
double x
Definition: point.h:265
virtual double fontSize() const
Definition: MolDraw2D.h:100
void getStringSize(const std::string &label, double &label_width, double &label_height) const
Definition: MolDraw2Dwx.h:100