Qmmp with Jyrki's patches
aboutsummaryrefslogblamecommitdiff
path: root/src/plugins/Ui/skinned/playlistheader.cpp
blob: a1449e0a7e1fdd6b8666a0a6b01e6e4cb021518b (plain) (tree)




















                                                                             
                  




                       
                            


                       
                
                
                      
                                   
                                       
                                   


                           


                          





















                                                      


                                                 
                           
                  
                  
                     
                       
                          
                       
                     
                                                         
                              
                             

                                                                                                  

                                                                                         
                                                                     
                           
                                                                                                        
 
                                                                     
                                                                         
                                                                        

                                                                                 








                                 
                    




                                                                 
                                   
                                                                                            
 




                         
 
                                         
                                        
 



                                                                                      
                                    


                                                                           
                                                                                  

                                                 


                                                           
                                 
                                                               
                                     
                                                       
         

     
                        
 
                    

 
                                              
 
                                                                                              
                                      
 
                                                                                      
                                          
 
                                               
     
                               
                                     
                        
     

 
                                    
 
                                                      
 
               
                      
                                                
 
                             
     

                                                                                                                            

                                 

                                                                                                                    


            

                                                                                             
         


               
                                             
     
                                                  




                                                      
               
                                                                                     
            
                                                                    

                                 

                                                                                                            


            

                                                                                             
         
 
                   
     
             

 




                                          

                                        
                        

                                                        
                    

 


















                                                                 


                                 
             

 

                                
                                           










                                                                            



                  
                                



                                 


                            
                                        



                                   










                                            

          

                                                   

    
                                                        

 


                                  
               
 
                                                          
                    
                               



                                             

                                                       




                                                    


                                   

                                                                        


                                      

                                                                        
     
                    

 
                                                    
 

                                                    
                                     
     

                                                
         


                                     
                   
             
                                                                                                             
                 
                                                        



                                    
                                                                                                       

                                  


                
                                                                                                                 
                 
                                                        



                                    
                                                                                                       

                                  
             
         




                             
     
 
 
                                                     
 
                      
                                                                                        
 

                     
 
 

                                                   

                                                    



                      
 
                                                
     
               
                                                                                     
            
                                                                                     
                                                                                           

                                   





                               
                                                 
         


                                                         

                         

                   

                                                                           





                             
             

                                                                           



                             







                                                  
                                              

                                
     


                                          
 

               
                                                                                                              



                                            
            
         
                                                                                                                  



                                            
     
 
 
                                                 
 
                             
     
                        


               

                                                         
                                             
     
                                                  




                      
 

                                               
                                                                  
                        
               
     
 
                                                                                           
     
                        
               
     

 


                                                           
                                            

                             
                                                                                        
                                       


                                                    


        




                                                    
     
                                 

 


                                              


                               
                            
                                                        
 
                                
 
                                                      
 
           
     

                                                     
                          
         
                                                                      
         
 
                                 
         

                                                                                  

                                     
                                                



                                                                

         
                                                 
         

                                                   



                                                       
                                                                          



                                            
 

                                                                                  
 
                                                                          


                                     
                                                





                                                                           
         

                                                                 
                                                                                               


                                                               


                                                                                                    
         
     
        
     

                                                     

                          
                                                              
         
 
                                 
         
                                                                                 

                                     
                                                                         





                                                                
                                                 
         

                                                   



                                                       
                                                                          




                                            
                                                                                 
 
                                                                                


                                     
                                                                         








                                                                           
                                                                                               


                                                                          
                                                                                                    
         
     




                                                         

                                                              

                                      
                                        





                                                                   
 


                                          
                                             
     
                                                        



                     
 









                                                 









                                                   






                                                                 
                                             
     

                                                 





                                                                
/***************************************************************************
 *   Copyright (C) 2015 by Ilya Kotov                                      *
 *   forkotov02@hotmail.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 <QPainter>
#include <QBitmap>
#include <QFont>
#include <QFontMetrics>
#include <QSettings>
#include <QApplication>
#include <QMouseEvent>
#include <QContextMenuEvent>
#include <QMenu>
#include <QLineEdit>
#include <QInputDialog>
#include <QIcon>
#include <QHash>
#include <qmmp/qmmp.h>
#include <qmmpui/playlistmanager.h>
#include <qmmpui/playlistheadermodel.h>
#include <qmmpui/playlistmanager.h>
#include "skin.h"
#include "playlistheader.h"

#define INITAL_SIZE 150
#define INITAL_MIN_SIZE 30

static const char * const skinned_arrow_down_xpm[] = {
    "11 6 2 1",
    " 	c None",
    "x	c #000000",
    "           ",
    " xxxxxxxxx ",
    "  xxxxxxx  ",
    "   xxxxx   ",
    "    xxx    ",
    "     x     "};

static const char * const skinned_arrow_up_xpm[] = {
    "11 6 2 1",
    " 	c None",
    "x	c #000000",
    "     x     ",
    "    xxx    ",
    "   xxxxx   ",
    "  xxxxxxx  ",
    " xxxxxxxxx ",
    "           "};

PlayListHeader::PlayListHeader(QWidget *parent) :
    QWidget(parent)
{
    setMouseTracking(true);
    m_metrics = 0;
    m_padding = 0;
    m_pl_padding = 0;
    m_number_width = 0;
    m_sorting_column = -1;
    m_reverted = false;
    m_task = NO_TASK;
    m_model = PlayListManager::instance()->headerModel();
    m_skin = Skin::instance();
    m_menu = new QMenu(this);
    m_menu->addAction(QIcon::fromTheme("list-add"), tr("Add Column"), this, SLOT(addColumn()));
    m_menu->addAction(QIcon::fromTheme("configure"), tr("Edit Column"), this, SLOT(editColumn()));
    m_autoResize = m_menu->addAction(tr("Auto-resize"), this, SLOT(setAutoResize(bool)));
    m_autoResize->setCheckable(true);
    m_menu->addAction(tr("Restore Size"), this, SLOT(restoreSize()));
    m_menu->addSeparator();
    m_menu->addAction(QIcon::fromTheme("list-remove"), tr("Remove Column"), this, SLOT(removeColumn()));

    connect(m_skin, SIGNAL(skinChanged()), this, SLOT(updateSkin()));
    connect(m_model, SIGNAL(columnAdded(int)), SLOT(onColumnAdded(int)));
    connect(m_model, SIGNAL(columnRemoved(int)), SLOT(updateColumns()));
    connect(m_model, SIGNAL(columnMoved(int,int)), SLOT(onColumnMoved(int,int)));
    connect(m_model, SIGNAL(columnChanged(int)), SLOT(updateColumns()));
    loadColors();
    readSettings();
}

PlayListHeader::~PlayListHeader()
{
    if (m_metrics)
        delete m_metrics;
    m_metrics = 0;
    writeSettings();
}

void PlayListHeader::readSettings()
{
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Skinned");
    m_font.fromString(settings.value("pl_header_font", qApp->font().toString()).toString());

    if (m_metrics)
    {
        delete m_metrics;
        m_metrics = 0;
    }

    m_metrics = new QFontMetrics(m_font);
    m_padding = m_metrics->width("9")/2;

    QFont pl_font;
    pl_font.fromString(settings.value("pl_font", qApp->font().toString()).toString());
    m_pl_padding = QFontMetrics(pl_font).width("9")/2;

    if(!m_model->isSettingsLoaded())
    {
        m_model->restoreSettings(&settings);
        QList<QVariant> sizes = settings.value("pl_column_sizes").toList();
        int autoResizeColumn = settings.value("pl_autoresize_column", -1).toInt();
        for(int i = 0; i < m_model->count(); ++i)
        {
            m_model->setData(i, SIZE, INITAL_SIZE);
            m_model->setData(i, MIN_SIZE, INITAL_MIN_SIZE);

            if(i < sizes.count())
                m_model->setData(i, SIZE, sizes.at(i).toInt());
            if(i == autoResizeColumn)
                m_model->setData(i, AUTO_RESIZE, true);
        }
    }

    settings.endGroup();

    updateColumns();
}

void PlayListHeader::setNumberWidth(int width)
{
    m_model->setData(0, MIN_SIZE, INITAL_MIN_SIZE + (width ? (width + 2 * m_pl_padding) : 0));
    int s = qMax(size(0), minSize(0));

    for(int i = 1; i < m_model->count(); ++i) //restore mimimal size for other columns
        m_model->setData(i, MIN_SIZE, 30);

    if(width != m_number_width || s != size(0))
    {
        m_number_width = width;
        m_model->setData(0, SIZE, s);
        updateColumns();
    }
}

void PlayListHeader::updateColumns()
{
    bool rtl = (layoutDirection() == Qt::RightToLeft);

    int sx = 5;
    if(m_number_width)
        sx += m_number_width + 2 * m_pl_padding;

    if(m_model->count() == 1)
    {
        m_model->setData(0, RECT, rtl ? QRect(5, 0, width() - sx - 5, height()) : QRect(sx, 0, width() - sx - 5, height()));
        QRect rect = m_model->data(0, RECT).toRect();
        if(m_sorting_column == 0)
        {
            m_model->setData(0, NAME, m_metrics->elidedText(m_model->name(0), Qt::ElideRight,
                                                            rect.width() - 2 * m_padding - m_arrow_up.width() - 4));
        }
        else
        {
            m_model->setData(0, NAME, m_metrics->elidedText(m_model->name(0), Qt::ElideRight,
                                                            rect.width() - 2 * m_padding));
        }
        return;
    }

    for(int i = 0; i < m_model->count(); ++i)
    {
        int size = m_model->data(i, SIZE).toInt();

        //add number width to the first column
        if(i == 0 && m_number_width)
            size -= m_number_width + 2 * m_pl_padding;

        if(rtl)
            m_model->setData(i, RECT, QRect(width() - sx - size, 0, size, height()));
        else
            m_model->setData(i, RECT, QRect(sx, 0, size, height()));
        if(i == m_sorting_column)
        {
            m_model->setData(i, NAME, m_metrics->elidedText(m_model->name(i), Qt::ElideRight,
                                                            size - 2 * m_padding - m_arrow_up.width() - 4));
        }
        else
        {
            m_model->setData(i, NAME, m_metrics->elidedText(m_model->name(i), Qt::ElideRight,
                                                            size - 2 * m_padding));
        }

        sx += size;
    }
    update();
}

int PlayListHeader::requiredHeight() const
{
    return m_metrics->lineSpacing() + 1;
}

QList<int> PlayListHeader::sizes() const
{
    QList<int> sizeList;
    for(int i = 0; i < m_model->count(); ++i)
        sizeList.append(m_model->data(i, SIZE).toInt());
    return sizeList;
}

void PlayListHeader::showSortIndicator(int column, bool reverted)
{
    if(m_sorting_column == column && m_reverted == reverted)
        return;

    m_sorting_column = column;
    m_reverted = reverted;
    updateColumns();
}

void PlayListHeader::hideSortIndicator()
{
    if(m_sorting_column != -1)
    {
        m_sorting_column = -1;
        updateColumns();
    }
}

void PlayListHeader::updateSkin()
{
    loadColors();
    update();
}

void PlayListHeader::addColumn()
{
    int column = findColumn(m_pressed_pos);

    if(column < 0)
    {
        QRect firstRect = m_model->data(0, RECT).toRect();
        QRect lastRect = m_model->data(m_model->count() - 1, RECT).toRect();

        if(m_pressed_pos.x() > lastRect.right())
            column = m_model->count();
        else if(m_pressed_pos.x() < firstRect.x())
            column = 0;
    }

    if(column < 0)
        return;

    m_model->execInsert(column);
}

void PlayListHeader::editColumn()
{
    if(m_pressed_column < 0)
         return;

    m_model->execEdit(m_pressed_column);
}

void PlayListHeader::removeColumn()
{
    if(m_pressed_column < 0)
         return;

    m_model->remove(m_pressed_column);
}

void PlayListHeader::setAutoResize(bool yes)
{
   if(m_pressed_column < 0)
        return;

   if(yes)
   {
       for(int i = 0; i < m_model->count(); ++i)
           m_model->setData(i, AUTO_RESIZE, false);
   }

   m_model->setData(m_pressed_column, AUTO_RESIZE, yes);
}

void PlayListHeader::restoreSize()
{
    if(m_pressed_column < 0)
        return;

    m_model->setData(m_pressed_column, SIZE, INITAL_SIZE);
    updateColumns();
    emit resizeColumnRequest();
}

void PlayListHeader::onColumnAdded(int index)
{
    m_model->setData(index, SIZE, INITAL_SIZE);
    m_model->setData(index, MIN_SIZE, INITAL_MIN_SIZE);
    updateColumns();
}

void PlayListHeader::onColumnMoved(int from, int to)
{
    //correct geometry
    if(from == 0 && m_number_width)
    {
        setSize(from, size(from) + (m_number_width + 2 * m_pl_padding));
        setSize(to, size(to) - (m_number_width + 2 * m_pl_padding));
    }
    else if(to == 0 && m_number_width)
    {
        setSize(from, size(from) - (m_number_width + 2 * m_pl_padding));
        setSize(to, size(to) + (m_number_width + 2 * m_pl_padding));
    }
    updateColumns();
}

void PlayListHeader::mousePressEvent(QMouseEvent *e)
{
    bool rtl = layoutDirection() == Qt::RightToLeft;

    if(e->button() == Qt::LeftButton)
    {
        m_pressed_column = findColumn(e->pos());
        if(m_pressed_column >= 0)
        {
            m_pressed_pos = e->pos();
            m_mouse_pos = e->pos();

            if(rtl)
            {
                if(e->pos().x() < m_model->data(m_pressed_column, RECT).toRect().x() + m_metrics->width("9"))
                {
                    m_old_size = size(m_pressed_column);
                    m_task = RESIZE;
                }
                else
                {
                    m_press_offset = e->pos().x() - m_model->data(m_pressed_column, RECT).toRect().x();
                    m_task = SORT;
                }
            }
            else
            {
                if(e->pos().x() > m_model->data(m_pressed_column, RECT).toRect().right() - m_metrics->width("9"))
                {
                    m_old_size = size(m_pressed_column);
                    m_task = RESIZE;
                }
                else
                {
                    m_press_offset = e->pos().x() - m_model->data(m_pressed_column, RECT).toRect().x();
                    m_task = SORT;
                }
            }
        }
        else
        {
            m_task = NO_TASK;
            update();
        }
    }
}

void PlayListHeader::mouseReleaseEvent(QMouseEvent *)
{
    if(m_task == SORT)
        PlayListManager::instance()->selectedPlayList()->sortByColumn(m_pressed_column);

    m_task = NO_TASK;
    update();
}

void PlayListHeader::mouseMoveEvent(QMouseEvent *e)
{
    bool rtl = layoutDirection() == Qt::RightToLeft;

    if(m_task == SORT)
    {
        m_task = MOVE;
    }

    if(m_task == RESIZE && m_model->count() > 1)
    {
        if(rtl)
            setSize(m_pressed_column, m_old_size - e->pos().x() + m_pressed_pos.x());
        else
            setSize(m_pressed_column, m_old_size + e->pos().x() - m_pressed_pos.x());
        setSize(m_pressed_column, qMax(size(m_pressed_column), minSize(m_pressed_column)));
        updateColumns();
        emit resizeColumnRequest();
    }
    else if(m_task == MOVE)
    {
        m_mouse_pos = e->pos();

        int dest = -1;
        for(int i = 0; i < m_model->count(); ++i)
        {
            QRect rect = m_model->data(i, RECT).toRect();
            int x_delta = m_mouse_pos.x() - rect.x();
            if(x_delta < 0 || x_delta > rect.width())
                continue;

            if(rtl)
            {
                if((x_delta > rect.width()/2 && m_pressed_column > i) ||
                        (x_delta < rect.width()/2 && m_pressed_column < i))
                {
                    dest = i;
                    break;
                }
            }
            else
            {
                if((x_delta > rect.width()/2 && m_pressed_column < i) ||