File indexing completed on 2024-05-05 03:50:46

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2017 Spencer Brown <spencerbrown991@gmail.com>
0004 //
0005 
0006 #include "NotesItem.h"
0007 #include "MarbleDirs.h"
0008 
0009 #include <QPainter>
0010 #include <QPainterPath>
0011 #include <QRect>
0012 
0013 using namespace Marble;
0014 
0015 const QFont NotesItem::s_font = QFont(QStringLiteral("Sans Serif"), 10);
0016 const int NotesItem::s_labelOutlineWidth = 5;
0017 
0018 NotesItem::NotesItem(QObject *parent)
0019     : AbstractDataPluginItem(parent),
0020       m_pixmap_open(QPixmap(MarbleDirs::path("bitmaps/notes_open.png"))),
0021       m_pixmap_closed(QPixmap(MarbleDirs::path("bitmaps/notes_closed.png")))
0022 {
0023     setSize(m_pixmap_open.size());
0024     setAlignment(Qt::AlignHCenter | Qt::AlignTop);
0025     setCacheMode(ItemCoordinateCache);
0026 }
0027 
0028 NotesItem::~NotesItem()
0029 {
0030 }
0031 
0032 bool NotesItem::initialized() const
0033 {
0034     return !id().isEmpty();
0035 }
0036 
0037 bool NotesItem::operator<(const AbstractDataPluginItem *other) const
0038 {
0039     return this->id() < other->id();
0040 }
0041 
0042 void NotesItem::paint(QPainter *painter)
0043 {
0044     painter->save();
0045 
0046     painter->setFont(s_font);
0047     const int fontAscent = painter->fontMetrics().ascent();
0048     QPen outlinepen(Qt::white);
0049     outlinepen.setWidthF(s_labelOutlineWidth);
0050     QBrush  outlinebrush(Qt::black);
0051 
0052     const QPointF baseline(s_labelOutlineWidth / 2.0, fontAscent);
0053 
0054     QPainterPath outlinepath;
0055     outlinepath.addText(baseline, painter->font(), m_labelText);
0056 
0057     painter->setRenderHint(QPainter::Antialiasing, true);
0058     painter->setPen(outlinepen);
0059     painter->setBrush(outlinebrush);
0060     painter->drawPath(outlinepath);
0061     painter->setPen(Qt::NoPen);
0062     painter->drawPath(outlinepath);
0063     painter->setRenderHint(QPainter::Antialiasing, false);
0064 
0065     int const y = qMax(0, int(size().width() - m_pixmap_open.width()) / 2);
0066 
0067     //The two pixmaps have the same dimensions, so all the logic for one works for the other
0068     QPixmap const & pixmap = m_noteStatus == "closed" ? m_pixmap_closed : m_pixmap_open;
0069     painter->drawPixmap(y, 2 + painter->fontMetrics().height(), pixmap);
0070 
0071     painter->restore();
0072 }
0073 
0074 void NotesItem::setDateCreated(const QDateTime& dateCreated)
0075 {
0076     m_dateCreated = dateCreated;
0077 }
0078 
0079 void NotesItem::setDateClosed(const QDateTime& dateClosed)
0080 {
0081     m_dateClosed = dateClosed;
0082 }
0083 
0084 void NotesItem::setNoteStatus(const QString& noteStatus)
0085 {
0086     m_noteStatus = noteStatus;
0087 }
0088 
0089 void NotesItem::addComment(const Comment& comment)
0090 {
0091     m_commentsList.push_back(comment);
0092     std::sort(m_commentsList.begin(), m_commentsList.end(), [](const Comment & a, const Comment & b) {
0093         return a.date() > b.date();
0094     });
0095 
0096     QStringList toolTip;
0097     for (auto const &entry: m_commentsList) {
0098         QString const date = entry.date().toString(Qt::SystemLocaleShortDate);
0099         QString const user = entry.user().isEmpty() ? tr("anonymous", "The author name is not known") : entry.user();
0100         toolTip << QStringLiteral("%1\n--%2, %3").arg(entry.text().trimmed()).arg(user).arg(date);
0101     }
0102     setToolTip(toolTip.join(QStringLiteral("\n\n")));
0103     QFontMetrics fontmet(s_font);
0104     m_labelText = fontmet.elidedText(m_commentsList.front().text(), Qt::ElideRight, 125);
0105     auto const width = qMax(fontmet.horizontalAdvance(m_labelText), m_pixmap_open.width());
0106     setSize(QSizeF(width, fontmet.height() + 2 + m_pixmap_open.height()));
0107 }
0108 
0109 Comment::Comment() : m_uid(0)
0110 {
0111 }
0112 
0113 Comment::Comment(const QDateTime &date, const QString &text, const QString &user, int uid)
0114     : m_date(date)
0115     , m_text(text)
0116     , m_user(user)
0117 {
0118     m_uid = uid;
0119 }
0120 
0121 QDateTime Comment::date() const
0122 {
0123     return m_date;
0124 }
0125 
0126 QString Comment::text() const
0127 {
0128     return m_text;
0129 }
0130 
0131 QString Comment::user() const
0132 {
0133     return m_user;
0134 }
0135 
0136 int Comment::uid() const
0137 {
0138     return m_uid;
0139 }
0140 
0141 #include "moc_NotesItem.cpp"