1/***************************************************************************
2 * Copyright (C) 2009 by Kashyap R Puranik, kashthealien@gmail.com *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
19
20#include "calculator.h"
21
22#include <kdebug.h>
23#include <kactioncollection.h>
24#include <kstandardaction.h>
25#include <kstandarddirs.h>
26#include <ktoolinvocation.h>
27
28calculator::calculator(QWidget *parent)
29 : KDialog(parent)
30{
31 setCaption(i18n("Chemical Calculator"));
32 setButtons(Help | Close);
33 setDefaultButton(Close);
34
35 ui.setupUi(mainWidget());
36
37 int maxTextWidth = 0;
38 QStyleOptionViewItemV4 option;
39 option.initFrom(ui.tree);
40 for(int i = 0; i < ui.tree->topLevelItemCount(); ++i) {
41 maxTextWidth = qMax(maxTextWidth, ui.tree->itemDelegate()->sizeHint(option, ui.tree->model()->index(i, 0)).width());
42 }
43 // 20 because we want some margins, not a too tight text
44 ui.tree->setMaximumWidth(qMax(ui.tree->maximumWidth(), maxTextWidth + 20));
45
46 // Add the nuclear calculator to the user interface
47 m_nuclearCalculator = new nuclearCalculator(this);
48 ui.stack->addWidget(m_nuclearCalculator);
49 // Add the gas calculator to the user interface
50 m_gasCalculator = new gasCalculator(this);
51 ui.stack->addWidget(m_gasCalculator);
52 // Add the concentration calculator to the user interface
53 m_concCalculator = new concCalculator(this);
54 ui.stack->addWidget(m_concCalculator);
55 // Add the molecular mass Calculator widget to the user interface
56 m_moleCalculator = new MolcalcWidget(this);
57 ui.stack->addWidget(m_moleCalculator);
58 // Add the molecular mass Calculator widget to the user interface
59 m_titraCalculator = new titrationCalculator(this);
60 ui.stack->addWidget(m_titraCalculator);
61
62#ifdef HAVE_FACILE
63 // Add the equation balancer widget to the user interface
64 QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui.tree);
65 treeItem->setText(0, i18n("Equation Balancer"));
66
67 m_equationBalancer = new EQChemDialog(this);
68 ui.stack->addWidget(m_equationBalancer);
69#endif
70 // Add an image to the file
71 ui.pic->setPixmap( (KIcon( "calculate" )).pixmap(128,128) );
72
73 // Connect the tree item selection signal to the corresponding slot
74 connect(ui.tree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this,
75 SLOT(slotItemSelection(QTreeWidgetItem*)));
76
77 ui.tree->setCurrentItem ( ui.tree->topLevelItem(0), 0, QItemSelectionModel::ToggleCurrent );
78
79 // help clicked
80 connect( this, SIGNAL(helpClicked()), this, SLOT(slotHelp()) );
81}
82
83calculator :: ~calculator()
84{
85
86}
87
88void calculator::slotItemSelection(QTreeWidgetItem *item)
89{
90 if (item == 0L) return;
91
92 //DEBUG
93 kDebug() << "Item clicked: " << item->text(0);
94
95 QString s = item->text(0);
96
97 if (!(s.compare(i18n("Introduction"))))
98 ui.stack->setCurrentWidget(ui.intro);
99 // Check if nuclear calculator is selected, show the widget in the user interface
100 else if (!(s.compare(i18n("Nuclear Calculator"))))
101 ui.stack->setCurrentWidget(m_nuclearCalculator);
102
103 // Check if gas calculator is selected, show the widget in the user interface
104 else if (!(s.compare(i18n("Gas Calculator"))))
105 ui.stack->setCurrentWidget(m_gasCalculator);
106
107 // Check if the concentration calculator is selected, sho the widget in the UI if yes
108 else if (!(s.compare(i18n("Concentration Calculator"))))
109 ui.stack->setCurrentWidget(m_concCalculator);
110
111 // The equation balancer needs FACILE library, if its present HAVE_FACILE = 1
112 // If the equation balancer was selected, open it in the UI.
113#ifdef HAVE_FACILE
114 else if (!(s.compare(i18n("Equation Balancer"))))
115 ui.stack->setCurrentWidget(m_equationBalancer);
116#endif
117 else if (!(s.compare(i18n("Molecular mass Calculator"))))
118 ui.stack->setCurrentWidget(m_moleCalculator);
119 else if (!(s.compare(i18n("Titration Calculator"))))
120 ui.stack->setCurrentWidget(m_titraCalculator);
121}
122
123
124void calculator::slotHelp()
125{
126 KToolInvocation::invokeHelp( "calculator", "kalzium" );
127}
128
129