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
|
/* This file is part of KDevelop
*
* Copyright 2017-2018 Friedrich W. H. Kossebau <kossebau@kde.org>
*
* 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 "vcsannotationitemdelegate.h"
#include <models/vcsannotationmodel.h>
#include <vcsannotation.h>
#include <debug.h>
#include <ktexteditor_version.h>
#include <KTextEditor/AnnotationInterface>
#include <KTextEditor/View>
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Attribute>
#include <KLocalizedString>
#include <QHelpEvent>
#include <QPainter>
#include <QAction>
#include <QMenu>
#include <QToolTip>
#include <QFontMetricsF>
#include <QDate>
#include <QStyle>
#include <QApplication>
#include <cmath>
using namespace KDevelop;
VcsAnnotationItemDelegate::VcsAnnotationItemDelegate(KTextEditor::View* view, KTextEditor::AnnotationModel* model,
QObject* parent)
: KTextEditor::AbstractAnnotationItemDelegate(parent)
, m_model(model)
{
// dump background brushes on schema change
Q_ASSERT(qobject_cast<KTextEditor::ConfigInterface*>(view));
#if KTEXTEDITOR_VERSION >= QT_VERSION_CHECK(5, 79, 0)
connect(view, &KTextEditor::View::configChanged, this, &VcsAnnotationItemDelegate::resetBackgrounds);
#else
connect(view, SIGNAL(configChanged()), this, SLOT(resetBackgrounds()));
#endif
view->installEventFilter(this);
}
VcsAnnotationItemDelegate::~VcsAnnotationItemDelegate() = default;
static QString ageOfDate(const QDate& date)
{
const auto now = QDate::currentDate();
int ageInYears = now.year() - date.year();
if (now < date.addYears(ageInYears)) {
--ageInYears;
}
if (ageInYears > 0) {
return i18ncp("@item age", "%1 year", "%1 years", ageInYears);
}
int ageInMonths = now.month() - date.month();
if (now.day() < date.day()) {
--ageInMonths;
}
if (ageInMonths < 0) {
ageInMonths += 12;
}
if (ageInMonths > 0) {
return i18ncp("@item age", "%1 month", "%1 months", ageInMonths);
}
const int ageInDays = date.daysTo(now);
if (ageInDays > 0) {
return i18ncp("@item age", "%1 day", "%1 days", ageInDays);
}
return i18nc("@item age", "Today");
}
void VcsAnnotationItemDelegate::doMessageLineLayout(const KTextEditor::StyleOptionAnnotationItem& option,
QRect* messageRect, QRect* ageRect) const
{
Q_ASSERT(messageRect && messageRect->isValid());
Q_ASSERT(ageRect);
const QWidget* const widget = option.view;
QStyle* const style = widget ? widget->style() : QApplication::style();
const bool hasAge = ageRect->isValid();
// "+ 1" as used in QItemDelegate
const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;
const int ageMargin = hasAge ? textMargin : 0;
const int x = option.rect.left();
const int y = option.rect.top();
const int w = option.rect.width();
const int h = option.rect.height();
// add margins for fixed elements
QSize ageSize(0, 0); // ageRect could be invalid, so use separate object for calculation
if (hasAge) {
ageSize = ageRect->size();
ageSize.rwidth() += 2 * ageMargin;
}
// distribute space among layout items
QRect message;
QRect age;
if (option.direction == Qt::LeftToRight) {
message.setRect(x, y, w - ageSize.width(), h);
age.setRect(message.right() + 1, y, ageSize.width(), h);
} else {
age.setRect(x, y, ageSize.width(), h);
message.setRect(age.right() + 1, y, w - ageSize.width(), h);
}
// remove margins here, so renderMessageAndAge does not have to
message.adjust(textMargin, 0, -textMargin, 0);
age.adjust(ageMargin, 0, -ageMargin, 0);
// return result
*ageRect = age;
*messageRect = QStyle::alignedRect(option.direction, Qt::AlignLeading,
messageRect->size().boundedTo(message.size()), message);
}
void VcsAnnotationItemDelegate::doAuthorLineLayout(const KTextEditor::StyleOptionAnnotationItem& option,
QRect* authorRect) const
{
Q_ASSERT(authorRect);
// if invalid, nothing to be done, keep as is
if (!authorRect->isValid()) {
return;
}
const QWidget* const widget = option.view;
QStyle* const style = widget ? widget->style() : QApplication::style();
// "+ 1" as used in QItemDelegate
const int authorMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;
QRect author = option.rect;
// remove margins here, so renderAuthor does not have to
author.adjust(authorMargin, 0, -authorMargin, 0);
// return result
*authorRect = QStyle::alignedRect(option.direction, Qt::AlignLeading,
authorRect->size().boundedTo(author.size()), author);
}
void VcsAnnotationItemDelegate::renderBackground(QPainter* painter,
const KTextEditor::StyleOptionAnnotationItem& option,
const VcsAnnotationLine& annotationLine) const
{
Q_UNUSED(option);
const auto revision = annotationLine.revision();
auto brushIt = m_backgrounds.find(revision);
if (brushIt == m_backgrounds.end()) {
KTextEditor::Attribute::Ptr normalStyle = option.view->defaultStyleAttribute(KTextEditor::dsNormal);
const auto background = (normalStyle->hasProperty(QTextFormat::BackgroundBrush)) ? normalStyle->background().color() : QColor(Qt::white);
const int background_y = background.red()*0.299 + 0.587*background.green()
+ 0.114*background.blue();
// get random, but reproducable 8-bit values from last two bytes of the revision hash
const uint revisionHash = qHash(revision);
const int u = static_cast<int>((0xFF & revisionHash));
const int v = static_cast<int>((0xFF00 & revisionHash) >> 8);
const int r = qRound(qMin(255.0, qMax(0.0, background_y + 1.402*(v-128))));
const int g = qRound(qMin(255.0, qMax(0.0, background_y - 0.344*(u-128) - 0.714*(v-128))));
const int b = qRound(qMin(255.0, qMax(0.0, background_y + 1.772*(u-128))));
brushIt = m_backgrounds.insert(revision, QBrush(QColor(r, g, b)));
}
painter->fillRect(option.rect, brushIt.value());
}
void VcsAnnotationItemDelegate::renderMessageAndAge(QPainter* painter,
const KTextEditor::StyleOptionAnnotationItem& option,
const QRect& messageRect, const QString& messageText,
const QRect& ageRect, const QString& ageText) const
{
Q_UNUSED(option);
painter->save();
KTextEditor::Attribute::Ptr normalStyle = option.view->defaultStyleAttribute(KTextEditor::dsNormal);
painter->setPen(normalStyle->foreground().color());
painter->drawText(messageRect, Qt::AlignLeading | Qt::AlignVCenter,
painter->fontMetrics().elidedText(messageText, Qt::ElideRight, messageRect.width()));
// TODO: defaultStyleAttribute only returns reliably for dsNormal, so what to do for a comment-like color?
KTextEditor::Attribute::Ptr commentStyle = option.view->defaultStyleAttribute(KTextEditor::dsNormal);
painter->setPen(commentStyle->foreground().color());
painter->drawText(ageRect, Qt::AlignTrailing | Qt::AlignVCenter, ageText);
painter->restore();
}
void VcsAnnotationItemDelegate::renderAuthor(QPainter* painter,
const KTextEditor::StyleOptionAnnotationItem& option,
const QRect& authorRect, const QString& authorText) const
{
Q_UNUSED(option);
painter->save();
// TODO: defaultStyleAttribute only returns reliably for dsNormal, so what to do for a comment-like color?
KTextEditor::Attribute::Ptr commentStyle = option.view->defaultStyleAttribute(KTextEditor::dsNormal);
painter->setPen(commentStyle->foreground().color());
painter->drawText(authorRect, Qt::AlignLeading | Qt::AlignVCenter,
painter->fontMetrics().elidedText(authorText, Qt::ElideRight, authorRect.width()));
painter->restore();
}
void VcsAnnotationItemDelegate::renderHighlight(QPainter* painter,
const KTextEditor::StyleOptionAnnotationItem& option) const
{
// Draw a border around all adjacent entries that have the same text as the currently hovered one
if ((option.state & QStyle::State_MouseOver) &&
(option.annotationItemGroupingPosition & KTextEditor::StyleOptionAnnotationItem::InGroup)) {
KTextEditor::Attribute::Ptr style = option.view->defaultStyleAttribute(KTextEditor::dsNormal);
painter->setPen(style->foreground().color());
// Use floating point coordinates to support scaled rendering
QRectF rect(option.rect);
rect.adjust(0.5, 0.5, -0.5, -0.5);
// draw left and right highlight borders
painter->drawLine(rect.topLeft(), rect.bottomLeft());
painter->drawLine(rect.topRight(), rect.bottomRight());
if ((option.annotationItemGroupingPosition & KTextEditor::StyleOptionAnnotationItem::GroupBegin) &&
(option.wrappedLine == 0)) {
painter->drawLine(rect.topLeft(), rect.topRight());
}
if ((option.annotationItemGroupingPosition & KTextEditor::StyleOptionAnnotationItem::GroupEnd) &&
(option.wrappedLine == (option.wrappedLineCount-1))) {
painter->drawLine(rect.bottomLeft(), rect.bottomRight());
}
}
}
void VcsAnnotationItemDelegate::paint(QPainter* painter, const KTextEditor::StyleOptionAnnotationItem& option,
KTextEditor::AnnotationModel* model, int line) const
{
Q_ASSERT(painter);
// we cannot use custom roles and data() API (cmp. VcsAnnotationModel dox), so accessing custom API instead
auto* vcsModel = qobject_cast<VcsAnnotationModel*>(model);
Q_ASSERT(vcsModel);
if (!painter || !vcsModel) {
return;
}
// test of line just for sake of completeness skipped here
// Fetch data from the model
const VcsAnnotationLine annotationLine = vcsModel->annotationLine(line);
if (annotationLine.revision().revisionType() == VcsRevision::Invalid) {
return;
}
// prepare
painter->save();
renderBackground(painter, option, annotationLine);
// We use the normal UI font here, which usually is a proportimal one,
// so more text fits into the available space.
// Though we do this at the cost of not adapting to any scaled content font size,
// as there is no zooming state info available, so we cannot adapt.
// Tooltip font also is not scaled, and annotations could be considered to fall into
// that category, so might be fine.
painter->setFont(option.view->font());
if (option.visibleWrappedLineInGroup == 0) {
QRect ageRect;
QString ageText;
const auto date = annotationLine.date();
if (date.isValid()) {
ageText = ageOfDate(date.date());
ageRect = QRect(QPoint(0, 0),
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
QSize(option.fontMetrics.horizontalAdvance(ageText), option.rect.height()));
#else
QSize(option.fontMetrics.width(ageText), option.rect.height()));
#endif
}
const auto messageText = annotationLine.commitMessage();
auto messageRect = QRect(QPoint(0, 0),
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
QSize(option.fontMetrics.horizontalAdvance(messageText), option.rect.height()));
#else
QSize(option.fontMetrics.width(messageText), option.rect.height()));
#endif
doMessageLineLayout(option, &messageRect, &ageRect);
renderMessageAndAge(painter, option, messageRect, messageText, ageRect, ageText);
} else if (option.visibleWrappedLineInGroup == 1) {
const auto author = annotationLine.author();
if (!author.isEmpty()) {
const auto authorText = i18nc("By: commit author", "By: %1", author);
auto authorRect = QRect(QPoint(0, 0),
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
QSize(option.fontMetrics.horizontalAdvance(authorText), option.rect.height()));
#else
QSize(option.fontMetrics.width(authorText), option.rect.height()));
#endif
doAuthorLineLayout(option, &authorRect);
renderAuthor(painter, option, authorRect, authorText);
}
}
renderHighlight(painter, option);
// done
painter->restore();
}
bool VcsAnnotationItemDelegate::helpEvent(QHelpEvent* event, KTextEditor::View* view,
const KTextEditor::StyleOptionAnnotationItem& option,
KTextEditor::AnnotationModel* model, int line)
{
Q_UNUSED(option);
if (!model || event->type() != QEvent::ToolTip) {
return false;
}
const QVariant data = model->data(line, Qt::ToolTipRole);
if (!data.isValid()) {
return false;
}
const QString toolTipText = data.toString();
if (toolTipText.isEmpty()) {
return false;
}
QToolTip::showText(event->globalPos(), toolTipText, view, option.rect);
return true;
}
void VcsAnnotationItemDelegate::hideTooltip(KTextEditor::View *view)
{
Q_UNUSED(view);
QToolTip::hideText();
}
QSize VcsAnnotationItemDelegate::sizeHint(const KTextEditor::StyleOptionAnnotationItem& option,
KTextEditor::AnnotationModel* model, int line) const
{
Q_UNUSED(line);
Q_ASSERT(model);
if (!model) {
return QSize(0, 0);
}
// Ideally the user could configure the width of the annotations, best interactively.
// Until this is possible, the sizehint is: roughly 40 chars, but maximal 25 % of the view
// See eventFilter for making sure we adapt the max 25 % to a changed width.
const QFontMetricsF& fm(option.fontMetrics);
// if only averageCharWidth would yield sane values,
// multiply by 40 in average seemed okayish at least with english, showing enough of message
m_lastCharBasedWidthHint = ceil(40 * fm.averageCharWidth());
m_lastViewBasedWidthHint = widthHintFromViewWidth(option.view->width());
return QSize(qMin(m_lastCharBasedWidthHint, m_lastViewBasedWidthHint), fm.height());
}
bool VcsAnnotationItemDelegate::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::Resize) {
auto resizeEvent = static_cast<QResizeEvent*>(event);
const int viewBasedWidthHint = widthHintFromViewWidth(resizeEvent->size().width());
if ((viewBasedWidthHint < m_lastCharBasedWidthHint) &&
(viewBasedWidthHint != m_lastViewBasedWidthHint)) {
// emit for first line only, assuming uniformAnnotationItemSizes is set to true
emit sizeHintChanged(m_model, 0);
}
}
return KTextEditor::AbstractAnnotationItemDelegate::eventFilter(object, event);
}
void VcsAnnotationItemDelegate::resetBackgrounds()
{
m_backgrounds.clear();
}
int VcsAnnotationItemDelegate::widthHintFromViewWidth(int viewWidth) const
{
return viewWidth * m_maxWidthViewPercent / 100;
}
|