3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "sidebarheader.h"
22 #include "fontutils.h"
23 #include "iconutils.h"
24 #include "mainwindow.h"
25 #include "mediaview.h"
26 #include "videosource.h"
28 SidebarHeader::SidebarHeader(QWidget *parent) : QToolBar(parent) {}
30 void SidebarHeader::setup() {
31 static bool isSetup = false;
35 setIconSize(QSize(16, 16));
37 backAction = new QAction(tr("&Back"), this);
38 IconUtils::setIcon(backAction, "go-previous");
39 backAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Left));
40 connect(backAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goBack()));
41 addAction(backAction);
43 forwardAction = new QAction(tr("&Forward"), this);
44 IconUtils::setIcon(forwardAction, "go-next");
45 forwardAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
46 connect(forwardAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goForward()));
47 addAction(forwardAction);
49 const auto a = actions();
50 for (QAction *action : a) {
51 window()->addAction(action);
52 MainWindow::instance()->setupAction(action);
55 QWidget *spacerWidget = new QWidget(this);
56 spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
57 addWidget(spacerWidget);
60 QSize SidebarHeader::minimumSizeHint() const {
61 return QSize(160, QFontMetrics(font()).height() * 1.9);
64 void SidebarHeader::updateInfo() {
67 const QVector<VideoSource *> &history = MediaView::instance()->getHistory();
68 int currentIndex = MediaView::instance()->getHistoryIndex();
70 bool canGoForward = MediaView::instance()->canGoForward();
71 forwardAction->setVisible(canGoForward);
72 forwardAction->setEnabled(canGoForward);
74 VideoSource *nextVideoSource = history.at(currentIndex + 1);
75 forwardAction->setStatusTip(tr("Forward to %1").arg(nextVideoSource->getName()) + " (" +
76 forwardAction->shortcut().toString(QKeySequence::NativeText) +
80 bool canGoBack = MediaView::instance()->canGoBack();
81 bool backVisible = canGoForward || canGoBack;
82 backAction->setVisible(backVisible);
83 backAction->setEnabled(canGoBack);
85 VideoSource *previousVideoSource = history.at(currentIndex - 1);
86 backAction->setStatusTip(tr("Back to %1").arg(previousVideoSource->getName()) + " (" +
87 backAction->shortcut().toString(QKeySequence::NativeText) + ")");
90 VideoSource *currentVideoSource = history.at(currentIndex);
91 connect(currentVideoSource, SIGNAL(nameChanged(QString)), SLOT(updateTitle(QString)),
92 Qt::UniqueConnection);
93 setTitle(currentVideoSource->getName());
96 void SidebarHeader::updateTitle(const QString &title) {
97 sender()->disconnect(this);
101 void SidebarHeader::setTitle(const QString &title) {
105 QVector<VideoSource *> history = MediaView::instance()->getHistory();
106 int currentIndex = MediaView::instance()->getHistoryIndex();
107 VideoSource *currentVideoSource = history.at(currentIndex);
108 for (QAction *action : videoSourceActions)
109 removeAction(action);
110 videoSourceActions = currentVideoSource->getActions();
111 addActions(videoSourceActions);
114 void SidebarHeader::paintEvent(QPaintEvent *event) {
115 if (title.isEmpty()) return;
117 p.setPen(palette().windowText().color());
119 const QRect r = rect();
122 QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
124 const int margin = forwardAction->isVisible() ? 50 : 25;
125 while (textBox.width() > r.width() - margin * 2 && t.length() > 3) {
126 t = t.left(t.length() - i).trimmed() + QStringLiteral("\u2026");
127 textBox = p.boundingRect(r, Qt::AlignCenter, t);
130 p.drawText(r, Qt::AlignCenter, t);