| 1 | #include <CommitInfoPanel.h> |
| 2 | |
| 3 | #include <ButtonLink.hpp> |
| 4 | #include <CommitInfo.h> |
| 5 | |
| 6 | #include <QApplication> |
| 7 | #include <QClipboard> |
| 8 | #include <QLabel> |
| 9 | #include <QScrollArea> |
| 10 | #include <QToolTip> |
| 11 | #include <QVBoxLayout> |
| 12 | |
| 13 | CommitInfoPanel::CommitInfoPanel(QWidget *parent) |
| 14 | : QFrame(parent) |
| 15 | , mLabelSha(new ButtonLink()) |
| 16 | , mLabelTitle(new QLabel()) |
| 17 | , mLabelDescription(new QLabel()) |
| 18 | , mLabelAuthor(new QLabel()) |
| 19 | , mLabelDateTime(new QLabel()) |
| 20 | { |
| 21 | mLabelSha->setObjectName("labelSha" ); |
| 22 | mLabelSha->setAlignment(Qt::AlignCenter); |
| 23 | mLabelSha->setWordWrap(true); |
| 24 | |
| 25 | QFont font1; |
| 26 | font1.setBold(true); |
| 27 | font1.setWeight(QFont::Bold); |
| 28 | mLabelTitle->setFont(font1); |
| 29 | mLabelTitle->setAlignment(Qt::AlignCenter); |
| 30 | mLabelTitle->setWordWrap(true); |
| 31 | mLabelTitle->setObjectName("labelTitle" ); |
| 32 | |
| 33 | mLabelDescription->setWordWrap(true); |
| 34 | mLabelDescription->setObjectName("labelDescription" ); |
| 35 | |
| 36 | mScrollArea = new QScrollArea(); |
| 37 | mScrollArea->setWidget(mLabelDescription); |
| 38 | mScrollArea->setWidgetResizable(true); |
| 39 | mScrollArea->setFixedHeight(50); |
| 40 | |
| 41 | mLabelAuthor->setObjectName("labelAuthor" ); |
| 42 | |
| 43 | mLabelDateTime->setObjectName("labelDateTime" ); |
| 44 | |
| 45 | const auto wipSeparator = new QFrame(); |
| 46 | wipSeparator->setObjectName("separator" ); |
| 47 | |
| 48 | const auto descriptionLayout = new QVBoxLayout(this); |
| 49 | descriptionLayout->setContentsMargins(0, 0, 0, 0); |
| 50 | descriptionLayout->setSpacing(0); |
| 51 | descriptionLayout->addWidget(mLabelSha); |
| 52 | descriptionLayout->addWidget(mLabelTitle); |
| 53 | descriptionLayout->addWidget(mScrollArea); |
| 54 | descriptionLayout->addWidget(wipSeparator); |
| 55 | descriptionLayout->addWidget(mLabelAuthor); |
| 56 | descriptionLayout->addWidget(mLabelDateTime); |
| 57 | |
| 58 | connect(mLabelSha, &ButtonLink::clicked, this, [this]() { |
| 59 | const auto button = qobject_cast<ButtonLink *>(sender()); |
| 60 | QApplication::clipboard()->setText(button->data().toString()); |
| 61 | QToolTip::showText(QCursor::pos(), tr("Copied!" ), button); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | void CommitInfoPanel::configure(const CommitInfo &commit) |
| 66 | { |
| 67 | mLabelSha->setText(commit.sha.left(8)); |
| 68 | mLabelSha->setData(commit.sha); |
| 69 | mLabelSha->setToolTip("Click to save" ); |
| 70 | |
| 71 | const auto authorName = commit.committer.split("<" ).first(); |
| 72 | mLabelTitle->setText(commit.shortLog); |
| 73 | mLabelAuthor->setText(authorName); |
| 74 | |
| 75 | QDateTime commitDate = QDateTime::fromSecsSinceEpoch(commit.dateSinceEpoch.count()); |
| 76 | mLabelDateTime->setText(commitDate.toString("dd/MM/yyyy hh:mm" )); |
| 77 | |
| 78 | const auto description = commit.longLog; |
| 79 | mLabelDescription->setText(description.isEmpty() ? "<No description provided>" : description); |
| 80 | |
| 81 | QFontMetrics fm(mLabelDescription->font()); |
| 82 | const auto neededsize = fm.boundingRect(QRect(0, 0, 300, 250), Qt::TextWordWrap, mLabelDescription->text()); |
| 83 | auto height = neededsize.height(); |
| 84 | |
| 85 | if (height > 250) |
| 86 | height = 250; |
| 87 | else if (height < 50) |
| 88 | height = 50; |
| 89 | |
| 90 | mScrollArea->setFixedHeight(height); |
| 91 | |
| 92 | auto f = mLabelDescription->font(); |
| 93 | f.setItalic(description.isEmpty()); |
| 94 | mLabelDescription->setFont(f); |
| 95 | } |
| 96 | |
| 97 | void CommitInfoPanel::clear() |
| 98 | { |
| 99 | mLabelSha->clear(); |
| 100 | mLabelTitle->clear(); |
| 101 | mLabelAuthor->clear(); |
| 102 | mLabelDateTime->clear(); |
| 103 | mLabelDescription->clear(); |
| 104 | } |
| 105 | |