File: MolDraw2DQt.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (175 lines) | stat: -rw-r--r-- 5,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
// Original author: David Cosgrove (AstraZeneca)
// 19th June 2014
//

#include "MolDraw2DQt.h"

#include <QPainter>
#include <QString>

using namespace boost;
using namespace std;

namespace RDKit {

// ****************************************************************************
MolDraw2DQt::MolDraw2DQt(int width, int height, QPainter &qp, int panelWidth,
                         int panelHeight)
    : MolDraw2D(width, height, panelWidth, panelHeight), qp_(qp) {}

// ****************************************************************************
void MolDraw2DQt::setColour(const DrawColour &col) {
  MolDraw2D::setColour(col);
  QColor this_col(int(255.0 * col.get<0>()), int(255.0 * col.get<1>()),
                  int(255.0 * col.get<2>()));

  QPen pen(this_col);
  pen.setJoinStyle(Qt::RoundJoin);
  pen.setColor(this_col);
  qp_.setPen(pen);

  QBrush brush(this_col);
  brush.setStyle(Qt::SolidPattern);
  qp_.setBrush(brush);
}

// ****************************************************************************
void MolDraw2DQt::drawLine(const Point2D &cds1, const Point2D &cds2) {
  Point2D c1 = getDrawCoords(cds1);
  Point2D c2 = getDrawCoords(cds2);

  const DashPattern &dashes = dash();
  QPen pen = qp_.pen();
  if (dashes.size()) {
    QVector<qreal> dd;
    for (unsigned int di = 0; di < dashes.size(); ++di) dd << dashes[di];
    pen.setDashPattern(dd);
  } else {
    pen.setStyle(Qt::SolidLine);
  }
  pen.setWidth(lineWidth());
  qp_.setPen(pen);
  qp_.drawLine(QPointF(c1.x, c1.y), QPointF(c2.x, c2.y));
}

// ****************************************************************************
// draw the char, with the bottom left hand corner at cds
void MolDraw2DQt::drawChar(char c, const Point2D &cds) {
  QRectF br = qp_.boundingRect(0, 0, 100, 100, Qt::AlignLeft | Qt::AlignBottom,
                               QString(c));
  qp_.drawText(QRectF(cds.x, cds.y - br.height(), br.width(), br.height()),
               Qt::AlignLeft | Qt::AlignBottom, QString(c), &br);
}

// ****************************************************************************
void MolDraw2DQt::drawPolygon(const vector<Point2D> &cds) {
  PRECONDITION(cds.size() >= 3, "must have at least three points");
#ifdef NOTYET
  QBrush brush("Black");
  brush.setStyle(Qt::SolidPattern);
  DrawColour cc = colour();
  brush.setColor(
      QColor(255.0 * cc.get<0>(), 255.0 * cc.get<1>(), 255.0 * cc.get<2>()));
#endif

  qp_.save();
  QBrush brush = qp_.brush();
  if (fillPolys())
    brush.setStyle(Qt::SolidPattern);
  else
    brush.setStyle(Qt::NoBrush);
  qp_.setBrush(brush);

  QPointF points[cds.size()];
  for (unsigned int i = 0; i < cds.size(); ++i) {
    Point2D lc = getDrawCoords(cds[i]);
    points[i] = QPointF(lc.x, lc.y);
  }
  qp_.drawConvexPolygon(points, cds.size());
  qp_.restore();
}

// ****************************************************************************
void MolDraw2DQt::clearDrawing() {
  QColor this_col(int(255.0 * drawOptions().backgroundColour.get<0>()),
                  int(255.0 * drawOptions().backgroundColour.get<1>()),
                  int(255.0 * drawOptions().backgroundColour.get<2>()));

  qp_.setBackground(QBrush(this_col));
  qp_.fillRect(0, 0, width(), height(), this_col);
}

// ****************************************************************************
void MolDraw2DQt::setFontSize(double new_size) {
  MolDraw2D::setFontSize(new_size);
  double font_size_in_points = fontSize() * scale();
#ifdef NOTYET
  cout << "initial font size in points : " << qp_.font().pointSizeF() << endl;
  cout << "font_size_in_points : " << font_size_in_points << endl;
#endif
  QFont font(qp_.font());
  font.setPointSizeF(font_size_in_points);
  qp_.setFont(font);

  while (1) {
    double old_font_size_in_points = font_size_in_points;
    double font_size_in_points = fontSize() * scale();
    if (fabs(font_size_in_points - old_font_size_in_points) < 0.1) {
      break;
    }
    QFont font(qp_.font());
    font.setPointSizeF(font_size_in_points);
    qp_.setFont(font);
    calculateScale();
  }
}

// ****************************************************************************
// using the current scale, work out the size of the label in molecule
// coordinates
void MolDraw2DQt::getStringSize(const string &label, double &label_width,
                                double &label_height) const {
  label_width = 0.0;
  label_height = 0.0;

  TextDrawType draw_mode =
      TextDrawNormal;  // 0 for normal, 1 for superscript, 2 for subscript
  QString next_char(" ");
  bool had_a_super = false;

  for (int i = 0, is = label.length(); i < is; ++i) {
    // setStringDrawMode moves i along to the end of any <sub> or <sup>
    // markup
    if ('<' == label[i] && setStringDrawMode(label, draw_mode, i)) {
      continue;
    }

    next_char[0] = label[i];
    QRectF br = qp_.boundingRect(0, 0, 100, 100,
                                 Qt::AlignBottom | Qt::AlignLeft, next_char);
    label_height = br.height() / scale();
    double char_width = br.width() / scale();
    if (TextDrawSubscript == draw_mode) {
      char_width *= 0.5;
    } else if (TextDrawSuperscript == draw_mode) {
      char_width *= 0.5;
      had_a_super = true;
    }
    label_width += char_width;
  }

  // subscript keeps its bottom in line with the bottom of the bit chars,
  // superscript goes above the original char top by a quarter
  if (had_a_super) {
    label_height *= 1.25;
  }
}

}  // EO namespace RDKit