[9b49c1]: src / plugins / circuit / circuitscene.cpp  Maximize  Restore  History

Download this file

170 lines (142 with data), 5.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2009-2010 Julian B채ume <julian@svg4all.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "circuitscene.h"
#include "theme.h"
#include "ktlcircuitplugin.h"
#include "interfaces/component/componentmimedata.h"
#include "interfaces/component/connectoritem.h"
#include "componentitem.h"
#include <QGraphicsSceneDragDropEvent>
#include <KDebug>
#include "circuitmodel.h"
#include "pinitem.h"
#include <interfaces/component/connector.h>
using namespace KTechLab;
CircuitScene::CircuitScene ( QObject* parent, CircuitModel *model, KTLCircuitPlugin* plugin )
: IDocumentScene ( parent ),
m_model( model ),
m_theme( new Theme() ),
m_plugin( plugin )
{
// m_componentSize = QSizeF( cg.readEntry("componentWidth", "64").toInt(), cg.readEntry("componentHeight", "64").toInt() );
setupData();
connect(this,SIGNAL(transactionAborted()),model,SLOT(revert()));
connect(this,SIGNAL(transactionCompleted()),model,SLOT(submit()));
}
CircuitScene::~CircuitScene()
{
qDeleteAll( m_components.values() );
delete m_theme;
}
QString CircuitScene::circuitName() const
{
return m_circuitName;
}
void CircuitScene::dropEvent ( QGraphicsSceneDragDropEvent* event )
{
if (!event->mimeData()->hasFormat("ktechlab/x-icomponent")) {
kDebug() << "Dropped unknown data";
return;
}
const ComponentMimeData *mimeData = qobject_cast<const ComponentMimeData*>(event->mimeData());
ComponentItem* item =
new ComponentItem(
m_model->createComponent(mimeData,event->scenePos()),
m_theme
);
item->setVisible(true);
addItem( item );
m_components.insert(item->id(), item);
event->accept();
}
void CircuitScene::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
{
if (!event->mimeData()->hasFormat("ktechlab/x-icomponent")) {
event->ignore();
return;
}
event->accept();
}
void CircuitScene::setupData()
{
if (!m_model && !m_plugin)
return;
foreach (QVariant component, m_model->components())
{
if (component.canConvert(QVariant::Map)) {
ComponentItem* item = m_plugin->createComponentItem( component.toMap(), m_theme );
if (!item) {
kWarning() << "Couldn't create item";
continue;
}
addItem( item );
m_components.insert(item->id(), item);
}
}
foreach (QVariant pins, m_model->nodes()){
if (pins.canConvert(QVariant::Map)) {
QPointF p(pins.toMap().value("x").toDouble(),pins.toMap().value("y").toDouble());
QRectF rect(p, QSize(4,4));
PinItem* item = new PinItem(rect, 0, this);
item->setId(pins.toMap().value("id").toString());
m_pins.insert(item->id(),item);
}
}
foreach (QVariant connector, m_model->connectors())
{
if (connector.canConvert(QVariant::Map)) {
new ConnectorItem(connector.toMap(),this);
}
}
}
void CircuitScene::updateData( const QString& name, const QVariantMap& data )
{
//Plasma::DataEngine *docEngine = dataEngine( "ktechlabdocument" );
//kDebug() << "isContainment() ==" << isContainment();
/*
if ( data["mime"].toString().endsWith("circuit") ) {
const QStringList &itemSources = data["itemList"].toStringList();
foreach ( const QString &source, itemSources ) {
if ( !m_components.contains(source) ) {
ComponentApplet *component = new ComponentApplet( this, m_theme );
kDebug() << "before connecting";
//docEngine->connectSource( m_circuitName + "/" + source, component );
kDebug() << "after connecting";
//addApplet( component, position, false );
m_components.insert( source, component );
}
}
}*/
//kDebug() << "Difference between components and applets" << (m_components.size() - applets().size());
}
IComponentItem* CircuitScene::item(const QString& id) const
{
return m_components.value(id);
}
Node* CircuitScene::node(const QString& id) const
{
return m_pins.value(id);
}
void CircuitScene::setCircuitName ( const QString& name )
{
//if name didn't change, do nothing
if ( m_circuitName == name ) {
return;
}
m_circuitName = name;
setupData();
}
#include "circuitscene.moc"

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks