1
/***********************************************************************************
2
* Mass calculator: Plasmoid to calculate mass of a Molecule.
3
* Copyright (C) 2009, 2010 Etienne Rebetez, etienne.rebetez@oberwallis.ch
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
***********************************************************************************/
22
#include <QtGui/QApplication>
23
#include <QGraphicsLinearLayout>
28
#include <KConfigDialog>
29
#include <psetables.h>
32
#include <plasma/theme.h>
35
#include "Molmasscalculator.h"
37
Molmasscalculator::Molmasscalculator ( QObject *parent, const QVariantList &args )
38
: Plasma::PopupApplet ( parent, args ),
45
// Some Applet settings
46
setAspectRatioMode ( Plasma::IgnoreAspectRatio );
47
setHasConfigurationInterface ( true );
48
setAcceptDrops ( false );
49
setAssociatedApplication("kalzium");
51
m_triggerTimer = new QTimer();
52
m_triggerTimer->setSingleShot( true );
53
m_triggerTimer->setInterval( 700 );
55
connect(m_triggerTimer, SIGNAL(timeout()), this, SLOT(ParseMolecule()));
57
// Needed for the Popupapplet
58
setPopupIcon ( "kalzium" );
61
Molmasscalculator::~Molmasscalculator()
63
if (!hasFailedToLaunch()) {
67
delete m_switchButton;
70
delete m_PeriodWidget;
72
delete m_triggerTimer;
75
void Molmasscalculator::init()
77
// checking if the Dataengine is availiable
78
if (!dataEngine( "kalzium" )->isValid()) {
79
setFailedToLaunch(true, i18n("Dataengine \"kalzium\" not found"));
82
// loads the configuration
85
// Create the Periodic Table
86
m_PeriodWidget = new PeriodicGrid( config().readEntry("tableType", 0 ), this );
90
// Calculate the demo molecule.
97
void Molmasscalculator::appendElement ( QString elementSymbol )
100
molecule = m_lineedit->text();
101
molecule.append ( elementSymbol );
103
ParseMolecule( molecule );
106
void Molmasscalculator::ParseMolecule( QString strInput )
108
if ( !strInput.isEmpty() ) {
109
m_molecule = dataEngine ( "kalzium" )->query ( QString ( "Molecule:Parser:" ) + strInput );
114
void Molmasscalculator::ParseMolecule()
116
ParseMolecule( m_lineedit->text() );
119
void Molmasscalculator::newCalculatedMass()
121
if ( m_molecule["molMass"].toString() == QString() ) {
122
m_MassLabel->setText ( i18n ( "Invalid Molecule" ) );
126
//Set new MassLabel Text
127
m_MassLabel->setText ( QString::number( m_molecule["molMass"].toDouble(), 'g', 6) + " u" );
129
//Sets the niceMolecule string in the Lineedit //Configuration Option?
130
m_lineedit->setText ( m_molecule["niceMolecule"].toString() );
132
//Copy new Mass to Clipboard
133
if ( m_copyToClipboard )
134
QApplication::clipboard()->setText ( m_molecule["molMass"].toString() );
137
QGraphicsWidget *Molmasscalculator::graphicsWidget()
142
m_widget = new QGraphicsWidget(this);
144
QGraphicsLinearLayout *MainLayout = new QGraphicsLinearLayout ( Qt::Vertical , m_widget );
145
QGraphicsLinearLayout *TopLayout = new QGraphicsLinearLayout ( Qt::Horizontal, MainLayout );
147
Plasma::Label *MoleculeLabel = new Plasma::Label;
148
MoleculeLabel->setText ( i18n ( "Molecule:" ) );
150
m_MassLabel = new Plasma::Label;
151
m_MassLabel->setAlignment ( Qt::AlignCenter );
153
QString css("font-size:18px; color:" + this->palette().text().color().name() + ";");
154
m_MassLabel->setStyleSheet ( css );
156
m_lineedit = new Plasma::LineEdit();
157
m_lineedit->setClearButtonShown ( true );
158
m_lineedit->setMinimumWidth ( 100 );
159
m_lineedit->setText ( i18n ( "C2H5OH" ) );
160
connect ( m_lineedit, SIGNAL( textEdited(QString)), m_triggerTimer, SLOT( start() ) );
162
m_switchButton = new Plasma::IconWidget();
163
connect ( m_switchButton, SIGNAL( clicked() ), this, SLOT( toggleTable() ) );
165
TopLayout->addItem ( MoleculeLabel );
166
TopLayout->addItem ( m_lineedit );
167
TopLayout->addItem ( m_switchButton );
168
TopLayout->setSpacing( 0 );
169
TopLayout->setContentsMargins(0,0,0,0);
171
MainLayout->addItem ( TopLayout );
172
MainLayout->addItem ( m_MassLabel );
173
MainLayout->addItem ( m_PeriodWidget );
174
MainLayout->setSpacing( 2 );
179
void Molmasscalculator::toggleTable()
181
if ( m_showPeriodicTable ) {
182
m_showPeriodicTable = false;
184
m_showPeriodicTable = true;
186
managePeriodSystem();
189
void Molmasscalculator::managePeriodSystem()
192
KIconLoader iconLoader;
195
if ( m_showPeriodicTable ) {
196
iconName = "arrow-down";
197
m_PeriodWidget->show();
198
x = pseTables::instance()->getTabletype(m_PeriodWidget->getCurrentPseTyp())->tableSize().x() * 33;
199
y = pseTables::instance()->getTabletype(m_PeriodWidget->getCurrentPseTyp())->tableSize().y() * 34;
201
iconName = "arrow-right";
202
m_PeriodWidget->hide();
207
m_switchButton->setIcon( iconLoader.loadIcon( iconName, KIconLoader::Small ) );
209
m_widget->setPreferredSize( x, y );
210
m_widget->resize( x, y );
214
void Molmasscalculator::createConfigurationInterface ( KConfigDialog* parent )
216
parent->setButtons ( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
218
QWidget *widget = new QWidget( parent );
220
m_ui.setupUi ( widget );
222
parent->addPage ( widget, i18n ( "General" ), "kalzium" );
224
m_ui.showPeriodic->setChecked ( m_showPeriodicTable );
225
m_ui.copyToCliboard->setChecked ( m_copyToClipboard );
227
foreach(QString thisTable, pseTables::instance()->tables()) {
228
m_ui.tabletyp->addItem(thisTable);
231
m_ui.tabletyp->setCurrentIndex(m_PeriodWidget->getCurrentPseTyp());
233
connect ( parent, SIGNAL ( applyClicked() ), this, SLOT ( configAccepted() ) );
234
connect ( parent, SIGNAL ( okClicked() ), this, SLOT ( configAccepted() ) );
236
connect (m_ui.showPeriodic, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
237
connect (m_ui.copyToCliboard, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
238
connect (m_ui.tabletyp, SIGNAL(currentIndexChanged(QString)), parent, SLOT(settingsModified()));
241
void Molmasscalculator::configAccepted()
243
if ( m_ui.showPeriodic->isChecked() != m_showPeriodicTable ) {
244
m_showPeriodicTable = m_ui.showPeriodic->isChecked();
245
managePeriodSystem();
248
if ( m_ui.copyToCliboard->checkState() != m_copyToClipboard ) {
249
m_copyToClipboard = m_ui.copyToCliboard->checkState();
252
if ( m_ui.tabletyp->currentIndex() != m_PeriodWidget->getCurrentPseTyp()) {
253
m_PeriodWidget->setCurrentPseTyp(m_ui.tabletyp->currentIndex());
254
managePeriodSystem();
259
void Molmasscalculator::saveConfig()
261
config().writeEntry("showPeriodicTable",m_showPeriodicTable);
262
config().writeEntry("copyToClipboard",m_copyToClipboard);
263
config().writeEntry("tableType",m_PeriodWidget->getCurrentPseTyp());
266
void Molmasscalculator::configChanged()
268
m_showPeriodicTable = config().readEntry("showPeriodicTable", true );
269
m_copyToClipboard = config().readEntry("copyToClipboard", false );
272
#include "Molmasscalculator.moc"