]> git.sur5r.net Git - minitube/blob - src/sidebarheader.cpp
New upstream version 3.1
[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 "fontutils.h"
23 #include "iconutils.h"
24 #include "mainwindow.h"
25 #include "mediaview.h"
26 #include "videosource.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(tr("&Back"), this);
38     IconUtils::setIcon(backAction, "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);
42
43     forwardAction = new QAction(tr("&Forward"), this);
44     IconUtils::setIcon(forwardAction, "go-next");
45     forwardAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
46     connect(forwardAction, SIGNAL(triggered()), MediaView::instance(), SLOT(goForward()));
47     addAction(forwardAction);
48
49     const auto a = actions();
50     for (QAction *action : a) {
51         window()->addAction(action);
52         MainWindow::instance()->setupAction(action);
53     }
54
55     QWidget *spacerWidget = new QWidget(this);
56     spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
57     addWidget(spacerWidget);
58 }
59
60 QSize SidebarHeader::minimumSizeHint() const {
61     return QSize(160, QFontMetrics(font()).height() * 1.9);
62 }
63
64 void SidebarHeader::updateInfo() {
65     setup();
66
67     const QVector<VideoSource *> &history = MediaView::instance()->getHistory();
68     int currentIndex = MediaView::instance()->getHistoryIndex();
69
70     bool canGoForward = MediaView::instance()->canGoForward();
71     forwardAction->setVisible(canGoForward);
72     forwardAction->setEnabled(canGoForward);
73     if (canGoForward) {
74         VideoSource *nextVideoSource = history.at(currentIndex + 1);
75         forwardAction->setStatusTip(tr("Forward to %1").arg(nextVideoSource->getName()) + " (" +
76                                     forwardAction->shortcut().toString(QKeySequence::NativeText) +
77                                     ")");
78     }
79
80     bool canGoBack = MediaView::instance()->canGoBack();
81     bool backVisible = canGoForward || canGoBack;
82     backAction->setVisible(backVisible);
83     backAction->setEnabled(canGoBack);
84     if (canGoBack) {
85         VideoSource *previousVideoSource = history.at(currentIndex - 1);
86         backAction->setStatusTip(tr("Back to %1").arg(previousVideoSource->getName()) + " (" +
87                                  backAction->shortcut().toString(QKeySequence::NativeText) + ")");
88     }
89
90     VideoSource *currentVideoSource = history.at(currentIndex);
91     connect(currentVideoSource, SIGNAL(nameChanged(QString)), SLOT(updateTitle(QString)),
92             Qt::UniqueConnection);
93     setTitle(currentVideoSource->getName());
94 }
95
96 void SidebarHeader::updateTitle(const QString &title) {
97     sender()->disconnect(this);
98     setTitle(title);
99 }
100
101 void SidebarHeader::setTitle(const QString &title) {
102     this->title = title;
103     update();
104
105     QVector<VideoSource *> history = MediaView::instance()->getHistory();
106     int currentIndex = MediaView::instance()->getHistoryIndex();
107     VideoSource *currentVideoSource = history.at(currentIndex);
108     for (QAction *action : videoSourceActions)
109         removeAction(action);
110     videoSourceActions = currentVideoSource->getActions();
111     addActions(videoSourceActions);
112 }
113
114 void SidebarHeader::paintEvent(QPaintEvent *event) {
115     if (title.isEmpty()) return;
116     QPainter p(this);
117     p.setPen(palette().windowText().color());
118
119     const QRect r = rect();
120
121     QString t = title;
122     QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
123     int i = 1;
124     const int margin = forwardAction->isVisible() ? 50 : 25;
125     while (textBox.width() > r.width() - margin * 2 && t.length() > 3) {
126         t = t.left(t.length() - i).trimmed() + QStringLiteral("\u2026");
127         textBox = p.boundingRect(r, Qt::AlignCenter, t);
128         i++;
129     }
130     p.drawText(r, Qt::AlignCenter, t);
131 }