This repository has been archived by the owner on Sep 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
PreviewGraphicView.cpp
411 lines (352 loc) · 12.8 KB
/
PreviewGraphicView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/***************************************************************************//**
* @brief Thumbnail me 3.0
* Thumbnail me is a user interface for Movie thumbnailer.
* Generate thumbnails from any movie is now easier !
*
* @file PreviewGraphicView.cpp
* @class PreviewGraphicView
* Cette classe permet la génération du Dock de prévisualisation.
*
* @author Quentin Rousseau\n
* @note Copyright (C) 2011-2012 Quentin Rousseau\n
* License: GNU General Public License version 2 (GPLv2) - http://www.gnu.org/licenses/gpl-2.0.html\n
* Site web: www.thumbnailme.com\n
* Email: [email protected]
*
* @since 3.0
* @version 3.0
* @date 2011-2012
*******************************************************************************/
#include "defines.h"
#include "PreviewGraphicView.h"
#include "DockConf.h"
#include "SuccessDialog.h"
#include "MainWindow.h"
#include "ThumbnailListwidget.h"
/**
*@brief Constructeur.
*@param *main_window Fenêtre principale de Thumbnail me.
*@param *zoomInAction QAction de la fonction zoomIn.
*@param *zoomOutAction QAction de la fonction zoomOut.
*@param *clearSceneAction QAction de la fonction clearScene.
*@param *printAction QAction de la fonction Impression.
*/
PreviewGraphicView::PreviewGraphicView(MainWindow * main_window, QAction *zoomInAction, QAction *zoomOutAction ,QAction *clearSceneAction, QAction *printAction) : QGraphicsView(main_window)
{
this->main_window = main_window;
this->zoomInAction = zoomInAction;
this->zoomOutAction = zoomOutAction;
this->clearSceneAction = clearSceneAction;
this->printAction = printAction;
this->animatedPixmap = true;
this->setAttribute(Qt::WA_TransparentForMouseEvents,true); //ignore mouse
this->setFocusPolicy( Qt::NoFocus );//ignore keyboard
settings = new QSettings(QSettings::IniFormat,DEFAULT_PATH_INI,APPLICATION_NAME,DEFAULT_FILE_INI,this);
scene = new QGraphicsScene(this);
previewPixmapItem = scene->addPixmap(QPixmap(":sprites/about.png"));
animationIntro(previewPixmapItem);
this->setScene(scene);
this->setInteractive(false);
this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QTimer::singleShot(3000, scene, SLOT(clear()));
if (settings->value("Extras/backgroundColorPreviewWindow").toString().isEmpty())
setBackgroundBrush(QBrush(Qt::lightGray));
else
setBackgroundBrush(QBrush(QColor("#"+settings->value("Extras/backgroundColorPreviewWindow").toString())));
//Construit le contextMenu-> contextuel
contextMenu = new QMenu(this);
openInViewerSystemAction = contextMenu->addAction(QIcon( ":/sprites/run.png" ),QString());
contextMenu->addSeparator ();
contextMenu->addAction(zoomInAction);
contextMenu->addAction(zoomOutAction);
contextMenu->addAction(clearSceneAction);
contextMenu->addSeparator ();
saveAsAction = contextMenu->addAction(QIcon( ":/sprites/save.png" ),QString());
saveAsAction->setShortcut(QKeySequence(QKeySequence::SaveAs));
copyAction = contextMenu->addAction(QIcon( ":/sprites/picture clipping.png" ),QString());
copyAction->setShortcut(QKeySequence(QKeySequence::Copy));
contextMenu->addAction(printAction);
this->retranslate();
connect(openInViewerSystemAction, SIGNAL(triggered()),this, SLOT(openInViewerSystem()));
connect(zoomInAction, SIGNAL(triggered()),this, SLOT(zoomIn()));
connect(zoomOutAction, SIGNAL(triggered()),this, SLOT(zoomOut()));
connect(clearSceneAction, SIGNAL(triggered()),this, SLOT(clear()));
connect(saveAsAction, SIGNAL(triggered()),this, SLOT(saveAs()));
connect(copyAction, SIGNAL(triggered()),this, SLOT(copyToClipboard()));
connect(printAction, SIGNAL(triggered()),this, SLOT(openPrintPreviewDialog()));
connect(scene, SIGNAL(changed(const QList<QRectF> &)),this,SLOT(manageQActionsState()));
}
/**
*@brief Destructeur.
*/
PreviewGraphicView::~PreviewGraphicView()
{
}
/**
*@brief Setter de la vignette à afficher.
*@param absolutePathFile Chemin absolu du fichier.
*/
void PreviewGraphicView::setPreview(QString absolutePathFile)
{
this->setAttribute(Qt::WA_TransparentForMouseEvents,false);
this->currentPixmapPathLoaded = absolutePathFile;
//Clear previous scene
scene->clear();
previewPixmapItem = scene->addPixmap(QPixmap(absolutePathFile));
//200 pour décalage verticale de l'image vers le haut OK doit être égale au i de l'animation.
int verticalOffset = 200;
if(!this->animatedPixmap)
verticalOffset=0;
scene->setSceneRect(0,verticalOffset,previewPixmapItem->pixmap().width(),previewPixmapItem->pixmap().height());
animationPixmap(previewPixmapItem);
this->setScene(scene);
this->setInteractive(true);
this->setDragMode(QGraphicsView::ScrollHandDrag);
this->setTransformationAnchor(AnchorUnderMouse);
this->setResizeAnchor(AnchorViewCenter);
this->fitInView(previewPixmapItem,Qt::KeepAspectRatio);
contextMenu->setEnabled(true);
if (absolutePathFile.contains(QDir::toNativeSeparators(QDir::tempPath()), Qt::CaseInsensitive))
openInViewerSystemAction->setDisabled(true);
else openInViewerSystemAction->setEnabled(true);
}
/**
*@brief Anime le pixmap d'introduction lors de son affichage.
*/
void PreviewGraphicView::animationIntro(QGraphicsPixmapItem *pixmap)
{
/*-------------Animation rebond-------------*/
QTimeLine *timeline = new QTimeLine(2000);
timeline->setEasingCurve(QEasingCurve::OutBounce);
QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(pixmap);
animation->setTimeLine(timeline);
for (int i = 0; i < 200; ++i)
animation->setTranslationAt(i / 200.0, 0 , i );
timeline->start();
/*------------------------------------------*/
}
/**
*@brief Anime le pixmap lors de son affichage.
*/
void PreviewGraphicView::animationPixmap(QGraphicsPixmapItem *pixmap)
{
if(this->animatedPixmap)
{
/*-------------Animation rebond-------------*/
QTimeLine *timeline = new QTimeLine(2000);
timeline->setEasingCurve(QEasingCurve::OutBounce);
QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(pixmap);
animation->setTimeLine(timeline);
for (int i = 0; i < 200; ++i)
animation->setTranslationAt(i / 200.0, 0 , i );
timeline->start();
}
/*------------------------------------------*/
}
/**
*@brief Réimplémentation de wheelEvent pour zoomer/dézoomer la vignette à l'aide de la molette souris.
*@param *e Evénement.
*/
void PreviewGraphicView::wheelEvent ( QWheelEvent * e )
{
if(!scene->items().isEmpty())
{
if (e->delta() > 0)
zoomIn();
else if (e->delta() < 0)
zoomOut();
}
QGraphicsView::wheelEvent(e);
}
/**
*@brief Retourne le Pixmap courant.
*@return QPixmap.
*/
QPixmap PreviewGraphicView::getCurrentPixmap()
{
//if(this->previewPixmapItem != 0)
return this->previewPixmapItem->pixmap();
//else return QPixmap;
}
/**
*@brief Zoom In.
*/
void PreviewGraphicView::zoomIn()
{
scale(1.2, 1.2);
}
/**
*@brief Zoom Out.
*/
void PreviewGraphicView::zoomOut()
{
scale(1 / 1.2, 1 / 1.2);
}
/**
*@brief Réimplémentation de mouseDoubleClickEvent pour ouvrir la vignette dans le visionneur d'image par défaut du système.
*@param *e Evénement.
*/
void PreviewGraphicView::mouseDoubleClickEvent ( QMouseEvent * e )
{
if (!scene->items().isEmpty() && e->button() == Qt::LeftButton && !currentPixmapPathLoaded.contains(QDir::toNativeSeparators(QDir::tempPath()),Qt::CaseInsensitive))
openInViewerSystem();
QGraphicsView::mouseDoubleClickEvent(e);
}
/**
*@brief Réimplémentation de keyPressEvent pour zoomer/dézoomer la vignette à l'aide du clavier.
*@param *e Evénement.
*/
void PreviewGraphicView::keyPressEvent ( QKeyEvent * e )
{
if(e->matches(QKeySequence::ZoomIn))
zoomIn();
else if (e->matches(QKeySequence::ZoomOut))
zoomOut();
QGraphicsView::keyPressEvent(e);
}
/**
*@brief Réimplémentation de contextMenuEvent pour dérouler le menu clic droit.
*@param *e Evénement.
*/
void PreviewGraphicView::contextMenuEvent( QContextMenuEvent * e )
{
if (!currentPixmapPathLoaded.isEmpty())
contextMenu->exec(e->globalPos());
QGraphicsView::contextMenuEvent(e);
}
/**
*@brief Ouvre la vignette courante dans le visionneur d'image par défaut du système.
*/
void PreviewGraphicView::openInViewerSystem()
{
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(currentPixmapPathLoaded)))
QMessageBox::critical(this,_ERROR_, APPLICATION_NAME +" "+ tr("failed to open this file in your default image viewer."));
}
/**
*@brief Copie la vignette courante dans presse-papier.
*/
void PreviewGraphicView::copyToClipboard()
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setPixmap(previewPixmapItem->pixmap(),QClipboard::Clipboard);
}
/**
*@brief Enregistre l'image sous...
*/
void PreviewGraphicView::saveAs()
{
QFileDialog saveAsDialog(this,tr("Save File"),QString(),"Windows bitmap (*.bmp);;Joint Photographic Experts Group (*.jpeg);;Portable Network Graphics (*.png);;Tagged Image File Format (*.tiff)");
saveAsDialog.setAcceptMode(QFileDialog::AcceptSave);
if (saveAsDialog.exec())
{
if (!saveAsDialog.selectedFiles().isEmpty())
{
QString format;
if(saveAsDialog.selectedNameFilter().contains(".bmp"))
format = "bmp";
else if(saveAsDialog.selectedNameFilter().contains(".jpeg"))
format = "jpeg";
else if(saveAsDialog.selectedNameFilter().contains(".png"))
format = "png";
else if(saveAsDialog.selectedNameFilter().contains(".tiff"))
format = "tiff";
saveAsDialog.setDefaultSuffix(format);
if(previewPixmapItem->pixmap().save(saveAsDialog.selectedFiles().first(),format.toLatin1().constData(),main_window->mpDockConf->getQuality()))
{
SuccessDialog successDialog(main_window,saveAsDialog.directory().absolutePath());
successDialog.getListWidget()->addItem(saveAsDialog.selectedFiles().first());
successDialog.exec();
}
else QMessageBox::warning(this,_WARNING_,tr("Saving the file failed"));
}
}
}
/**
*@brief Ouvre la fenêtre de prévisualisation du QPixmap courant.
*/
void PreviewGraphicView::openPrintPreviewDialog()
{
QPrinter printer(QPrinter::HighResolution);
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
if(!QPrinterInfo::availablePrinters().isEmpty())
{
QPrintPreviewDialog *preview = new QPrintPreviewDialog(&printer,this);
connect(preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
preview->exec();
}
else QMessageBox::critical(this, _ERROR_, tr("No Printer Detected"));
}
/**
*@brief Imprime la vignette courante.
*@param *printer - QPrinter Object.
*/
void PreviewGraphicView::print(QPrinter *printer)
{
QPainter painter(printer);
this->scene->render(&painter);
}
/**
*@brief Active ou désactive l'openGL sur la fenêtre de prévisualisation.
*@param value Vrai ou faux.
*/
void PreviewGraphicView::setViewportModeOpenGL(bool value)
{
if (value)
{
QGLWidget *openGL = new QGLWidget(QGLFormat(QGL::SampleBuffers));
this->setViewport(openGL);
}
else
{
this->setViewport(0);
}
}
/**
*@brief Efface la scène.
*/
void PreviewGraphicView::clear()
{
scene->clear();
contextMenu->setDisabled(true);
}
/**
*@brief Gère les QAction en fonction de la présence de vignette dans la fenêtre de prévisualisation.
*/
void PreviewGraphicView::manageQActionsState()
{
bool state = (scene->items().isEmpty()) ? false : true;
zoomInAction->setEnabled(state);
zoomOutAction->setEnabled(state);
clearSceneAction->setEnabled(state);
printAction->setEnabled(state);
}
/**
*@brief Setter qui permet d'animer le pixmap lors de l'affichage du rendu.
*@param value Vrai ou faux.
*/
void PreviewGraphicView::setAnimatedPixmap(bool value)
{
this->animatedPixmap = value;
}
/**
*@brief ChangeEvent.
*@param *event Evenement.
*/
void PreviewGraphicView::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange)
retranslate();
QGraphicsView::changeEvent(event);
}
/**
*@brief Fonction de traduction dynamique.
*/
void PreviewGraphicView::retranslate()
{
openInViewerSystemAction->setText( tr("Open to my default picture viewer") );
saveAsAction->setText( tr("Save As") );
copyAction->setText( tr("Copy to clipboard") );
}