HunkHeader resizes itself to not interfere with line wrapping

This commit is contained in:
Ryan Jensen 2024-03-08 02:47:24 -06:00
parent dac06473c0
commit d4dc0f57b2
4 changed files with 71 additions and 4 deletions

View File

@ -11,6 +11,7 @@ add_library(
ContextMenuButton.cpp
DetailView.cpp
DiffView/DiffView.cpp
DiffView/HunkHeader.cpp
DiffView/HunkWidget.cpp
DiffView/FileWidget.cpp
DiffView/FileLabel.cpp

View File

@ -0,0 +1,42 @@
#include "HunkHeader.h"
#include "DiffView.h"
#include "../RepoView.h"
#include <QPainter>
#include <QPainterPath>
HunkHeader::HunkHeader(const QString &name, bool submodule, QWidget *parent)
: QWidget(parent), mName(name) {
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
}
void HunkHeader::setName(const QString &name) { mName = name; }
void HunkHeader::setOldName(const QString &oldName) { mOldName = oldName; }
QSize HunkHeader::sizeHint() const {
QFontMetrics fm = fontMetrics();
int width = fm.boundingRect(mName).width() + 2;
if (!mOldName.isEmpty())
width += fm.boundingRect(mOldName).width() + DiffViewStyle::kArrowWidth;
return QSize(width, fm.lineSpacing());
}
void HunkHeader::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QFontMetrics fm = fontMetrics();
QRect rect = fm.boundingRect(0, 0, this->rect().width(), 300,
Qt::AlignLeft | Qt::ElideRight, mName);
painter.drawText(rect, Qt::AlignLeft | Qt::ElideRight, mName);
}
void HunkHeader::mouseReleaseEvent(QMouseEvent *event) {
if (!rect().contains(event->pos()))
return;
QUrl url;
url.setScheme("submodule");
url.setPath(mName);
RepoView::parentView(this)->visitLink(url.toString());
}

View File

@ -0,0 +1,22 @@
#ifndef HUNKHEADER_H
#define HUNKHEADER_H
#include <QWidget>
class HunkHeader : public QWidget {
public:
HunkHeader(const QString &name, bool submodule, QWidget *parent = nullptr);
void setName(const QString &name);
void setOldName(const QString &oldName);
QSize sizeHint() const override;
protected:
void paintEvent(QPaintEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
QString mName;
QString mOldName;
};
#endif // HUNKHEADER_H

View File

@ -5,6 +5,7 @@
#include "DiffView.h"
#include "DisclosureButton.h"
#include "EditButton.h"
#include "HunkHeader.h"
#include "DiscardButton.h"
#include "app/Application.h"
@ -39,15 +40,16 @@ _HunkWidget::Header::Header(const git::Diff &diff, const git::Patch &patch,
int index, bool lfs, bool submodule,
QWidget *parent)
: QFrame(parent) {
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
setObjectName("HunkHeader");
mCheck = new QCheckBox(this);
mCheck->setTristate(true);
mCheck->setVisible(diff.isStatusDiff() && !submodule &&
!patch.isConflicted());
QString header = (index >= 0) ? patch.header(index) : QString();
QString escaped = header.trimmed().toHtmlEscaped();
QLabel *label = new QLabel(DiffViewStyle::kHunkFmt.arg(escaped), this);
QString headerString = (index >= 0) ? patch.header(index) : QString();
QString escaped = headerString.trimmed().toHtmlEscaped();
HunkHeader *header = new HunkHeader(escaped, submodule, this);
if (patch.isConflicted()) {
mSave = new QToolButton(this);
@ -132,7 +134,7 @@ _HunkWidget::Header::Header(const git::Diff &diff, const git::Patch &patch,
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setContentsMargins(4, 4, 4, 4);
layout->addWidget(mCheck);
layout->addWidget(label);
layout->addWidget(header, 1);
layout->addStretch();
layout->addLayout(buttons);