You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
437 lines
13 KiB
437 lines
13 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 2003 - 2004 Dag Andersen <danders@get2net.dk>
|
||
|
|
||
|
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"
|
||
|
|
||
14 years ago
|
#include <tqbuffer.h>
|
||
|
#include <tqtimer.h>
|
||
13 years ago
|
#include <tqclipboard.h>
|
||
14 years ago
|
#include <tqprogressdialog.h>
|
||
|
#include <tqobjectlist.h>
|
||
|
#include <tqpainter.h>
|
||
|
#include <tqheader.h>
|
||
|
#include <tqcursor.h>
|
||
|
#include <tqrect.h>
|
||
|
#include <tqsize.h>
|
||
|
#include <tqptrlist.h>
|
||
15 years ago
|
|
||
|
#include <KoStore.h>
|
||
12 years ago
|
#include <tdetempfile.h>
|
||
|
#include <tdelocale.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
12 years ago
|
#include <tdemessagebox.h>
|
||
15 years ago
|
#include <assert.h>
|
||
12 years ago
|
#include <tdemultipledrag.h>
|
||
12 years ago
|
#include <tdelistview.h>
|
||
15 years ago
|
|
||
|
namespace KPlato
|
||
|
{
|
||
|
|
||
14 years ago
|
PertCanvas::PertCanvas( TQWidget *parent )
|
||
|
: TQCanvasView( parent, "Pert canvas" /*WNorthWestGravity WStaticContents| WResizeNoErase | WRepaintNoErase */),
|
||
15 years ago
|
m_verticalGap(20),
|
||
|
m_horizontalGap(10),
|
||
|
m_itemSize(100,30)
|
||
|
|
||
|
{
|
||
14 years ago
|
//setHScrollBarMode(TQScrollView::AlwaysOn);
|
||
|
m_canvas = new TQCanvas( TQT_TQOBJECT(this) );
|
||
15 years ago
|
setCanvas( m_canvas );
|
||
|
}
|
||
|
|
||
|
PertCanvas::~PertCanvas()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void PertCanvas::draw(Project& project)
|
||
|
{
|
||
|
//kdDebug()<<k_funcinfo<<endl;
|
||
|
clear();
|
||
|
updateContents();
|
||
|
|
||
|
// First make node items
|
||
14 years ago
|
TQPtrListIterator<Node> nit(project.childNodeIterator());
|
||
15 years ago
|
for ( ; nit.current(); ++nit ) {
|
||
|
createChildItems(createNodeItem(nit.current()));
|
||
|
}
|
||
|
|
||
|
// First all items with relations
|
||
14 years ago
|
TQPtrDictIterator<PertNodeItem> it(m_nodes);
|
||
15 years ago
|
for(; it.current(); ++it)
|
||
|
{
|
||
|
if (!(it.current()->hasParent()) && it.current()->hasChild())
|
||
|
{
|
||
14 years ago
|
m_rows.append(new TQMemArray<bool>(1)); // New node always goes into new row, first column
|
||
13 years ago
|
it.current()->move(this, m_rows.count()-1, 0); // item also moves it's children
|
||
15 years ago
|
}
|
||
|
}
|
||
|
// now items without relations
|
||
|
for(it.toFirst(); it.current(); ++it)
|
||
|
{
|
||
|
if (!(it.current()->hasParent() || it.current()->hasChild()))
|
||
|
{
|
||
14 years ago
|
m_rows.append(new TQMemArray<bool>(1)); // New node always goes into new row, first column
|
||
15 years ago
|
it.current()->move(this, m_rows.count()-1, 0);
|
||
|
}
|
||
|
}
|
||
|
drawRelations(); // done _after_ all nodes are drawn
|
||
14 years ago
|
TQSize s = canvasSize();
|
||
15 years ago
|
m_canvas->resize(s.width(), s.height());
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
PertNodeItem *PertCanvas::createNodeItem(Node *node)
|
||
|
{
|
||
14 years ago
|
PertNodeItem *item = m_nodes.find(node);
|
||
15 years ago
|
if (!item)
|
||
|
{
|
||
|
if ( node->type() == Node::Type_Project)
|
||
|
kdDebug()<<k_funcinfo<<"Project nodes should not have relations"<<endl;
|
||
|
else if (node->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()<<k_funcinfo<<"Not implemented yet"<<endl;
|
||
|
|
||
|
if (item)
|
||
|
m_nodes.insert(node, item);
|
||
|
}
|
||
|
return item;
|
||
|
}
|
||
|
|
||
14 years ago
|
void PertCanvas::createChildItems(PertNodeItem *parentItem)
|
||
15 years ago
|
{
|
||
14 years ago
|
//kdDebug()<<k_funcinfo<<"parentItem="<<(parentItem ? parentItem->node().name() : "nil")<<endl;
|
||
|
if (!parentItem)
|
||
15 years ago
|
return;
|
||
|
|
||
14 years ago
|
TQPtrListIterator<Relation> it(parentItem->node().dependChildNodes());
|
||
15 years ago
|
for (; it.current(); ++it)
|
||
|
{
|
||
|
PertNodeItem *childItem = createNodeItem(it.current()->child());
|
||
|
if (childItem)
|
||
14 years ago
|
parentItem->addChildRelation(it.current(), childItem);
|
||
15 years ago
|
m_relations.append(it.current());
|
||
|
}
|
||
|
|
||
13 years ago
|
// Now my children
|
||
14 years ago
|
TQPtrListIterator<Node> nit(parentItem->node().childNodeIterator());
|
||
15 years ago
|
for ( ; nit.current(); ++nit ) {
|
||
|
createChildItems(createNodeItem(nit.current()));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PertCanvas::drawRelations()
|
||
|
{
|
||
|
//kdDebug()<<k_funcinfo<<endl;
|
||
14 years ago
|
TQPtrListIterator<Relation> it(m_relations);
|
||
15 years ago
|
for (; it.current(); ++it)
|
||
|
{
|
||
14 years ago
|
PertNodeItem *parentItem = m_nodes.find(it.current()->parent());
|
||
14 years ago
|
PertNodeItem *childItem = m_nodes.find(it.current()->child());
|
||
14 years ago
|
if (parentItem && childItem)
|
||
15 years ago
|
{
|
||
14 years ago
|
PertRelationItem *item = new PertRelationItem(this, parentItem, childItem, it.current());
|
||
15 years ago
|
item->show();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PertCanvas::mapNode(PertNodeItem *item)
|
||
|
{
|
||
|
//kdDebug()<<k_funcinfo<<endl;
|
||
|
if (! m_rows.at(item->row()) || (item->column() >= 0 && m_rows.at(item->row())->count() <= uint(item->column())))
|
||
|
{
|
||
|
kdError()<<k_funcinfo<<item->node().name()<<": non existing map for: ("<<item->row()<<","<<item->column()<<")"<<endl;
|
||
|
return;
|
||
|
}
|
||
|
m_rows.at(item->row())->at(item->column()) = true;
|
||
|
}
|
||
|
|
||
14 years ago
|
void PertCanvas::mapChildNode(PertNodeItem *parentItem, PertNodeItem *childItem, Relation::Type type)
|
||
15 years ago
|
{
|
||
14 years ago
|
//kdDebug()<<k_funcinfo<<"Parent: "<<parentItem->node().name()<<" to child: "<<(childItem ? childItem->node().name() : "None")<<endl;
|
||
15 years ago
|
if (!childItem)
|
||
|
{ // shouldn't happen...
|
||
|
kdError()<<k_funcinfo<<"No childItem"<<endl;
|
||
|
return;
|
||
|
}
|
||
14 years ago
|
int row = parentItem->row();
|
||
|
int col = parentItem->column();
|
||
15 years ago
|
int chRow = childItem->row();
|
||
|
int chCol = childItem->column();
|
||
|
bool chMapped = (chRow > -1 && chCol > -1);
|
||
14 years ago
|
//kdDebug()<<k_funcinfo<<"Parent: "<<parentItem->node().name()<<" at ("<<row<<","<<col<<"): Moving "<<childItem->node().name()<<" from: "<<chRow<<","<<chCol<<endl;
|
||
15 years ago
|
|
||
|
if (type == Relation::StartStart ||
|
||
|
type == Relation::FinishFinish)
|
||
|
{
|
||
14 years ago
|
// node goes into row below parent, at least same col
|
||
15 years ago
|
if (chMapped)
|
||
|
{
|
||
|
m_rows.at(chRow)->at(chCol) = false;
|
||
|
//kdDebug()<<k_funcinfo<<" Moving "<<childItem->node().name()<<" from: "<<chRow<<","<<chCol<<endl;
|
||
|
if (chRow <= row)
|
||
|
{
|
||
|
chRow = row+1;
|
||
|
if (chRow >= 0 && m_rows.count() <= uint(chRow)) {
|
||
14 years ago
|
m_rows.append(new TQMemArray<bool>(1)); // make a new row
|
||
15 years ago
|
chRow = m_rows.count()-1; // to be safe
|
||
|
}
|
||
|
//kdDebug()<<k_funcinfo<<" Moving "<<childItem->node().name()<<" to row: "<<chRow<<endl;
|
||
|
}
|
||
|
if (chCol < col)
|
||
|
{
|
||
|
chCol = col;
|
||
|
if (chCol >= 0 && m_rows.at(chRow)->count() <= uint(chCol)) // col does not exist
|
||
|
m_rows.at(chRow)->resize(chCol+1);
|
||
|
|
||
|
//kdDebug()<<k_funcinfo<<" Moved "<<childItem->node().name()<<" to col: "<<chCol<<endl;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (!(m_rows.at(row+1)) || // next row does not exists
|
||
|
m_rows.at(row+1)->at(col) == true) // col is not free
|
||
|
{
|
||
14 years ago
|
m_rows.append(new TQMemArray<bool>(col+1)); // make a new row
|
||
15 years ago
|
}
|
||
|
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)
|
||
14 years ago
|
m_rows.append(new TQMemArray<bool>(col+1)); // col not free, so make a new row
|
||
15 years ago
|
|
||
|
chRow = m_rows.count() -1;
|
||
|
chCol = col;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kdError()<<k_funcinfo<<"Unknow relation type"<<endl;
|
||
|
return;
|
||
|
}
|
||
|
childItem->move(this, chRow, chCol);
|
||
|
}
|
||
|
|
||
14 years ago
|
TQSize PertCanvas::canvasSize()
|
||
15 years ago
|
{
|
||
|
//kdDebug()<<k_funcinfo<<endl;
|
||
14 years ago
|
TQSize s(0,0);
|
||
|
TQCanvasItemList list = canvas()->allItems();
|
||
|
TQCanvasItemList::Iterator it = list.begin();
|
||
15 years ago
|
for (; it != list.end(); ++it)
|
||
|
{
|
||
14 years ago
|
TQRect r = (*it)->boundingRect();
|
||
|
s.setWidth(TQMAX(s.width(), r.right()));
|
||
|
s.setHeight(TQMAX(s.height(), r.bottom()));
|
||
15 years ago
|
}
|
||
|
s.setWidth(s.width()+20);
|
||
|
s.setHeight(s.height()+20);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
void PertCanvas::clear()
|
||
|
{
|
||
|
m_nodes.clear();
|
||
|
m_relations.clear();
|
||
|
m_rows.clear();
|
||
14 years ago
|
TQCanvasItemList list = canvas()->allItems();
|
||
|
TQCanvasItemList::Iterator it = list.begin();
|
||
15 years ago
|
for (; it != list.end(); ++it)
|
||
|
{
|
||
|
if ( *it )
|
||
|
delete *it;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void PertCanvas::contentsMousePressEvent ( TQMouseEvent * e )
|
||
15 years ago
|
{
|
||
|
//kdDebug()<<k_funcinfo<<" gl.X,gl.Y="<<e->globalX()<<","<<e->globalY()<<" x,y="<<e->x()<<","<<e->y()<<endl;
|
||
|
switch (e->button())
|
||
|
{
|
||
14 years ago
|
case Qt::LeftButton:
|
||
15 years ago
|
{
|
||
|
break;
|
||
|
}
|
||
14 years ago
|
case Qt::RightButton:
|
||
15 years ago
|
{
|
||
|
PertNodeItem *item = selectedItem();
|
||
|
if (item)
|
||
|
item->setSelected(false);
|
||
|
canvas()->update();
|
||
|
|
||
14 years ago
|
TQCanvasItemList l = canvas()->collisions(e->pos());
|
||
|
for (TQCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
|
||
15 years ago
|
{
|
||
|
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;
|
||
|
}
|
||
14 years ago
|
case Qt::MidButton:
|
||
15 years ago
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void PertCanvas::contentsMouseReleaseEvent ( TQMouseEvent * e )
|
||
15 years ago
|
{
|
||
|
//kdDebug()<<k_funcinfo<<" gl.X,gl.Y="<<e->globalX()<<","<<e->globalY()<<" x,y="<<e->x()<<","<<e->y()<<endl;
|
||
|
switch (e->button())
|
||
|
{
|
||
14 years ago
|
case Qt::LeftButton:
|
||
15 years ago
|
{
|
||
|
bool hit = false;
|
||
14 years ago
|
TQCanvasItemList l = canvas()->collisions(e->pos());
|
||
|
for (TQCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
|
||
15 years ago
|
{
|
||
|
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()<<k_funcinfo<<" First node="<<item->node().name()<<endl;
|
||
|
item->setSelected(true);
|
||
|
canvas()->update();
|
||
|
return;
|
||
|
}
|
||
|
par->setSelected(false);
|
||
|
if (&(item->node()) == &(par->node()))
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
//kdDebug()<<k_funcinfo<<" Second node="<<item->node().name()<<endl;
|
||
|
// open relation dialog
|
||
|
if (!par->node().legalToLink(&(item->node()))) {
|
||
|
KMessageBox::sorry(this, i18n("Cannot link these nodes"));
|
||
|
} else {
|
||
|
Relation *rel = item->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;
|
||
|
}
|
||
14 years ago
|
case Qt::RightButton:
|
||
15 years ago
|
{
|
||
|
break;
|
||
|
}
|
||
14 years ago
|
case Qt::MidButton:
|
||
15 years ago
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
PertNodeItem *PertCanvas::selectedItem()
|
||
|
{
|
||
14 years ago
|
TQCanvasItemList list = canvas()->allItems();
|
||
|
TQCanvasItemList::Iterator it = list.begin();
|
||
15 years ago
|
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"
|