From d4dc0f57b25927b89c4cceb4267ed44b1d284589 Mon Sep 17 00:00:00 2001 From: Ryan Jensen Date: Fri, 8 Mar 2024 02:47:24 -0600 Subject: [PATCH 1/3] HunkHeader resizes itself to not interfere with line wrapping --- src/ui/CMakeLists.txt | 1 + src/ui/DiffView/HunkHeader.cpp | 42 ++++++++++++++++++++++++++++++++++ src/ui/DiffView/HunkHeader.h | 22 ++++++++++++++++++ src/ui/DiffView/HunkWidget.cpp | 10 ++++---- 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 src/ui/DiffView/HunkHeader.cpp create mode 100644 src/ui/DiffView/HunkHeader.h diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 2e69f48ef..81f189656 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -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 diff --git a/src/ui/DiffView/HunkHeader.cpp b/src/ui/DiffView/HunkHeader.cpp new file mode 100644 index 000000000..170fbb13c --- /dev/null +++ b/src/ui/DiffView/HunkHeader.cpp @@ -0,0 +1,42 @@ +#include "HunkHeader.h" +#include "DiffView.h" +#include "../RepoView.h" + +#include +#include + +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()); +} diff --git a/src/ui/DiffView/HunkHeader.h b/src/ui/DiffView/HunkHeader.h new file mode 100644 index 000000000..9f3793099 --- /dev/null +++ b/src/ui/DiffView/HunkHeader.h @@ -0,0 +1,22 @@ +#ifndef HUNKHEADER_H +#define HUNKHEADER_H + +#include + +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 diff --git a/src/ui/DiffView/HunkWidget.cpp b/src/ui/DiffView/HunkWidget.cpp index d21b24411..45fad68a0 100644 --- a/src/ui/DiffView/HunkWidget.cpp +++ b/src/ui/DiffView/HunkWidget.cpp @@ -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); From cec48ca95db3c356d098c5bf3d8308280f49a2a7 Mon Sep 17 00:00:00 2001 From: Ryan Jensen Date: Sun, 10 Mar 2024 21:10:57 -0500 Subject: [PATCH 2/3] Move HunkLabel.cpp into HunkWidget.cpp --- src/ui/CMakeLists.txt | 1 - src/ui/DiffView/HunkHeader.cpp | 42 ---------------------------------- src/ui/DiffView/HunkHeader.h | 22 ------------------ src/ui/DiffView/HunkWidget.cpp | 22 +++++++++++++----- src/ui/DiffView/HunkWidget.h | 13 +++++++++++ 5 files changed, 29 insertions(+), 71 deletions(-) delete mode 100644 src/ui/DiffView/HunkHeader.cpp delete mode 100644 src/ui/DiffView/HunkHeader.h diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 81f189656..2e69f48ef 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -11,7 +11,6 @@ add_library( ContextMenuButton.cpp DetailView.cpp DiffView/DiffView.cpp - DiffView/HunkHeader.cpp DiffView/HunkWidget.cpp DiffView/FileWidget.cpp DiffView/FileLabel.cpp diff --git a/src/ui/DiffView/HunkHeader.cpp b/src/ui/DiffView/HunkHeader.cpp deleted file mode 100644 index 170fbb13c..000000000 --- a/src/ui/DiffView/HunkHeader.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "HunkHeader.h" -#include "DiffView.h" -#include "../RepoView.h" - -#include -#include - -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()); -} diff --git a/src/ui/DiffView/HunkHeader.h b/src/ui/DiffView/HunkHeader.h deleted file mode 100644 index 9f3793099..000000000 --- a/src/ui/DiffView/HunkHeader.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef HUNKHEADER_H -#define HUNKHEADER_H - -#include - -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 diff --git a/src/ui/DiffView/HunkWidget.cpp b/src/ui/DiffView/HunkWidget.cpp index 45fad68a0..76995a183 100644 --- a/src/ui/DiffView/HunkWidget.cpp +++ b/src/ui/DiffView/HunkWidget.cpp @@ -5,7 +5,6 @@ #include "DiffView.h" #include "DisclosureButton.h" #include "EditButton.h" -#include "HunkHeader.h" #include "DiscardButton.h" #include "app/Application.h" @@ -36,20 +35,31 @@ const QString noNewLineAtEndOfFile = HunkWidget::tr("No newline at end of file"); } // namespace +_HunkWidget::HunkLabel::HunkLabel(const QString &name, bool submodule, + QWidget *parent) + : QWidget(parent), mName(name) {} +void _HunkWidget::HunkLabel::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); +} + _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 headerString = (index >= 0) ? patch.header(index) : QString(); - QString escaped = headerString.trimmed().toHtmlEscaped(); - HunkHeader *header = new HunkHeader(escaped, submodule, this); + QString label_string = (index >= 0) ? patch.header(index) : QString(); + label_string = label_string.trimmed().toHtmlEscaped(); + HunkLabel *label = new HunkLabel(label_string, submodule, this); if (patch.isConflicted()) { mSave = new QToolButton(this); @@ -134,7 +144,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(header, 1); + layout->addWidget(label, 1); layout->addStretch(); layout->addLayout(buttons); diff --git a/src/ui/DiffView/HunkWidget.h b/src/ui/DiffView/HunkWidget.h index 8e67c3618..708bf2c91 100644 --- a/src/ui/DiffView/HunkWidget.h +++ b/src/ui/DiffView/HunkWidget.h @@ -19,6 +19,19 @@ class DisclosureButton; class Line; namespace _HunkWidget { + +class HunkLabel : public QWidget { +public: + HunkLabel(const QString &name, bool submodule, QWidget *parent = nullptr); + +protected: + void paintEvent(QPaintEvent *event) override; + +private: + QString mName; + QString mOldName; +}; + class Header : public QFrame { Q_OBJECT public: From 5e124bd3634b24ae1999a12cf23312718e158145 Mon Sep 17 00:00:00 2001 From: Ryan Jensen Date: Sun, 10 Mar 2024 23:24:38 -0500 Subject: [PATCH 3/3] Simplify HunkLabel --- src/ui/DiffView/HunkWidget.cpp | 3 +-- src/ui/DiffView/HunkWidget.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ui/DiffView/HunkWidget.cpp b/src/ui/DiffView/HunkWidget.cpp index 76995a183..24bc5eff4 100644 --- a/src/ui/DiffView/HunkWidget.cpp +++ b/src/ui/DiffView/HunkWidget.cpp @@ -42,8 +42,7 @@ void _HunkWidget::HunkLabel::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); + QRect rect = fm.boundingRect(0, 0, this->rect().width(), 300, 0, mName); painter.drawText(rect, Qt::AlignLeft | Qt::ElideRight, mName); } diff --git a/src/ui/DiffView/HunkWidget.h b/src/ui/DiffView/HunkWidget.h index 708bf2c91..592301472 100644 --- a/src/ui/DiffView/HunkWidget.h +++ b/src/ui/DiffView/HunkWidget.h @@ -19,7 +19,6 @@ class DisclosureButton; class Line; namespace _HunkWidget { - class HunkLabel : public QWidget { public: HunkLabel(const QString &name, bool submodule, QWidget *parent = nullptr);