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

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 "buttonitem.h"
0014 
0015 #include <debug.h>
0016 
0017 #include <QDebug>
0018 #include <QGraphicsScene>
0019 #include <QGraphicsProxyWidget>
0020 #include <QPushButton>
0021 
0022 namespace Kst {
0023 
0024 ButtonItem::ButtonItem(View *parent) : ViewItem(parent), _pushButton(new QPushButton),_proxy(new QGraphicsProxyWidget(this)) {
0025   setTypeName(tr("Button"));
0026   setBrush(Qt::white);
0027   applyDialogDefaultsStroke();
0028   applyDialogDefaultsFill();
0029   applyDialogDefaultsLockPosToData();
0030   _pushButton->setAutoFillBackground(0);
0031   QPalette pal=_pushButton->palette();
0032   pal.setColor(_pushButton->backgroundRole(),"white");
0033   _pushButton->setPalette(pal);
0034   _pushButton->setText("");
0035   _proxy->setWidget(_pushButton);
0036   connect(_pushButton,SIGNAL(clicked()),this,SLOT(notifyScripts()));
0037 }
0038 
0039 
0040 ButtonItem::~ButtonItem() {
0041 }
0042 
0043 
0044 void ButtonItem::paint(QPainter *) {
0045     _proxy->setGeometry(rect());
0046 }
0047 
0048 
0049 void ButtonItem::save(QXmlStreamWriter &xml) {
0050   if (isVisible()) {
0051     xml.writeStartElement("button");
0052     xml.writeAttribute("text", _pushButton->text());
0053     ViewItem::save(xml);
0054     xml.writeEndElement();
0055   }
0056 }
0057 
0058 void ButtonItem::setText(QString t) {
0059     _pushButton->setText(t);
0060 }
0061 
0062 void ButtonItem::addSocket(QLocalSocket* s) {
0063     _sockets.push_back(s);
0064 }
0065 
0066 void ButtonItem::notifyScripts() {
0067     foreach(QLocalSocket*s,_sockets) {
0068         s->write("clicked");
0069         s->flush();
0070     }
0071 }
0072 
0073 ButtonItemFactory::ButtonItemFactory()
0074 : GraphicsFactory() {
0075   registerFactory("button", this);
0076 }
0077 
0078 
0079 ButtonItemFactory::~ButtonItemFactory() {
0080 }
0081 
0082 
0083 ViewItem* ButtonItemFactory::generateGraphics(QXmlStreamReader& xml, ObjectStore *store, View *view, ViewItem *parent) {
0084     qDebug()<<"GG!";
0085   ButtonItem *rc = 0;
0086   while (!xml.atEnd()) {
0087     bool validTag = true;
0088     if (xml.isStartElement()) {
0089       if (!rc && xml.name().toString() == "button") {
0090         Q_ASSERT(!rc);
0091         QXmlStreamAttributes attrs = xml.attributes();
0092         rc = new ButtonItem(view);
0093         if (parent) {
0094           rc->setParentViewItem(parent);
0095         }
0096         QStringRef av = attrs.value("text");
0097         if (!av.isNull()) {
0098           rc->_pushButton->setText(av.toString());
0099         }
0100         // Add any new specialized ButtonItem Properties here.
0101       } else {
0102         Q_ASSERT(rc);
0103         if (!rc->parse(xml, validTag) && validTag) {
0104           ViewItem *i = GraphicsFactory::parse(xml, store, view, rc);
0105           if (!i) {
0106           }
0107         }
0108       }
0109     } else if (xml.isEndElement()) {
0110       if (xml.name().toString() == "button") {
0111         break;
0112       } else {
0113         validTag = false;
0114       }
0115     }
0116     if (!validTag) {
0117       qDebug("invalid Tag\n");
0118       Debug::self()->log(QObject::tr("Error creating button object from Kst file."), Debug::Warning);
0119       delete rc;
0120       return 0;
0121     }
0122     xml.readNext();
0123   }
0124   return rc;
0125 }
0126 
0127 }
0128 
0129 // vim: ts=2 sw=2 et