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"
26 #include "iconutils.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(
38 IconUtils::icon("go-previous"),
40 backAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Left));
41 connect(backAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goBack()));
42 addAction(backAction);
44 forwardAction = new QAction(
45 IconUtils::icon("go-next"),
47 forwardAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
48 connect(forwardAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goForward()));
49 addAction(forwardAction);
51 foreach (QAction* action, actions()) {
52 window()->addAction(action);
53 IconUtils::setupAction(action);
56 QWidget *spacerWidget = new QWidget(this);
57 spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
58 addWidget(spacerWidget);
61 QSize SidebarHeader::minimumSizeHint (void) const {
62 return(QSize(160, QFontMetrics(font()).height() * 1.9));
65 void SidebarHeader::updateInfo() {
68 QList<VideoSource*> history = MediaView::instance()->getHistory();
69 int currentIndex = MediaView::instance()->getHistoryIndex();
71 bool canGoForward = MediaView::instance()->canGoForward();
72 forwardAction->setVisible(canGoForward);
73 forwardAction->setEnabled(canGoForward);
75 VideoSource *nextVideoSource = history.at(currentIndex + 1);
76 forwardAction->setStatusTip(
78 .arg(nextVideoSource->getName())
79 + " (" + forwardAction->shortcut().toString(QKeySequence::NativeText) + ")"
83 bool canGoBack = MediaView::instance()->canGoBack();
84 bool backVisible = canGoForward || canGoBack;
85 backAction->setVisible(backVisible);
86 backAction->setEnabled(canGoBack);
88 VideoSource *previousVideoSource = history.at(currentIndex - 1);
89 backAction->setStatusTip(
91 .arg(previousVideoSource->getName())
92 + " (" + backAction->shortcut().toString(QKeySequence::NativeText) + ")"
96 VideoSource *currentVideoSource = history.at(currentIndex);
97 connect(currentVideoSource, SIGNAL(nameChanged(QString)),
98 SLOT(updateTitle(QString)), Qt::UniqueConnection);
99 setTitle(currentVideoSource->getName());
102 void SidebarHeader::updateTitle(QString title) {
103 sender()->disconnect(this);
107 void SidebarHeader::setTitle(QString title) {
111 QList<VideoSource*> history = MediaView::instance()->getHistory();
112 int currentIndex = MediaView::instance()->getHistoryIndex();
113 VideoSource *currentVideoSource = history.at(currentIndex);
114 foreach (QAction* action, videoSourceActions)
115 removeAction(action);
116 videoSourceActions = currentVideoSource->getActions();
117 addActions(videoSourceActions);
120 void SidebarHeader::paintEvent(QPaintEvent *event) {
121 QToolBar::paintEvent(event);
122 if (title.isEmpty()) return;
124 p.setFont(FontUtils::smallBold());
127 const QRect r = rect();
130 QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
132 static const int margin = 50;
133 while (textBox.width() > r.width() - margin*2 && t.length() > 3) {
134 t = t.left(t.length() - i).trimmed() + QLatin1String("...");
135 textBox = p.boundingRect(r, Qt::AlignCenter, t);
138 p.drawText(r, Qt::AlignCenter, t);