• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevplatform/vcs

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • vcs
  • models
vcsannotationmodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * This file is part of KDevelop *
3  * Copyright 2007 Andreas Pakulat <[email protected]> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Library General Public License as *
7  * published by the Free Software Foundation; either version 2 of the *
8  * License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Library General Public *
16  * License along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "vcsannotationmodel.h"
22 
23 #include "../vcsannotation.h"
24 #include "../vcsrevision.h"
25 #include "../vcsjob.h"
26 
27 #include <QDateTime>
28 #include <QBrush>
29 #include <QPen>
30 #include <QHash>
31 #include <QLocale>
32 #include <QUrl>
33 #include <QApplication>
34 
35 #include <KLocalizedString>
36 
37 #include <interfaces/icore.h>
38 #include <interfaces/iruncontroller.h>
39 
40 namespace KDevelop
41 {
42 
43 class VcsAnnotationModelPrivate
44 {
45 public:
46  explicit VcsAnnotationModelPrivate( VcsAnnotationModel* q_ ) : q(q_) {}
47  KDevelop::VcsAnnotation m_annotation;
48  mutable QHash<KDevelop::VcsRevision, QBrush> m_brushes;
49  VcsAnnotationModel* q;
50  VcsJob* job;
51  QColor foreground;
52  QColor background;
53 
54  const QBrush& brush(const VcsRevision& revision) const
55  {
56  auto brushIt = m_brushes.find(revision);
57  if (brushIt == m_brushes.end()) {
58  const int background_y = background.red()*0.299 + 0.587*background.green()
59  + 0.114*background.blue();
60  // get random, but reproducable 8-bit values from last two bytes of the revision hash
61  const uint revisionHash = qHash(revision);
62  const int u = static_cast<int>((0xFF & revisionHash));
63  const int v = static_cast<int>((0xFF00 & revisionHash) >> 8);
64  const int r = qRound(qMin(255.0, qMax(0.0, background_y + 1.402*(v-128))));
65  const int g = qRound(qMin(255.0, qMax(0.0, background_y - 0.344*(u-128) - 0.714*(v-128))));
66  const int b = qRound(qMin(255.0, qMax(0.0, background_y + 1.772*(u-128))));
67  brushIt = m_brushes.insert(revision, QBrush(QColor(r, g, b)));
68  }
69  return brushIt.value();
70  }
71 
72  void addLines( KDevelop::VcsJob* job )
73  {
74  if( job == this->job )
75  {
76  const auto results = job->fetchResults().toList();
77  for (const QVariant& v : results) {
78  if( v.canConvert<KDevelop::VcsAnnotationLine>() )
79  {
80  VcsAnnotationLine l = v.value<KDevelop::VcsAnnotationLine>();
81  m_annotation.insertLine( l.lineNumber(), l );
82  emit q->lineChanged( l.lineNumber() );
83  }
84  }
85  }
86  }
87 };
88 
89 VcsAnnotationModel::VcsAnnotationModel(VcsJob *job, const QUrl& url, QObject* parent,
90  const QColor &foreground, const QColor &background)
91  : d_ptr(new VcsAnnotationModelPrivate(this))
92 {
93  Q_D(VcsAnnotationModel);
94 
95  setParent( parent );
96  d->m_annotation.setLocation( url );
97  d->job = job;
98  d->foreground = foreground;
99  d->background = background;
100  connect( d->job, &VcsJob::resultsReady,this, [this] (VcsJob* job) { Q_D(VcsAnnotationModel); d->addLines(job); } );
101  ICore::self()->runController()->registerJob( d->job );
102 }
103 
104 VcsAnnotationModel::~VcsAnnotationModel() = default;
105 
106 static QString abbreviateLastName(const QString& author) {
107  auto parts = author.split(QLatin1Char(' '));
108  bool onlyOneFragment = parts.size() == 1 || ( parts.size() == 2 && parts.at(1).isEmpty() );
109  return onlyOneFragment ? parts.first() : parts.first() + QStringLiteral(" %1.").arg(parts.last()[0]);
110 }
111 
112 static QString annotationToolTip(const VcsAnnotationLine& aline)
113 {
114  const bool textIsLeftToRight = (QApplication::layoutDirection() == Qt::LeftToRight);
115 
116  const QString boldStyle = QStringLiteral(";font-weight:bold");
117  const QString one = QStringLiteral("1");
118  const QString two = QStringLiteral("2");
119  const QString line = QStringLiteral(
120  "<tr>"
121  "<td align=\"right\" style=\"white-space:nowrap%1\">%%2</td>"
122  "<td align=\"left\" style=\"white-space:nowrap%3\">%%4</td>"
123  "</tr>").arg(
124  (textIsLeftToRight ? boldStyle : QString()),
125  (textIsLeftToRight ? one : two),
126  (textIsLeftToRight ? QString() : boldStyle),
127  (textIsLeftToRight ? two : one)
128  );
129 
130  const QString authorLabel = i18n("Author:").toHtmlEscaped();
131  const QString dateLabel = i18n("Date:").toHtmlEscaped();
132  const QString messageLabel = i18n("Commit message:").toHtmlEscaped();
133 
134  const QString author = aline.author().toHtmlEscaped();
135  const QString date = QLocale().toString(aline.date()).toHtmlEscaped();
136  const QString message = aline.commitMessage().toHtmlEscaped().replace(QLatin1Char('\n'), QLatin1String("<br/>"));
137 
138  return
139  QLatin1String("<table>") +
140  line.arg(authorLabel, author) +
141  line.arg(dateLabel, date) +
142  line.arg(messageLabel, message) +
143  QLatin1String("</table>");
144 }
145 
146 QVariant VcsAnnotationModel::data( int line, Qt::ItemDataRole role ) const
147 {
148  Q_D(const VcsAnnotationModel);
149 
150  if( line < 0 || !d->m_annotation.containsLine( line ) )
151  {
152  return QVariant();
153  }
154 
155  KDevelop::VcsAnnotationLine aline = d->m_annotation.line( line );
156  if( role == Qt::ForegroundRole )
157  {
158  return QVariant(QPen(d->foreground));
159  }
160  if( role == Qt::BackgroundRole )
161  {
162  return QVariant(d->brush(aline.revision()));
163  } else if( role == Qt::DisplayRole )
164  {
165  return QVariant( QStringLiteral("%1 ").arg(aline.date().date().year()) + abbreviateLastName(aline.author()) );
166  } else if( role == (int) KTextEditor::AnnotationModel::GroupIdentifierRole )
167  {
168  return aline.revision().revisionValue();
169  } else if( role == Qt::ToolTipRole )
170  {
171  return QVariant(annotationToolTip(aline));
172  }
173  return QVariant();
174 }
175 
176 VcsRevision VcsAnnotationModel::revisionForLine( int line ) const
177 {
178  Q_D(const VcsAnnotationModel);
179 
182  if (!d->m_annotation.containsLine(line)) {
183  return VcsRevision();
184  }
185 
186  Q_ASSERT(line >= 0 && d->m_annotation.containsLine(line));
187  return d->m_annotation.line( line ).revision();
188 }
189 
190 VcsAnnotationLine VcsAnnotationModel::annotationLine(int line) const
191 {
192  Q_D(const VcsAnnotationModel);
193 
194  if (line < 0 || !d->m_annotation.containsLine(line)) {
195  return VcsAnnotationLine();
196  }
197 
198  return d->m_annotation.line(line);
199 }
200 
201 }
202 
203 #include "moc_vcsannotationmodel.cpp"
KDevelop::VcsJob::fetchResults
virtual QVariant fetchResults()=0
This method will return all new results of the job.
QColor
KDevelop::VcsAnnotationModel::data
QVariant data(int line, Qt::ItemDataRole role=Qt::DisplayRole) const override
Definition: vcsannotationmodel.cpp:163
KDevelop::VcsRevision::revisionValue
QVariant revisionValue() const
Return the value of this revision.
Definition: vcsrevision.cpp:100
KDevelop::VcsAnnotationModel::~VcsAnnotationModel
~VcsAnnotationModel() override
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KDevelop::annotationToolTip
static QString annotationToolTip(const VcsAnnotationLine &aline)
Definition: vcsannotationmodel.cpp:129
KDevelop::VcsAnnotationLine::author
QString author() const
Definition: vcsannotation.cpp:91
QUrl
KDevelop::VcsAnnotationModel::revisionForLine
VcsRevision revisionForLine(int line) const
Definition: vcsannotationmodel.cpp:193
QHash::find
iterator find(const Key &key)
QColor::red
int red() const
QPen
QLocale
QHash::insert
iterator insert(const Key &key, const T &value)
KDevelop::VcsAnnotationLine::revision
VcsRevision revision() const
Definition: vcsannotation.cpp:96
QList::size
int size() const
KDevelop::qHash
uint qHash(const KDevelop::VcsLocation &loc)
Definition: vcslocation.h:141
QObject
QString
KDevelop::VcsAnnotationModel::annotationLine
VcsAnnotationLine annotationLine(int line) const
Definition: vcsannotationmodel.cpp:207
KDevelop::VcsJob::resultsReady
void resultsReady(KDevelop::VcsJob *)
This signal is emitted when new results are available.
QLocale::toString
QString toString(qlonglong i) const
KDevelop::VcsAnnotationLine::commitMessage
QString commitMessage() const
Definition: vcsannotation.cpp:137
QColor::green
int green() const
KDevelop::VcsAnnotationLine
Annotation information for a line of a version controlled file.
Definition: vcsannotation.h:40
QBrush
QLatin1String
QString::replace
QString & replace(int position, int n, QChar after)
KDevelop::VcsAnnotation
Annotations for a local file.
Definition: vcsannotation.h:115
KDevelop::VcsJob
This class provides an extension of KJob to get various VCS-specific information about the job.
Definition: vcsjob.h:43
QLatin1Char
QColor::blue
int blue() const
QDateTime::date
QDate date() const
QVariant::toList
QList< QVariant > toList() const
KDevelop::VcsAnnotation::containsLine
bool containsLine(int lineno) const
Definition: vcsannotation.cpp:195
KDevelop::VcsAnnotation::insertLine
void insertLine(int lineno, const VcsAnnotationLine &line)
insert a new line to list of lines using the parameters
Definition: vcsannotation.cpp:170
KDevelop::VcsAnnotationLine::date
QDateTime date() const
Definition: vcsannotation.cpp:101
KDevelop
Definition: dvcsevent.h:33
KDevelop::VcsAnnotationModel::VcsAnnotationModel
VcsAnnotationModel(VcsJob *job, const QUrl &, QObject *, const QColor &foreground=QColor(Qt::black), const QColor &background=QColor(Qt::white))
Definition: vcsannotationmodel.cpp:106
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
vcsannotationmodel.h
QVariant
QApplication::layoutDirection
Qt::LayoutDirection layoutDirection()
QHash< KDevelop::VcsRevision, QBrush >
KDevelop::abbreviateLastName
static QString abbreviateLastName(const QString &author)
Definition: vcsannotationmodel.cpp:123
QDate::year
int year() const
QHash::end
iterator end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Jan 17 2021 23:36:31 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/vcs

Skip menu "kdevplatform/vcs"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  • kdevplatform
  •   debugger
  •   documentation
  •   interfaces
  •   language
  •     assistant
  •     backgroundparser
  •     checks
  •     classmodel
  •     codecompletion
  •     codegen
  •     duchain
  •     editor
  •     highlighting
  •     interfaces
  •     util
  •   outputview
  •   project
  •   serialization
  •   shell
  •   sublime
  •   tests
  •   util
  •   vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal