• 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
  • widgets
vcseventwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * This file is part of KDevelop *
3  * Copyright 2007 Dukju Ahn <[email protected]> *
4  * Copyright 2007 Andreas Pakulat <[email protected]> *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU Library General Public License as *
8  * published by the Free Software Foundation; either version 2 of the *
9  * License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Library General Public *
17  * License along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20  ***************************************************************************/
21 
22 #include "vcseventwidget.h"
23 
24 #include <QAction>
25 #include <QClipboard>
26 #include <QDialog>
27 #include <QDialogButtonBox>
28 #include <QHeaderView>
29 #include <QMenu>
30 #include <QPushButton>
31 #include <QVBoxLayout>
32 #include <QDesktopServices>
33 
34 #include <KLocalizedString>
35 #include <KTextToHTML>
36 
37 #include <interfaces/iplugin.h>
38 
39 #include "ui_vcseventwidget.h"
40 #include "vcsdiffwidget.h"
41 
42 #include "../interfaces/ibasicversioncontrol.h"
43 #include "../models/vcseventmodel.h"
44 #include "../models/vcsitemeventmodel.h"
45 #include "../vcsevent.h"
46 #include "../vcsjob.h"
47 #include "../vcsrevision.h"
48 #include "debug.h"
49 
50 
51 namespace KDevelop
52 {
53 
54 class VcsEventWidgetPrivate
55 {
56 public:
57  explicit VcsEventWidgetPrivate( VcsEventWidget* w )
58  : q( w )
59  {
60  m_copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18nc("@action:inmenu", "Copy Revision Id"), q);
61  m_copyAction->setShortcut(Qt::CTRL | Qt::Key_C);
62  QObject::connect(m_copyAction, &QAction::triggered, q, [&] { copyRevision(); });
63  }
64 
65  Ui::VcsEventWidget* m_ui;
66  VcsItemEventModel* m_detailModel;
67  VcsEventLogModel *m_logModel;
68  QUrl m_url;
69  QModelIndex m_contextIndex;
70  VcsEventWidget* q;
71  QAction* m_copyAction;
72  IBasicVersionControl* m_iface;
73  void eventViewCustomContextMenuRequested( const QPoint &point );
74  void eventViewClicked( const QModelIndex &index );
75  void jobReceivedResults( KDevelop::VcsJob* job );
76  void copyRevision();
77  void diffToPrevious();
78  void diffRevisions();
79  void currentRowChanged(const QModelIndex& start, const QModelIndex& end);
80 };
81 
82 void VcsEventWidgetPrivate::eventViewCustomContextMenuRequested( const QPoint &point )
83 {
84  m_contextIndex = m_ui->eventView->indexAt( point );
85  if( !m_contextIndex.isValid() ){
86  qCDebug(VCS) << "contextMenu is not in TreeView";
87  return;
88  }
89 
90  QMenu menu( m_ui->eventView );
91  menu.addAction(m_copyAction);
92  auto diffToPreviousAction = menu.addAction(i18nc("@action:inmenu", "Diff to Previous Revision"));
93  QObject::connect(diffToPreviousAction, &QAction::triggered, q, [&] { diffToPrevious(); });
94 
95  auto diffRevisionsAction = menu.addAction(i18nc("@action:inmenu", "Diff between Revisions"));
96  QObject::connect(diffRevisionsAction, &QAction::triggered, q, [&] { diffRevisions(); });
97  diffRevisionsAction->setEnabled(m_ui->eventView->selectionModel()->selectedRows().size()>=2);
98 
99  menu.exec( m_ui->eventView->viewport()->mapToGlobal(point) );
100 }
101 
102 void VcsEventWidgetPrivate::currentRowChanged(const QModelIndex& start, const QModelIndex& end)
103 {
104  Q_UNUSED(end);
105  if(start.isValid())
106  eventViewClicked(start);
107 }
108 
109 void VcsEventWidgetPrivate::eventViewClicked( const QModelIndex &index )
110 {
111  KDevelop::VcsEvent ev = m_logModel->eventForIndex( index );
112  m_detailModel->removeRows(0, m_detailModel->rowCount());
113 
114  if( ev.revision().revisionType() != KDevelop::VcsRevision::Invalid )
115  {
116  m_ui->itemEventView->setEnabled(true);
117  m_ui->message->setEnabled(true);
118  const KTextToHTML::Options markupOptions = KTextToHTML::PreserveSpaces;
119  const QString markupMessage =
120  QLatin1String("<tt>") + KTextToHTML::convertToHtml(ev.message(), markupOptions) + QLatin1String("</tt>");
121  m_ui->message->setHtml(markupMessage);
122  m_detailModel->addItemEvents( ev.items() );
123  }else
124  {
125  m_ui->itemEventView->setEnabled(false);
126  m_ui->message->setEnabled(false);
127  m_ui->message->clear();
128  }
129 
130  QHeaderView* header = m_ui->itemEventView->header();
131  header->setSectionResizeMode(QHeaderView::ResizeToContents);
132  header->setStretchLastSection(true);
133 }
134 
135 void VcsEventWidgetPrivate::copyRevision()
136 {
137  qApp->clipboard()->setText(m_contextIndex.sibling(m_contextIndex.row(), 0).data().toString());
138 }
139 
140 void VcsEventWidgetPrivate::diffToPrevious()
141 {
142  KDevelop::VcsEvent ev = m_logModel->eventForIndex( m_contextIndex );
143  KDevelop::VcsRevision prev = KDevelop::VcsRevision::createSpecialRevision(KDevelop::VcsRevision::Previous);
144  KDevelop::VcsJob* job = m_iface->diff( m_url, prev, ev.revision() );
145 
146  auto* widget = new VcsDiffWidget( job );
147  widget->setRevisions( prev, ev.revision() );
148  auto* dlg = new QDialog( q );
149 
150  widget->connect(widget, &VcsDiffWidget::destroyed, dlg, &QDialog::deleteLater);
151 
152  dlg->setWindowTitle( i18nc("@title:window", "Difference To Previous") );
153 
154  auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
155  auto mainWidget = new QWidget;
156  auto *mainLayout = new QVBoxLayout;
157  dlg->setLayout(mainLayout);
158  mainLayout->addWidget(mainWidget);
159  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
160  okButton->setDefault(true);
161  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
162  dlg->connect(buttonBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept);
163  dlg->connect(buttonBox, &QDialogButtonBox::rejected, dlg, &QDialog::reject);
164  mainLayout->addWidget(widget);
165  mainLayout->addWidget(buttonBox);
166 
167  dlg->show();
168 }
169 
170 void VcsEventWidgetPrivate::diffRevisions()
171 {
172  QModelIndexList l = m_ui->eventView->selectionModel()->selectedRows();
173  KDevelop::VcsEvent ev1 = m_logModel->eventForIndex( l.first() );
174  KDevelop::VcsEvent ev2 = m_logModel->eventForIndex( l.last() );
175  KDevelop::VcsJob* job = m_iface->diff( m_url, ev1.revision(), ev2.revision() );
176 
177  auto* widget = new VcsDiffWidget( job );
178  widget->setRevisions( ev1.revision(), ev2.revision() );
179 
180  auto dlg = new QDialog( q );
181  dlg->setWindowTitle( i18nc("@title:window", "Difference between Revisions") );
182 
183  widget->connect(widget, &VcsDiffWidget::destroyed, dlg, &QDialog::deleteLater);
184 
185  auto mainLayout = new QVBoxLayout(dlg);
186  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
187  auto okButton = buttonBox->button(QDialogButtonBox::Ok);
188  okButton->setDefault(true);
189  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
190  dlg->connect(buttonBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept);
191  dlg->connect(buttonBox, &QDialogButtonBox::rejected, dlg, &QDialog::reject);
192  mainLayout->addWidget(buttonBox);
193  mainLayout->addWidget(widget);
194  dlg->show();
195 }
196 
197 VcsEventWidget::VcsEventWidget( const QUrl& url, const VcsRevision& rev, KDevelop::IBasicVersionControl* iface, QWidget* parent )
198  : QWidget(parent)
199  , d_ptr(new VcsEventWidgetPrivate(this))
200 {
201  Q_D(VcsEventWidget);
202 
203  d->m_iface = iface;
204  d->m_url = url;
205  d->m_ui = new Ui::VcsEventWidget();
206  d->m_ui->setupUi(this);
207 
208  d->m_logModel = new VcsEventLogModel(iface, rev, url, this);
209  d->m_ui->eventView->setModel( d->m_logModel );
210  d->m_ui->eventView->sortByColumn(0, Qt::DescendingOrder);
211  d->m_ui->eventView->setContextMenuPolicy( Qt::CustomContextMenu );
212  QHeaderView* header = d->m_ui->eventView->header();
213  header->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
214  header->setSectionResizeMode( 1, QHeaderView::Stretch );
215  header->setSectionResizeMode( 2, QHeaderView::ResizeToContents );
216  header->setSectionResizeMode( 3, QHeaderView::ResizeToContents );
217  // Select first row as soon as the model got populated
218  connect(d->m_logModel, &QAbstractItemModel::rowsInserted, this, [this]() {
219  Q_D(VcsEventWidget);
220  auto view = d->m_ui->eventView;
221  view->setCurrentIndex(view->model()->index(0, 0));
222  });
223 
224  d->m_detailModel = new VcsItemEventModel(this);
225  d->m_ui->itemEventView->setModel( d->m_detailModel );
226 
227  connect(d->m_ui->eventView, &QTreeView::clicked, this, [this] (const QModelIndex& index) {
228  Q_D(VcsEventWidget);
229  d->eventViewClicked(index);
230  });
231  connect(d->m_ui->eventView->selectionModel(), &QItemSelectionModel::currentRowChanged,
232  this, [this] (const QModelIndex& start, const QModelIndex& end) {
233  Q_D(VcsEventWidget);
234  d->currentRowChanged(start, end);
235  });
236  connect(d->m_ui->eventView, &QTreeView::customContextMenuRequested,
237  this, [this] (const QPoint& point) {
238  Q_D(VcsEventWidget);
239  d->eventViewCustomContextMenuRequested(point);
240  });
241 
242  connect(d->m_ui->message, &QTextBrowser::anchorClicked,
243  this, [&] (const QUrl& url) { QDesktopServices::openUrl(url); });
244 }
245 
246 VcsEventWidget::~VcsEventWidget()
247 {
248  Q_D(VcsEventWidget);
249 
250  delete d->m_ui;
251 }
252 
253 }
254 
255 
256 #include "moc_vcseventwidget.cpp"
QHeaderView
QVBoxLayout
QDialog::reject
virtual void reject()
QAbstractButton::setShortcut
void setShortcut(const QKeySequence &key)
QUrl
QWidget::customContextMenuRequested
void customContextMenuRequested(const QPoint &pos)
QHeaderView::setStretchLastSection
void setStretchLastSection(bool stretch)
KDevelop::VcsEventWidget::VcsEventWidget
VcsEventWidget(const QUrl &url, const VcsRevision &rev, KDevelop::IBasicVersionControl *iface, QWidget *parent=nullptr)
Definition: vcseventwidget.cpp:215
QWidget
QIcon::fromTheme
QIcon fromTheme(const QString &name, const QIcon &fallback)
KDevelop::VcsEvent::revision
VcsRevision revision() const
Definition: vcsevent.cpp:137
KDevelop::VcsRevision::Invalid
The type is not set, this is an invalid revision.
Definition: vcsrevision.h:82
KDevelop::IBasicVersionControl
This is the basic interface that all Version Control or Source Code Management plugins need to implem...
Definition: ibasicversioncontrol.h:52
QMenu
KDevelop::VcsEventWidget::~VcsEventWidget
~VcsEventWidget() override
Definition: vcseventwidget.cpp:264
KDevelop::VcsRevision
Encapsulates a vcs revision number, date or range of revisions.
Definition: vcsrevision.h:66
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
KDevelop::VcsEventWidget
Definition: vcseventwidget.h:35
QObject::destroyed
void destroyed(QObject *obj)
QObject::deleteLater
void deleteLater()
QTextBrowser::anchorClicked
void anchorClicked(const QUrl &link)
QPushButton
KDevelop::VcsEvent
Small container class that contains information about a single revision.
Definition: vcsevent.h:96
KDevelop::VcsRevision::revisionType
RevisionType revisionType() const
returns the type of the revision
Definition: vcsrevision.cpp:89
QString
QAbstractTableModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QItemSelectionModel::currentRowChanged
void currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
QDialogButtonBox::accepted
void accepted()
QDialog::accept
virtual void accept()
KDevelop::VcsRevision::Previous
The version prior the other one (only valid in functions that take two revisions).
Definition: vcsrevision.h:90
QModelIndex::isValid
bool isValid() const
QLatin1String
KDevelop::VcsEvent::items
QList< VcsItemEvent > items() const
Definition: vcsevent.cpp:157
QAction::triggered
void triggered(bool checked)
KDevelop::VcsEvent::message
QString message() const
Definition: vcsevent.cpp:152
KDevelop::VcsJob
This class provides an extension of KJob to get various VCS-specific information about the job.
Definition: vcsjob.h:43
QAbstractItemModel::rowsInserted
void rowsInserted(const QModelIndex &parent, int start, int end)
QDialogButtonBox::rejected
void rejected()
QAction
KDevelop
Definition: dvcsevent.h:33
KDevelop::VcsRevision::createSpecialRevision
static VcsRevision createSpecialRevision(KDevelop::VcsRevision::RevisionSpecialType type)
Helper function to create a vcs revision for one of the special types.
Definition: vcsrevision.cpp:49
QPushButton::setDefault
void setDefault(bool)
QModelIndex
KDevelop::VcsItemEventModel
Definition: vcsitemeventmodel.h:34
QDialog
vcsdiffwidget.h
QAbstractItemView::clicked
void clicked(const QModelIndex &index)
vcseventwidget.h
QDialogButtonBox
QPoint
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 09:41:52 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