Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / exportdialog.cpp
blob6756db1013b2315b15bee886aa126c8dfd377081
1 /***************************************************************************
2 copyright : (C) 2007 by Johannes Simon
3 email : johannes.simon@gmail.com
4 ***************************************************************************/
5 /***************************************************************************
6 * *
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. *
11 * *
12 ***************************************************************************/
14 #include "exportdialog.h"
15 #include "kalziumutils.h"
17 #include <QFont>
18 #include <KMessageBox>
20 #include <kdebug.h>
23 static const char HTML_HEADER[] =
24 "<html>"
25 "\n<head>"
26 "\n<style type=\"text/css\">"
27 "\nbody {"
28 "\n font-family:arial;"
29 "\n}"
30 "\n.property {"
31 "\n font-style:italic;"
32 "\n}"
33 "\nth {"
34 "\n font-weight:bold;"
35 "\n text-align:left;"
36 "\n background-color:#F0F0F0;"
37 "\n}"
38 "\n</style>"
39 "\n</head>"
40 "\n<body>";
42 static const char HTML_FOOTER[] =
43 "\n</body>"
44 "\n</html>";
46 static const char XML_HEADER[] =
47 "<?xml version=\"1.0\"?>\n";
49 ElementListEntry::ElementListEntry( Element * element)
50 : QListWidgetItem()
52 m_atomicNum = element->dataAsVariant(ChemicalDataObject::atomicNumber).toInt();
53 m_name = element->dataAsString(ChemicalDataObject::name);
54 m_element = element;
56 setText(m_name);
59 ElementListEntry::~ElementListEntry()
63 PropertyListEntry::PropertyListEntry( const QString & name, ChemicalDataObject::BlueObelisk type )
64 : QListWidgetItem()
66 setText( name );
67 m_type = type;
70 PropertyListEntry::~PropertyListEntry()
74 ExportDialog::ExportDialog( QWidget * parent )
75 : KDialog( 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 );
107 QFont font;
108 font.setBold( true );
109 header1->setFont( font );
110 header2->setFont( font );
111 ui.elementListWidget->addItem( header1 );
112 ui.propertyListWidget->addItem( header2 );
114 // Add elements
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 )
147 return;
150 if( !outputFile.open( QIODevice::WriteOnly ) )
152 KMessageBox::error( this, i18n( "Could not open file for writing." ) );
153 return;
156 m_outputStream = new QTextStream( &outputFile );
157 if(format == "html")
158 exportToHtml();
159 else if(format == "xml")
160 exportToXml();
161 else
162 exportToCsv();
164 // close the dialog
165 done(0);
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 )
175 << "</th>\n</tr>\n";
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 )
184 << "</td>\n</tr>\n";
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 )
197 << "\">\n";
198 foreach( QListWidgetItem * property, ui.propertyListWidget->selectedItems() )
200 *m_outputStream << " <property name=\""
201 << ( ( PropertyListEntry* )property )->text()
202 << "\">"
203 << KalziumUtils::prettyUnit(
204 ( ( ElementListEntry* )element )->m_element,
205 ( ( PropertyListEntry* )property )->m_type )
206 << "</property>\n";
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()
220 << "\"";
222 *m_outputStream << "\n";
223 foreach( QListWidgetItem * element, ui.elementListWidget->selectedItems() )
225 *m_outputStream << "\""
226 << ( (ElementListEntry* )element )->m_element->dataAsString( ChemicalDataObject::name )
227 << "\"";
228 foreach( QListWidgetItem * property, ui.propertyListWidget->selectedItems() )
230 *m_outputStream << ", \""
231 << KalziumUtils::prettyUnit(
232 ( ( ElementListEntry* )element )->m_element,
233 ( ( PropertyListEntry* )property )->m_type )
234 << "\"";
236 *m_outputStream << "\n";
240 void ExportDialog::slotHelpClicked()