ches
aboutsummaryrefslogblamecommitdiff
path: root/src/plugins/Ui/skinned/mainvisual.cpp
blob: bd67cde328f6763ee7b6dd0b7f1a9fbf465730e7 (plain) (tree)
1
2
3
                                                                            
                                                                            
                                                                            













                                                                            
                                                                            



                                                                             

                       
                        
                      
                 
                   




                       
                                             
 
                                  
 


                                                           

 
                                                                           
 
                              
                              
                                                                       

                                                                 
                      
                     
                      

                   



                         
                    

              
                     
                        
     
                         

 
                                               
 










                                        

 
                        
 

                       
                    
             

 

                          
                                   
     
                                 


                              
                 
     

 
                                           
 

                                       

 
                                         
 
                    

 
                                         
 
                           
                         
 
 
                                                 
 


                                       
     
                        
                   
                                                
                                             
                                             
                                          
                               
 
                                                    
                                                     
         




                                              
         
                        
     

 








                         
                      
                    
            

 
                                 
 
                                                
                                         



                                    
                            
                                             
     
                                               

                                                       





                                                   
     

 
                                
 
                                                                 
                                   

                                                                               
                                                   
                                                                                  
 
                                                                    
 
                                               
                                                                          
                                               
                                                                          
                                                                              
 
                                           
                                                                        
 

                                                                  

 
                             
 
                              

                                                                         

                                                               
                                       

                                                                   
                                                         
                                                 
     
                                

                                
 







                                                                     
                                                      
     
                                
                                     
     
                                  
                                                      
     
                                
                                     
     

                                                         
                                      



                                                             
                                   



                                                     
                                             
     
                                
                                    

     

                                                                     
                                               




                                                                   
                                                         
     
                                

                                        
 

                                                               
                                            




                                                                 
                                                      
     
                                
                                     
     

                                                                   
                                            
             

 
 
                               
 
                                                                 
                                   



                                                                        
 
                                                     





                                                                                   
                                                 




                                                                  
                                                           




                                                                  
                                                           




                                                                           
                                                           




                                                                       
                                                              




                                                                                              
 

















                                                                                             
                           

                                       

 

                           
                    
 
            
                              
                                                           
 
                                                                 
                                   

                                                                                
                                                                   
 

                                                                  

 

                     
 

                      
                               





                                 
                                 
 
                                      
               
                           

                    
                                    






                                                                           
 
                                     



                                                  
 

                        
                                                                 
                                      
 
                                 
     
                    
                                                                        
             
                                


                                
                                                                          
             
                                

                                
                
                          
                   
         
                                               
                               
                               
                              










                                                                  

         
                
 
 
                                 
 
                            
                
                                    
         
                                                           






                                                                                      


                                                                


                                                 




                                                                         











                                                                                      



                                                                                                 


                                                 




                                                                      
                                                                           
             

         
 

              
            
                              
                              



                   
                               
                                 
 
 

               
 
                             
 
                                               
                
 
                                

                    

                                                                   
     
                

 
                             
 
                              
     

                                            

                          
                                                           
                                                                      
     
                               
                                 
 
/***************************************************************************
 *   Copyright (C) 2007-2021 by Ilya Kotov                                 *
 *   forkotov02@ya.ru                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include <QTimer>
#include <QSettings>
#include <QPainter>
#include <QMenu>
#include <QActionGroup>
#include <qmmp/buffer.h>
#include <qmmp/qmmp.h>
#include <math.h>
#include <stdlib.h>
#include "skin.h"
#include "fft.h"
#include "inlines.h"
#include "mainvisual.h"

MainVisual *MainVisual::m_instance = nullptr;

MainVisual *MainVisual::instance()
{
    if (!m_instance)
        qFatal ("MainVisual: this object is not created!");
    return m_instance;
}

MainVisual::MainVisual (QWidget *parent) : Visual (parent), m_vis (nullptr)
{
    m_skin = Skin::instance();
    m_ratio = m_skin->ratio();
    connect(m_skin, SIGNAL(skinChanged()), this, SLOT(readSettings()));
    m_timer = new QTimer (this);
    connect(m_timer, SIGNAL (timeout()), this, SLOT (timeout()));
    m_instance = this;
    m_update = false;
    m_running = false;
    createMenu();
    readSettings();
}

MainVisual::~MainVisual()
{
    writeSettings();
    if (m_vis)
    {
        delete m_vis;
        m_vis = nullptr;
    }
    m_instance = nullptr;
}

void MainVisual::setVisual (VisualBase *newvis)
{
    m_timer->stop();
    if (m_vis)
        delete m_vis;
    m_vis = newvis;
    if (m_vis)
        m_timer->start();
    else
    {
        m_pixmap.fill (Qt::transparent);
        update();
    }
}

void MainVisual::clear()
{
    if (m_vis)
        m_vis->clear();
    m_pixmap = m_bg;
    update();
}

void MainVisual::timeout()
{
    if(m_vis && takeData(m_buffer))
    {
        m_vis->process(m_buffer);
        m_pixmap = m_bg;
        QPainter p(&m_pixmap);
        m_vis->draw (&p);
        update();
    }
}

void MainVisual::paintEvent (QPaintEvent *)
{
    QPainter painter (this);
    painter.drawPixmap (0,0, m_pixmap);
}

void MainVisual::hideEvent (QHideEvent *)
{
    m_timer->stop();
}

void MainVisual::showEvent (QShowEvent *)
{
    if (m_vis && m_running)
        m_timer->start();
}

void MainVisual::mousePressEvent (QMouseEvent *e)
{
    if (e->button() == Qt::RightButton)
        m_menu->exec(e->globalPos());
    else
    {
        m_pixmap = m_bg;
        if (!m_vis)
            setVisual(new mainvisual::Analyzer);
        else if (m_vis->name() == "Analyzer")
            setVisual(new mainvisual::Scope);
        else if (m_vis->name() == "Scope")
            setVisual(nullptr);

        QString str = m_vis ? m_vis->name() : "Off";
        for(QAction *act : m_visModeGroup->actions())
        {
            if (str == act->data().toString())
            {
                act->setChecked(true);
                break;
            }
        }
        writeSettings();
    }
}

void MainVisual::start()
{
    m_running = true;
    if(isVisible())
        m_timer->start();
}

void MainVisual::stop()
{
    m_running = false;
    m_timer->stop();
    clear();
}

void MainVisual::drawBackGround()
{
    m_bg = QPixmap (76 * m_ratio, 16 * m_ratio);
    if (m_transparentAction->isChecked())
    {
        m_bg.fill (Qt::transparent);
        return;
    }
    QPainter painter(&m_bg);
    for (int x = 0; x < 76 * m_ratio; x += 2)
    {
        painter.setPen(m_skin->getVisColor(0));
        painter.drawLine(x + 1, 0, x + 1, 16 *m_ratio);
        for (int y = 0; y < 16 *m_ratio; y += 2)
        {
            painter.setPen(m_skin->getVisColor(0));
            painter.drawPoint(x,y);
            painter.setPen(m_skin->getVisColor(1));
            painter.drawPoint(x,y + 1);
        }
    }
}

void MainVisual::writeSettings()
{
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Skinned");
    QAction *act = m_peaksFalloffGroup->checkedAction ();
    settings.setValue("vis_peaks_falloff", act ? act->data().toDouble() : 0.2);
    act = m_analyzerFalloffGroup->checkedAction ();
    settings.setValue("vis_analyzer_falloff", act ? act->data().toDouble() : 2.2);

    settings.setValue("vis_show_peaks", m_peaksAction->isChecked());

    act = m_analyzerModeGroup->checkedAction();
    settings.setValue("vis_analyzer_mode", act ? act->data().toInt() : 0);
    act = m_analyzerTypeGroup->checkedAction();
    settings.setValue("vis_analyzer_type", act ? act->data().toInt() : 1);
    settings.setValue("vis_transparent_bg", m_transparentAction->isChecked());

    act = m_visModeGroup->checkedAction ();
    settings.setValue("vis_type", act ? act->data().toString() : "Off");

    act = m_fpsGroup->checkedAction();
    settings.setValue("vis_rate", act ? act->data().toInt() : 25);
}

void MainVisual::createMenu()
{
    m_menu = new QMenu (this);
    connect(m_menu, SIGNAL(triggered (QAction *)),SLOT(writeSettings()));
    connect(m_menu, SIGNAL(triggered (QAction *)),SLOT(readSettings()));
    QMenu *visMode = m_menu->addMenu(tr("Visualization Mode"));
    m_visModeGroup = new QActionGroup(this);
    m_visModeGroup->setExclusive(true);
    m_visModeGroup->addAction(tr("Analyzer"))->setData("Analyzer");
    m_visModeGroup->addAction(tr("Scope"))->setData("Scope");
    m_visModeGroup->addAction(tr("Off"))->setData("Off");
    for(QAction *act : m_visModeGroup->actions())
    {
        act->setCheckable(true);
        visMode->addAction(act);
    }

    QMenu *analyzerMode = m_menu->addMenu(tr("Analyzer Mode"));
    m_analyzerModeGroup = new QActionGroup(this);
    m_analyzerTypeGroup = new QActionGroup(this);
    m_analyzerModeGroup->addAction(tr("Normal"))->setData(0);
    m_analyzerModeGroup->addAction(tr("Fire"))->setData(1);
    m_analyzerModeGroup->addAction(tr("Vertical Lines"))->setData(2);
    m_analyzerTypeGroup->addAction(tr("Lines"))->setData(0);
    m_analyzerTypeGroup->addAction(tr("Bars"))->setData(1);
    for(QAction *act : m_analyzerModeGroup->actions())
    {
        act->setCheckable(true);
        analyzerMode->addAction(act);
    }
    analyzerMode->addSeparator ();
    for(QAction *act : m_analyzerTypeGroup->actions())
    {
        act->setCheckable(true);
        analyzerMode->addAction(act);
    }
    analyzerMode->addSeparator ();
    m_peaksAction = analyzerMode->addAction(tr("Peaks"));
    m_peaksAction->setCheckable(true);


    QMenu *refreshRate = m_menu->addMenu(tr("Refresh Rate"));
    m_fpsGroup = new QActionGroup(this);
    m_fpsGroup->setExclusive(true);
    m_fpsGroup->addAction(tr("50 fps"))->setData(50);
    m_fpsGroup->addAction(tr("25 fps"))->setData(25);
    m_fpsGroup->addAction(tr("10 fps"))->setData(10);
    m_fpsGroup->addAction(tr("5 fps"))->setData(5);
    for(QAction *act : m_fpsGroup->actions())
    {