Fixed bug #3 and some other bugs
[qallinone.git] / mainwindow.cpp
bloba9277842dae4993e1312203d20c0be3a6837ab34
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 #include "mainwindow.h"
17 #include "ui_mainwindow.h"
18 #include "Defines.h"
20 MainWindow::MainWindow(QWidget *parent) :
21 QMainWindow(parent),
22 ui(new Ui::MainWindow)
25 //Setting Public Variables
26 ImageShiftCounter = -1;
27 ZoomCounter = 0; //Set Zoom to 0; since the image is not yet zoomed in
29 //Spaces and Margins
30 ToolBarSpace = 64;
31 ImageMargin = 100;
33 //UI setup and stuff
34 ui->setupUi(this);
35 setMouseTracking(true);
37 //Align MainWidget to center of MainWindow
38 ui->MainWidget->setGeometry(((this->width() / 2) - (ui->MainWidget->width() / 2)),(this->height() / 2) - (ui->MainWidget->height() / 2) - ToolBarSpace,ui->MainWidget->width(),ui->MainWidget->height() + ToolBarSpace);
40 ui->MainWidget->setStyleSheet("* { background-color: rgb(33, 33, 33); }");
42 Screen = new ImageScreen(ui->MainWidget);
43 Screen->setGeometry(0,0,ui->MainWidget->width(),ui->MainWidget->height());
45 Screen->setHidden(true);
47 VideoInfo = new QGraphicsVideoItem;
50 VideoScreen = new QVideoWidget(this);
52 VideoScreen->setStyleSheet("* { background-color: rgb(0, 0, 0); }");
53 VideoScreen->setHidden(true);
55 //Intialize Media Players so they can be used later
56 VideoPlayer = new QMediaPlayer(this);
57 AudioPlayer = new QMediaPlayer(this);
58 //End of Media Players intialization
60 //Intialize Toolbars but disable them until needed
61 Toolb = new ImageViewerToolBar(this);
62 Toolb->setHidden(true);
64 VToolb = new VideoToolBar(this);
65 VToolb->setHidden(true);
67 AToolb = new AudioToolBar(this);
68 AToolb->setHidden(true);
69 //End of toolbar settings
71 //Set Minimum size of MainWindow to the size of Toolbar and MenuBar
72 this->setMinimumSize(450,ToolBarSpace + ui->menuBar->height() + 10);
74 //End of UI and components setup
77 //Connecting SIGNALS and SLOTS
78 connect(VideoPlayer,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(VMediaStatus(QMediaPlayer::MediaStatus)));
79 connect(VideoPlayer,SIGNAL(metaDataAvailableChanged(bool)),this,SLOT(VmetaDataAvailable(bool)));
80 connect(VideoPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(VelapsedTimeChanged(qint64)));
82 connect(AudioPlayer,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(AMediaStatus(QMediaPlayer::MediaStatus)));
83 connect(AudioPlayer,SIGNAL(metaDataAvailableChanged(bool)),this,SLOT(AmetaDataAvailable(bool)));
84 connect(AudioPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(AelapsedTimeChanged(qint64)));
86 connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showAbout()));
87 connect(ui->actionReport_a_bug,SIGNAL(triggered()),this,SLOT(reportBug()));
88 connect(ui->actionHomepage,SIGNAL(triggered()),this,SLOT(visitHomepage()));
89 connect(ui->actionOpen_media,SIGNAL(triggered()),this,SLOT(openMedia()));
90 connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(close()));
91 //End of Connecting SIGNALS and SLOTS
94 MainWindow::~MainWindow()
96 delete ui;
99 //If MainWindow was resized, keep MainWidget in the center of MainWindow
100 void MainWindow::resizeEvent(QResizeEvent* event)
102 QMainWindow::resizeEvent(event);
104 //Don't change the Label size, but set the MainWidget to the Center
105 if(Screen->isHidden() == false)
107 ui->MainWidget->setGeometry(0,0,event->size().width(),event->size().height() - ToolBarSpace - ui->menuBar->height());
108 Screen->setGeometry((ui->MainWidget->width() / 2) - (Screen->width() / 2),(ui->MainWidget->height() / 2) - (Screen->height() / 2),Screen->width(),Screen->height());
109 if(Toolb->isHidden() == false)
111 Toolb->setGeometry(0,this->height() - ToolBarSpace,this->width(),ToolBarSpace);
112 Toolb->RepositionObjects();
115 else if(VideoScreen->isHidden() == false)
117 ui->MainWidget->setGeometry(0,0,event->size().width(),event->size().height() - ToolBarSpace - ui->menuBar->height());
118 VideoScreen->resize(QSize(event->size().width(),event->size().height() - ToolBarSpace));
119 VideoScreen->setGeometry(((event->size().width() / 2) - (VideoScreen->width() / 2)),((event->size().height() / 2) - (VideoScreen->height() / 2) - ui->menuBar->height()),VideoScreen->width(),VideoScreen->height());
120 if(VToolb->isHidden() == false)
122 VToolb->setGeometry(0,this->height() - ToolBarSpace,this->width(),ToolBarSpace);
123 VToolb->RepositionObjects();
126 if(!AToolb->isHidden())
128 ui->MainWidget->setGeometry(0,0,event->size().width(),event->size().height() - ToolBarSpace - ui->menuBar->height());
129 AToolb->setGeometry(0,this->height() - ToolBarSpace,this->width(),ToolBarSpace);
130 AToolb->RepositionObjects();
135 void MainWindow::wheelEvent(QWheelEvent* event)
137 if(!Toolb->isHidden())
139 if(event->delta() > 0 && ZoomCounter < 9)
141 //Create an image reference to the original image in order to get the original properties from the image
142 //Original Image is at index 0
143 QImage ORImage = ScaledImageList.at(0);
145 //Increase the zoom count by 1
146 ZoomCounter += 1;
148 //Declare a pixmap to the Image Viewer Pixmap
149 //Load the scaled image order from the ScaledImageList
150 const QPixmap Scaled1 = QPixmap::fromImage(ScaledImageList.at(ZoomCounter));
152 //Set the new scaled pixmap to the Image Viewer and resize it to the same size
153 Screen->setPixmap(Scaled1);
154 Screen->resize(Scaled1.width(),Scaled1.height());
156 //Keep the Image Viewer in center of MainWidget by substracting the added size
157 Screen->setGeometry(Screen->x() - (ORImage.width()/8)/2,Screen->y() - (ORImage.height()/8)/2,Screen->width(),Screen->height());
162 else if(event->delta() < 0 && ZoomCounter > 0)
164 //Create an image reference to the original image in order to get the original properties from the image
165 //Original Image is at index 0
166 QImage ORImage = ScaledImageList.at(0);
168 //Decrease the zoom count by 1
169 ZoomCounter -= 1;
171 //Declare a pixmap to the Image Viewer Pixmap
172 //Load the scaled image order from the ScaledImageList
173 const QPixmap Scaled1 = QPixmap::fromImage(ScaledImageList.at(ZoomCounter));
175 //Set the new scaled pixmap to the Image Viewer and resize it to the same size
176 Screen->setPixmap(Scaled1);
177 Screen->resize(Scaled1.width(),Scaled1.height());
179 //Keep the Image Viewer in center of MainWidget by adding the substracted size
180 Screen->setGeometry(Screen->x() + (ORImage.width()/8)/2,Screen->y() + (ORImage.height()/8)/2,Screen->width(),Screen->height());
185 //If Image is back to default size, recenter it in the middle of MainWindow
186 if(ZoomCounter == 0)
188 Screen->setGeometry((ui->MainWidget->width() / 2) - (Screen->width() / 2),(ui->MainWidget->height() / 2) - (Screen->height() / 2),Screen->width(),Screen->height());
195 int MainWindow::AIO_ProcessMedia(QString Filename, MMFiles Filetype)
198 //Disable all toolbars before loading media and stop any playing/showing media
199 if(VToolb->isHidden() == false)
201 VToolb->hide();
202 VideoPlayer->stop();
203 QVideoWidget* nl = NULL;
204 VideoPlayer->setVideoOutput(nl);
206 if(AToolb->isHidden() == false)
208 AToolb->hide();
209 AudioPlayer->stop();
211 if(Toolb->isHidden() == false)
213 Toolb->hide();
214 QImage NULLImage;
215 Screen->setPixmap(QPixmap::fromImage(NULLImage));
216 ScaledImageList.clear();
217 this->setMinimumSize(450,ToolBarSpace + ui->menuBar->height() + 10);
218 ZoomCounter = 0;
221 //Check Filetype and proccess it depending on its type
222 //As of v0.94 the media functions were moved to mainwindow.h and made more portable
223 if(Filetype == JPG)
225 processImage(JPG,Filename);
228 else if(Filetype == PNG)
230 processImage(PNG,Filename);
233 else if(Filetype == BMP)
235 processImage(BMP,Filename);
238 else if(Filetype == GIF)
240 processImage(GIF,Filename);
243 else if(Filetype == WMV)
245 processVideo(WMV,Filename);
248 else if(Filetype == MP4)
250 processVideo(MP4,Filename);
253 else if(Filetype == AVI)
255 processVideo(AVI,Filename);
258 else if(Filetype == MP3)
260 processAudio(MP3,Filename);
266 return 0;
269 //Convert number to time format MM:SS
270 QString MainWindow::IntToTime(int seconds)
272 int Minutes = seconds / 60;
273 int Seconds = seconds % 60;
274 QString SSeconds;
276 QString SMinutes = QString::number(Minutes);
278 if(QString::number(Seconds).length() < 2)
280 SSeconds = "0" + QString::number(Seconds);
282 else if(QString::number(Seconds).length() == 2)
284 SSeconds = QString::number(Seconds);
287 QString Time = SMinutes + ":" + SSeconds;
289 return Time;
292 //Show About dialog
293 void MainWindow::showAbout()
295 QDialog * AboutDial = new QDialog();
296 AboutDial->setMinimumSize(560,350);
297 AboutDial->setMaximumSize(560,350);
298 AboutDial->resize(560,350);
300 QLabel * Title = new QLabel(AboutDial);
301 Title->setGeometry(10,3,155,40);
302 Title->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
303 Title->setStyleSheet("font: 75 26pt \"Arial\";");
304 Title->setText("qAllInOne");
306 QLabel * Version = new QLabel(AboutDial);
307 Version->setGeometry(159,25,50,20);
308 Version->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
309 Version->setStyleSheet("font: 7pt \"Times New Roman\";");
310 Version->setText(CurrentVersion);
312 QTabWidget *AboutWidget = new QTabWidget(AboutDial);
313 AboutWidget->setGeometry(0,50,560,350);
314 AboutWidget->setMinimumSize(560,350);
315 AboutWidget->setMaximumSize(560,350);
317 //About widget
318 QWidget * About = new QWidget(AboutDial);
320 QLabel * AboutT = new QLabel(About);
321 AboutT->setGeometry(7,15,350,25);
322 AboutT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
323 AboutT->setStyleSheet("font: 75 18pt \"Arial\";");
324 AboutT->setText("About");
327 QLabel * AboutB = new QLabel(About);
328 AboutB->setGeometry(10,50,540,330);
329 AboutB->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
330 AboutB->setWordWrap(true);
331 AboutB->setStyleSheet("font: 13pt \"Arial\";");
332 AboutB->setText("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. Instead of having multiple separate software for each media type, qAllInOne aims on providing one software for all media types.\n\nCopyright 2013 - MJaoune\nhttp://qallinone.sourceforge.net/");
333 AboutWidget->insertTab(0,About, "About");
334 //End of About Widget
336 //Author widget
337 QWidget * Author = new QWidget(AboutDial);
339 QLabel * AuthorT = new QLabel(Author);
340 AuthorT->setGeometry(7,15,350,25);
341 AuthorT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
342 AuthorT->setStyleSheet("font: 75 18pt \"Arial\";");
343 AuthorT->setText("Author");
345 QLabel * AuthorL = new QLabel(Author);
346 AuthorL->setGeometry(7,50,250,25);
347 AuthorL->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
348 AuthorL->setStyleSheet("font: 75 13pt \"Arial\";");
349 AuthorL->setText("Author & current maintainer:");
352 QLabel * AuthorB = new QLabel(Author);
353 AuthorB->setGeometry(10,75,540,330);
354 AuthorB->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
355 AuthorB->setWordWrap(true);
356 AuthorB->setStyleSheet("font: 13pt \"Arial\";");
357 AuthorB->setText("-Mahmoud Jaoune <MJaoune>\nWebsite: http://mjaoune.users.sourceforge.net/\nE-Mail:mjaoune55@gmail.com");
358 AboutWidget->insertTab(1,Author, "Author");
359 //End of Author Widget
361 //License widget
362 QWidget * License = new QWidget(AboutDial);
364 QLabel * LicenseT = new QLabel(License);
365 LicenseT->setGeometry(7,15,350,25);
366 LicenseT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
367 LicenseT->setStyleSheet("font: 75 18pt \"Arial\";");
368 LicenseT->setText("License");
371 QPlainTextEdit * LicenseB = new QPlainTextEdit(License);
372 LicenseB->setGeometry(10,50,530,210);
373 LicenseB->setStyleSheet("font: 13pt \"Arial\";");
374 LicenseB->setReadOnly(true);
375 LicenseB->setPlainText(LicenseText);
376 AboutWidget->insertTab(2,License, "License");
377 //End of License Widget
379 //Contributors widget
380 QWidget * Contributor = new QWidget(AboutDial);
382 QLabel * ContributorT = new QLabel(Contributor);
383 ContributorT->setGeometry(7,15,350,25);
384 ContributorT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
385 ContributorT->setStyleSheet("font: 75 18pt \"Arial\";");
386 ContributorT->setText("Contributors");
389 QPlainTextEdit * ContributorB = new QPlainTextEdit(Contributor);
390 ContributorB->setGeometry(10,50,530,210);
391 ContributorB->setStyleSheet("font: 13pt \"Arial\";");
392 ContributorB->setReadOnly(true);
393 ContributorB->setPlainText("List of Contributors:\n\n");
394 AboutWidget->insertTab(3,Contributor, "Contributors");
395 //End of Contributors Widget
397 AboutDial->show();
400 //Open bug page
401 void MainWindow::reportBug()
403 QDesktopServices::openUrl(QUrl("https://sourceforge.net/p/qallinone/tickets/", QUrl::TolerantMode));
406 //Open homepage
407 void MainWindow::visitHomepage()
409 QDesktopServices::openUrl(QUrl("http://qallinone.sourceforge.net/", QUrl::TolerantMode));
412 void MainWindow::openMedia()
415 QString Filefilter = "Media Files (*.jpg *.jpeg *.png *.bmp *.gif *.wmv *.mp4 *.avi *.mp3)";
416 QString Fname = QFileDialog::getOpenFileName(this,"Open Media File...",QString(),Filefilter);
418 if(Fname.endsWith(".jpg",Qt::CaseInsensitive) || Fname.endsWith(".jpeg",Qt::CaseInsensitive))
420 AIO_ProcessMedia(Fname,JPG);
422 else if(Fname.endsWith(".png",Qt::CaseInsensitive))
424 AIO_ProcessMedia(Fname,PNG);
426 else if(Fname.endsWith(".bmp",Qt::CaseInsensitive))
428 AIO_ProcessMedia(Fname,BMP);
430 else if(Fname.endsWith(".gif",Qt::CaseInsensitive))
432 AIO_ProcessMedia(Fname,GIF);
434 else if(Fname.endsWith(".wmv",Qt::CaseInsensitive))
436 AIO_ProcessMedia(Fname,WMV);
438 else if(Fname.endsWith(".mp4",Qt::CaseInsensitive))
440 AIO_ProcessMedia(Fname,MP4);
442 else if(Fname.endsWith(".avi",Qt::CaseInsensitive))
444 AIO_ProcessMedia(Fname,AVI);
446 else if(Fname.endsWith(".mp3",Qt::CaseInsensitive))
448 AIO_ProcessMedia(Fname,MP3);