made better use of Ui::trayActivated() so it can not only display the
context menu, it will also show the app when double clicked. AudFile::getDuration() and AudFile::duration() can now calculate the length of the audio based on the rate at which QMediaPlayer calls AudFile::seek() and the amount of data it pulls at a time. it converts that time internally to a string, where it is then used to display on Ui::timeDisp. i've also modified the minimize to tray function as a feasure that can be turned on/off within Conf and added the needed check box to the config menu.
This commit is contained in:
		
							parent
							
								
									2fa5bc7bf3
								
							
						
					
					
						commit
						c3ec7e7085
					
				
							
								
								
									
										21
									
								
								gui/ui.cpp
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								gui/ui.cpp
									
									
									
									
									
								
							| 
						 | 
					@ -5,6 +5,7 @@ Ui::Ui(const QStringList &args, QWidget *parent) : QWidget(parent)
 | 
				
			||||||
    QWidget     *btnWid        = new QWidget(this);
 | 
					    QWidget     *btnWid        = new QWidget(this);
 | 
				
			||||||
    QHBoxLayout *btnLayout     = new QHBoxLayout(btnWid);
 | 
					    QHBoxLayout *btnLayout     = new QHBoxLayout(btnWid);
 | 
				
			||||||
    QVBoxLayout *mainLayout    = new QVBoxLayout(this);
 | 
					    QVBoxLayout *mainLayout    = new QVBoxLayout(this);
 | 
				
			||||||
 | 
					    QLabel      *timeDisp      = new QLabel("00:00:00", this);
 | 
				
			||||||
    Icon        *trayPausePlay = new Icon(Icon::PAUSE_PLAY, this);
 | 
					    Icon        *trayPausePlay = new Icon(Icon::PAUSE_PLAY, this);
 | 
				
			||||||
    Icon        *trayOpen      = new Icon(Icon::OPEN, this);
 | 
					    Icon        *trayOpen      = new Icon(Icon::OPEN, this);
 | 
				
			||||||
    Icon        *trayStop      = new Icon(Icon::STOP, this);
 | 
					    Icon        *trayStop      = new Icon(Icon::STOP, this);
 | 
				
			||||||
| 
						 | 
					@ -38,7 +39,7 @@ Ui::Ui(const QStringList &args, QWidget *parent) : QWidget(parent)
 | 
				
			||||||
    fileName->setText(tr("Ready"));
 | 
					    fileName->setText(tr("Ready"));
 | 
				
			||||||
    fileName->setFont(fnt);
 | 
					    fileName->setFont(fnt);
 | 
				
			||||||
    slider->setOrientation(Qt::Horizontal);
 | 
					    slider->setOrientation(Qt::Horizontal);
 | 
				
			||||||
    trayIcon->show();
 | 
					    trayIcon->setVisible(conf->trayIcon());
 | 
				
			||||||
    trayIcon->setContextMenu(trayMenu);
 | 
					    trayIcon->setContextMenu(trayMenu);
 | 
				
			||||||
    conf->populateOptionsMenu(menu, this);
 | 
					    conf->populateOptionsMenu(menu, this);
 | 
				
			||||||
    player->setVolume(conf->getVolume());
 | 
					    player->setVolume(conf->getVolume());
 | 
				
			||||||
| 
						 | 
					@ -56,6 +57,7 @@ Ui::Ui(const QStringList &args, QWidget *parent) : QWidget(parent)
 | 
				
			||||||
    btnLayout->addWidget(settings);
 | 
					    btnLayout->addWidget(settings);
 | 
				
			||||||
    mainLayout->addWidget(fileName);
 | 
					    mainLayout->addWidget(fileName);
 | 
				
			||||||
    mainLayout->addWidget(slider);
 | 
					    mainLayout->addWidget(slider);
 | 
				
			||||||
 | 
					    mainLayout->addWidget(timeDisp, 0, Qt::AlignCenter);
 | 
				
			||||||
    mainLayout->addWidget(btnWid, 0, Qt::AlignCenter);
 | 
					    mainLayout->addWidget(btnWid, 0, Qt::AlignCenter);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    connect(trayRestore, SIGNAL(restore()), this, SLOT(showNormal()));
 | 
					    connect(trayRestore, SIGNAL(restore()), this, SLOT(showNormal()));
 | 
				
			||||||
| 
						 | 
					@ -75,11 +77,13 @@ Ui::Ui(const QStringList &args, QWidget *parent) : QWidget(parent)
 | 
				
			||||||
    connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(error(QMediaPlayer::Error)));
 | 
					    connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(error(QMediaPlayer::Error)));
 | 
				
			||||||
    connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), pausePlay, SLOT(stateChanged(QMediaPlayer::State)));
 | 
					    connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), pausePlay, SLOT(stateChanged(QMediaPlayer::State)));
 | 
				
			||||||
    connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), trayPausePlay, SLOT(stateChanged(QMediaPlayer::State)));
 | 
					    connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), trayPausePlay, SLOT(stateChanged(QMediaPlayer::State)));
 | 
				
			||||||
 | 
					    connect(ioDev, SIGNAL(duration(QString)), timeDisp, SLOT(setText(QString)));
 | 
				
			||||||
    connect(ioDev, SIGNAL(posChanged(qint64)), this, SLOT(posChanged(qint64)));
 | 
					    connect(ioDev, SIGNAL(posChanged(qint64)), this, SLOT(posChanged(qint64)));
 | 
				
			||||||
    connect(ioDev, SIGNAL(endOfPlayback()), this, SLOT(nextFile()));
 | 
					    connect(ioDev, SIGNAL(endOfPlayback()), this, SLOT(nextFile()));
 | 
				
			||||||
    connect(slider, SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
 | 
					    connect(slider, SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
 | 
				
			||||||
    connect(slider, SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
 | 
					    connect(slider, SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
 | 
				
			||||||
    connect(conf, SIGNAL(volume(int)), player, SLOT(setVolume(int)));
 | 
					    connect(conf, SIGNAL(volume(int)), player, SLOT(setVolume(int)));
 | 
				
			||||||
 | 
					    connect(conf, SIGNAL(enableTrayIcon(bool)), trayIcon, SLOT(setVisible(bool)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (args.size() > 1)
 | 
					    if (args.size() > 1)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
| 
						 | 
					@ -224,14 +228,21 @@ void Ui::fileDir(char direction)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Ui::trayActivated(QSystemTrayIcon::ActivationReason reason)
 | 
					void Ui::trayActivated(QSystemTrayIcon::ActivationReason reason)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    Q_UNUSED(reason);
 | 
					    if (reason == QSystemTrayIcon::DoubleClick)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
    trayMenu->popup(trayIcon->geometry().center());
 | 
					        showNormal();
 | 
				
			||||||
 | 
					        raise();
 | 
				
			||||||
 | 
					        activateWindow();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if (reason == QSystemTrayIcon::Trigger)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        trayMenu->popup(trayIcon->geometry().center());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Ui::changeEvent(QEvent *event)
 | 
					void Ui::changeEvent(QEvent *event)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (event->type() == QEvent::WindowStateChange)
 | 
					    if ((event->type() == QEvent::WindowStateChange) && conf->trayIcon())
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (windowState() & Qt::WindowMinimized)
 | 
					        if (windowState() & Qt::WindowMinimized)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -96,9 +96,13 @@ qint64 AudFile::getOffset()
 | 
				
			||||||
    return offset;
 | 
					    return offset;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
qint64 AudFile::getDuration()
 | 
					QString AudFile::getDuration()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return ((size() - offset) / buffRate) * 1000;
 | 
					    QTime time(0, 0, 0, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    time = time.addMSecs((size() / buffRate) * 1000);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return time.toString("hh:mm:ss");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void AudFile::delayFinished()
 | 
					void AudFile::delayFinished()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,9 @@
 | 
				
			||||||
#include <QObject>
 | 
					#include <QObject>
 | 
				
			||||||
#include <QString>
 | 
					#include <QString>
 | 
				
			||||||
#include <QTimer>
 | 
					#include <QTimer>
 | 
				
			||||||
 | 
					#include <QTime>
 | 
				
			||||||
#include <QDebug>
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					#include <QStringList>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AudFile : public QFile
 | 
					class AudFile : public QFile
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -26,10 +28,10 @@ public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    AudFile(QObject *parent = 0);
 | 
					    AudFile(QObject *parent = 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    bool   openFile(const QString &path);
 | 
					    bool    openFile(const QString &path);
 | 
				
			||||||
    bool   seek(qint64 off);
 | 
					    bool    seek(qint64 off);
 | 
				
			||||||
    qint64 getOffset();
 | 
					    qint64  getOffset();
 | 
				
			||||||
    qint64 getDuration();
 | 
					    QString getDuration();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ~AudFile();
 | 
					    ~AudFile();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,7 +39,7 @@ signals:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void posChanged(qint64 off);
 | 
					    void posChanged(qint64 off);
 | 
				
			||||||
    void endOfPlayback();
 | 
					    void endOfPlayback();
 | 
				
			||||||
    void duration(qint64 msec);
 | 
					    void duration(QString timeStr);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // AUD_FILE_H
 | 
					#endif // AUD_FILE_H
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										24
									
								
								io/conf.cpp
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								io/conf.cpp
									
									
									
									
									
								
							| 
						 | 
					@ -23,7 +23,6 @@ QWidget *MenuItem::createWidget(QWidget *parent)
 | 
				
			||||||
        layout->addWidget(new QLabel(str, parent));
 | 
					        layout->addWidget(new QLabel(str, parent));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return widget;
 | 
					        return widget;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,12 +45,15 @@ Conf::Conf(QObject *parent) : QObject(parent)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QByteArray pathBa = idm.value(LAST_PATH);
 | 
					    QByteArray pathBa = idm.value(LAST_PATH);
 | 
				
			||||||
    QByteArray nextBa = idm.value(NEXT_FILE);
 | 
					    QByteArray nextBa = idm.value(NEXT_FILE);
 | 
				
			||||||
 | 
					    QByteArray trayBa = idm.value(TRAY_ICON);
 | 
				
			||||||
    QByteArray volBa  = idm.value(VOLUME);
 | 
					    QByteArray volBa  = idm.value(VOLUME);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!pathBa.isEmpty()) lastPath      = pathBa;
 | 
					    if (!pathBa.isEmpty()) lastPath      = pathBa;
 | 
				
			||||||
    else                   lastPath      = QDir::homePath();
 | 
					    else                   lastPath      = QDir::homePath();
 | 
				
			||||||
    if (!nextBa.isEmpty()) nextFileState = (nextBa == "Y");
 | 
					    if (!nextBa.isEmpty()) nextFileState = (nextBa == "Y");
 | 
				
			||||||
    else                   nextFileState = true;
 | 
					    else                   nextFileState = true;
 | 
				
			||||||
 | 
					    if (!trayBa.isEmpty()) showTrayIcon  = (trayBa == "Y");
 | 
				
			||||||
 | 
					    else                   showTrayIcon  = false;
 | 
				
			||||||
    if (!volBa.isEmpty())  volumeValue   = rdInt(volBa);
 | 
					    if (!volBa.isEmpty())  volumeValue   = rdInt(volBa);
 | 
				
			||||||
    else                   volumeValue   = 50;
 | 
					    else                   volumeValue   = 50;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -67,6 +69,9 @@ void Conf::sync()
 | 
				
			||||||
    if (nextFileState) idm.insert(NEXT_FILE, "Y");
 | 
					    if (nextFileState) idm.insert(NEXT_FILE, "Y");
 | 
				
			||||||
    else               idm.insert(NEXT_FILE, "N");
 | 
					    else               idm.insert(NEXT_FILE, "N");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (showTrayIcon) idm.insert(TRAY_ICON, "Y");
 | 
				
			||||||
 | 
					    else              idm.insert(TRAY_ICON, "N");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QFile file(confPath(), this);
 | 
					    QFile file(confPath(), this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    file.open(QFile::WriteOnly | QFile::Truncate);
 | 
					    file.open(QFile::WriteOnly | QFile::Truncate);
 | 
				
			||||||
| 
						 | 
					@ -79,6 +84,7 @@ void Conf::populateOptionsMenu(QMenu *menu, QWidget *parent)
 | 
				
			||||||
    QHBoxLayout *volLayout = new QHBoxLayout(volWidget);
 | 
					    QHBoxLayout *volLayout = new QHBoxLayout(volWidget);
 | 
				
			||||||
    QSlider     *volSlider = new QSlider(parent);
 | 
					    QSlider     *volSlider = new QSlider(parent);
 | 
				
			||||||
    QCheckBox   *nextBox   = new QCheckBox(parent);
 | 
					    QCheckBox   *nextBox   = new QCheckBox(parent);
 | 
				
			||||||
 | 
					    QCheckBox   *trayBox   = new QCheckBox(parent);
 | 
				
			||||||
    Icon        *volUp     = new Icon(Icon::VOL_UP, parent);
 | 
					    Icon        *volUp     = new Icon(Icon::VOL_UP, parent);
 | 
				
			||||||
    Icon        *volDown   = new Icon(Icon::VOL_DOWN, parent);
 | 
					    Icon        *volDown   = new Icon(Icon::VOL_DOWN, parent);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -93,13 +99,17 @@ void Conf::populateOptionsMenu(QMenu *menu, QWidget *parent)
 | 
				
			||||||
    volLayout->addWidget(volUp);
 | 
					    volLayout->addWidget(volUp);
 | 
				
			||||||
    nextBox->setText(tr("Auto play next file in directory"));
 | 
					    nextBox->setText(tr("Auto play next file in directory"));
 | 
				
			||||||
    nextBox->setChecked(nextFile());
 | 
					    nextBox->setChecked(nextFile());
 | 
				
			||||||
 | 
					    trayBox->setText(tr("Enable tray icon"));
 | 
				
			||||||
 | 
					    trayBox->setChecked(trayIcon());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    connect(volSlider, SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
 | 
					    connect(volSlider, SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
 | 
				
			||||||
    connect(volSlider, SIGNAL(valueChanged(int)), this, SIGNAL(volume(int)));
 | 
					    connect(volSlider, SIGNAL(valueChanged(int)), this, SIGNAL(volume(int)));
 | 
				
			||||||
    connect(nextBox, SIGNAL(clicked(bool)), this, SLOT(setNextFile(bool)));
 | 
					    connect(nextBox, SIGNAL(clicked(bool)), this, SLOT(setNextFile(bool)));
 | 
				
			||||||
 | 
					    connect(trayBox, SIGNAL(clicked(bool)), this, SLOT(setTrayIcon(bool)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    menu->addAction(new MenuItem("", volWidget, this));
 | 
					    menu->addAction(new MenuItem("", volWidget, this));
 | 
				
			||||||
    menu->addAction(new MenuItem("", nextBox, this));
 | 
					    menu->addAction(new MenuItem("", nextBox, this));
 | 
				
			||||||
 | 
					    menu->addAction(new MenuItem("", trayBox, this));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Conf::setLastPath(const QString &path)
 | 
					void Conf::setLastPath(const QString &path)
 | 
				
			||||||
| 
						 | 
					@ -112,6 +122,13 @@ void Conf::setNextFile(bool state)
 | 
				
			||||||
    nextFileState = state; sync();
 | 
					    nextFileState = state; sync();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Conf::setTrayIcon(bool state)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    emit enableTrayIcon(state);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    showTrayIcon = state; sync();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Conf::setVolume(int value)
 | 
					void Conf::setVolume(int value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    emit volume(value);
 | 
					    emit volume(value);
 | 
				
			||||||
| 
						 | 
					@ -134,6 +151,11 @@ bool Conf::nextFile()
 | 
				
			||||||
    return nextFileState;
 | 
					    return nextFileState;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Conf::trayIcon()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return showTrayIcon;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int Conf::getVolume()
 | 
					int Conf::getVolume()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return volumeValue;
 | 
					    return volumeValue;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -42,10 +42,12 @@ private:
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LAST_PATH = 1,
 | 
					        LAST_PATH = 1,
 | 
				
			||||||
        NEXT_FILE,
 | 
					        NEXT_FILE,
 | 
				
			||||||
        VOLUME
 | 
					        VOLUME,
 | 
				
			||||||
 | 
					        TRAY_ICON
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QString lastPath;
 | 
					    QString lastPath;
 | 
				
			||||||
 | 
					    bool    showTrayIcon;
 | 
				
			||||||
    bool    nextFileState;
 | 
					    bool    nextFileState;
 | 
				
			||||||
    int     volumeValue;
 | 
					    int     volumeValue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -57,11 +59,13 @@ public slots:
 | 
				
			||||||
    void setLastPath(const QString &path);
 | 
					    void setLastPath(const QString &path);
 | 
				
			||||||
    void setNextFile(bool state);
 | 
					    void setNextFile(bool state);
 | 
				
			||||||
    void setVolume(int value);
 | 
					    void setVolume(int value);
 | 
				
			||||||
 | 
					    void setTrayIcon(bool state);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QString getLastPath();
 | 
					    QString getLastPath();
 | 
				
			||||||
    bool    nextFile();
 | 
					    bool    nextFile();
 | 
				
			||||||
 | 
					    bool    trayIcon();
 | 
				
			||||||
    void    populateOptionsMenu(QMenu *menu, QWidget *parent);
 | 
					    void    populateOptionsMenu(QMenu *menu, QWidget *parent);
 | 
				
			||||||
    int     getVolume();
 | 
					    int     getVolume();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,7 +73,8 @@ public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
signals:
 | 
					signals:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void volume(int);
 | 
					    void volume(int value);
 | 
				
			||||||
 | 
					    void enableTrayIcon(bool state);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // CONF_H
 | 
					#endif // CONF_H
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user