File: catch_qt.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (180 lines) | stat: -rw-r--r-- 5,575 bytes parent folder | download | duplicates (2)
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
176
177
178
179
180
//
//  Copyright (C) 2019 Greg Landrum
//
//   @@ 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.
//
#define CATCH_CONFIG_RUNNER
#include <catch2/catch_all.hpp>

#include <GraphMol/RDKitBase.h>

#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/MolDraw2D/MolDraw2D.h>
#include <GraphMol/MolDraw2D/Qt/MolDraw2DQt.h>
#include <GraphMol/MolDraw2D/MolDraw2DUtils.h>
#include <GraphMol/MolDraw2D/MolDraw2DDetails.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <GraphMol/Depictor/RDDepictor.h>

#include <QImage>
#include <QPainter>
#include <QGuiApplication>

using namespace RDKit;

TEST_CASE("basic generate PNGs", "[drawing][Qt]") {
  SECTION("with freetype") {
    auto m1 = "C1N[C@@H]2OCC12"_smiles;
    REQUIRE(m1);
    QImage qimg(250, 200, QImage::Format_RGB32);
    QPainter qpt(&qimg);
    MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
    MolDraw2DUtils::prepareAndDrawMolecule(drawer, *m1);
    qimg.save("qttest-1a.png");
  }
  SECTION("no freetype") {
    auto m1 = "C1N[C@@H]2OCC12"_smiles;
    REQUIRE(m1);
    QImage qimg(250, 200, QImage::Format_RGB32);
    QPainter qpt(&qimg);
    bool no_freetype = true;
    MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt, -1, -1, no_freetype);
    MolDraw2DUtils::prepareAndDrawMolecule(drawer, *m1);
    qimg.save("qttest-1b.png");
  }
}

TEST_CASE("Github #4764") {
  SECTION("basics") {
    auto mol = "c1ccccc1-C1CCCCC1"_smiles;
    REQUIRE(mol);
    std::vector<int> highlights{6, 7, 8, 9, 10, 11};
    {
      QImage qimg(200, 150, QImage::Format_RGB32);
      QPainter qpt(&qimg);
      MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
      drawer.drawMolecule(*mol, "highlight", &highlights);
      qimg.save("testGithub4764.qt.sz1.png");
    }
    {
      QImage qimg(400, 350, QImage::Format_RGB32);
      QPainter qpt(&qimg);
      MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
      drawer.drawMolecule(*mol, "highlight", &highlights);
      qimg.save("testGithub4764.qt.sz2.png");
    }
    {
      QImage qimg(800, 700, QImage::Format_RGB32);
      QPainter qpt(&qimg);
      MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
      drawer.drawMolecule(*mol, "highlight", &highlights);
      qimg.save("testGithub4764.qt.sz3.png");
    }
  }
}

TEST_CASE("Github #5122: bad highlighting when bondLineWidth is increased") {
  SECTION("basics") {
    auto mol = "CC1=CC=C(C=C1)C1=CC=CC=C1"_smiles;
    REQUIRE(mol);
    std::vector<int> highlightAtoms{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    std::vector<int> highlightBonds{1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13};

    {
      QImage qimg(400, 400, QImage::Format_ARGB32);
      QPainter qpt(&qimg);
      MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
      drawer.drawOptions().bondLineWidth = 3;
      drawer.drawOptions().scaleHighlightBondWidth = true;
      drawer.drawMolecule(*mol, "highlight both", &highlightAtoms,
                          &highlightBonds);
      qimg.save("testGithub5122.qt.1.png");
    }
    {
      QImage qimg(400, 400, QImage::Format_ARGB32);
      QPainter qpt(&qimg);
      MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
      drawer.drawOptions().bondLineWidth = 3;
      drawer.drawOptions().scaleHighlightBondWidth = true;
      drawer.drawMolecule(*mol, "highlight bonds", nullptr, &highlightBonds);
      qimg.save("testGithub5122.qt.2.png");
    }
  }
}

TEST_CASE("wavy bonds and Qt") {
  {
    auto mol = "CC=CC"_smiles;
    REQUIRE(mol);
    mol->getBondWithIdx(1)->setStereoAtoms(0, 3);
    mol->getBondWithIdx(1)->setStereo(Bond::BondStereo::STEREOANY);
    bool kekulize = true;
    bool addChiralHs = true;
    bool wedgeBonds = true;
    bool forceCoords = true;
    bool wavyBonds = true;
    MolDraw2DUtils::prepareMolForDrawing(*mol, kekulize, addChiralHs,
                                         wedgeBonds, forceCoords, wavyBonds);
    CHECK(mol->getBondWithIdx(0)->getBondDir() == Bond::BondDir::UNKNOWN);
    CHECK(mol->getBondWithIdx(1)->getStereo() == Bond::BondStereo::STEREONONE);

    {
      QImage qimg(400, 400, QImage::Format_ARGB32);
      QPainter qpt(&qimg);
      MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
      drawer.drawMolecule(*mol, "wavy bonds");
      qimg.save("testWavyBonds.qt.1.png");
    }
  }
}

TEST_CASE("drawMoleculeBrackets") {
  SECTION("basics") {
    auto mol = R"CTAB(
  ACCLDraw11042015112D

  0  0  0     0  0            999 V3000
M  V30 BEGIN CTAB
M  V30 COUNTS 5 4 1 0 0
M  V30 BEGIN ATOM
M  V30 1 C 7 -6.7813 0 0 
M  V30 2 C 8.0229 -6.1907 0 0 CFG=3 
M  V30 3 C 8.0229 -5.0092 0 0 
M  V30 4 C 9.046 -6.7814 0 0 
M  V30 5 C 10.0692 -6.1907 0 0 
M  V30 END ATOM
M  V30 BEGIN BOND
M  V30 1 1 1 2 
M  V30 2 1 2 3 
M  V30 3 1 2 4 
M  V30 4 1 4 5 
M  V30 END BOND
M  V30 BEGIN SGROUP
M  V30 1 SRU 1 ATOMS=(3 3 2 4) XBONDS=(2 1 4) BRKXYZ=(9 7.51 -7.08 0 7.51 -
M  V30 -5.9 0 0 0 0) BRKXYZ=(9 9.56 -5.9 0 9.56 -7.08 0 0 0 0) -
M  V30 CONNECT=HT LABEL=n 
M  V30 END SGROUP
M  V30 END CTAB
M  END
)CTAB"_ctab;
    REQUIRE(mol);
    {
      QImage qimg(400, 400, QImage::Format_ARGB32);
      QPainter qpt(&qimg);
      MolDraw2DQt drawer(qimg.width(), qimg.height(), &qpt);
      drawer.drawMolecule(*mol, "brackets");
      qimg.save("testBrackets-qt-1a.png");
    }
  }
}
int main(int argc, char *argv[]) {
  QGuiApplication app(argc, argv);

  int result = Catch::Session().run(argc, argv);

  return result;
}