summaryrefslogtreecommitdiffstats
path: root/cervisia/logtree.cpp
blob: 9ae6593036488056e53a58b055646dea9feb7d25 (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
/*
 *  Copyright (C) 1999-2002 Bernd Gehrmann
 *                          bernd@mail.berlios.de
 *  Copyright (c) 2003-2004 Christian Loose <christian.loose@hamburg.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "logtree.h"

#include <tqpainter.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kglobalsettings.h>

#include "loginfo.h"
#include "tooltip.h"


const int LogTreeView::BORDER = 8;
const int LogTreeView::INSPACE = 3;

namespace
{
    bool static_initialized = false;
    int static_width;
    int static_height;
}

class LogTreeItem
{
public:
    Cervisia::LogInfo m_logInfo;
    TQString branchpoint;
    bool firstonbranch;
    int row;
    int col;
    bool selected;
};


class LogTreeConnection
{
public:
    LogTreeItem *start;
    LogTreeItem *end;
};


LogTreeView::LogTreeView(TQWidget *tqparent, const char *name)
    : TQTable(tqparent, name)
{
    if (!static_initialized)
    {
        static_initialized = true;
        TQFontMetrics fm( fontMetrics() );
        static_width = fm.width("1234567890") + 2*BORDER + 2*INSPACE;
        static_height = 2*fm.height() + 2*BORDER + 3*INSPACE;
    }

    setNumCols(0);
    setNumRows(0);
    setReadOnly(true);
    setFocusStyle(TQTable::FollowStyle);
    setSelectionMode(TQTable::NoSelection);
    setShowGrid(false);
    horizontalHeader()->hide();
    setTopMargin(0);
    verticalHeader()->hide();
    setLeftMargin(0);
    setFrameStyle( TQFrame::WinPanel | TQFrame::Sunken );
    setBackgroundMode(PaletteBase);
    setFocusPolicy(TQ_NoFocus);

    currentRow = -1;
    currentCol = -1;

    items.setAutoDelete(true);
    connections.setAutoDelete(true);

    Cervisia::ToolTip* toolTip = new Cervisia::ToolTip(viewport());

    connect(toolTip, TQT_SIGNAL(queryToolTip(const TQPoint&, TQRect&, TQString&)),
            this, TQT_SLOT(slotQueryToolTip(const TQPoint&, TQRect&, TQString&)));
}


void LogTreeView::addRevision(const Cervisia::LogInfo& logInfo)
{
    TQString branchpoint, branchrev;

    const TQString rev(logInfo.m_revision);

    // find branch
    int pos1, pos2;
    if ((pos2 = rev.tqfindRev('.')) > 0 &&
        (pos1 = rev.tqfindRev('.', pos2-1)) > 0)
    {
        // e. g. for rev = 1.1.2.3 we have
        // branchrev = 1.1.2, branchpoint = 1.1
        branchrev = rev.left(pos2);
        branchpoint = rev.left(pos1);
    }

    if (branchrev.isEmpty())
    {
        // Most probably we are on the trunk
        setNumRows(numRows()+1);
        setNumCols(1);
        LogTreeItem *item = new LogTreeItem;
        item->m_logInfo = logInfo;
        item->branchpoint = branchpoint;
        item->firstonbranch = false;
        item->row = numRows()-1;
        item->col = 0;
        item->selected = false;
        items.append(item);
        return;
    }

    // look whether we have revisions on this branch
    // shift them up
    int row=-1, col=-1;
    TQPtrListIterator<LogTreeItem> it(items);
    for (; it.current(); ++it)
    {
        if (branchrev == (it.current()->m_logInfo.m_revision).left(branchrev.length()))
        {
            it.current()->firstonbranch = false;
            row = it.current()->row;
            col = it.current()->col;
            it.current()->row--;
            // Are we at the top of the widget?
            if (row == 0)
            {
                TQPtrListIterator<LogTreeItem> it2(items);
                for (; it2.current(); ++it2)
                    it2.current()->row++;
                setNumRows(numRows()+1);
                row = 1;
            }
        }
    }

    if (row == -1)
    {
        // Ok, so we must open a new branch
        // Let's find the branch point
        TQPtrListIterator<LogTreeItem> it3(items);
        for (it3.toLast(); it3.current(); --it3)
        {
            if (branchpoint == it3.current()->m_logInfo.m_revision)
            {
                // Move existing branches to the right
                TQPtrListIterator<LogTreeItem> it4(items);
                for (; it4.current(); ++it4)
                    if (it4.current()->col > it3.current()->col)
                    {
                        it4.current()->col++;
                    }
                setNumCols(numCols()+1);
                row = it3.current()->row-1;
                col = it3.current()->col+1;
                if (row == -1)
                {
                    TQPtrListIterator<LogTreeItem> it5(items);
                    for (; it5.current(); ++it5)
                        it5.current()->row++;
                    setNumRows(numRows()+1);
                    row = 0;
                }
                break;
            }
        }
    }

    LogTreeItem *item = new LogTreeItem;
    item->m_logInfo = logInfo;
    item->branchpoint = branchpoint;
    item->firstonbranch = true;
    item->row = row;
    item->col = col;
    item->selected = false;
    items.append(item);

#if 0
    cout << "Dump: " << endl;
    cout << "Rows: " << numRows() << "Cols: " << numCols() << endl;
    TQPtrListIterator<LogTreeItem> it5(items);
    for (; it5.current(); ++it5)
    {
        cout << "Rev: "<< it5.current()->rev << endl;
        cout << "row: "<< it5.current()->row << ", col: " << it5.current()->col << endl;
        cout << "fob: "<< it5.current()->firstonbranch << endl;
    }
    cout << "End Dump" << endl;
#endif

}


void LogTreeView::collectConnections()
{
    TQPtrListIterator<LogTreeItem> it(items);
    for (; it.current(); ++it)
    {
        TQString rev = it.current()->m_logInfo.m_revision;

        TQPtrListIterator<LogTreeItem> it2(items);
        for (it2=it,++it2; it2.current(); ++it2)
            if (it2.current()->branchpoint == rev &&
                it2.current()->firstonbranch)
            {
                LogTreeConnection *conn = new LogTreeConnection;
                conn->start = it.current();
                conn->end = it2.current();
                connections.append(conn);
            }
    }
}


void LogTreeView::setSelectedPair(TQString selectionA, TQString selectionB)
{
    TQPtrListIterator<LogTreeItem> it(items);
    for(; it.current(); ++it)
    {
        bool oldstate = it.current()->selected;
        bool newstate = ( selectionA == it.current()->m_logInfo.m_revision ||
                          selectionB == it.current()->m_logInfo.m_revision );
        if (oldstate != newstate)
        {
            it.current()->selected = newstate;
            tqrepaint(false);
        }
    }
}


TQSize LogTreeView::tqsizeHint() const
{
    return TQSize(2 * static_width, 3 * static_height);
}


TQString LogTreeView::text(int row, int col) const
{
    LogTreeItem* item = 0;
    
    TQPtrListIterator<LogTreeItem> it(items);
    for( ; it.current(); ++it )
    {
        if( it.current()->col == col && it.current()->row == row )
        {
            item = it.current();
            break;
        }
    }
    
    TQString text;
    
    if( item && !item->m_logInfo.m_author.isNull() )
        text = item->m_logInfo.createToolTipText();
        
    return text;
}


void LogTreeView::paintCell(TQPainter *p, int row, int col, const TQRect& cr,
                            bool selected, const TQColorGroup& cg)
{
    Q_UNUSED(selected)
    Q_UNUSED(cr)
    bool followed, branched;
    LogTreeItem *item;

    branched = false;
    followed = false;
    item = 0;

    TQPtrListIterator<LogTreeItem> it(items);
    for(; it.current(); ++it)
    {
        int itcol = it.current()->col;
        int itrow = it.current()->row;
        if (itrow == row-1 && itcol == col)
            followed = true;
        if (itrow == row && itcol == col)
            item = it.current();
    }
    TQPtrListIterator<LogTreeConnection> it2(connections);
    for (; it2.current(); ++it2)
    {
        int itcol1 = it2.current()->start->col;
        int itcol2 = it2.current()->end->col;
        int itrow = it2.current()->start->row;
        if (itrow == row && itcol1 <= col && itcol2 > col)
            branched = true;
    }

    p->fillRect(0, 0, columnWidth(col), rowHeight(row),
                cg.base());
    p->setPen(cg.text());
    if (item)
        paintRevisionCell(p, row, col, item->m_logInfo,
                          followed, branched, item->selected);
    else if (followed || branched)
        paintConnector(p, row, col, followed, branched);
}


void LogTreeView::paintConnector(TQPainter *p,
                                 int row, int col, bool followed, bool branched)
{
    const int midx = columnWidth(col) / 2;
    const int midy = rowHeight(row) / 2;

    p->drawLine(0, midy, branched ? columnWidth(col) : midx, midy);
    if (followed)
        p->drawLine(midx, midy, midx, 0);
}


TQSize LogTreeView::computeSize(const Cervisia::LogInfo& logInfo,
                               int*                     authorHeight,
                               int*                     tagsHeight) const
{
    const TQFontMetrics fm(fontMetrics());

    const TQString tags(logInfo.tagsToString(Cervisia::TagInfo::Branch | Cervisia::TagInfo::Tag,
                                            Cervisia::TagInfo::Branch));

    const TQSize r1 = fm.size(AlignCenter, logInfo.m_revision);
    const TQSize r3 = fm.size(AlignCenter, logInfo.m_author);

    if (authorHeight)
        *authorHeight = r3.height();

    int infoWidth = kMax(static_width - 2 * BORDER, kMax(r1.width(), r3.width()));
    int infoHeight = r1.height() + r3.height() + 3 * INSPACE;

    if (!tags.isEmpty())
    {
        const TQSize r2 = fm.size(AlignCenter, tags);
        infoWidth = kMax(infoWidth, r2.width());
        infoHeight += r2.height() + INSPACE;
        if (tagsHeight)
            *tagsHeight = r2.height();
    }
    else
    {
        if (tagsHeight)
            *tagsHeight = 0;
    }
    infoWidth += 2 * INSPACE;

    return TQSize(infoWidth, infoHeight);
}


void LogTreeView::paintRevisionCell(TQPainter *p,
                                    int row, int col,
                                    const Cervisia::LogInfo& logInfo,
                                    bool followed, bool branched, bool selected)
{
    int authorHeight;
    int tagsHeight;
    const TQSize infoSize(computeSize(logInfo, &authorHeight, &tagsHeight));
    const TQSize cellSize(columnWidth(col), rowHeight(row));

    const int midx(cellSize.width() / 2);
    const int midy(cellSize.height() / 2);

    TQRect rect(TQPoint((cellSize.width() - infoSize.width()) / 2,
                      (cellSize.height() - infoSize.height()) / 2),
               infoSize);

    // Connectors
    if (followed)
        p->drawLine(midx, 0, midx, rect.y()); // to the top

    if (branched)
        p->drawLine(rect.x() + infoSize.width(), midy, cellSize.width(), midy); // to the right

    p->drawLine(midx, rect.y() + infoSize.height(), midx, cellSize.height()); // to the bottom

    // The box itself
    if (selected)
    {
        p->fillRect(rect, KGlobalSettings::highlightColor());
        p->setPen(KGlobalSettings::highlightedTextColor());
    }
    else
    {
        p->drawRoundRect(rect, 10, 10);
    }

    rect.setY(rect.y() + INSPACE);

    p->drawText(rect, AlignHCenter, logInfo.m_author);
    rect.setY(rect.y() + authorHeight + INSPACE);

    const TQString tags(logInfo.tagsToString(Cervisia::TagInfo::Branch | Cervisia::TagInfo::Tag,
                                            Cervisia::TagInfo::Branch));
    if (!tags.isEmpty())
    {
        const TQFont font(p->font());
        TQFont underline(font);
        underline.setUnderline(true);

        p->setFont(underline);
        p->drawText(rect, AlignHCenter, tags);
        p->setFont(font);

        rect.setY(rect.y() + tagsHeight + INSPACE);
    }

    p->drawText(rect, AlignHCenter, logInfo.m_revision);
}


void LogTreeView::contentsMousePressEvent(TQMouseEvent *e)
{
    if ( e->button() == Qt::MidButton ||
         e->button() == Qt::LeftButton)
    {
        int row = rowAt( e->pos().y() );
        int col = columnAt( e->pos().x() );

        TQPtrListIterator<LogTreeItem> it(items);
        for(; it.current(); ++it)
            if (it.current()->row == row
                && it.current()->col == col)
            {
                // Change selection for revision B if the middle mouse button or
                // the left mouse button with the control key was pressed
                bool changeRevB = (e->button() == Qt::MidButton) ||
                                  (e->button() == Qt::LeftButton &&
                                   e->state() & ControlButton);

                emit revisionClicked(it.current()->m_logInfo.m_revision, changeRevB);
                break;
            }
    }

    viewport()->update();
}


void LogTreeView::recomputeCellSizes ()
{
    // Compute maximum for each column and row
    for (TQPtrListIterator<LogTreeItem> it(items); it.current(); ++it)
    {
        const LogTreeItem *item = it.current();

        const TQSize cellSize(computeSize(item->m_logInfo) + TQSize(2 * BORDER,  2 * BORDER));

        setColumnWidth(item->col, kMax(columnWidth(item->col), cellSize.width()));
        setRowHeight(item->row, kMax(rowHeight(item->row), cellSize.height()));
    }

    viewport()->update();
}


void LogTreeView::slotQueryToolTip(const TQPoint& viewportPos,
                                   TQRect&        viewportRect,
                                   TQString&      tipText)
{
    const TQPoint contentsPos(viewportToContents(viewportPos));
    const int column(columnAt(contentsPos.x()));
    const int row(rowAt(contentsPos.y()));

    tipText = text(row, column);
    if (tipText.isEmpty())
        return;

    viewportRect = cellGeometry(row, column);
    viewportRect.moveTopLeft(contentsToViewport(viewportRect.topLeft()));
}


#include "logtree.moc"


// Local Variables:
// c-basic-offset: 4
// End: