File indexing completed on 2024-05-19 09:33:50
0001 /*************************************************************************** 0002 * Copyright (C) 2015 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * This file is part of rcm * 0006 * * 0007 * Rolisteam is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published by * 0009 * the Free Software Foundation; either version 2 of the License, or * 0010 * (at your option) any later version. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0015 * GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License * 0018 * along with this program; if not, write to the * 0019 * Free Software Foundation, Inc., * 0020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0021 ***************************************************************************/ 0022 #include "imageselectordialog.h" 0023 #include "ui_imageselectordialog.h" 0024 0025 #include <QDragEnterEvent> 0026 #include <QDropEvent> 0027 #include <QFileDialog> 0028 #include <QFileInfo> 0029 #include <QMimeData> 0030 #include <QPixmap> 0031 #include <QPushButton> 0032 #include <QUrl> 0033 0034 #include "controller/view_controller/imageselectorcontroller.h" 0035 #include "worker/iohelper.h" 0036 #include "worker/utilshelper.h" 0037 #include "rwidgets/customs/overlay.h" 0038 0039 ImageSelectorDialog::ImageSelectorDialog(ImageSelectorController* ctrl, QWidget* parent, const QString& defaultPath) 0040 : QDialog(parent), m_ctrl(ctrl), ui(new Ui::ImageSelectorDialog), m_overlay(new Overlay()) 0041 { 0042 ui->setupUi(this); 0043 if(!defaultPath.isEmpty()) 0044 ui->m_textEdit->setText(defaultPath); 0045 0046 auto download= [this]() { m_ctrl->downloadImageFrom(QUrl::fromUserInput(ui->m_textEdit->text())); }; 0047 connect(ui->m_textEdit, &QLineEdit::textEdited, m_ctrl, download); 0048 connect(ui->m_downloadAct, &QAction::triggered, m_ctrl, download); 0049 connect(ui->m_pasteAct, &QAction::triggered, m_ctrl, &ImageSelectorController::imageFromClipboard); 0050 connect(ui->m_openFileAct, &QAction::triggered, this, &ImageSelectorDialog::openImage); 0051 connect(m_ctrl, &ImageSelectorController::titleChanged, this, 0052 [this]() { ui->m_titleLineEdit->setText(m_ctrl->title()); }); 0053 connect(ui->m_titleLineEdit, &QLineEdit::textEdited, this, 0054 [this]() { m_ctrl->setTitle(ui->m_titleLineEdit->text()); }); 0055 0056 0057 auto checkButton = [this] { 0058 auto button= ui->buttonBox->button(QDialogButtonBox::Ok); 0059 if(button) 0060 button->setEnabled(m_ctrl->validData() && m_ctrl->rectInShape()); 0061 }; 0062 0063 0064 auto func= [this, checkButton](const QRect& rect) { 0065 // qreal scale= m_ctrl->pixmap().size().width() / m_imageViewerLabel->rect().width(); 0066 // QRect scaledRect(rect.x() * scale, rect.y() * scale, rect.width() * scale, rect.height() * scale); 0067 0068 m_ctrl->setRect(rect); 0069 checkButton(); 0070 }; 0071 connect(m_overlay.get(), &Overlay::selectedRectChanged, m_ctrl, func); 0072 0073 0074 setAcceptDrops(m_ctrl->canDrop()); 0075 0076 connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0077 connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0078 0079 ui->m_downloadPush->setDefaultAction(ui->m_downloadAct); 0080 ui->m_pasteBtn->setDefaultAction(ui->m_pasteAct); 0081 ui->m_openBtn->setDefaultAction(ui->m_openFileAct); 0082 0083 ui->m_downloadPush->setVisible(m_ctrl->canDownload()); 0084 ui->m_pasteBtn->setVisible(m_ctrl->canPaste()); 0085 0086 connect(m_ctrl, &ImageSelectorController::contentToPasteChanged, ui->m_pasteAct, 0087 [this]() { ui->m_pasteAct->setEnabled(m_ctrl->hasContentToPaste()); }); 0088 0089 ui->m_pasteAct->setEnabled(m_ctrl->hasContentToPaste()); 0090 0091 ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0092 ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0093 0094 ui->scrollArea->setAlignment(Qt::AlignCenter); 0095 m_imageViewerLabel= new QLabel(this); 0096 auto pix= QPixmap(QString::fromUtf8(":/resources/images/preview.png")); 0097 m_imageViewerLabel->setPixmap(pix); 0098 m_imageViewerLabel->setLineWidth(0); 0099 m_imageViewerLabel->setFrameStyle(QFrame::NoFrame); 0100 m_imageViewerLabel->setScaledContents(true); 0101 m_imageViewerLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); 0102 m_imageViewerLabel->resize(pix.size()); 0103 m_overlay->setParent(m_imageViewerLabel); 0104 0105 m_overlay->resize(m_imageViewerLabel->rect().size()); 0106 0107 // m_overlay->setSelectedRect(m_imageViewerLabel->rect()); 0108 0109 m_overlay->setRatio(m_ctrl->shape() == ImageSelectorController::Square ? Overlay::Ratio::Ratio_Square : 0110 Overlay::Ratio::Ratio_Unconstrained); 0111 m_overlay->initRect(); 0112 0113 connect(m_ctrl, &ImageSelectorController::pixmapChanged, this, [this, checkButton]() { 0114 if(m_ctrl->isMovie()) 0115 { 0116 auto movie= m_ctrl->movie(); 0117 m_imageViewerLabel->setMovie(movie); 0118 m_imageViewerLabel->resize(movie->frameRect().size()); 0119 } 0120 else 0121 { 0122 auto pix= m_ctrl->pixmap(); 0123 m_imageViewerLabel->setPixmap(pix); 0124 m_imageViewerLabel->resize(pix.size()); 0125 } 0126 if(!m_ctrl->rect().isEmpty()) 0127 m_ctrl->setRect(m_overlay->selectedRect()); 0128 0129 checkButton(); 0130 0131 m_overlay->setVisible(!m_ctrl->dataInShape()); 0132 resizeLabel(); 0133 update(); 0134 }); 0135 0136 ui->scrollArea->setWidget(m_imageViewerLabel); 0137 0138 if(m_ctrl->askPath()) 0139 { 0140 openImage(); 0141 } 0142 if(m_overlay) 0143 m_ctrl->setRect(m_overlay->selectedRect()); 0144 } 0145 0146 ImageSelectorDialog::~ImageSelectorDialog() 0147 { 0148 delete ui; 0149 } 0150 0151 void ImageSelectorDialog::openImage() 0152 { 0153 auto filename 0154 = QFileDialog::getOpenFileName(this, tr("Open Image file"), m_ctrl->currentDir(), 0155 QString("Image File (%1)").arg(helper::utils::allSupportedImageFormatFilter())); 0156 0157 if(!filename.isEmpty()) 0158 { 0159 m_ctrl->setAddress(filename); 0160 ui->m_textEdit->setText(filename); 0161 m_ctrl->openImageFromFile(); 0162 } 0163 } 0164 0165 void ImageSelectorDialog::resizeLabel() 0166 { 0167 if(!isVisible()) 0168 return; 0169 0170 m_ctrl->setVisualSize(ui->scrollArea->viewport()->rect().size()); 0171 auto thumbnail = m_ctrl->thumbnail(); 0172 0173 m_imageViewerLabel->resize(thumbnail.size()); 0174 m_overlay->resize(thumbnail.size()); 0175 m_overlay->setSelectedRect(thumbnail.rect()); 0176 0177 0178 /*auto pix= m_ctrl->pixmap(); 0179 auto const sImg= pix.size(); 0180 0181 int w= ui->scrollArea->viewport()->rect().width(); 0182 int h= ui->scrollArea->viewport()->rect().height(); 0183 0184 if(sImg.width() < w) 0185 { 0186 w= sImg.width(); 0187 } 0188 if(sImg.height() < h) 0189 { 0190 h= sImg.height(); 0191 } 0192 0193 double const ratioImage= static_cast<double>(pix.size().width()) / pix.size().height(); 0194 double const ratioImageBis= static_cast<double>(pix.size().height()) / pix.size().width(); 0195 0196 if(w > h * ratioImage) 0197 { 0198 m_imageViewerLabel->resize(h * ratioImage, h); 0199 } 0200 else 0201 { 0202 m_imageViewerLabel->resize(w, w * ratioImageBis); 0203 } 0204 0205 m_overlay->resize(m_imageViewerLabel->rect().size()); 0206 m_overlay->setSelectedRect(m_overlay->rect());*/ 0207 } 0208 0209 void ImageSelectorDialog::resizeEvent(QResizeEvent* event) 0210 { 0211 resizeLabel(); 0212 QDialog::resizeEvent(event); 0213 } 0214 0215 void ImageSelectorDialog::dragEnterEvent(QDragEnterEvent* event) 0216 { 0217 if(event->mimeData()->hasUrls() || event->mimeData()->hasImage()) 0218 { 0219 event->acceptProposedAction(); 0220 } 0221 QDialog::dragEnterEvent(event); 0222 } 0223 0224 void ImageSelectorDialog::dropEvent(QDropEvent* event) 0225 { 0226 const QMimeData* data= event->mimeData(); 0227 if(!data->hasUrls() && !data->hasImage()) 0228 return; 0229 0230 QList<QUrl> list= data->urls(); 0231 auto img= qvariant_cast<QImage>(data->imageData()); 0232 if(!img.isNull()) 0233 { 0234 m_ctrl->setAddress({}); 0235 m_ctrl->setImageData(IOHelper::imageToData(img)); 0236 } 0237 else if(!list.isEmpty()) 0238 { 0239 auto first= list.first(); 0240 m_ctrl->downloadImageFrom(first); 0241 } 0242 event->acceptProposedAction(); 0243 } 0244 0245 void ImageSelectorDialog::showEvent(QShowEvent* event) 0246 { 0247 QDialog::showEvent(event); 0248 resizeLabel(); 0249 }