1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A MIDI and audio sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7 
8     Other copyrights also apply to some parts of this work.  Please
9     see the AUTHORS file and individual file headers for details.
10 
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.  See the file
15     COPYING included with this distribution for more information.
16 */
17 
18 #include "ControlRulerTabBar.h"
19 
20 #include "misc/Debug.h"
21 #include "gui/general/IconLoader.h"
22 
23 #include <QTabBar>
24 #include <QString>
25 #include <QPainter>
26 #include <QMouseEvent>
27 
28 namespace Rosegarden
29 {
30 
ControlRulerTabBar()31 ControlRulerTabBar::ControlRulerTabBar():QTabBar()
32 {
33     m_closeIcon = QPixmap(IconLoader::loadPixmap("tab-close"));
34 }
35 
paintEvent(QPaintEvent * event)36 void ControlRulerTabBar::paintEvent(QPaintEvent *event)
37 {
38     QTabBar::paintEvent(event);
39 
40     QPainter painter(this);
41 
42     for (std::vector<QRect*>::iterator it = m_closeButtons.begin(); it != m_closeButtons.end(); ++it) {
43         painter.drawPixmap((*it)->topLeft(), m_closeIcon);
44     }
45 }
46 
mousePressEvent(QMouseEvent * event)47 void ControlRulerTabBar::mousePressEvent(QMouseEvent *event)
48 {
49     if (event->buttons() & Qt::LeftButton) {
50         int index = 0;
51         for (std::vector<QRect*>::iterator it = m_closeButtons.begin(); it != m_closeButtons.end(); ++it) {
52             if ((*it)->contains(event->pos())) {
53                 emit tabCloseRequest(index);
54                 return;
55             }
56             index++;
57         }
58     }
59 
60     QTabBar::mousePressEvent(event);
61 }
62 
tabLayoutChange()63 void ControlRulerTabBar::tabLayoutChange()
64 {
65     for (std::vector<QRect*>::iterator it = m_closeButtons.begin(); it != m_closeButtons.end(); ++it) {
66         delete(*it);
67     }
68     m_closeButtons.clear();
69 
70     QRect *newButton, rect;
71     for (int index = 0; index < count(); ++index) {
72         rect = tabRect(index);
73         // ??? MEMORY LEAK (confirmed)
74         newButton = new QRect(rect.right()-hMargin-m_closeIcon.width(),
75                 rect.top()+(rect.height()-m_closeIcon.height())/2,
76                 m_closeIcon.width(),
77                 m_closeIcon.height());
78         m_closeButtons.push_back(newButton);
79     }
80 }
81 
addTab(const QString & text)82 int ControlRulerTabBar::addTab(const QString &text)
83 {
84     // Append some white space to the tab name to make room
85     // for our close icon
86     QString str = text;
87     str.append("        ");
88 
89     int newindex = QTabBar::addTab(str);
90 
91     return newindex;
92 }
93 
94 }
95 
96