summaryrefslogtreecommitdiffstats
path: root/cervisia/diffview.cpp
blob: d246ca7b76c5e19e98888857b78b0ec09d42073d (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
/*
 *  Copyright (C) 1999-2002 Bernd Gehrmann
 *                          bernd@mail.berlios.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 "diffview.h"

#include <tqpainter.h>
#include <tqscrollbar.h>
#include <tqpixmap.h>
#include <tqregexp.h>
#include <tqstyle.h>

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeglobalsettings.h>
#include <tdelocale.h>


class DiffViewItem
{
public:
    TQString line;
    DiffView::DiffType type;
    bool inverted;
    int no;
};


int DiffViewItemList::compareItems(TQPtrCollection::Item item1, TQPtrCollection::Item item2)
{
    return (static_cast<DiffViewItem*>(item1)->no
            == static_cast<DiffViewItem*>(item2)->no)? 0 : 1;
}


const int DiffView::BORDER = 7;


DiffView::DiffView( TDEConfig& cfg, bool withlinenos, bool withmarker,
                    TQWidget *parent, const char *name )
    : QtTableView(parent, name, WRepaintNoErase)
    , partConfig(cfg)
{
    setNumRows(0);
    setNumCols( 1 + (withlinenos?1:0) + (withmarker?1:0) );
    setTableFlags( Tbl_autoVScrollBar|Tbl_autoHScrollBar|
                   Tbl_smoothVScrolling );
    setFrameStyle( TQFrame::WinPanel | TQFrame::Sunken );
    setBackgroundMode( PaletteBase );
    setWFlags( WResizeNoErase );

    partConfig.setGroup("LookAndFeel");
    setFont(partConfig.readFontEntry("DiffFont"));
    TQFontMetrics fm(font());
    setCellHeight(fm.lineSpacing());
    setCellWidth(0);
    textwidth = 0;

    partConfig.setGroup("General");
    m_tabWidth = partConfig.readNumEntry("TabWidth", 8);

    items.setAutoDelete(true);
    linenos = withlinenos;
    marker = withmarker;

    partConfig.setGroup("Colors");
    TQColor defaultColor=TQColor(237, 190, 190);
    diffChangeColor=partConfig.readColorEntry("DiffChange",&defaultColor);
    defaultColor=TQColor(190, 190, 237);
    diffInsertColor=partConfig.readColorEntry("DiffInsert",&defaultColor);
    defaultColor=TQColor(190, 237, 190);
    diffDeleteColor=partConfig.readColorEntry("DiffDelete",&defaultColor);
}


void DiffView::setFont(const TQFont &font)
{
    QtTableView::setFont(font);
    TQFontMetrics fm(font);
    setCellHeight(fm.lineSpacing());
}


void DiffView::setPartner(DiffView *other)
{
    partner = other;
    if (partner)
    {
        connect( verticalScrollBar(), TQT_SIGNAL(valueChanged(int)),
                 TQT_SLOT(vertPositionChanged(int)) );
        connect( verticalScrollBar(), TQT_SIGNAL(sliderMoved(int)),
                 TQT_SLOT(vertPositionChanged(int)) );
        connect( horizontalScrollBar(), TQT_SIGNAL(valueChanged(int)),
                 TQT_SLOT(horzPositionChanged(int)) );
        connect( horizontalScrollBar(), TQT_SIGNAL(sliderMoved(int)),
                 TQT_SLOT(horzPositionChanged(int)) );
    }
}


void DiffView::vertPositionChanged(int val)
{
    if (partner)
        partner->setYOffset(TQMIN(val,partner->maxYOffset()));
}


void DiffView::horzPositionChanged(int val)
{
    if (partner)
        partner->setXOffset(TQMIN(val,partner->maxXOffset()));
}


// *offset methods are only for views withlineno
void DiffView::removeAtOffset(int offset)
{
    items.remove(offset);
    setNumRows(numRows()-1);
}


void DiffView::insertAtOffset(const TQString &line, DiffType type, int offset)
{
    DiffViewItem *item = new DiffViewItem;
    item->line = line;
    item->type = type;
    item->no = -1;
    item->inverted = false;
    items.insert(offset, item);
    setNumRows(numRows()+1);
}


void DiffView::setCenterOffset(int offset)
{
    if (!rowIsVisible(offset))
    {
        int visiblerows = viewHeight()/cellHeight(0);
        setTopCell( TQMAX(0, offset - visiblerows/2) );
    }
}


void DiffView::addLine(const TQString &line, DiffType type, int no)
{
    TQFont f(font());
    f.setBold(true);
    TQFontMetrics fmbold(f);
    TQFontMetrics fm(font());


    // calculate textwidth based on 'line' where tabs are expanded
    //
    // *Please note*
    // For some fonts, e.g. "Clean", is fm.maxWidth() greater than
    // fmbold.maxWidth().
    TQString copy(line);
    const int numTabs = copy.contains('\t', false);
    copy.replace( TQRegExp("\t"), "");

    const int tabSize   = m_tabWidth * TQMAX(fm.maxWidth(), fmbold.maxWidth());
    const int copyWidth = TQMAX(fm.width(copy), fmbold.width(copy));
    textwidth = TQMAX(textwidth, copyWidth + numTabs * tabSize);

    DiffViewItem *item = new DiffViewItem;
    item->line = line;
    item->type = type;
    item->no = no;
    item->inverted = false;
    items.append(item);
    setNumRows(numRows()+1);
}


TQString DiffView::stringAtOffset(int offset)
{
    if (offset >= (int)items.count())
    {
        kdDebug(8050) << "Internal error: lineAtOffset" << endl;
    }
    return items.at(offset)->line;
}


int DiffView::count()
{
    return items.count();
}


int DiffView::findLine(int lineno)
{
    int offset;
    DiffViewItem tmp;
    tmp.no = lineno;
    if ( (offset = items.find(&tmp)) == -1)
    {
        kdDebug(8050) << "Internal Error: Line " << lineno << " not found" << endl;
        return -1;
    }
    return offset;
}


void DiffView::setInverted(int lineno, bool inverted)
{
    int offset;
    if ( (offset = findLine(lineno)) != -1)
        items.at(offset)->inverted = inverted;
}


void DiffView::setCenterLine(int lineno)
{
    int offset;
    if ( (offset = findLine(lineno)) != -1)
        setCenterOffset(offset);
}


TQString DiffView::stringAtLine(int lineno)
{
    int pos;
    if ( (pos = findLine(lineno)) != -1 )
        return items.at(pos)->line;
    else
        return TQString();
}


TQByteArray DiffView::compressedContent()
{
    TQByteArray res(items.count());

    TQPtrListIterator<DiffViewItem> it(items);
    int i=0;
    for (; it.current(); ++it)
    {
        switch (it.current()->type)
        {
            case Change:   res[i] = 'C'; break;
            case Insert:   res[i] = 'I'; break;
            case Delete:   res[i] = 'D'; break;
            case Neutral:  res[i] = 'N'; break;
            case Unchanged:res[i] = 'U'; break;
            default:       res[i] = ' ';
        }
        ++i;
    }
    return res;
}


int DiffView::cellWidth(int col)
{
    if (col == 0 && linenos)
    {
        TQFontMetrics fm(font());
        return fm.width("10000");
    }
    else if (marker && (col == 0 || col == 1))
    {
        TQFontMetrics fm( fontMetrics() );
        return TQMAX(TQMAX( fm.width(i18n("Delete")),
                          fm.width(i18n("Insert"))),
                    fm.width(i18n("Change")))+2*BORDER;
    }
    else
    {
        int rest = (linenos || marker)? cellWidth(0) : 0;
        if (linenos && marker)
            rest += cellWidth(1);
        return TQMAX(textwidth, viewWidth()-rest);
    }
}


TQSize DiffView::sizeHint() const
{
    TQFontMetrics fm(font());
    return TQSize( 4*fm.width("0123456789"), fm.lineSpacing()*8 );
}


void DiffView::paintCell(TQPainter *p, int row, int col)
{
    TQFontMetrics fm(font());
    p->setTabStops(m_tabWidth * fm.maxWidth());

    DiffViewItem *item = items.at(row);

    int width = cellWidth(col);
    int height = cellHeight();

    TQColor backgroundColor;
    bool inverted;
    int align;
    int innerborder;
    TQString str;

    TQFont oldFont(p->font());
    if (item->type==Separator)
    {
        backgroundColor = TDEGlobalSettings::highlightColor();
        p->setPen(TDEGlobalSettings::highlightedTextColor());
        inverted = false;
        align = AlignLeft;
        innerborder = 0;
        if (col == (linenos?1:0) + (marker?1:0))
            str = item->line;
        TQFont f(oldFont);
        f.setBold(true);
        p->setFont(f);
    }
    else if (col == 0 && linenos)
    {
        backgroundColor = TDEGlobalSettings::highlightColor();
        p->setPen(TDEGlobalSettings::highlightedTextColor());
        inverted = false;
        align = AlignLeft;
        innerborder = 0;
        if (item->no == -1)
            str = "+++++";
        else
            str.setNum(item->no);
    }
    else if (marker && (col == 0 || col == 1))
    {
        backgroundColor = TDEGlobalSettings::alternateBackgroundColor();
        p->setPen(TDEGlobalSettings::textColor());
        inverted = false;
        align = AlignRight;
        innerborder = BORDER;
        str = (item->type==Change)? i18n("Change")
            : (item->type==Insert)? i18n("Insert")
            : (item->type==Delete)? i18n("Delete") : TQString();
    }
    else
    {
        backgroundColor =
            (item->type==Change)? diffChangeColor
            : (item->type==Insert)? diffInsertColor
            : (item->type==Delete)? diffDeleteColor
            : (item->type==Neutral)? TDEGlobalSettings::alternateBackgroundColor() : TDEGlobalSettings::baseColor();
        p->setPen(TDEGlobalSettings::textColor());
        inverted = item->inverted;
        align = AlignLeft;
        innerborder = 0;
        str = item->line;
    }

    if (inverted)
    {
        p->setPen(backgroundColor);
        backgroundColor = TDEGlobalSettings::textColor();
        TQFont f(oldFont);
        f.setBold(true);
        p->setFont(f);
    }

    p->fillRect(0, 0, width, height, backgroundColor);
    p->drawText(innerborder, 0, width-2*innerborder, height, align|ExpandTabs, str);
    p->setFont(oldFont);
}


void DiffView::wheelEvent(TQWheelEvent *e)
{
    TQApplication::sendEvent(verticalScrollBar(), e);
}


DiffZoomWidget::DiffZoomWidget(TDEConfig& cfg, TQWidget *parent, const char *name)
    : TQFrame(parent, name)
{
    setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Minimum ) );

    cfg.setGroup("Colors");
    TQColor defaultColor=TQColor(237, 190, 190);
    diffChangeColor=cfg.readColorEntry("DiffChange",&defaultColor);
    defaultColor=TQColor(190, 190, 237);
    diffInsertColor=cfg.readColorEntry("DiffInsert",&defaultColor);
    defaultColor=TQColor(190, 237, 190);
    diffDeleteColor=cfg.readColorEntry("DiffDelete",&defaultColor);
}


DiffZoomWidget::~DiffZoomWidget()
{}


void DiffZoomWidget::setDiffView(DiffView *view)
{
    diffview = view;
    TQScrollBar *sb = const_cast<TQScrollBar*>(diffview->scrollBar());
    sb->installEventFilter(this);
}


TQSize DiffZoomWidget::sizeHint() const
{
    return TQSize(25, style().pixelMetric(TQStyle::PM_ScrollBarExtent, this));
}


bool DiffZoomWidget::eventFilter(TQObject *o, TQEvent *e)
{
    if (e->type() == TQEvent::Show
        || e->type() == TQEvent::Hide
        || e->type() == TQEvent::Resize)
        repaint();

    return TQFrame::eventFilter(o, e);
}


void DiffZoomWidget::paintEvent(TQPaintEvent *)
{
    const TQScrollBar* scrollBar = diffview->scrollBar();
    if (!scrollBar)
        return;

    // only y and height are important
    const TQRect scrollBarGroove(scrollBar->isVisible()
                                ? style().querySubControlMetrics(TQStyle::CC_ScrollBar,
                                                                 scrollBar,
                                                                 TQStyle::SC_ScrollBarGroove)
                                : rect());

    // draw rectangles at the positions of the differences

    const TQByteArray& lineTypes(diffview->compressedContent());

    TQPixmap pixbuf(width(), scrollBarGroove.height());
    pixbuf.fill(TDEGlobalSettings::baseColor());

    TQPainter p(&pixbuf, this);
    if (const unsigned int numberOfLines = lineTypes.size())
    {
        const double scale(((double) scrollBarGroove.height()) / numberOfLines);
        for (unsigned int index(0); index < numberOfLines;)
        {
            const char lineType(lineTypes[index]);

            // don't use tqRound() to avoid painting outside of the pixmap
            // (yPos1 must be lesser than scrollBarGroove.height())
            const int yPos1(static_cast<int>(index * scale));

            // search next line with different lineType
            for (++index; index < numberOfLines && lineType == lineTypes[index]; ++index)
                ;

            TQColor color;
            switch (lineType)
            {
            case 'C':
                color = diffChangeColor;
                break;
            case 'I':
                color = diffInsertColor;
                break;
            case 'D':
                color = diffDeleteColor;
                break;
            case ' ':
            case 'N':
                color = TDEGlobalSettings::alternateBackgroundColor();
                break;
            }

            if (color.isValid())
            {
                const int yPos2(tqRound(index * scale));
                const int areaHeight((yPos2 != yPos1) ? yPos2 - yPos1 : 1);

                p.fillRect(0, yPos1, pixbuf.width(), areaHeight, TQBrush(color));
            }
        }
    }
    p.flush();
    bitBlt(this, 0, scrollBarGroove.y(), &pixbuf);
}

#include "diffview.moc"