~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to plasmoid/applet/psePlasmoid/Molmasscalculator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***********************************************************************************
 
2
* Mass calculator: Plasmoid to calculate mass of a Molecule.
 
3
* Copyright (C) 2009, 2010 Etienne Rebetez, etienne.rebetez@oberwallis.ch
 
4
*
 
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.
 
9
*
 
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.
 
14
*
 
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.
 
18
*
 
19
***********************************************************************************/
 
20
 
 
21
// Qt
 
22
#include <QtGui/QApplication>
 
23
#include <QGraphicsLinearLayout>
 
24
#include <QClipboard>
 
25
 
 
26
// KDE
 
27
#include <KLocale>
 
28
#include <KConfigDialog>
 
29
#include <psetables.h>
 
30
 
 
31
// Plasma
 
32
#include <plasma/theme.h>
 
33
 
 
34
// local
 
35
#include "Molmasscalculator.h"
 
36
 
 
37
Molmasscalculator::Molmasscalculator ( QObject *parent, const QVariantList &args )
 
38
        : Plasma::PopupApplet ( parent, args ),
 
39
        m_widget( 0 ),
 
40
        m_PeriodWidget( 0 ),
 
41
        m_lineedit( 0 ),
 
42
        m_MassLabel( 0 ),
 
43
        m_switchButton( 0 )
 
44
{
 
45
    // Some Applet settings
 
46
    setAspectRatioMode ( Plasma::IgnoreAspectRatio );
 
47
    setHasConfigurationInterface ( true );
 
48
    setAcceptDrops ( false );
 
49
    setAssociatedApplication("kalzium");
 
50
 
 
51
    m_triggerTimer = new QTimer();
 
52
    m_triggerTimer->setSingleShot( true );
 
53
    m_triggerTimer->setInterval( 700 );
 
54
 
 
55
    connect(m_triggerTimer, SIGNAL(timeout()), this, SLOT(ParseMolecule()));
 
56
 
 
57
    // Needed for the Popupapplet
 
58
    setPopupIcon ( "kalzium" );
 
59
}
 
60
 
 
61
Molmasscalculator::~Molmasscalculator()
 
62
{
 
63
    if (!hasFailedToLaunch()) {
 
64
        saveConfig();
 
65
    }
 
66
 
 
67
    delete m_switchButton;
 
68
    delete m_MassLabel;
 
69
    delete m_lineedit;
 
70
    delete m_PeriodWidget;
 
71
    delete m_widget;
 
72
    delete m_triggerTimer;
 
73
}
 
74
 
 
75
void Molmasscalculator::init()
 
76
{
 
77
    // checking if the Dataengine is availiable
 
78
    if (!dataEngine( "kalzium" )->isValid()) {
 
79
        setFailedToLaunch(true, i18n("Dataengine \"kalzium\" not found"));
 
80
    }
 
81
 
 
82
    // loads the configuration
 
83
    configChanged();
 
84
 
 
85
    // Create the Periodic Table
 
86
    m_PeriodWidget = new PeriodicGrid( config().readEntry("tableType", 0 ), this );
 
87
 
 
88
    graphicsWidget();
 
89
 
 
90
    // Calculate the demo molecule.
 
91
    ParseMolecule();
 
92
 
 
93
    //set sizes
 
94
    managePeriodSystem();
 
95
}
 
96
 
 
97
void Molmasscalculator::appendElement ( QString elementSymbol )
 
98
{
 
99
    QString molecule;
 
100
    molecule = m_lineedit->text();
 
101
    molecule.append ( elementSymbol );
 
102
 
 
103
    ParseMolecule( molecule );
 
104
}
 
105
 
 
106
void Molmasscalculator::ParseMolecule( QString strInput )
 
107
{
 
108
    if ( !strInput.isEmpty() ) {
 
109
        m_molecule = dataEngine ( "kalzium" )->query ( QString ( "Molecule:Parser:" ) + strInput );
 
110
        newCalculatedMass();
 
111
    }
 
112
}
 
113
 
 
114
void Molmasscalculator::ParseMolecule()
 
115
{
 
116
    ParseMolecule( m_lineedit->text() );
 
117
}
 
118
 
 
119
void Molmasscalculator::newCalculatedMass()
 
120
{
 
121
    if ( m_molecule["molMass"].toString() == QString() ) {
 
122
        m_MassLabel->setText ( i18n ( "Invalid Molecule" ) );
 
123
        return;
 
124
    }
 
125
 
 
126
    //Set new MassLabel Text
 
127
    m_MassLabel->setText ( QString::number( m_molecule["molMass"].toDouble(), 'g', 6) + " u" );
 
128
 
 
129
    //Sets the niceMolecule string in the Lineedit        //Configuration Option?
 
130
    m_lineedit->setText ( m_molecule["niceMolecule"].toString() );
 
131
 
 
132
    //Copy new Mass to Clipboard
 
133
    if ( m_copyToClipboard )
 
134
        QApplication::clipboard()->setText ( m_molecule["molMass"].toString() );
 
135
}
 
136
 
 
137
QGraphicsWidget *Molmasscalculator::graphicsWidget()
 
138
{
 
139
    if ( m_widget )
 
140
        return m_widget;
 
141
 
 
142
    m_widget = new QGraphicsWidget(this);
 
143
 
 
144
    QGraphicsLinearLayout *MainLayout = new QGraphicsLinearLayout ( Qt::Vertical , m_widget );
 
145
    QGraphicsLinearLayout *TopLayout = new QGraphicsLinearLayout ( Qt::Horizontal, MainLayout );
 
146
 
 
147
    Plasma::Label *MoleculeLabel = new Plasma::Label;
 
148
    MoleculeLabel->setText ( i18n ( "Molecule:" ) );
 
149
 
 
150
    m_MassLabel = new Plasma::Label;
 
151
    m_MassLabel->setAlignment ( Qt::AlignCenter );
 
152
 
 
153
    QString css("font-size:18px; color:" + this->palette().text().color().name() + ";");
 
154
    m_MassLabel->setStyleSheet ( css );
 
155
 
 
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() ) );
 
161
 
 
162
    m_switchButton = new Plasma::IconWidget();
 
163
    connect ( m_switchButton, SIGNAL( clicked() ), this, SLOT( toggleTable() ) );
 
164
 
 
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);
 
170
 
 
171
    MainLayout->addItem ( TopLayout );
 
172
    MainLayout->addItem ( m_MassLabel );
 
173
    MainLayout->addItem ( m_PeriodWidget );
 
174
    MainLayout->setSpacing( 2 );
 
175
 
 
176
    return m_widget;
 
177
}
 
178
 
 
179
void Molmasscalculator::toggleTable()
 
180
{
 
181
    if ( m_showPeriodicTable ) {
 
182
        m_showPeriodicTable = false;
 
183
    } else {
 
184
        m_showPeriodicTable = true;
 
185
    }
 
186
    managePeriodSystem();
 
187
}
 
188
 
 
189
void Molmasscalculator::managePeriodSystem()
 
190
{
 
191
    QString iconName;
 
192
    KIconLoader iconLoader;
 
193
    int x, y;
 
194
 
 
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;
 
200
    } else {
 
201
        iconName = "arrow-right";
 
202
        m_PeriodWidget->hide();
 
203
        x = 300;
 
204
        y = 60;
 
205
    }
 
206
 
 
207
    m_switchButton->setIcon( iconLoader.loadIcon( iconName, KIconLoader::Small ) );
 
208
 
 
209
    m_widget->setPreferredSize( x, y );
 
210
    m_widget->resize( x, y );
 
211
    resize( x, y );
 
212
}
 
213
 
 
214
void Molmasscalculator::createConfigurationInterface ( KConfigDialog* parent )
 
215
{
 
216
    parent->setButtons ( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
 
217
 
 
218
    QWidget *widget = new QWidget( parent );
 
219
 
 
220
    m_ui.setupUi ( widget );
 
221
 
 
222
    parent->addPage ( widget, i18n ( "General" ), "kalzium" );
 
223
 
 
224
    m_ui.showPeriodic->setChecked ( m_showPeriodicTable );
 
225
    m_ui.copyToCliboard->setChecked ( m_copyToClipboard );
 
226
 
 
227
    foreach(QString thisTable, pseTables::instance()->tables()) {
 
228
        m_ui.tabletyp->addItem(thisTable);
 
229
    }
 
230
 
 
231
    m_ui.tabletyp->setCurrentIndex(m_PeriodWidget->getCurrentPseTyp());
 
232
 
 
233
    connect ( parent, SIGNAL ( applyClicked() ), this, SLOT ( configAccepted() ) );
 
234
    connect ( parent, SIGNAL ( okClicked() ), this, SLOT ( configAccepted() ) );
 
235
 
 
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()));
 
239
}
 
240
 
 
241
void Molmasscalculator::configAccepted()
 
242
{
 
243
    if ( m_ui.showPeriodic->isChecked() != m_showPeriodicTable ) {
 
244
        m_showPeriodicTable = m_ui.showPeriodic->isChecked();
 
245
        managePeriodSystem();
 
246
    }
 
247
 
 
248
    if ( m_ui.copyToCliboard->checkState() != m_copyToClipboard ) {
 
249
        m_copyToClipboard = m_ui.copyToCliboard->checkState();
 
250
    }
 
251
 
 
252
    if ( m_ui.tabletyp->currentIndex() != m_PeriodWidget->getCurrentPseTyp()) {
 
253
        m_PeriodWidget->setCurrentPseTyp(m_ui.tabletyp->currentIndex());
 
254
        managePeriodSystem();
 
255
    }
 
256
    saveConfig();
 
257
}
 
258
 
 
259
void Molmasscalculator::saveConfig()
 
260
{
 
261
    config().writeEntry("showPeriodicTable",m_showPeriodicTable);
 
262
    config().writeEntry("copyToClipboard",m_copyToClipboard);
 
263
    config().writeEntry("tableType",m_PeriodWidget->getCurrentPseTyp());
 
264
}
 
265
 
 
266
void Molmasscalculator::configChanged()
 
267
{
 
268
    m_showPeriodicTable = config().readEntry("showPeriodicTable", true );
 
269
    m_copyToClipboard = config().readEntry("copyToClipboard", false );
 
270
}
 
271
 
 
272
#include "Molmasscalculator.moc"