]> git.sur5r.net Git - minitube/blob - src/sidebarheader.cpp
399d65f244687dc05b96bac9809f2b0a527fc6c3
[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 "iconutils.h"
23 #include "mediaview.h"
24 #include "videosource.h"
25 #include "fontutils.h"
26
27 SidebarHeader::SidebarHeader(QWidget *parent) : QToolBar(parent) { }
28
29 void SidebarHeader::setup() {
30     static bool isSetup = false;
31     if (isSetup) return;
32     isSetup = true;
33
34     setIconSize(QSize(16, 16));
35
36     backAction = new QAction(
37                 IconUtils::icon("go-previous"),
38                 tr("&Back"), this);
39     backAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Left));
40     connect(backAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goBack()));
41     addAction(backAction);
42
43     forwardAction = new QAction(
44                 IconUtils::icon("go-next"),
45                 tr("&Back"), this);
46     forwardAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
47     connect(forwardAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goForward()));
48     addAction(forwardAction);
49
50     const auto a = actions();
51     for (QAction* action : a) {
52         window()->addAction(action);
53         IconUtils::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() const {
62     return QSize(160, QFontMetrics(font()).height() * 1.9);
63 }
64
65 void SidebarHeader::updateInfo() {
66     setup();
67
68     const QVector<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(const QString &title) {
103     sender()->disconnect(this);
104     setTitle(title);
105 }
106
107 void SidebarHeader::setTitle(const QString &title) {
108     this->title = title;
109     update();
110
111     QVector<VideoSource*> history = MediaView::instance()->getHistory();
112     int currentIndex = MediaView::instance()->getHistoryIndex();
113     VideoSource *currentVideoSource = history.at(currentIndex);
114     for (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.setPen(Qt::white);
125
126     const QRect r = rect();
127
128     QString t = title;
129     QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
130     int i = 1;
131     const int margin = forwardAction->isVisible() ? 50 : 25;
132     while (textBox.width() > r.width() - margin*2 && t.length() > 3) {
133         t = t.left(t.length() - i).trimmed() + QStringLiteral("\u2026");
134         textBox = p.boundingRect(r, Qt::AlignCenter, t);
135         i++;
136     }
137     p.drawText(r, Qt::AlignCenter, t);
138
139
140 }