Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-webphotoalbum-git / original.cpp @ b489456c

Historique | Voir | Annoter | Télécharger (12,9 ko)

1
/**
2
  * Classe
3
  * @see https://redmine.ryxeo.com/projects/
4
  * @author 2012 Eric Seigne <eric.seigne@ryxeo.com>
5
  * @see The GNU Public License (GNU/GPL) v3
6
  *
7
  *
8
  *
9
  * This program is free software; you can redistribute it and/or modify
10
  * it under the terms of the GNU General Public License as published by
11
  * the Free Software Foundation; either version 3 of the License, or
12
  * (at your option) any later version.
13
  *
14
  * This program is distributed in the hope that it will be useful, but
15
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17
  * for more details.
18
  *
19
  * You should have received a copy of the GNU General Public License along
20
  * with this program. If not, see <http://www.gnu.org/licenses/>.
21
  */
22

    
23

    
24
#include "original.h"
25
#include "ui_original.h"
26

    
27

    
28
//---------------------------------------------------------------------------
29
QString formatSize(qint64 num)
30
{
31
    QString total;
32
    const qint64 kb = 1024;
33
    const qint64 mb = 1024 * kb;
34
    const qint64 gb = 1024 * mb;
35
    const qint64 tb = 1024 * gb;
36

    
37
    if (num >= tb) total = QString("%1TB").arg(QString::number(qreal(num) / tb, 'f', 2));
38
    else if(num >= gb) total = QString("%1GB").arg(QString::number(qreal(num) / gb, 'f', 2));
39
    else if(num >= mb) total = QString("%1MB").arg(QString::number(qreal(num) / mb, 'f', 1));
40
    else if(num >= kb) total = QString("%1KB").arg(QString::number(qreal(num) / kb,'f', 1));
41
    else total = QString("%1 bytes").arg(num);
42

    
43
    return total;
44
}
45

    
46
// =========================================================================================================================
47

    
48

    
49
original::original(QWidget *parent) :
50
    QMainWindow(parent),
51
    ui(new Ui::original)
52
{
53
    ui->setupUi(this);
54

    
55
    ui->stackedWidget->setCurrentWidget(ui->page);
56

    
57
    QDir dir(QDir::homePath() + "/RyXeo-WebPhotoAlbum");
58
    dir.setFilter(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
59
    QFileInfoList list = dir.entryInfoList();
60
    for(int i = 0; i < list.count(); i++) {
61
        QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
62
        item->setText(0, list.at(i).fileName());
63
        item->setData(1,0,list.at(i).absoluteFilePath());
64
    }
65

    
66
    //============== le menu contextuel du treewidget (gauche)
67
    m_menu = new QMenu(ui->treeWidget);
68
    QList<QAction*> actions;
69

    
70
    QAction *a_nouveau = new QAction(QIcon(":/images/folder_add.png"),trUtf8("&Nouvel album"),m_menu);
71
    a_nouveau->setIconVisibleInMenu(true);
72
    a_nouveau->connect(a_nouveau, SIGNAL(triggered()), this, SLOT(on_action_Nouvel_album_triggered()));
73

    
74
    QAction *a_renommer = new QAction(QIcon(":/images/folder_edit.png"),trUtf8("&Renommer..."),m_menu);
75
    a_renommer->setIconVisibleInMenu(true);
76
    //a_renommer->connect(a_renommer, SIGNAL(triggered()), this, SLOT(on_action_Nouvel_album_triggered()));
77

    
78
    QAction *a_supprimer = new QAction(QIcon(":/images/folder_delete.png"),trUtf8("&Supprimer..."),m_menu);
79
    a_supprimer->setIconVisibleInMenu(true);
80
    //a_supprimer->connect(a_supprimer, SIGNAL(triggered()), this, SLOT(on_action_Nouvel_album_triggered()));
81

    
82
    actions << a_nouveau << a_renommer << a_supprimer;
83
    m_menu->addActions(actions);
84
}
85

    
86
original::~original()
87
{
88
    delete ui;
89
}
90

    
91
void original::on_btnExport_clicked()
92
{
93
    //Creation de la destination
94
    QDir rep;
95
    QString dest = ui->leDest->text().trimmed() + "/web-gallery";
96
    rep.mkpath(dest);
97
    rep.mkpath(dest + "/hq");
98
    rep.mkpath(dest + "/lq");
99
    rep.mkpath(dest + "/mq");
100
    rep.mkpath(dest + "/thumbs");
101
    rep.mkpath(dest + "/zip");
102
    rep.mkpath(dest + "/comments");
103

    
104
    //Creation des images
105
    QDir dir(ui->leSource->text().trimmed());
106
    dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
107
    QFileInfoList list = dir.entryInfoList();
108
    for(int i = 0; i < list.count(); i++) {
109
        ui->statusBar->clearMessage();
110
        ui->statusBar->showMessage(trUtf8("Fichier en cours: %1").arg(list.at(i).fileName()),3000);
111
        qApp->processEvents();
112

    
113
        qDebug() << "Processing : " << list.at(i).absoluteFilePath();
114
        QPixmap pixmap(list.at(i).absoluteFilePath());
115
        pixmap.save(QString("%1/hq/img-%2.jpg").arg(dest).arg(i));
116

    
117
        ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq]").arg(list.at(i).fileName()),3000);
118
        QPixmap pixmapThumbs(pixmap.scaled(QSize(120,120), Qt::KeepAspectRatio, Qt::SmoothTransformation));
119
        pixmapThumbs.save(QString("%1/thumbs/img-%2.jpg").arg(dest).arg(i));
120

    
121
        ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq]").arg(list.at(i).fileName()),3000);
122
        QPixmap pixmapMQ(pixmap.scaled(QSize(800,600), Qt::KeepAspectRatio, Qt::SmoothTransformation));
123
        pixmapMQ.save(QString("%1/mq/img-%2.jpg").arg(dest).arg(i));
124

    
125
        ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq] [lq]").arg(list.at(i).fileName()),3000);
126
        QPixmap pixmapLQ(pixmap.scaled(QSize(640,480), Qt::KeepAspectRatio, Qt::SmoothTransformation));
127
        pixmapLQ.save(QString("%1/lq/img-%2.jpg").arg(dest).arg(i));
128

    
129

    
130
        QFile file(QString("%1/comments/%2.txt").arg(dest).arg(i));
131
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
132
            return;
133

    
134
        QTextStream out(&file);
135
        out << "<span>Photo " + QString::number(i) + "</span>\n";
136
        file.close();
137
        ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq] [lq] [meta]").arg(list.at(i).fileName()),3000);
138

    
139
        //================== on copie les metadata
140
        Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(list.at(i).absoluteFilePath().toStdString());
141
        Exiv2::Image::AutoPtr imageLQ = Exiv2::ImageFactory::open(QString("%1/lq/img-%2.jpg").arg(dest).arg(i).toStdString());
142
        Exiv2::Image::AutoPtr imageMQ = Exiv2::ImageFactory::open(QString("%1/mq/img-%2.jpg").arg(dest).arg(i).toStdString());
143
        Exiv2::Image::AutoPtr imageHQ = Exiv2::ImageFactory::open(QString("%1/hq/img-%2.jpg").arg(dest).arg(i).toStdString());
144
        Exiv2::Image::AutoPtr imageThumb = Exiv2::ImageFactory::open(QString("%1/thumbs/img-%2.jpg").arg(dest).arg(i).toStdString());
145
        if (image.get() != 0)
146
        {
147
            image->readMetadata();
148
            Exiv2::ExifData &exifData = image->exifData();
149
            if (!exifData.empty())
150
            {
151
                bool setArtist = false;
152
                bool setCopyright = false;
153
                bool setSoftware = false;
154
                Exiv2::ExifData::const_iterator end = exifData.end();
155
                qDebug() << "============================ DEBUT ===============================";
156
                for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i)
157
                {
158
                    qDebug() << i->key().c_str() << " = " << i->value().toString().c_str();
159
                    if (i->key() == "Exif.Image.Artist") {
160
                        exifData["Exif.Image.Artist"]    = "Eric S.";
161
                        setArtist = true;
162
                    }
163
                    if (i->key() == "Exif.Image.Copyright") {
164
                        exifData["Exif.Image.Copyright"]    = "Eric S.";
165
                        setCopyright = true;
166
                    }
167
                    if (i->key() == "Exif.Image.Software") {
168
                        exifData["Exif.Image.Software"]  = "RyXeo WebPhotoAlbum - http://www.ryxeo.com/";
169
                        setSoftware = true;
170
                    }
171
                }
172
                //Si on n'a pas les meta il faut les ajouter
173

    
174
                //Mise à jour de l'auteur
175
                if(!setArtist) {
176
                    Exiv2::ExifKey k("Exif.Image.Artist");
177
                    Exiv2::Value::AutoPtr v = Exiv2::Value::create(Exiv2::asciiString);
178
                    v->read("Eric S.");
179
                    exifData.add(k,v.get());
180
                }
181

    
182
                if(!setCopyright) {
183
                    Exiv2::ExifKey k2("Exif.Image.Copyright");
184
                    Exiv2::Value::AutoPtr v2 = Exiv2::Value::create(Exiv2::asciiString);
185
                    v2->read("Eric S.");
186
                    exifData.add(k2,v2.get());
187
                }
188

    
189
                if(!setSoftware) {
190
                    Exiv2::ExifKey k3("Exif.Image.Software");
191
                    Exiv2::Value::AutoPtr v3 = Exiv2::Value::create(Exiv2::asciiString);
192
                    v3->read("RyXéo WebPhotoAlbum - http://www.ryxeo.com/");
193
                    exifData.add(k3,v3.get());
194
                }
195

    
196
                qDebug() << "============================ APRES ===============================";
197
                Exiv2::ExifData::const_iterator end2 = exifData.end();
198
                for (Exiv2::ExifData::const_iterator i2 = exifData.begin(); i2 != end2; ++i2)
199
                {
200
                    qDebug() << i2->key().c_str() << " = " << i2->value().toString().c_str();
201
                }
202
                qDebug() << "============================ FIN ===============================";
203

    
204
                imageLQ->setExifData(exifData);
205
                imageLQ->writeMetadata();
206

    
207
                imageMQ->setExifData(exifData);
208
                imageMQ->writeMetadata();
209

    
210
                imageHQ->setExifData(exifData);
211
                imageHQ->writeMetadata();
212

    
213
                imageThumb->setExifData(exifData);
214
                imageThumb->writeMetadata();
215
            }
216
        }
217
        ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq] [lq] [meta] ... terminé").arg(list.at(i).fileName()),3000);
218
    }
219
}
220

    
221
void original::on_action_Nouvel_album_triggered()
222
{
223
    QTreeWidgetItem *i = new QTreeWidgetItem(ui->treeWidget);
224
    i->setText(0, trUtf8("Nouvel album"));
225
}
226

    
227
void original::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
228
{
229
    ui->lwPictures->clear();
230
    QDir dir(item->data(1,0).toString()+"/thumbs/");
231
    dir.setFilter(QDir::Files);
232
    dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
233
    QFileInfoList list = dir.entryInfoList();
234
    for(int i = 0; i < list.count(); i++) {
235
        qDebug() << list.at(i).fileName();
236
        QListWidgetItem *newitem = new QListWidgetItem();
237
        QIcon icone(list.at(i).absoluteFilePath());//pour la mettre  à coté de l'item
238
        newitem->setIcon(icone); // ajout de la petite icone sur l'item
239
        newitem->setText(list.at(i).fileName());
240
        ui->lwPictures->insertItem(i,newitem);
241
    }
242
}
243

    
244
void original::on_commandLinkButton_clicked()
245
{
246
    if(ui->stackedWidget->currentWidget() == ui->page_2)
247
        ui->stackedWidget->setCurrentWidget(ui->page);
248
    else {
249
        QFileInfo fi(ui->treeWidget->currentItem()->data(1,0).toString()+"/info.txt");
250
        qDebug() << "Lecture de " << fi.absoluteFilePath();
251
        if(fi.exists()) {
252
            QFile file(fi.absoluteFilePath());
253
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
254
                return;
255

    
256
            /* Exemple de fichier
257
            name|Sortie Plongée SAGC. (Cala Montjoi, Spain) 28.05.2012
258
            author|Éric Seigne
259
            description|Sortie Plongée SAGC. (Cala Montjoi, Spain)
260
            date|28.05.2012
261
            restricted_user|sagc
262
            restricted_password|motdepasse
263
              */
264
            while (!file.atEnd()) {
265
                QByteArray line = file.readLine();
266
                QStringList tab = QString::fromUtf8(line.constData()).split('|');
267
                if(tab.at(0) == "name")
268
                    ui->leTitre->setText(tab.at(1));
269
                if(tab.at(0) == "author")
270
                    ui->leAuthor->setText(tab.at(1));
271
                if(tab.at(0) == "description")
272
                    ui->leDesc->setText(tab.at(1));
273
                if(tab.at(0) == "date")
274
                    ui->dateEdit->setDate(QDate::fromString(tab.at(1)));
275
                if(tab.at(0) == "restricted_user")
276
                    ui->leLogin->setText(tab.at(1));
277
                if(tab.at(0) == "restricted_password")
278
                    ui->lePasswd->setText(tab.at(1));
279

    
280
            }
281

    
282
        }
283
        ui->stackedWidget->setCurrentWidget(ui->page_2);
284
    }
285
}
286

    
287
void original::on_btnSave_clicked()
288
{
289
    QByteArray data;
290
    data.append(QString("name|%1\n").arg(ui->leTitre->text().trimmed()));
291
    data.append(QString("author|%1\n").arg(ui->leAuthor->text().trimmed()));
292
    data.append(QString("description|%1\n").arg(ui->leDesc->text().trimmed()));
293
    data.append(QString("date|%1\n").arg(ui->dateEdit->text().trimmed()));
294
    if(ui->leLogin->text().trimmed() != "") {
295
        data.append(QString("restricted_user|%1\n").arg(ui->leLogin->text().trimmed()));
296
        data.append(QString("restricted_password|%1\n").arg(ui->lePasswd->text().trimmed()));
297
    }
298
    QFileInfo fi(ui->treeWidget->currentItem()->data(1,0).toString()+"/info.txt");
299
    qDebug() << "Ecriture de " << fi.absoluteFilePath();
300
    QFile file(fi.absoluteFilePath());
301
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
302
        return;
303
    QTextStream out(&file);
304
    out.setCodec("UTF-8");
305
    out << data;
306
    file.close();
307
}
308

    
309

    
310
void original::on_treeWidget_customContextMenuRequested(const QPoint &pos)
311
{
312
    m_menu->exec(ui->treeWidget->mapToGlobal(pos));
313
}
Redmine Appliance - Powered by TurnKey Linux