+
Skip to content

Implemented tooltip for timeline #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions minitube.pro
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ include(src/yt/yt.pri)
INCLUDEPATH += $$PWD/src

HEADERS += src/video.h \
src/helper.h \
src/messagebar.h \
src/spacer.h \
src/constants.h \
src/playlistitemdelegate.h \
src/timeslider.h \
src/updateutils.h \
src/videoapi.h \
src/videomimedata.h \
Expand Down Expand Up @@ -126,8 +128,10 @@ HEADERS += src/video.h \
src/videoarea.h \
src/searchlineedit.h
SOURCES += src/main.cpp \
src/helper.cpp \
src/messagebar.cpp \
src/spacer.cpp \
src/timeslider.cpp \
src/updateutils.cpp \
src/video.cpp \
src/videomimedata.cpp \
Expand Down
13 changes: 13 additions & 0 deletions src/helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "helper.h"

QString Helper::formatTime(qint64 duration) {
duration /= 1000;
QString res;
int seconds = (int)(duration % 60);
duration /= 60;
int minutes = (int)(duration % 60);
duration /= 60;
int hours = (int)(duration % 24);
if (hours == 0) return res.sprintf("%02d:%02d", minutes, seconds);
return res.sprintf("%02d:%02d:%02d", hours, minutes, seconds);
}
12 changes: 12 additions & 0 deletions src/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef HELPER_H
#define HELPER_H

#include <QString>

class Helper
{
public:
static QString formatTime(qint64 duration);
};

#endif // HELPER_H
29 changes: 11 additions & 18 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ along with Minitube. If not, see <http://www.gnu.org/licenses/>.
#include "httputils.h"
#include "jsfunctions.h"
#include "seekslider.h"
#include "timeslider.h"
#include "helper.h"
#include "sidebarwidget.h"
#include "toolbarmenu.h"
#include "videoarea.h"
Expand Down Expand Up @@ -310,8 +312,10 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *e) {
}

if (t == QEvent::ToolTip) {
// kill tooltips
return true;
// kill tooltips but not for time slider
if (!obj->isWidgetType() || qobject_cast<QWidget *>(obj) != seekSlider) {
return true;
}
}

if (t == QEvent::Show && obj == toolbarMenu) {
Expand Down Expand Up @@ -804,10 +808,9 @@ void MainWindow::createToolBar() {
// Create widgets
currentTimeLabel = new QLabel("00:00", this);

seekSlider = new SeekSlider(this);
seekSlider = new TimeSlider(this);
seekSlider->setEnabled(false);
seekSlider->setTracking(false);
seekSlider->setMaximum(1000);
volumeSlider = new SeekSlider(this);
volumeSlider->setValue(volumeSlider->maximum());

Expand Down Expand Up @@ -1592,6 +1595,8 @@ void MainWindow::initMedia() {
connect(media, &Media::stateChanged, this, &MainWindow::stateChanged);
connect(media, &Media::positionChanged, this, &MainWindow::tick);

seekSlider->setMedia(media);

connect(seekSlider, &QSlider::sliderMoved, this, [this](int value) {
// value : maxValue = posit ion : duration
qint64 ms = (value * media->duration()) / seekSlider->maximum();
Expand Down Expand Up @@ -1635,29 +1640,17 @@ void MainWindow::tick(qint64 time) {
seekSlider->setValue(value);
}

const QString s = formatTime(time);
const QString s = Helper::formatTime(time);
if (s != currentTimeLabel->text()) {
currentTimeLabel->setText(s);
emit currentTimeChanged(s);

// remaining time
const qint64 remainingTime = media->remainingTime();
currentTimeLabel->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
currentTimeLabel->setStatusTip(tr("Remaining time: %1").arg(Helper::formatTime(remainingTime)));
}
}

QString MainWindow::formatTime(qint64 duration) {
duration /= 1000;
QString res;
int seconds = (int)(duration % 60);
duration /= 60;
int minutes = (int)(duration % 60);
duration /= 60;
int hours = (int)(duration % 24);
if (hours == 0) return res.sprintf("%02d:%02d", minutes, seconds);
return res.sprintf("%02d:%02d:%02d", hours, minutes, seconds);
}

void MainWindow::volumeUp() {
qreal newVolume = media->volume() + .1;
if (newVolume > 1.) newVolume = 1.;
Expand Down
6 changes: 3 additions & 3 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SearchParams;
class VideoSource;
class Suggestion;
class ToolbarMenu;
class TimeSlider;

class MainWindow : public QMainWindow {
Q_OBJECT
Expand All @@ -43,7 +44,7 @@ class MainWindow : public QMainWindow {
static MainWindow *instance();
MainWindow();

QSlider *getSeekSlider() { return seekSlider; }
TimeSlider *getSeekSlider() { return seekSlider; }
QSlider *getVolumeSlider() { return volumeSlider; }

QLabel *getCurrentTimeLabel() { return currentTimeLabel; }
Expand Down Expand Up @@ -156,7 +157,6 @@ private slots:
void createToolBar();
void createStatusBar();
void showView(View *view, bool transition = false);
static QString formatTime(qint64 duration);
bool confirmQuit();
bool needStatusBar();
void adjustMessageLabelPosition();
Expand Down Expand Up @@ -216,7 +216,7 @@ private slots:
SearchLineEdit *toolbarSearch;
QToolBar *statusToolBar;
QAction *regionAction;
QSlider *seekSlider;
TimeSlider *seekSlider;
QSlider *volumeSlider;
QLabel *currentTimeLabel;

Expand Down
41 changes: 41 additions & 0 deletions src/timeslider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "timeslider.h"
#include "media.h"
#include "helper.h"

#include <QHelpEvent>

TimeSlider::TimeSlider(QWidget *parent) : SeekSlider(parent), media(nullptr) {
setMouseTracking( true );
setMaximum(1000);
}

void TimeSlider::setMedia(Media* newMedia)
{
media = newMedia;
}

bool TimeSlider::event(QEvent *event)
{
if (isEnabled() && event->type() == QEvent::ToolTip && nullptr != media) {
QHelpEvent * help_event = static_cast<QHelpEvent *>(event);
const qint64 duration = media->duration();

if (duration > 0) {
const qreal percentage = (qreal) help_event->x() / (qreal) width();
const qint64 time = (percentage * duration);

if (time > 0 && time <= duration) {
QToolTip::showText(help_event->globalPos(), Helper::formatTime(time), this);
} else {
QToolTip::hideText();
event->ignore();
}
}
else {
QToolTip::hideText();
event->ignore();
}
return true;
}
return SeekSlider::event(event);
}
26 changes: 26 additions & 0 deletions src/timeslider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef TIMESLIDER_H
#define TIMESLIDER_H

#include "seekslider.h"

class Media;


class TimeSlider : public SeekSlider
{
Q_OBJECT

public:
TimeSlider(QWidget *parent = nullptr);

void setMedia(Media* newMedia);

protected:
bool event(QEvent *event) override;

private:
Media* media;

};

#endif // TIMESLIDER_H
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载