summaryrefslogtreecommitdiffstats
path: root/kplato/kptresourceview.cc
blob: c20b9fd26fc2540d8524c45fcd46f52f7016e70f (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/* This file is part of the KDE project
   Copyright (C) 2003 - 2004 Dag Andersen kplato@kde.org>

   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 "kptresourceview.h"

#include "kptcalendar.h"
#include "kptduration.h"
#include "kptresourceappointmentsview.h"
#include "kptview.h"
#include "kptnode.h"
#include "kptproject.h"
#include "kpttask.h"
#include "kptresource.h"
#include "kptdatetime.h"
#include "kptcontext.h"

#include <tqheader.h>
#include <tqpopupmenu.h>
#include <tqpainter.h>
#include <tqpaintdevicemetrics.h>
#include <tqstyle.h>

#include <tdelistview.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <kprinter.h>

#include <kdebug.h>

namespace KPlato
{

class ResListView : public TDEListView {
public:
    ResListView(TQWidget * parent = 0, const char* name=0)
    : TDEListView(parent, name)
    {}

    int headerHeight() const {
        return header()->count() > 0 ? header()->sectionRect(0).height() : 0;
    }
    virtual void paintToPrinter(TQPainter *p, int x, int y, int w, int h) {
        p->save();
        TQColor bgc(193, 223, 255);
        TQBrush bg(bgc);
        p->setBackgroundMode(Qt::OpaqueMode);
        p->setBackgroundColor(bgc);
        TQHeader *head = header();
        int offset = 0;
        TQRect sr;
        // Header shall always be at top/left on page
        for (int s = 0; s < head->count(); ++s) {
            sr = head->sectionRect(s);
            if (offset > sr.x())
                offset = sr.x();
        }
        for (int s = 0; s < head->count(); ++s) {
            sr = head->sectionRect(s);
            if (offset != 0) {
                sr = TQRect(sr.x()-offset, sr.y(), sr.width(), sr.height());
            }
            //kdDebug()<<s<<": "<<head->label(s)<<" "<<sr<<endl;
            if (sr.x()+sr.width() <= x || sr.x() >= x+w) {
                //kdDebug()<<s<<": "<<h->label(s)<<" "<<sr<<": continue"<<endl;
                continue;
            }
            TQRect tr = sr;
            if (sr.x() < x) {
                tr.setX(x);
                //kdDebug()<<s<<": "<<head->label(s)<<" "<<tr<<endl;
            }
            p->eraseRect(tr);
            p->drawText(tr, columnAlignment(s)|TQt::AlignVCenter, head->label(s), -1);
        }
        p->restore();
        p->save();
        p->translate(0, headerHeight());
        drawAllContents(p, x, y, w, h);
        p->restore();
    }
    int calculateY(int ymin, int ymax) const {
        TQPtrList<ResListView::DrawableItem> drawables;
        drawables.setAutoDelete(true);
        TQListViewItem *child = firstChild();
        int level = 0;
        int ypos = 0;
        for (; child; child = child->nextSibling()) {
            ypos = buildDrawables(drawables, level, ypos, child, ymin, ymax);
        }
        int y = 0;
        DrawableItem *item = drawables.getLast();
        if (item) {
            y = item->y + item->i->height();
        }
        //kdDebug()<<k_funcinfo<<y<<" ("<<ymin<<", "<<ymax<<")"<<endl;
        return y;
    }
    class DrawableItem {
    public:
        DrawableItem(int level, int ypos, TQListViewItem *item ) { y = ypos; l = level; i = item; };
        int y;
        int l;
        TQListViewItem * i;
    };
protected:
    int buildDrawables(TQPtrList<ResListView::DrawableItem> &lst, int level, int ypos, TQListViewItem *item, int ymin, int ymax) const {
        int y = ypos;
        int ih = item->height();
        if (y < ymin && y+ih > ymin) {
            y = ymin; // include partial item at top
        }
        if (y >= ymin && y+ih < ymax) { // exclude partial item at bottom
            ResListView::DrawableItem *dr = new ResListView::DrawableItem(level, y, item);
            lst.append(dr);
            //kdDebug()<<k_funcinfo<<level<<", "<<y<<" : "<<item->text(0)<<endl;
        }
        y += ih;
        if (item->isOpen()) {
            TQListViewItem *child = item->firstChild();
            for (; child; child = child->nextSibling()) {
                y = buildDrawables(lst, level+1, y, child, ymin, ymax);
            }
        }
        return y;
    }
    // This is a copy of TQListView::drawContentsOffset(), with a few changes
    // because drawContentsOffset() only draws *visible* items,
    // we want to draw *all* items.
    // FIXME: Haven't got paintBraches() to work, atm live without it.
    virtual void drawAllContents(TQPainter * p, int cx, int cy, int cw, int ch) {
        if ( columns() == 0 ) {
            paintEmptyArea( p, TQRect( cx, cy, cw, ch ) );
            return;
        }
        //kdDebug()<<k_funcinfo<<TQRect(cx, cy, cw, ch)<<endl;
        TQPtrList<ResListView::DrawableItem> drawables;
        drawables.setAutoDelete(true);
        TQListViewItem *child = firstChild();
        int level = 0;
        int ypos = 0;
        for (; child; child = child->nextSibling()) {
            ypos = buildDrawables(drawables, level, ypos, child, cy, cy+ch);
        }
        
        p->setFont( font() );
    
        TQPtrListIterator<ResListView::DrawableItem> it(drawables);
    
        TQRect r;
        int fx = -1, x, fc = 0, lc = 0;
        int tx = -1;
        ResListView::DrawableItem * current;
    
        while ( (current = it.current()) != 0 ) {
            ++it;
            int ih = current->i->height();
            int ith = current->i->totalHeight();
            int c;
            int cs;
    
            // need to paint current?
            if ( ih > 0 && current->y < cy+ch && current->y+ih > cy ) {
                //kdDebug()<<k_funcinfo<<"Paint: "<<current->i->text(0)<<" y="<<current->y<<endl;
                if ( fx < 0 ) {
                    // find first interesting column, once
                    x = 0;
                    c = 0;
                    cs = header()->cellSize( 0 );
                    while ( x + cs <= cx && c < header()->count() ) {
                        x += cs;
                        c++;
                        if ( c < header()->count() )
                            cs = header()->cellSize( c );
                    }
                    fx = x;
                    fc = c;
                    while( x < cx + cw && c < header()->count() ) {
                        x += cs;
                        c++;
                        if ( c < header()->count() )
                            cs = header()->cellSize( c );
                    }
                    lc = c;
                }
    
                x = fx;
                c = fc;
                // draw to last interesting column
    
                const TQColorGroup &cg = ( palette().inactive() );
    
                while ( c < lc && !drawables.isEmpty() ) {
                    int i = header()->mapToLogical( c );
                    cs = header()->cellSize( c );
                    r.setRect( x, current->y-cy, cs, ih );
                    if ( i == 0 )
                        r.setLeft( r.left() + current->l * treeStepSize() );
    
                    p->save();
                    // No need to paint if the cell isn't technically visible
                    if ( !( r.width() == 0 || r.height() == 0 ) ) {
                        p->translate( r.left(), r.top() );
                        int ac = header()->mapToLogical( c );
                        // map to Left currently. This should change once we
                        // can really reverse the listview.
                        int align = columnAlignment( ac );
                        if ( align == AlignAuto ) align = AlignLeft;
                        bool sel = current->i->isSelected();
                        if (sel)
                            current->i->setSelected(false);
                        current->i->paintCell( p, cg, ac, r.width(), align );
                        if (sel)
                            current->i->setSelected(sel);
                    }
                    p->restore();
                    x += cs;
                    c++;
                }
    
            }
    
            const int cell = header()->mapToActual( 0 );
    
            if ( tx < 0 )
                tx = header()->cellPos( cell );
    
            // do any children of current need to be painted?
/* FIXME: painting branches doesn't work for some reason...
              if ( ih != ith && 
                 rootIsDecorated() &&
                 current->y + ith > cy &&
                 current->y + ih < cy + ch &&
                 tx + current->l * treeStepSize() < cx + cw &&
                 tx + (current->l+1) * treeStepSize() > cx ) {
                // compute the clip rectangle the safe way
    
                int rtop = current->y + ih;
                int rbottom = current->y + ith;
                int rleft = tx + current->l*treeStepSize();
                int rright = rleft + treeStepSize();
    
                int crtop = TQMAX( rtop, cy );
                int crbottom = TQMIN( rbottom, cy+ch );
                int crleft = TQMAX( rleft, cx );
                int crright = TQMIN( rright, cx+cw );
    
                r.setRect( crleft, crtop,
                        crright-crleft, crbottom-crtop );
    
                if ( r.isValid() ) {
                    p->save();
                    p->translate( rleft, crtop );
                    //kdDebug()<<k_funcinfo<<"paintBranches: "<<current->i->text(0)<<endl;

                     current->i->paintBranches( p, colorGroup(), treeStepSize(),
                                             rtop - crtop, r.height() );
                    p->restore();
                }
            }*/
        }
    }

};

class ResourceItemPrivate : public TDEListViewItem {
public:
    ResourceItemPrivate(Resource *r, TQListViewItem *parent)
        : TDEListViewItem(parent, r->name()),
        resource(r) {}

    Resource *resource;

    virtual void paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int align) {
        TQColorGroup g = cg;
        if (m_columns[column] == 1) {
            g.setColor(TQColorGroup::Text, TQColor(red));
            g.setColor(TQColorGroup::HighlightedText, TQColor(red));
        }

        TDEListViewItem::paintCell(p, g, column, width, align);
    }
    void setColumnState(int c, int state=1) {
        m_columns[c] = state;
    }
private:
    TQMap<int, int> m_columns;
};

class NodeItemPrivate : public TDEListViewItem {
public:
    NodeItemPrivate(Task *n, TQListView *parent)
    : TDEListViewItem(parent, n->name()),
      node(n) {
        init();
    }

    NodeItemPrivate(TQString name, TQListView *parent)
    : TDEListViewItem(parent, name),
      node(0) {
        init();
    }

    void setPriority(int col, int prio) {
        if (prioColors.contains(prio)) {
            columnPrio.insert(col, prio);
        } else {
            columnPrio.remove(col);
        }
    }
    int priority(int col) {
        if (columnPrio.contains(col)) {
            return columnPrio[col];
        }
        return 0;
    }
        
    virtual void paintCell ( TQPainter * p, const TQColorGroup & cg, int column, int width, int align ) {
        //kdDebug()<<k_funcinfo<<"c="<<column<<" prio="<<(columnPrio.contains(column)?columnPrio[column]:0)<<endl;
        TQColorGroup g = cg;
        if (columnPrio.contains(column)) {
            g.setColor(TQColorGroup::Base, prioColors[columnPrio[column]]);
        }
        TDEListViewItem::paintCell(p, g, column, width, align);
    }
    
    Task *node;
private:
    void init() {
        prioColors.insert(1, TQColor(gray));
        prioColors.insert(2, TQColor(green));
        prioColors.insert(3, TQColor(yellow));
        prioColors.insert(4, TQColor(red));
    }
    TQMap<int, TQColor> prioColors;
    TQMap<int, int> columnPrio;
};

class AppointmentItem : public TDEListViewItem {
public:
    AppointmentItem(Appointment *a, TQDate &d, TQListViewItem *parent)
        : TDEListViewItem(parent),
        appointment(a),
        date(d) {}

    Appointment *appointment;
    TQDate date;
};

TQSize ResourceView::sizeHint() const {
    return minimumSizeHint(); // HACK: koshell splitter minimumSize problem
}

ResourceView::ResourceView(View *view, TQWidget *parent)
    : TQSplitter(parent, "Resource view"),
    m_mainview(view),
    m_selectedItem(0),
    m_currentNode(0)
{
    setOrientation(Qt::Vertical);

    resList = new ResListView(this, "Resource list");
    resList->setItemMargin(2);
#if KDE_IS_VERSION(3,3,9)
    resList->setShadeSortColumn(false);
#endif
    resList->setRootIsDecorated(true);
    resList->addColumn(i18n("Name"));
    resList->setColumnAlignment(1, AlignHCenter);
    resList->addColumn(i18n("Type"));
    resList->setColumnAlignment(2, AlignHCenter);
    resList->addColumn(i18n("Initials"));
    resList->setColumnAlignment(3, AlignLeft);
    resList->addColumn(i18n("Email"));
    resList->setColumnAlignment(4, AlignHCenter);
    resList->addColumn(i18n("Calendar Name"));
    resList->setColumnAlignment(5, AlignRight);
    resList->addColumn(i18n("Available From"));
    resList->setColumnAlignment(6, AlignRight);
    resList->addColumn(i18n("Available Until"));
    resList->setColumnAlignment(7, AlignRight);
    resList->addColumn(i18n("%"));
    resList->setColumnAlignment(8, AlignRight);
    resList->addColumn(i18n("Normal Rate"));
    resList->setColumnAlignment(9, AlignRight);
    resList->addColumn(i18n("Overtime Rate"));

    m_showAppointments = false;
    m_appview = new ResourceAppointmentsView(view, this);
    m_appview->hide();
    draw(view->getProject());

    //connect(resList, TQT_SIGNAL(selectionChanged(TQListViewItem*)), TQT_SLOT(resSelectionChanged(TQListViewItem*)));
    connect(resList, TQT_SIGNAL(selectionChanged()), TQT_SLOT(resSelectionChanged()));
    connect(resList, TQT_SIGNAL( contextMenuRequested(TQListViewItem*, const TQPoint&, int)), TQT_SLOT(popupMenuRequested(TQListViewItem*, const TQPoint&, int)));
    //NOTE: using doubleClicked, not executed() to be consistent with ganttview
    connect(resList, TQT_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint&, int)), TQT_SLOT(slotItemDoubleClicked(TQListViewItem*)));

}

void ResourceView::zoom(double /*zoom*/)
{
}

Resource *ResourceView::currentResource() {
    if (m_selectedItem)
        return m_selectedItem->resource;
    return 0;
}

void ResourceView::draw(Project &project)
{
    //kdDebug()<<k_funcinfo<<endl;
    resList->clear();
    m_appview->clear();
    m_selectedItem = 0;

    TQPtrListIterator<ResourceGroup> it(project.resourceGroups());
    for (; it.current(); ++it) {
        TDEListViewItem *item = new TDEListViewItem(resList, it.current()->name());
        item->setOpen(true);
        drawResources(project, item, it.current());
    }
    if (m_selectedItem) {
        resList->setSelected(m_selectedItem, true);
    } else {
        resSelectionChanged(m_selectedItem);
    }
}


void ResourceView::drawResources(const Project &proj, TQListViewItem *parent, ResourceGroup *group)
{
    //kdDebug()<<k_funcinfo<<"group: "<<group->name()<<" ("<<group<<")"<<endl;
    TQPtrListIterator<Resource> it(group->resources());
    for (; it.current(); ++it) {
        Resource *r = it.current();
        ResourceItemPrivate *item = new ResourceItemPrivate(r, parent);
        // set column colors
        item->setColumnState(0, 0);
        item->setColumnState(4, 0);
        item->setColumnState(5, 0);
        item->setColumnState(6, 0);
        item->setColumnState(7, 0);
        if (r->calendar() == 0) {
            item->setColumnState(0, 1);
            item->setColumnState(4, 1);
        }
        if (proj.constraint() == Node::MustFinishOn) {
            if (proj.mustFinishOn() <= r->availableFrom()) {
                item->setColumnState(0, 1);
                item->setColumnState(5, 1);
            }
        } else {
            if (proj.mustStartOn() >= r->availableUntil()) {
                item->setColumnState(0, 1);
                item->setColumnState(6, 1);
            }
        }
        if (r->units() == 0) {
            item->setColumnState(0, 1);
            item->setColumnState(7, 1);
        }
        // and the texts
        item->setText(0, r->name()); // refresh
        switch (r->type()) {
            case Resource::Type_Work:
                item->setText(1, i18n("Work"));
                break;
            case Resource::Type_Material:
                item->setText(1, i18n("Material"));
                break;
            default:
                item->setText(1, i18n("Undefined"));
                break;
        }
        item->setText(2, r->initials());
        item->setText(3, r->email());
        item->setText(4, r->calendar() ? r->calendar()->name() : i18n("None"));
        item->setText(5, TDEGlobal::locale()->formatDateTime(r->availableFrom()));
        item->setText(6, TDEGlobal::locale()->formatDateTime(r->availableUntil()));
        item->setText(7, TQString().setNum(r->units()));
        item->setText(8, TDEGlobal::locale()->formatMoney(r->normalRate()));
        item->setText(9, TDEGlobal::locale()->formatMoney(r->overtimeRate()));
        if (!m_selectedItem) {
            m_selectedItem = item;
        }
    }
}


void ResourceView::resSelectionChanged() {
    //kdDebug()<<k_funcinfo<<endl;
    resSelectionChanged(resList->selectedItem());
}

void ResourceView::resSelectionChanged(TQListViewItem *item) {
    //kdDebug()<<k_funcinfo<<item<<endl;
    ResourceItemPrivate *ritem = dynamic_cast<ResourceItemPrivate *>(item);
    if (ritem) {
        m_selectedItem = ritem;
        if (m_showAppointments) {
            m_appview->show();
            m_appview->draw(ritem->resource, m_mainview->getProject().startTime().date(), m_mainview->getProject().endTime().date());
        } else {
            m_appview->hide();
        }
        return;
    }
    m_selectedItem = 0;
    m_appview->clear();
}


void ResourceView::slotItemDoubleClicked(TQListViewItem*) {
    emit itemDoubleClicked();
}

void ResourceView::popupMenuRequested(TQListViewItem* item, const TQPoint & pos, int)
{
    ResourceItemPrivate *ritem = dynamic_cast<ResourceItemPrivate *>(item);
    if (ritem) {
        if (ritem != m_selectedItem)
            resSelectionChanged(ritem);
        TQPopupMenu *menu = m_mainview->popupMenu("resource_popup");
        if (menu)
        {
            menu->exec(pos);
            //kdDebug()<<k_funcinfo<<"id="<<id<<endl;
        }
        else
            kdDebug()<<k_funcinfo<<"No menu!"<<endl;
    }
}

TQValueList<int> ResourceView::listOffsets(int pageHeight) const {
    TQValueList<int> lst;
    int hh = resList->headerHeight();
    int ph = pageHeight-hh;
    int lh = resList->contentsHeight() - hh; // list height ex header.
    int ly = 0;
    kdDebug()<<k_funcinfo<<ly<<", "<<lh<<endl;
    while (ly < lh) {
        lst << ly;
        ly = resList->calculateY(ly+1, ly+ph); // offset into the list, ex header
        //kdDebug()<<k_funcinfo<<ly<<", "<<lh<<endl;
    }
    return lst;
}

void ResourceView::print(KPrinter &printer) {
    //kdDebug()<<k_funcinfo<<endl;
    TQPaintDeviceMetrics m = TQPaintDeviceMetrics ( &printer );
    uint top, left, bottom, right;
    printer.margins(&top, &left, &bottom, &right);
    //kdDebug()<<m.width()<<"x"<<m.height()<<" : "<<top<<", "<<left<<", "<<bottom<<", "<<right<<" : "<<size()<<endl;
    TQPainter p;
    p.begin(&printer);
    p.setViewport(left, top, m.width()-left-right, m.height()-top-bottom);
    p.setClipRect(left, top, m.width()-left-right, m.height()-top-bottom);
    TQRect preg = p.clipRegion(TQPainter::CoordPainter).boundingRect();
    //kdDebug()<<"p="<<preg<<endl;
    //p.drawRect(preg.x(), preg.y(), preg.width()-1, preg.height()-1);
    int ch = resList->contentsHeight();
    int cw = resList->contentsWidth();
    double scale = (double)preg.width()/(double)(cw);
    //kdDebug()<<"scale="<<scale<<endl;
    if (scale < 1.0) {
        p.scale(scale, scale);
    }
    int ph = preg.height()-resList->headerHeight();
    TQValueList<int> lst = listOffsets(preg.height());
    for (int i=0; i < lst.count(); ++i) {
        //kdDebug()<<"Page "<<i+1<<": "<<"scale="<<scale<<" "<<lst[i]<<" : "<<cw<<"x"<<ch<<endl;
        if (i > 0) {
            printer.newPage();
        }
        resList->paintToPrinter(&p, 0, lst[i], cw, ph);
    }
    
    p.end();
}

bool ResourceView::setContext(Context::Resourceview &/*context*/) {
    //kdDebug()<<k_funcinfo<<endl;
    return true;
}

void ResourceView::getContext(Context::Resourceview &/*context*/) const {
    //kdDebug()<<k_funcinfo<<endl;
}


}  //KPlato namespace

#include "kptresourceview.moc"