[7d8b45]: src / plugins / circuit / circuitscene.cpp Maximize Restore History

Download this file

circuitscene.cpp    270 lines (232 with data), 8.8 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
<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>
#include <interfaces/component/icomponentplugin.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()));
connect(this,SIGNAL(componentsMoved(QList<KTechLab::IComponentItem*>)),
this,SLOT(updateModel(QList<KTechLab::IComponentItem*>)));
connect(this,SIGNAL(routeCreated(KTechLab::ConnectorItem*)),
this,SLOT(addConnector(KTechLab::ConnectorItem*)));
connect(this,SIGNAL(routed(QList<KTechLab::ConnectorItem*>)),
this,SLOT(updateModel(QList<KTechLab::ConnectorItem*>)));
connect(this,SIGNAL(itemAdded(KTechLab::IDocumentItem*)),
this,SLOT(addItem(KTechLab::IDocumentItem*)));
connect(this,SIGNAL(itemRemoved(KTechLab::IDocumentItem*)),
this,SLOT(removeItem(KTechLab::IDocumentItem*)));
}
CircuitScene::~CircuitScene()
{
delete m_theme;
}
QString CircuitScene::circuitName() const
{
return m_circuitName;
}
QVariantMap CircuitScene::createItemData(const KTechLab::ComponentMimeData* data, const QPointF& pos) const
{
QVariantMap comp;
// type in the xml files is name in the component mimedata :/
comp.insert("type", data->name());
comp.insert("x", pos.x());
comp.insert("y", pos.y());
comp.insert("id", m_model->generateUid(data->name()));
return comp;
}
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());
const QVariantMap& itemData = createItemData(mimeData, event->scenePos());
m_model->addComponent(itemData);
ComponentItem* item = mimeData->factory()->createItem(itemData, m_theme);
item->setVisible(true);
addItem( item );
m_components.insert(item->id(), item);
item->setPos(alignToGrid(item->scenePos()));
QList<IComponentItem*> list;
list << item;
emit IDocumentScene::componentsMoved(list);
emit transactionCompleted();
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)) {
const QVariantMap& map(component.toMap());
IComponentItemFactory* f =
m_plugin->componentItemFactory( map.value("type").toString() );
ComponentItem* item = f->createItem( map, 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::updateModel(QList< IComponentItem* > components)
{
foreach(IDocumentItem* item, components){
updateModel(item);
}
}
void CircuitScene::updateModel(QList< ConnectorItem* > connectors)
{
foreach(IDocumentItem* item, connectors){
updateModel(item);
}
}
void CircuitScene::updateModel(IDocumentItem* item)
{
const QVariantMap& data = item->data();
m_model->updateData(data.value("id").toString(),data);
}
void CircuitScene::addConnector(ConnectorItem* item)
{
QVariantMap data = item->data();
if (!data.contains("id"))
data.insert("id", m_model->generateUid(((IDocumentItem*)item)->type()));
m_model->addConnector(data);
}
void CircuitScene::removeItem(IDocumentItem* item)
{
QModelIndex index = m_model->index(item->data());
if (!m_model->removeRow(index.row(),index.parent()))
kWarning() << "Could not remove item:" << item->data();
}
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());
}
void CircuitScene::rotateSelectedComponents(qreal angle)
{
QList<IComponentItem*> components;
QList<ConnectorItem*> connectors;
foreach(QGraphicsItem* item, selectedItems()){
IComponentItem* c = qgraphicsitem_cast<IComponentItem*>(item);
if (!c) continue;
components.append(c);
scheduleForRerouting(collidingItems(c));
c->setRotation(c->rotation()+angle);
}
emit componentsMoved(components);
performRerouting();
emit transactionCompleted();
}
void CircuitScene::flipSelectedComponents(Qt::Axis axis)
{
QList<IComponentItem*> components;
foreach(QGraphicsItem* item, selectedItems()){
IComponentItem* c = qgraphicsitem_cast<IComponentItem*>(item);
if (!c) continue;
components.append(c);
scheduleForRerouting(collidingItems(c));
QPointF center = c->boundingRect().center();
QTransform t;
int sx = (axis == Qt::XAxis) ? 1 : -1;
int sy = (axis == Qt::YAxis) ? 1 : -1;
t.translate(center.x(),center.y()).scale(sx,sy).translate(-center.x(),-center.y());
c->setTransform(t,true);
}
emit componentsMoved(components);
performRerouting();
emit transactionCompleted();
}
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"