Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-webphotoalbum-git / original.cpp @ d044a969

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

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

    
22

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

    
26

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

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

    
42
    return total;
43
}
44

    
45
// =========================================================================================================================
46

    
47

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

    
54
    m_currentProjectPictureNumber = 0;
55

    
56
    //Hop pour que le splitter soit a peu pres a la bonne taille dès le début
57
    QList<int> sizes;
58
    sizes.append(200);
59
    sizes.append(500);
60
    ui->splitter->setSizes(sizes);
61

    
62
    m_baseDir = QDir(QDir::homePath() + "/RyXeo-Applications/WebPhotoAlbum");
63
    m_baseDir.setFilter(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
64
    QFileInfoList list = m_baseDir  .entryInfoList();
65
    for(int i = 0; i < list.count(); i++) {
66
        QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
67
        item->setText(0, list.at(i).fileName());
68
        item->setData(1,0,list.at(i).absoluteFilePath());
69
    }
70

    
71
    ui->dateEdit->setDate(QDate::currentDate());
72
    m_tools = new tools(m_baseDir, this);
73

    
74
    //============== l'étiquette invitant a déposer des images
75
    m_labelDeposez = new QLabel(trUtf8("Glissez-déposez ici les photos que vous voulez ajouter à cet album ..."),ui->lwPictures);
76
    m_labelDeposez->setStyleSheet("color: #aaa;\nfont-size: 24pt;\nfont-weight: bold;\n");
77
    m_labelDeposez->setScaledContents(true);
78
    m_labelDeposez->setWordWrap(true);
79
    m_labelDeposez->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
80
    m_labelDeposez->hide();
81

    
82
    //============== le menu contextuel du treewidget (gauche)
83
    m_menu = new QMenu(ui->treeWidget);
84
    QList<QAction*> actions;
85

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

    
90
    QAction *a_renommer = new QAction(QIcon(":/images/folder_edit.png"),trUtf8("&Renommer..."),m_menu);
91
    a_renommer->setIconVisibleInMenu(true);
92
    //a_renommer->connect(a_renommer, SIGNAL(triggered()), this, SLOT(on_action_Nouvel_album_triggered()));
93

    
94
    QAction *a_supprimer = new QAction(QIcon(":/images/folder_delete.png"),trUtf8("&Supprimer..."),m_menu);
95
    a_supprimer->setIconVisibleInMenu(true);
96
    //a_supprimer->connect(a_supprimer, SIGNAL(triggered()), this, SLOT(on_action_Nouvel_album_triggered()));
97

    
98
    actions << a_nouveau << a_renommer << a_supprimer;
99
    m_menu->addActions(actions);
100

    
101
    //============== Accepte le drag & drop
102
    ui->lwPictures->setDragEnabled(true);
103
    ui->lwPictures->setDropIndicatorShown(true);
104
}
105

    
106
original::~original()
107
{
108
    delete ui;
109
}
110

    
111
void original::on_action_Nouvel_album_triggered()
112
{
113
    m_currentProjectPictureNumber = 0;
114
    bool ok;
115
    QString text = NewAlbumDialog::getValue(this);
116
    //Si ce nom n'existe pas déjà ...
117
    QTreeWidgetItem *i = new QTreeWidgetItem(ui->treeWidget);
118
    i->setText(0, text);
119
    m_currentProjectDir = m_baseDir.absolutePath() + "/" + text;
120
    i->setData(1,0,m_currentProjectDir);
121

    
122
    //Creation de la destination
123
    QDir rep;
124
    rep.mkpath(m_currentProjectDir);
125
    rep.mkpath(m_currentProjectDir + "/hq");
126
    rep.mkpath(m_currentProjectDir + "/lq");
127
    rep.mkpath(m_currentProjectDir + "/mq");
128
    rep.mkpath(m_currentProjectDir + "/thumbs");
129
    rep.mkpath(m_currentProjectDir + "/zip");
130
    rep.mkpath(m_currentProjectDir + "/comments");
131

    
132
}
133

    
134
void original::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
135
{
136
    QFileInfo fi(ui->treeWidget->currentItem()->data(1,0).toString()+"/info.txt");
137
    qDebug() << "Lecture de " << fi.absoluteFilePath();
138

    
139
    //Remise a zero
140
    ui->leTitre->clear();
141
    ui->leAuthor->clear();
142
    ui->leDesc->clear();
143
    ui->dateEdit->clear();
144
    ui->leLogin->clear();
145
    ui->lePasswd->clear();
146

    
147
    if(fi.exists()) {
148
        QFile file(fi.absoluteFilePath());
149
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
150
            return;
151

    
152
        /* Exemple de fichier
153
                name|Sortie Plongée SAGC. (Cala Montjoi, Spain) 28.05.2012
154
                author|Éric Seigne
155
                description|Sortie Plongée SAGC. (Cala Montjoi, Spain)
156
                date|28.05.2012
157
                restricted_user|sagc
158
                restricted_password|motdepasse
159
                  */
160
        while (!file.atEnd()) {
161
            QByteArray line = file.readLine();
162
            QStringList tab = QString::fromUtf8(line.constData()).split('|');
163
            if(tab.at(0) == "name")
164
                ui->leTitre->setText(tab.at(1));
165
            if(tab.at(0) == "author")
166
                ui->leAuthor->setText(tab.at(1));
167
            if(tab.at(0) == "description")
168
                ui->leDesc->setText(tab.at(1));
169
            if(tab.at(0) == "date") {
170
                qDebug() << "**************** DATE : " << tab.at(1).trimmed();
171
                ui->dateEdit->setDate(QDate::fromString(tab.at(1).trimmed(),"dd.MM.yyyy"));
172
            }
173
            if(tab.at(0) == "restricted_user")
174
                ui->leLogin->setText(tab.at(1));
175
            if(tab.at(0) == "restricted_password")
176
                ui->lePasswd->setText(tab.at(1));
177

    
178
        }
179

    
180
    }
181
    m_currentProjectPictureNumber++;
182
    refreshPictureList();
183
    qDebug() << "On continue la numerotation a " << m_currentProjectPictureNumber;
184
    qDebug() << "Espace de stockage du projet " << m_currentProjectDir;
185
}
186

    
187
void original::on_btnSave_clicked()
188
{
189
    QByteArray data;
190
    data.append(QString("name|%1\n").arg(ui->leTitre->text().trimmed()));
191
    data.append(QString("author|%1\n").arg(ui->leAuthor->text().trimmed()));
192
    data.append(QString("description|%1\n").arg(ui->leDesc->text().trimmed()));
193
    data.append(QString("date|%1\n").arg(ui->dateEdit->date().toString("dd.MM.yyyy")));
194
    if(ui->leLogin->text().trimmed() != "") {
195
        data.append(QString("restricted_user|%1\n").arg(ui->leLogin->text().trimmed()));
196
        data.append(QString("restricted_password|%1\n").arg(ui->lePasswd->text().trimmed()));
197
    }
198
    QFileInfo fi(ui->treeWidget->currentItem()->data(1,0).toString()+"/info.txt");
199
    qDebug() << "Ecriture de " << fi.absoluteFilePath();
200
    QFile file(fi.absoluteFilePath());
201
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
202
        return;
203
    QTextStream out(&file);
204
    out.setCodec("UTF-8");
205
    out << data;
206
    file.close();
207
}
208

    
209

    
210
void original::on_treeWidget_customContextMenuRequested(const QPoint &pos)
211
{
212
    m_menu->exec(ui->treeWidget->mapToGlobal(pos));
213
}
214

    
215
void original::dragEnterEvent(QDragEnterEvent *event)
216
{
217
    if (event->mimeData()->hasUrls())
218
    {
219
        event->acceptProposedAction();
220
    }
221
    else
222
    {
223
        event->ignore();
224
    }
225
}
226

    
227
void original::dropEvent(QDropEvent *event)
228
{
229
    qDebug() << "Accepte le drop d'une image";
230

    
231
    if(event->mimeData()->hasUrls())
232
    {
233
        for(int i = 0; i < event->mimeData()->urls().count(); i++) {
234
            //recopier l'image dans le projet
235
            qDebug() << "Windows d&drop : " << event->mimeData()->urls().at(i).toString();
236

    
237
            //Attention sous windows le drag & drop est un file:/// sous linux file:// et osx ?
238
#ifdef Q_OS_WIN32
239
            addPictureToProject(event->mimeData()->urls().at(i).toString().remove("file:///"));
240
#endif
241
#ifdef Q_OS_LINUX
242
            addPictureToProject(event->mimeData()->urls().at(i).toString().remove("file://"));
243
#endif
244
            qDebug() << "Accepte le drop d'une image";
245
            event->acceptProposedAction();
246
        }
247
    }
248
}
249

    
250
void original::addPictureToProject(QString fic)
251
{
252
    ui->statusBar->clearMessage();
253
    ui->statusBar->showMessage(trUtf8("Fichier en cours: %1").arg(fic),3000);
254
    qApp->processEvents();
255

    
256
    if(m_currentProjectPictureNumber == 0)
257
        m_currentProjectPictureNumber = 1;
258

    
259
    qDebug() << "Processing : " << fic;
260
    QPixmap pixmap(fic);
261
    pixmap.save(QString("%1/hq/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber));
262
    qDebug() << "Save to : " << QString("%1/hq/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber);
263

    
264
    ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq]").arg(fic),3000);
265
    QPixmap pixmapThumbs(pixmap.scaled(QSize(120,120), Qt::KeepAspectRatio, Qt::SmoothTransformation));
266
    pixmapThumbs.save(QString("%1/thumbs/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber));
267

    
268
    ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq]").arg(fic),3000);
269
    QPixmap pixmapMQ(pixmap.scaled(QSize(800,600), Qt::KeepAspectRatio, Qt::SmoothTransformation));
270
    pixmapMQ.save(QString("%1/mq/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber));
271

    
272
    ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq] [lq]").arg(fic),3000);
273
    QPixmap pixmapLQ(pixmap.scaled(QSize(640,480), Qt::KeepAspectRatio, Qt::SmoothTransformation));
274
    pixmapLQ.save(QString("%1/lq/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber));
275

    
276

    
277
    QFile file(QString("%1/comments/%2.txt").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber));
278
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
279
        return;
280

    
281
    QTextStream out(&file);
282
    out << "<span>Photo " + QString::number(m_currentProjectPictureNumber) + "</span>\n";
283
    file.close();
284
    ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq] [lq] [meta]").arg(fic),3000);
285

    
286
    //================== on copie les metadata
287
    try {
288
        Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(fic.toStdString());
289
        Exiv2::Image::AutoPtr imageLQ = Exiv2::ImageFactory::open(QString("%1/lq/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber).toStdString());
290
        Exiv2::Image::AutoPtr imageMQ = Exiv2::ImageFactory::open(QString("%1/mq/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber).toStdString());
291
        Exiv2::Image::AutoPtr imageHQ = Exiv2::ImageFactory::open(QString("%1/hq/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber).toStdString());
292
        Exiv2::Image::AutoPtr imageThumb = Exiv2::ImageFactory::open(QString("%1/thumbs/img-%2.jpg").arg(m_currentProjectDir).arg(m_currentProjectPictureNumber).toStdString());
293
        if (image.get() != 0)
294
        {
295
            image->readMetadata();
296
            Exiv2::ExifData &exifData = image->exifData();
297
            if (!exifData.empty())
298
            {
299
                bool setArtist = false;
300
                bool setCopyright = false;
301
                bool setSoftware = false;
302
                Exiv2::ExifData::const_iterator end = exifData.end();
303
                //                qDebug() << "============================ DEBUT ===============================";
304
                for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i)
305
                {
306
                    //                    qDebug() << i->key().c_str() << " = " << i->value().toString().c_str();
307
                    if (i->key() == "Exif.Image.Artist") {
308
                        exifData["Exif.Image.Artist"]    = ui->leAuthor->text().trimmed();
309
                        setArtist = true;
310
                    }
311
                    if (i->key() == "Exif.Image.Copyright") {
312
                        exifData["Exif.Image.Copyright"]    = ui->leAuthor->text().trimmed();
313
                        setCopyright = true;
314
                    }
315
                    if (i->key() == "Exif.Image.Software") {
316
                        exifData["Exif.Image.Software"]  = "RyXeo WebPhotoAlbum - http://www.ryxeo.com/";
317
                        setSoftware = true;
318
                    }
319
                }
320
                //Si on n'a pas les meta il faut les ajouter
321

    
322
                //Mise à jour de l'auteur
323
                if(!setArtist) {
324
                    Exiv2::ExifKey k("Exif.Image.Artist");
325
                    Exiv2::Value::AutoPtr v = Exiv2::Value::create(Exiv2::asciiString);
326
                    v->read(ui->leAuthor->text().trimmed());
327
                    exifData.add(k,v.get());
328
                }
329

    
330
                if(!setCopyright) {
331
                    Exiv2::ExifKey k2("Exif.Image.Copyright");
332
                    Exiv2::Value::AutoPtr v2 = Exiv2::Value::create(Exiv2::asciiString);
333
                    v2->read(ui->leAuthor->text().trimmed());
334
                    exifData.add(k2,v2.get());
335
                }
336

    
337
                if(!setSoftware) {
338
                    Exiv2::ExifKey k3("Exif.Image.Software");
339
                    Exiv2::Value::AutoPtr v3 = Exiv2::Value::create(Exiv2::asciiString);
340
                    v3->read("RyXéo WebPhotoAlbum - http://www.ryxeo.com/");
341
                    exifData.add(k3,v3.get());
342
                }
343

    
344
                //                qDebug() << "============================ APRES ===============================";
345
                Exiv2::ExifData::const_iterator end2 = exifData.end();
346
                //                for (Exiv2::ExifData::const_iterator i2 = exifData.begin(); i2 != end2; ++i2)
347
                //                {
348
                //                    qDebug() << i2->key().c_str() << " = " << i2->value().toString().c_str();
349
                //                }
350
                //                qDebug() << "============================ FIN ===============================";
351

    
352
                imageLQ->setExifData(exifData);
353
                imageLQ->writeMetadata();
354

    
355
                imageMQ->setExifData(exifData);
356
                imageMQ->writeMetadata();
357

    
358
                imageHQ->setExifData(exifData);
359
                imageHQ->writeMetadata();
360

    
361
                imageThumb->setExifData(exifData);
362
                imageThumb->writeMetadata();
363
            }
364
        }
365
    }
366
    catch(...) {
367
        qDebug() << "Erreur EXIV2";
368
    }
369
    ui->statusBar->showMessage(trUtf8("Fichier en cours: %1 [hq] [mq] [lq] [meta] ... terminé").arg(fic),3000);
370
    m_currentProjectPictureNumber++;
371
    refreshPictureList();
372
}
373

    
374
void original::refreshPictureList()
375
{
376
    QTreeWidgetItem *item = ui->treeWidget->currentItem();
377
    ui->lwPictures->clear();
378
    m_currentProjectDir = item->data(1,0).toString();
379
    int currentProjectPictureNumber=0;
380

    
381
    QDir dir(item->data(1,0).toString()+"/thumbs/");
382
    dir.setFilter(QDir::Files);
383
    dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
384
    QFileInfoList list = dir.entryInfoList();
385
    for(int i = 0; i < list.count(); i++) {
386
        qDebug() << list.at(i).fileName();
387
        QListWidgetItem *newitem = new QListWidgetItem();
388
        QIcon icone(list.at(i).absoluteFilePath());//pour la mettre  à coté de l'item
389
        newitem->setIcon(icone); // ajout de la petite icone sur l'item
390
        newitem->setText(list.at(i).fileName());
391
        ui->lwPictures->insertItem(i,newitem);
392
        currentProjectPictureNumber = i+1;
393
    }
394

    
395
    if(currentProjectPictureNumber == 0) {
396
        qDebug() << "Pas d'image ... on affiche le message drag & drop";
397
        m_labelDeposez->resize(ui->lwPictures->size());
398
        m_labelDeposez->move(ui->lwPictures->pos());
399
        m_labelDeposez->show();
400
        qDebug() << m_labelDeposez->pos();
401
        qDebug() << m_labelDeposez->size();
402
    }
403
    else {
404
        m_labelDeposez->hide();
405
    }
406
    m_currentProjectPictureNumber = currentProjectPictureNumber+1;
407
}
408

    
409
void original::on_btnUpload_clicked()
410
{
411
    qDebug() << "on_btnUpload_clicked : " << ui->treeWidget->currentItem()->data(1,0).toString();
412
    m_tools->clear();
413

    
414
    m_tools->ftpConnect(ui->leFTPServeur->text(),
415
                        ui->leFTPLogin->text(),
416
                        ui->leFTPPass->text());
417

    
418
    m_tools->ftpMkdir(ui->leFTPDirectory->text());
419
    m_tools->ftpMkdir(ui->leFTPDirectory->text() + "/galleries");
420
    m_tools->ftpMkdir(ui->leFTPDirectory->text() + "/galleries/" + ui->treeWidget->currentItem()->text(0));
421

    
422
    //On uploade la gallerie
423
    //m_ftp->cd(ui->leFTPDirectory->text());
424

    
425
    QFileInfo fi(ui->treeWidget->currentItem()->data(1,0).toString());
426
    QDir dir(fi.absoluteFilePath());
427
    m_tools->parcoursRecursif(dir.absolutePath(),ui->treeWidget->currentItem()->text(0));
428
    //qDebug() << m_fileListToUpload;
429
    m_tools->uploadRecursifGallery(ui->leFTPDirectory->text());
430
    connect(m_tools, SIGNAL(signalUpload(int,int)), this, SLOT(updateProgressFichier(int,int)));
431
    connect(m_tools, SIGNAL(signalUploadData(int,int)), this, SLOT(updateProgressData(int,int)));
432
}
433

    
434
void original::on_action_Assistant_d_installation_triggered()
435
{
436
    Assistant *a = new Assistant(this);
437
    a->show();
438
}
439

    
440
void original::updateProgressFichier(int total, int current)
441
{
442
    ui->pbFTPFichier->setMaximum(total);
443
    ui->pbFTPFichier->setValue(current);
444
}
445

    
446
void original::updateProgressData(int total, int current)
447
{
448
    ui->pbFTPProgress->setMaximum(total);
449
    ui->pbFTPProgress->setValue(current);
450
}
Redmine Appliance - Powered by TurnKey Linux