RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
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#include <RDGeneral/export.h>
14#ifndef MOLDRAW2DWX_H
15#define MOLDRAW2DWX_H
16
18#include <wx/dc.h>
19#include <wx/font.h>
20#include <wx/pen.h>
21#include <wx/colour.h>
22#include <wx/brush.h>
23
24// ****************************************************************************
25
26namespace RDKit {
27
29 public:
30 MolDraw2Dwx(int width, int height, wxDC &dc, int panelWidth = -1,
31 int panelHeight = -1)
32 : MolDraw2D(width, height, panelWidth, panelHeight), m_dc(dc) {
33 // m_dc.SetFont(wxFont(10, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL,
34 // wxFONTWEIGHT_NORMAL));
35 }
36
37 // set font size in molecule coordinate units. That's probably Angstrom for
38 // RDKit. It will turned into drawing units using scale_, which might be
39 // changed as a result, to make sure things still appear in the window.
40
41 void setFontSize(double new_size) {
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 MolDraw2D::setColour(col);
52 double r = col.get<0>();
53 double g = col.get<1>();
54 double b = col.get<2>();
55 wxColour colour(r * 255, g * 255, b * 255);
56 m_dc.SetTextForeground(colour);
57 m_dc.SetPen(wxPen(colour));
58 m_dc.SetBrush(wxBrush(colour));
59 }
60
61 void drawLine(const Point2D &cds1, const Point2D &cds2) {
62 Point2D c1 = getDrawCoords(cds1);
63 Point2D c2 = getDrawCoords(cds2);
64 m_dc.DrawLine(c1.x, c1.y, c2.x, c2.y);
65 }
66
67 void drawChar(char c, const Point2D &cds) {
68 m_dc.DrawText(wxString(c), cds.x, cds.y);
69 }
70
71 void drawPolygon(const std::vector<Point2D> &cds) {
72 PRECONDITION(cds.size() >= 3, "must have at least three points");
73 wxPoint lines[cds.size()];
74 for (unsigned int i = 0; i < cds.size(); ++i) {
75 Point2D c1 = getDrawCoords(cds[i]);
76 lines[i] = wxPoint(c1.x, c1.y);
77 }
78 // FIX: deal with toggling fills
79 m_dc.DrawPolygon(cds.size(), lines);
80 }
81
82 void clearDrawing() {
83 const wxBrush &brush = m_dc.GetBrush();
84 const wxPen &pen = m_dc.GetPen();
85 setColour(drawOptions.backgroundColour);
86 m_dc.DrawRectangle(0, 0, width(), height());
87 m_dc.SetBrush(brush);
88 m_dc.SetPen(pen);
89 }
90
91 // using the current scale, work out the size of the label in molecule
92 // coordinates
93 void getStringSize(const std::string &label, double &label_width,
94 double &label_height) const {
95 if (m_dc.CanGetTextExtent()) {
96 wxCoord width, height;
97 m_dc.GetTextExtent(wxString(label), &width, &height);
98 label_width = double(width) / scale();
99 label_height = double(height) / scale();
100 }
101 }
102
103 private:
104 wxDC &m_dc;
105};
106} // namespace RDKit
107#endif // MOLDRAW2DWX_H
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition MolDraw2D.h:47
void getStringSize(const std::string &label, double &label_width, double &label_height) const
Definition MolDraw2Dwx.h:93
void setColour(const DrawColour &col)
sets the current draw color
Definition MolDraw2Dwx.h:50
void drawPolygon(const std::vector< Point2D > &cds)
Definition MolDraw2Dwx.h:71
void drawChar(char c, const Point2D &cds)
Definition MolDraw2Dwx.h:67
void clearDrawing()
clears the contents of the drawing
Definition MolDraw2Dwx.h:82
void drawLine(const Point2D &cds1, const Point2D &cds2)
Definition MolDraw2Dwx.h:61
void setFontSize(double new_size)
Definition MolDraw2Dwx.h:41
MolDraw2Dwx(int width, int height, wxDC &dc, int panelWidth=-1, int panelHeight=-1)
Definition MolDraw2Dwx.h:30
#define RDKIT_MOLDRAW2D_EXPORT
Definition export.h:297
Std stuff.