File indexing completed on 2024-04-14 03:40:22

0001 /*
0002     SPDX-FileCopyrightText: 2005, 2006 Pino Toscano <toscano.pino@tiscali.it>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kalziumschemetype.h"
0008 
0009 #include "kalziumdataobject.h"
0010 #include "prefs.h"
0011 
0012 #ifdef HAVE_OPENBABEL2
0013 #include <openbabel/mol.h>
0014 #endif
0015 #ifdef HAVE_OPENBABEL3
0016 #include <openbabel/elements.h>
0017 #endif
0018 
0019 #include "kalzium_debug.h"
0020 #include <QStandardPaths>
0021 
0022 #include <KLocalizedString>
0023 
0024 KalziumSchemeTypeFactory::KalziumSchemeTypeFactory()
0025 {
0026     m_schemes << KalziumMonoColorSchemeType::instance();
0027     m_schemes << KalziumBlocksSchemeType::instance();
0028     m_schemes << KalziumIconicSchemeType::instance();
0029     m_schemes << KalziumFamilySchemeType::instance();
0030     m_schemes << KalziumGroupsSchemeType::instance();
0031 #ifdef HAVE_OPENBABEL
0032     m_schemes << KalziumColorSchemeType::instance();
0033 #endif
0034 }
0035 
0036 KalziumSchemeTypeFactory *KalziumSchemeTypeFactory::instance()
0037 {
0038     static KalziumSchemeTypeFactory kstf;
0039     return &kstf;
0040 }
0041 
0042 KalziumSchemeType *KalziumSchemeTypeFactory::build(int id) const
0043 {
0044     if ((id < 0) || (id >= m_schemes.count())) {
0045         return nullptr;
0046     }
0047 
0048     return m_schemes.at(id);
0049 }
0050 
0051 KalziumSchemeType *KalziumSchemeTypeFactory::build(const QByteArray &id) const
0052 {
0053     for (int i = 0; i < m_schemes.count(); ++i) {
0054         if (m_schemes.at(i)->name() == id) {
0055             return m_schemes.at(i);
0056         }
0057     }
0058 
0059     return nullptr;
0060 }
0061 
0062 QStringList KalziumSchemeTypeFactory::schemes() const
0063 {
0064     QStringList l;
0065     for (int i = 0; i < m_schemes.count(); ++i) {
0066         l << m_schemes.at(i)->description();
0067     }
0068     return l;
0069 }
0070 
0071 KalziumSchemeType *KalziumSchemeType::instance()
0072 {
0073     return nullptr;
0074 }
0075 
0076 KalziumSchemeType::KalziumSchemeType() = default;
0077 
0078 KalziumSchemeType::~KalziumSchemeType() = default;
0079 
0080 KalziumMonoColorSchemeType::KalziumMonoColorSchemeType()
0081     : KalziumSchemeType()
0082 {
0083 }
0084 
0085 KalziumMonoColorSchemeType *KalziumMonoColorSchemeType::instance()
0086 {
0087     static KalziumMonoColorSchemeType kmcst;
0088     return &kmcst;
0089 }
0090 
0091 QByteArray KalziumMonoColorSchemeType::name() const
0092 {
0093     return "MonoColor";
0094 }
0095 
0096 QString KalziumMonoColorSchemeType::description() const
0097 {
0098     return i18n("Monochrome");
0099 }
0100 
0101 QBrush KalziumMonoColorSchemeType::elementBrush(int el) const
0102 {
0103     Q_UNUSED(el);
0104     return QBrush(Prefs::noscheme());
0105 }
0106 
0107 QColor KalziumMonoColorSchemeType::textColor(int el) const
0108 {
0109     Q_UNUSED(el);
0110     return Qt::black;
0111 }
0112 
0113 QList<legendPair> KalziumMonoColorSchemeType::legendItems() const
0114 {
0115     QList<legendPair> ll;
0116     ll << qMakePair(i18n("All the Elements"), QColor(Prefs::noscheme()));
0117     return ll;
0118 }
0119 
0120 KalziumBlocksSchemeType::KalziumBlocksSchemeType()
0121     : KalziumSchemeType()
0122 {
0123 }
0124 
0125 KalziumBlocksSchemeType *KalziumBlocksSchemeType::instance()
0126 {
0127     static KalziumBlocksSchemeType kbst;
0128     return &kbst;
0129 }
0130 
0131 QByteArray KalziumBlocksSchemeType::name() const
0132 {
0133     return "Blocks";
0134 }
0135 
0136 QString KalziumBlocksSchemeType::description() const
0137 {
0138     return i18n("Blocks");
0139 }
0140 
0141 QBrush KalziumBlocksSchemeType::elementBrush(int el) const
0142 {
0143     QString block = KalziumDataObject::instance()->element(el)->dataAsString(ChemicalDataObject::periodTableBlock);
0144 
0145     QColor c;
0146     if (block == QLatin1String("s")) {
0147         c = Prefs::block_s();
0148     } else if (block == QLatin1String("p")) {
0149         c = Prefs::block_p();
0150     } else if (block == QLatin1String("d")) {
0151         c = Prefs::block_d();
0152     } else if (block == QLatin1String("f")) {
0153         c = Prefs::block_f();
0154     } else {
0155         c = Qt::lightGray;
0156     }
0157 
0158     return QBrush(c);
0159 }
0160 
0161 QColor KalziumBlocksSchemeType::textColor(int el) const
0162 {
0163     Q_UNUSED(el);
0164     return Qt::black;
0165 }
0166 
0167 QList<legendPair> KalziumBlocksSchemeType::legendItems() const
0168 {
0169     QList<legendPair> ll;
0170     ll << qMakePair(i18n("s-Block"), QColor(Prefs::block_s()));
0171     ll << qMakePair(i18n("p-Block"), QColor(Prefs::block_p()));
0172     ll << qMakePair(i18n("d-Block"), QColor(Prefs::block_d()));
0173     ll << qMakePair(i18n("f-Block"), QColor(Prefs::block_f()));
0174     return ll;
0175 }
0176 
0177 /// ICONIC SCHEME///
0178 
0179 KalziumIconicSchemeType::KalziumIconicSchemeType()
0180     : KalziumSchemeType()
0181 {
0182 }
0183 
0184 KalziumIconicSchemeType *KalziumIconicSchemeType::instance()
0185 {
0186     static KalziumIconicSchemeType kist;
0187     return &kist;
0188 }
0189 
0190 QByteArray KalziumIconicSchemeType::name() const
0191 {
0192     return "Iconic";
0193 }
0194 
0195 QString KalziumIconicSchemeType::description() const
0196 {
0197     return i18n("Iconic");
0198 }
0199 
0200 QBrush KalziumIconicSchemeType::elementBrush(int el) const
0201 {
0202     QPixmap pixmap = KalziumDataObject::instance()->pixmap(el);
0203     return QBrush(pixmap);
0204 }
0205 
0206 QColor KalziumIconicSchemeType::textColor(int) const
0207 {
0208     return Qt::transparent;
0209 }
0210 
0211 QList<legendPair> KalziumIconicSchemeType::legendItems() const
0212 {
0213     QList<legendPair> ll;
0214     ll << qMakePair(i18n("Each element is represented by an icon which represents its use."), QColor());
0215     return ll;
0216 }
0217 
0218 /// Family///
0219 KalziumFamilySchemeType::KalziumFamilySchemeType()
0220     : KalziumSchemeType()
0221 {
0222 }
0223 
0224 KalziumFamilySchemeType *KalziumFamilySchemeType::instance()
0225 {
0226     static KalziumFamilySchemeType kbst;
0227     return &kbst;
0228 }
0229 
0230 QByteArray KalziumFamilySchemeType::name() const
0231 {
0232     return "Family";
0233 }
0234 
0235 QString KalziumFamilySchemeType::description() const
0236 {
0237     return i18n("Family");
0238 }
0239 
0240 QBrush KalziumFamilySchemeType::elementBrush(int el) const
0241 {
0242     QString family = KalziumDataObject::instance()->element(el)->dataAsString(ChemicalDataObject::family);
0243 
0244     QColor c;
0245 
0246     if (family == QLatin1String("Noblegas")) {
0247         c = Prefs::noble_gas();
0248     } else if (family == QLatin1String("Non-Metal")) {
0249         c = Prefs::nonmetal();
0250     } else if (family == QLatin1String("Rare_Earth")) {
0251         c = Prefs::rare();
0252     } else if (family == QLatin1String("Alkaline_Earth")) {
0253         c = Prefs::alkaline();
0254     } else if (family == QLatin1String("Alkali_Earth")) {
0255         c = Prefs::alkalie();
0256     } else if (family == QLatin1String("Transition")) {
0257         c = Prefs::transition();
0258     } else if (family == QLatin1String("Other_Metal")) {
0259         c = Prefs::other_metal();
0260     } else if (family == QLatin1String("Metalloids")) {
0261         c = Prefs::metalloid();
0262     } else if (family == QLatin1String("Halogen")) {
0263         c = Prefs::halogene();
0264     } else {
0265         c = Qt::lightGray;
0266     }
0267 
0268     return QBrush(c);
0269 }
0270 
0271 QColor KalziumFamilySchemeType::textColor(int) const
0272 {
0273     return Qt::black;
0274 }
0275 
0276 QList<legendPair> KalziumFamilySchemeType::legendItems() const
0277 {
0278     QList<legendPair> ll;
0279     ll << qMakePair(i18n("Alkaline"), QColor(Prefs::alkalie()));
0280     ll << qMakePair(i18n("Rare Earth"), QColor(Prefs::rare()));
0281     ll << qMakePair(i18n("Non-Metals"), QColor(Prefs::nonmetal()));
0282     ll << qMakePair(i18n("Alkalie Metal"), QColor(Prefs::alkaline()));
0283     ll << qMakePair(i18n("Other Metal"), QColor(Prefs::other_metal()));
0284     ll << qMakePair(i18n("Halogen"), QColor(Prefs::halogene()));
0285     ll << qMakePair(i18n("Transition Metal"), QColor(Prefs::transition()));
0286     ll << qMakePair(i18n("Noble Gas"), QColor(Prefs::noble_gas()));
0287     ll << qMakePair(i18n("Metalloid"), QColor(Prefs::metalloid()));
0288 
0289     return ll;
0290 }
0291 
0292 /// GROUPS///
0293 KalziumGroupsSchemeType::KalziumGroupsSchemeType()
0294     : KalziumSchemeType()
0295 {
0296 }
0297 
0298 KalziumGroupsSchemeType *KalziumGroupsSchemeType::instance()
0299 {
0300     static KalziumGroupsSchemeType kbst;
0301     return &kbst;
0302 }
0303 
0304 QByteArray KalziumGroupsSchemeType::name() const
0305 {
0306     return "Groups";
0307 }
0308 
0309 QString KalziumGroupsSchemeType::description() const
0310 {
0311     return i18n("Groups");
0312 }
0313 
0314 QBrush KalziumGroupsSchemeType::elementBrush(int el) const
0315 {
0316     QString group = KalziumDataObject::instance()->element(el)->dataAsString(ChemicalDataObject::group);
0317 
0318     QColor c;
0319 
0320     if (group == QLatin1String("1")) {
0321         c = Prefs::group_1();
0322     } else if (group == QLatin1String("2")) {
0323         c = Prefs::group_2();
0324     } else if (group == QLatin1String("3")) {
0325         c = Prefs::group_3();
0326     } else if (group == QLatin1String("4")) {
0327         c = Prefs::group_4();
0328     } else if (group == QLatin1String("5")) {
0329         c = Prefs::group_5();
0330     } else if (group == QLatin1String("6")) {
0331         c = Prefs::group_6();
0332     } else if (group == QLatin1String("7")) {
0333         c = Prefs::group_7();
0334     } else if (group == QLatin1String("8")) {
0335         c = Prefs::group_8();
0336     } else {
0337         c = Qt::lightGray;
0338     }
0339 
0340     return QBrush(c);
0341 }
0342 
0343 QColor KalziumGroupsSchemeType::textColor(int) const
0344 {
0345     return Qt::black;
0346 }
0347 
0348 QList<legendPair> KalziumGroupsSchemeType::legendItems() const
0349 {
0350     QList<legendPair> ll;
0351     ll << qMakePair(i18n("Group 1"), QColor(Prefs::group_1()));
0352     ll << qMakePair(i18n("Group 2"), QColor(Prefs::group_2()));
0353     ll << qMakePair(i18n("Group 3"), QColor(Prefs::group_3()));
0354     ll << qMakePair(i18n("Group 4"), QColor(Prefs::group_4()));
0355     ll << qMakePair(i18n("Group 5"), QColor(Prefs::group_5()));
0356     ll << qMakePair(i18n("Group 6"), QColor(Prefs::group_6()));
0357     ll << qMakePair(i18n("Group 7"), QColor(Prefs::group_7()));
0358     ll << qMakePair(i18n("Group 8"), QColor(Prefs::group_8()));
0359 
0360     return ll;
0361 }
0362 
0363 #ifdef HAVE_OPENBABEL
0364 /// OpenBabel Color///
0365 KalziumColorSchemeType::KalziumColorSchemeType()
0366     : KalziumSchemeType()
0367 {
0368 }
0369 
0370 KalziumColorSchemeType *KalziumColorSchemeType::instance()
0371 {
0372     static KalziumColorSchemeType kbst;
0373     return &kbst;
0374 }
0375 
0376 QByteArray KalziumColorSchemeType::name() const
0377 {
0378     return "Color";
0379 }
0380 
0381 QString KalziumColorSchemeType::description() const
0382 {
0383     return i18n("Colors");
0384 }
0385 
0386 QBrush KalziumColorSchemeType::elementBrush(int el) const
0387 {
0388     QColor c;
0389 
0390 #ifdef HAVE_OPENBABEL2
0391     std::vector<double> color = OpenBabel::etab.GetRGB(el);
0392     c.setRgbF(color[0], color[1], color[2]);
0393 #endif
0394 #ifdef HAVE_OPENBABEL3
0395     double red, green, blue;
0396     OpenBabel::OBElements::GetRGB(el, &red, &green, &blue);
0397     c.setRgbF(red, green, blue);
0398 #endif
0399 
0400     return QBrush(c);
0401 }
0402 
0403 QColor KalziumColorSchemeType::textColor(int) const
0404 {
0405     return Qt::black;
0406 }
0407 
0408 QList<legendPair> KalziumColorSchemeType::legendItems() const
0409 {
0410     QList<legendPair> ll;
0411     ll << qMakePair(i18n("Nice colors without meaning. (From the Open Babel project)"), QColor());
0412     return ll;
0413 }
0414 #endif
0415 
0416 /// CRYSTAL///
0417 // X KalziumCrystalSchemeType::KalziumCrystalSchemeType()
0418 // X   : KalziumSchemeType()
0419 // X {
0420 // X }
0421 // X
0422 // X KalziumCrystalSchemeType* KalziumCrystalSchemeType::instance()
0423 // X {
0424 // X     static KalziumCrystalSchemeType kbst;
0425 // X     return &kbst;
0426 // X }
0427 // X
0428 // X QByteArray KalziumCrystalSchemeType::name() const
0429 // X {
0430 // X     return "Crystal";
0431 // X }
0432 // X
0433 // X QString KalziumCrystalSchemeType::description() const
0434 // X {
0435 // X     return i18n("Crystal Structures");
0436 // X }
0437 // X
0438 // X QBrush KalziumCrystalSchemeType::elementBrush(int el, const QRect& elrect) const
0439 // X {
0440 // X     QString crystal = KalziumDataObject::instance()->element(el)->dataAsString(ChemicalDataObject::crystalstructure);
0441 // X
0442 // X     qCDebug(KALZIUM_LOG) << "crystal is " << crystal;
0443 // X
0444 // X     static QString resourcepath;
0445 // X     if (resourcepath.isEmpty()) {
0446 // X         resourcepath = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, "data/latticeicons/");
0447 // X     }
0448 // X
0449 // X     QString filename;
0450 // X     if (crystal == "bcc") {
0451 // X         filename = "ci.png";
0452 // X     } else if (crystal == "ccp") {
0453 // X         filename = "cp.png";
0454 // X     } else if (crystal ==  "fcc") {
0455 // X         filename = "cf.png";
0456 // X     } else if (crystal ==  "hcp") {
0457 // X         filename = "hp.png";
0458 // X     } else if (crystal ==  "rh") {
0459 // X         filename = "hr.png";//Rhombohedral primitive
0460 // X     } else if (crystal ==  "or") {
0461 // X         filename = "op.png";//Orthorhombic primitive
0462 // X     } else if (crystal ==  "mono") {
0463 // X         filename = "ms.png";//Monoclinic primitive
0464 // X     } else if (crystal ==  "tri") {
0465 // X         filename = "ap.png";//Triclinic
0466 // X     } else if (crystal ==  "tp") {
0467 // X         filename = "tp.png";//Tetragonal primitive
0468 // X     }
0469 // X
0470 // X         filename.prepend(resourcepath);
0471 // X
0472 // X     QBrush ret;
0473 // X     if (!filename.isEmpty()) {
0474 // X         qCDebug(KALZIUM_LOG) << el << ": FILENAME is not EMPTY... " << filename;
0475 // X         QPixmap pixmap(resourcepath + filename);
0476 // X         ret = QBrush(pixmap.scaled(elrect.size(), Qt::KeepAspectRatio));
0477 // X     } else {
0478 // X             qCDebug(KALZIUM_LOG) << el << ": FILENAME EMPTY... " << filename;
0479 // X         ret.setColor(Qt::gray);
0480 // X     }
0481 // X
0482 // X     return ret;
0483 // X }
0484 // X
0485 // X QColor KalziumCrystalSchemeType::textColor(int) const
0486 // X {
0487 // X     return Qt::black;
0488 // X }
0489 // X
0490 // X QList<legendPair> KalziumCrystalSchemeType::legendItems() const
0491 // X {
0492 // X     static QString resourcepath;
0493 // X     if (resourcepath.isEmpty())
0494 // X     {
0495 // X         resourcepath = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, "data/latticeicons/");
0496 // X     }
0497 // X
0498 // X     QList<legendPair> ll;
0499 // X     ll << qMakePair(i18n("bcc, body centered cubic"), QColor(QPixmap(resourcepath + "ci.png")));
0500 // X     ll << qMakePair(i18n("ccp, cubic close packed"), QColor(QPixmap(resourcepath + "cp.png")));
0501 // X     ll << qMakePair(i18n("fcc, face centered cubic"), QColor(QPixmap(resourcepath + "cf.png")));
0502 // X     ll << qMakePair(i18n("hcp, hexagonal close packed"), QColor(QPixmap(resourcepath + "hp.png")));
0503 // X     ll << qMakePair(i18n("rh, rhombohedral"), QColor(QPixmap(resourcepath + "hr.png")));
0504 // X     ll << qMakePair(i18n("or, orthorhombic primitive"), QColor(QPixmap(resourcepath + "op.png")));
0505 // X     ll << qMakePair(i18n("ms, monoclinic"), QColor(QPixmap(resourcepath + "ms.png")));
0506 // X     ll << qMakePair(i18n("ap, triclinic"), QColor(QPixmap(resourcepath + "ap.png")));
0507 // X     ll << qMakePair(i18n("tp, tetragonal primitive"), QColor(QPixmap(resourcepath + "tp.png")));
0508 // X
0509 // X     return ll;
0510 // X }
0511 
0512 ////
0513 // X KalziumDiscoverymapSchemeType::KalziumDiscoverymapSchemeType()
0514 // X   : KalziumSchemeType()
0515 // X {
0516 // X }
0517 // X
0518 // X KalziumDiscoverymapSchemeType* KalziumDiscoverymapSchemeType::instance()
0519 // X {
0520 // X     static KalziumDiscoverymapSchemeType kbst;
0521 // X     return &kbst;
0522 // X }
0523 // X
0524 // X QByteArray KalziumDiscoverymapSchemeType::name() const
0525 // X {
0526 // X     return "Crystal";
0527 // X }
0528 // X
0529 // X QString KalziumDiscoverymapSchemeType::description() const
0530 // X {
0531 // X     return i18n("Discovery Country");
0532 // X }
0533 // X
0534 // X QBrush KalziumDiscoverymapSchemeType::elementBrush(int el, const QRect& elrect) const
0535 // X {
0536 // X     QString map = KalziumDataObject::instance()->element(el)->dataAsString(ChemicalDataObject::discoveryCountry);
0537 // X
0538 // X     static QString resourcepath;
0539 // X     if (resourcepath.isEmpty()) {
0540 // X         resourcepath = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, "data/maps/");
0541 // X     }
0542 // X
0543 // X     QString filename;
0544 // X     if (map == "se") {
0545 // X         filename = "se.png";
0546 // X     } else if (map == "uk") {
0547 // X         filename = "uk.png";
0548 // X     } else if (map == "us") {
0549 // X         filename = "us.png";
0550 // X     } else if (map == "ru") {
0551 // X         filename = "ru.png";
0552 // X     } else if (map == "it") {
0553 // X         filename = "it.png";
0554 // X     } else if (map == "de") {
0555 // X         filename = "de.png";
0556 // X     } else if (map == "dk") {
0557 // X         filename = "dk.png";
0558 // X     } else if (map == "fr") {
0559 // X         filename = "fr.png";
0560 // X     } else if (map == "fi") {
0561 // X         filename = "fi.png";
0562 // X     } else if (map == "es") {
0563 // X         filename = "es.png";
0564 // X     } else if (map == "ancient") {
0565 // X         return QBrush(Qt::lightGray);
0566 // X     } else if (map == "uk,fr") {
0567 // X         filename = "ukfr.png";
0568 // X     } else if (map == "se,uk") {
0569 // X         filename = "ukse.png";
0570 // X     } else if (map == "ru,us") {
0571 // X         filename = "ruus.png";
0572 // X     } else {
0573 // X       return QBrush(Qt::blue);
0574 // X     }
0575 // X
0576 // X     QBrush ret;
0577 // X     if (!filename.isEmpty()) {
0578 // X         QPixmap pixmap(resourcepath + filename);
0579 // X         ret = QBrush(pixmap.scaled(elrect.size(), Qt::KeepAspectRatio));
0580 // X     } else {
0581 // X         ret.setColor(Qt::gray);
0582 // X     }
0583 // X
0584 // X     return ret;
0585 // X }
0586 // X
0587 // X QColor KalziumDiscoverymapSchemeType::textColor(int) const
0588 // X {
0589 // X     return Qt::black;
0590 // X }
0591 // X
0592 // X QList<legendPair> KalziumDiscoverymapSchemeType::legendItems() const
0593 // X {
0594 // X     static QString resourcepath;
0595 // X     if (resourcepath.isEmpty()) {
0596 // X         resourcepath = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, "data/maps/");
0597 // X     }
0598 // X
0599 // X     QList<legendPair> ll;
0600 // X     ll << qMakePair(i18n("Germany"), QColor(QPixmap(resourcepath + "de.png")));
0601 // X     ll << qMakePair(i18n("United Kindom"), QColor(QPixmap(resourcepath + "uk.png")));
0602 // X     ll << qMakePair(i18n("Sweden"), QColor(QPixmap(resourcepath + "se.png")));
0603 // X     ll << qMakePair(i18n("USA"), QColor(QPixmap(resourcepath + "us.png")));
0604 // X     ll << qMakePair(i18n("Russia"), QColor(QPixmap(resourcepath + "ru.png")));
0605 // X     ll << qMakePair(i18n("Italy"), QColor(QPixmap(resourcepath + "it.png")));
0606 // X     ll << qMakePair(i18n("Denmark"), QColor(QPixmap(resourcepath + "dk.png")));
0607 // X     ll << qMakePair(i18n("France"), QColor(QPixmap(resourcepath + "fr.png")));
0608 // X     ll << qMakePair(i18n("Finland"), QColor(QPixmap(resourcepath + "fi.png")));
0609 // X     ll << qMakePair(i18n("Spain"), QColor(QPixmap(resourcepath + "es.png")));
0610 // X
0611 // X     return ll;
0612 // X }