]> git.sur5r.net Git - minitube/blob - src/sidebarheader.cpp
Use normal font
[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 "iconutils.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     foreach (QAction* action, actions()) {
51         window()->addAction(action);
52         IconUtils::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 (void) const {
61     return(QSize(160, QFontMetrics(font()).height() * 1.9));
62 }
63
64 void SidebarHeader::updateInfo() {
65     setup();
66
67     QList<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(
76                     tr("Forward to %1")
77                     .arg(nextVideoSource->getName())
78                     + " (" + forwardAction->shortcut().toString(QKeySequence::NativeText) + ")"
79                     );
80     }
81
82     bool canGoBack = MediaView::instance()->canGoBack();
83     bool backVisible = canGoForward || canGoBack;
84     backAction->setVisible(backVisible);
85     backAction->setEnabled(canGoBack);
86     if (canGoBack) {
87         VideoSource *previousVideoSource = history.at(currentIndex - 1);
88         backAction->setStatusTip(
89                     tr("Back to %1")
90                     .arg(previousVideoSource->getName())
91                     + " (" + backAction->shortcut().toString(QKeySequence::NativeText) + ")"
92                     );
93     }
94
95     VideoSource *currentVideoSource = history.at(currentIndex);
96     connect(currentVideoSource, SIGNAL(nameChanged(QString)),
97             SLOT(updateTitle(QString)), Qt::UniqueConnection);
98     setTitle(currentVideoSource->getName());
99 }
100
101 void SidebarHeader::updateTitle(QString title) {
102     sender()->disconnect(this);
103     setTitle(title);
104 }
105
106 void SidebarHeader::setTitle(QString title) {
107     this->title = title;
108     update();
109
110     QList<VideoSource*> history = MediaView::instance()->getHistory();
111     int currentIndex = MediaView::instance()->getHistoryIndex();
112     VideoSource *currentVideoSource = history.at(currentIndex);
113     foreach (QAction* action, videoSourceActions)
114         removeAction(action);
115     videoSourceActions = currentVideoSource->getActions();
116     addActions(videoSourceActions);
117 }
118
119 void SidebarHeader::paintEvent(QPaintEvent *event) {
120     QToolBar::paintEvent(event);
121     if (title.isEmpty()) return;
122     QPainter p(this);
123     p.setPen(Qt::white);
124
125     const QRect r = rect();
126
127     QString t = title;
128     QRect textBox = p.boundingRect(r, Qt::AlignCenter, t);
129     int i = 1;
130     static const int margin = 50;
131     while (textBox.width() > r.width() - margin*2 && t.length() > 3) {
132         t = t.left(t.length() - i).trimmed() + QLatin1String("...");
133         textBox = p.boundingRect(r, Qt::AlignCenter, t);
134         i++;
135     }
136     p.drawText(r, Qt::AlignCenter, t);
137
138
139 }