Fixed bug #3 and some other bugs
[qallinone.git] / mainwindow.h
blobc8c3c30bc52e36e3e5f83f2a6368739830cceef6
1 /*qAllInOne is a free open-source software built using Qt to provide users an All-In-One media player, so it can view images, play videos and audio.
2 qAllInOne Copyright (C) 2013 Mahmoud Jaoune
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 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
16 #ifndef MAINWINDOW_H
17 #define MAINWINDOW_H
19 #include <QtPlugin>
20 #include "ui_mainwindow.h"
21 #include <QMainWindow>
22 #include <QLabel>
23 #include <QString>
24 #include <QFile>
25 #include <QMovie>
26 #include <QImage>
27 #include <QMediaPlayer>
28 #include <QMediaPlaylist>
29 #include <QVideoWidget>
30 #include <QFileInfo>
31 #include <QGraphicsVideoItem>
32 #include <QGraphicsView>
33 #include <QMediaObject>
34 #include <QMessageBox>
35 #include <QDesktopWidget>
36 #include <QMediaResource>
37 #include <QMediaMetaData>
38 #include <QResizeEvent>
39 #include <QAbstractVideoSurface>
40 #include "MediaToolbars.h"
41 #include <QDir>
42 #include <QMouseEvent>
43 #include <QScrollArea>
44 #include <QTabWidget>
45 #include "ImageViewerContents.h"
46 #include <cmath>
47 #include <QList>
49 namespace Ui {
50 class MainWindow;
53 class MainWindow : public QMainWindow
55 Q_OBJECT
57 public:
58 enum MMFiles
60 JPG,PNG,GIF,WMV,MP3,MP4,BMP,AVI
63 //Variables
64 public:
65 ImageScreen * Screen;
66 QGraphicsVideoItem * VideoInfo;
67 QVideoWidget * VideoScreen;
68 QMediaPlayer * VideoPlayer;
69 QMediaPlayer * AudioPlayer;
70 int ToolBarSpace;
71 int ImageMargin;
72 QString CurrentImage;
73 QString CurrentAudio;
74 QString CurrentVideo;
75 ImageViewerToolBar *Toolb;
76 VideoToolBar * VToolb;
77 AudioToolBar * AToolb;
79 MiniImage * ScaleImage;
81 QString FileString;
82 QStringList Dirlist;
83 QString DirStack;
84 QStringList ScanDirectory;
85 QDir ScanDirs;
87 int ImageShiftCounter;
88 int ZoomCounter;
90 QList<QImage> ScaledImageList;
93 //Functions
94 public:
95 explicit MainWindow(QWidget *parent = 0);
96 ~MainWindow();
99 int AIO_ProcessMedia(QString Filename, MMFiles Filetype);
101 QString IntToTime(int seconds);
104 public slots:
105 //Audio Slots and settings
107 void AMediaStatus(QMediaPlayer::MediaStatus status)
109 if(status == QMediaPlayer::LoadedMedia)
116 void AmetaDataAvailable(bool Available)
118 if(Available == true)
120 //Obtaining Video information
121 QVariant CoverImage = (AudioPlayer->metaData(QMediaMetaData::CoverArtImage));
122 QString Title = (AudioPlayer->metaData(QMediaMetaData::Title).toString());
123 qint64 DurationMS = (AudioPlayer->metaData(QMediaMetaData::Duration).toInt());
124 int Duration = DurationMS / 1000;
126 if(!Title.isEmpty())
128 this->setWindowTitle(Title);
130 else this->setWindowTitle("qAllInOne - (" + CurrentAudio + ")");
132 //Set Screen size and calibrate Window
134 int OWidth = 471;
135 int OHeight = 353;
137 if(!QPixmap::fromImage(CoverImage.value<QImage>()).isNull())
139 OWidth = QPixmap::fromImage(CoverImage.value<QImage>()).width();
140 OHeight = QPixmap::fromImage(CoverImage.value<QImage>()).height();
142 else
144 OWidth = 471;
145 OHeight = 353;
148 //Calculate screen center and set MainWindow to center
149 QDesktopWidget *desktop = QApplication::desktop();
150 int CenterWidth = ((desktop->width() - OWidth) / 2);
151 int CenterHeight = ((desktop->height() - OHeight) / 2);
152 //Set MainWindow in Center
153 this->setGeometry(CenterWidth,CenterHeight,OWidth,OHeight + ToolBarSpace);
155 Screen->setPixmap(QPixmap::fromImage(CoverImage.value<QImage>()));
157 Screen->setGeometry(Screen->x(),Screen->y(),OWidth,OHeight);
159 Screen->show();
161 AToolb->TimeSlider->setMaximum(Duration);
163 AToolb->TotalTimeLabel->setText(IntToTime(Duration));
166 else
168 //In case the metadata was not found, use default properties
169 QVariant CoverImage = (AudioPlayer->metaData(QMediaMetaData::CoverArtImage));
170 QString Title = CurrentAudio;
171 qint64 DurationMS = 0;
172 int Duration = DurationMS / 1000;
174 if(!Title.isEmpty())
176 this->setWindowTitle(Title);
178 else this->setWindowTitle("qAllInOne - (" + CurrentAudio + ")");
180 //Set Screen size and calibrate Window
182 int OWidth = 471;
183 int OHeight = 353;
185 //Calculate screen center and set MainWindow to center
186 QDesktopWidget *desktop = QApplication::desktop();
187 int CenterWidth = ((desktop->width() - OWidth) / 2);
188 int CenterHeight = ((desktop->height() - OHeight) / 2);
189 //Set MainWindow in Center
190 this->setGeometry(CenterWidth,CenterHeight,OWidth,OHeight + ToolBarSpace);
192 Screen->setPixmap(QPixmap::fromImage(CoverImage.value<QImage>()));
194 Screen->setGeometry(Screen->x(),Screen->y(),OWidth,OHeight);
197 Screen->show();
199 AToolb->TimeSlider->setMaximum(Duration);
201 AToolb->TotalTimeLabel->setText(IntToTime(Duration));
205 void APlayVideo()
207 AudioPlayer->play();
209 void APauseVideo()
211 AudioPlayer->pause();
213 void AStopVideo()
215 AudioPlayer->stop();
216 //AudioPlayer->pause();
217 /*if(AudioPlayer->position() != AudioPlayer->duration())
219 AudioPlayer->pause();
220 AudioPlayer->setPosition(0);
221 AudioPlayer->pause();
225 void AelapsedTimeChanged(qint64 mseconds)
227 int Seconds = mseconds / 1000;
229 AToolb->ElapsedTimeLabel->setText(IntToTime(Seconds));
230 AToolb->TimeSlider->setValue(Seconds);
233 void AchangePosition(int mseconds)
235 if(AudioPlayer->state() == QMediaPlayer::PausedState)
237 AudioPlayer->setPosition(mseconds * 1000);
241 void AplayAfterChangePosition()
243 if(AudioPlayer->state() == QMediaPlayer::PausedState)
245 AudioPlayer->play();
249 void ApauseBeforeChangePosition()
251 if(AudioPlayer->state() != QMediaPlayer::PausedState)
253 AudioPlayer->pause();
257 void AchangeVolume(int vol)
259 AudioPlayer->setVolume(vol);
260 AToolb->VolumeLabel->setText(QString::number(vol) + "%");
262 //End of Audio Slots and Settings
264 //Video Slots and settings
265 void VMediaStatus(QMediaPlayer::MediaStatus status)
267 if(status == QMediaPlayer::LoadedMedia)
274 void VmetaDataAvailable(bool Available)
276 if(Available == true)
278 //Obtaining Video information
279 int OWidth = (VideoPlayer->metaData(QMediaMetaData::Resolution).toSize()).width();
280 int OHeight = (VideoPlayer->metaData(QMediaMetaData::Resolution).toSize()).height();
281 QString Title = (VideoPlayer->metaData(QMediaMetaData::Title).toString());
282 qint64 DurationMS = (VideoPlayer->metaData(QMediaMetaData::Duration).toInt());
283 int Duration = DurationMS / 1000;
285 if(!Title.isEmpty())
287 this->setWindowTitle(Title);
289 else this->setWindowTitle("qAllInOne - (" + CurrentVideo + ")");
291 //Calculate screen center and set MainWindow to center
292 QDesktopWidget *desktop = QApplication::desktop();
293 int CenterWidth = ((desktop->width() - OWidth) / 2);
294 int CenterHeight = ((desktop->height() - OHeight) / 2);
295 //Set MainWindow in Center
296 this->setGeometry(CenterWidth,CenterHeight,OWidth,OHeight + ToolBarSpace);
298 //Set VideoScreen size
299 VideoScreen->setGeometry(VideoScreen->x(),VideoScreen->y(),OWidth,OHeight);
301 VToolb->TimeSlider->setMaximum(Duration);
303 VToolb->TotalTimeLabel->setText(IntToTime(Duration));
306 else
308 //In case the metadata was not found, use default properties
309 int OWidth = 640;
310 int OHeight = 480;
311 QString Title = CurrentVideo;
312 qint64 DurationMS = 0;
313 int Duration = DurationMS / 1000;
315 if(!Title.isEmpty())
317 this->setWindowTitle(Title);
319 else this->setWindowTitle("qAllInOne - (" + CurrentVideo + ")");
321 //Calculate screen center and set MainWindow to center
322 QDesktopWidget *desktop = QApplication::desktop();
323 int CenterWidth = ((desktop->width() - OWidth) / 2);
324 int CenterHeight = ((desktop->height() - OHeight) / 2);
325 //Set MainWindow in Center
326 this->setGeometry(CenterWidth,CenterHeight,OWidth,OHeight + ToolBarSpace);
328 //Set VideoScreen size
329 VideoScreen->setGeometry(VideoScreen->x(),VideoScreen->y(),OWidth,OHeight);
331 VToolb->TimeSlider->setMaximum(Duration);
333 VToolb->TotalTimeLabel->setText(IntToTime(Duration));
337 void VPlayVideo()
339 VideoPlayer->play();
341 void VPauseVideo()
343 VideoPlayer->pause();
345 void VStopVideo()
347 VideoPlayer->stop();
348 //VideoPlayer->pause();
349 /*if(VideoPlayer->position() != VideoPlayer->duration())
351 VideoPlayer->pause();
352 VideoPlayer->setPosition(0);
353 VideoPlayer->pause();
357 void VelapsedTimeChanged(qint64 mseconds)
359 int Seconds = mseconds / 1000;
361 VToolb->ElapsedTimeLabel->setText(IntToTime(Seconds));
362 VToolb->TimeSlider->setValue(Seconds);
365 void VchangePosition(int mseconds)
367 if(VideoPlayer->state() == QMediaPlayer::PausedState)
369 VideoPlayer->setPosition(mseconds * 1000);
373 void VplayAfterChangePosition()
375 if(VideoPlayer->state() == QMediaPlayer::PausedState)
377 VideoPlayer->play();
381 void VpauseBeforeChangePosition()
383 if(VideoPlayer->state() != QMediaPlayer::PausedState)
385 VideoPlayer->pause();
389 void VchangeVolume(int vol)
391 VideoPlayer->setVolume(vol);
393 VToolb->VolumeLabel->setText(QString::number(vol) + "%");
395 //End of Video Slots and Settings
401 //Image viewer settings
402 //Code of PreviousImage() and NextImage() is influenced by GenCore software
403 //http://gen-core.sourceforge.net/
404 //The code actually gets the list of file in the same folder of the current image and then selects the one before it
405 //or after it depending on the button clicked.
406 void NextImage()
409 if(ImageShiftCounter == -1)
411 Dirlist.clear();
412 DirStack.clear();
413 ScanDirectory.clear();
415 QFileInfo FileInfo(CurrentImage);
417 FileString = FileInfo.absolutePath();
418 Dirlist.push_back(FileString);
419 DirStack.push_back(Dirlist.takeFirst());
420 ScanDirs.setPath(DirStack);
421 ScanDirectory = ScanDirs.entryList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks,QDir::Name);
423 for (int i = 0; i < ScanDirectory.count(); i++)
425 QString FileStack(FileString + "/" + ScanDirectory.at(i));
426 if(FileStack == CurrentImage)
428 ImageShiftCounter = i;
429 break;
435 while(!ScanDirectory.isEmpty())
437 QString FileName;
438 if(ImageShiftCounter < ScanDirectory.count() - 1)
440 ImageShiftCounter += 1;
441 FileName.push_back(ScanDirectory.at(ImageShiftCounter));
443 else if(ImageShiftCounter == ScanDirectory.count() - 1)
445 ImageShiftCounter = 0;
446 FileName.push_back(ScanDirectory.at(ImageShiftCounter));
452 QString FileStack(FileString + "/" + FileName);
454 if(FileStack != CurrentImage)
456 if(FileName.endsWith(".jpg",Qt::CaseInsensitive) || FileName.endsWith(".jpeg",Qt::CaseInsensitive))
458 AIO_ProcessMedia(FileStack, JPG);
459 break;
461 else if(FileName.endsWith(".png",Qt::CaseInsensitive))
463 AIO_ProcessMedia(FileStack, PNG);
464 break;
466 else if(FileName.endsWith(".gif",Qt::CaseInsensitive))
468 AIO_ProcessMedia(FileStack, GIF);
469 break;
471 else if(FileName.endsWith(".bmp",Qt::CaseInsensitive))
473 AIO_ProcessMedia(FileStack, BMP);
474 break;
477 else break;
482 void PreviousImage()
485 if(ImageShiftCounter == -1)
487 Dirlist.clear();
488 DirStack.clear();
489 ScanDirectory.clear();
491 QFileInfo FileInfo(CurrentImage);
493 FileString = FileInfo.absolutePath();
494 Dirlist.push_back(FileString);
495 DirStack.push_back(Dirlist.takeFirst());
496 ScanDirs.setPath(DirStack);
497 ScanDirectory = ScanDirs.entryList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks,QDir::Name);
499 for (int i = 0; i < ScanDirectory.count(); i++)
501 QString FileStack(FileString + "/" + ScanDirectory.at(i));
502 if(FileStack == CurrentImage)
504 ImageShiftCounter = i;
505 break;
510 while(!ScanDirectory.isEmpty())
512 QString FileName;
514 if(ImageShiftCounter > 0)
516 ImageShiftCounter -= 1;
517 FileName.push_back(ScanDirectory.at(ImageShiftCounter));
519 else if(ImageShiftCounter <= 0)
521 ImageShiftCounter = ScanDirectory.count() - 1;
522 FileName.push_back(ScanDirectory.at(ImageShiftCounter));
528 QString FileStack(FileString + "/" + FileName);
530 if(FileStack != CurrentImage)
532 if(FileName.endsWith(".jpg",Qt::CaseInsensitive) || FileName.endsWith(".jpeg",Qt::CaseInsensitive))
534 AIO_ProcessMedia(FileStack, JPG);
535 break;
537 else if(FileName.endsWith(".png",Qt::CaseInsensitive))
539 AIO_ProcessMedia(FileStack, PNG);
540 break;
542 else if(FileName.endsWith(".gif",Qt::CaseInsensitive))
544 AIO_ProcessMedia(FileStack, GIF);
545 break;
547 else if(FileName.endsWith(".bmp",Qt::CaseInsensitive))
549 AIO_ProcessMedia(FileStack, BMP);
550 break;
553 else break;
557 //End of Image Viewer settings
559 //Experimental code of the MiniImage and MiniFrame featured (Unimplemented)
561 void MovePicture(QPoint pos)
563 Screen->setGeometry(-1 * (ScaleImage->MiniFrame->x() * (Screen->width() / ScaleImage->width())),(-1 * (ScaleImage->MiniFrame->y() * (Screen->height() / ScaleImage->height()))),Screen->width(),Screen->height());
566 //Media functions
568 void processVideo(MMFiles format,QString Filename)
570 //Everything is done using Signals and Slots
571 //If Video Toolbar is not already setup, setup it
572 if(VToolb->isHidden())
574 VToolb->setGeometry(0,this->height() - ToolBarSpace,this->width(),ToolBarSpace);
575 VToolb->SetupToolbar();
577 VToolb->VolumeSlider->setMaximum(100);
578 VToolb->VolumeSlider->setValue(VideoPlayer->volume());
580 VToolb->show();
582 //End of Video Toolbar setup
584 CurrentVideo = Filename;
586 //Hide the Image Screen
587 Screen->setHidden(true);
589 //Load the media and setup the video screen
590 VideoPlayer->setMedia(QUrl::fromLocalFile(Filename));
592 VideoPlayer->setVideoOutput(VideoScreen);
594 VideoScreen->show();
596 VideoPlayer->play();
599 void processImage(MMFiles format,QString Filename)
601 if(format == GIF)
603 this->setWindowTitle("qAllInOne - Loading image...");
605 //Load the GIF as a QMovie
606 //Note Loading the file as QFile and then getting its name will return a Cross-platfrom filename
607 QFile ImageFile(Filename);
608 ImageFile.open(QFile::ReadOnly);
609 ImageFile.close();
611 QMovie * MvImage = new QMovie(ImageFile.fileName(),QByteArray(),ui->MainWidget);
612 //Set the GIF Image to the label
613 Screen->setMovie(MvImage);
614 CurrentImage = ImageFile.fileName();
616 //Start playing the Gif Image
617 MvImage->start();
618 //If the GIF Image is bigger than 1280x800 scale it to 1280x800
619 if(MvImage->currentImage().width() > 1280 || MvImage->currentImage().height() > 800)
621 QSize ScreenSize(1280,800);
622 MvImage->setScaledSize(ScreenSize);
625 //Process the scales of the image (For zooming)
627 QImage ORImage;
628 ORImage = MvImage->currentImage();
630 //Add the original image first to be at index 0
632 ScaledImageList.push_back(ORImage);
634 //Scale the image to increase the width and height with the width and height of the original image divided by 8
635 //Do this process 9 times and the scaled image to the ScaledImageList each time
637 for(int i=0;i < 9; i++)
639 ORImage = ORImage.scaled(ORImage.width() + MvImage->currentImage().width()/8,ORImage.height() + MvImage->currentImage().height()/8,Qt::KeepAspectRatio,Qt::SmoothTransformation);
640 ScaledImageList.push_back(ORImage);
643 //End of scale processing
645 //Center MainWindow
646 QDesktopWidget *desktop = QApplication::desktop();
647 int CenterWidth = ((desktop->width() - (MvImage->currentImage().width() + ImageMargin)) / 2);
648 int CenterHeight = ((desktop->height() - (MvImage->currentImage().height())) / 2);
650 //Set MainWindow in Center and add a 60x60 Margin
651 //If MainWindow is not maximized
652 if(!this->isMaximized())
654 //Lock MainWindow sizes
655 this->setMinimumSize(MvImage->currentImage().width() + ImageMargin,MvImage->currentImage().height() + ToolBarSpace + ui->menuBar->height());
656 //Set MainWindow to Center and set the size with a 60x60 margin
657 this->setGeometry(CenterWidth,CenterHeight - (ToolBarSpace / 2),MvImage->currentImage().width() + ImageMargin,MvImage->currentImage().height() + ToolBarSpace + ui->menuBar->height());
660 //If Image Toolbar is not already setup then setup it
661 if(Toolb->isHidden())
663 Toolb->setGeometry(0,this->height() - ToolBarSpace,this->width(),ToolBarSpace);
664 Toolb->SetupToolbar();
665 Toolb->show();
668 //Set the MainWidget to the center of MainWindow
669 ui->MainWidget->setGeometry(0,0,this->width(),this->height() - ToolBarSpace - ui->menuBar->height());
670 //Scale the Screen Label to the same size of the GIF image
671 Screen->setGeometry((ui->MainWidget->width() / 2) - (MvImage->currentImage().width() / 2),(ui->MainWidget->height() / 2) - (MvImage->currentImage().height() / 2),MvImage->currentImage().width(),MvImage->currentImage().height());
673 //Set MainWindow title and add file path
674 this->setWindowTitle("qAllInOne - (" + Filename + ")");
675 Screen->show();
676 VideoScreen->setHidden(true);
678 else
680 this->setWindowTitle("qAllInOne - Loading image...");
682 //Intialize QImage and load the file
683 QImage PrcImage;
684 //Note Loading the file as QFile and then getting its name will return a Cross-platfrom filename
685 QFile ImageFile(Filename);
686 ImageFile.open(QFile::ReadOnly);
687 ImageFile.close();
689 PrcImage.load(ImageFile.fileName(),0);
690 CurrentImage = ImageFile.fileName();
692 //If the image height is larger than 768 scale it to 768
693 if(PrcImage.height() > 768)
695 PrcImage = PrcImage.scaledToHeight(768,Qt::SmoothTransformation);
698 //Process the scales of the image (For zooming)
700 QImage ORImage;
701 ORImage = PrcImage;
703 //Add the original image first to be at index 0
705 ScaledImageList.push_back(ORImage);
707 //Scale the image to increase the width and height with the width and height of the original image divided by 8
708 //Do this process 9 times and the scaled image to the ScaledImageList each time
710 for(int i=0;i < 9; i++)
712 ORImage = ORImage.scaled(ORImage.width() + PrcImage.width()/8,ORImage.height() + PrcImage.height()/8,Qt::KeepAspectRatio,Qt::SmoothTransformation);
713 ScaledImageList.push_back(ORImage);
716 //End of scale processing
719 //Center MainWindow
720 QDesktopWidget *desktop = QApplication::desktop();
721 int CenterWidth = ((desktop->width() - (PrcImage.width() + ImageMargin)) / 2);
722 int CenterHeight = ((desktop->height() - (PrcImage.height())) / 2);
724 //Set MainWindow in Center and add a 60x60 Margin
725 //If MainWindow is not maximized
726 if(!this->isMaximized())
728 //Lock MainWindow sizes
729 this->setMinimumSize(PrcImage.width() + ImageMargin,PrcImage.height() + ToolBarSpace + ui->menuBar->height());
730 //Set MainWindow to Center and set the size with a 60x60 margin
731 this->setGeometry(CenterWidth,CenterHeight - (ToolBarSpace / 2),PrcImage.width() + ImageMargin,PrcImage.height() + ToolBarSpace + ui->menuBar->height());
734 //If Image Toolbar is not already setup then setup it
735 if(Toolb->isHidden())
737 Toolb->setGeometry(0,this->height() - ToolBarSpace,this->width(),ToolBarSpace);
738 Toolb->SetupToolbar();
739 Toolb->show();
742 //Put the MainWidget in the center and resize it to the Image's size
743 ui->MainWidget->setGeometry(0,0,this->width(),this->height() - ToolBarSpace - ui->menuBar->height());
745 //Resize the Scroll Screen and Screen Label to the Image size and load the image to it
746 Screen->setGeometry((ui->MainWidget->width() / 2) - (PrcImage.width() / 2),(ui->MainWidget->height() / 2) - (PrcImage.height() / 2),PrcImage.width(),PrcImage.height());
748 Screen->setPixmap(QPixmap::fromImage(PrcImage,Qt::AutoColor));
750 //Set MainWindow title and add file path
751 this->setWindowTitle("qAllInOne - (" + Filename + ")");
752 Screen->show();
753 VideoScreen->setHidden(true);
757 void processAudio(MMFiles format,QString Filename)
759 //Everything is done using Signals and Slots
760 //If Audio Toolbar is not already setup, setup it
761 if(AToolb->isHidden())
763 AToolb->setGeometry(0,this->height() - ToolBarSpace,this->width(),ToolBarSpace);
764 AToolb->SetupToolbar();
766 AToolb->VolumeSlider->setMaximum(100);
767 AToolb->VolumeSlider->setValue(AudioPlayer->volume());
769 AToolb->show();
771 //End of Video Toolbar setup
773 CurrentAudio = Filename;
775 //Hide the Image Screen
776 Screen->setHidden(true);
778 //Load the media
779 AudioPlayer->setMedia(QUrl::fromLocalFile(Filename));
781 AudioPlayer->play();
788 private:
789 Ui::MainWindow *ui;
790 void resizeEvent(QResizeEvent* event);
791 void wheelEvent(QWheelEvent *event);
793 private slots:
794 void showAbout();
795 void openMedia();
796 void reportBug();
797 void visitHomepage();
800 #endif // MAINWINDOW_H