FlowVis 1.0

FlowVis/LayerManagerWidget.cpp

00001 #include "LayerManagerWidget.h"
00002 #include <QtGui>
00003 #include <iostream>
00004 
00005 #define MAX_LAYERS 5
00006 
00007 LayerManagerWidget::LayerManagerWidget(QWidget *parent)
00008         : QWidget(parent)
00009 {
00010         layout = new QVBoxLayout();
00011         stackedLayers = new QVBoxLayout();
00012         addLayerButton = new QPushButton("Add Layer");
00013 
00014         layout->addLayout(stackedLayers);
00015         layout->addStretch();
00016         layout->addWidget(addLayerButton,0,Qt::AlignRight);
00017         
00018         this->setLayout(layout);
00019 
00020 
00021         testAddLayers();
00022         this->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
00023 
00024         connectSignals();
00025 }
00026 
00027 void LayerManagerWidget::connectSignals()
00028 {
00029         QObject::connect(addLayerButton,SIGNAL(clicked()),this,SLOT(addLayer()));
00030 }
00031 
00032 void LayerManagerWidget::layerTypeChanged(int type, LayerWidget *sender)
00033 {
00034         int index = stackedLayers->indexOf(sender);
00035         if(index != -1)         //exists
00036                 emit layerTypeChanged(type, index);
00037 }
00038 
00039 void LayerManagerWidget::layerChannelChanged(int channel, LayerWidget *sender)
00040 {
00041         int index = stackedLayers->indexOf(sender);
00042         if(index != -1)         //exists
00043                 emit layerChannelChanged(channel, index);
00044 }
00045 
00046 void LayerManagerWidget::layerTransferImageUpdated(const QImage& image, LayerWidget *sender)
00047 {
00048         int index = stackedLayers->indexOf(sender);
00049         if(index != -1)         //exists
00050                 emit layerTransferImageChanged(image,index);
00051 }
00052 LayerWidget* LayerManagerWidget::addLayerWidget()
00053 {
00054         if(stackedLayers->count() == MAX_LAYERS)
00055                 return NULL;
00056 
00057         LayerWidget *layer = new LayerWidget();
00058         //layers.append(layer);
00059         
00060         stackedLayers->addWidget(layer);
00061 
00062         QObject::connect(layer,SIGNAL(buttonClicked(int,LayerWidget*)),this,SLOT(layerButtonClicked(int,LayerWidget*)));
00063         QObject::connect(layer,SIGNAL(typeChanged(int,LayerWidget*)),this,SLOT(layerTypeChanged(int,LayerWidget*)));
00064         QObject::connect(layer,SIGNAL(channelChanged(int,LayerWidget*)),this,SLOT(layerChannelChanged(int,LayerWidget*)));
00065         QObject::connect(layer,SIGNAL(transferImageUpdated(const QImage,LayerWidget*)),this,SLOT(layerTransferImageUpdated(const QImage,LayerWidget*)));
00066         QObject::connect(this,SIGNAL(channelCountUpdated(int)),layer,SLOT(channelCountChanged(int)));
00067         emit layerAdded(stackedLayers->indexOf(layer));         //neuer layer
00068         layer->channelCountChanged(channelCount);
00069         this->update();
00070 
00071         //removeLayer(layer);
00072 
00073         return layer;
00074 }
00075 
00076 void LayerManagerWidget::addLayer()
00077 {
00078         addLayerWidget();
00079 }
00080 void LayerManagerWidget::layerButtonClicked(int button,LayerWidget* sender)
00081 {
00082         int pos = stackedLayers->indexOf(sender);
00083         switch(button)
00084         {
00085         case 0: //Down
00086                 if(pos == stackedLayers->count()-1)
00087                         break;
00088 
00089                 stackedLayers->removeWidget(sender);
00090                 stackedLayers->insertWidget(pos+1,sender);
00091                 
00092                 emit layerPosChanged(pos, pos+1);
00093                 break;
00094 
00095         case 1: //up
00096                 if(pos == 0)
00097                         break;
00098                 
00099                 stackedLayers->removeWidget(sender);
00100                 stackedLayers->insertWidget(pos-1,sender);
00101 
00102                 emit layerPosChanged(pos, pos-1);
00103                 break;
00104 
00105         case 2: //Remove
00106                 
00107                 removeLayer(sender);
00108                 break;
00109         
00110         }
00111                 
00112 }
00113 void LayerManagerWidget::removeLayer(LayerWidget *layer, bool check)
00114 {
00115         int count = stackedLayers->count();
00116         if((stackedLayers->indexOf(layer) != -1) && (stackedLayers->count() > 1 || check == false))
00117         {
00118 
00119                 emit layerRemoved(stackedLayers->indexOf(layer));
00120                 stackedLayers->removeWidget(layer);
00121 
00122                 delete layer;
00123                 this->repaint();
00124         }
00125 }
00126 void LayerManagerWidget::testAddLayers()
00127 {
00128         addLayer();
00129         
00130 }
00131 
00132 void LayerManagerWidget::channelCountChanged(int channels)
00133 {
00134         channelCount = channels;
00135         emit channelCountUpdated(channels);
00136 }
00137 LayerManagerWidget::~LayerManagerWidget()
00138 {
00139         delete layout;
00140 }
00141 
00142 void LayerManagerWidget::saveToFile()
00143 {
00144         QString fileName = QFileDialog::getSaveFileName(this, tr("Save Config File"), ".", tr("Config Files (*.cfg)"));
00145         
00146         QFile file(fileName);
00147 
00148         file.open(QIODevice::WriteOnly);
00149 
00150         QDataStream out(&file);
00151 
00152         int count = stackedLayers->count();
00153 
00154         out << count;
00155 
00156         for(int i=0; i< count; i++)
00157                 out << (LayerWidget*)stackedLayers->itemAt(i)->widget();
00158 
00159         file.close();
00160 
00161         std::cout << "Config saved to " << fileName.toStdString() << std::endl;
00162 }
00163 
00164 void LayerManagerWidget::loadFromFile()
00165 {
00166         QString fileName = QFileDialog::getOpenFileName(this, tr("Open Config File"), ".", tr("Config Files (*.cfg)"));
00167 
00168         QFile file(fileName);
00169         if(file.exists()){
00170                 file.open(QIODevice::ReadOnly);
00171 
00172                 QDataStream in(&file);
00173 
00174                 int count;
00175                 
00176                 while(stackedLayers->count() > 0)
00177                         removeLayer((LayerWidget*)stackedLayers->itemAt(0)->widget(),false);    //alle removen 
00178                 
00179                 in >> count;
00180 
00181                 for(int i=0; i<count; i++)      //Layer einlesen
00182                 {
00183                         LayerWidget* widget = addLayerWidget();
00184                         in >> widget;
00185                         widget->channelCountChanged(channelCount);
00186                 }
00187 
00188                 std::cout << "Config read from " << fileName.toStdString() << std::endl;
00189         }
00190 }
 All Classes Functions Variables Friends