File indexing completed on 2025-01-05 03:52:00
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2003-11-03 0007 * Description : Calendar month image selection widget. 0008 * 0009 * SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com> 0010 * SPDX-FileCopyrightText: 2006 by Tom Albers <tomalbers at kde dot nl> 0011 * SPDX-FileCopyrightText: 2007-2008 by Orgad Shaneh <orgads at gmail dot com> 0012 * SPDX-FileCopyrightText: 2012 by Angelo Naselli <anaselli at linux dot it> 0013 * SPDX-FileCopyrightText: 2012-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0014 * 0015 * SPDX-License-Identifier: GPL-2.0-or-later 0016 * 0017 * ============================================================ */ 0018 0019 #include "calmonthwidget.h" 0020 0021 // Qt includes 0022 0023 #include <QDragEnterEvent> 0024 #include <QFileInfo> 0025 #include <QImageReader> 0026 #include <QMouseEvent> 0027 #include <QPaintEvent> 0028 #include <QPainter> 0029 #include <QStandardPaths> 0030 #include <QMimeData> 0031 #include <QUrl> 0032 #include <QSize> 0033 #include <QLocale> 0034 0035 // Local includes 0036 0037 #include "calsettings.h" 0038 #include "imagedialog.h" 0039 #include "digikam_debug.h" 0040 0041 namespace DigikamGenericCalendarPlugin 0042 { 0043 0044 class Q_DECL_HIDDEN CalMonthWidget::Private 0045 { 0046 public: 0047 0048 explicit Private() 0049 : thumbSize (QSize(64, 64)), 0050 month (0), 0051 thumbLoadThread(ThumbnailLoadThread::defaultThread()) 0052 { 0053 } 0054 0055 const QSize thumbSize; 0056 QPixmap thumb; 0057 int month; 0058 QUrl imagePath; 0059 ThumbnailLoadThread* thumbLoadThread; 0060 }; 0061 0062 CalMonthWidget::CalMonthWidget(QWidget* const parent, int month) 0063 : QPushButton(parent), 0064 d(new Private) 0065 { 0066 setAcceptDrops(true); 0067 setFixedSize(QSize(74, 94)); 0068 d->month = month; 0069 d->imagePath = QUrl(); 0070 setThumb(QPixmap(QIcon::fromTheme(QLatin1String("view-preview")) 0071 .pixmap(32, QIcon::Disabled))); 0072 0073 connect(d->thumbLoadThread, SIGNAL(signalThumbnailLoaded(LoadingDescription,QPixmap)), 0074 this, SLOT(slotThumbnail(LoadingDescription,QPixmap))); 0075 0076 connect(this, SIGNAL(pressed()), 0077 this, SLOT(slotMonthSelected())); 0078 } 0079 0080 CalMonthWidget::~CalMonthWidget() 0081 { 0082 delete d; 0083 } 0084 0085 QUrl CalMonthWidget::imagePath() const 0086 { 0087 return d->imagePath; 0088 } 0089 0090 void CalMonthWidget::paintEvent(QPaintEvent* e) 0091 { 0092 QPushButton::paintEvent(e); 0093 0094 QPainter painter(this); 0095 QString name = QLocale().standaloneMonthName(d->month, QLocale::ShortFormat); 0096 QRect cr = contentsRect(); 0097 cr.setBottom(70); 0098 painter.drawPixmap(cr.width() / 2 - d->thumb.width() / 2, 0099 cr.height() / 2 - d->thumb.height() / 2, 0100 d->thumb); 0101 0102 cr = contentsRect(); 0103 cr.setTop(70); 0104 painter.drawText(cr, Qt::AlignHCenter, name); 0105 } 0106 0107 void CalMonthWidget::dragEnterEvent(QDragEnterEvent* e) 0108 { 0109 if (e->mimeData()->hasImage()) 0110 { 0111 e->acceptProposedAction(); 0112 } 0113 } 0114 0115 QPixmap CalMonthWidget::thumb() const 0116 { 0117 return d->thumb; 0118 } 0119 0120 int CalMonthWidget::month() 0121 { 0122 return d->month; 0123 } 0124 0125 void CalMonthWidget::setThumb(const QPixmap& pic) 0126 { 0127 d->thumb = pic.scaled(d->thumbSize, Qt::KeepAspectRatio); 0128 update(); 0129 } 0130 0131 void CalMonthWidget::setImage(const QUrl& url) 0132 { 0133 if (!url.isValid()) 0134 { 0135 return; 0136 } 0137 0138 d->imagePath = url; 0139 CalSettings::instance()->setImage(d->month, d->imagePath); 0140 0141 d->thumbLoadThread->find(ThumbnailIdentifier(url.toLocalFile()), d->thumbSize.width()); 0142 } 0143 0144 void CalMonthWidget::dropEvent(QDropEvent* e) 0145 { 0146 QList<QUrl> srcURLs = e->mimeData()->urls(); 0147 0148 if (srcURLs.isEmpty()) 0149 { 0150 return; 0151 } 0152 0153 QUrl url = srcURLs.first(); 0154 setImage(url); 0155 } 0156 0157 void CalMonthWidget::slotMonthSelected() 0158 { 0159 Q_EMIT monthSelected(d->month); 0160 } 0161 0162 void CalMonthWidget::slotThumbnail(const LoadingDescription& desc, const QPixmap& pix) 0163 { 0164 if (QUrl::fromLocalFile(desc.filePath) != d->imagePath) 0165 { 0166 return; 0167 } 0168 0169 setThumb(pix); 0170 } 0171 0172 void CalMonthWidget::mouseReleaseEvent(QMouseEvent* e) 0173 { 0174 if (!contentsRect().contains(e->pos())) 0175 { 0176 return; 0177 } 0178 0179 if (e->button() == Qt::LeftButton) 0180 { 0181 ImageDialog dlg(this, 0182 QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)), 0183 true); 0184 setImage(dlg.url()); 0185 } 0186 else if (e->button() == Qt::RightButton) 0187 { 0188 d->imagePath = QUrl(); 0189 CalSettings::instance()->setImage(d->month, d->imagePath); 0190 setThumb(QPixmap(QIcon::fromTheme(QLatin1String("view-preview")).pixmap(32, QIcon::Disabled))); 0191 } 0192 } 0193 0194 } // Namespace Digikam 0195 0196 #include "moc_calmonthwidget.cpp"