From 501110c328dfeb0aefc7ffe4d8d0dba89e6c52bf Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Wed, 24 Jul 2019 01:53:24 -0700 Subject: [PATCH] started timeruler object --- app/common/CMakeLists.txt | 2 + app/common/qtversionabstraction.cpp | 9 ++ app/common/qtversionabstraction.h | 8 ++ app/panel/timeline/timeline.cpp | 15 ++++ app/widget/CMakeLists.txt | 1 + app/widget/nodeview/nodeviewitem.cpp | 13 +-- .../projectexplorericonviewitemdelegate.cpp | 8 +- app/widget/timeruler/CMakeLists.txt | 22 +++++ app/widget/timeruler/timeruler.cpp | 84 +++++++++++++++++++ app/widget/timeruler/timeruler.h | 39 +++++++++ 10 files changed, 186 insertions(+), 15 deletions(-) create mode 100644 app/common/qtversionabstraction.cpp create mode 100644 app/common/qtversionabstraction.h create mode 100644 app/widget/timeruler/CMakeLists.txt create mode 100644 app/widget/timeruler/timeruler.cpp create mode 100644 app/widget/timeruler/timeruler.h diff --git a/app/common/CMakeLists.txt b/app/common/CMakeLists.txt index cbe006e96..5ac00026b 100644 --- a/app/common/CMakeLists.txt +++ b/app/common/CMakeLists.txt @@ -22,5 +22,7 @@ set(OLIVE_SOURCES common/rational.h common/rational.cpp common/qobjectlistcast.h + common/qtversionabstraction.h + common/qtversionabstraction.cpp PARENT_SCOPE ) diff --git a/app/common/qtversionabstraction.cpp b/app/common/qtversionabstraction.cpp new file mode 100644 index 000000000..ed61b6122 --- /dev/null +++ b/app/common/qtversionabstraction.cpp @@ -0,0 +1,9 @@ +#include "qtversionabstraction.h" + +int QFontMetricsWidth(const QFontMetrics* fm, const QString& s) { +#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) + return fm->width(s); +#else + return fm->horizontalAdvance(s); +#endif +} diff --git a/app/common/qtversionabstraction.h b/app/common/qtversionabstraction.h new file mode 100644 index 000000000..006c7d041 --- /dev/null +++ b/app/common/qtversionabstraction.h @@ -0,0 +1,8 @@ +#ifndef QTVERSIONABSTRACTION_H +#define QTVERSIONABSTRACTION_H + +#include + +int QFontMetricsWidth(const QFontMetrics* fm, const QString& s); + +#endif // QTVERSIONABSTRACTION_H diff --git a/app/panel/timeline/timeline.cpp b/app/panel/timeline/timeline.cpp index 5a05792f2..d09c91ae8 100644 --- a/app/panel/timeline/timeline.cpp +++ b/app/panel/timeline/timeline.cpp @@ -20,9 +20,24 @@ #include "timeline.h" +#include + +#include "widget/timeruler/timeruler.h" + TimelinePanel::TimelinePanel(QWidget *parent) : PanelWidget(parent) { + // FIXME: Test code + QWidget* main = new QWidget(this); + setWidget(main); + + QVBoxLayout* layout = new QVBoxLayout(main); + + TimeRuler* tr = new TimeRuler(true, this); + layout->addWidget(tr); + layout->addStretch(); + // End test code + Retranslate(); } diff --git a/app/widget/CMakeLists.txt b/app/widget/CMakeLists.txt index f1e1f87bc..b5345aaee 100644 --- a/app/widget/CMakeLists.txt +++ b/app/widget/CMakeLists.txt @@ -24,6 +24,7 @@ add_subdirectory(playbackcontrols) add_subdirectory(projectexplorer) add_subdirectory(projecttoolbar) add_subdirectory(taskview) +add_subdirectory(timeruler) add_subdirectory(toolbar) add_subdirectory(viewer) diff --git a/app/widget/nodeview/nodeviewitem.cpp b/app/widget/nodeview/nodeviewitem.cpp index f7e889957..d73716587 100644 --- a/app/widget/nodeview/nodeviewitem.cpp +++ b/app/widget/nodeview/nodeviewitem.cpp @@ -26,6 +26,7 @@ #include #include +#include "common/qtversionabstraction.h" #include "core.h" #include "nodeview.h" #include "nodeviewundo.h" @@ -52,11 +53,7 @@ NodeViewItem::NodeViewItem(QGraphicsItem *parent) : // // Not particularly great way of using text scaling to set the width (DPI-awareness, etc.) -#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) - int widget_width = font_metrics.width("HHHHHHHHHHHHHHHH"); -#else - int widget_width = font_metrics.horizontalAdvance("HHHHHHHHHHHHHHHH"); -#endif + int widget_width = QFontMetricsWidth(&font_metrics, "HHHHHHHHHHHHHHHH"); // Set border width node_border_width_ = font_metrics.height() / 12; @@ -207,11 +204,7 @@ void NodeViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti QPointF text_pt = GetParameterTextPoint(i); if (param->type() == NodeParam::kOutput) { -#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) - text_pt -= QPointF(font_metrics.width(param->name()), 0); -#else - text_pt -= QPointF(font_metrics.horizontalAdvance(param->name()), 0); -#endif + text_pt -= QPointF(QFontMetricsWidth(&font_metrics, param->name()), 0); } painter->drawText(text_pt, param->name()); diff --git a/app/widget/projectexplorer/projectexplorericonviewitemdelegate.cpp b/app/widget/projectexplorer/projectexplorericonviewitemdelegate.cpp index bdcce74d9..524c06917 100644 --- a/app/widget/projectexplorer/projectexplorericonviewitemdelegate.cpp +++ b/app/widget/projectexplorer/projectexplorericonviewitemdelegate.cpp @@ -22,6 +22,8 @@ #include +#include "common/qtversionabstraction.h" + ProjectExplorerIconViewItemDelegate::ProjectExplorerIconViewItemDelegate(QObject *parent) : QStyledItemDelegate (parent) { @@ -62,11 +64,7 @@ void ProjectExplorerIconViewItemDelegate::paint(QPainter *painter, const QStyleO QString duration_str = index.data(Qt::UserRole).toString(); -#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) - int timecode_width = fm.width(duration_str); -#else - int timecode_width = fm.horizontalAdvance(duration_str); -#endif + int timecode_width = QFontMetricsWidth(&fm, duration_str); int max_name_width = option.rect.width(); diff --git a/app/widget/timeruler/CMakeLists.txt b/app/widget/timeruler/CMakeLists.txt new file mode 100644 index 000000000..9fe06158f --- /dev/null +++ b/app/widget/timeruler/CMakeLists.txt @@ -0,0 +1,22 @@ +# Olive - Non-Linear Video Editor +# Copyright (C) 2019 Olive Team +# +# 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 3 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, see . + +set(OLIVE_SOURCES + ${OLIVE_SOURCES} + widget/timeruler/timeruler.h + widget/timeruler/timeruler.cpp + PARENT_SCOPE +) diff --git a/app/widget/timeruler/timeruler.cpp b/app/widget/timeruler/timeruler.cpp new file mode 100644 index 000000000..c2e3a3d3d --- /dev/null +++ b/app/widget/timeruler/timeruler.cpp @@ -0,0 +1,84 @@ +#include "timeruler.h" + +#include + +#include "common/qtversionabstraction.h" + +TimeRuler::TimeRuler(bool text_visible, QWidget* parent) : + QWidget(parent), + scroll_(0), + scale_(1.0) +{ + // Text height is used to calculate widget height + text_height_ = fontMetrics().height(); + + // FIXME: Fairly mediocre, but reliable way of scaling UI objects by font/DPI size + QFontMetrics fm = fontMetrics(); + width_of_second_ = QFontMetricsWidth(&fm, "HHHHHHHHHHHHHHHH"); + minimum_gap_between_lines_ = QFontMetricsWidth(&fm, "HH"); + + SetTextVisible(text_visible); +} + +void TimeRuler::SetTextVisible(bool e) +{ + text_visible_ = e; + + if (text_visible_) { + setMinimumHeight(text_height_ * 2); + } else { + setMinimumHeight(text_height_); + } + + update(); +} + +void TimeRuler::SetScale(double d) +{ + scale_ = d; + + update(); +} + +void TimeRuler::SetTimebase(const rational &r) +{ + time_base_ = r; + + update(); +} + +void TimeRuler::paintEvent(QPaintEvent *e) +{ + Q_UNUSED(e) + + QPainter p(this); + + // FIXME: Make this CSS configurable + p.setPen(Qt::white); + + // Calculate where the ruler lines should be + int ruler_second_line_top = 0; + int ruler_second_line_bottom = height(); + + //int ruler_frame_line_top = (text_visible_) ? text_height_ : 0; + //int ruler_frame_line_bottom = height(); + + // Get scaled width of a second + double scaled_width = width_of_second_ * scale_; + + // Draw a line for each second + int last_draw = -1; + for (double i=0;i minimum_gap_between_lines_) { + + p.drawLine(line_x, ruler_second_line_top, line_x, ruler_second_line_bottom); + + p.drawText(line_x, p.fontMetrics().ascent(), QString::number(line_x)); + + last_draw = line_x; + } + } +} diff --git a/app/widget/timeruler/timeruler.h b/app/widget/timeruler/timeruler.h new file mode 100644 index 000000000..06249589d --- /dev/null +++ b/app/widget/timeruler/timeruler.h @@ -0,0 +1,39 @@ +#ifndef TIMERULER_H +#define TIMERULER_H + +#include + +#include "common/rational.h" + +class TimeRuler : public QWidget +{ + Q_OBJECT +public: + TimeRuler(bool text_visible = true, QWidget* parent = nullptr); + + void SetTextVisible(bool e); + + void SetScale(double d); + + void SetTimebase(const rational& r); + +protected: + virtual void paintEvent(QPaintEvent* e) override; + +private: + int text_height_; + + int width_of_second_; + + int minimum_gap_between_lines_; + + int scroll_; + + bool text_visible_; + + double scale_; + + rational time_base_; +}; + +#endif // TIMERULER_H