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

« back to all changes in this revision

Viewing changes to plasmoid/applet/bodr/kalzium_plasma.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
    copyright            : (C) 2008 by Carsten Niehaus
 
3
    email                : cniehaus@kde.org
 
4
    Copyright 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
 
5
 ***************************************************************************/
 
6
/***************************************************************************
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or modify  *
 
9
 *   it under the terms of the GNU General Public License as published by  *
 
10
 *   the Free Software Foundation; either version 2 of the License, or     *
 
11
 *   (at your option) any later version.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
#include "kalzium_plasma.h"
 
15
 
 
16
#include <KDialog>
 
17
#include <KConfigGroup>
 
18
#include <KFontDialog>
 
19
#include <KLineEdit>
 
20
 
 
21
#include <QDebug>
 
22
#include <QPainter>
 
23
#include <QPen>
 
24
#include <QColor>
 
25
 
 
26
KalziumPlasma::KalziumPlasma(QObject *parent, const QVariantList &args)
 
27
    : Plasma::Applet(parent, args),
 
28
    m_dialog(0),
 
29
    m_font(QFont())
 
30
{
 
31
    m_theme.setImagePath("widgets/testtube"),
 
32
    m_engine = dataEngine("kalzium");
 
33
 
 
34
    m_dialog = 0;
 
35
    m_label1 = 0;
 
36
    m_lineedit = new Plasma::LineEdit( this );
 
37
    m_lineedit->setDefaultText( i18n("Enter the atomic number.") );
 
38
    connect( m_lineedit, SIGNAL(editingFinished() ),
 
39
            this, SLOT(textChanged() ) );
 
40
 
 
41
    setHasConfigurationInterface(true);
 
42
    setAcceptDrops(false);
 
43
    setAcceptsHoverEvents(true);
 
44
//     setDrawStandardBackground(false);
 
45
 
 
46
    resize(385,177);
 
47
}
 
48
 
 
49
void KalziumPlasma::init()
 
50
{
 
51
    qDebug() << "initializing Kalzium";
 
52
 
 
53
    configChanged();
 
54
 
 
55
    m_currentSource = "BlueObelisk:RandomElement";
 
56
    // connect to random source and update ever 5 seconds
 
57
    m_engine->connectSource( m_currentSource, this, 5000);
 
58
 
 
59
    m_theme.setContainsMultipleImages(false);
 
60
 
 
61
    m_label1 = new QGraphicsTextItem(this);
 
62
    m_label1->setPos( m_theme.elementRect( "name" ).topLeft() );
 
63
    m_label1->setFont(m_font);
 
64
    m_label1->setDefaultTextColor(Qt::white);
 
65
}
 
66
 
 
67
void KalziumPlasma::configChanged()
 
68
{
 
69
    KConfigGroup cg = config();
 
70
    m_font = cg.readEntry("font",QFont());
 
71
}
 
72
 
 
73
void KalziumPlasma::constraintsUpdated(Plasma::Constraints constraints)
 
74
{
 
75
//     setDrawStandardBackground(false);
 
76
    prepareGeometryChange();
 
77
    if (constraints & Plasma::SizeConstraint) {
 
78
        //m_theme.resize(contentSize().toSize());
 
79
        m_theme.resize(size());
 
80
    }
 
81
 
 
82
    m_label1->setPos( m_theme.elementRect( "canvas" ).topLeft() );
 
83
}
 
84
 
 
85
KalziumPlasma::~KalziumPlasma()
 
86
{
 
87
    delete m_dialog;
 
88
}
 
89
 
 
90
void KalziumPlasma::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
 
91
{
 
92
    qDebug() << "dataUpdated" << source;
 
93
    if (source != m_currentSource) {
 
94
        return;
 
95
    }
 
96
 
 
97
    QString bp = data["bp"].toString();
 
98
    QString mp = data["mp"].toString();
 
99
    QString mass = data["mass"].toString();
 
100
    QString symbol = data["symbol"].toString();
 
101
    QString name = data["name"].toString();
 
102
    QString text;
 
103
    text = QString(i18n( "\nName: %1", name ));
 
104
    text.append(QString(i18n( "\nSymbol: %1", symbol)));
 
105
    text.append(QString(i18n( "\nBoiling point: %1", bp)));
 
106
    text.append(QString(i18n( "\nMelting point: %1", mp)));
 
107
    text.append(QString(i18n( "\nMass: %1", mass)));
 
108
    if (m_label1)  {
 
109
//         m_label1->setAlignment(Qt::AlignLeft);
 
110
        m_label1->setPlainText(text);
 
111
    }
 
112
}
 
113
 
 
114
void KalziumPlasma::paintInterface(QPainter *p,
 
115
                       const QStyleOptionGraphicsItem *option,
 
116
                       const QRect &contentsRect)
 
117
{
 
118
    Q_UNUSED(option);
 
119
 
 
120
    p->setRenderHint(QPainter::SmoothPixmapTransform);
 
121
    p->setRenderHint(QPainter::Antialiasing);
 
122
 
 
123
    // Now we draw the applet, starting with our svg
 
124
    //m_theme.resize((int)contentsRect.width(), (int)contentsRect.height());
 
125
    m_theme.resize(size());
 
126
    //m_theme.paint(p, (int)contentsRect.left(), (int)contentsRect.top());
 
127
    m_theme.paint(p, 0,0 );
 
128
 
 
129
    m_lineedit->setPos( 0,150 );
 
130
}
 
131
 
 
132
void KalziumPlasma::showConfigurationInterface()
 
133
{
 
134
if (m_dialog == 0) {
 
135
    m_dialog = new KDialog;
 
136
    m_dialog->setWindowIcon(KIcon("kalzium"));
 
137
        m_dialog->setCaption( i18n("KalziumPlasma Configuration") );
 
138
        ui.setupUi(m_dialog->mainWidget());
 
139
        m_dialog->mainWidget()->layout()->setMargin(0);
 
140
        m_dialog->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
 
141
        connect( m_dialog, SIGNAL(applyClicked()), 
 
142
                this, SLOT(configAccepted()) );
 
143
        connect( m_dialog, SIGNAL(okClicked()), 
 
144
                this, SLOT(configAccepted()) );
 
145
        connect( ui.fontSelectButton, SIGNAL(clicked()), 
 
146
                this, SLOT(showFontSelectDlg()) );
 
147
    }
 
148
 
 
149
    m_dialog->show();
 
150
}
 
151
 
 
152
void KalziumPlasma::showFontSelectDlg()
 
153
{
 
154
    KFontDialog::getFont(m_font);
 
155
}
 
156
 
 
157
void KalziumPlasma::configAccepted()
 
158
{
 
159
    prepareGeometryChange();
 
160
 
 
161
    KConfigGroup cg = config();
 
162
    cg.writeEntry("font", m_font);
 
163
    m_label1->setFont(m_font);
 
164
 
 
165
    emit configNeedsSaving();
 
166
}
 
167
 
 
168
void KalziumPlasma::textChanged()
 
169
{
 
170
    m_engine->disconnectSource(m_currentSource, this);
 
171
 
 
172
    QString currentText = m_lineedit->toPlainText();
 
173
    if (!currentText.isEmpty()) {
 
174
        // simply assume the user entered the atomic number of the element
 
175
        m_currentSource = QString("BlueObelisk:Element:") + currentText;
 
176
    } else {
 
177
        m_currentSource = QString("BlueObelisk:RandomElement");
 
178
    }
 
179
    m_engine->connectSource( m_currentSource, this, 5000);
 
180
}
 
181
 
 
182
#include "kalzium_plasma.moc"