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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
#include <iostream.h>
#include <math.h>
#include <qwidget.h>
#include <qpainter.h>
#include <qprinter.h>
#include <qpaintdevicemetrics.h>
#include <qpointarray.h>
#include <qrect.h>
#include <qpixmap.h>
#include "paintable.h"
#include "render2d.h"
#include "chemdata.h"
#include "defs.h"
void Render2D::PrintSetup() {
printer = new QPrinter;
// the following statement is needed for Qt 3.0 or higher
#if QT_VERSION >= 300
printer->setResolution(100); // for convenient calculation of line length
#endif
printer->setFullPage(true);
printer->setPageSize(QPrinter::Letter);
printer->setOrientation(QPrinter::Portrait);
printer->setColorMode(QPrinter::GrayScale);
}
void Render2D::UpdatePageGeometry() {
if (page_orientation == PAGE_PORTRAIT) {
printer->setOrientation(QPrinter::Portrait);
switch (page_size) {
case PAGE_LETTER:
printer->setPageSize(QPrinter::Letter);
renderHeight = 1000;
renderWidth = 750;
break;
case PAGE_LEGAL:
printer->setPageSize(QPrinter::Legal);
renderHeight = 1300;
renderWidth = 750;
break;
case PAGE_A4:
printer->setPageSize(QPrinter::A4);
renderHeight = 1070;
renderWidth = 776;
break;
case PAGE_640:
renderWidth = 640;
renderWidth = 480;
break;
case PAGE_800:
renderWidth = 800;
renderWidth = 600;
break;
case PAGE_1024:
renderWidth = 1024;
renderWidth = 768;
break;
default:
break;
}
} else { // page_orientation == PAGE_LANDSCAPE
printer->setOrientation(QPrinter::Landscape);
switch (page_size) {
case PAGE_LETTER:
printer->setPageSize(QPrinter::Letter);
renderHeight = 750;
renderWidth = 1000;
break;
case PAGE_LEGAL:
printer->setPageSize(QPrinter::Legal);
renderHeight = 750;
renderWidth = 1300;
break;
case PAGE_A4:
printer->setPageSize(QPrinter::A4);
renderHeight = 776;
renderWidth = 1070;
break;
case PAGE_640:
renderWidth = 640;
renderWidth = 480;
break;
case PAGE_800:
renderWidth = 800;
renderWidth = 600;
break;
case PAGE_1024:
renderWidth = 1024;
renderWidth = 768;
break;
default:
break;
}
}
resize(renderWidth, renderHeight);
}
void Render2D::Print() {
if (!printer->setup(this)) {
emit SignalSetStatusBar( QString("Printing canceled.") );
return;
}
//QPaintDeviceMetrics pm(printer);
c->DeselectAll();
// set output device
outputDevice = OUTPUT_PRINTER;
// render all objects
paintqueue.clear();
repaint();
QPainter p(printer);
Paintable *tmp_paint;
for (tmp_paint = paintqueue.first(); tmp_paint != NULL;
tmp_paint = paintqueue.next()) {
QPoint a = tmp_paint->a;
QPoint b = tmp_paint->b;
QColor c1 = tmp_paint->c;
if (tmp_paint->op == OP_LINE) {
if (tmp_paint->s == 0)
p.setPen(tmp_paint->c);
if (tmp_paint->s == 1) // dashed line
p.setPen(QPen(tmp_paint->c,0,DotLine));
p.drawLine(tmp_paint->a, tmp_paint->b);
}
if (tmp_paint->op == OP_FILLBOX) {
p.setPen(c1);
p.fillRect(QRect(a, b), c1);
}
if (tmp_paint->op == OP_DASH_LINE) {
p.setPen(QPen(tmp_paint->c,0,DotLine));
p.drawLine(tmp_paint->a, tmp_paint->b);
}
if (tmp_paint->op == OP_TEXT) {
p.setPen(tmp_paint->c);
p.setFont(tmp_paint->f);
p.drawText(tmp_paint->a, tmp_paint->ch);
}
if (tmp_paint->op == OP_CURVE_CW180) {
// calculate curve
p.setPen(tmp_paint->c);
QPoint ce = Midpoint(tmp_paint->a, tmp_paint->b);
int d = RoundOff(DistanceBetween(tmp_paint->a, ce));
double sa = getAngle(ce, tmp_paint->a);
QPointArray pa;
pa.makeArc(ce.x() - d, ce.y() - d, 2*d, 2*d, -sa*16, -2880);
p.drawPolyline(pa);
// calculate arrowhead
// if curve too small, don't draw arrowhead
if (pa.count() == 0) return;
QPoint realb(pa.at(pa.count() - 1));
sa = getAngle(tmp_paint->b, tmp_paint->a);
double newang1 = sa + 60.0;
double newang2 = sa + 120.0;
QPoint a1(realb.x() + (cos(newang1/MOL_ARAD) * 10.0),
realb.y() + (sin(newang1/MOL_ARAD) * 10.0));
QPoint a2(realb.x() + (cos(newang2/MOL_ARAD) * 10.0),
realb.y() + (sin(newang2/MOL_ARAD) * 10.0));
p.drawLine(realb, a1);
p.drawLine(realb, a2);
}
if (tmp_paint->op == OP_CURVE_CCW180) {
// calculate curve
p.setPen(tmp_paint->c);
QPoint ce = Midpoint(tmp_paint->a, tmp_paint->b);
int d = RoundOff(DistanceBetween(tmp_paint->a, ce));
double sa = getAngle(ce, tmp_paint->a);
QPointArray pa;
pa.makeArc(ce.x() - d, ce.y() - d, 2*d, 2*d, -sa*16, 2880);
p.drawPolyline(pa);
// calculate arrowhead
// if curve too small, don't draw arrowhead
if (pa.count() == 0) return;
QPoint realb(pa.at(pa.count() - 1));
sa = getAngle(tmp_paint->b, tmp_paint->a);
double newang1 = sa + 60.0;
double newang2 = sa + 120.0;
QPoint a1(realb.x() + (cos(newang1/MOL_ARAD) * 10.0),
realb.y() + (sin(newang1/MOL_ARAD) * 10.0));
QPoint a2(realb.x() + (cos(newang2/MOL_ARAD) * 10.0),
realb.y() + (sin(newang2/MOL_ARAD) * 10.0));
p.drawLine(realb, a1);
p.drawLine(realb, a2);
}
if (tmp_paint->op == OP_PIXMAP) {
p.drawPixmap(tmp_paint->a, tmp_paint->p);
}
if (tmp_paint->op == OP_CURVE_CW90) {
// first, figure out where middle of circle is
double d1 = DistanceBetween(a, b);
double ia = getAngle(a, b);
d1 = d1 / 1.4142136;
ia = 45.0;
double dx1 = ( (double)b.x() - (double)a.x() ) / 1.4142136;
double dy1 = ( (double)b.y() - (double)a.y() ) / 1.4142136;
// rotate vector (dx1, dy1) 45 degrees clockwise
double ia_rad = ia * M_PI / 180.0;
double dx2 = dx1*cos(ia_rad) - dy1*sin(ia_rad);
double dy2 = dx1*sin(ia_rad) + dy1*cos(ia_rad);
QPoint ce(a.x() + RoundOff(dx2), a.y() + RoundOff(dy2));
double sa = getAngle(ce, a);
QPointArray pa;
int d = RoundOff(d1);
pa.makeArc(ce.x() - d, ce.y() - d, 2*d, 2*d, -sa*16, -1440);
drawPolyline(pa, c1);
// calculate arrowhead
// if curve too small, don't draw arrowhead
if (pa.count() == 0) return;
QPoint realb(pa.at(pa.count() - 1));
sa = getAngle(b, a);
double newang1 = sa + 15.0;
double newang2 = sa + 75.0;
QPoint a1(realb.x() + (cos(newang1/MOL_ARAD) * 10.0),
realb.y() + (sin(newang1/MOL_ARAD) * 10.0));
QPoint a2(realb.x() + (cos(newang2/MOL_ARAD) * 10.0),
realb.y() + (sin(newang2/MOL_ARAD) * 10.0));
drawLine(realb, a1, 1, c1);
drawLine(realb, a2, 1, c1);
}
if (tmp_paint->op == OP_CURVE_CCW90) {
// first, figure out where middle of circle is
double d1 = DistanceBetween(a, b);
double ia = getAngle(a, b);
d1 = d1 / 1.4142136;
ia = -45.0;
double dx1 = ( (double)b.x() - (double)a.x() ) / 1.4142136;
double dy1 = ( (double)b.y() - (double)a.y() ) / 1.4142136;
// rotate vector (dx1, dy1) 45 degrees clockwise
double ia_rad = ia * M_PI / 180.0;
double dx2 = dx1*cos(ia_rad) - dy1*sin(ia_rad);
double dy2 = dx1*sin(ia_rad) + dy1*cos(ia_rad);
QPoint ce(a.x() + RoundOff(dx2), a.y() + RoundOff(dy2));
double sa = getAngle(ce, a);
QPointArray pa;
int d = RoundOff(d1);
pa.makeArc(ce.x() - d, ce.y() - d, 2*d, 2*d, -sa*16, 1440);
drawPolyline(pa, c1);
// calculate arrowhead
// if curve too small, don't draw arrowhead
if (pa.count() == 0) return;
QPoint realb(pa.at(pa.count() - 1));
sa = getAngle(b, a);
double newang1 = sa - 15.0;
double newang2 = sa - 75.0;
QPoint a1(realb.x() + (cos(newang1/MOL_ARAD) * 10.0),
realb.y() + (sin(newang1/MOL_ARAD) * 10.0));
QPoint a2(realb.x() + (cos(newang2/MOL_ARAD) * 10.0),
realb.y() + (sin(newang2/MOL_ARAD) * 10.0));
drawLine(realb, a1, 1, c1);
drawLine(realb, a2, 1, c1);
}
if (tmp_paint->op == OP_CURVE_CW270) {
// first, figure out where middle of circle is
double d1 = DistanceBetween(a, b);
double ia = getAngle(a, b);
d1 = d1 / 1.4142136;
ia = -45.0;
double dx1 = ( (double)b.x() - (double)a.x() ) / 1.4142136;
double dy1 = ( (double)b.y() - (double)a.y() ) / 1.4142136;
// rotate vector (dx1, dy1) 45 degrees clockwise
double ia_rad = ia * M_PI / 180.0;
double dx2 = dx1*cos(ia_rad) - dy1*sin(ia_rad);
double dy2 = dx1*sin(ia_rad) + dy1*cos(ia_rad);
QPoint ce(a.x() + RoundOff(dx2), a.y() + RoundOff(dy2));
double sa = getAngle(ce, a);
QPointArray pa;
int d = RoundOff(d1);
pa.makeArc(ce.x() - d, ce.y() - d, 2*d, 2*d, -sa*16, -4320);
drawPolyline(pa, c1);
// calculate arrowhead
// if curve too small, don't draw arrowhead
if (pa.count() == 0) return;
QPoint realb(pa.at(pa.count() - 1));
sa = getAngle(b, a);
double newang1 = sa + 165.0;
double newang2 = sa + 105.0;
QPoint a1(realb.x() + (cos(newang1/MOL_ARAD) * 10.0),
realb.y() + (sin(newang1/MOL_ARAD) * 10.0));
QPoint a2(realb.x() + (cos(newang2/MOL_ARAD) * 10.0),
realb.y() + (sin(newang2/MOL_ARAD) * 10.0));
drawLine(realb, a1, 1, c1);
drawLine(realb, a2, 1, c1);
}
if (tmp_paint->op == OP_CURVE_CCW270) {
// first, figure out where middle of circle is
double d1 = DistanceBetween(a, b);
double ia = getAngle(a, b);
d1 = d1 / 1.4142136;
ia = 45.0;
double dx1 = ( (double)b.x() - (double)a.x() ) / 1.4142136;
double dy1 = ( (double)b.y() - (double)a.y() ) / 1.4142136;
// rotate vector (dx1, dy1) 45 degrees clockwise
double ia_rad = ia * M_PI / 180.0;
double dx2 = dx1*cos(ia_rad) - dy1*sin(ia_rad);
double dy2 = dx1*sin(ia_rad) + dy1*cos(ia_rad);
QPoint ce(a.x() + RoundOff(dx2), a.y() + RoundOff(dy2));
double sa = getAngle(ce, a);
QPointArray pa;
int d = RoundOff(d1);
pa.makeArc(ce.x() - d, ce.y() - d, 2*d, 2*d, -sa*16, 4320);
drawPolyline(pa, c1);
// calculate arrowhead
// if curve too small, don't draw arrowhead
if (pa.count() == 0) return;
QPoint realb(pa.at(pa.count() - 1));
sa = getAngle(b, a);
double newang1 = sa - 165.0;
double newang2 = sa - 105.0;
QPoint a1(realb.x() + (cos(newang1/MOL_ARAD) * 10.0),
realb.y() + (sin(newang1/MOL_ARAD) * 10.0));
QPoint a2(realb.x() + (cos(newang2/MOL_ARAD) * 10.0),
realb.y() + (sin(newang2/MOL_ARAD) * 10.0));
drawLine(realb, a1, 1, c1);
drawLine(realb, a2, 1, c1);
}
}
// set output to screen
outputDevice = OUTPUT_SCREEN;
repaint();
}
|