]> git.sur5r.net Git - minitube/blob - src/sidebarheader.cpp
dc8026e4550d18d651f923ca02e13c4ccae375ee
[minitube] / src / sidebarheader.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "sidebarheader.h"
22 #include "utils.h"
23 #include "mediaview.h"
24 #include "videosource.h"
25 #include "fontutils.h"
26 #include "utils.h"
27
28 SidebarHeader::SidebarHeader(QWidget *parent) : QToolBar(parent) { }
29
30 void SidebarHeader::setup() {
31     static bool isSetup = false;
32     if (isSetup) return;
33     isSetup = true;
34
35     setIconSize(QSize(16, 16));
36
37     backAction = new QAction(
38                 Utils::icon("go-previous"),
39                 tr("&Back"), this);
40     backAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Left));
41     connect(backAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goBack()));
42     addAction(backAction);
43
44     forwardAction = new QAction(
45                 Utils::icon("go-next"),
46                 tr("&Back"), this);
47     forwardAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
48     connect(forwardAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goForward()));
49     addAction(forwardAction);
50
51     foreach (QAction* action, actions()) {
52         window()->addAction(action);
53         Utils::setupAction(action);
54     }
55
56     QWidget *spacerWidget = new QWidget(this);
57     spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
58     addWidget(spacerWidget);
59 }
60
61 QSize SidebarHeader::minimumSizeHint (void) const {
62     return(QSize(160, QFontMetrics(font()).height() * 1.9));
63 }
64
65 void SidebarHeader::updateInfo() {
66     setup();
67
68     QList<VideoSource*> history = MediaView::instance()->getHistory();
69     int currentIndex = MediaView::instance()->getHistoryIndex();
70
71     bool canGoForward = MediaView::instance()->canGoForward();
72     forwardAction->setVisible(canGoForward);
73     forwardAction->setEnabled(canGoForward);
74     if (canGoForward) {
75         VideoSource *nextVideoSource = history.at(currentIndex + 1);
76         forwardAction->setStatusTip(
77                     tr("Forward to %1")
78                     .arg(nextVideoSource->getName())
79                     + " (" + forwardAction->shortcut().toString(QKeySequence::NativeText) + ")"
80                     );
81     }
82
83     bool canGoBack = MediaView::instance()->canGoBack();
84     bool backVisible = canGoForward || canGoBack;
85     backAction->setVisible(backVisible);
86     backAction->setEnabled(canGoBack);
87     if (canGoBack) {
88         VideoSource *previousVideoSource = history.at(currentIndex - 1);
89         backAction->setStatusTip(
90                     tr("Back to %1")
91                     .arg(previousVideoSource->getName())
92                     + " (" + backAction->shortcut().toString(QKeySequence::NativeText) + ")"
93                     );
94     }
95
96     VideoSource *currentVideoSource = history.at(currentIndex);
97     connect(currentVideoSource, SIGNAL(nameChanged(QString)),
98             SLOT(updateTitle(QString)), Qt::UniqueConnection);
99     setTitle(currentVideoSource->getName());
100 }
101
102 void SidebarHeader::updateTitle(QString title) {
103     sender()->disconnect(this);
104     setTitle(title);
105 }
106
107 void SidebarHeader::setTitle(QString title) {
108     this->title = title;
109     update();
110
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);
118 }
119
120 void SidebarHeader::paintEvent(QPaintEvent *event) {
121     QToolBar::paintEvent(event);
122     if (title.isEmpty()) return;
123     QPainter p(this);
124     p.setFont(FontUtils::smallBold());
125     p.setPen(Qt::white);
126
127     const QRect r = rect();
128
129     QString t = title;
130     QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
131     int i = 1;
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);
136         i++;
137     }
138     p.drawText(r, Qt::AlignCenter, t);
139
140
141 }