Repository navigation
Expand file tree
/
Copy pathtabbar.cpp
More file actions
349 lines (305 loc) · 10.3 KB
/
Copy pathtabbar.cpp
File metadata and controls
349 lines (305 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright 2008 Aaron Dewes <aaron.dewes@web.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/****************************************************************************
**
** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. In
** addition, as a special exception, Trolltech gives you certain
** additional rights. These rights are described in the Trolltech GPL
** Exception version 1.2, which can be found at
** http://www.trolltech.com/products/qt/gplexception/ and in the file
** GPL_EXCEPTION.txt in this package.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "tabbar.h"
#include "tabwidget.h"
#include <qaction.h>
#include <qapplication.h>
#include <qclipboard.h>
#include <qevent.h>
#include <qmenu.h>
#include <qstyle.h>
#include <qurl.h>
#include <QMimeData>
#include <QDrag>
#include <qdebug.h>
TabShortcut::TabShortcut(int tab, const QKeySequence &key, QWidget *parent)
: QShortcut(key, parent)
, m_tab(tab)
{
}
int TabShortcut::tab()
{
return m_tab;
}
TabBar::TabBar(QWidget *parent)
: QTabBar(parent)
, m_viewTabBarAction(0)
, m_showTabBarWhenOneTab(true)
{
setContextMenuPolicy(Qt::CustomContextMenu);
setAcceptDrops(true);
setElideMode(Qt::ElideRight);
setUsesScrollButtons(true);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(contextMenuRequested(const QPoint &)));
QString alt = QLatin1String("Ctrl+%1");
for (int i = 0; i < 9; ++i) {
int key = i + 1;
TabShortcut *tabShortCut = new TabShortcut(i, alt.arg(key), this);
connect(tabShortCut, SIGNAL(activated()), this, SLOT(selectTabAction()));
}
m_viewTabBarAction = new QAction(this);
updateViewToolBarAction();
connect(m_viewTabBarAction, SIGNAL(triggered()),
this, SLOT(viewTabBar()));
setMovable(true);
}
bool TabBar::showTabBarWhenOneTab() const
{
return m_showTabBarWhenOneTab;
}
void TabBar::setShowTabBarWhenOneTab(bool enabled)
{
m_showTabBarWhenOneTab = enabled;
updateVisibility();
}
QAction *TabBar::viewTabBarAction() const
{
return m_viewTabBarAction;
}
QTabBar::ButtonPosition TabBar::freeSide()
{
QTabBar::ButtonPosition side = (QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this);
side = (side == QTabBar::LeftSide) ? QTabBar::RightSide : QTabBar::LeftSide;
return side;
}
void TabBar::updateViewToolBarAction()
{
bool show = showTabBarWhenOneTab();
if (count() > 1)
show = true;
m_viewTabBarAction->setText(!show ? tr("Show Tab Bar") : tr("Hide Tab Bar"));
}
void TabBar::viewTabBar()
{
setShowTabBarWhenOneTab(!showTabBarWhenOneTab());
updateViewToolBarAction();
}
void TabBar::selectTabAction()
{
int index = qobject_cast<TabShortcut*>(sender())->tab();
setCurrentIndex(index);
}
void TabBar::contextMenuRequested(const QPoint &position)
{
QMenu menu;
TabWidget *tabWidget = qobject_cast<TabWidget*>(parentWidget());
if (!tabWidget)
return;
menu.addAction(tabWidget->newTabAction());
int index = tabAt(position);
if (-1 != index) {
QAction *action = menu.addAction(tr("Duplicate Tab"),
this, SLOT(cloneTab()));
action->setData(index);
menu.addSeparator();
action = menu.addAction(tr("&Close Tab"),
this, SLOT(closeTab()), QKeySequence::Close);
action->setData(index);
action = menu.addAction(tr("Close &Other Tabs"),
this, SLOT(closeOtherTabs()));
action->setData(index);
menu.addSeparator();
action = menu.addAction(tr("Reload Tab"),
this, SLOT(reloadTab()), QKeySequence::Refresh);
action->setData(index);
} else {
menu.addSeparator();
}
menu.addAction(tr("Reload All Tabs"), this, SIGNAL(reloadAllTabs()));
menu.addSeparator();
menu.addAction(tabWidget->bookmarkTabsAction());
menu.exec(QCursor::pos());
}
void TabBar::cloneTab()
{
if (QAction *action = qobject_cast<QAction*>(sender())) {
int index = action->data().toInt();
emit cloneTab(index);
}
}
void TabBar::closeTab()
{
if (QAction *action = qobject_cast<QAction*>(sender())) {
int index = action->data().toInt();
emit closeTab(index);
}
}
void TabBar::closeOtherTabs()
{
if (QAction *action = qobject_cast<QAction*>(sender())) {
int index = action->data().toInt();
emit closeOtherTabs(index);
}
}
void TabBar::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton
&& tabAt(event->pos()) == -1) {
emit newTab();
return;
}
QTabBar::mouseDoubleClickEvent(event);
}
void TabBar::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton) {
int index = tabAt(event->pos());
if (index != -1) {
emit closeTab(index);
return;
#ifndef QT_NO_CLIPBOARD
} else {
QUrl url(QApplication::clipboard()->text(QClipboard::Selection));
if (!url.isEmpty() && url.isValid() && !url.scheme().isEmpty())
emit loadUrl(url, TabWidget::NewTab);
#endif
}
}
QTabBar::mouseReleaseEvent(event);
}
void TabBar::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
m_dragStartPos = event->pos();
QTabBar::mousePressEvent(event);
}
void TabBar::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton) {
int diffX = event->pos().x() - m_dragStartPos.x();
int diffY = event->pos().y() - m_dragStartPos.y();
if ((event->pos() - m_dragStartPos).manhattanLength() > QApplication::startDragDistance()
&& diffX < 3 && diffX > -3
&& diffY < -10) {
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QList<QUrl> urls;
int index = tabAt(event->pos());
QUrl url = tabData(index).toUrl();
urls.append(url);
mimeData->setUrls(urls);
mimeData->setText(tabText(index));
mimeData->setData(QLatin1String("action"), "tab-reordering");
drag->setMimeData(mimeData);
drag->exec();
}
}
QTabBar::mouseMoveEvent(event);
}
void TabBar::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData *mimeData = event->mimeData();
if (mimeData->hasUrls() || mimeData->hasText())
event->acceptProposedAction();
QTabBar::dragEnterEvent(event);
}
void TabBar::dropEvent(QDropEvent *event)
{
const QMimeData *mimeData = event->mimeData();
QUrl url;
if (mimeData->hasUrls())
url = mimeData->urls().at(0);
else if (mimeData->hasText())
url = QUrl::fromEncoded(mimeData->text().toUtf8());
if (!url.isEmpty() && url.isValid()) {
event->acceptProposedAction();
int index = tabAt(event->pos());
if (-1 != index) {
setCurrentIndex(index);
emit loadUrl(url, TabWidget::CurrentTab);
} else {
emit loadUrl(url, TabWidget::NewSelectedTab);
}
}
QTabBar::dropEvent(event);
}
QSize TabBar::tabSizeHint(int index) const
{
QSize sizeHint = QTabBar::tabSizeHint(index);
QFontMetrics fm = fontMetrics();
return sizeHint.boundedTo(QSize(fm.horizontalAdvance(QLatin1Char('M')) * 18, sizeHint.height()));
}
void TabBar::reloadTab()
{
if (QAction *action = qobject_cast<QAction*>(sender())) {
int index = action->data().toInt();
emit reloadTab(index);
}
}
void TabBar::tabInserted(int position)
{
Q_UNUSED(position);
updateVisibility();
}
void TabBar::tabRemoved(int position)
{
Q_UNUSED(position);
updateVisibility();
}
void TabBar::updateVisibility()
{
setVisible((count()) > 1 || m_showTabBarWhenOneTab);
bool enabled = (count() == 1);
if (m_viewTabBarAction->isEnabled() != enabled)
m_viewTabBarAction->setEnabled(enabled);
updateViewToolBarAction();
}