File: annotateview.cpp

package info (click to toggle)
cervisia 4%3A16.08.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,892 kB
  • sloc: cpp: 14,347; xml: 371; sh: 3; makefile: 1
file content (298 lines) | stat: -rw-r--r-- 8,586 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 1999-2002 Bernd Gehrmann <bernd@mail.berlios.de>
 * Copyright (c) 2003-2008 André Wöbbeking <Woebbeking@kde.org>
 * Copyright (c) 2015 Martin Koller <kollix@aon.at>
 *
 * 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 "annotateview.h"

#include <QHeaderView>
#include <qpainter.h>
#include <kcolorscheme.h>
#include <QApplication>

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


using namespace Cervisia;


class AnnotateViewItem : public QTreeWidgetItem
{
public:
    enum { LineNumberColumn, AuthorColumn, ContentColumn };

    AnnotateViewItem(AnnotateView *parent, const LogInfo& logInfo,
                     const QString &content, bool odd, int linenumber);

    int lineNumber() const { return m_lineNumber; }

    virtual QVariant data(int column, int role) const;

private:
    LogInfo m_logInfo;
    QString m_content;
    bool    m_odd;
    int     m_lineNumber;

    friend class AnnotateView;
    friend class AnnotateViewDelegate;
};


AnnotateViewItem::AnnotateViewItem(AnnotateView *parent, const LogInfo& logInfo,
                                   const QString &content, bool odd, int linenumber)
    : QTreeWidgetItem(parent)
    , m_logInfo(logInfo)
    , m_content(content)
    , m_odd(odd)
    , m_lineNumber(linenumber)
{}


QVariant AnnotateViewItem::data(int column, int role) const
{
    if ( role == Qt::DisplayRole )
    {
        switch (column)
        {
        case LineNumberColumn:
            return QString::number(m_lineNumber);
        case AuthorColumn:
            if( m_logInfo.m_author.isNull() )
                return QString();
            else
                return (m_logInfo.m_author + QChar(' ') + m_logInfo.m_revision);
        case ContentColumn:
            return m_content;
        default:
            ;
        };

        return QString();
    }

    return QTreeWidgetItem::data(column, role);
}

//---------------------------------------------------------------------

void AnnotateViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();

    AnnotateViewItem *item = static_cast<AnnotateViewItem *>(view->topLevelItem(index.row()));

    QColor backgroundColor;
    QColor foregroundColor;

    if ( item->isSelected() || index.column() == AnnotateViewItem::LineNumberColumn )
    {
        backgroundColor = KColorScheme(QPalette::Active, KColorScheme::Selection).background().color();
        foregroundColor = KColorScheme(QPalette::Active, KColorScheme::Selection).foreground().color();
    }
    else
    {
        backgroundColor = item->m_odd
            ? KColorScheme(QPalette::Active, KColorScheme::View).background().color()
            : KColorScheme(QPalette::Active, KColorScheme::View).background(KColorScheme::AlternateBackground).color();
        foregroundColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
    }

    painter->setPen(foregroundColor);
    painter->fillRect(option.rect, backgroundColor);

    QString str = item->text(index.column());
    if (str.isEmpty())
    {
        painter->restore();
        return;
    }

    Qt::Alignment align = (index.column() == AnnotateViewItem::LineNumberColumn) ? Qt::AlignRight : option.displayAlignment;

    if ((align & (Qt::AlignTop | Qt::AlignBottom)) == 0)
            align |= Qt::AlignVCenter;

    // only the content shall use the configured font, others use default
    // reason: usually you want fixed-width font in sourcecode, but line numbers and revision column look
    // nicer when they are not fixed-width
    if ( index.column() == AnnotateViewItem::ContentColumn )
        painter->setFont(view->font());
    else
        painter->setFont(QApplication::font());

    painter->drawText(option.rect.x() + BORDER, option.rect.y(), option.rect.width() - 2*BORDER, option.rect.height(), align, str);

    painter->restore();
}


QSize AnnotateViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem opt(option);

    if ( index.column() == AnnotateViewItem::ContentColumn )
        opt.font = view->font();
    else
        opt.font = QApplication::font();

    QSize s = QStyledItemDelegate::sizeHint(opt, index);
    s.setWidth(s.width() + 2*BORDER);

    return s;
}

//---------------------------------------------------------------------

AnnotateView::AnnotateView(QWidget *parent)
    : QTreeWidget(parent)
{
    setItemDelegate(new AnnotateViewDelegate(this));

    setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    setAllColumnsShowFocus(true);
    setRootIsDecorated(false);
    setAutoScroll(false);
    setSelectionMode(QAbstractItemView::SingleSelection); // to be able to show the found item
    header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    header()->setStretchLastSection(false);
    header()->hide();
    setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);

    setColumnCount(3);

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

    connect(toolTip, SIGNAL(queryToolTip(QPoint,QRect&,QString&)),
            this, SLOT(slotQueryToolTip(QPoint,QRect&,QString&)));

    configChanged();

    connect(CervisiaSettings::self(), SIGNAL(configChanged()),
            this, SLOT(configChanged()));
}



void AnnotateView::addLine(const LogInfo& logInfo, const QString& content, bool odd)
{
    new AnnotateViewItem(this, logInfo, content, odd, topLevelItemCount()+1);
}


QSize AnnotateView::sizeHint() const
{
    QFontMetrics fm(fontMetrics());
    return QSize(100 * fm.width("0"), 10 * fm.lineSpacing());
}


void AnnotateView::configChanged()
{
    setFont(CervisiaSettings::annotateFont());
}


void AnnotateView::slotQueryToolTip(const QPoint& viewportPos,
                                    QRect&        viewportRect,
                                    QString&      text)
{
    if (const AnnotateViewItem* item = static_cast<AnnotateViewItem*>(itemAt(viewportPos)))
    {
        const int column(indexAt(viewportPos).column());
        if ((column == AnnotateViewItem::AuthorColumn) && !item->m_logInfo.m_author.isNull())
        {
            viewportRect = visualRect(indexAt(viewportPos));
            text = item->m_logInfo.createToolTipText(false);
        }
    }
}

void AnnotateView::findText(const QString &textToFind, bool up)
{
    QTreeWidgetItem *item = currentItem();
    if ( !item )
    {
        if ( up )
            item = topLevelItem(topLevelItemCount() - 1);  // last
        else
            item = topLevelItem(0);  // first
    }
    else
    {
        if ( up )
            item = itemAbove(item);
        else
            item = itemBelow(item);
    }

    for (; item && !item->text(AnnotateViewItem::ContentColumn).contains(textToFind, Qt::CaseInsensitive);
         item = up ? itemAbove(item) : itemBelow(item))
      ;

    setCurrentItem(item);

    if ( item )
    {
        item->setSelected(true);
        scrollToItem(item);
    }
}

int AnnotateView::currentLine() const
{
    QTreeWidgetItem *item = currentItem();
    if ( !item )
        return -1;

    return static_cast<AnnotateViewItem*>(item)->lineNumber();
}

int AnnotateView::lastLine() const
{
    AnnotateViewItem *item = static_cast<AnnotateViewItem*>(topLevelItem(topLevelItemCount() - 1));
    if ( !item )
        return 0;

    return item->lineNumber();
}


void AnnotateView::gotoLine(int line)
{
    for (AnnotateViewItem *item = static_cast<AnnotateViewItem*>(topLevelItem(0));
         item;
         item = static_cast<AnnotateViewItem*>(itemBelow(item)))
    {
        if ( item->lineNumber() == line )
        {
            setCurrentItem(item);
            item->setSelected(true);
            scrollToItem(item);
            return;
        }
    }
}


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