1 /*
2 * psitabbar.cpp - Tabbar child for Psi
3 * Copyright (C) 2006 Kevin Smith
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "psitabbar.h"
22 #include "psitabwidget.h"
23 #include <QMouseEvent>
24 #include <QApplication>
25 #include <QDrag>
26 #include <QMimeData>
27 #include <QPainter>
28
29 #include "psioptions.h"
30
31 /**
32 * Constructor
33 */
PsiTabBar(PsiTabWidget * parent)34 PsiTabBar::PsiTabBar(PsiTabWidget *parent)
35 : QTabBar(parent)
36 , dragsEnabled_(true) {
37 //setAcceptDrops(true);
38
39 setMovable(true);
40 setTabsClosable(true);
41 setSelectionBehaviorOnRemove ( QTabBar::SelectPreviousTab );
42 currTab=-1;
43 }
44
45 /**
46 * Destructor
47 */
~PsiTabBar()48 PsiTabBar::~PsiTabBar() {
49 }
50
51 /**
52 * Returns the parent PsiTabWidget.
53 */
psiTabWidget()54 PsiTabWidget* PsiTabBar::psiTabWidget() {
55 return dynamic_cast<PsiTabWidget*> (parent());
56 }
57
58 /**
59 * Overriding this allows us to emit signals for double clicks
60 */
mouseDoubleClickEvent(QMouseEvent * event)61 void PsiTabBar::mouseDoubleClickEvent(QMouseEvent *event) {
62 const QPoint pos = event->pos();
63 int tab = findTabUnder(pos);
64 if (tab >= 0 && tab < count()) {
65 emit mouseDoubleClickTab(tab);
66 }
67 }
68
69 /*
70 * Returns the index of the tab at a position, or -1 if out of bounds.
71 */
findTabUnder(const QPoint & pos)72 int PsiTabBar::findTabUnder(const QPoint &pos) {
73 for (int i = 0; i < count(); i++) {
74 if (tabRect(i).contains(pos)) {
75 return i;
76 }
77 }
78 return -1;
79 }
80
mousePressEvent(QMouseEvent * event)81 void PsiTabBar::mousePressEvent(QMouseEvent *event) {
82 QTabBar::mousePressEvent(event);
83 event->accept();
84 }
85
mouseReleaseEvent(QMouseEvent * event)86 void PsiTabBar::mouseReleaseEvent ( QMouseEvent * event )
87 {
88 if (event->button() == Qt::MidButton && findTabUnder(event->pos())!=-1) {
89 emit mouseMiddleClickTab(findTabUnder(event->pos()));
90 event->accept();
91 }
92 QTabBar::mouseReleaseEvent(event);
93
94 if ((dragTab_ != -1) && (event->button() != Qt::MidButton)) {
95 this->setCurrentIndex(currentIndex());
96 }
97 };
98
99 /*
100 * Used for starting drags of tabs
101 */
mouseMoveEvent(QMouseEvent * event)102 void PsiTabBar::mouseMoveEvent(QMouseEvent *event) {
103 if (!dragsEnabled_) {
104 return;
105 }
106 if (!(event->buttons() & Qt::LeftButton)) {
107 currTab=-1;
108 return;
109 }
110 if ((event->pos() - dragStartPosition_).manhattanLength()
111 < QApplication::startDragDistance()) {
112 return;
113 }
114
115 QTabBar::mouseMoveEvent(event);
116 }
117
contextMenuEvent(QContextMenuEvent * event)118 void PsiTabBar::contextMenuEvent(QContextMenuEvent *event) {
119 event->accept();
120 emit contextMenu(event, findTabUnder(event->pos()));
121 }
122
wheelEvent(QWheelEvent * event)123 void PsiTabBar::wheelEvent(QWheelEvent *event) {
124 int numDegrees = event->delta() / 8;
125 int numSteps = numDegrees / 15;
126
127 int newIndex = currentIndex() - numSteps;
128
129 while (newIndex < 0) {
130 newIndex += count();
131 }
132 newIndex = newIndex % count();
133
134 setCurrentIndex(newIndex);
135
136 event->accept();
137 }
138
139 /*
140 * Enable/disable dragging of tabs
141 */
setDragsEnabled(bool enabled)142 void PsiTabBar::setDragsEnabled(bool enabled) {
143 dragsEnabled_ = enabled;
144 }
145
paintEvent(QPaintEvent * event)146 void PsiTabBar::paintEvent(QPaintEvent *event)
147 {
148 QTabBar::paintEvent(event);
149 };
150
resizeEvent(QResizeEvent * event)151 void PsiTabBar::resizeEvent(QResizeEvent * event)
152 {
153 QTabBar::resizeEvent(event);
154 };
155