1 /***************************************************************************
2 copyright : (C) 2007 by Johannes Simon
3 email : johannes.simon@gmail.com
4 ***************************************************************************/
5 /***************************************************************************
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 ***************************************************************************/
14 #include "exportdialog.h"
15 #include "kalziumutils.h"
18 #include <KMessageBox>
23 static const char HTML_HEADER
[] =
26 "\n<style type=\"text/css\">"
28 "\n font-family:arial;"
31 "\n font-style:italic;"
34 "\n font-weight:bold;"
36 "\n background-color:#F0F0F0;"
42 static const char HTML_FOOTER
[] =
46 static const char XML_HEADER
[] =
47 "<?xml version=\"1.0\"?>\n";
49 ElementListEntry::ElementListEntry( Element
* element
)
52 m_atomicNum
= element
->dataAsVariant(ChemicalDataObject::atomicNumber
).toInt();
53 m_name
= element
->dataAsString(ChemicalDataObject::name
);
59 ElementListEntry::~ElementListEntry()
63 PropertyListEntry::PropertyListEntry( const QString
& name
, ChemicalDataObject::BlueObelisk type
)
70 PropertyListEntry::~PropertyListEntry()
74 ExportDialog::ExportDialog( QWidget
* parent
)
77 setButtons( Help
| User1
| Cancel
);
78 ui
.setupUi( mainWidget() );
79 setButtonGuiItem( User1
, KGuiItem( i18n( "Ok" ) ) );
81 ui
.targetFile
->setMode( KFile::File
| KFile::Directory
| KFile::LocalOnly
);
83 setCaption( i18n( "Export Chemical Data" ) );
85 populateElementList();
87 ui
.formatList
->addItem( ".html (formatted html document)", "html" );
88 ui
.formatList
->addItem( ".xml (raw element data)", "xml" );
89 ui
.formatList
->addItem( ".csv (comma-separated data)", "csv" );
91 connect( this, SIGNAL( user1Clicked() ), this, SLOT( slotOkClicked() ) );
92 connect( this, SIGNAL( helpClicked() ), this, SLOT( slotHelpClicked() ) );
93 setHelp(QString(),"kalzium");
96 ExportDialog::~ExportDialog()
100 void ExportDialog::populateElementList()
102 // Add descriptive headers
103 QListWidgetItem
* header1
= new QListWidgetItem( i18n( "Elements" ) );
104 QListWidgetItem
* header2
= new QListWidgetItem( i18n( "Properties" ) );
105 header1
->setFlags( Qt::ItemIsEnabled
);
106 header2
->setFlags( Qt::ItemIsEnabled
);
108 font
.setBold( true );
109 header1
->setFont( font
);
110 header2
->setFont( font
);
111 ui
.elementListWidget
->addItem( header1
);
112 ui
.propertyListWidget
->addItem( header2
);
115 foreach( Element
* element
, KalziumDataObject::instance()->ElementList
)
117 ElementListEntry
* entry
= new ElementListEntry( element
);
118 ui
.elementListWidget
->addItem( entry
);
121 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Atomic Number" ), ChemicalDataObject::atomicNumber
) );
122 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Symbol" ), ChemicalDataObject::symbol
) );
123 //ui.propertyListWidget->addItem( new PropertyListEntry( i18n( "Name" ), ChemicalDataObject::name ) );
124 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Mass" ), ChemicalDataObject::mass
) );
125 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Exact Mass" ), ChemicalDataObject::exactMass
) );
126 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Ionization" ), ChemicalDataObject::ionization
) );
127 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Electron Affinity" ), ChemicalDataObject::exactMass
) );
128 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Electronegativity" ), ChemicalDataObject::electronegativityPauling
) );
129 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Covalent Radius" ), ChemicalDataObject::radiusCovalent
) );
130 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Van der Waals Radius" ), ChemicalDataObject::radiusVDW
) );
131 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Melting Point" ), ChemicalDataObject::meltingpoint
) );
132 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Boiling Point" ), ChemicalDataObject::boilingpoint
) );
133 ui
.propertyListWidget
->addItem( new PropertyListEntry( i18n( "Family" ), ChemicalDataObject::family
) );
136 void ExportDialog::slotOkClicked()
138 QString format
= ui
.formatList
->itemData( ui
.formatList
->currentIndex(), Qt::UserRole
).toString();
139 QString filename
= ui
.targetFile
->url().path();
140 if( !filename
.endsWith( format
) )
141 filename
+= "." + format
;
142 QFile
outputFile( filename
);
143 if( outputFile
.exists() )
145 if( KMessageBox::questionYesNo( this, i18n( "File already exists. Do you want to overwrite it?" ) ) == KMessageBox::No
)
150 if( !outputFile
.open( QIODevice::WriteOnly
) )
152 KMessageBox::error( this, i18n( "Could not open file for writing." ) );
156 m_outputStream
= new QTextStream( &outputFile
);
159 else if(format
== "xml")
168 void ExportDialog::exportToHtml()
170 *m_outputStream
<< HTML_HEADER
<< "<table>\n";
171 foreach( QListWidgetItem
* element
, ui
.elementListWidget
->selectedItems() )
173 *m_outputStream
<< "<tr>\n<th colspan=\"2\">"
174 << ( (ElementListEntry
* )element
)->m_element
->dataAsString( ChemicalDataObject::name
)
176 foreach( QListWidgetItem
* property
, ui
.propertyListWidget
->selectedItems() )
178 *m_outputStream
<< "<tr>\n<td class=\"property\">"
179 << ( ( PropertyListEntry
* )property
)->text()
180 << "</td>\n<td class=\"value\">"
181 << KalziumUtils::prettyUnit(
182 ( ( ElementListEntry
* )element
)->m_element
,
183 ( ( PropertyListEntry
* )property
)->m_type
)
187 *m_outputStream
<< "</table>\n" << HTML_FOOTER
;
190 void ExportDialog::exportToXml()
192 *m_outputStream
<< XML_HEADER
<< "<elements>\n";
193 foreach( QListWidgetItem
* element
, ui
.elementListWidget
->selectedItems() )
195 *m_outputStream
<< " <element name=\""
196 << ( (ElementListEntry
* )element
)->m_element
->dataAsString( ChemicalDataObject::name
)
198 foreach( QListWidgetItem
* property
, ui
.propertyListWidget
->selectedItems() )
200 *m_outputStream
<< " <property name=\""
201 << ( ( PropertyListEntry
* )property
)->text()
203 << KalziumUtils::prettyUnit(
204 ( ( ElementListEntry
* )element
)->m_element
,
205 ( ( PropertyListEntry
* )property
)->m_type
)
208 *m_outputStream
<< " </element>\n";
210 *m_outputStream
<< "</elements>\n";
213 void ExportDialog::exportToCsv()
215 *m_outputStream
<< "Name";
216 foreach( QListWidgetItem
* property
, ui
.propertyListWidget
->selectedItems() )
218 *m_outputStream
<< ", \""
219 << ( ( PropertyListEntry
* )property
)->text()
222 *m_outputStream
<< "\n";
223 foreach( QListWidgetItem
* element
, ui
.elementListWidget
->selectedItems() )
225 *m_outputStream
<< "\""
226 << ( (ElementListEntry
* )element
)->m_element
->dataAsString( ChemicalDataObject::name
)
228 foreach( QListWidgetItem
* property
, ui
.propertyListWidget
->selectedItems() )
230 *m_outputStream
<< ", \""
231 << KalziumUtils::prettyUnit(
232 ( ( ElementListEntry
* )element
)->m_element
,
233 ( ( PropertyListEntry
* )property
)->m_type
)
236 *m_outputStream
<< "\n";
240 void ExportDialog::slotHelpClicked()