Initial Commit
[qallinone.git] / mainwindow.cpp
blobd378414cddafd1e464920493b2027c1d5a88a5e3
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();
204 if(AToolb->isHidden() == false)
206 AToolb->hide();
207 AudioPlayer->stop();
209 if(Toolb->isHidden() == false)
211 Toolb->hide();
212 QImage NULLImage;
213 Screen->setPixmap(QPixmap::fromImage(NULLImage));
214 ScaledImageList.clear();
215 this->setMinimumSize(450,ToolBarSpace + ui->menuBar->height() + 10);
216 ZoomCounter = 0;
219 //Check Filetype and proccess it depending on its type
220 //As of v0.94 the media functions were moved to mainwindow.h and made more portable
221 if(Filetype == JPG)
223 processImage(JPG,Filename);
226 else if(Filetype == PNG)
228 processImage(PNG,Filename);
231 else if(Filetype == BMP)
233 processImage(BMP,Filename);
236 else if(Filetype == GIF)
238 processImage(GIF,Filename);
241 else if(Filetype == WMV)
243 processVideo(WMV,Filename);
246 else if(Filetype == MP4)
248 processVideo(MP4,Filename);
251 else if(Filetype == AVI)
253 processVideo(AVI,Filename);
256 else if(Filetype == MP3)
258 processAudio(MP3,Filename);
264 return 0;
267 //Convert number to time format MM:SS
268 QString MainWindow::IntToTime(int seconds)
270 int Minutes = seconds / 60;
271 int Seconds = seconds % 60;
272 QString SSeconds;
274 QString SMinutes = QString::number(Minutes);
276 if(QString::number(Seconds).length() < 2)
278 SSeconds = "0" + QString::number(Seconds);
280 else if(QString::number(Seconds).length() == 2)
282 SSeconds = QString::number(Seconds);
285 QString Time = SMinutes + ":" + SSeconds;
287 return Time;
290 //Show About dialog
291 void MainWindow::showAbout()
293 QDialog * AboutDial = new QDialog();
294 AboutDial->setMinimumSize(560,350);
295 AboutDial->setMaximumSize(560,350);
296 AboutDial->resize(560,350);
298 QLabel * Title = new QLabel(AboutDial);
299 Title->setGeometry(10,3,155,40);
300 Title->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
301 Title->setStyleSheet("font: 75 26pt \"Arial\";");
302 Title->setText("qAllInOne");
304 QLabel * Version = new QLabel(AboutDial);
305 Version->setGeometry(159,25,50,20);
306 Version->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
307 Version->setStyleSheet("font: 7pt \"Times New Roman\";");
308 Version->setText(CurrentVersion);
310 QTabWidget *AboutWidget = new QTabWidget(AboutDial);
311 AboutWidget->setGeometry(0,50,560,350);
312 AboutWidget->setMinimumSize(560,350);
313 AboutWidget->setMaximumSize(560,350);
315 //About widget
316 QWidget * About = new QWidget(AboutDial);
318 QLabel * AboutT = new QLabel(About);
319 AboutT->setGeometry(7,15,350,25);
320 AboutT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
321 AboutT->setStyleSheet("font: 75 18pt \"Arial\";");
322 AboutT->setText("About");
325 QLabel * AboutB = new QLabel(About);
326 AboutB->setGeometry(10,50,540,330);
327 AboutB->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
328 AboutB->setWordWrap(true);
329 AboutB->setStyleSheet("font: 13pt \"Arial\";");
330 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/");
331 AboutWidget->insertTab(0,About, "About");
332 //End of About Widget
334 //Author widget
335 QWidget * Author = new QWidget(AboutDial);
337 QLabel * AuthorT = new QLabel(Author);
338 AuthorT->setGeometry(7,15,350,25);
339 AuthorT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
340 AuthorT->setStyleSheet("font: 75 18pt \"Arial\";");
341 AuthorT->setText("Author");
343 QLabel * AuthorL = new QLabel(Author);
344 AuthorL->setGeometry(7,50,250,25);
345 AuthorL->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
346 AuthorL->setStyleSheet("font: 75 13pt \"Arial\";");
347 AuthorL->setText("Author & current maintainer:");
350 QLabel * AuthorB = new QLabel(Author);
351 AuthorB->setGeometry(10,75,540,330);
352 AuthorB->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
353 AuthorB->setWordWrap(true);
354 AuthorB->setStyleSheet("font: 13pt \"Arial\";");
355 AuthorB->setText("-Mahmoud Jaoune <MJaoune>\nWebsite: http://mjaoune.users.sourceforge.net/\nE-Mail:mjaoune55@gmail.com");
356 AboutWidget->insertTab(1,Author, "Author");
357 //End of Author Widget
359 //License widget
360 QWidget * License = new QWidget(AboutDial);
362 QLabel * LicenseT = new QLabel(License);
363 LicenseT->setGeometry(7,15,350,25);
364 LicenseT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
365 LicenseT->setStyleSheet("font: 75 18pt \"Arial\";");
366 LicenseT->setText("License");
369 QPlainTextEdit * LicenseB = new QPlainTextEdit(License);
370 LicenseB->setGeometry(10,50,530,210);
371 LicenseB->setStyleSheet("font: 13pt \"Arial\";");
372 LicenseB->setReadOnly(true);
373 LicenseB->setPlainText(LicenseText);
374 AboutWidget->insertTab(2,License, "License");
375 //End of License Widget
377 //Contributors widget
378 QWidget * Contributor = new QWidget(AboutDial);
380 QLabel * ContributorT = new QLabel(Contributor);
381 ContributorT->setGeometry(7,15,350,25);
382 ContributorT->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
383 ContributorT->setStyleSheet("font: 75 18pt \"Arial\";");
384 ContributorT->setText("Contributors");
387 QPlainTextEdit * ContributorB = new QPlainTextEdit(Contributor);
388 ContributorB->setGeometry(10,50,530,210);
389 ContributorB->setStyleSheet("font: 13pt \"Arial\";");
390 ContributorB->setReadOnly(true);
391 ContributorB->setPlainText("List of Contributors:\n\n");
392 AboutWidget->insertTab(3,Contributor, "Contributors");
393 //End of Contributors Widget
395 AboutDial->show();
398 //Open bug page
399 void MainWindow::reportBug()
401 QDesktopServices::openUrl(QUrl("https://sourceforge.net/p/qallinone/tickets/", QUrl::TolerantMode));
404 //Open homepage
405 void MainWindow::visitHomepage()
407 QDesktopServices::openUrl(QUrl("http://qallinone.sourceforge.net/", QUrl::TolerantMode));
410 void MainWindow::openMedia()
413 QString Filefilter = "Media Files (*.jpg *.jpeg *.png *.bmp *.gif *.wmv *.mp4 *.avi *.mp3)";
414 QString Fname = QFileDialog::getOpenFileName(this,"Open Media File...",QString(),Filefilter);
416 if(Fname.endsWith(".jpg",Qt::CaseInsensitive) || Fname.endsWith(".jpeg",Qt::CaseInsensitive))
418 AIO_ProcessMedia(Fname,JPG);
420 else if(Fname.endsWith(".png",Qt::CaseInsensitive))
422 AIO_ProcessMedia(Fname,PNG);
424 else if(Fname.endsWith(".bmp",Qt::CaseInsensitive))
426 AIO_ProcessMedia(Fname,BMP);
428 else if(Fname.endsWith(".gif",Qt::CaseInsensitive))
430 AIO_ProcessMedia(Fname,GIF);
432 else if(Fname.endsWith(".wmv",Qt::CaseInsensitive))
434 AIO_ProcessMedia(Fname,WMV);
436 else if(Fname.endsWith(".mp4",Qt::CaseInsensitive))
438 AIO_ProcessMedia(Fname,MP4);
440 else if(Fname.endsWith(".avi",Qt::CaseInsensitive))
442 AIO_ProcessMedia(Fname,AVI);
444 else if(Fname.endsWith(".mp3",Qt::CaseInsensitive))
446 AIO_ProcessMedia(Fname,MP3);