1/* This file is part of the KDE project
2 *
3 * Copyright (C) 2008 Bernhard Beschow <bbeschow AT cs DOT tu-berlin DOT de>
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#include "khtmlviewbar.h"
21
22#include "khtmlview.h"
23#include "khtmlviewbarwidget.h"
24
25#include <kdebug.h>
26
27#include <QtGui/QBoxLayout>
28#include <QtGui/QKeyEvent>
29
30KHTMLViewBar::KHTMLViewBar( Position position, KHTMLView *view, QWidget *parent ) :
31 QWidget( parent ),
32 m_view( view ),
33 m_permanentBarWidget( 0 )
34{
35 const QBoxLayout::Direction direction = ( position == Top ? QBoxLayout::TopToBottom : QBoxLayout::BottomToTop );
36
37 setLayout( new QBoxLayout( direction, this ) );
38 layout()->setContentsMargins( 0, 0, 0, 0 );
39 layout()->setSpacing( 0 );
40}
41
42void KHTMLViewBar::addBarWidget (KHTMLViewBarWidget *newBarWidget)
43{
44 if (hasWidget(newBarWidget)) {
45 kDebug(6050) << "this bar widget is already added";
46 return;
47 }
48 // add new widget, invisible...
49 newBarWidget->hide();
50 layout()->addWidget( newBarWidget );
51 connect(newBarWidget, SIGNAL(hideMe()), SLOT(hideCurrentBarWidget()));
52
53 kDebug(6050) << "add barwidget " << newBarWidget;
54}
55
56void KHTMLViewBar::addPermanentBarWidget (KHTMLViewBarWidget *barWidget)
57{
58 // remove old widget from layout (if any)
59 if (m_permanentBarWidget) {
60 m_permanentBarWidget->hide();
61 layout()->removeWidget(m_permanentBarWidget);
62 }
63
64 layout()->addWidget(barWidget /*, 0, Qt::AlignBottom*/ ); // FIXME
65 m_permanentBarWidget = barWidget;
66 m_permanentBarWidget->show();
67
68 setViewBarVisible(true);
69}
70
71void KHTMLViewBar::removePermanentBarWidget (KHTMLViewBarWidget *barWidget)
72{
73 if (m_permanentBarWidget != barWidget) {
74 kDebug(6050) << "no such permanent widget exists in bar";
75 return;
76 }
77
78 if (!m_permanentBarWidget)
79 return;
80
81 m_permanentBarWidget->hide();
82 layout()->removeWidget(m_permanentBarWidget);
83 m_permanentBarWidget = 0;
84}
85
86bool KHTMLViewBar::hasPermanentWidget (KHTMLViewBarWidget *barWidget ) const
87{
88 return (m_permanentBarWidget == barWidget);
89}
90
91void KHTMLViewBar::showBarWidget (KHTMLViewBarWidget *barWidget)
92{
93 // raise correct widget
94// TODO m_stack->setCurrentWidget (barWidget);
95 barWidget->show();
96
97 // if we have any permanent widget, bar is always visible,
98 // no need to show it
99 if (!m_permanentBarWidget) {
100 setViewBarVisible(true);
101 }
102}
103
104bool KHTMLViewBar::hasWidget(KHTMLViewBarWidget* wid) const
105{
106 Q_UNUSED(wid);
107 return layout()->count() != 0;
108}
109
110void KHTMLViewBar::hideCurrentBarWidget ()
111{
112// m_stack->hide();
113
114 // if we have any permanent widget, bar is always visible,
115 // no need to hide it
116 if (!m_permanentBarWidget) {
117 setViewBarVisible(false);
118 }
119
120 m_view->setFocus();
121 kDebug(6050)<<"hide barwidget";
122}
123
124void KHTMLViewBar::setViewBarVisible (bool visible)
125{
126 setVisible( visible );
127}
128
129void KHTMLViewBar::keyPressEvent(QKeyEvent* event)
130{
131 if (event->key() == Qt::Key_Escape) {
132 hideCurrentBarWidget();
133 return;
134 }
135 QWidget::keyPressEvent(event);
136
137}
138
139void KHTMLViewBar::hideEvent(QHideEvent* event)
140{
141 Q_UNUSED(event);
142// if (!event->spontaneous())
143// m_view->setFocus();
144}
145