SilentEye 0.4.1

mediawidget.cpp

Go to the documentation of this file.
00001 //  This file is part of SilentEye.
00002 //
00003 //  SilentEye is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  SilentEye is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with SilentEye.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 #include "mediawidget.h"
00017 
00018 namespace SilentEye {
00019 
00020     MediaWidget::MediaWidget(QWidget* parent, QString filepath)
00021         : QWidget(parent)
00022     {
00023         setupUi(this);
00024         m_menu = NULL;
00025         m_hasImg = false;
00026         playerWidget->setVisible(false);
00027         setFilepath(filepath);
00028 
00029         connect(playButton, SIGNAL(pressed()), this, SLOT(playPressed()));
00030         connect(stopButton, SIGNAL(pressed()), this, SLOT(stopPressed()));
00031     }
00032 
00033     MediaWidget::~MediaWidget() {
00034         // NOTHING TO DO
00035     }
00036 
00037     void MediaWidget::setPixmap(const QPixmap pix, bool autosize)
00038     {
00039         imageLabel->setPixmap(pix);
00040         if (autosize)
00041         {
00042             m_hasImg = true;
00043             m_size = pix.size();
00044             updateImageSize();
00045         }
00046     }
00047 
00048     void MediaWidget::setFilepath(const QString path)
00049     {
00050         m_filepath = path;
00051         setToolTip(path);
00052     }
00053 
00054     void MediaWidget::setText(const QString text)
00055     {
00056         m_hasImg = false;
00057         imageLabel->setText(text);
00058     }
00059 
00060     void MediaWidget::setContextMenu(QMenu* menu)
00061     {
00062         m_menu = menu;
00063     }
00064 
00065     QString MediaWidget::filepath() const
00066     {
00067         return m_filepath;
00068     }
00069 
00070     void MediaWidget::showPlayer(bool value)
00071     {
00072         playerWidget->setVisible(value);
00073     }
00074 
00075     void MediaWidget::contextMenuEvent(QContextMenuEvent *event)
00076     {
00077         if(m_menu)
00078             m_menu->exec(event->globalPos());
00079     }
00080 
00081     void MediaWidget::updateImageSize()
00082     {
00083         if(!m_hasImg)
00084             return;
00085 
00086         int width=0, height=0;
00087         double ratio = m_size.width() / (double)m_size.height();
00088         double ratioW = this->width() / (double)this->height();
00089 
00090         if((ratio>1 && ratio>ratioW) || (ratio<1 && ratio>ratioW))
00091         {
00092             width = this->width();
00093             height = (int)(width / ratio);
00094         }
00095         else
00096         {
00097             height = this->height();
00098             width = (int)(height * ratio);
00099         }
00100 
00101         imageLabel->setMaximumWidth(width);
00102         imageLabel->setMaximumHeight(height);
00103         imageLabel->setMinimumWidth(width);
00104         imageLabel->setMinimumHeight(height);
00105 
00106         int margin = (this->width()-width)/2;
00107         verticalLayout->setContentsMargins(margin, 0, 0, 0);
00108     }
00109 
00110     void MediaWidget::resizeEvent(QResizeEvent* event)
00111     {
00112         Q_UNUSED(event);
00113         updateImageSize();
00114     }
00115 
00116     void MediaWidget::playPressed()
00117     {
00118         emit play();
00119     }
00120 
00121     void MediaWidget::stopPressed()
00122     {
00123         emit stop();
00124     }
00125 
00126 }