File indexing completed on 2022-11-29 18:11:58

0001 /*
0002     SPDX-FileCopyrightText: 2007 Johannes Simon <johannes.simon@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "exportdialog.h"
0007 
0008 #include "kalziumutils.h"
0009 
0010 #include "kalzium_debug.h"
0011 #include <QDialogButtonBox>
0012 #include <QFont>
0013 #include <QPushButton>
0014 #include <QVBoxLayout>
0015 
0016 #include <kwidgetsaddons_version.h>
0017 #include <KGuiItem>
0018 #include <KHelpClient>
0019 #include <KLocalizedString>
0020 #include <KMessageBox>
0021 
0022 static const char HTML_HEADER[] =
0023     "<html>"
0024     "\n<head>"
0025     "\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"
0026     "\n<style type=\"text/css\">"
0027     "\nbody {"
0028     "\n   font-family:arial;"
0029     "\n}"
0030     "\n.property {"
0031     "\n   font-style:italic;"
0032     "\n}"
0033     "\nth {"
0034     "\n   font-weight:bold;"
0035     "\n   text-align:left;"
0036     "\n   background-color:#F0F0F0;"
0037     "\n}"
0038     "\n</style>"
0039     "\n</head>"
0040     "\n<body>";
0041 
0042 static const char HTML_FOOTER[] =
0043     "\n</body>"
0044     "\n</html>";
0045 
0046 static const char XML_HEADER[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
0047 
0048 ElementListEntry::ElementListEntry(Element *element)
0049     : QListWidgetItem()
0050 {
0051     m_atomicNum = element->dataAsVariant(ChemicalDataObject::atomicNumber).toInt();
0052     m_name = element->dataAsString(ChemicalDataObject::name);
0053     m_element = element;
0054 
0055     setText(m_name);
0056 }
0057 
0058 ElementListEntry::~ElementListEntry() = default;
0059 
0060 PropertyListEntry::PropertyListEntry(const QString &name, ChemicalDataObject::BlueObelisk type)
0061     : QListWidgetItem()
0062 {
0063     setText(name);
0064     m_type = type;
0065 }
0066 
0067 PropertyListEntry::~PropertyListEntry() = default;
0068 
0069 ExportDialog::ExportDialog(QWidget *parent)
0070     : QDialog(parent)
0071 {
0072     qCDebug(KALZIUM_LOG) << "ExportDialog::ExportDialog";
0073     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Help, this);
0074     auto mainWidget = new QWidget(this);
0075     auto mainLayout = new QVBoxLayout(this);
0076     mainLayout->addWidget(mainWidget);
0077     auto user1Button = new QPushButton;
0078     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0079     connect(buttonBox, &QDialogButtonBox::accepted, this, &ExportDialog::accept);
0080     connect(buttonBox, &QDialogButtonBox::rejected, this, &ExportDialog::reject);
0081     mainLayout->addWidget(buttonBox);
0082     qCDebug(KALZIUM_LOG) << "ExportDialog: setButtons";
0083     ui.setupUi(mainWidget);
0084     qCDebug(KALZIUM_LOG) << "ExportDialog: ui.setupUi(mainWidget)";
0085     KGuiItem::assign(user1Button, KGuiItem(i18n("OK")));
0086     qCDebug(KALZIUM_LOG) << "ExportDialog: setButtonGuiItem(User1, KGuiItem(i18n(\"OK\")));";
0087     ui.targetFile->setMode(KFile::File | KFile::Directory | KFile::LocalOnly);
0088     qCDebug(KALZIUM_LOG) << "ui.targetFile->setMode(KFile::File | KFile::Directory | KFile::LocalOnly);";
0089 
0090     setWindowTitle(i18nc("@title:window", "Export Chemical Data"));
0091     qCDebug(KALZIUM_LOG) << "ui.targetFile->setMode(KFile::File | KFile::Directory | KFile::LocalOnly);";
0092 
0093     populateElementList();
0094     qCDebug(KALZIUM_LOG) << "ui.targetFile->setMode(KFile::File | KFile::Directory | KFile::LocalOnly);";
0095 
0096     ui.formatList->addItem(QStringLiteral(".html (formatted html document)"), "html");
0097     ui.formatList->addItem(QStringLiteral(".xml (raw element data)"), "xml");
0098     ui.formatList->addItem(QStringLiteral(".csv (comma-separated data)"), "csv");
0099     qCDebug(KALZIUM_LOG) << "ui.formatList->addItem(...);";
0100 
0101     connect(user1Button, &QPushButton::clicked, this, &ExportDialog::slotOkClicked);
0102     qCDebug(KALZIUM_LOG) << "connect(user1Button, SIGNAL(clicked()), this, SLOT(slotOkClicked()));";
0103     connect(buttonBox, &QDialogButtonBox::helpRequested, this, &ExportDialog::slotHelpRequested);
0104     qCDebug(KALZIUM_LOG) << "KHelpClient::invokeHelp(QString(), \"kalzium\");";
0105 }
0106 
0107 ExportDialog::~ExportDialog()
0108 {
0109     delete m_outputStream;
0110 }
0111 
0112 void ExportDialog::populateElementList()
0113 {
0114     // Add descriptive headers
0115     auto header1 = new QListWidgetItem(i18n("Elements"));
0116     auto header2 = new QListWidgetItem(i18n("Properties"));
0117     header1->setFlags(Qt::ItemIsEnabled);
0118     header2->setFlags(Qt::ItemIsEnabled);
0119     QFont font;
0120     font.setBold(true);
0121     header1->setFont(font);
0122     header2->setFont(font);
0123     ui.elementListWidget->addItem(header1);
0124     ui.propertyListWidget->addItem(header2);
0125 
0126     // Add elements
0127     foreach (Element *element, KalziumDataObject::instance()->ElementList) {
0128         auto entry = new ElementListEntry(element);
0129         ui.elementListWidget->addItem(entry);
0130     }
0131 
0132     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Atomic Number"), ChemicalDataObject::atomicNumber));
0133     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Symbol"), ChemicalDataObject::symbol));
0134     // ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Name"), ChemicalDataObject::name));
0135     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Mass"), ChemicalDataObject::mass));
0136     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Exact Mass"), ChemicalDataObject::exactMass));
0137     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Ionization"), ChemicalDataObject::ionization));
0138     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Electron Affinity"), ChemicalDataObject::electronAffinity));
0139     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Electronegativity"), ChemicalDataObject::electronegativityPauling));
0140     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Covalent Radius"), ChemicalDataObject::radiusCovalent));
0141     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Van der Waals Radius"), ChemicalDataObject::radiusVDW));
0142     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Melting Point"), ChemicalDataObject::meltingpoint));
0143     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Boiling Point"), ChemicalDataObject::boilingpoint));
0144     ui.propertyListWidget->addItem(new PropertyListEntry(i18n("Family"), ChemicalDataObject::family));
0145 }
0146 
0147 void ExportDialog::slotOkClicked()
0148 {
0149     QString format = ui.formatList->itemData(ui.formatList->currentIndex(), Qt::UserRole).toString();
0150     QString filename = ui.targetFile->url().toLocalFile();
0151     if (!filename.endsWith(format)) {
0152         filename += '.' + format;
0153     }
0154     QFile outputFile(filename);
0155     if (outputFile.exists()) {
0156 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0157         if (KMessageBox::questionTwoActions(this,
0158 #else
0159         if (KMessageBox::questionYesNo(this,
0160 #endif
0161             i18n("File already exists. Do you want to overwrite it?"), QString(),
0162             KStandardGuiItem::overwrite(),
0163             KStandardGuiItem::cancel())
0164 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0165             == KMessageBox::SecondaryAction) {
0166 #else
0167             == KMessageBox::No) {
0168 #endif
0169             return;
0170         }
0171     }
0172     if (!outputFile.open(QIODevice::WriteOnly)) {
0173         KMessageBox::error(this, i18n("Could not open file for writing."));
0174         return;
0175     }
0176 
0177     delete m_outputStream;
0178     m_outputStream = new QTextStream(&outputFile);
0179     if (format == QLatin1String("html")) {
0180         exportToHtml();
0181     } else if (format == QLatin1String("xml")) {
0182         exportToXml();
0183     } else {
0184         exportToCsv();
0185     }
0186 
0187     // close the dialog
0188     done(0);
0189 }
0190 
0191 void ExportDialog::slotHelpRequested()
0192 {
0193     KHelpClient::invokeHelp(QString(), QStringLiteral("kalzium"));
0194 }
0195 
0196 void ExportDialog::exportToHtml()
0197 {
0198     *m_outputStream << HTML_HEADER << "<table>\n";
0199     foreach (QListWidgetItem *element, ui.elementListWidget->selectedItems()) {
0200         *m_outputStream << "<tr>\n<th colspan=\"2\">" << ((ElementListEntry *)element)->m_element->dataAsString(ChemicalDataObject::name) << "</th>\n</tr>\n";
0201         foreach (QListWidgetItem *property, ui.propertyListWidget->selectedItems()) {
0202             *m_outputStream << "<tr>\n<td class=\"property\">" << ((PropertyListEntry *)property)->text() << "</td>\n<td class=\"value\">"
0203                             << KalziumUtils::prettyUnit(((ElementListEntry *)element)->m_element, ((PropertyListEntry *)property)->m_type) << "</td>\n</tr>\n";
0204         }
0205     }
0206     *m_outputStream << "</table>\n" << HTML_FOOTER;
0207 }
0208 
0209 void ExportDialog::exportToXml()
0210 {
0211     *m_outputStream << XML_HEADER << "<elements>\n";
0212     foreach (QListWidgetItem *element, ui.elementListWidget->selectedItems()) {
0213         *m_outputStream << "  <element name=\"" << ((ElementListEntry *)element)->m_element->dataAsString(ChemicalDataObject::name) << "\">\n";
0214         foreach (QListWidgetItem *property, ui.propertyListWidget->selectedItems()) {
0215             *m_outputStream << "    <property name=\"" << ((PropertyListEntry *)property)->text() << "\">"
0216                             << KalziumUtils::prettyUnit(((ElementListEntry *)element)->m_element, ((PropertyListEntry *)property)->m_type) << "</property>\n";
0217         }
0218         *m_outputStream << "  </element>\n";
0219     }
0220     *m_outputStream << "</elements>\n";
0221 }
0222 
0223 void ExportDialog::exportToCsv()
0224 {
0225     *m_outputStream << "Name";
0226     foreach (QListWidgetItem *property, ui.propertyListWidget->selectedItems()) {
0227         *m_outputStream << ", \"" << ((PropertyListEntry *)property)->text() << "\"";
0228     }
0229     *m_outputStream << "\n";
0230     foreach (QListWidgetItem *element, ui.elementListWidget->selectedItems()) {
0231         *m_outputStream << "\"" << ((ElementListEntry *)element)->m_element->dataAsString(ChemicalDataObject::name) << "\"";
0232         foreach (QListWidgetItem *property, ui.propertyListWidget->selectedItems()) {
0233             *m_outputStream << ", \"" << KalziumUtils::prettyUnit(((ElementListEntry *)element)->m_element, ((PropertyListEntry *)property)->m_type) << "\"";
0234         }
0235         *m_outputStream << "\n";
0236     }
0237 }