File indexing completed on 2024-12-22 04:17:35

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2011 Joshua Netterfield                               *
0004  *                   joshua.netterfield@gmail.com                          *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "lineedititem.h"
0014 
0015 #include <debug.h>
0016 
0017 #include <QDebug>
0018 #include <QGraphicsScene>
0019 #include <QGraphicsProxyWidget>
0020 #include <QLineEdit>
0021 
0022 namespace Kst {
0023 
0024 LineEditItem::LineEditItem(View *parent) : ViewItem(parent), _lineEdit(new QLineEdit),_proxy(new QGraphicsProxyWidget(this)) {
0025   setTypeName(tr("LineEdit", "scripting interface for editing lines"));
0026   setBrush(Qt::white);
0027   applyDialogDefaultsStroke();
0028   applyDialogDefaultsFill();
0029   applyDialogDefaultsLockPosToData();
0030   _lineEdit->setAutoFillBackground(0);
0031   QPalette pal=_lineEdit->palette();
0032   pal.setColor(_lineEdit->backgroundRole(),"white");
0033   _lineEdit->setPalette(pal);
0034   _lineEdit->setText("");
0035   _proxy->setWidget(_lineEdit);
0036   connect(_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(notifyScripts()));
0037 }
0038 
0039 
0040 LineEditItem::~LineEditItem() {
0041 }
0042 
0043 
0044 void LineEditItem::paint(QPainter *) {
0045     _proxy->setGeometry(rect());
0046 }
0047 
0048 
0049 void LineEditItem::save(QXmlStreamWriter &xml) {
0050   if (isVisible()) {
0051     xml.writeStartElement("lineEdit");
0052     xml.writeAttribute("text", _lineEdit->text());
0053     ViewItem::save(xml);
0054     xml.writeEndElement();
0055   }
0056 }
0057 
0058 void LineEditItem::setText(QString t) {
0059     _lineEdit->setText(t);
0060 }
0061 
0062 void LineEditItem::addSocket(QLocalSocket* s) {
0063     _sockets.push_back(s);
0064 }
0065 
0066 void LineEditItem::notifyScripts() {
0067     foreach(QLocalSocket*s,_sockets) {
0068         s->write(QString("valueSet:"+_lineEdit->text()).toLatin1());
0069         s->flush();
0070     }
0071 }
0072 
0073 LineEditItemFactory::LineEditItemFactory()
0074 : GraphicsFactory() {
0075   registerFactory("lineEdit", this);
0076 }
0077 
0078 
0079 LineEditItemFactory::~LineEditItemFactory() {
0080 }
0081 
0082 
0083 ViewItem* LineEditItemFactory::generateGraphics(QXmlStreamReader& xml, ObjectStore *store, View *view, ViewItem *parent) {
0084   LineEditItem *rc = 0;
0085   while (!xml.atEnd()) {
0086     bool validTag = true;
0087     if (xml.isStartElement()) {
0088       if (!rc && xml.name().toString() == "lineEdit") {
0089         Q_ASSERT(!rc);
0090         QXmlStreamAttributes attrs = xml.attributes();
0091         rc = new LineEditItem(view);
0092         if (parent) {
0093           rc->setParentViewItem(parent);
0094         }
0095         QStringRef av = attrs.value("text");
0096         if (!av.isNull()) {
0097           rc->_lineEdit->setText(av.toString());
0098         }
0099         // Add any new specialized LineEditItem Properties here.
0100       } else {
0101         Q_ASSERT(rc);
0102         if (!rc->parse(xml, validTag) && validTag) {
0103           ViewItem *i = GraphicsFactory::parse(xml, store, view, rc);
0104           if (!i) {
0105           }
0106         }
0107       }
0108     } else if (xml.isEndElement()) {
0109       if (xml.name().toString() == "lineEdit") {
0110         break;
0111       } else {
0112         validTag = false;
0113       }
0114     }
0115     if (!validTag) {
0116       qDebug("invalid Tag\n");
0117       Debug::self()->log(QObject::tr("Error creating lineEdit object from Kst file."), Debug::Warning);
0118       delete rc;
0119       return 0;
0120     }
0121     xml.readNext();
0122   }
0123   return rc;
0124 }
0125 
0126 }
0127 
0128 // vim: ts=2 sw=2 et