• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kalgebra

  • sources
  • kde-4.12
  • kdeedu
  • kalgebra
  • plasmoids
  • calculator
kalgebraplasma.cpp
Go to the documentation of this file.
1 /*************************************************************************************
2  * Copyright (C) 2008 by Aleix Pol <aleixpol@kde.org> *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License *
6  * as published by the Free Software Foundation; either version 2 *
7  * of the License, or (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 Free Software *
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
17  *************************************************************************************/
18 
19 #include "kalgebraplasma.h"
20 
21 #include <QPainter>
22 #include <QFontMetrics>
23 #include <QSizeF>
24 #include <QLabel>
25 #include <QGraphicsProxyWidget>
26 #include <QFont>
27 #include <KIcon>
28 #include <KIconLoader>
29 
30 #include <plasma/theme.h>
31 #include <plasma/dataengine.h>
32 #include <plasma/tooltipcontent.h>
33 #include <plasma/tooltipmanager.h>
34 
35 #include <analitza/expression.h>
36 
37 using namespace Plasma;
38 using Analitza::Expression;
39 
40 QColor KAlgebraPlasmoid::correctColor() { return Theme::defaultTheme()->color(Theme::TextColor);}
41 QColor KAlgebraPlasmoid::errorColor() { return Qt::red; }
42 int KAlgebraPlasmoid::simplificationSize() { return Theme::defaultTheme()->font(Theme::DefaultFont).pointSize(); }
43 
44 KAlgebraPlasmoid::KAlgebraPlasmoid(QObject *parent, const QVariantList &args)
45  : PopupApplet(parent, args), m_widget(0), m_layout(0)
46 {
47  KGlobal::locale()->insertCatalog("kalgebra");
48  setAspectRatioMode(IgnoreAspectRatio);
49  setAssociatedApplication("kalgebra");
50 }
51 
52 KAlgebraPlasmoid::~KAlgebraPlasmoid() {}
53 
54 void KAlgebraPlasmoid::init()
55 {
56 // updateFactor();
57 
58  setPopupIcon("kalgebra");
59 }
60 
61 QGraphicsWidget* KAlgebraPlasmoid::graphicsWidget()
62 {
63  if(!m_widget) {
64  m_widget = new QGraphicsWidget(this);
65  m_input = new Plasma::LineEdit(m_widget);
66  m_input->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
67  m_input->setClearButtonShown(true);
68 
69  m_output = new Plasma::Label(m_widget);
70  m_output->setMinimumSize(20, 20);
71  m_output->nativeWidget()->setAlignment(Qt::AlignCenter);
72  m_output->setText(i18n("Enter some expression."));
73 
74  m_layout = new QGraphicsLinearLayout(m_widget);
75  m_layout->setOrientation(Qt::Vertical);
76  m_layout->addItem(m_input);
77  m_layout->addItem(m_output);
78  m_widget->setPreferredSize(300,300);
79 
80  connect(m_input, SIGNAL(editingFinished()), this, SLOT(addOperation()));
81  connect(m_input->nativeWidget(), SIGNAL(textChanged(QString)), this, SLOT(simplify()));
82  }
83  m_input->nativeWidget()->selectAll();
84  m_input->setFocus();
85 
86  return m_widget;
87 }
88 
89 void KAlgebraPlasmoid::addOperation()
90 {
91  if ( m_input->text().isEmpty() )
92  return;
93 
94  Expression res;
95  a.setExpression(Expression(m_input->text(), false));
96  if(a.isCorrect()) {
97  res=a.evaluate();
98  }
99 
100  QColor c;
101  if(a.isCorrect()) {
102  QString result=res.toString();
103  m_output->setText(result);
104 
105  Plasma::ToolTipContent data;
106  data.setMainText(i18n("KAlgebra"));
107  data.setSubText(i18n("%1 = %2", m_input->text(), result));
108  data.setImage(KIcon("kalgebra").pixmap(IconSize(KIconLoader::Desktop)));
109  Plasma::ToolTipManager::self()->setContent(this, data);
110 
111  c=correctColor();
112  } else {
113  m_output->setText(a.errors().join("\n"));
114  c=errorColor();
115  }
116  plasmoidFont(true, c, true);
117  update();
118 }
119 
120 void KAlgebraPlasmoid::plasmoidFont(bool big, const QColor& c, bool bold)
121 {
122  QFont f=m_output->nativeWidget()->font();
123  f.setBold(bold);
124  int size;
125 
126  if(big) {
127  size=(m_output->size().height()*2)/3;
128  f.setPointSize(size);
129  QFontMetrics fm(f);
130 
131  int w=m_output->size().width();
132  Q_ASSERT(w>0);
133  for(; fm.width(m_output->text()) > w; size--) {
134  f.setPointSize(size);
135  fm=QFontMetrics(f);
136  }
137  } else
138  size=simplificationSize();
139  f.setPointSize(size);
140 
141  QPalette palette = m_output->palette();
142  palette.setColor(QPalette::WindowText, c);
143  m_output->nativeWidget()->setPalette(palette);
144  m_output->nativeWidget()->setFont(f);
145 }
146 
147 void KAlgebraPlasmoid::simplify()
148 {
149  Expression e(m_input->text(), false);
150  if(e.isCorrect())
151  a.setExpression(e);
152 
153  if(e.isCorrect() && a.isCorrect()) {
154  a.simplify();
155  m_output->setText(a.expression().toString());
156 
157  plasmoidFont(false, correctColor(), true);
158  } else
159  m_output->setText(QString());
160 }
161 
162 #include "kalgebraplasma.moc"
QObject
KAlgebraPlasmoid::KAlgebraPlasmoid
KAlgebraPlasmoid(QObject *parent, const QVariantList &args)
Definition: kalgebraplasma.cpp:44
KAlgebraPlasmoid::init
void init()
Definition: kalgebraplasma.cpp:54
KAlgebraPlasmoid::graphicsWidget
virtual QGraphicsWidget * graphicsWidget()
Definition: kalgebraplasma.cpp:61
KAlgebraPlasmoid::~KAlgebraPlasmoid
~KAlgebraPlasmoid()
Definition: kalgebraplasma.cpp:52
kalgebraplasma.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalgebra

Skip menu "kalgebra"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal