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

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • plasmoid
parley_plasma.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  Copyright 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
4 
5  ***************************************************************************/
6 
7 /***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 
17 #include "parley_plasma.h"
18 
19 #include <KDialog>
20 #include <KConfigGroup>
21 #include <KConfigDialog>
22 #include <KFontDialog>
23 #include <KColorDialog>
24 #include <KDebug>
25 
26 ParleyPlasma::ParleyPlasma(QObject *parent, const QVariantList &args)
27  : Plasma::Applet(parent, args),
28  m_label1(0),
29  m_label2(0),
30  m_font(QFont())
31 {
32  KGlobal::locale()->insertCatalog("parley");
33  resize(256, 160);
34  setHasConfigurationInterface(true);
35  setAcceptDrops(false);
36  setAcceptsHoverEvents(true);
37  setAspectRatioMode(Plasma::KeepAspectRatio);
38  setBackgroundHints(NoBackground);
39 
40  m_theme = new Plasma::Svg(this);
41  m_theme->setImagePath("widgets/parley_plasma_card");
42  m_theme->setContainsMultipleImages(false);
43  m_theme->resize(size());
44  setAssociatedApplication("parley");
45 }
46 
47 void ParleyPlasma::init()
48 {
49  m_theme->size().height();
50 
51  configChanged();
52 
53 
54  m_engine = dataEngine("parley");
55 
56  m_label1 = new QGraphicsTextItem(this);
57  m_label2 = new QGraphicsTextItem(this);
58  m_label1->setPos(m_theme->elementRect("translation1").topLeft());
59  m_label2->setPos(m_theme->elementRect("translation2").topLeft());
60  m_label1->setFont(m_font);
61  m_label2->setFont(m_font);
62 
63  m_engine->connectSource(m_sourceFile.url(), this, m_updateInterval);
64 }
65 
66 void ParleyPlasma::configChanged()
67 {
68  KConfigGroup cg = config();
69  m_updateInterval = cg.readEntry("updateInterval", 10000);
70  m_solutionType = cg.readEntry("Solution", 0);
71 
72  m_lang1 = cg.readEntry("Top Language", 0);
73  m_lang2 = cg.readEntry("Bottom Language", 1);
74  m_font = cg.readEntry("font", QFont());
75 
76  m_sourceFile = cg.readEntry("File Name", KUrl());
77  if (m_sourceFile.isEmpty()) {
78  kDebug() << "open file from parleyrc";
79  KConfig parleyConfig("parleyrc");
80  kDebug() << parleyConfig.groupList();
81  KConfigGroup recentFilesGroup(&parleyConfig, "Recent Files");
82  // take the last file, but there are File1..n and Name1..n entries..
83  m_sourceFile = recentFilesGroup.readEntry(recentFilesGroup.keyList().value(recentFilesGroup.keyList().count() / 2 - 1), KUrl());
84  kDebug() << "open file: " << m_sourceFile;
85  }
86 }
87 
88 void ParleyPlasma::constraintsEvent(Plasma::Constraints constraints)
89 {
90  if (constraints & Plasma::FormFactorConstraint) {
91  setBackgroundHints(NoBackground);
92  }
93 
94  if (constraints & Plasma::SizeConstraint) {
95  double aspect = 256.0 / 160.0; // original aspect ratio
96  if (formFactor() == Plasma::Horizontal) {
97  // We have a fixed height, set some sensible width
98  setMinimumWidth(contentsRect().height() * aspect);
99  } else if (formFactor() == Plasma::Vertical) {
100  // We have a fixed width, set some sensible height
101  setMinimumHeight((int)contentsRect().width() / aspect);
102  }
103 
104  m_label1->setPos(m_theme->elementRect("translation1").topLeft());
105  m_label1->setFont(m_font);
106  double scale = qMin(m_theme->elementRect("translation1").width() / m_label1->boundingRect().width(), m_theme->elementRect("translation1").height() / m_label1->boundingRect().height());
107  m_label1->setTransform(QTransform().scale(scale, scale));
108 
109  m_label2->setPos(m_theme->elementRect("translation2").topLeft());
110  m_label2->setFont(m_font);
111  scale = qMin(m_theme->elementRect("translation2").width() / m_label2->boundingRect().width(), m_theme->elementRect("translation2").height() / m_label2->boundingRect().height());
112  m_label2->setTransform(QTransform().scale(scale, scale));
113  }
114 }
115 
116 ParleyPlasma::~ParleyPlasma()
117 {
118 }
119 
120 void ParleyPlasma::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
121 {
122  kDebug() << "data updated" << source << data;
123 
124  m_languages = data.keys();
125 
126  if (m_label1) {
127  m_label1->setPlainText(data[m_languages.value(m_lang1)].toString());
128  double scale = qMin(m_theme->elementRect("translation1").width() / m_label1->boundingRect().width(), m_theme->elementRect("translation1").height() / m_label1->boundingRect().height());
129  m_label1->setTransform(QTransform().scale(scale, scale));
130  m_label1->setPos(m_theme->elementRect("translation1").topLeft()
131  + QPointF(
132  (m_theme->elementRect("translation1").width() - m_label1->boundingRect().width()*scale) / 2.0,
133  (m_theme->elementRect("translation1").height() - m_label1->boundingRect().height()*scale) / 2.0));
134 
135  if (m_languages.size() > 1) {
136  m_label2->setPlainText(data[m_languages.value(m_lang2)].toString());
137  scale = qMin(m_theme->elementRect("translation2").width() / m_label2->boundingRect().width(), m_theme->elementRect("translation2").height() / m_label2->boundingRect().height());
138  m_label2->setTransform(QTransform().scale(scale, scale));
139  if (m_solutionType == Hover) {
140  m_label2->hide();
141  }
142  m_label2->setPos(m_theme->elementRect("translation2").topLeft()
143  + QPointF(
144  (m_theme->elementRect("translation2").width() - m_label2->boundingRect().width()*scale) / 2.0,
145  (m_theme->elementRect("translation2").height() - m_label2->boundingRect().height()*scale) / 2.0));
146  }
147  }
148 }
149 
150 void ParleyPlasma::setContentSize(const QSizeF& size)
151 {
152  m_size = size;
153 }
154 
155 QSizeF ParleyPlasma::contentSizeHint() const
156 {
157  return m_size;
158 }
159 
160 void ParleyPlasma::paintInterface(QPainter *p,
161  const QStyleOptionGraphicsItem *option,
162  const QRect &contentsRect)
163 {
164  Q_UNUSED(option);
165 
166  QSizeF boundSize = geometry().size();
167  m_theme->resize(boundSize);
168  m_theme->paint(p, contentsRect, "Card");
169 }
170 
171 void ParleyPlasma::createConfigurationInterface(KConfigDialog * parent)
172 {
173  QWidget *widget = new QWidget();
174  ui.setupUi(widget);
175  parent->addPage(widget, parent->windowTitle(), icon());
176  parent->setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
177  connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
178  connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
179 
180  connect(ui.fontSelectButton, SIGNAL(clicked()), this, SLOT(showFontSelectDlg()));
181 
182  ui.updateIntervalSpinBox->setValue(m_updateInterval / 1000);
183  ui.updateIntervalSpinBox->setSuffix(ki18np(" second", " seconds"));
184  KConfigGroup cg = config();
185 
186  ui.filechooser->setUrl(m_sourceFile);
187  ui.filechooser->setFilter(i18n("*.kvtml|Vocabulary Collections"));
188  connect(ui.filechooser, SIGNAL(urlSelected(const KUrl &)), this, SLOT(urlSelected(const KUrl &)));
189  ui.language1->addItems(m_languages);
190  ui.language2->addItems(m_languages);
191  ui.language1->setCurrentIndex(m_lang1);
192  ui.language2->setCurrentIndex(m_lang2);
193 
194  switch (m_solutionType) {
195  case Hover:
196  ui.solutionOnHover->setChecked(true);
197  break;
198  case Always:
199  ui.solutionAlways->setChecked(true);
200  break;
201  }
202 
203  connect(ui.fontSelectButton, SIGNAL(clicked()), parent, SLOT(settingsModified()));
204  connect(ui.solutionOnHover, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
205  connect(ui.solutionAlways, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
206  connect(ui.filechooser, SIGNAL(urlSelected(const KUrl &)), parent, SLOT(settingsModified()));
207  connect(ui.updateIntervalSpinBox, SIGNAL(valueChanged(int)), parent, SLOT(settingsModified()));
208 }
209 
210 void ParleyPlasma::urlSelected(const KUrl &)
211 {
212  // turn off old engine
213  m_engine->disconnectSource(m_sourceFile.url(), this);
214  // connect to new file
215  m_sourceFile = ui.filechooser->url();
216  m_engine->connectSource(m_sourceFile.url(), this, m_updateInterval);
217  // get language data
218  Plasma::DataEngine::Data data = m_engine->query(m_sourceFile.url());
219  m_languages = data.keys();
220  // update language selection
221  ui.language1->clear();
222  ui.language2->clear();
223  ui.language1->addItems(m_languages);
224  ui.language2->addItems(m_languages);
225  // just select the first languages - better than nothing ;)
226  ui.language1->setCurrentIndex(0);
227  ui.language2->setCurrentIndex(1);
228 }
229 
230 void ParleyPlasma::showFontSelectDlg()
231 {
232  KFontDialog::getFont(m_font);
233 }
234 
235 void ParleyPlasma::configAccepted()
236 {
237  prepareGeometryChange();
238 
239  KConfigGroup cg = config();
240  cg.writeEntry("font", m_font);
241  m_label1->setFont(m_font);
242  m_label2->setFont(m_font);
243  m_updateInterval = ui.updateIntervalSpinBox->value() * 1000;
244  cg.writeEntry("updateInterval", m_updateInterval);
245 
246  m_engine->disconnectSource(m_sourceFile.url(), this);
247  m_sourceFile = ui.filechooser->url();
248  cg.writeEntry("File Name", m_sourceFile);
249 
250  m_solutionType = Hover;
251  if (ui.solutionAlways->isChecked()) {
252  m_solutionType = Always;
253  m_label2->show();
254  }
255  cg.writeEntry("Solution", m_solutionType);
256 
257  m_lang1 = ui.language1->currentIndex();
258  m_lang2 = ui.language2->currentIndex();
259 
260  cg.writeEntry("Top Language", m_lang1);
261  cg.writeEntry("Bottom Language", m_lang2);
262 
263 
264  m_engine->connectSource(m_sourceFile.url(), this, m_updateInterval);
265 
266  kDebug() << "open:" << m_sourceFile;
267 
268  emit configNeedsSaving();
269 }
270 
271 void ParleyPlasma::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
272 {
273  Plasma::Applet::hoverEnterEvent(event);
274  m_label2->show();
275 }
276 
277 void ParleyPlasma::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
278 {
279  Plasma::Applet::hoverLeaveEvent(event);
280 }
281 
282 #include "parley_plasma.moc"
QGraphicsTextItem::setFont
void setFont(const QFont &font)
QTransform
QWidget
ParleyPlasma::constraintsEvent
void constraintsEvent(Plasma::Constraints constraints)
Definition: parley_plasma.cpp:88
ParleyPlasma::init
void init()
Definition: parley_plasma.cpp:47
QFont
QGraphicsTextItem::boundingRect
virtual QRectF boundingRect() const
ParleyPlasma::hoverLeaveEvent
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Definition: parley_plasma.cpp:277
parley_plasma.h
QGraphicsItem::hide
void hide()
ParleyPlasma::hoverEnterEvent
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Definition: parley_plasma.cpp:271
ParleyPlasma::ParleyPlasma
ParleyPlasma(QObject *parent, const QVariantList &args)
Definition: parley_plasma.cpp:26
ParleyPlasma::paintInterface
void paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
Definition: parley_plasma.cpp:160
QList::size
int size() const
QPointF
QList::value
T value(int i) const
ParleyPlasma::contentSizeHint
QSizeF contentSizeHint() const
Definition: parley_plasma.cpp:155
ParleyPlasma::urlSelected
void urlSelected(const KUrl &file)
Definition: parley_plasma.cpp:210
QRect
KConfigDialog
ParleyPlasma::showFontSelectDlg
void showFontSelectDlg()
Definition: parley_plasma.cpp:230
QObject
QGraphicsItem::setPos
void setPos(const QPointF &pos)
QPainter
QGraphicsTextItem
QString
ParleyPlasma::configAccepted
void configAccepted()
Definition: parley_plasma.cpp:235
QGraphicsSceneHoverEvent
ParleyPlasma::configChanged
void configChanged()
Definition: parley_plasma.cpp:66
QRectF::width
qreal width() const
ParleyPlasma::~ParleyPlasma
~ParleyPlasma()
Definition: parley_plasma.cpp:116
QSizeF
QGraphicsItem::setTransform
void setTransform(const QTransform &matrix, bool combine)
ParleyPlasma::setContentSize
void setContentSize(const QSizeF &size)
Definition: parley_plasma.cpp:150
QStyleOptionGraphicsItem
QGraphicsTextItem::setPlainText
void setPlainText(const QString &text)
QRectF::height
qreal height() const
ParleyPlasma::dataUpdated
void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data)
Definition: parley_plasma.cpp:120
QGraphicsItem::show
void show()
ParleyPlasma::createConfigurationInterface
void createConfigurationInterface(KConfigDialog *parent)
Definition: parley_plasma.cpp:171
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • 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