~ubuntu-branches/ubuntu/lucid/kchmviewer/lucid

« back to all changes in this revision

Viewing changes to src/kchmviewwindowmgr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Tallon
  • Date: 2006-06-08 20:08:39 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20060608200839-8b7jodug8yvtj126
Tags: upstream-2.5
ImportĀ upstreamĀ versionĀ 2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2004-2005 by Georgy Yunaev, gyunaev@ulduzsoft.com       *
 
3
 *   Please do not use email address above for bug reports; see            *
 
4
 *   the README file                                                       *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, write to the                         *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
20
 ***************************************************************************/
 
21
 
 
22
#include "kchmconfig.h"
 
23
#include "kchmmainwindow.h"
 
24
#include "kchmviewwindow.h"
 
25
#include "kchmviewwindowmgr.h"
 
26
#include "iconstorage.h"
 
27
 
 
28
#include "kchmviewwindow_qtextbrowser.h"
 
29
 
 
30
#if defined (USE_KDE)
 
31
        #include "kchmviewwindow_khtmlpart.h"
 
32
#endif
 
33
 
 
34
 
 
35
KCHMViewWindowMgr::KCHMViewWindowMgr( QWidget *parent )
 
36
        : QTabWidget( parent ) //QTabWidget
 
37
{
 
38
        m_MenuWindow = 0;
 
39
        
 
40
        // on current tab changed
 
41
        connect( this, SIGNAL( currentChanged(QWidget *) ), this, SLOT( onTabChanged(QWidget *) ) );
 
42
        
 
43
        // Create an iconset for the button
 
44
        QIconSet iset( *gIconStorage.getCloseWindowIcon() );
 
45
        
 
46
        // Create a pushbutton
 
47
        m_closeButton = new QPushButton( iset, QString::null, this );
 
48
        m_closeButton->setFlat( true );
 
49
        m_closeButton->setEnabled( false );
 
50
        connect( m_closeButton, SIGNAL( clicked() ), this, SLOT( closeCurrentWindow() ) );
 
51
        
 
52
        setCornerWidget( m_closeButton );
 
53
}
 
54
 
 
55
KCHMViewWindowMgr::~KCHMViewWindowMgr( )
 
56
{
 
57
}
 
58
        
 
59
void KCHMViewWindowMgr::createMenu( KCHMMainWindow * parent )
 
60
{
 
61
        // Create the approptiate menu entries in 'View' main menu
 
62
        m_MenuWindow = new KQPopupMenu( parent );
 
63
        parent->menuBar()->insertItem( i18n( "&Window"), m_MenuWindow );
 
64
 
 
65
        m_menuIdClose = m_MenuWindow->insertItem( i18n( "&Close"), this, SLOT( closeCurrentWindow()), CTRL+Key_W );
 
66
        m_MenuWindow->insertSeparator();
 
67
 
 
68
        connect( m_MenuWindow, SIGNAL( activated(int) ), this, SLOT ( onCloseWindow(int) ));
 
69
}
 
70
 
 
71
void KCHMViewWindowMgr::invalidate()
 
72
{
 
73
        deleteAllWindows();
 
74
        addNewTab( true );
 
75
}
 
76
 
 
77
 
 
78
KCHMViewWindow * KCHMViewWindowMgr::current()
 
79
{
 
80
        QWidget * w = currentPage();
 
81
        WindowsIterator it;
 
82
                        
 
83
        if ( !w || (it = m_Windows.find( w )) == m_Windows.end() )
 
84
                qFatal( "KCHMViewWindowMgr::current called without any windows!" );
 
85
        
 
86
        return it.data().window;
 
87
}
 
88
 
 
89
KCHMViewWindow * KCHMViewWindowMgr::addNewTab( bool set_active )
 
90
{
 
91
        KCHMViewWindow * viewvnd;
 
92
        
 
93
#if defined (USE_KDE)
 
94
        if ( !appConfig.m_kdeUseQTextBrowser )
 
95
                viewvnd = new KCHMViewWindow_KHTMLPart( this );
 
96
        else
 
97
#endif
 
98
                viewvnd = new KCHMViewWindow_QTextBrowser( this );
 
99
 
 
100
        QWidget * widget = viewvnd->getQWidget();
 
101
        m_Windows[widget].window = viewvnd;
 
102
        m_Windows[widget].menuitem = 0;
 
103
        m_Windows[widget].widget = widget;
 
104
        
 
105
        addTab( widget, "" );
 
106
 
 
107
        Q_ASSERT( m_Windows.size() == (unsigned int) count() );
 
108
                
 
109
        // Set active if it is the first tab
 
110
        if ( set_active || m_Windows.size() == 1 )
 
111
                showPage( widget );
 
112
        
 
113
        // Handle clicking on link in browser window
 
114
        connect( viewvnd->getQObject(), SIGNAL( signalLinkClicked (const QString &, bool &) ), ::mainWindow, SLOT( slotLinkClicked(const QString &, bool &) ) );
 
115
        
 
116
        return viewvnd;
 
117
}
 
118
 
 
119
void KCHMViewWindowMgr::deleteAllWindows( )
 
120
{
 
121
        // No it++ - we removing the window by every closeWindow call
 
122
        while ( m_Windows.begin() != m_Windows.end() )
 
123
                closeWindow( m_Windows.begin().data() );
 
124
}
 
125
 
 
126
void KCHMViewWindowMgr::setTabName( KCHMViewWindow * window )
 
127
{
 
128
        WindowsIterator it = m_Windows.find( window->getQWidget() );
 
129
                        
 
130
        if ( it == m_Windows.end() )
 
131
                qFatal( "KCHMViewWindowMgr::setTabName called with unknown window!" );
 
132
        
 
133
        QString title = window->getTitle();
 
134
        setTabLabel( window->getQWidget(), title );
 
135
        
 
136
        if ( it.data().menuitem == 0 )
 
137
        {
 
138
                int menuid = m_Windows.size();
 
139
                QString menutitle = "&" + QString::number(menuid) + " " + title;
 
140
                it.data().menuitem = m_MenuWindow->insertItem( menutitle, menuid );
 
141
        }
 
142
        else
 
143
        {
 
144
                QString menutitle = "&" + QString::number(it.data().menuitem) + " " + title;
 
145
                m_MenuWindow->changeItem( it.data().menuitem, menutitle );
 
146
        }
 
147
        
 
148
        updateCloseButtons();
 
149
}
 
150
 
 
151
void KCHMViewWindowMgr::closeCurrentWindow( )
 
152
{
 
153
        // Do not allow to close the last window
 
154
        if ( m_Windows.size() == 1 )
 
155
                return;
 
156
                        
 
157
        QWidget * w = currentPage();
 
158
        WindowsIterator it;
 
159
                        
 
160
        if ( !w || (it = m_Windows.find( w )) == m_Windows.end() )
 
161
                qFatal( "KCHMViewWindowMgr::closeCurrentWindow called without any windows!" );
 
162
        
 
163
        closeWindow( it.data() );
 
164
}
 
165
 
 
166
void KCHMViewWindowMgr::closeWindow( const tab_window_t & tab )
 
167
{
 
168
        WindowsIterator it = m_Windows.find( tab.widget );
 
169
                        
 
170
        if ( it == m_Windows.end() )
 
171
                qFatal( "KCHMViewWindowMgr::closeWindow called with unknown widget!" );
 
172
 
 
173
        if ( tab.menuitem != 0 )
 
174
                m_MenuWindow->removeItem( tab.menuitem );
 
175
 
 
176
        removePage( tab.widget );
 
177
        delete tab.window;
 
178
        
 
179
        m_Windows.remove( it );
 
180
        updateCloseButtons();
 
181
}
 
182
 
 
183
void KCHMViewWindowMgr::onCloseWindow( int id )
 
184
{
 
185
        for ( WindowsIterator it = m_Windows.begin(); it != m_Windows.end(); it++ )
 
186
        {
 
187
                if ( it.data().menuitem != id )
 
188
                        continue;
 
189
                
 
190
                closeWindow( it.data() );
 
191
                break;
 
192
        }
 
193
}
 
194
 
 
195
 
 
196
void KCHMViewWindowMgr::restoreSettings( const KCHMSettings::viewindow_saved_settings_t & settings )
 
197
{
 
198
        // Destroy pre-created tab
 
199
        closeWindow( m_Windows.begin().data() );
 
200
        
 
201
        for ( unsigned int i = 0; i < settings.size(); i++ )
 
202
        {
 
203
                KCHMViewWindow * window = addNewTab( false );
 
204
                window->openUrl( settings[i].url ); // will call setTabName()
 
205
                window->setScrollbarPosition( settings[i].scroll_y );
 
206
                window->setZoomFactor( settings[i].zoom );
 
207
        }
 
208
}
 
209
 
 
210
 
 
211
void KCHMViewWindowMgr::saveSettings( KCHMSettings::viewindow_saved_settings_t & settings )
 
212
{
 
213
        settings.clear();
 
214
        
 
215
        for ( int i = 0; i < count(); i++ )
 
216
        {
 
217
                QWidget * p = page( i );
 
218
                WindowsIterator it = m_Windows.find( p );
 
219
                        
 
220
                if ( it == m_Windows.end() )
 
221
                        qFatal( "KCHMViewWindowMgr::saveSettings: could not find widget!" );
 
222
 
 
223
                settings.push_back( 
 
224
                                KCHMSettings::SavedViewWindow( 
 
225
                                                it.data().window->getOpenedPage(), 
 
226
                                                it.data().window->getScrollbarPosition(), 
 
227
                                                it.data().window->getZoomFactor()) );
 
228
        }
 
229
}
 
230
 
 
231
void KCHMViewWindowMgr::updateCloseButtons( )
 
232
{
 
233
        m_MenuWindow->setItemEnabled( m_menuIdClose, m_Windows.size() > 1 );
 
234
        m_closeButton->setEnabled( m_Windows.size() > 1 );
 
235
}
 
236
 
 
237
void KCHMViewWindowMgr::onTabChanged( QWidget * newtab )
 
238
{
 
239
        WindowsIterator it = m_Windows.find( newtab );
 
240
                        
 
241
        if ( it == m_Windows.end() )
 
242
                qFatal( "KCHMViewWindowMgr::onTabChanged called with unknown widget!" );
 
243
 
 
244
        it.data().window->updateNavigationToolbar();
 
245
        mainWindow->slotBrowserChanged( it.data().window );
 
246
}
 
247
 
 
248
#include "kchmviewwindowmgr.moc"