summaryrefslogtreecommitdiffstats
path: root/kplato/kptpertcanvas.cc
blob: 9d12945cf92571fa32e2c80f77aab31a5cfa6129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* 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"

#include <tqbuffer.h>
#include <tqtimer.h>
#include <tqclipboard.h>
#include <tqprogressdialog.h>
#include <tqobjectlist.h>
#include <tqpainter.h>
#include <tqheader.h>
#include <tqcursor.h>
#include <tqrect.h>
#include <tqsize.h>
#include <tqptrlist.h>

#include <KoStore.h>
#include <tdetempfile.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeapplication.h>
#include <tdemessagebox.h>
#include <assert.h>
#include <tdemultipledrag.h>
#include <tdelistview.h>

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()<<k_funcinfo<<endl;
    clear();
    updateContents();

    // First make node items
    TQPtrListIterator<Node> nit(project.childNodeIterator());
    for ( ; nit.current(); ++nit ) {
        createChildItems(createNodeItem(nit.current()));
    }

    // First all items with relations
    TQPtrDictIterator<PertNodeItem> it(m_nodes);
    for(; it.current(); ++it)
    {
        if (!(it.current()->hasParent()) && it.current()->hasChild())
        {
            m_rows.append(new TQMemArray<bool>(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<bool>(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()<<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;
}

void PertCanvas::createChildItems(PertNodeItem *parentItem)
{
    //kdDebug()<<k_funcinfo<<"parentItem="<<(parentItem ? parentItem->node().name() : "nil")<<endl;
    if (!parentItem)
        return;

    TQPtrListIterator<Relation> 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<Node> nit(parentItem->node().childNodeIterator());
    for ( ; nit.current(); ++nit ) {
        createChildItems(createNodeItem(nit.current()));
	}
}

void PertCanvas::drawRelations()
{
	//kdDebug()<<k_funcinfo<<endl;
    TQPtrListIterator<Relation> 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()<<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;
}

void PertCanvas::mapChildNode(PertNodeItem *parentItem, PertNodeItem *childItem, Relation::Type type)
{
	//kdDebug()<<k_funcinfo<<"Parent: "<<parentItem->node().name()<<" to child: "<<(childItem ? childItem->node().name() : "None")<<endl;
    if (!childItem)
    {   // shouldn't happen...
        kdError()<<k_funcinfo<<"No childItem"<<endl;
        return;
    }
    int row = parentItem->row();
    int col = parentItem->column();
    int chRow = childItem->row();
    int chCol = childItem->column();
    bool chMapped = (chRow > -1 && chCol > -1);
	//kdDebug()<<k_funcinfo<<"Parent: "<<parentItem->node().name()<<" at ("<<row<<","<<col<<"): Moving "<<childItem->node().name()<<" from: "<<chRow<<","<<chCol<<endl;

    if (type == Relation::StartStart ||
        type == Relation::FinishFinish)
    {
        // node goes into row below parent, at least same col
        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)) {
                    m_rows.append(new TQMemArray<bool>(1)); // make a new row
                    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
            {
                m_rows.append(new TQMemArray<bool>(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<bool>(col+1)); // col not free, so make a new row

            chRow = m_rows.count() -1;
            chCol = col;
        }
    }
    else
    {
        kdError()<<k_funcinfo<<"Unknow relation type"<<endl;
        return;
    }
    childItem->move(this, chRow, chCol);
}

TQSize PertCanvas::canvasSize()
{
    //kdDebug()<<k_funcinfo<<endl;
	TQSize s(0,0);
    TQCanvasItemList list = canvas()->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()<<k_funcinfo<<" gl.X,gl.Y="<<e->globalX()<<","<<e->globalY()<<" x,y="<<e->x()<<","<<e->y()<<endl;
    switch (e->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()<<k_funcinfo<<" gl.X,gl.Y="<<e->globalX()<<","<<e->globalY()<<" x,y="<<e->x()<<","<<e->y()<<endl;
    switch (e->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()<<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;
        }
        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"