aboutsummaryrefslogblamecommitdiff
path: root/src/ui/positionbar.cpp
blob: 423aaee7bfa829737963996f8342dc2b2c313732 (plain) (tree)
1
2
                                                                            
                                                                            





































                                                                             
              












                                                 
                                        

















                                                              
                 
     
                           

                            
                                    









                                                 
                
                                    



                          
                     

 
                                    






                               
                                    













                                         
                                                                            
                                   








                                                                       


              
                                     
 
                                                                    
 
/***************************************************************************
 *   Copyright (C) 2009 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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QMouseEvent>
#include <QPainter>
#include <math.h>

#include "skin.h"
#include "button.h"
#include "mainwindow.h"

#include "positionbar.h"


PositionBar::PositionBar(QWidget *parent)
        : PixmapWidget(parent)
{
    m_skin = Skin::getPointer();
    connect(m_skin, SIGNAL(skinChanged()), this, SLOT(updateSkin()));
    setPixmap(m_skin->getPosBar());
    mw = qobject_cast<MainWindow*>(window());
    m_moving = FALSE;
    m_min = 0;
    m_max = 0;
    m_old = m_value = 0;
    draw(FALSE);
}


PositionBar::~PositionBar()
{}

void PositionBar::mousePressEvent(QMouseEvent *e)
{

    m_moving = TRUE;
    press_pos = e->x();
    if (m_pos<e->x() && e->x()<m_pos+29)
    {
        press_pos = e->x()-m_pos;
    }
    else
    {
        m_value = convert(qMax(qMin(width()-30,e->x()-15),0));
        press_pos = 15;
        if (m_value!=m_old)
        {
            emit sliderMoved(m_value);

        }
    }
    draw();
}

void PositionBar::mouseMoveEvent (QMouseEvent *e)
{
    if (m_moving)
    {
        qint64 po = e->x();
        po = po - press_pos;

        if (0<=po && po<=width()-30)
        {
            m_value = convert(po);
            draw();
            emit sliderMoved(m_value);
        }
    }
}

void PositionBar::mouseReleaseEvent(QMouseEvent*)
{
    draw(FALSE);
    if (m_value!=m_old && m_max > 0)
    {
        m_old = m_value;
        mw->seek(m_value);
    }
    m_moving = FALSE;
}

void PositionBar::setValue(qint64 v)
{
    if (m_moving || m_max == 0)
        return;
    m_value = v;
    draw(FALSE);
}

void PositionBar::setMax(qint64 max)
{
    m_max = max;
    draw(FALSE);
}

void PositionBar::updateSkin()
{
    draw(FALSE);
    //setPixmap(m_skin->getPosBar());
    //setButtonPixmap(Skin::BT_POSBAR_N);
}

void PositionBar::draw(bool pressed)
{
    qint64 p=qint64(ceil(double(m_value-m_min)*(width()-30)/(m_max-m_min)));
    m_pixmap = m_skin->getPosBar();
    if (m_max > 0)
    {
        QPainter paint(&m_pixmap);
        if (pressed)
            paint.drawPixmap(p,0,m_skin->getButton(Skin::BT_POSBAR_P));
        else
            paint.drawPixmap(p,0,m_skin->getButton(Skin::BT_POSBAR_N));
    }
        setPixmap(m_pixmap);
    m_pos = p;
}

qint64 PositionBar::convert(qint64 p)
{
    return qint64(ceil(double(m_max-m_min)*(p)/(width()-30)+m_min));
}
class="hl opt">, track->value(Qmmp::ALBUMARTIST)); query.bindValue(":album", track->value(Qmmp::ALBUM)); query.bindValue(":comment", track->value(Qmmp::COMMENT)); query.bindValue(":genre", track->value(Qmmp::GENRE)); query.bindValue(":composer", track->value(Qmmp::COMPOSER)); query.bindValue(":year", track->value(Qmmp::YEAR)); query.bindValue(":track", track->value(Qmmp::TRACK)); query.bindValue(":discnumber", track->value(Qmmp::DISCNUMBER)); query.bindValue(":duration", track->duration()); query.bindValue(":audioinfo", serializeAudioInfo(track->properties())); query.bindValue(":url", track->path()); query.bindValue(":filepath", filePath); query.bindValue(":searchstring", QString("%1|||%2|||%3").arg(track->value(Qmmp::ARTIST)) .arg(track->value(Qmmp::ALBUM)).arg(track->value(Qmmp::TITLE)).toLower()); if(!query.exec()) qWarning("Library: exec error: %s", qPrintable(query.lastError().text())); } QByteArray Library::serializeAudioInfo(const QMap<Qmmp::TrackProperty, QString> &properties) { QJsonObject obj; QMap<Qmmp::TrackProperty, QString>::const_iterator it = properties.cbegin(); while(it != properties.cend()) { QString value = properties[it.key()]; switch(it.key()) { case Qmmp::BITRATE: obj.insert("bitrate", value.toInt()); break; case Qmmp::SAMPLERATE: obj.insert("samplerate", value.toInt()); break; case Qmmp::CHANNELS: obj.insert("channels", value.toInt()); break; case Qmmp::BITS_PER_SAMPLE: obj.insert("bitsPerSample", value.toInt()); break; case Qmmp::FORMAT_NAME: obj.insert("formatName", value); break; case Qmmp::DECODER: obj.insert("decoder", value); break; case Qmmp::FILE_SIZE: obj.insert("fileSize", value.toLongLong()); break; default: ; } ++it; } return QJsonDocument(obj).toJson(QJsonDocument::Compact); } bool Library::scanDirectories(const QStringList &paths) { m_stopped = false; { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME); db.setDatabaseName(Qmmp::configDir() + "/" + "library.sqlite"); db.open(); QSqlQuery query(db); query.exec("PRAGMA journal_mode = WAL"); query.exec("PRAGMA synchronous = NORMAL"); for(const QString &path : qAsConst(paths)) { addDirectory(path); if(m_stopped) { db.close(); QSqlDatabase::removeDatabase(CONNECTION_NAME); return false; } } removeMissingFiles(paths); db.close(); } QSqlDatabase::removeDatabase(CONNECTION_NAME); qDebug("Library: directory scan finished"); return true; } void Library::addDirectory(const QString &s) { QList<TrackInfo *> tracks; QHash<const TrackInfo *, QString> filePathHash; QStringList ignoredPaths; QDir dir(s); dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); dir.setSorting(QDir::Name); QFileInfoList l = dir.entryInfoList(m_filters); for(const QFileInfo &info : qAsConst(l)) { if(!checkFile(info)) { QStringList paths; const QList<TrackInfo *> pl = MetaDataManager::instance()->createPlayList(info.absoluteFilePath(), TrackInfo::AllParts, &paths); //save local file path for(const TrackInfo *t : qAsConst(pl)) filePathHash.insert(t, info.absoluteFilePath()); tracks << pl; ignoredPaths << paths; } if (m_stopped) { qDeleteAll(tracks); tracks.clear(); return; } } removeIgnoredTracks(&tracks, ignoredPaths); for(TrackInfo *info : qAsConst(tracks)) addTrack(info, filePathHash.value(info)); qDeleteAll(tracks); tracks.clear(); //filter directories dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); dir.setSorting(QDir::Name); l.clear(); l = dir.entryInfoList(); for (int i = 0; i < l.size(); ++i) { QFileInfo fileInfo = l.at(i); addDirectory(fileInfo.absoluteFilePath()); if (m_stopped) return; } } void Library::removeMissingFiles(const QStringList &paths) { QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME); if(!db.isOpen()) return; QSqlQuery query(db); if(!query.exec("SELECT FilePath FROM track_library")) { qWarning("Library: exec error: %s", qPrintable(query.lastError().text())); return; } QString previousPath; while (query.next()) { QString path = query.value(0).toString(); if(previousPath == path) continue; previousPath = path; if(!QFile::exists(path) || //remove missing or disabled file paths !std::any_of(paths.cbegin(), paths.cend(), [path](const QString &p){ return path.startsWith(p); } )) { qDebug("Library: removing '%s' from library", qPrintable(path)); QSqlQuery rmQuery(db); rmQuery.prepare("DELETE FROM track_library WHERE FilePath = :filepath"); rmQuery.bindValue(":filepath", path); if(!rmQuery.exec()) { qWarning("Library: exec error: %s", qPrintable(query.lastError().text())); return; } } } } bool Library::checkFile(const QFileInfo &info) { QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME); if(!db.isOpen()) return false; QSqlQuery query(db); query.prepare("SELECT Timestamp FROM track_library WHERE FilePath = :filepath"); query.bindValue(":filepath", info.absoluteFilePath()); if(!query.exec()) { qWarning("Library: exec error: %s", qPrintable(query.lastError().text())); return false; } if(!query.next()) return false; return info.lastModified() == query.value("Timestamp").toDateTime(); } void Library::removeIgnoredTracks(QList<TrackInfo *> *tracks, const QStringList &ignoredPaths) { if(ignoredPaths.isEmpty()) return; QList<TrackInfo *>::iterator it = tracks->begin(); while(it != tracks->end()) { if(ignoredPaths.contains((*it)->path())) { delete (*it); it = tracks->erase(it); } else { ++it; } } }