File indexing completed on 2024-05-12 16:39:46

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include <QDomDocument>
0021 
0022 #include "events.h"
0023 
0024 using namespace KFormDesigner;
0025 
0026 class Q_DECL_HIDDEN Connection::Private
0027 {
0028 public:
0029     Private(const QString &sender_, const QString &signal_, const QString &receiver_, const QString &slot_);
0030     Private()
0031     {
0032     }
0033 
0034     ~Private()
0035     {
0036     }
0037 
0038     QString sender;
0039     QString signal;
0040     QString receiver;
0041     QString slot;
0042 };
0043 
0044 Connection::Private::Private(const QString &sender_, const QString &signal_, const QString &receiver_, const QString &slot_)
0045     :sender(sender_), signal(signal_), receiver(receiver_), slot(slot_)
0046 
0047 {
0048 
0049 }
0050 
0051 Connection::Connection(const QString &sender, const QString &signal,
0052                        const QString &receiver, const QString &slot) : d(new Private(sender, signal, receiver, slot))
0053 {
0054 }
0055 
0056 Connection::Connection() : d(new Private())
0057 {
0058 }
0059 
0060 Connection::~Connection()
0061 {
0062     delete d;
0063 }
0064 
0065 QString Connection::sender() const
0066 {
0067     return d->sender;
0068 }
0069 
0070 QString Connection::receiver() const
0071 {
0072     return d->receiver;
0073 }
0074 
0075 QString Connection::signal() const
0076 {
0077     return d->signal;
0078 }
0079 
0080 QString Connection::slot() const
0081 {
0082     return d->slot;
0083 }
0084 
0085 void Connection::setSender(const QString &v)
0086 {
0087     d->sender = v;
0088 }
0089 
0090 void Connection::setReceiver(const QString &v)
0091 {
0092     d->receiver = v;
0093 }
0094 
0095 void Connection::setSignal(const QString &v)
0096 {
0097     d->signal = v;
0098 }
0099 
0100 void Connection::setSlot(const QString &v)
0101 {
0102     d->slot = v;
0103 }
0104 ///////////////////////////////////////
0105 
0106 ConnectionBuffer::ConnectionBuffer()
0107 {
0108 }
0109 
0110 ConnectionBuffer::~ConnectionBuffer()
0111 {
0112 }
0113 
0114 void
0115 ConnectionBuffer::fixName(const QString &oldName, const QString &newName)
0116 {
0117     foreach (Connection *c, *this) {
0118         if (c->sender() == oldName)
0119             c->setSender(newName);
0120         if (c->receiver() == oldName)
0121             c->setReceiver(newName);
0122     }
0123 }
0124 
0125 ConnectionBuffer*
0126 ConnectionBuffer::allConnectionsForWidget(const QString &widget)
0127 {
0128     ConnectionBuffer *list = new ConnectionBuffer();
0129     foreach (Connection *c, *this) {
0130         if ((c->sender() == widget) || (c->receiver() == widget))
0131             list->append(c);
0132     }
0133 
0134     return list;
0135 }
0136 
0137 void
0138 ConnectionBuffer::save(QDomNode &parentNode)
0139 {
0140     if (isEmpty())
0141         return;
0142 
0143     QDomDocument domDoc = parentNode.ownerDocument();
0144     QDomElement connections;
0145     if (!parentNode.firstChildElement("connections").isNull())
0146         connections = parentNode.firstChildElement("connections");
0147     else
0148         connections = domDoc.createElement("connections");
0149     parentNode.appendChild(connections);
0150 
0151     foreach (Connection *c, *this) {
0152         QDomElement connection = domDoc.createElement("connection");
0153         connection.setAttribute("language", "C++");
0154         connections.appendChild(connection);
0155 
0156         QDomElement sender = domDoc.createElement("sender");
0157         connection.appendChild(sender);
0158         QDomText senderText = domDoc.createTextNode(c->sender());
0159         sender.appendChild(senderText);
0160 
0161         QDomElement signal = domDoc.createElement("signal");
0162         connection.appendChild(signal);
0163         QDomText signalText = domDoc.createTextNode(c->signal());
0164         signal.appendChild(signalText);
0165 
0166         QDomElement receiver = domDoc.createElement("receiver");
0167         connection.appendChild(receiver);
0168         QDomText receiverText = domDoc.createTextNode(c->receiver());
0169         receiver.appendChild(receiverText);
0170 
0171         QDomElement slot = domDoc.createElement("slot");
0172         connection.appendChild(slot);
0173         QDomText slotText = domDoc.createTextNode(c->slot());
0174         slot.appendChild(slotText);
0175     }
0176 }
0177 
0178 void ConnectionBuffer::saveAllConnectionsForWidget(const QString &widget, QDomNode &parentNode)
0179 {
0180     ConnectionBuffer *buff = allConnectionsForWidget(widget);
0181     buff->save(parentNode);
0182     delete buff;
0183 }
0184 
0185 void ConnectionBuffer::load(const QDomNode &node)
0186 {
0187     for (QDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling()) {
0188         Connection *conn = new Connection();
0189         conn->setSender(n.firstChildElement("sender").text());
0190         conn->setSignal(n.firstChildElement("signal").text());
0191         conn->setReceiver(n.firstChildElement("receiver").text());
0192         conn->setSlot(n.firstChildElement("slot").text());
0193         append(conn);
0194     }
0195 }
0196 
0197 void
0198 ConnectionBuffer::removeAllConnectionsForWidget(const QString &widget)
0199 {
0200     ConnectionList toRemove;
0201     foreach (Connection *c, *this) {
0202         if ((c->sender() == widget) || (c->receiver() == widget)) {
0203             toRemove.append(c);
0204         }
0205     }
0206     foreach (Connection *c, toRemove) {
0207         removeAll(c);
0208     }
0209     qDeleteAll(toRemove);
0210 }