Skip to content
Snippets Groups Projects
Commit 5be843c8 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

Merge branch 'DesignerScene2'

parents a8f8af5d 331dd1eb
No related branches found
No related tags found
No related merge requests found
Showing
with 892 additions and 522 deletions
......@@ -48,3 +48,9 @@ ParameterizedItem *ItemFactory::createEmptyItem()
{
return new ParameterizedItem();
}
bool ItemFactory::isValidName(const QString &name)
{
return m_all_item_names.contains(name);
}
......@@ -33,6 +33,9 @@ public:
return m_all_item_names;
}
//! returns true if item with given name can be created
static bool isValidName(const QString &name);
private:
static QList<QString> m_all_item_names;
ItemFactory() {}
......
......@@ -21,6 +21,7 @@ MultiLayerItem::MultiLayerItem(ParameterizedItem *parent)
{
setProperty("Cross Correlation Length", 0.0);
m_valid_children.append(QString("Layer"));
m_valid_children.append(QString("MultiLayer"));
}
MultiLayerItem::~MultiLayerItem()
......
......@@ -355,6 +355,7 @@ ParameterizedItem *SessionModel::insertNewItem(QString model_type,
return 0;
}
ParameterizedItem *new_item = ItemFactory::createItem(model_type);
Q_ASSERT(new_item);
parent->insertChildItem(row, new_item);
return new_item;
}
......@@ -467,3 +468,36 @@ void SessionModel::writeProperty(QXmlStreamWriter *writer,
writer->writeEndElement(); // end ParameterTag
}
}
void SessionModel::moveParameterizedItem(ParameterizedItem *item, ParameterizedItem *new_parent, int row)
{
qDebug() << "";
qDebug() << "";
qDebug() << "SessionModel::moveParameterizedItem() " << item << new_parent << row;
if(!new_parent) new_parent = m_root_item;
if(item->parent() == new_parent && indexOfItem(item).row() == row) {
qDebug() << "SessionModel::moveParameterizedItem() -> no need to move, same parent, same raw. ";
return;
}
QByteArray xml_data;
QXmlStreamWriter writer(&xml_data);
writeItemAndChildItems(&writer, item);
QXmlStreamReader reader(xml_data);
if (row == -1) row = new_parent->childItemCount();
qDebug() << " >>> Beginning to insert indexOfItem(new_parent)" << indexOfItem(new_parent);
beginInsertRows(indexOfItem(new_parent), row, row);
readItems(&reader, new_parent, row);
endInsertRows();
qDebug() << " >>> Now deleting indexOfItem(item).row()" << indexOfItem(item).row();
removeRows(indexOfItem(item).row(), 1, indexOfItem(item->parent()));
}
......@@ -96,6 +96,8 @@ public:
void writeTo(QXmlStreamWriter *writer);
void moveParameterizedItem(ParameterizedItem *item, ParameterizedItem *new_parent = 0, int row = -1);
private:
ParameterizedItem *insertNewItem(QString model_type,
ParameterizedItem *parent,
......
......@@ -136,9 +136,16 @@ QList<QGraphicsItem *> ConnectableView::connectInputPort(ConnectableView *other)
foreach(QGraphicsItem *item, childItems()) {
NodeEditorPort *port = dynamic_cast<NodeEditorPort *>(item);
if (port && port->isInput() && !port->isConnected()) {
// if (port && port->isInput()) {
foreach(QGraphicsItem *other_item, other->childItems()) {
NodeEditorPort *other_port= dynamic_cast<NodeEditorPort *>(other_item);
if(other_port && port->getPortType() == other_port->getPortType()) {
// // deleting old connection
// if(port->isConnected()) port->deleteAllConnections();
NodeEditorConnection *conn = new NodeEditorConnection(0, scene());
conn->setPort2(port);
conn->setPort1(other_port);
......
......@@ -142,3 +142,21 @@ int DesignerHelper::nanometerToScreen(double nanometer)
return result;
}
QRectF DesignerHelper::getDefaultBoundingRect(const QString &name)
{
if (name==QString("MultiLayer")) {
return QRect(0, 0, getDefaultMultiLayerWidth(), getDefaultMultiLayerHeight());
} else if (name==QString("Layer")) {
return QRect(0, 0, getDefaultLayerWidth(), getDefaultLayerHeight());
} else if (name==QString("ParticleLayout")) {
return QRect(0, 0, getDefaultDecorationWidth(), getDefaultDecorationHeight());
} else {
return QRect();
}
}
......@@ -72,7 +72,10 @@ public:
//! to have reasonable graphics representation of layer in the form of QRect
static int nanometerToScreen(double nanometer);
private:
//! returns default bounding rectangle for given SampleView name
static QRectF getDefaultBoundingRect(const QString &name);
private:
static int m_default_layer_height;
static int m_default_layer_width;
};
......
......@@ -4,11 +4,17 @@
#include "SampleViewFactory.h"
#include "SampleViewAligner.h"
#include "IView.h"
#include "LayerView.h"
#include "ConnectableView.h"
#include "ItemFactory.h"
#include "ParameterizedGraphicsItem.h"
#include "NodeEditor.h"
#include "NodeEditorConnection.h"
#include "DesignerMimeData.h"
#include <QItemSelection>
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
DesignerScene2::DesignerScene2(QObject *parent)
......@@ -22,6 +28,7 @@ DesignerScene2::DesignerScene2(QObject *parent)
NodeEditor *nodeEditor = new NodeEditor(parent);
nodeEditor->install(this);
connect(nodeEditor, SIGNAL(connectionIsEstablished(NodeEditorConnection*)), this, SLOT(onEstablishedConnection(NodeEditorConnection*)));
connect(this, SIGNAL(selectionChanged()), this, SLOT(onSceneSelectionChanged()));
......@@ -36,19 +43,20 @@ void DesignerScene2::setSessionModel(SessionModel *model)
if(model != m_sessionModel) {
if(m_sessionModel) {
disconnect(m_sessionModel, SIGNAL(modelAboutToBeReset()), this, SLOT(resetScene()));
disconnect(m_sessionModel, SIGNAL(rowsInserted(QModelIndex, int,int)), this, SLOT(updateScene(QModelIndex, int,int)));
disconnect(m_sessionModel, SIGNAL(rowsRemoved(QModelIndex, int,int)), this, SLOT(updateScene(QModelIndex, int,int)));
disconnect(m_sessionModel, SIGNAL(modelReset()), this, SLOT(updateScene()));
// TODO disconnect all
}
m_sessionModel = model;
connect(m_sessionModel, SIGNAL(modelAboutToBeReset()), this, SLOT(resetScene()));
connect(m_sessionModel, SIGNAL(rowsInserted(QModelIndex, int,int)), this, SLOT(updateScene(QModelIndex, int,int)));
connect(m_sessionModel, SIGNAL(rowsRemoved(QModelIndex, int,int)), this, SLOT(updateScene(QModelIndex, int,int)));
connect(m_sessionModel, SIGNAL(modelReset()), this, SLOT(updateScene()));
connect(m_sessionModel, SIGNAL(rowsInserted(QModelIndex, int,int)), this, SLOT(onRowsInserted(QModelIndex, int,int)));
connect(m_sessionModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int,int)), this, SLOT(onRowsAboutToBeRemoved(QModelIndex,int,int)));
connect(m_sessionModel, SIGNAL(rowsRemoved(QModelIndex, int,int)), this, SLOT(onRowsRemoved(QModelIndex, int,int)));
connect(m_sessionModel, SIGNAL(modelReset()), this, SLOT(updateScene()));
resetScene();
updateScene();
......@@ -88,24 +96,33 @@ void DesignerScene2::resetScene()
}
void DesignerScene2::updateScene(const QModelIndex &parent, int first, int last)
void DesignerScene2::updateScene()
{
qDebug() << "DesignerScene2::updateScene()";
m_orderedViews.clear();
updateViews();
alignViews();
}
void DesignerScene2::onRowsInserted(const QModelIndex &parent, int first, int last)
{
Q_UNUSED(parent);
Q_UNUSED(first);
Q_UNUSED(last);
qDebug() << "DesignerScene2::updateScene(const QModelIndex &parent, int first, int lastB)";
qDebug() << "DesignerScene2::onRowsInserted()" << parent;
updateScene();
}
void DesignerScene2::updateScene()
void DesignerScene2::onRowsRemoved(const QModelIndex &parent, int first, int last)
{
qDebug() << "DesignerScene2::updateScene()";
m_orderedViews.clear();
updateViews();
alignViews();
Q_UNUSED(parent);
Q_UNUSED(first);
Q_UNUSED(last);
qDebug() << "DesignerScene2::onRowsRemoved()" << parent;
updateScene();
}
......@@ -115,10 +132,8 @@ void DesignerScene2::onRowsAboutToBeRemoved(const QModelIndex &parent, int first
qDebug() << "DesignerScene2::onRowsAboutToBeRemoved()" << parent << first << last;
for(int irow = first; irow<=last; ++irow ) {
QModelIndex itemIndex = m_sessionModel->index(irow, 0, parent);
deleteViews(itemIndex);
removeItemFromScene(m_sessionModel->itemForIndex(itemIndex));
// ParameterizedItem *item = m_sessionModel->itemForIndex(itemIndex);
// removeItemFromScene(item);
deleteViews(itemIndex); // deleting all child items
removeItemFromScene(m_sessionModel->itemForIndex(itemIndex)); // deleting parent item
}
m_block_selection = false;
}
......@@ -126,12 +141,11 @@ void DesignerScene2::onRowsAboutToBeRemoved(const QModelIndex &parent, int first
void DesignerScene2::onSessionSelectionChanged(const QItemSelection &selected, const QItemSelection & /* deselected */)
{
qDebug() << "DesignerScene2::onSessionSelectionChanged()";
//qDebug() << "DesignerScene2::onSessionSelectionChanged()";
QModelIndexList indices = selected.indexes();
if(indices.size()) {
ParameterizedGraphicsItem *item = static_cast<ParameterizedGraphicsItem *>(
indices.back().internalPointer());
ParameterizedItem *item = m_sessionModel->itemForIndex(indices.back());
Q_ASSERT(item);
IView *view = m_ItemToView[item];
//Q_ASSERT(view);
......@@ -147,11 +161,11 @@ void DesignerScene2::onSessionSelectionChanged(const QItemSelection &selected, c
}
void DesignerScene2::onSceneSelectionChanged()
{
qDebug() << "DesignerScene2::onSceneSelectionChanged() 1.1";
//qDebug() << "DesignerScene2::onSceneSelectionChanged() 1.1";
if(m_block_selection) return;
qDebug() << "DesignerScene2::onSceneSelectionChanged() 1.2";
m_selectionModel->clearSelection();
......@@ -159,7 +173,7 @@ void DesignerScene2::onSceneSelectionChanged()
for(int i=0; i<selected.size(); ++i) {
IView *view = dynamic_cast<IView *>(selected[i]);
if(view) {
ParameterizedItem *sessionItem = view->getSessionItem();
ParameterizedItem *sessionItem = view->getParameterizedItem();
QModelIndex itemIndex = m_sessionModel->indexOfItem(sessionItem);
Q_ASSERT(itemIndex.isValid());
m_selectionModel->select(itemIndex, QItemSelectionModel::Select);
......@@ -169,13 +183,11 @@ void DesignerScene2::onSceneSelectionChanged()
}
void DesignerScene2::updateViews(const QModelIndex & parentIndex, IView *parentView)
{
Q_ASSERT(m_sessionModel);
qDebug() << "DesignerScene2::update()";
qDebug() << "DesignerScene2::updateVIews()";
if(!parentIndex.isValid()) {
qDebug() << "Dumping model";
......@@ -189,7 +201,7 @@ void DesignerScene2::updateViews(const QModelIndex & parentIndex, IView *parentV
childView = addViewForItem(item);
m_orderedViews.push_back(childView);
if(parentView) parentView->addView(childView);
if(parentView) parentView->addView(childView, i_row);
} else {
qDebug() << "not a parameterized graphics item";
......@@ -197,7 +209,6 @@ void DesignerScene2::updateViews(const QModelIndex & parentIndex, IView *parentV
updateViews( itemIndex, childView);
}
}
......@@ -212,7 +223,7 @@ IView *DesignerScene2::addViewForItem(ParameterizedItem *item)
view = SampleViewFactory::createSampleView(item->modelType());
if(view) {
m_ItemToView[item] = view;
view->setSessionItem(item);
view->setParameterizedItem(item);
addItem(view);
return view;
}
......@@ -261,7 +272,8 @@ void DesignerScene2::removeItemFromScene(ParameterizedItem *item)
IView *view = it.value();
view->setSelected(false);
m_ItemToView.erase(it);
delete view;
emit view->aboutToBeDeleted();
view->deleteLater();
update();
break;
}
......@@ -269,4 +281,160 @@ void DesignerScene2::removeItemFromScene(ParameterizedItem *item)
}
void DesignerScene2::deleteSelectedItems()
{
qDebug() << "DesignerScene::deleteSelectedItems()" << selectedItems().size();
// FIXME handle multiple selection
foreach(QGraphicsItem *graphicsItem, selectedItems()) {
IView *view = dynamic_cast<IView *>(graphicsItem);
if(view) {
qDebug() << "xxx";
ParameterizedItem *item = view->getParameterizedItem();
Q_ASSERT(item);
m_sessionModel->removeRows(m_sessionModel->indexOfItem(item).row(), 1, m_sessionModel->indexOfItem(item->parent()));
}
NodeEditorConnection *connection = dynamic_cast<NodeEditorConnection *>(graphicsItem);
if(connection) removeConnection(connection);
}
}
void DesignerScene2::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// if(event->button()==Qt::LeftButton) {
// qDebug() << "DesignerScene2::mouseMoveEvent()";
// }
// LayerView2 *layer = qgraphicsitem_cast<LayerView2 *>(mouseGrabberItem());
// if(layer) {
// qDebug() << "DesignerScene2::mouseMoveEvent()";
// foreach(QGraphicsItem *item, items()) {
// if(item->type() == DesignerHelper::MultiLayerType) {
// MultiLayerView2 *multilayer = qgraphicsitem_cast<MultiLayerView2 *>(item);
// if(multilayer->mapRectToScene(multilayer->boundingRect()).intersects(layerRect)) {
// }
QGraphicsScene::mouseMoveEvent(event);
}
void DesignerScene2::drawForeground(QPainter* painter, const QRectF& rect)
{
Q_UNUSED(rect);
// QRectF SceneRect = this->sceneRect();
LayerView *layer = qgraphicsitem_cast<LayerView *>(mouseGrabberItem());
if(!m_layer_drop_area.isNull() && layer) {
//qDebug() << "DesignerScene2::drawForeground" << m_layer_drop_area;
painter->setPen(QPen(Qt::darkBlue, 2, Qt::DashLine));
painter->drawLine(m_layer_drop_area.left()-10, m_layer_drop_area.center().y(), m_layer_drop_area.right()+10, m_layer_drop_area.center().y());
//painter->drawRect(m_layer_drop_area);
invalidate();
}
}
void DesignerScene2::onEstablishedConnection(NodeEditorConnection *connection)
{
qDebug() << "DesignerScene2::onEstablishedConnection()";
IView *parentView = dynamic_cast<IView *>(connection->getInputPort()->parentItem());
IView *childView = dynamic_cast<IView *>(connection->getOutputPort()->parentItem());
Q_ASSERT(parentView);
Q_ASSERT(childView);
delete connection; // deleting just created connection because it will be recreated from the model
m_sessionModel->moveParameterizedItem(childView->getParameterizedItem(), parentView->getParameterizedItem());
}
void DesignerScene2::removeConnection(NodeEditorConnection *connection)
{
IView *childView = dynamic_cast<IView *>(connection->getOutputPort()->parentItem());
m_sessionModel->moveParameterizedItem(childView->getParameterizedItem(), 0);
}
void DesignerScene2::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "DesignerScene2::dragEnterEvent()";
return QGraphicsScene::dragEnterEvent(event);
}
void DesignerScene2::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << "DesignerScene2::dragMoveEvent()";
const DesignerMimeData *mimeData = checkDragEvent(event);
if(mimeData) {
// Layer can be droped only on MultiLayer
if(mimeData->getClassName() == QString("Layer")) {
QGraphicsScene::dragMoveEvent(event);
}
}
}
//void DesignerScene2::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
//{
// qDebug() << "DesignerScene2::dragLeaveEvent()";
// return QGraphicsScene::dragLeaveEvent(event);
//}
void DesignerScene2::dropEvent(QGraphicsSceneDragDropEvent *event)
{
const DesignerMimeData *mimeData = checkDragEvent(event);
qDebug() << "DesignerScene2::dropEvent()" << mimeData;
if (mimeData) {
// layer can be dropped on MultiLayer only
if(mimeData->getClassName() == "Layer") {
qDebug() << "DesignerScene2::dropEvent() dont want to drop" << mimeData;
QGraphicsScene::dropEvent(event);
} else {
qDebug() << "DesignerScene2::dropEvent() -> about to drop";
if(SampleViewFactory::isValidName(mimeData->getClassName())) {
ParameterizedItem *new_item = m_sessionModel->insertNewItem(mimeData->getClassName());
// propagating drop coordinates to ParameterizedItem
QRectF boundingRect = DesignerHelper::getDefaultBoundingRect(mimeData->getClassName());
new_item->setProperty("xpos", event->scenePos().x()-boundingRect.width()/2);
new_item->setProperty("ypos", event->scenePos().y()-boundingRect.height()/2);
}
}
}
// return QGraphicsScene::dropEvent(event);
}
const DesignerMimeData *DesignerScene2::checkDragEvent(QGraphicsSceneDragDropEvent * event)
{
//qDebug() << "DesignerScene2::checkDragEvent -> ";
const DesignerMimeData *mimeData = qobject_cast<const DesignerMimeData *>(event->mimeData());
if (!mimeData) {
event->ignore();
return 0;
}
if(mimeData->hasFormat("bornagain/widget") ) {
//qDebug() << "DesignerScene::checkDragEvent -> yes";
event->setAccepted(true);
} else {
event->setAccepted(false);
}
return mimeData;
}
......@@ -12,6 +12,8 @@ class ParameterizedGraphicsItem;
class QItemSelectionModel;
class IView;
class QItemSelection;
class NodeEditorConnection;
class DesignerMimeData;
class DesignerScene2 : public DesignerSceneInterface
......@@ -25,13 +27,35 @@ public:
void setSessionModel(SessionModel *model);
void setSelectionModel(QItemSelectionModel *model);
SessionModel *getSessionModel() { return m_sessionModel; }
public slots:
void onSceneSelectionChanged();
void onSessionSelectionChanged(const QItemSelection &, const QItemSelection &);
void resetScene();
void updateScene();
void updateScene(const QModelIndex &parent, int first, int last);
void onRowsInserted(const QModelIndex &parent, int first, int last);
void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
void onRowsRemoved(const QModelIndex &parent, int first, int last);
void setLayerDropArea(const QRectF &rect) { m_layer_drop_area = rect; }
void deleteSelectedItems();
void onEstablishedConnection(NodeEditorConnection *); // to process signals from NodeEditor
void removeConnection(NodeEditorConnection *);
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
//void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void drawForeground(QPainter* painter, const QRectF& rect);
const DesignerMimeData *checkDragEvent(QGraphicsSceneDragDropEvent * event);
private:
IView *addViewForItem(ParameterizedItem *item);
......@@ -46,6 +70,8 @@ private:
QMap<ParameterizedItem *, IView *> m_ItemToView;
QList<IView *> m_orderedViews;
QRectF m_layer_drop_area;
};
......
#include "DesignerView.h"
#include "DesignerMimeData.h"
#include "DesignerScene2.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QVBoxLayout>
......@@ -84,14 +85,11 @@ void DesignerView::clearAll()
}
void DesignerView::deleteItem()
void DesignerView::deleteSelectedItems()
{
QList<QGraphicsItem*> selected = m_graphicsView->scene()->selectedItems();
for(int i=0; i<selected.size(); ++i) {
m_graphicsView->scene()->removeItem(selected[i]);
delete selected[i];
}
m_graphicsView->scene()->update();
DesignerScene2 *designerScene = dynamic_cast<DesignerScene2 *>(m_graphicsView->scene());
Q_ASSERT(designerScene);
designerScene->deleteSelectedItems();
}
......@@ -103,10 +101,10 @@ void DesignerView::keyPressEvent(QKeyEvent *event)
case Qt::Key_Space:
break;
case Qt::Key_Delete:
deleteItem();
deleteSelectedItems();
break;
case Qt::Key_Backspace:
deleteItem();
deleteSelectedItems();
break;
default:
QWidget::keyPressEvent(event);
......
......@@ -26,9 +26,10 @@ public slots:
void zoomIn();
void zoomOut();
void zoomFit();
void deleteItem();
void deleteSelectedItems();
void clearAll();
protected:
// void wheelEvent(QWheelEvent *event);
void scaleView(qreal scaleFactor);
......
......@@ -85,22 +85,22 @@ void ISampleToIView::visit(const ParticleLayout *sample)
}
void ISampleToIView::visit(const Layer *sample)
void ISampleToIView::visit(const Layer *)
{
LayerView *layerView = new LayerView();
layerView->setThickness(sample->getThickness());
layerView->setName(sample->getName().c_str());
m_multiLayer->addBottomLayer(layerView);
m_sample_to_view[sample] = layerView;
// LayerView *layerView = new LayerView();
// layerView->setThickness(sample->getThickness());
// layerView->setName(sample->getName().c_str());
// m_multiLayer->addBottomLayer(layerView);
// m_sample_to_view[sample] = layerView;
const ILayout *decoration = sample->getLayout();
if(decoration) {
decoration->accept(this);
// const ILayout *decoration = sample->getLayout();
// if(decoration) {
// decoration->accept(this);
Q_ASSERT(m_sample_to_view[sample]);
Q_ASSERT(m_sample_to_view[decoration]);
m_connections += m_sample_to_view[sample]->connectInputPort(m_sample_to_view[decoration]);
}
// Q_ASSERT(m_sample_to_view[sample]);
// Q_ASSERT(m_sample_to_view[decoration]);
// m_connections += m_sample_to_view[sample]->connectInputPort(m_sample_to_view[decoration]);
// }
}
......@@ -111,24 +111,24 @@ void ISampleToIView::visit(const LayerInterface *)
}
void ISampleToIView::visit(const MultiLayer *sample)
void ISampleToIView::visit(const MultiLayer *)
{
Q_ASSERT(sample);
// Q_ASSERT(sample);
m_multiLayer = MultiLayerView::createTopMultiLayer();
// m_multiLayer = MultiLayerView::createTopMultiLayer();
// goForward();
for(size_t i_layer=0; i_layer < sample->getNumberOfLayers(); ++i_layer) {
const Layer *layer = sample->getLayer(i_layer);
layer->accept(this);
if(i_layer < sample->getNumberOfInterfaces()) {
const LayerInterface *interface = sample->getLayerInterface(i_layer);
interface->accept(this);
}
}
// goBack();
// // goForward();
// for(size_t i_layer=0; i_layer < sample->getNumberOfLayers(); ++i_layer) {
// const Layer *layer = sample->getLayer(i_layer);
// layer->accept(this);
// if(i_layer < sample->getNumberOfInterfaces()) {
// const LayerInterface *interface = sample->getLayerInterface(i_layer);
// interface->accept(this);
// }
// }
// // goBack();
m_sample_to_view[sample] = m_multiLayer;
// m_sample_to_view[sample] = m_multiLayer;
}
......
......@@ -4,8 +4,6 @@
#include <QDebug>
IView::IView(QGraphicsItem *parent)
: QGraphicsObject(parent), m_item(0)
{
......@@ -14,7 +12,7 @@ IView::IView(QGraphicsItem *parent)
}
void IView::setSessionItem(ParameterizedItem *item)
void IView::setParameterizedItem(ParameterizedItem *item)
{
if(item) {
m_item = item;
......@@ -27,9 +25,9 @@ void IView::setSessionItem(ParameterizedItem *item)
}
void IView::addView(IView *childView)
void IView::addView(IView *childView, int row)
{
qDebug() << "IView::addView() " << m_item->itemName() << childView->getSessionItem()->itemName();
qDebug() << "IView::addView() " << m_item->itemName() << childView->getParameterizedItem()->itemName() << " row:" << row;
}
......
......@@ -22,11 +22,14 @@ public:
int type() const { return Type; }
virtual void setSessionItem(ParameterizedItem *item);
virtual void setParameterizedItem(ParameterizedItem *item);
virtual ParameterizedItem *getSessionItem() { return m_item; }
virtual ParameterizedItem *getParameterizedItem() { return m_item; }
virtual void addView(IView *childView);
virtual void addView(IView *childView, int row = 0);
signals:
void aboutToBeDeleted();
public slots:
virtual void onChangedX();
......@@ -35,7 +38,6 @@ public slots:
protected:
ParameterizedItem *m_item;
};
......
......@@ -65,21 +65,21 @@ void IViewToISample::visit(MultiLayerView *view)
}
void IViewToISample::visit(LayerView *view)
void IViewToISample::visit(LayerView *)
{
Q_ASSERT(view);
std::cout << get_indent() << "ViewVisitor(LayerView ) " << m_level << " " << view->type() << " " << view->getName().toStdString() << std::endl;
//m_views.insertMulti(m_level, view);
// Q_ASSERT(view);
// std::cout << get_indent() << "ViewVisitor(LayerView ) " << m_level << " " << view->type() << " " << view->getName().toStdString() << std::endl;
// //m_views.insertMulti(m_level, view);
QList<ConnectableView *> connections = view->getConnectedInputItems();
// QList<ConnectableView *> connections = view->getConnectedInputItems();
// in the absence of connections it is simple Layer
if(connections.empty()) {
m_view_to_sample[view] = view->getLayer()->clone();
// // in the absence of connections it is simple Layer
// if(connections.empty()) {
// m_view_to_sample[view] = view->getLayer()->clone();
// with connections it is LayerDecorator
} else {
// // with connections it is LayerDecorator
// } else {
// goForward();
// foreach(ConnectableView *item, connections) {
// item->accept(this);
......@@ -92,7 +92,7 @@ void IViewToISample::visit(LayerView *view)
// }
// goBack();
}
// }
}
......
......@@ -2,27 +2,23 @@
#include "Units.h"
#include "ParticleLayoutView.h"
#include "ParameterizedItem.h"
#include "MultiLayerView.h"
#include "DesignerScene2.h"
#include "SessionModel.h"
#include "GUIHelpers.h"
#include "DesignerHelper.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QDrag>
#include <QCursor>
#include <QApplication>
#include <QMimeData>
#include <QBitmap>
#include <QWidget>
#include <QGradient>
#include <QStyleOptionGraphicsItem>
#include <QDebug>
#include "DesignerHelper.h"
#include <iostream>
#include <QGraphicsSceneMouseEvent>
LayerView2::LayerView2(QGraphicsItem *parent)
LayerView::LayerView(QGraphicsItem *parent)
: ConnectableView(parent)
, m_requested_parent(0)
, m_requested_row(-1)
{
setColor(QColor(qrand() % 256, qrand() % 256, qrand() % 256) );
setRectangle(QRect(0, 0, DesignerHelper::getDefaultLayerWidth(), DesignerHelper::getDefaultLayerHeight()));
......@@ -37,7 +33,7 @@ LayerView2::LayerView2(QGraphicsItem *parent)
}
void LayerView2::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
void LayerView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
painter->setPen(Qt::black);
......@@ -49,16 +45,17 @@ void LayerView2::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
}
void LayerView2::addView(IView *childView)
void LayerView::addView(IView *childView, int row)
{
qDebug() << "LayerView2::addView() " << m_item->itemName() << childView->getSessionItem()->itemName();
Q_UNUSED(row);
qDebug() << "LayerView::addView() " << m_item->itemName() << childView->getParameterizedItem()->itemName();
ParticleLayoutView *layout = dynamic_cast<ParticleLayoutView *>(childView);
Q_ASSERT(layout);
connectInputPort(layout);
}
void LayerView2::onPropertyChange(QString propertyName)
void LayerView::onPropertyChange(QString propertyName)
{
Q_ASSERT(m_item);
if(propertyName == "Thickness") {
......@@ -71,71 +68,99 @@ void LayerView2::onPropertyChange(QString propertyName)
}
// ----------------------------------------------------------------------------
QVariant LayerView::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
m_requested_parent = 0;
m_requested_row = -1;
QRectF layerRect = mapRectToScene(boundingRect());
foreach(QGraphicsItem *item, scene()->items()) {
if(item->type() == DesignerHelper::MultiLayerType) {
MultiLayerView *multilayer = qgraphicsitem_cast<MultiLayerView *>(item);
if(multilayer->mapRectToScene(multilayer->boundingRect()).intersects(layerRect)) {
//qDebug() << " XXX " << multilayer->getDropArea(multilayer->mapFromScene(layerRect.center()));
m_requested_parent = multilayer;
m_requested_row = multilayer->getDropArea(multilayer->mapFromScene(layerRect.center()));
break;
}
}
}
if(m_requested_parent) {
DesignerScene2 *designerScene = dynamic_cast<DesignerScene2 *>(scene());
QRectF rect = m_requested_parent->getDropAreaRectangle(m_requested_row);
designerScene->setLayerDropArea(m_requested_parent->mapRectToScene(rect));
}
}
return QGraphicsItem::itemChange(change, value);
}
LayerView::LayerView(QGraphicsItem *parent)
: ConnectableView(parent)
, m_fixed_xpos(0)
, m_fixed(false)
, m_layer(new Layer())
// , m_thickness(10*Units::nanometer)
void LayerView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//setColor(QColor(qrand() % 256, qrand() % 256, qrand() % 256) );
setRectangle(QRect(0, 0, DesignerHelper::getDefaultLayerWidth(), DesignerHelper::getDefaultLayerHeight()));
setName(QString("Layer"));
setToolTip(QString("%1\n%2").arg("LayerView").arg("Homogeneous layer"));
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
setAcceptDrops(false);
addPort(" ", NodeEditorPort::Input, NodeEditorPort::ParticleFactory);
setMaterialProperty(MaterialBrowser::getDefaultMaterialProperty());
if(event->button() == Qt::LeftButton) {
m_drag_start_position = pos();
}
QGraphicsItem::mousePressEvent(event);
}
void LayerView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
void LayerView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(widget);
painter->setPen(Qt::black);
if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus)) {
painter->setPen(Qt::DashLine);
//qDebug() << "LayerView::mouseReleaseEvent()" << parentItem() << m_requested_parent << m_requested_row;
DesignerScene2 *designerScene = dynamic_cast<DesignerScene2 *>(scene());
designerScene->setLayerDropArea(QRectF());
// Simple move of lonely layer across the scene: let it be.
if(m_requested_parent == 0 && parentItem() == 0) {
QGraphicsItem::mouseReleaseEvent(event);
return;
}
painter->setBrush(DesignerHelper::getLayerGradient(m_color, getRectangle() ) );
painter->drawRect(getRectangle());
}
Q_ASSERT(designerScene);
SessionModel *model = designerScene->getSessionModel();
void LayerView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// remove selection from child items
QList<QGraphicsItem *> list = childItems();
foreach(QGraphicsItem *item, list) {
item->setSelected(false);
// Layer was moved on top of MultiLayer but not in the right drop area:
// returning layer back to starting position.
if(m_requested_parent && m_requested_row == -1) {
//qDebug() << "1.1 Layer->MultiLayer, wrong drop area.";
setPos(m_drag_start_position);
QGraphicsItem::mouseReleaseEvent(event);
return;
}
QGraphicsObject::mousePressEvent(event);
}
// Layer was moved to the wrong row of his own MultiLayer: returning back.
if(m_requested_parent == parentItem() && m_requested_row == model->indexOfItem(getParameterizedItem()).row()) {
//Debug() << "1.2 Layer->MultiLayer (same), same drop area";
setPos(m_drag_start_position);
QGraphicsItem::mouseReleaseEvent(event);
return;
}
void LayerView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
std::cout << "LayerView::mouseReleaseEvent -> " << x() << " " << y() << std::endl;
emit LayerMoved();
QGraphicsObject::mouseReleaseEvent(event);
// setCursor(Qt::ArrowCursor);
}
// Layer was moved from MultiLayer he belong's to, to the empty place of
// the scene: changing ownership.
if(parentItem() && !m_requested_parent) {
//qDebug() << "1.3 Layer->Scene";
setPos( mapToScene(event->pos()) - event->pos());
model->moveParameterizedItem(this->getParameterizedItem(), 0);
QGraphicsItem::mouseReleaseEvent(event);
return;
}
// Layer was moved either from one MultiLayer to another, or is moved inside
// one multilayer: changing ownership.
if(m_requested_parent) {
//qDebug() << "1.4 MultiLayer->MultiLayer";
model->moveParameterizedItem(this->getParameterizedItem(), m_requested_parent->getParameterizedItem(), m_requested_row);
QGraphicsItem::mouseReleaseEvent(event);
return;
}
// should not be here
throw GUIHelpers::Error(tr("LayerView::mouseReleaseEvent() -> Loggic error."));
}
// layers are not allowed to move horizontally
QVariant LayerView::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene() && m_fixed) {
QPointF newPos = value.toPointF();
newPos.setX(m_fixed_xpos);
return newPos;
}
return QGraphicsItem::itemChange(change, value);
}
......@@ -2,104 +2,41 @@
#define LAYERVIEW_H
#include "ConnectableView.h"
#include "MaterialBrowser.h"
#include "Layer.h"
class MultiLayerView;
class LayerView2 : public ConnectableView
{
Q_OBJECT
public:
enum { Type = DesignerHelper::LayerType };
LayerView2(QGraphicsItem *parent = 0);
~LayerView2() { }
int type() const { return Type; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void addView(IView *childView);
public slots:
void onPropertyChange(QString propertyName);
};
//! graphics representation of Layer
class LayerView : public ConnectableView
{
Q_OBJECT
Q_PROPERTY(QString name READ getName WRITE setName )
Q_PROPERTY(double thickness READ getThickness WRITE setThickness )
Q_PROPERTY(MaterialProperty material READ getMaterialProperty WRITE setMaterialProperty )
public:
enum { Type = DesignerHelper::LayerType };
LayerView(QGraphicsItem *parent = 0);
~LayerView() { delete m_layer; }
//! сalls the ISampleViewVisitor's visit method
virtual void accept(IViewVisitor *visitor) { visitor->visit(this); }
~LayerView() { }
int type() const { return Type; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void setFixedX() { m_fixed_xpos = x(); m_fixed=true; }
qreal getFixedX() { return m_fixed_xpos; }
double getThickness() const { return m_layer->getThickness(); }
MaterialProperty getMaterialProperty() const { return m_materialProperty; }
void setLayer(Layer *layer) { delete m_layer; m_layer = layer; }
const Layer *getLayer() { return m_layer; }
public slots:
void setThickness(double thickness);
void setMaterialProperty(const MaterialProperty &materialProperty);
signals:
void LayerMoved();
void addView(IView *childView, int row = 0); // to add ParticleLayout
protected:
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
qreal m_fixed_xpos;
bool m_fixed;
public slots:
void onPropertyChange(QString propertyName);
MaterialProperty m_materialProperty;
Layer *m_layer;
private:
MultiLayerView * m_requested_parent;
//! Possible parent (MultiLayer) encountered during the movement of the Layer across the scene.
int m_requested_row;
//! possible row to drop the layer in encountered MultiLayer
QPointF m_drag_start_position;
};
inline void LayerView::setThickness(double thickness)
{
if(thickness != m_layer->getThickness()) {
m_layer->setThickness(thickness);
m_rect.setHeight(DesignerHelper::nanometerToScreen(getThickness()));
setPortCoordinates();
emit heightChanged();
}
}
inline void LayerView::setMaterialProperty(const MaterialProperty &materialProperty)
{
m_materialProperty = materialProperty;
setColor(materialProperty.getColor());
update();
}
#endif // LAYERVIEW_H
......@@ -12,102 +12,113 @@
class LayerView;
class LayerView2;
class DesignerMimeData;
class MultiLayerView2 : public ConnectableView
class MultiLayerView : public ConnectableView
{
Q_OBJECT
public:
enum { Type = DesignerHelper::MultiLayerType };
MultiLayerView2(QGraphicsItem *parent = 0);
MultiLayerView(QGraphicsItem *parent = 0);
int type() const { return Type; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void addView(IView *childView);
void addView(IView *childView, int row = 0);
virtual void addLayer(LayerView *layer, QPointF pos=QPointF());
virtual void addNewLayer(LayerView *layer, int row);
virtual void removeLayer(LayerView *layer);
bool isInDropArea(QPointF pos);
int getDropArea(QPointF pos);
virtual void addLayer(LayerView2 *layer, QPointF pos=QPointF());
QRectF getDropAreaRectangle(int row);
public slots:
void updateHeight();
void onLayerAboutToBeDeleted();
};
protected:
void dropEvent(QGraphicsSceneDragDropEvent *event);
const DesignerMimeData *checkDragEvent(QGraphicsSceneDragDropEvent * event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
private:
QList<LayerView *> m_layers;
QList<QRectF> m_drop_areas;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
};
#include "LayerView.h"
class MultiLayerView : public ConnectableView
{
Q_OBJECT
public:
enum { Type = DesignerHelper::MultiLayerType };
//class MultiLayerView : public ConnectableView
//{
// Q_OBJECT
MultiLayerView(QGraphicsItem *parent = 0);
//public:
// enum { Type = DesignerHelper::MultiLayerType };
int type() const { return Type; }
// MultiLayerView(QGraphicsItem *parent = 0);
//! create top MultiLayer, which will be the dock for all Layer's and MultiLayer's
static MultiLayerView *createTopMultiLayer();
// int type() const { return Type; }
//! сalls the ISampleViewVisitor's visit method
virtual void accept(IViewVisitor *visitor) { visitor->visit(this); }
// //! create top MultiLayer, which will be the dock for all Layer's and MultiLayer's
// static MultiLayerView *createTopMultiLayer();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
// //! сalls the ISampleViewVisitor's visit method
// virtual void accept(IViewVisitor *visitor) { visitor->visit(this); }
void addLayer(LayerView *layer, QPointF pos=QPointF());
void addBottomLayer(LayerView *layer);
void addMultiLayer(MultiLayerView *layer, QPointF pos=QPointF());
// void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
//! allows droping of object of given type
void allowDropType(const QString &name);
// void addLayer(LayerView *layer, QPointF pos=QPointF());
// void addBottomLayer(LayerView *layer);
// void addMultiLayer(MultiLayerView *layer, QPointF pos=QPointF());
void setFixedX() { m_fixed_xpos = x(); m_fixed=true; }
qreal getFixedX() { return m_fixed_xpos; }
// //! allows droping of object of given type
// void allowDropType(const QString &name);
public slots:
void updateHeight();
// void setFixedX() { m_fixed_xpos = x(); m_fixed=true; }
// qreal getFixedX() { return m_fixed_xpos; }
signals:
void LayerMoved();
//public slots:
// void updateHeight();
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
//signals:
// void LayerMoved();
// void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
//protected:
// void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
// void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
// void dropEvent(QGraphicsSceneDragDropEvent *event);
// void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
const DesignerMimeData *checkDragEvent(QGraphicsSceneDragDropEvent * event);
//// void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
// void mousePressEvent(QGraphicsSceneMouseEvent *event);
// void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
// QVariant itemChange(GraphicsItemChange change, const QVariant &value);
//! returns true if given point is in alowed areas of multilayer
bool isInDropArea(QPointF pos);
// const DesignerMimeData *checkDragEvent(QGraphicsSceneDragDropEvent * event);
//! returns true if name of the object match whose the multi layer can handle
bool isExpectedObject(const QString &name);
// //! returns true if given point is in alowed areas of multilayer
// bool isInDropArea(QPointF pos);
private:
QList<QRectF> m_drop_areas;
QStringList m_expected_types;
QStringList m_current_types;
qreal m_fixed_xpos;
bool m_fixed;
};
// //! returns true if name of the object match whose the multi layer can handle
// bool isExpectedObject(const QString &name);
//private:
// QList<QRectF> m_drop_areas;
// QStringList m_expected_types;
// QStringList m_current_types;
// qreal m_fixed_xpos;
// bool m_fixed;
//};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment