/* This file is part of the KDE project Copyright (C) 2003 - 2004 Dag Andersen This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; version 2 of the License. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "kptpertcanvas.h" #include "kptnode.h" #include "kptrelation.h" #include "kptrelationdialog.h" #include "kptcanvasitem.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace KPlato { PertCanvas::PertCanvas( TQWidget *parent ) : TQCanvasView( parent, "Pert canvas" /*WNorthWestGravity WStaticContents| WResizeNoErase | WRepaintNoErase */), m_verticalGap(20), m_horizontalGap(10), m_itemSize(100,30) { //setHScrollBarMode(TQScrollView::AlwaysOn); m_canvas = new TQCanvas( TQT_TQOBJECT(this) ); setCanvas( m_canvas ); } PertCanvas::~PertCanvas() { } void PertCanvas::draw(Project& project) { //kdDebug()< nit(project.childNodeIterator()); for ( ; nit.current(); ++nit ) { createChildItems(createNodeItem(nit.current())); } // First all items with relations TQPtrDictIterator it(m_nodes); for(; it.current(); ++it) { if (!(it.current()->hasParent()) && it.current()->hasChild()) { m_rows.append(new TQMemArray(1)); // New node always goes into new row, first column it.current()->move(this, m_rows.count()-1, 0); // item also moves it's children } } // now items without relations for(it.toFirst(); it.current(); ++it) { if (!(it.current()->hasParent() || it.current()->hasChild())) { m_rows.append(new TQMemArray(1)); // New node always goes into new row, first column it.current()->move(this, m_rows.count()-1, 0); } } drawRelations(); // done _after_ all nodes are drawn TQSize s = canvasSize(); m_canvas->resize(s.width(), s.height()); update(); } PertNodeItem *PertCanvas::createNodeItem(Node *node) { PertNodeItem *item = m_nodes.find(node); if (!item) { if ( node->type() == Node::Type_Project) kdDebug()<type() == Node::Type_Subproject) item = new PertProjectItem(this, *node); else if (node->type()== Node::Type_Summarytask) item = new PertTaskItem(this, *node); else if (node->type()== Node::Type_Task) item = new PertTaskItem(this, *node); else if (node->type() == Node::Type_Milestone) item = new PertMilestoneItem(this, *node); else kdDebug()< it(parentItem->node().dependChildNodes()); for (; it.current(); ++it) { PertNodeItem *childItem = createNodeItem(it.current()->child()); if (childItem) parentItem->addChildRelation(it.current(), childItem); m_relations.append(it.current()); } // Now my children TQPtrListIterator nit(parentItem->node().childNodeIterator()); for ( ; nit.current(); ++nit ) { createChildItems(createNodeItem(nit.current())); } } void PertCanvas::drawRelations() { //kdDebug()< it(m_relations); for (; it.current(); ++it) { PertNodeItem *parentItem = m_nodes.find(it.current()->parent()); PertNodeItem *childItem = m_nodes.find(it.current()->child()); if (parentItem && childItem) { PertRelationItem *item = new PertRelationItem(this, parentItem, childItem, it.current()); item->show(); } } } void PertCanvas::mapNode(PertNodeItem *item) { //kdDebug()<row()) || (item->column() >= 0 && m_rows.at(item->row())->count() <= uint(item->column()))) { kdError()<node().name()<<": non existing map for: ("<row()<<","<column()<<")"<row())->at(item->column()) = true; } void PertCanvas::mapChildNode(PertNodeItem *parentItem, PertNodeItem *childItem, Relation::Type type) { //kdDebug()<node().name()<<" to child: "<<(childItem ? childItem->node().name() : "None")<row(); int col = parentItem->column(); int chRow = childItem->row(); int chCol = childItem->column(); bool chMapped = (chRow > -1 && chCol > -1); //kdDebug()<node().name()<<" at ("<node().name()<<" from: "<at(chCol) = false; //kdDebug()<node().name()<<" from: "<= 0 && m_rows.count() <= uint(chRow)) { m_rows.append(new TQMemArray(1)); // make a new row chRow = m_rows.count()-1; // to be safe } //kdDebug()<node().name()<<" to row: "<= 0 && m_rows.at(chRow)->count() <= uint(chCol)) // col does not exist m_rows.at(chRow)->resize(chCol+1); //kdDebug()<node().name()<<" to col: "<at(col) == true) // col is not free { m_rows.append(new TQMemArray(col+1)); // make a new row } else if (col >= 0 && m_rows.at(row+1)->count() <= uint(col)) // col does not exist m_rows.at(row)->resize(col+1); chRow = m_rows.count() -1; chCol = col; } } else if (type == Relation::FinishStart) { // node goes into same row, next col if col free if (chMapped) { m_rows.at(chRow)->at(chCol) = false; if (chRow < row) chRow = row; if (chCol <= col) { chCol = col+1; } if (chCol >= 0 && m_rows.at(chRow)->count() <= uint(chCol)) // col does not exist m_rows.at(chRow)->resize(chCol+1); } else { ++col; if (col >= 0 && m_rows.at(row)->count() <= uint(col)) m_rows.at(row)->resize(col+1); // make new column else if (m_rows.at(row)->at(col) = true) m_rows.append(new TQMemArray(col+1)); // col not free, so make a new row chRow = m_rows.count() -1; chCol = col; } } else { kdError()<move(this, chRow, chCol); } TQSize PertCanvas::canvasSize() { //kdDebug()<allItems(); TQCanvasItemList::Iterator it = list.begin(); for (; it != list.end(); ++it) { TQRect r = (*it)->boundingRect(); s.setWidth(TQMAX(s.width(), r.right())); s.setHeight(TQMAX(s.height(), r.bottom())); } s.setWidth(s.width()+20); s.setHeight(s.height()+20); return s; } void PertCanvas::clear() { m_nodes.clear(); m_relations.clear(); m_rows.clear(); TQCanvasItemList list = canvas()->allItems(); TQCanvasItemList::Iterator it = list.begin(); for (; it != list.end(); ++it) { if ( *it ) delete *it; } } void PertCanvas::contentsMousePressEvent ( TQMouseEvent * e ) { //kdDebug()<globalY()<<" x,y="<x()<<","<y()<button()) { case Qt::LeftButton: { break; } case Qt::RightButton: { PertNodeItem *item = selectedItem(); if (item) item->setSelected(false); canvas()->update(); TQCanvasItemList l = canvas()->collisions(e->pos()); for (TQCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { if ( (*it)->rtti() == PertProjectItem::RTTI || (*it)->rtti() == PertTaskItem::RTTI || (*it)->rtti() == PertMilestoneItem::RTTI ) { PertNodeItem *item = (PertNodeItem *)(*it); { item->setSelected(true); canvas()->update(); emit rightButtonPressed(&(item->node()), e->globalPos()); if (item == selectedItem()) { // item maybe deleted item->setSelected(false); } canvas()->update(); break; } } } break; } case Qt::MidButton: break; default: break; } } void PertCanvas::contentsMouseReleaseEvent ( TQMouseEvent * e ) { //kdDebug()<globalY()<<" x,y="<x()<<","<y()<button()) { case Qt::LeftButton: { bool hit = false; TQCanvasItemList l = canvas()->collisions(e->pos()); for (TQCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { if ( (*it)->rtti() == PertProjectItem::RTTI || (*it)->rtti() == PertTaskItem::RTTI || (*it)->rtti() == PertMilestoneItem::RTTI ) { hit = true; PertNodeItem *item = (PertNodeItem *)(*it); PertNodeItem *par = selectedItem(); if ( !par) { //kdDebug()<node().findRelation(&(par->node())); if (rel) emit modifyRelation(rel); else emit addRelation(&(par->node()), &(item->node())); } break; } } if (!hit) { PertNodeItem *i = selectedItem(); if (i) i->setSelected(false); } canvas()->update(); break; } case Qt::RightButton: { break; } case Qt::MidButton: break; default: break; } } PertNodeItem *PertCanvas::selectedItem() { TQCanvasItemList list = canvas()->allItems(); TQCanvasItemList::Iterator it = list.begin(); for (; it != list.end(); ++it) { if ( (*it)->isSelected() ) { if ( (*it)->rtti() == PertProjectItem::RTTI || (*it)->rtti() == PertTaskItem::RTTI || (*it)->rtti() == PertMilestoneItem::RTTI ) return (PertNodeItem *)(*it); } } return 0; } Node *PertCanvas::selectedNode() { return selectedItem() ? &(selectedItem()->node()) : 0; } #ifndef NDEBUG void PertCanvas::printDebug( int /*info*/ ) { } #endif } //KPlato namespace #include "kptpertcanvas.moc"