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 "iconutils.h"
23 #include "mediaview.h"
24 #include "videosource.h"
25 #include "fontutils.h"
27 SidebarHeader::SidebarHeader(QWidget *parent) : QToolBar(parent) { }
29 void SidebarHeader::setup() {
30 static bool isSetup = false;
34 setIconSize(QSize(16, 16));
36 backAction = new QAction(
37 IconUtils::icon("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(
44 IconUtils::icon("go-next"),
46 forwardAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
47 connect(forwardAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goForward()));
48 addAction(forwardAction);
50 foreach (QAction* action, actions()) {
51 window()->addAction(action);
52 IconUtils::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 QList<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(
77 .arg(nextVideoSource->getName())
78 + " (" + forwardAction->shortcut().toString(QKeySequence::NativeText) + ")"
82 bool canGoBack = MediaView::instance()->canGoBack();
83 bool backVisible = canGoForward || canGoBack;
84 backAction->setVisible(backVisible);
85 backAction->setEnabled(canGoBack);
87 VideoSource *previousVideoSource = history.at(currentIndex - 1);
88 backAction->setStatusTip(
90 .arg(previousVideoSource->getName())
91 + " (" + backAction->shortcut().toString(QKeySequence::NativeText) + ")"
95 VideoSource *currentVideoSource = history.at(currentIndex);
96 connect(currentVideoSource, SIGNAL(nameChanged(QString)),
97 SLOT(updateTitle(QString)), Qt::UniqueConnection);
98 setTitle(currentVideoSource->getName());
101 void SidebarHeader::updateTitle(const QString &title) {
102 sender()->disconnect(this);
106 void SidebarHeader::setTitle(const QString &title) {
110 QList<VideoSource*> history = MediaView::instance()->getHistory();
111 int currentIndex = MediaView::instance()->getHistoryIndex();
112 VideoSource *currentVideoSource = history.at(currentIndex);
113 foreach (QAction* action, videoSourceActions)
114 removeAction(action);
115 videoSourceActions = currentVideoSource->getActions();
116 addActions(videoSourceActions);
119 void SidebarHeader::paintEvent(QPaintEvent *event) {
120 QToolBar::paintEvent(event);
121 if (title.isEmpty()) return;
124 p.setFont(FontUtils::small());
126 const QRect r = rect();
129 QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
131 const int margin = forwardAction->isVisible() ? 45 : 20;
132 while (textBox.width() > r.width() - margin*2 && t.length() > 3) {
133 t = t.left(t.length() - i).trimmed() + QLatin1String("...");
134 textBox = p.boundingRect(r, Qt::AlignCenter, t);
137 p.drawText(r, Qt::AlignCenter, t);