1 /**********************************************************************************************
2     Copyright (C) 2014 Oliver Eichler <oliver.eichler@gmx.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #include "helpers/CPhotoViewer.h"
20 #include "helpers/CSettings.h"
21 #include "widgets/CPhotoAlbum.h"
22 
23 #include <QtWidgets>
24 
CPhotoAlbum(QWidget * parent)25 CPhotoAlbum::CPhotoAlbum(QWidget* parent)
26     : QWidget(parent)
27 {
28     setupUi(this);
29     setFocusPolicy(Qt::WheelFocus);
30     connect(toolLeft, &QToolButton::clicked, this, &CPhotoAlbum::slotLeft);
31     connect(toolRight, &QToolButton::clicked, this, &CPhotoAlbum::slotRight);
32 }
33 
~CPhotoAlbum()34 CPhotoAlbum::~CPhotoAlbum()
35 {
36 }
37 
resizeEvent(QResizeEvent * e)38 void CPhotoAlbum::resizeEvent(QResizeEvent* e)
39 {
40     QWidget::resizeEvent(e);
41     updateView();
42 }
43 
44 
mouseReleaseEvent(QMouseEvent * e)45 void CPhotoAlbum::mouseReleaseEvent(QMouseEvent* e)
46 {
47     CPhotoViewer dlg(images, 0, this);
48     dlg.exec();
49 
50     e->accept();
51 }
52 
reload(const QList<CGisItemWpt::image_t> & imgs)53 void CPhotoAlbum::reload(const QList<CGisItemWpt::image_t>& imgs)
54 {
55     images = imgs;
56 
57     if(idxSelected >= images.size())
58     {
59         idx1stVisible = 0;
60         idxSelected = 0;
61     }
62 
63     updateView();
64 }
65 
slotAddImage()66 void CPhotoAlbum::slotAddImage()
67 {
68     SETTINGS;
69     QString path = cfg.value("Paths/lastWptImagePath", QDir::homePath()).toString();
70     QString filters = "All Files (*);; All Images (*.png *.jpg);; PNG Image (*.png);; JPEG Image (*.jpg)";
71     QString defaultFilter = "All Images (*.png *.jpg)";
72 
73     const QStringList& filenames = QFileDialog::getOpenFileNames(this, tr("Select images..."), path, filters, &defaultFilter);
74     if(filenames.isEmpty())
75     {
76         return;
77     }
78     for(const QString& filename : filenames)
79     {
80         CGisItemWpt::image_t image;
81         image.fileName = filename;
82         if(image.pixmap.load(filename))
83         {
84             int w = image.pixmap.width();
85             int h = image.pixmap.height();
86 
87             if(w < h)
88             {
89                 h *= 400.0 / w;
90                 w = 400;
91             }
92             else
93             {
94                 h *= 600.0 / w;
95                 w = 600;
96             }
97             image.pixmap = image.pixmap.scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation);
98 
99             images << image;
100         }
101         else
102         {
103             qDebug() << "Cannot load image from file " << filename;
104         }
105     }
106 
107     QFileInfo fi(filenames.first());
108     path = fi.absolutePath();
109     cfg.setValue("Paths/lastWptImagePath", path);
110 
111     emit sigChanged(images);
112 }
113 
slotDelImage()114 void CPhotoAlbum::slotDelImage()
115 {
116     images.removeAt(idxSelected);
117     emit sigChanged(images);
118 }
119 
slotRight()120 void CPhotoAlbum::slotRight()
121 {
122     idxSelected++;
123     QRect r1 = rects[idxSelected];
124     QRect r2 = label->rect();
125 
126     while(!r2.contains(r1))
127     {
128         int w = rects[idx1stVisible].width();
129         r1.moveLeft(r1.left() - w);
130         idx1stVisible++;
131     }
132 
133     updateView();
134 }
135 
slotLeft()136 void CPhotoAlbum::slotLeft()
137 {
138     idxSelected--;
139     QRect r1 = rects[idxSelected];
140     QRect r2 = label->rect();
141 
142     while(!r2.contains(r1))
143     {
144         idx1stVisible--;
145         int w = rects[idx1stVisible].width();
146         r1.moveLeft(r1.left() + w);
147     }
148 
149     updateView();
150 }
151 
152 
updateView()153 void CPhotoAlbum::updateView()
154 {
155     toolLeft->setEnabled(idxSelected != 0);
156     toolRight->setEnabled(idxSelected != (images.size() - 1));
157 
158     if(images.isEmpty())
159     {
160         hide();
161         return;
162     }
163     setEnabled(true);
164     show();
165 
166     QPixmap img(label->size());
167     img.fill(Qt::black);
168     QPainter p(&img);
169 
170     int xoff = 0;
171 
172     for(int i = 0; i < rects.size() && i < idx1stVisible; i++)
173     {
174         xoff -= rects[i].width();
175     }
176 
177     rects.clear();
178     for(int i = 0; i < images.size(); i++)
179     {
180         CGisItemWpt::image_t& image = images[i];
181 
182         QImage tmp = image.pixmap.scaledToHeight(label->height(), Qt::SmoothTransformation);
183 
184         if(tmp.width() > label->width())
185         {
186             tmp = image.pixmap.scaledToWidth(label->width(), Qt::SmoothTransformation);
187         }
188 
189         QRect r = tmp.rect();
190 
191         int yoff = (height() - r.height()) / 2;
192 
193         p.save();
194         p.translate(xoff, yoff);
195         p.drawImage(0, 0, tmp);
196         p.setPen(QPen(Qt::black, 3));
197         p.setBrush(Qt::NoBrush);
198         p.drawRect(r);
199         p.restore();
200 
201         r.moveTopLeft(QPoint(xoff, yoff));
202         rects << r;
203 
204         xoff += tmp.width();
205     }
206 
207     if(idxSelected < rects.size())
208     {
209         p.setPen(QPen(Qt::yellow, 5));
210         p.drawRect(rects[idxSelected]);
211     }
212 
213     label->setPixmap(img);
214 }
215