]> git.sur5r.net Git - minitube/blob - src/sidebarheader.cpp
Imported Upstream version 2.0
[minitube] / src / sidebarheader.cpp
1 #include "sidebarheader.h"
2 #include "utils.h"
3 #include "mediaview.h"
4 #include "videosource.h"
5 #include "fontutils.h"
6
7 SidebarHeader::SidebarHeader(QWidget *parent) : QToolBar(parent) { }
8
9 void SidebarHeader::setup() {
10     static bool isSetup = false;
11     if (isSetup) return;
12     isSetup = true;
13
14     backAction = new QAction(
15                 Utils::icon("go-previous"),
16                 tr("&Back"), this);
17     backAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Left));
18     connect(backAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goBack()));
19     addAction(backAction);
20
21     forwardAction = new QAction(
22                 Utils::icon("go-next"),
23                 tr("&Back"), this);
24     forwardAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
25     connect(forwardAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goForward()));
26     addAction(forwardAction);
27
28     foreach (QAction* action, actions()) {
29         window()->addAction(action);
30         action->setAutoRepeat(false);
31     }
32
33     /*
34     QWidget *spacerWidget = new QWidget(this);
35     spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
36     spacerWidget->setVisible(true);
37     addWidget(spacerWidget);
38     */
39 }
40
41 QSize SidebarHeader::minimumSizeHint (void) const {
42     return(QSize(1, QFontMetrics(font()).height() * 1.9));
43 }
44
45 void SidebarHeader::updateInfo() {
46     setup();
47
48     QList<VideoSource*> history = MediaView::instance()->getHistory();
49     int currentIndex = MediaView::instance()->getHistoryIndex();
50
51     bool canGoForward = MediaView::instance()->canGoForward();
52     forwardAction->setVisible(canGoForward);
53     forwardAction->setEnabled(canGoForward);
54     if (canGoForward) {
55         VideoSource *nextVideoSource = history.at(currentIndex + 1);
56         forwardAction->setStatusTip(
57                     tr("Forward to %1")
58                     .arg(nextVideoSource->getName())
59                     + " (" + forwardAction->shortcut().toString(QKeySequence::NativeText) + ")"
60                     );
61     }
62
63     bool canGoBack = MediaView::instance()->canGoBack();
64     bool backVisible = canGoForward || canGoBack;
65     backAction->setVisible(backVisible);
66     backAction->setEnabled(canGoBack);
67     if (canGoBack) {
68         VideoSource *previousVideoSource = history.at(currentIndex - 1);
69         backAction->setStatusTip(
70                     tr("Back to %1")
71                     .arg(previousVideoSource->getName())
72                     + " (" + backAction->shortcut().toString(QKeySequence::NativeText) + ")"
73                     );
74     }
75
76     VideoSource *currentVideoSource = history.at(currentIndex);
77     connect(currentVideoSource, SIGNAL(nameChanged(QString)),
78             SLOT(updateTitle(QString)), Qt::UniqueConnection);
79     setTitle(currentVideoSource->getName());
80 }
81
82 void SidebarHeader::updateTitle(QString title) {
83     sender()->disconnect(this);
84     setTitle(title);
85 }
86
87 void SidebarHeader::setTitle(QString title) {
88     this->title = title;
89     update();
90 }
91
92 void SidebarHeader::paintEvent(QPaintEvent *event) {
93     QToolBar::paintEvent(event);
94     if (title.isEmpty()) return;
95     QPainter p(this);
96     p.setFont(FontUtils::smallBold());
97     p.setPen(Qt::white);
98
99     const QRect r = rect();
100
101     QString t = title;
102     QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
103     int i = 1;
104     static const int margin = 100;
105     while (textBox.width() > r.width() - margin) {
106         t = t.left(t.length() - i) + "...";
107         textBox = p.boundingRect(r, Qt::AlignCenter, t);
108         i++;
109     }
110
111     p.drawText(r, Qt::AlignCenter, t);
112 }