void IsotopeForm::onMolChanged( QString key ) { using adcontrols::CTable; using adcontrols::ChemicalFormula; QStandardItemModel& model = *pModel_; CTable ctab; if ( ui->molwidget->getCTable( key, ctab ) ) { ctabs_.push_back( std::make_pair( key, ctab ) ); boost::filesystem::path path( qtwrapper::wstring::copy( key ) ); QString structure = qtwrapper::qstring::copy( path.leaf().wstring() ); std::wstring formula = ChemicalFormula::getFormula( ctab ); std::wstring stdformula = ChemicalFormula().standardFormula( formula ); double m = ChemicalFormula().getMonoIsotopicMass( stdformula ); int row = model.rowCount(); int col = 0; model.insertRows( row, 1 ); model.setData( model.index( row, col++ ), structure ); model.setData( model.index( row, col++ ), qtwrapper::qstring::copy( stdformula ) ); model.setData( model.index( row, col++ ), m ); model.setData( model.index( row, col++ ), key ); // hidden } }
void MolWidget::paintEvent( QPaintEvent * ) { using adcontrols::ChemicalFormula; QPainter painter( this ); if ( ctabs_.empty() ) { QRect rc = painter.viewport(); rc.setRect( rc.x() + 1, rc.y() + 1, rc.width() - 2, rc.height() - 2 ); painter.drawRect( rc ); painter.setFont( QFont( "Decorative" ) ); painter.drawText( rc, Qt::AlignHCenter | Qt::AlignCenter, "Drop MOL file here" ); return; } const adcontrols::CTable& ctab = ctabs_.back().second; std::wstring formula = ChemicalFormula::getFormula( ctab ); std::vector< std::wstring > hydrogens; do { typedef boost::char_separator< wchar_t > separator; typedef boost::tokenizer< boost::char_separator<wchar_t> , std::wstring::const_iterator , std::wstring > tokenizer; separator sep( L" ", L"" ); tokenizer tokens( formula, sep ); for ( tokenizer::iterator it = tokens.begin(); it != tokens.end(); ++it ) hydrogens.push_back( *it ); } while(0); std::wstring stdformula = ChemicalFormula().standardFormula( formula ); double m = ChemicalFormula().getMonoIsotopicMass( stdformula ); std::wostringstream label; label << stdformula << " mass: " << std::fixed << std::setprecision(10) << m; painter.drawText( 16, 24, qtwrapper::qstring( label.str() ) ); // painter.drawText( 16, 24 + 16, qtwrapper::qstring( formula ) ); // --- draw molecule -- painter.translate( painter.window().center() ); painter.setFont( QFont( "Decorative" ) ); const double factor = 25; for ( size_t n = 0; n < ctab.atoms().size(); ++n ) { const adcontrols::CTable::Atom& a = ctab.atom( n ); if ( hydrogens[ n ].find( L"H" ) == std::string::npos ) painter.setPen( QColor( Qt::black ) ); else painter.setPen( QColor( Qt::red ) ); //QRectF rc( a.x * factor - 16, a.y * factor - 16, 32, 32 ); //painter.drawText( rc, Qt::AlignCenter | Qt::AlignHCenter, qtwrapper::qstring::copy( a.symbol ) ); painter.drawText( a.x * factor, a.y * factor, qtwrapper::qstring::copy( a.symbol ) ); } for ( const adcontrols::CTable::Bond& b: ctab.bonds() ) { adcontrols::CTable::Atom a1 = ctab.atom( b.first_atom_number - 1 ); adcontrols::CTable::Atom a2 = ctab.atom( b.second_atom_number - 1 ); a1.x *= factor; a1.y *= factor; a2.x *= factor; a2.y *= factor; if ( b.bond_type == 1 ) { painter.setPen( QColor( Qt::blue ) ); painter.drawLine( a1.x, a1.y, a2.x, a2.y ); } else if ( b.bond_type == 2 ) { painter.setPen( QColor( Qt::blue ) ); double dx = a2.x - a1.x; double dy = a2.y - a1.y; double L = std::sqrt( ( dx * dx ) + ( dy * dy ) ); const int offset = 2; do { double x1 = a1.x - offset * dy / L; double x2 = a2.x - offset * dy / L; double y1 = a1.y - offset * (-dx) / L; double y2 = a2.y - offset * (-dx) / L; painter.drawLine( x1, y1, x2, y2 ); } while(0); do { double x1 = a1.x + offset * dy / L; double x2 = a2.x + offset * dy / L; double y1 = a1.y + offset * (-dx) / L; double y2 = a2.y + offset * (-dx) / L; painter.drawLine( x1, y1, x2, y2 ); } while(0); } else if ( b.bond_type == 3 ) { painter.setPen( QColor( Qt::blue ) ); painter.drawLine( a1.x, a1.y, a2.x, a2.y ); double dx = a2.x - a1.x; double dy = a2.y - a1.y; double L = std::sqrt( ( dx * dx ) + ( dy * dy ) ); const int offset = 3; do { double x1 = a1.x - offset * dy / L; double x2 = a2.x - offset * dy / L; double y1 = a1.y - offset * (-dx) / L; double y2 = a2.y - offset * (-dx) / L; painter.drawLine( x1, y1, x2, y2 ); } while(0); do { double x1 = a1.x + offset * dy / L; double x2 = a2.x + offset * dy / L; double y1 = a1.y + offset * (-dx) / L; double y2 = a2.y + offset * (-dx) / L; painter.drawLine( x1, y1, x2, y2 ); } while(0); } } std::vector< key_ctable_pair_t >::iterator it = std::remove_if( ctabs_.begin(), ctabs_.end() , boost::bind( &key_ctable_pair_t::first, _1 ) == QString( "delete me" ) ); ctabs_.erase( it, ctabs_.end() ); }