File indexing completed on 2025-02-09 07:11:34

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "viewareabox.hpp"
0010 
0011 // lib
0012 #include "toolinlineviewwidget.hpp"
0013 // Qt
0014 #include <QVBoxLayout>
0015 #include <QShortcut>
0016 
0017 namespace Kasten {
0018 
0019 ViewAreaBox::ViewAreaBox(QWidget* centralWidget, QWidget* parent)
0020     : QWidget(parent)
0021     , mCentralWidget(centralWidget)
0022 {
0023     setFocusProxy(mCentralWidget);
0024 
0025     auto* layout = new QVBoxLayout(this);
0026     layout->setContentsMargins(0, 0, 0, 0);
0027     layout->setSpacing(0);
0028     if (mCentralWidget) {
0029         layout->addWidget(mCentralWidget);
0030     }
0031 
0032     mEscapeShortcut = new QShortcut(Qt::Key_Escape, this);
0033     mEscapeShortcut->setContext(Qt::WidgetWithChildrenShortcut);
0034     mEscapeShortcut->setEnabled(false);
0035     connect(mEscapeShortcut, &QShortcut::activated, this, &ViewAreaBox::onDone);   // TODO: better use onCancelled
0036 }
0037 
0038 ViewAreaBox::~ViewAreaBox()
0039 {
0040     if (mCentralWidget) {
0041         mCentralWidget->setParent(nullptr);
0042     }
0043 }
0044 
0045 QWidget* ViewAreaBox::centralWidget() const { return mCentralWidget; }
0046 QWidget* ViewAreaBox::bottomToolWidget()  const { return mBottomToolWidget; }
0047 
0048 void ViewAreaBox::setCentralWidget(QWidget* centralWidget)
0049 {
0050     if (mCentralWidget == centralWidget) {
0051         return;
0052     }
0053 
0054     auto* layout = static_cast<QVBoxLayout*>(this->layout());
0055     const bool centralWidgetIsFocusProxy =
0056         mCentralWidget ? (focusProxy() == mCentralWidget) : false;
0057     // TODO: works if focus is on childwidget?
0058     const bool centralWidgetHasFocus =
0059         mCentralWidget ? mCentralWidget->hasFocus() : false;
0060     if (mCentralWidget) {
0061         layout->removeWidget(mCentralWidget);
0062     }
0063 
0064     mCentralWidget = centralWidget;
0065 
0066     if (mCentralWidget) {
0067         layout->insertWidget(0, mCentralWidget);
0068         mCentralWidget->show(); // TODO: needed?
0069         if (centralWidgetHasFocus) {
0070             mCentralWidget->setFocus();
0071         }
0072         if (centralWidgetIsFocusProxy) {
0073             setFocusProxy(mCentralWidget);
0074         }
0075     }
0076 }
0077 
0078 void ViewAreaBox::setBottomToolWidget(QWidget* bottomToolWidget)
0079 {
0080     if (mBottomToolWidget == bottomToolWidget) {
0081         return;
0082     }
0083 
0084     auto* layout = static_cast<QVBoxLayout*>(this->layout());
0085 
0086     if (mToolInlineViewWidget) {
0087         layout->removeWidget(mToolInlineViewWidget);
0088         delete mToolInlineViewWidget;
0089         mToolInlineViewWidget = nullptr;
0090         setFocusProxy(mCentralWidget);
0091     }
0092 
0093     mBottomToolWidget = bottomToolWidget;
0094 
0095     if (mBottomToolWidget) {
0096         mToolInlineViewWidget = new ToolInlineViewWidget(mBottomToolWidget);
0097 
0098         connect(mToolInlineViewWidget, &ToolInlineViewWidget::done, this, &ViewAreaBox::onDone);
0099         layout->addWidget(mToolInlineViewWidget);
0100     }
0101 }
0102 
0103 void ViewAreaBox::showBottomToolWidget()
0104 {
0105     if (!mToolInlineViewWidget) {
0106         return;
0107     }
0108 
0109     setFocusProxy(mToolInlineViewWidget);
0110     mToolInlineViewWidget->show();
0111     mBottomToolWidget->setFocus();
0112 
0113     mEscapeShortcut->setEnabled(true);
0114 }
0115 
0116 void ViewAreaBox::onDone()
0117 {
0118     if (!mToolInlineViewWidget) {
0119         return;
0120     }
0121 
0122     setFocusProxy(mCentralWidget);
0123     mToolInlineViewWidget->hide();
0124     mEscapeShortcut->setEnabled(false);
0125 }
0126 
0127 }
0128 
0129 #include "moc_viewareabox.cpp"