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 322 323 324 325 326 327 328 329
|
#include <BALL/VIEW/WIDGETS/SDWidget.h>
#include <BALL/VIEW/KERNEL/iconLoader.h>
#include <BALL/KERNEL/bond.h>
#include <BALL/KERNEL/PTE.h>
#include <BALL/STRUCTURE/geometricProperties.h>
#include <BALL/STRUCTURE/sdGenerator.h>
#include <QtGui/QPainter>
#include <QtWidgets/QStyle>
#include <QtWidgets/QStyleOptionFocusRect>
#include <QtWidgets/QAction>
#include <QtWidgets/QFileDialog>
#include <QtGui/QImageWriter>
#include <set>
namespace BALL
{
namespace VIEW
{
const char* SDWidget::Option::SHOW_HYDROGENS = "sd_widget_show_hydrogens";
const bool SDWidget::Default::SHOW_HYDROGENS = false;
SDWidget::SDWidget(QWidget *parent, bool show_hydrogens)
: QWidget(parent)
{
setup_();
options[SDWidget::Option::SHOW_HYDROGENS] = show_hydrogens;
}
SDWidget::SDWidget(const System& system, QWidget *parent)
: QWidget(parent)
{
setup_();
plot(system);
}
void SDWidget::setup_()
{
upper_ = Vector3( 5.0f);
lower_ = Vector3(-5.0f);
setDefaultOptions();
setBackgroundRole(QPalette::Base);
//Todo: Add a nice icon
QAction* export_image = new QAction(tr("Export image"), this);
export_image->setIcon(IconLoader::instance().getIcon("actions/document-save"));
addAction(export_image);
connect(export_image, SIGNAL(triggered()), this, SLOT(exportImage_()));
}
SDWidget::~SDWidget()
{}
void SDWidget::plot(const System& system, bool create_sd)
{
system_ = system;
if (create_sd)
{
SDGenerator sdg;
sdg.generateSD(system_);
}
update();
}
void SDWidget::paintEvent(QPaintEvent *)
{
drawFrame_();
renderSD_(this);
}
void SDWidget::drawFrame_()
{
QPainter p(this);
QStyleOptionFrame opt;
opt.initFrom(this);
opt.state |= QStyle::State_Sunken;
QRect erase_area = rect();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this) + 1;
erase_area.adjust(frameWidth, frameWidth, -frameWidth, -frameWidth);
p.eraseRect(erase_area);
style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, this);
p.end();
}
void SDWidget::renderSD_(QPaintDevice* pd)
{
if (!pd)
{
return;
}
BoundingBoxProcessor bp;
system_.apply(bp);
upper_ = bp.getUpper()*1.1;
lower_ = bp.getLower()*1.1;
GeometricCenterProcessor gcp;
system_.apply(gcp);
Vector3 center = gcp.getCenter();
float xscale = pd->width() / (upper_.x - lower_.x);
float yscale = pd->height() / (upper_.y - lower_.y);
xscale = yscale = std::min(xscale, yscale);
QPainter painter(pd);
QPen pen(palette().foreground().color(), 3, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.translate(pd->width()/2, pd->height()/2);
QFont newFont = font();
newFont.setPixelSize(12);
painter.setFont(newFont);
QFontMetrics fontMetrics(newFont);
// check if hydrogen atoms should be shown!
bool show_H = options.getBool(Option::SHOW_HYDROGENS);
AtomIterator at_it = system_.beginAtom();
std::set<Atom*> already_seen;
for (; +at_it; ++at_it)
{
if ((at_it->getElement() == PTE[Element::H]) && !show_H) continue;
already_seen.insert(&(*at_it));
Atom::BondIterator b_it = at_it->beginBond();
Vector3 from3d = at_it->getPosition() - center;
QPointF from2d(from3d.x*xscale, from3d.y*yscale);
for (; +b_it; ++b_it)
{
Atom* partner = b_it->getPartner(*at_it);
if (
// treat each bond only once
(std::find(already_seen.begin(), already_seen.end(), partner) == already_seen.end())
// we don't draw hydrogens
&& !((partner->getElement() == PTE[Element::H]) && !show_H)
)
{
Vector3 to3d = partner->getPosition() - center;
QPointF to2d(to3d.x*xscale, to3d.y*yscale);
QPointF shifted_from = from2d;
// do we need to draw a character for the "to" atom?
if (partner->getElement() != PTE[Element::C])
{
QRect br = fontMetrics.boundingRect(partner->getElement().getSymbol().c_str());
QPointF label_pos(to2d.x() - br.width()/2, to2d.y() + br.height()/2 - 4);
painter.drawText(label_pos, partner->getElement().getSymbol().c_str());
QRectF bounding_box(to2d.x() - br.width()/2 - 3,
to2d.y() - br.height()/2 - 4,
br.width() + 6, br.height() + 6);
to2d = getEndpoint_(bounding_box, to2d, from2d, false);
}
// and do we need to draw a character for the "from" atom?
if (at_it->getElement() != PTE[Element::C])
{
QRect br = fontMetrics.boundingRect(at_it->getElement().getSymbol().c_str());
QRectF bounding_box(from2d.x() - br.width()/2 - 3,
from2d.y() - br.height()/2 - 4,
br.width() + 6, br.height() + 6);
shifted_from = getEndpoint_(bounding_box, to2d, from2d, true);
}
// don't draw if the points are too close to each other (Qt tends to crash if this happens...)
if ((to2d - shifted_from).toPoint().manhattanLength() == 0)
continue;
// do we need to draw a double bond?
if (b_it->getOrder() == Bond::ORDER__DOUBLE)
{
// compute two lines, slightly shifted wrt the normal of this line
QLineF bond_line = QLineF(shifted_from, to2d);
QLineF normal = bond_line.normalVector();
normal = normal.unitVector();
QPointF shift;
shift.setX(2*(normal.p2().x() - bond_line.p1().x()));
shift.setY(2*(normal.p2().y() - bond_line.p1().y()));
QLineF first_line = bond_line;
first_line.translate(shift);
shift.setX(-1.*shift.x());
shift.setY(-1.*shift.y());
QLineF second_line = bond_line;
second_line.translate(shift);
pen.setWidth(2);
painter.setPen(pen);
painter.drawLine(first_line);
painter.drawLine(second_line);
pen.setWidth(3);
painter.setPen(pen);
}
else if (b_it->getOrder() == Bond::ORDER__TRIPLE)
{
// compute two lines, slightly shifted wrt the normal of this line
QLineF bond_line = QLineF(shifted_from, to2d);
QLineF normal = bond_line.normalVector();
normal = normal.unitVector();
QPointF shift;
shift.setX(3*(normal.p2().x() - bond_line.p1().x()));
shift.setY(3*(normal.p2().y() - bond_line.p1().y()));
QLineF first_line = bond_line;
first_line.translate(shift);
shift.setX(-1.*shift.x());
shift.setY(-1.*shift.y());
QLineF second_line = bond_line;
second_line.translate(shift);
pen.setWidth(2);
painter.setPen(pen);
painter.drawLine(first_line);
painter.drawLine(bond_line);
painter.drawLine(second_line);
pen.setWidth(3);
painter.setPen(pen);
}
else // assume it's a single bond...
painter.drawLine(shifted_from, to2d);
}
}
// do we need to draw a character for the "from" atom?
if (at_it->getElement() != PTE[Element::C])
{
QRect br = fontMetrics.boundingRect(at_it->getElement().getSymbol().c_str());
QPointF label_pos(from2d.x() - br.width()/2, from2d.y() + br.height()/2 - 4);
painter.drawText(label_pos, at_it->getElement().getSymbol().c_str());
}
}
}
QPointF SDWidget::getEndpoint_(QRectF& character_boundary, QPointF from, QPointF to, bool character_is_from)
{
// compute all relevant lines...
// the line from "from" to "to" :-)
QLineF line_from_to(from, to);
// the upper part of the box
QLineF upper(character_boundary.topLeft(), character_boundary.topRight());
// the right part of the box
QLineF right(character_boundary.topRight(), character_boundary.bottomRight());
// the lower part of the box
QLineF lower(character_boundary.bottomRight(), character_boundary.bottomLeft());
// and finally the left part
QLineF left(character_boundary.bottomLeft(), character_boundary.topLeft());
// intersect them
QPointF intersection_point;
if (line_from_to.intersect(upper, &intersection_point) == QLineF::BoundedIntersection)
return intersection_point;
if (line_from_to.intersect(right, &intersection_point) == QLineF::BoundedIntersection)
return intersection_point;
if (line_from_to.intersect(lower, &intersection_point) == QLineF::BoundedIntersection)
return intersection_point;
if (line_from_to.intersect(left, &intersection_point) == QLineF::BoundedIntersection)
return intersection_point;
//If to and from are too close, the computed intersection is incorrect.
//In this case just return the point not having the character
return character_is_from ? from : to;
}
void SDWidget::clear()
{
upper_ = Vector3( 5.0f);
lower_ = Vector3(-5.0f);
system_.clear();
update();
}
QSize SDWidget::sizeHint() const
{
Vector3 diff = upper_ - lower_;
return QSize(diff.x * 10, diff.y * 10);
}
void SDWidget::setDefaultOptions()
{
options.setDefaultBool(SDWidget::Option::SHOW_HYDROGENS,
SDWidget::Default::SHOW_HYDROGENS);
}
void SDWidget::exportImage_()
{
QString file = QFileDialog::getSaveFileName(this, tr("Export image"), QString(), "Images (*.png *.xpm *.jpg *.bmp *.gif)");
if(file != QString::null)
{
QImage image(width(), height(), QImage::Format_ARGB32);
image.fill(0);
renderSD_(&image);
image.save(file);
}
}
}
}
|