• 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
vcsfilechangesmodel.cpp
Go to the documentation of this file.
1 /* This file is part of KDevelop
2  Copyright 2010 Aleix Pol <[email protected]>
3 
4  Split into separate class
5  Copyright 2011 Andrey Batyiev <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "vcsfilechangesmodel.h"
24 
25 #include "debug.h"
26 
27 #include <QIcon>
28 #include <QMimeDatabase>
29 
30 #include <KLocalizedString>
31 
32 #include <interfaces/icore.h>
33 #include <interfaces/iprojectcontroller.h>
34 
35 #include <vcs/vcsstatusinfo.h>
36 
37 namespace KDevelop
38 {
39 
40 static QString stateToString(KDevelop::VcsStatusInfo::State state)
41 {
42  switch(state)
43  {
44  case KDevelop::VcsStatusInfo::ItemAdded:
45  return i18nc("@item file was added to versioncontrolsystem", "Added");
46  case KDevelop::VcsStatusInfo::ItemDeleted:
47  return i18nc("@item file was deleted from versioncontrolsystem", "Deleted");
48  case KDevelop::VcsStatusInfo::ItemHasConflicts:
49  return i18nc("@item file is conflicting (versioncontrolsystem)", "Has Conflicts");
50  case KDevelop::VcsStatusInfo::ItemModified:
51  return i18nc("@item version controlled file was modified", "Modified");
52  case KDevelop::VcsStatusInfo::ItemUpToDate:
53  return i18nc("@item file is up to date in versioncontrolsystem", "Up To Date");
54  case KDevelop::VcsStatusInfo::ItemUnknown:
55  case KDevelop::VcsStatusInfo::ItemUserState:
56  return i18nc("@item file is not known to versioncontrolsystem", "Unknown");
57  }
58  return i18nc("@item Unknown VCS file status, probably a backend error", "?");
59 }
60 
61 static QIcon stateToIcon(KDevelop::VcsStatusInfo::State state)
62 {
63  switch(state)
64  {
65  case KDevelop::VcsStatusInfo::ItemAdded:
66  return QIcon::fromTheme(QStringLiteral("vcs-added"));
67  case KDevelop::VcsStatusInfo::ItemDeleted:
68  return QIcon::fromTheme(QStringLiteral("vcs-removed"));
69  case KDevelop::VcsStatusInfo::ItemHasConflicts:
70  return QIcon::fromTheme(QStringLiteral("vcs-conflicting"));
71  case KDevelop::VcsStatusInfo::ItemModified:
72  return QIcon::fromTheme(QStringLiteral("vcs-locally-modified"));
73  case KDevelop::VcsStatusInfo::ItemUpToDate:
74  return QIcon::fromTheme(QStringLiteral("vcs-normal"));
75  case KDevelop::VcsStatusInfo::ItemUnknown:
76  case KDevelop::VcsStatusInfo::ItemUserState:
77  return QIcon::fromTheme(QStringLiteral("unknown"));
78  }
79  return QIcon::fromTheme(QStringLiteral("dialog-error"));
80 }
81 
82 VcsFileChangesSortProxyModel::VcsFileChangesSortProxyModel(QObject* parent)
83  : QSortFilterProxyModel(parent)
84 {
85 }
86 
87 bool VcsFileChangesSortProxyModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
88 {
89  const auto leftStatus = source_left.data(VcsFileChangesModel::StateRole).value<VcsStatusInfo::State>();
90  const auto rightStatus = source_right.data(VcsFileChangesModel::StateRole).value<VcsStatusInfo::State>();
91  if (leftStatus != rightStatus) {
92  return leftStatus < rightStatus;
93  }
94 
95  const QString leftPath = source_left.data(VcsFileChangesModel::UrlRole).toString();
96  const QString rightPath = source_right.data(VcsFileChangesModel::UrlRole).toString();
97  return QString::localeAwareCompare(leftPath, rightPath) < 0;
98 }
99 
100 class VcsStatusInfoItem : public QStandardItem
101 {
102 public:
103  explicit VcsStatusInfoItem(const VcsStatusInfo& info)
104  : QStandardItem()
105  , m_info(info) {}
106 
107  void setStatus(const VcsStatusInfo& info) {
108  m_info = info;
109  emitDataChanged();
110  }
111 
112  QVariant data(int role) const override
113  {
114  switch(role) {
115  case Qt::DisplayRole:
116  return stateToString(m_info.state());
117  case Qt::DecorationRole:
118  return stateToIcon(m_info.state());
119  case VcsFileChangesModel::VcsStatusInfoRole:
120  return QVariant::fromValue(m_info);
121  case VcsFileChangesModel::UrlRole:
122  return m_info.url();
123  case VcsFileChangesModel::StateRole:
124  return QVariant::fromValue(m_info.state());
125  }
126  return {};
127  }
128 
129 private:
130  VcsStatusInfo m_info;
131 };
132 
133 class VcsFileChangesModelPrivate
134 {
135 public:
136  bool allowSelection;
137 };
138 
139 VcsFileChangesModel::VcsFileChangesModel(QObject *parent, bool allowSelection)
140  : QStandardItemModel(parent)
141  , d_ptr(new VcsFileChangesModelPrivate{allowSelection})
142 {
143  setColumnCount(2);
144  setHeaderData(0, Qt::Horizontal, i18nc("@title:column", "Filename"));
145  setHeaderData(1, Qt::Horizontal, i18nc("@title:column", "Status"));
146 }
147 
148 VcsFileChangesModel::~VcsFileChangesModel()
149 {
150 }
151 
152 int VcsFileChangesModel::updateState(QStandardItem *parent, const KDevelop::VcsStatusInfo &status)
153 {
154  Q_D(VcsFileChangesModel);
155 
156  if(status.state()==VcsStatusInfo::ItemUnknown || status.state()==VcsStatusInfo::ItemUpToDate) {
157  removeUrl(status.url());
158  return -1;
159  } else {
160  QStandardItem* item = fileItemForUrl(parent, status.url());
161  if(!item) {
162  QString path = ICore::self()->projectController()->prettyFileName(status.url(), KDevelop::IProjectController::FormatPlain);
163  QMimeType mime = status.url().isLocalFile()
164  ? QMimeDatabase().mimeTypeForFile(status.url().toLocalFile(), QMimeDatabase::MatchExtension)
165  : QMimeDatabase().mimeTypeForUrl(status.url());
166  QIcon icon = QIcon::fromTheme(mime.iconName());
167  item = new QStandardItem(icon, path);
168  auto itStatus = new VcsStatusInfoItem(status);
169 
170  if(d->allowSelection) {
171  item->setCheckable(true);
172  item->setCheckState(status.state() == VcsStatusInfo::ItemUnknown ? Qt::Unchecked : Qt::Checked);
173  }
174 
175  parent->appendRow({ item, itStatus });
176  } else {
177  QStandardItem *parent = item->parent();
178  if(parent == nullptr)
179  parent = invisibleRootItem();
180  auto statusInfoItem = static_cast<VcsStatusInfoItem*>(parent->child(item->row(), 1));
181  statusInfoItem->setStatus(status);
182  }
183 
184  return item->row();
185  }
186 }
187 
188 QVariant VcsFileChangesModel::data(const QModelIndex &index, int role) const
189 {
190  if (role >= VcsStatusInfoRole && index.column()==0) {
191  return QStandardItemModel::data(index.sibling(index.row(), 1), role);
192  }
193  return QStandardItemModel::data(index, role);
194 }
195 
196 QStandardItem* VcsFileChangesModel::fileItemForUrl(QStandardItem* parent, const QUrl& url) const
197 {
198  Q_ASSERT(parent);
199  if (!parent) {
200  qCWarning(VCS) << "null QStandardItem passed to" << Q_FUNC_INFO;
201  return nullptr;
202  }
203 
204  for(int i=0, c=parent->rowCount(); i<c; i++) {
205  QStandardItem* item = parent->child(i);
206  if(indexFromItem(item).data(UrlRole).toUrl() == url) {
207  return parent->child(i);
208  }
209  }
210  return nullptr;
211 }
212 
213 void VcsFileChangesModel::setAllChecked(bool checked)
214 {
215  Q_D(VcsFileChangesModel);
216 
217  if(!d->allowSelection)
218  return;
219  QStandardItem* parent = invisibleRootItem();
220  for(int i = 0, c = parent->rowCount(); i < c; i++) {
221  QStandardItem* item = parent->child(i);
222  item->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
223  }
224 }
225 
226 QList<QUrl> VcsFileChangesModel::checkedUrls(QStandardItem *parent) const
227 {
228  Q_D(const VcsFileChangesModel);
229 
230  Q_ASSERT(parent);
231  if (!parent) {
232  qCWarning(VCS) << "null QStandardItem passed to" << Q_FUNC_INFO;
233  return {};
234  }
235 
236  QList<QUrl> ret;
237  for(int i = 0, c = parent->rowCount(); i < c; i++) {
238  QStandardItem* item = parent->child(i);
239  if(!d->allowSelection || item->checkState() == Qt::Checked) {
240  ret << indexFromItem(item).data(UrlRole).toUrl();
241  }
242  }
243  return ret;
244 }
245 
246 QList<QUrl> VcsFileChangesModel::urls(QStandardItem *parent) const
247 {
248  Q_ASSERT(parent);
249  if (!parent) {
250  qCWarning(VCS) << "null QStandardItem passed to" << Q_FUNC_INFO;
251  return {};
252  }
253 
254  QList<QUrl> ret;
255  const int c = parent->rowCount();
256  ret.reserve(c);
257  for (int i = 0; i < c; i++) {
258  QStandardItem* item = parent->child(i);
259  ret << indexFromItem(item).data(UrlRole).toUrl();
260  }
261  return ret;
262 }
263 
264 void VcsFileChangesModel::checkUrls(QStandardItem *parent, const QList<QUrl>& urls) const
265 {
266  Q_D(const VcsFileChangesModel);
267 
268  Q_ASSERT(parent);
269  if (!parent) {
270  qCWarning(VCS) << "null QStandardItem passed to" << Q_FUNC_INFO;
271  return;
272  }
273 
274  if(!d->allowSelection)
275  return;
276 
277 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
278  const QSet<QUrl> urlSet(urls.begin(), urls.end());
279 #else
280  const QSet<QUrl> urlSet(urls.toSet());
281 #endif
282  for(int i = 0, c = parent->rowCount(); i < c; i++) {
283  QStandardItem* item = parent->child(i);
284  item->setCheckState(urlSet.contains(indexFromItem(item).data(UrlRole).toUrl()) ?
285  Qt::Checked :
286  Qt::Unchecked);
287  }
288 }
289 
290 void VcsFileChangesModel::setIsCheckbable(bool checkable)
291 {
292  Q_D(VcsFileChangesModel);
293 
294  d->allowSelection = checkable;
295 }
296 
297 bool VcsFileChangesModel::isCheckable() const
298 {
299  Q_D(const VcsFileChangesModel);
300 
301  return d->allowSelection;
302 }
303 
304 bool VcsFileChangesModel::removeUrl(const QUrl& url)
305 {
306  const auto matches = match(index(0, 0), UrlRole, url, 1, Qt::MatchExactly);
307  if (matches.isEmpty())
308  return false;
309 
310  const auto& idx = matches.first();
311  return removeRow(idx.row(), idx.parent());
312 }
313 
314 }
QAbstractItemModel::removeRow
bool removeRow(int row, const QModelIndex &parent)
QSet
KDevelop::VcsStatusInfo::ItemModified
Item was modified locally.
Definition: vcsstatusinfo.h:58
KDevelop::VcsFileChangesModel::VcsFileChangesModel
VcsFileChangesModel(QObject *parent=nullptr, bool isCheckable=false)
Constructor for class.
Definition: vcsfilechangesmodel.cpp:139
KDevelop::VcsFileChangesModel::StateRole
Definition: vcsfilechangesmodel.h:60
QStandardItemModel::indexFromItem
QModelIndex indexFromItem(const QStandardItem *item) const
QAbstractItemModel::match
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, QFlags< Qt::MatchFlag > flags) const
QVariant::toUrl
QUrl toUrl() const
QVariant::fromValue
QVariant fromValue(const T &value)
QStandardItem::row
int row() const
QModelIndex::sibling
QModelIndex sibling(int row, int column) const
QUrl
KDevelop::VcsFileChangesModel::~VcsFileChangesModel
~VcsFileChangesModel() override
Definition: vcsfilechangesmodel.cpp:148
QStandardItem::checkState
Qt::CheckState checkState() const
QModelIndex::column
int column() const
QVariant::value
T value() const
KDevelop::VcsStatusInfo::ItemDeleted
Item is scheduled to be deleted.
Definition: vcsstatusinfo.h:59
KDevelop::VcsFileChangesModel
This class holds and represents information about changes in files.
Definition: vcsfilechangesmodel.h:55
KDevelop::VcsFileChangesModel::UrlRole
Definition: vcsfilechangesmodel.h:60
QIcon::fromTheme
QIcon fromTheme(const QString &name, const QIcon &fallback)
KDevelop::stateToString
static QString stateToString(KDevelop::VcsStatusInfo::State state)
Definition: vcsfilechangesmodel.cpp:40
KDevelop::VcsStatusInfo::state
VcsStatusInfo::State state() const
Definition: vcsstatusinfo.cpp:108
KDevelop::VcsStatusInfo::ItemUserState
special states for individual vcs implementations should use this as base.
Definition: vcsstatusinfo.h:61
QList< QUrl >
KDevelop::VcsFileChangesModel::setAllChecked
void setAllChecked(bool checked)
Changes the check-state of all files to the given state.
Definition: vcsfilechangesmodel.cpp:213
QList::reserve
void reserve(int alloc)
QModelIndex::data
QVariant data(int role) const
QStandardItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const
vcsfilechangesmodel.h
KDevelop::VcsFileChangesModel::VcsStatusInfoRole
Definition: vcsfilechangesmodel.h:60
QSortFilterProxyModel
QStandardItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QObject
QList::toSet
QSet< T > toSet() const
KDevelop::VcsStatusInfo::ItemUpToDate
Item was updated or it is already at up to date version.
Definition: vcsstatusinfo.h:56
QString
KDevelop::VcsFileChangesModel::setIsCheckbable
void setIsCheckbable(bool checkable)
Definition: vcsfilechangesmodel.cpp:290
QStandardItem
KDevelop::VcsFileChangesModel::removeUrl
bool removeUrl(const QUrl &url)
Definition: vcsfilechangesmodel.cpp:304
KDevelop::VcsFileChangesModel::checkedUrls
QList< QUrl > checkedUrls() const
Returns list of currently checked urls.
Definition: vcsfilechangesmodel.h:75
QStandardItem::parent
QStandardItem * parent() const
QUrl::toLocalFile
QString toLocalFile() const
QIcon
KDevelop::VcsStatusInfo::ItemAdded
Item was added to the repository but not committed.
Definition: vcsstatusinfo.h:57
KDevelop::VcsStatusInfo::url
QUrl url() const
retrieves the url of this status information item
Definition: vcsstatusinfo.cpp:103
QModelIndex::row
int row() const
KDevelop::VcsFileChangesModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: vcsfilechangesmodel.cpp:188
KDevelop::VcsStatusInfo::ItemUnknown
No VCS information about a file is known (or file is not under VCS control).
Definition: vcsstatusinfo.h:55
QString::localeAwareCompare
int localeAwareCompare(const QString &other) const
QObject::child
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
KDevelop::stateToIcon
static QIcon stateToIcon(KDevelop::VcsStatusInfo::State state)
Definition: vcsfilechangesmodel.cpp:61
KDevelop::VcsStatusInfo::State
State
Status of a local file.
Definition: vcsstatusinfo.h:53
QUrl::isLocalFile
bool isLocalFile() const
QStandardItemModel::invisibleRootItem
QStandardItem * invisibleRootItem() const
KDevelop
Definition: dvcsevent.h:33
QList::begin
iterator begin()
KDevelop::VcsFileChangesSortProxyModel::lessThan
bool lessThan(const QModelIndex &rLeft, const QModelIndex &rRight) const override
Definition: vcsfilechangesmodel.cpp:87
QModelIndex
KDevelop::VcsStatusInfo
Class that encapsulates status information for one local url.
Definition: vcsstatusinfo.h:47
KDevelop::VcsFileChangesSortProxyModel::VcsFileChangesSortProxyModel
VcsFileChangesSortProxyModel(QObject *parent=nullptr)
Definition: vcsfilechangesmodel.cpp:82
KDevelop::VcsFileChangesModel::updateState
void updateState(const KDevelop::VcsStatusInfo &status)
Used to post update of status of some file.
Definition: vcsfilechangesmodel.h:115
QStandardItemModel
QStandardItem::setCheckable
void setCheckable(bool checkable)
QVariant
QList::end
iterator end()
vcsstatusinfo.h
QStandardItemModel::item
QStandardItem * item(int row, int column) const
QStandardItem::setCheckState
void setCheckState(Qt::CheckState state)
KDevelop::VcsFileChangesModel::isCheckable
bool isCheckable() const
Definition: vcsfilechangesmodel.cpp:297
QObject::parent
QObject * parent() const
KDevelop::VcsStatusInfo::ItemHasConflicts
Local version has conflicts that need to be resolved before commit.
Definition: vcsstatusinfo.h:60
KDevelop::VcsFileChangesModel::fileItemForUrl
QStandardItem * fileItemForUrl(QStandardItem *parent, const QUrl &url) const
Returns item for particular url.
Definition: vcsfilechangesmodel.cpp:196
KDevelop::VcsFileChangesModel::urls
QList< QUrl > urls() const
Returns urls of all files.
Definition: vcsfilechangesmodel.h:82
QVariant::toString
QString toString() const
KDevelop::VcsFileChangesModel::checkUrls
void checkUrls(QStandardItem *parent, const QList< QUrl > &urls) const
Checks the given urls, unchecks all others.
Definition: vcsfilechangesmodel.cpp:264
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 23:37:34 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