File indexing completed on 2024-05-19 12:54:54

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
0003    Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KexiObjectViewTabWidget.h"
0022 #include <KexiIcon.h>
0023 #include <KexiTester.h>
0024 #include <KexiWindow.h>
0025 #include <KexiMainWindowIface.h>
0026 
0027 #include <KLocalizedString>
0028 #include <KActionCollection>
0029 
0030 #include <QAction>
0031 #include <QMenu>
0032 #include <QMouseEvent>
0033 #include <QTabBar>
0034 #include <QVBoxLayout>
0035 
0036 //! @internal window container created to speedup opening new tabs
0037 class KexiWindowContainer : public QWidget
0038 {
0039     Q_OBJECT
0040 public:
0041     explicit KexiWindowContainer(QWidget* parent);
0042 
0043     virtual ~KexiWindowContainer();
0044 
0045     void setWindow(KexiWindow* w);
0046 
0047     KexiWindow* window() { return m_window; }
0048 
0049 private:
0050     QPointer<KexiWindow> m_window;
0051     QVBoxLayout *lyr;
0052 };
0053 
0054 KexiWindowContainer::KexiWindowContainer(QWidget* parent)
0055     : QWidget(parent)
0056     , lyr(new QVBoxLayout(this))
0057 {
0058     lyr->setContentsMargins(0, 0, 0, 0);
0059 }
0060 
0061 KexiWindowContainer::~KexiWindowContainer()
0062 {
0063     //! @todo warning if saveSettings() failed?
0064     if (m_window) {
0065         m_window->saveSettings();
0066         delete (KexiWindow*)m_window;
0067     }
0068 }
0069 
0070 void KexiWindowContainer::setWindow(KexiWindow* w)
0071 {
0072     m_window = w;
0073     if (w) {
0074         lyr->addWidget(w);
0075     }
0076 }
0077 
0078 //-------------------------------------------------
0079 
0080 KexiObjectViewTabWidget::KexiObjectViewTabWidget(QWidget *parent, KexiObjectViewWidget* mainWidget)
0081         : QTabWidget(parent)
0082         , m_mainWidget(mainWidget)
0083         , m_tabIndex(-1)
0084 {
0085 //! @todo  insert window list in the corner widget as in firefox
0086 #if 0
0087     // close-tab button:
0088     QToolButton* rightWidget = new QToolButton(this);
0089     rightWidget->setDefaultAction(m_closeAction);
0090     rightWidget->setText(QString());
0091     rightWidget->setAutoRaise(true);
0092     rightWidget->adjustSize();
0093     setCornerWidget(rightWidget, Qt::TopRightCorner);
0094 #endif
0095     setMovable(true);
0096     setDocumentMode(true);
0097     tabBar()->setExpanding(false);
0098 }
0099 
0100 KexiObjectViewTabWidget::~KexiObjectViewTabWidget()
0101 {
0102 }
0103 
0104 void KexiObjectViewTabWidget::paintEvent(QPaintEvent * event)
0105 {
0106     if (count() > 0)
0107         QTabWidget::paintEvent(event);
0108     else
0109         QWidget::paintEvent(event);
0110 }
0111 
0112 void KexiObjectViewTabWidget::mousePressEvent(QMouseEvent *event)
0113 {
0114     //! @todo KEXI3 test KexiMainWindowTabWidget's contextMenu event port from KTabWidget
0115     if (event->button() == Qt::RightButton) {
0116         int tab = tabBar()->tabAt(event->pos());
0117         const QPoint realPos(tabBar()->mapToGlobal(event->pos()));
0118         if (QRect(tabBar()->mapToGlobal(QPoint(0,0)),
0119               tabBar()->mapToGlobal(QPoint(tabBar()->width()-1, tabBar()->height()-1))).contains(realPos))
0120         {
0121             showContextMenuForTab(tab, tabBar()->mapToGlobal(event->pos()));
0122             return;
0123         }
0124     }
0125     QTabWidget::mousePressEvent(event);
0126 }
0127 
0128 KexiWindow* KexiObjectViewTabWidget::window(int index)
0129 {
0130     KexiWindowContainer *windowContainer = qobject_cast<KexiWindowContainer*>(widget(index));
0131     return windowContainer ? windowContainer->window() : 0;
0132 }
0133 
0134 void KexiObjectViewTabWidget::closeCurrentTab()
0135 {
0136     emit tabCloseRequested(m_tabIndex);
0137 }
0138 
0139 void KexiObjectViewTabWidget::closeAllTabs()
0140 {
0141     QAction *action_close_all_tabs = KexiMainWindowIface::global()->actionCollection()->action("close_all_tabs");
0142     action_close_all_tabs->trigger();
0143 }
0144 
0145 int KexiObjectViewTabWidget::addEmptyContainerTab(const QIcon &icon, const QString &label)
0146 {
0147     KexiWindowContainer *windowContainer = new KexiWindowContainer(this);
0148     return addTab(windowContainer, icon, label);
0149 }
0150 
0151 void KexiObjectViewTabWidget::setWindowForTab(int index, KexiWindow *window)
0152 {
0153     KexiWindowContainer *windowContainer = qobject_cast<KexiWindowContainer*>(widget(index));
0154     if (windowContainer) {
0155         if (windowContainer->window()) {
0156             qWarning() << "tab" << index << "already has KexiWindow assigned";
0157             return;
0158         }
0159         windowContainer->setWindow(window);
0160     }
0161 }
0162 
0163 void KexiObjectViewTabWidget::showContextMenuForTab(int index, const QPoint& point)
0164 {
0165     QMenu menu;
0166     if (index >= 0) {
0167         QAction *action_close_tab = KexiMainWindowIface::global()->actionCollection()->action("close_tab");
0168         // Copy the action; we can't use the original action because instead of calling closeCurrentTab()
0169         // tabCloseRequested() signal should be called.
0170         QAction *closeAction = new QAction(action_close_tab->icon(), action_close_tab->text(), &menu);
0171         closeAction->setToolTip(action_close_tab->toolTip());
0172         closeAction->setWhatsThis(action_close_tab->whatsThis());
0173         closeAction->setShortcut(action_close_tab->shortcut());
0174         connect(closeAction, &QAction::triggered, this, &KexiObjectViewTabWidget::tabCloseRequested);
0175         menu.addAction(closeAction);
0176     }
0177     if (count() > 0) {
0178         QAction *action_close_all_tabs = KexiMainWindowIface::global()->actionCollection()->action("close_all_tabs");
0179         menu.addAction(action_close_all_tabs);
0180     }
0181     //! @todo add "&Detach Tab"
0182     if (menu.actions().isEmpty()) {
0183         return;
0184     }
0185     setTabIndexFromContextMenu(index);
0186     menu.exec(point);
0187 }
0188 
0189 void KexiObjectViewTabWidget::setTabIndexFromContextMenu(int clickedIndex)
0190 {
0191     if (currentIndex() == -1) {
0192         m_tabIndex = -1;
0193         return;
0194     }
0195     m_tabIndex = clickedIndex;
0196 }
0197 
0198 #include "KexiObjectViewTabWidget.moc"