00001 #include "DescriptorWidget.h"
00002 #include "DescriptorWidget.moc"
00003
00004 #include <iostream>
00005
00006 DescriptorWidget::DescriptorWidget(QWidget *parent):
00007 QGraphicsView(parent),
00008 m_items(0)
00009 {
00010 m_scene = new QGraphicsScene(this);
00011 setScene(m_scene);
00012 setCacheMode(CacheBackground);
00013 setViewportUpdateMode(BoundingRectViewportUpdate);
00014 setRenderHint(QPainter::Antialiasing);
00015 }
00016
00017 void DescriptorWidget::addDescriptor(QGraphicsItem * item){
00018 if(!item) return;
00019 QRectF bounding = item->boundingRect();
00020 float adjust = 10;
00021 float width = adjust + bounding.width()/2;
00022 float height = adjust + bounding.height()/2;
00023 if(!m_items.size()){
00024 item->setPos(width, height);
00025 } else {
00026 QPointF position = m_items.back()->pos();
00027 item->setPos(position.x() + width * 2, height);
00028 }
00029 m_items.push_back(item);
00030 m_scene->addItem(item);
00031 update();
00032 }
00033
00034 void DescriptorWidget::clear(){
00035 if(m_scene) delete m_scene;
00036 m_scene = new QGraphicsScene(this);
00037 setScene(m_scene);
00038 setCacheMode(CacheBackground);
00039 setViewportUpdateMode(BoundingRectViewportUpdate);
00040 setRenderHint(QPainter::Antialiasing);
00041
00042
00043
00044
00045 m_items.clear();
00046
00047 }
00048
00049 void DescriptorWidget::mousePressEvent(QMouseEvent *event){
00050 const QGraphicsItem * selected = itemAt(event->pos());
00051 int index = findDescriptor(selected);
00052 if(index > -1) emit descriptorSelected(index);
00053 }
00054
00055 int DescriptorWidget::findDescriptor(const QGraphicsItem * item){
00056 int index = -1;
00057 for(int i = 0; i < m_items.size(); i++){
00058 if(m_items[i] == item){
00059 index = i;
00060 break;
00061 }
00062 }
00063 return index;
00064 }