File indexing completed on 2024-04-14 14:23:19

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2008 Bernhard Beschow <bbeschow AT cs DOT tu-berlin DOT de>
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 #include "khtmlviewbar.h"
0021 
0022 #include "khtmlview.h"
0023 #include "khtmlviewbarwidget.h"
0024 
0025 #include "khtml_debug.h"
0026 
0027 #include <QBoxLayout>
0028 #include <QKeyEvent>
0029 
0030 KHTMLViewBar::KHTMLViewBar(Position position, KHTMLView *view, QWidget *parent) :
0031     QWidget(parent),
0032     m_view(view),
0033     m_permanentBarWidget(nullptr)
0034 {
0035     const QBoxLayout::Direction direction = (position == Top ? QBoxLayout::TopToBottom : QBoxLayout::BottomToTop);
0036 
0037     setLayout(new QBoxLayout(direction, this));
0038     layout()->setContentsMargins(0, 0, 0, 0);
0039     layout()->setSpacing(0);
0040 }
0041 
0042 void KHTMLViewBar::addBarWidget(KHTMLViewBarWidget *newBarWidget)
0043 {
0044     if (hasWidget(newBarWidget)) {
0045         // qCDebug(KHTML_LOG) << "this bar widget is already added";
0046         return;
0047     }
0048     // add new widget, invisible...
0049     newBarWidget->hide();
0050     layout()->addWidget(newBarWidget);
0051     connect(newBarWidget, SIGNAL(hideMe()), SLOT(hideCurrentBarWidget()));
0052 
0053     // qCDebug(KHTML_LOG) << "add barwidget " << newBarWidget;
0054 }
0055 
0056 void KHTMLViewBar::addPermanentBarWidget(KHTMLViewBarWidget *barWidget)
0057 {
0058     // remove old widget from layout (if any)
0059     if (m_permanentBarWidget) {
0060         m_permanentBarWidget->hide();
0061         layout()->removeWidget(m_permanentBarWidget);
0062     }
0063 
0064     layout()->addWidget(barWidget /*, 0, Qt::AlignBottom*/);  // FIXME
0065     m_permanentBarWidget = barWidget;
0066     m_permanentBarWidget->show();
0067 
0068     setViewBarVisible(true);
0069 }
0070 
0071 void KHTMLViewBar::removePermanentBarWidget(KHTMLViewBarWidget *barWidget)
0072 {
0073     if (m_permanentBarWidget != barWidget) {
0074         // qCDebug(KHTML_LOG) << "no such permanent widget exists in bar";
0075         return;
0076     }
0077 
0078     if (!m_permanentBarWidget) {
0079         return;
0080     }
0081 
0082     m_permanentBarWidget->hide();
0083     layout()->removeWidget(m_permanentBarWidget);
0084     m_permanentBarWidget = nullptr;
0085 }
0086 
0087 bool KHTMLViewBar::hasPermanentWidget(KHTMLViewBarWidget *barWidget) const
0088 {
0089     return (m_permanentBarWidget == barWidget);
0090 }
0091 
0092 void KHTMLViewBar::showBarWidget(KHTMLViewBarWidget *barWidget)
0093 {
0094     // raise correct widget
0095 // TODO  m_stack->setCurrentWidget (barWidget);
0096     barWidget->show();
0097 
0098     // if we have any permanent widget, bar is always visible,
0099     // no need to show it
0100     if (!m_permanentBarWidget) {
0101         setViewBarVisible(true);
0102     }
0103 }
0104 
0105 bool KHTMLViewBar::hasWidget(KHTMLViewBarWidget *wid) const
0106 {
0107     Q_UNUSED(wid);
0108     return layout()->count() != 0;
0109 }
0110 
0111 void KHTMLViewBar::hideCurrentBarWidget()
0112 {
0113 //  m_stack->hide();
0114 
0115     // if we have any permanent widget, bar is always visible,
0116     // no need to hide it
0117     if (!m_permanentBarWidget) {
0118         setViewBarVisible(false);
0119     }
0120 
0121     m_view->setFocus();
0122     // qCDebug(KHTML_LOG)<<"hide barwidget";
0123 }
0124 
0125 void KHTMLViewBar::setViewBarVisible(bool visible)
0126 {
0127     setVisible(visible);
0128 }
0129 
0130 void KHTMLViewBar::keyPressEvent(QKeyEvent *event)
0131 {
0132     if (event->key() == Qt::Key_Escape) {
0133         hideCurrentBarWidget();
0134         return;
0135     }
0136     QWidget::keyPressEvent(event);
0137 
0138 }
0139 
0140 void KHTMLViewBar::hideEvent(QHideEvent *event)
0141 {
0142     Q_UNUSED(event);
0143 //   if (!event->spontaneous())
0144 //     m_view->setFocus();
0145 }
0146 
0147 #include "moc_khtmlviewbar.cpp"