Changeset View
Changeset View
Standalone View
Standalone View
src/main/KexiObjectViewTabWidget.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2003 Lucijan Busch <lucijan@kde.org> | ||||
3 | Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU Library General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This library 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 GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "KexiObjectViewTabWidget.h" | ||||
22 | #include <KexiIcon.h> | ||||
23 | #include <KexiTester.h> | ||||
24 | #include <KexiWindow.h> | ||||
25 | #include <KexiMainWindowIface.h> | ||||
26 | | ||||
27 | #include <KLocalizedString> | ||||
28 | #include <KActionCollection> | ||||
29 | | ||||
30 | #include <QAction> | ||||
31 | #include <QMenu> | ||||
32 | #include <QMouseEvent> | ||||
33 | #include <QTabBar> | ||||
34 | #include <QVBoxLayout> | ||||
35 | | ||||
36 | //! @internal window container created to speedup opening new tabs | ||||
37 | class KexiWindowContainer : public QWidget | ||||
38 | { | ||||
39 | Q_OBJECT | ||||
40 | public: | ||||
41 | explicit KexiWindowContainer(QWidget* parent); | ||||
42 | | ||||
43 | virtual ~KexiWindowContainer(); | ||||
44 | | ||||
45 | void setWindow(KexiWindow* w); | ||||
46 | | ||||
47 | KexiWindow* window() { return m_window; } | ||||
48 | | ||||
49 | private: | ||||
50 | QPointer<KexiWindow> m_window; | ||||
51 | QVBoxLayout *lyr; | ||||
52 | }; | ||||
53 | | ||||
54 | KexiWindowContainer::KexiWindowContainer(QWidget* parent) | ||||
55 | : QWidget(parent) | ||||
56 | , lyr(new QVBoxLayout(this)) | ||||
57 | { | ||||
58 | lyr->setContentsMargins(0, 0, 0, 0); | ||||
59 | } | ||||
60 | | ||||
61 | KexiWindowContainer::~KexiWindowContainer() | ||||
62 | { | ||||
63 | //! @todo warning if saveSettings() failed? | ||||
64 | if (m_window) { | ||||
65 | m_window->saveSettings(); | ||||
66 | delete (KexiWindow*)m_window; | ||||
67 | } | ||||
68 | } | ||||
69 | | ||||
70 | void KexiWindowContainer::setWindow(KexiWindow* w) | ||||
71 | { | ||||
72 | m_window = w; | ||||
73 | if (w) { | ||||
74 | lyr->addWidget(w); | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | //------------------------------------------------- | ||||
79 | | ||||
80 | KexiObjectViewTabWidget::KexiObjectViewTabWidget(QWidget *parent, KexiObjectViewWidget* mainWidget) | ||||
81 | : QTabWidget(parent) | ||||
82 | , m_mainWidget(mainWidget) | ||||
83 | , m_tabIndex(-1) | ||||
84 | { | ||||
85 | //! @todo insert window list in the corner widget as in firefox | ||||
86 | #if 0 | ||||
87 | // close-tab button: | ||||
88 | QToolButton* rightWidget = new QToolButton(this); | ||||
89 | rightWidget->setDefaultAction(m_closeAction); | ||||
90 | rightWidget->setText(QString()); | ||||
91 | rightWidget->setAutoRaise(true); | ||||
92 | rightWidget->adjustSize(); | ||||
93 | setCornerWidget(rightWidget, Qt::TopRightCorner); | ||||
94 | #endif | ||||
95 | setMovable(true); | ||||
96 | setDocumentMode(true); | ||||
97 | tabBar()->setExpanding(false); | ||||
98 | } | ||||
99 | | ||||
100 | KexiObjectViewTabWidget::~KexiObjectViewTabWidget() | ||||
101 | { | ||||
102 | } | ||||
103 | | ||||
104 | void KexiObjectViewTabWidget::paintEvent(QPaintEvent * event) | ||||
105 | { | ||||
106 | if (count() > 0) | ||||
107 | QTabWidget::paintEvent(event); | ||||
108 | else | ||||
109 | QWidget::paintEvent(event); | ||||
110 | } | ||||
111 | | ||||
112 | void KexiObjectViewTabWidget::mousePressEvent(QMouseEvent *event) | ||||
113 | { | ||||
114 | //! @todo KEXI3 test KexiMainWindowTabWidget's contextMenu event port from KTabWidget | ||||
115 | if (event->button() == Qt::RightButton) { | ||||
116 | int tab = tabBar()->tabAt(event->pos()); | ||||
117 | const QPoint realPos(tabBar()->mapToGlobal(event->pos())); | ||||
118 | if (QRect(tabBar()->mapToGlobal(QPoint(0,0)), | ||||
119 | tabBar()->mapToGlobal(QPoint(tabBar()->width()-1, tabBar()->height()-1))).contains(realPos)) | ||||
120 | { | ||||
121 | showContextMenuForTab(tab, tabBar()->mapToGlobal(event->pos())); | ||||
122 | return; | ||||
123 | } | ||||
124 | } | ||||
125 | QTabWidget::mousePressEvent(event); | ||||
126 | } | ||||
127 | | ||||
128 | KexiWindow* KexiObjectViewTabWidget::window(int index) | ||||
129 | { | ||||
130 | KexiWindowContainer *windowContainer = qobject_cast<KexiWindowContainer*>(widget(index)); | ||||
131 | return windowContainer ? windowContainer->window() : 0; | ||||
132 | } | ||||
133 | | ||||
134 | void KexiObjectViewTabWidget::closeCurrentTab() | ||||
135 | { | ||||
136 | emit tabCloseRequested(m_tabIndex); | ||||
137 | } | ||||
138 | | ||||
139 | void KexiObjectViewTabWidget::closeAllTabs() | ||||
140 | { | ||||
141 | QAction *action_close_all_tabs = KexiMainWindowIface::global()->actionCollection()->action("close_all_tabs"); | ||||
142 | action_close_all_tabs->trigger(); | ||||
143 | } | ||||
144 | | ||||
145 | int KexiObjectViewTabWidget::addEmptyContainerTab(const QIcon &icon, const QString &label) | ||||
146 | { | ||||
147 | KexiWindowContainer *windowContainer = new KexiWindowContainer(this); | ||||
148 | return addTab(windowContainer, icon, label); | ||||
149 | } | ||||
150 | | ||||
151 | void KexiObjectViewTabWidget::setWindowForTab(int index, KexiWindow *window) | ||||
152 | { | ||||
153 | KexiWindowContainer *windowContainer = qobject_cast<KexiWindowContainer*>(widget(index)); | ||||
154 | if (windowContainer) { | ||||
155 | if (windowContainer->window()) { | ||||
156 | qWarning() << "tab" << index << "already has KexiWindow assigned"; | ||||
157 | return; | ||||
158 | } | ||||
159 | windowContainer->setWindow(window); | ||||
160 | } | ||||
161 | } | ||||
162 | | ||||
163 | void KexiObjectViewTabWidget::showContextMenuForTab(int index, const QPoint& point) | ||||
164 | { | ||||
165 | QMenu menu; | ||||
166 | if (index >= 0) { | ||||
167 | QAction *action_close_tab = KexiMainWindowIface::global()->actionCollection()->action("close_tab"); | ||||
168 | // Copy the action; we can't use the original action because instead of calling closeCurrentTab() | ||||
169 | // tabCloseRequested() signal should be called. | ||||
170 | QAction *closeAction = new QAction(action_close_tab->icon(), action_close_tab->text(), &menu); | ||||
171 | closeAction->setToolTip(action_close_tab->toolTip()); | ||||
172 | closeAction->setWhatsThis(action_close_tab->whatsThis()); | ||||
173 | closeAction->setShortcut(action_close_tab->shortcut()); | ||||
174 | connect(closeAction, &QAction::triggered, this, &KexiObjectViewTabWidget::tabCloseRequested); | ||||
175 | menu.addAction(closeAction); | ||||
176 | } | ||||
177 | if (count() > 0) { | ||||
178 | QAction *action_close_all_tabs = KexiMainWindowIface::global()->actionCollection()->action("close_all_tabs"); | ||||
179 | menu.addAction(action_close_all_tabs); | ||||
180 | } | ||||
181 | //! @todo add "&Detach Tab" | ||||
182 | if (menu.actions().isEmpty()) { | ||||
183 | return; | ||||
184 | } | ||||
185 | setTabIndexFromContextMenu(index); | ||||
186 | menu.exec(point); | ||||
187 | } | ||||
188 | | ||||
189 | void KexiObjectViewTabWidget::setTabIndexFromContextMenu(int clickedIndex) | ||||
190 | { | ||||
191 | if (currentIndex() == -1) { | ||||
192 | m_tabIndex = -1; | ||||
193 | return; | ||||
194 | } | ||||
195 | m_tabIndex = clickedIndex; | ||||
196 | } | ||||
197 | | ||||
198 | #include "KexiObjectViewTabWidget.moc" |