aboutsummaryrefslogblamecommitdiff
path: root/src/plugins/FileDialogs/QmmpFileDialog/qmmpfiledialogimpl.h
blob: 6b3463fdc27b5a2ff3fb736debad4a5464bec20b (plain) (tree)
1
2
3
                                                                           
                                                                           
                                                                           













                                                                           
                                                                           

                                                                            




                              

                            
                              
                           
 


                                                                      
            
       

                                                                   
                          
 
                                                                                                    
                              

                                 
                
                                                                     


                                                           
                                                       
                                             
                                                               
                                    
                                         

                                            
 
        
                                                              
 
          






                                               
                              




                                           
 











                                                                                                                          
                                                                                          
















                                                                              
                                                                                              







                                                                                                            
  

 
                             
/**************************************************************************
*   Copyright (C) 2008-2012 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.         *
***************************************************************************/

#ifndef QMMPFILEDIALOGIMPL_H
#define QMMPFILEDIALOGIMPL_H

#include "ui_qmmpfiledialog.h"
#include <QDialog>
#include <QCompleter>
#include <QAbstractItemView>
#include <qmmpui/filedialog.h>
#include <QFileSystemModel>


class QmmpFileDialogImpl : public QDialog , private Ui::QmmpFileDialog
{
    Q_OBJECT
public:
    QmmpFileDialogImpl(QWidget *parent = 0, Qt::WindowFlags f = 0);

    ~QmmpFileDialogImpl();

    void setModeAndMask(const QString&,FileDialog::Mode m, const QStringList& mask = QStringList());
    void loadMountedVolumes();
    QStringList selectedFiles ();

protected slots:
    void on_mountPointsListWidget_itemClicked(QListWidgetItem *item);
    void on_lookInComboBox_activated(const QString&);
    void on_upToolButton_clicked();
    void on_fileListView_doubleClicked(const QModelIndex&);
    void on_treeView_doubleClicked(const QModelIndex&);
    void on_fileNameLineEdit_returnPressed();
    void on_fileNameLineEdit_textChanged (const QString &text);
    void on_addPushButton_clicked();
    void on_listToolButton_toggled(bool);
    void on_detailsToolButton_toggled(bool);
    void on_fileTypeComboBox_activated(int);

signals:
    void filesSelected(const QStringList&, bool play = false);

protected:
    virtual void hideEvent (QHideEvent *event);

private slots:
    void updateSelection ();

private:
    int m_mode;
    QFileSystemModel* m_model;
    void addToHistory(const QString &path);
    void addFiles(const QStringList &list);
    QStringList m_history;

};

class PathCompleter : public QCompleter
{
    Q_OBJECT
public:
    PathCompleter(QAbstractItemModel *model, QAbstractItemView *itemView, QObject *parent = 0) : QCompleter(model, parent)
    {
        m_itemView = itemView;
    };


    QString pathFromIndex(const QModelIndex &index) const
    {
        const QFileSystemModel *dirModel = static_cast<const QFileSystemModel *>(model());
        QString currentLocation = dirModel->filePath(m_itemView->rootIndex());
        QString path = dirModel->filePath(index);
        if (path.startsWith(currentLocation))
        {
            path = path.mid(currentLocation.length() + 1);
        }
        return path;
    }


    QStringList splitPath(const QString &path) const
    {
        if (path.isEmpty())
            return QStringList(completionPrefix());
        QStringList parts;
        if (!path.startsWith(QDir::separator()))
        {
            const QFileSystemModel *dirModel = static_cast<const QFileSystemModel *>(model());
            QString currentLocation = QDir::toNativeSeparators(dirModel->filePath(m_itemView->rootIndex()));
            parts = QCompleter::splitPath(currentLocation);
        }
        parts << QCompleter::splitPath(path);
        return parts;
    }
private:
    QAbstractItemView *m_itemView;
};


#endif //QMMPFILEDIALOGIMPL_H
m_pressed_button != BUTTON_UNKNOWN) { drawButtons(); update(); return; } bool selected = false; int index = findPlayList(e->pos()); if(index != -1) { selected = true; m_pl_manager->selectPlayList(index); } update(); QPoint pp = e->pos(); pp.rx() += m_offset; if(e->button() == Qt::RightButton) m_menu->exec(e->globalPos()); else if(e->button() == Qt::MidButton && selected) m_pl_manager->removePlayList(m_pl_manager->selectedPlayList()); else { m_moving = true; m_mouse_pos = e->pos(); m_press_offset = pp.x() - m_rects.at(m_pl_manager->selectedPlayListIndex()).x(); QWidget::mousePressEvent(e); } } void PlayListSelector::mouseReleaseEvent (QMouseEvent *e) { m_moving = false; int released_button = findButton(e->pos()); if(released_button == m_pressed_button) { switch(released_button) { case BUTTON_NEW_PL: m_pl_manager->createPlayList(); break; case BUTTON_LEFT: m_offset -= m_offset - firstVisible().x() + 9 + 2; m_offset = qMax(0, m_offset); break; case BUTTON_RIGHT: m_offset += lastVisible().right() - m_offset - width() + 42; m_offset = qMin(m_offset, m_offset_max); break; default: ; } } m_pressed_button = BUTTON_UNKNOWN; drawButtons(); update(); QWidget::mouseReleaseEvent(e); } void PlayListSelector::mouseDoubleClickEvent (QMouseEvent *e) { if(e->button() == Qt::LeftButton && !(m_scrollable && (e->x() > width() - 40))) renamePlaylist(); else QWidget::mouseDoubleClickEvent(e); } void PlayListSelector::mouseMoveEvent(QMouseEvent *e) { if(!m_moving) { QWidget::mouseMoveEvent(e); return; } m_mouse_pos = e->pos(); QPoint mp = e->pos(); mp.rx() += m_offset; int dest = -1; for(int i = 0; i < m_rects.count(); ++i) { int x_delta = mp.x() - m_rects.at(i).x(); if(x_delta < 0 || x_delta > m_rects.at(i).width()) continue; if((x_delta > m_rects.at(i).width()/2 && m_pl_manager->selectedPlayListIndex() < i) || (x_delta < m_rects.at(i).width()/2 && m_pl_manager->selectedPlayListIndex() > i)) { dest = i; break; } } if(dest == -1 || dest == m_pl_manager->selectedPlayListIndex()) { update(); QWidget::mouseMoveEvent(e); return; } m_pl_manager->move(m_pl_manager->selectedPlayListIndex(), dest); update(); } void PlayListSelector::resizeEvent (QResizeEvent *) { updateScrollers(); } void PlayListSelector::loadColors() { m_normal.setNamedColor(m_skin->getPLValue("normal")); m_current.setNamedColor(m_skin->getPLValue("current")); m_normal_bg.setNamedColor(m_skin->getPLValue("normalbg")); m_selected_bg.setNamedColor(m_skin->getPLValue("selectedbg")); } void PlayListSelector::drawButtons() { m_pixmap = QPixmap(40, height()); m_pixmap.fill(m_normal_bg); QPainter painter(&m_pixmap); painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(m_pressed_button == BUTTON_LEFT ? m_current : m_normal); painter.setBrush(QBrush(m_pressed_button == BUTTON_LEFT ? m_current : m_normal)); QPoint points[3] = { QPoint(m_pixmap.width() - 25, height()/2 - 5), QPoint(m_pixmap.width() - 35, height()/2-1), QPoint(m_pixmap.width() - 25, height()/2 + 3), }; painter.drawPolygon(points, 3); painter.setPen(m_pressed_button == BUTTON_RIGHT ? m_current : m_normal); painter.setBrush(QBrush(m_pressed_button == BUTTON_RIGHT ? m_current : m_normal)); QPoint points2[3] = { QPoint(m_pixmap.width() - 20, height()/2 - 5), QPoint(m_pixmap.width() - 10, height()/2-1), QPoint(m_pixmap.width() - 20, height()/2 + 3), }; painter.drawPolygon(points2, 3); } void PlayListSelector::updateScrollers() { int last_x = m_extra_rects.isEmpty() ? m_rects.last().right() : m_extra_rects.last().right(); m_scrollable = last_x > width(); if(m_scrollable) { m_offset_max = last_x - width() + 42; m_offset = qMin(m_offset, m_offset_max); } else { m_offset = 0; m_offset_max = 0; } } QRect PlayListSelector::firstVisible() { for(int i = 0; i < m_rects.size(); ++i) { if(m_rects.at(i).right() + m_metrics->width(m_pl_separator) >= 9 + m_offset) return m_rects.at(i); } return m_rects.at(0); } QRect PlayListSelector::lastVisible() { for(int i = m_extra_rects.size() - 1; i >= 0; --i) { if(m_extra_rects.at(i).x() - m_offset - m_metrics->width(" ") - 2 <= width() - 40) return m_extra_rects.at(i); } for(int i = m_rects.size() - 1; i >= 0; --i) { if(m_rects.at(i).x() - m_offset - m_metrics->width(m_pl_separator) - 2 <= width() - 40) return m_rects.at(i); } return m_extra_rects.isEmpty() ? m_rects.last() : m_extra_rects.last(); } int PlayListSelector::findPlayList(QPoint pos) { pos.rx() += m_offset; for(int i = 0; i < m_rects.count(); ++i) { if(m_rects.at(i).contains(pos)) return i; } return -1; } int PlayListSelector::findButton(QPoint pos) { if(m_scrollable) { if(pos.x() > width() - 20) return BUTTON_RIGHT; else if ((width() - 40 < pos.x()) && (pos.x() <= width() - 20)) return BUTTON_LEFT; } pos.rx() += m_offset; for(int i = 0; i < m_extra_rects.count(); ++i) { if(m_extra_rects.at(i).contains(pos)) return BUTTON_NEW_PL; } return BUTTON_UNKNOWN; }