File indexing completed on 2024-05-19 05:21:25

0001 /*
0002   This file is part of the KDE Kontact Plugin Interface Library.
0003 
0004   SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0005   SPDX-FileCopyrightText: 2003 Daniel Molkentin <molkentin@kde.org>
0006 
0007   SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "summary.h"
0011 
0012 #include <QDrag>
0013 #include <QDragEnterEvent>
0014 #include <QDropEvent>
0015 #include <QFont>
0016 #include <QFontDatabase>
0017 #include <QHBoxLayout>
0018 #include <QIcon>
0019 #include <QLabel>
0020 #include <QMimeData>
0021 #include <QMouseEvent>
0022 #include <QPainter>
0023 #include <QPixmap>
0024 #include <QStyle>
0025 
0026 using namespace KontactInterface;
0027 
0028 //@cond PRIVATE
0029 namespace KontactInterface
0030 {
0031 class SummaryMimeData : public QMimeData
0032 {
0033     Q_OBJECT
0034 public:
0035     bool hasFormat(const QString &format) const override
0036     {
0037         return format == QLatin1StringView("application/x-kontact-summary");
0038     }
0039 };
0040 }
0041 //@endcond
0042 
0043 //@cond PRIVATE
0044 class Q_DECL_HIDDEN Summary::SummaryPrivate
0045 {
0046 public:
0047     QPoint mDragStartPoint;
0048 };
0049 //@endcond
0050 
0051 Summary::Summary(QWidget *parent)
0052     : QWidget(parent)
0053     , d(new SummaryPrivate)
0054 {
0055     setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
0056     setAcceptDrops(true);
0057 }
0058 
0059 Summary::~Summary() = default;
0060 
0061 int Summary::summaryHeight() const
0062 {
0063     return 1;
0064 }
0065 
0066 QWidget *Summary::createHeader(QWidget *parent, const QString &iconname, const QString &heading)
0067 {
0068     auto box = new QWidget(parent);
0069     auto hbox = new QHBoxLayout(box);
0070     hbox->setContentsMargins({});
0071     hbox->setSpacing(0);
0072     box->setAutoFillBackground(true);
0073 
0074     QIcon icon = QIcon::fromTheme(iconname);
0075 
0076     auto label = new QLabel(box);
0077     hbox->addWidget(label);
0078     label->setPixmap(icon.pixmap(style()->pixelMetric(QStyle::PM_ToolBarIconSize)));
0079 
0080     label->setFixedSize(label->sizeHint());
0081     label->setAcceptDrops(true);
0082 
0083     label = new QLabel(heading, box);
0084     hbox->addWidget(label);
0085     label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
0086 
0087     box->setMaximumHeight(box->minimumSizeHint().height());
0088 
0089     return box;
0090 }
0091 
0092 QStringList Summary::configModules() const
0093 {
0094     return {};
0095 }
0096 
0097 void Summary::configChanged()
0098 {
0099 }
0100 
0101 void Summary::updateSummary(bool force)
0102 {
0103     Q_UNUSED(force)
0104 }
0105 
0106 void Summary::mousePressEvent(QMouseEvent *event)
0107 {
0108     d->mDragStartPoint = event->pos();
0109 
0110     QWidget::mousePressEvent(event);
0111 }
0112 
0113 void Summary::mouseMoveEvent(QMouseEvent *event)
0114 {
0115     if ((event->buttons() & Qt::LeftButton) && (event->pos() - d->mDragStartPoint).manhattanLength() > 4) {
0116         auto drag = new QDrag(this);
0117         drag->setMimeData(new SummaryMimeData());
0118         drag->setObjectName(QLatin1StringView("SummaryWidgetDrag"));
0119 
0120         QPixmap pm = grab();
0121         if (pm.width() > 300) {
0122             pm = QPixmap::fromImage(pm.toImage().scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation));
0123         }
0124 
0125         QPainter painter;
0126         painter.begin(&pm);
0127         painter.setPen(QPalette::AlternateBase);
0128         painter.drawRect(0, 0, pm.width(), pm.height());
0129         painter.end();
0130         drag->setPixmap(pm);
0131         drag->exec(Qt::MoveAction);
0132     } else {
0133         QWidget::mouseMoveEvent(event);
0134     }
0135 }
0136 
0137 void Summary::dragEnterEvent(QDragEnterEvent *event)
0138 {
0139     if (event->mimeData()->hasFormat(QStringLiteral("application/x-kontact-summary"))) {
0140         event->acceptProposedAction();
0141     }
0142 }
0143 
0144 void Summary::dropEvent(QDropEvent *event)
0145 {
0146     const int alignment = (event->position().y() < (height() / 2) ? Qt::AlignTop : Qt::AlignBottom);
0147     Q_EMIT summaryWidgetDropped(this, event->source(), alignment);
0148 }
0149 
0150 #include <summary.moc>
0151 
0152 #include "moc_summary.cpp"