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
|
/*
SPDX-FileCopyrightText: 2006-2007 Hamish Rodda <rodda@kde.org>
SPDX-FileCopyrightText: 2006 Adam Treat <treat@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "problemtreeview.h"
#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QItemDelegate>
#include <QMenu>
#include <QSortFilterProxyModel>
#include <KLocalizedString>
#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/iassistant.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/editor/documentrange.h>
#include <util/kdevstringhandler.h>
#include <shell/problemmodel.h>
#include <shell/problem.h>
#include <shell/problemconstants.h>
#include <algorithm>
#include <array>
using namespace KDevelop;
namespace {
QString descriptionFromProblem(IProblem::Ptr problem)
{
QString text;
const auto location = problem->finalLocation();
if (location.isValid()) {
text += location.document.toUrl()
.adjusted(QUrl::NormalizePathSegments)
.toDisplayString(QUrl::PreferLocalFile);
if (location.start().line() >= 0) {
text += QLatin1Char(':') + QString::number(location.start().line() + 1);
if (location.start().column() >= 0) {
text += QLatin1Char(':') + QString::number(location.start().column() + 1);
}
}
text += QLatin1String(": ");
}
text += problem->description();
if (!problem->explanation().isEmpty()) {
text += QLatin1Char('\n') + problem->explanation();
}
return text;
}
}
namespace KDevelop
{
class ProblemTreeViewItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ProblemTreeViewItemDelegate(QObject* parent = nullptr);
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
};
}
ProblemTreeViewItemDelegate::ProblemTreeViewItemDelegate(QObject* parent)
: QItemDelegate(parent)
{
}
void ProblemTreeViewItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyleOptionViewItem newOption(option);
newOption.textElideMode = index.column() == ProblemModel::File ? Qt::ElideMiddle : Qt::ElideRight;
QItemDelegate::paint(painter, newOption, index);
}
ProblemTreeView::ProblemTreeView(QWidget* parent, QAbstractItemModel* itemModel)
: QTreeView(parent)
, m_proxy(new QSortFilterProxyModel(this))
{
setObjectName(QStringLiteral("Problem Reporter Tree"));
setWhatsThis(i18nc("@info:whatsthis", "Problems"));
setItemDelegate(new ProblemTreeViewItemDelegate(this));
setSelectionBehavior(QAbstractItemView::SelectRows);
setUniformRowHeights(true);
m_proxy->setSortRole(ProblemModel::SeverityRole);
m_proxy->setDynamicSortFilter(true);
m_proxy->sort(0, Qt::AscendingOrder);
auto* problemModel = qobject_cast<ProblemModel*>(itemModel);
Q_ASSERT(problemModel);
setModel(problemModel);
header()->setStretchLastSection(false);
if (!problemModel->features().testFlag(ProblemModel::ShowSource)) {
hideColumn(ProblemModel::Source);
}
connect(this, &ProblemTreeView::clicked, this, &ProblemTreeView::itemActivated);
connect(model(), &QAbstractItemModel::rowsInserted, this, &ProblemTreeView::changed);
connect(model(), &QAbstractItemModel::rowsRemoved, this, &ProblemTreeView::changed);
connect(model(), &QAbstractItemModel::modelReset, this, &ProblemTreeView::changed);
m_proxy->setFilterKeyColumn(-1);
m_proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
resizeColumns();
}
ProblemTreeView::~ProblemTreeView()
{
}
void ProblemTreeView::openDocumentForCurrentProblem()
{
itemActivated(currentIndex());
}
void ProblemTreeView::itemActivated(const QModelIndex& index)
{
if (!index.isValid())
return;
KTextEditor::Cursor start;
QUrl url;
{
// TODO: is this really necessary?
DUChainReadLocker lock(DUChain::lock());
const auto problem = index.data(ProblemModel::ProblemRole).value<IProblem::Ptr>();
if (!problem)
return;
url = problem->finalLocation().document.toUrl();
start = problem->finalLocation().start();
}
if (QFile::exists(url.toLocalFile())) {
ICore::self()->documentController()->openDocument(url, start);
}
}
void ProblemTreeView::resizeColumns()
{
// Don't simply call QTreeView::resizeColumnToContents() for each column here,
// because it is not useful enough to justify significant performance cost.
// Instead, set column widths to heuristic values independent on the contents (the problem list).
const int averageCharWidth = fontMetrics().averageCharWidth();
const int headerWidth = header()->width();
if (averageCharWidth == m_averageCharWidth && headerWidth == m_headerWidth) {
// No reason to change column widths. This early return is not just an optimization: KDevelop should not
// gratuitously reapply unchanged heuristic column widths, because the user may have fine-tuned them manually.
return;
}
m_averageCharWidth = averageCharWidth;
m_headerWidth = headerWidth;
struct ColumnSizePolicy
{
int minWidthInCharacters;
int stretchFactor;
};
static constexpr std::array<ColumnSizePolicy, 5> sizePolicy{
ColumnSizePolicy{40, 20}, // Error
ColumnSizePolicy{25, 1}, // Source
ColumnSizePolicy{30, 10}, // File
ColumnSizePolicy{10, 1}, // Line
ColumnSizePolicy{10, 1}, // Column
};
static_assert(sizePolicy.size() == ProblemModel::LastColumn);
// Cannot use std::accumulate() here, because it is not constexpr in C++17.
static constexpr ColumnSizePolicy totalAllColumns = [] {
ColumnSizePolicy sum{};
for (auto p : sizePolicy) {
sum.minWidthInCharacters += p.minWidthInCharacters;
sum.stretchFactor += p.stretchFactor;
}
return sum;
}();
ColumnSizePolicy total = totalAllColumns;
if (!model()->features().testFlag(ProblemModel::ShowSource)) {
// Disregard the size policy of the hidden Source column.
static constexpr auto hiddenColumn = sizePolicy[ProblemModel::Source];
total.minWidthInCharacters -= hiddenColumn.minWidthInCharacters;
total.stretchFactor -= hiddenColumn.stretchFactor;
}
Q_ASSERT(total.stretchFactor > 0);
const int remainingPixels = std::max(0, headerWidth - total.minWidthInCharacters * averageCharWidth);
// Give each column its minimum needed width. If there is any horizontal space left,
// distribute it among columns in proportion to their stretch factors.
for (std::size_t i = 0; i < sizePolicy.size(); ++i) {
int width = sizePolicy[i].minWidthInCharacters * averageCharWidth;
width += remainingPixels * sizePolicy[i].stretchFactor / total.stretchFactor;
setColumnWidth(i, width);
}
}
int ProblemTreeView::setFilter(const QString& filterText)
{
m_proxy->setFilterFixedString(filterText);
return m_proxy->rowCount();
}
ProblemModel* ProblemTreeView::model() const
{
return static_cast<ProblemModel*>(m_proxy->sourceModel());
}
void ProblemTreeView::setModel(QAbstractItemModel* model)
{
Q_ASSERT(qobject_cast<ProblemModel*>(model));
m_proxy->setSourceModel(model);
QTreeView::setModel(m_proxy);
}
void ProblemTreeView::contextMenuEvent(QContextMenuEvent* event)
{
QModelIndex index = indexAt(event->pos());
if (!index.isValid())
return;
const auto problem = index.data(ProblemModel::ProblemRole).value<IProblem::Ptr>();
if (!problem) {
return;
}
QPointer<QMenu> m = new QMenu(this);
m->addSection(i18nc("@title:menu", "Problem"));
auto copyDescriptionAction = m->addAction(QIcon::fromTheme(QStringLiteral("edit-copy")),
i18nc("@action:inmenu", "&Copy Description"));
connect(copyDescriptionAction, &QAction::triggered, this, [problem]() {
QApplication::clipboard()->setText(descriptionFromProblem(problem), QClipboard::Clipboard);
});
QExplicitlySharedDataPointer<KDevelop::IAssistant> solution = problem->solutionAssistant();
if (solution && !solution->actions().isEmpty()) {
QList<QAction*> actions;
const auto solutionActions = solution->actions();
actions.reserve(solutionActions.size());
for (auto assistantAction : solutionActions) {
auto action = assistantAction->toQAction(m.data());
action->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok-apply")));
actions << action;
}
QString title = solution->title();
title = KDevelop::htmlToPlainText(title);
title.replace(QLatin1String("'"), QLatin1String("\'"));
m->addSection(i18nc("@title:menu", "Solve: %1", title));
m->addActions(actions);
}
m->exec(event->globalPos());
delete m;
}
void ProblemTreeView::resizeEvent(QResizeEvent* event)
{
QTreeView::resizeEvent(event);
// resizeEvent() is invoked whenever this tree view is resized and also whenever the default system font
// changes. So the resizeColumns() call below should cover all scenarios where heuristic column widths change.
resizeColumns();
}
#include "problemtreeview.moc"
#include "moc_problemtreeview.cpp"
|