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 "KexiObjectViewWidget.h"
0022 #include "KexiObjectViewTabWidget.h"
0023 #include <KexiPropertyPaneWidget.h>
0024 #include <KexiWidgetWidthAnimator.h>
0025 #include <KexiProjectNavigator.h>
0026 #include <KexiTester.h>
0027 #include <KexiStyle.h>
0028 #include <KexiWindow.h>
0029 #include <KexiMainWindowIface.h>
0030 
0031 #include <KPropertyEditorView>
0032 #include <KActionCollection>
0033 
0034 #include <QHBoxLayout>
0035 #include <QResizeEvent>
0036 #include <QSplitter>
0037 
0038 
0039 class KexiObjectViewWidget::Private
0040 {
0041 public:
0042     Private()
0043      : navigator(0)
0044      , navigatorWidthAnimator(0)
0045      , propertyPane(0)
0046      , propertyPaneWidthAnimator(0)
0047      , projectNavigatorWidthToSet(-1)
0048      , propertyPaneWidthToSet(-1)
0049     {
0050     }
0051     KexiProjectNavigator* navigator;
0052     KexiWidgetWidthAnimator* navigatorWidthAnimator;
0053     KexiObjectViewTabWidget* tabWidget;
0054     QPointer<KexiWindow> previouslyActiveWindow;
0055     KexiPropertyPaneWidget *propertyPane;
0056     KexiWidgetWidthAnimator* propertyPaneWidthAnimator;
0057     //QMap<KMultiTabBar::KMultiTabBarPosition, KMultiTabBar*> multiTabBars;
0058     QSplitter *splitter;
0059     int projectNavigatorWidthToSet;
0060     int propertyPaneWidthToSet;
0061 };
0062 
0063 
0064 KexiObjectViewWidget::KexiObjectViewWidget(Flags flags)
0065         : QWidget()
0066         , d(new Private)
0067 {
0068     QHBoxLayout *mainLyr = new QHBoxLayout;
0069     mainLyr->setContentsMargins(0, 0, 0, 0);
0070     mainLyr->setSpacing(0);
0071     setLayout(mainLyr);
0072 
0073     d->splitter = new QSplitter(Qt::Horizontal);
0074     connect(d->splitter, &QSplitter::splitterMoved,
0075             this, &KexiObjectViewWidget::slotSplitterMoved);
0076     mainLyr->addWidget(d->splitter);
0077 
0078     // Left tab bar
0079 //    KMultiTabBar *mtbar = new KMultiTabBar(KMultiTabBar::Left);
0080 //    mtbar->setStyle(KMultiTabBar::VSNET);
0081 //    mainLyr->addWidget(mtbar);
0082 //    d->multiTabBars.insert(mtbar->position(), mtbar);
0083 
0084     if (flags & ProjectNavigatorEnabled) {
0085         // Project navigator
0086         //    KexiDockableWidget* navDockableWidget = new KexiDockableWidget;
0087         d->navigator = new KexiProjectNavigator(d->splitter);
0088         kexiTester() << KexiTestObject(d->navigator, "KexiProjectNavigator");
0089         //navDockableWidget->setWidget(d->navigator);
0090         KexiStyle::setSidebarsPalette(d->navigator);
0091         d->navigatorWidthAnimator = new KexiWidgetWidthAnimator(d->navigator);
0092         connect(d->navigatorWidthAnimator, &KexiWidgetWidthAnimator::animationFinished,
0093                 this, &KexiObjectViewWidget::projectNavigatorAnimationFinished);
0094     }
0095 
0096     //d->navDockWidget = new KexiDockWidget(d->navigator->windowTitle(), d->objectViewWidget);
0097     //d->navDockWidget->setObjectName("ProjectNavigatorDockWidget");
0098     //d->objectViewWidget->addDockWidget(
0099     //    applyRightToLeftToDockArea(Qt::LeftDockWidgetArea), d->navDockWidget,
0100     //    Qt::Vertical);
0101     //navDockableWidget->setParent(d->navDockWidget);
0102     //d->navDockWidget->setWidget(navDockableWidget);
0103 
0104     // Central tab widget
0105     d->tabWidget = new KexiObjectViewTabWidget(d->splitter, this);
0106     d->tabWidget->setTabsClosable(true);
0107     connect(d->tabWidget, &KexiObjectViewTabWidget::currentChanged,
0108             this, &KexiObjectViewWidget::slotCurrentTabIndexChanged);
0109     connect(d->tabWidget, &KexiObjectViewTabWidget::tabCloseRequested,
0110             this, &KexiObjectViewWidget::closeWindowRequested);
0111 
0112     if (flags & PropertyPaneEnabled) {
0113         // Property editor
0114         d->propertyPane = new KexiPropertyPaneWidget(d->splitter);
0115         KexiStyle::setSidebarsPalette(d->propertyPane);
0116         KexiStyle::setSidebarsPalette(d->propertyPane->editor());
0117 
0118         d->propertyPaneWidthAnimator = new KexiWidgetWidthAnimator(d->propertyPane);
0119     }
0120 
0121 //    mtbar = new KMultiTabBar(KMultiTabBar::Right);
0122 //    mtbar->setStyle(KMultiTabBar::VSNET);
0123 //    mainLyr->addWidget(mtbar);
0124 //    d->multiTabBars.insert(mtbar->position(), mtbar);
0125 }
0126 
0127 KexiObjectViewWidget::~KexiObjectViewWidget()
0128 {
0129 }
0130 
0131 KexiProjectNavigator* KexiObjectViewWidget::projectNavigator() const
0132 {
0133     return d->navigator;
0134 }
0135 
0136 KexiObjectViewTabWidget* KexiObjectViewWidget::tabWidget() const
0137 {
0138     return d->tabWidget;
0139 }
0140 
0141 KexiPropertyPaneWidget* KexiObjectViewWidget::propertyPane() const
0142 {
0143     return d->propertyPane;
0144 }
0145 
0146 void KexiObjectViewWidget::slotCurrentTabIndexChanged(int index)
0147 {
0148     KexiWindow *window = d->tabWidget->window(index);
0149     if (!window || (KexiWindow*)d->previouslyActiveWindow == window) {
0150         return;
0151     }
0152     emit activeWindowChanged(window, d->previouslyActiveWindow);
0153     d->previouslyActiveWindow = window;
0154     emit currentTabIndexChanged(index);
0155 }
0156 
0157 void KexiObjectViewWidget::setSidebarWidths(int projectNavigatorWidth, int propertyEditorWidth)
0158 {
0159     d->projectNavigatorWidthToSet = projectNavigatorWidth;
0160     d->propertyPaneWidthToSet = propertyEditorWidth;
0161 }
0162 
0163 void KexiObjectViewWidget::resizeEvent(QResizeEvent *e)
0164 {
0165     QWidget::resizeEvent(e);
0166     //qDebug() << "___" << e->size() << size() << isVisible();
0167     if (isVisible()) {
0168         updateSidebarWidths();
0169     }
0170 }
0171 
0172 void KexiObjectViewWidget::showEvent(QShowEvent *e)
0173 {
0174     QWidget::showEvent(e);
0175     updateSidebarWidths();
0176 }
0177 
0178 const int PROJECT_NAVIGATOR_INDEX = 0;
0179 const int MAIN_AREA_INDEX = 1;
0180 const int PROPERTY_EDITOR_INDEX = 2;
0181 
0182 void KexiObjectViewWidget::updateSidebarWidths()
0183 {
0184     QList<int> sizes(d->splitter->sizes());
0185     if (sizes.count() <= 1) {
0186         return;
0187     }
0188     //qDebug() << "updateSidebarWidths" << d->projectNavigatorWidthToSet << d->propertyEditorWidthToSet << sizes << d->splitter->width() << isVisible();
0189     if (d->projectNavigatorWidthToSet <= 0) {
0190         if (d->navigator) {
0191             sizes[PROJECT_NAVIGATOR_INDEX] = d->navigator->sizeHint().width();
0192         }
0193     } else {
0194         sizes[PROJECT_NAVIGATOR_INDEX] = d->projectNavigatorWidthToSet;
0195     }
0196     if (sizes.count() >= (PROPERTY_EDITOR_INDEX+1)) {
0197         if (d->propertyPane && d->propertyPane->isVisible()) {
0198             if (d->propertyPaneWidthToSet <= 0) {
0199                 d->propertyPaneWidthToSet = d->propertyPane->sizeHint().width();
0200             }
0201             sizes[PROPERTY_EDITOR_INDEX] = d->propertyPaneWidthToSet;
0202             sizes[MAIN_AREA_INDEX] = d->splitter->width() - sizes[PROJECT_NAVIGATOR_INDEX] - sizes[PROPERTY_EDITOR_INDEX];
0203         } else {
0204             sizes[PROPERTY_EDITOR_INDEX] = 0;
0205             sizes[MAIN_AREA_INDEX] = d->splitter->width() - sizes[PROJECT_NAVIGATOR_INDEX];
0206         }
0207     }
0208     d->splitter->setSizes(sizes);
0209     //qDebug() << "updateSidebarWidths" << sizes << d->splitter->sizes();
0210 }
0211 
0212 void KexiObjectViewWidget::getSidebarWidths(int *projectNavigatorWidth, int *propertyEditorWidth) const
0213 {
0214     QList<int> sizes(d->splitter->sizes());
0215     if (sizes.count() < (PROPERTY_EDITOR_INDEX+1)) {
0216         *projectNavigatorWidth = -1;
0217         *propertyEditorWidth = -1;
0218         return;
0219     }
0220 
0221     //qDebug() << "getSidebarWidths" << d->propertyEditorTabWidget->width();
0222     *projectNavigatorWidth = (sizes.count() >= (PROJECT_NAVIGATOR_INDEX+1) && sizes[PROJECT_NAVIGATOR_INDEX] > 0)
0223             ? sizes[PROJECT_NAVIGATOR_INDEX] : d->projectNavigatorWidthToSet;
0224     *propertyEditorWidth = (sizes.count() >= (PROPERTY_EDITOR_INDEX+1) && sizes[PROPERTY_EDITOR_INDEX] > 0)
0225             ? sizes[PROPERTY_EDITOR_INDEX] : d->propertyPaneWidthToSet;
0226     //qDebug() << "getSidebarWidths" << *projectNavigatorWidth << *propertyEditorWidth;
0227 }
0228 
0229 void KexiObjectViewWidget::slotSplitterMoved(int pos, int index)
0230 {
0231     Q_UNUSED(pos)
0232     //qDebug() << "slotSplitterMoved" << pos << index;
0233     QList<int> sizes(d->splitter->sizes());
0234     if (index == PROJECT_NAVIGATOR_INDEX + 1) {
0235         if (sizes.count() >= (PROJECT_NAVIGATOR_INDEX+1)) {
0236             d->projectNavigatorWidthToSet = sizes[PROJECT_NAVIGATOR_INDEX];
0237         }
0238     } else if (index == PROPERTY_EDITOR_INDEX) {
0239         if (sizes.count() >= (PROPERTY_EDITOR_INDEX+1)) {
0240             d->propertyPaneWidthToSet = sizes[PROPERTY_EDITOR_INDEX];
0241         }
0242     }
0243 }
0244 
0245 void KexiObjectViewWidget::setProjectNavigatorVisible(bool set)
0246 {
0247     QAction *action_show_nav = KexiMainWindowIface::global()->actionCollection()->action("view_navigator");
0248     action_show_nav->setChecked(set);
0249     d->navigatorWidthAnimator->setVisible(set);
0250 }
0251 
0252 void KexiObjectViewWidget::setPropertyPaneVisible(bool set)
0253 {
0254     QAction *action_show_propeditor = KexiMainWindowIface::global()->actionCollection()->action("view_propeditor");
0255     action_show_propeditor->setChecked(set);
0256     d->propertyPaneWidthAnimator->setVisible(set);
0257 }