summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-gutenbrowser/helpwindow.cpp
authorllornkcor <llornkcor>2004-04-07 13:07:42 (UTC)
committer llornkcor <llornkcor>2004-04-07 13:07:42 (UTC)
commitfc42c7317c956e5bc6e74bfbb1ec185d2faa79f4 (patch) (unidiff)
tree703e89c7178f764aab25028d4525f74340b28dcb /noncore/apps/opie-gutenbrowser/helpwindow.cpp
parent9e1ecade17a77c3d50aecc4c92091d852e99e8d9 (diff)
downloadopie-fc42c7317c956e5bc6e74bfbb1ec185d2faa79f4.zip
opie-fc42c7317c956e5bc6e74bfbb1ec185d2faa79f4.tar.gz
opie-fc42c7317c956e5bc6e74bfbb1ec185d2faa79f4.tar.bz2
add gutenbrowser
Diffstat (limited to 'noncore/apps/opie-gutenbrowser/helpwindow.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-gutenbrowser/helpwindow.cpp326
1 files changed, 326 insertions, 0 deletions
diff --git a/noncore/apps/opie-gutenbrowser/helpwindow.cpp b/noncore/apps/opie-gutenbrowser/helpwindow.cpp
new file mode 100644
index 0000000..e86ca49
--- a/dev/null
+++ b/noncore/apps/opie-gutenbrowser/helpwindow.cpp
@@ -0,0 +1,326 @@
1/****************************************************************************
2** $Id$
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9 copyright : (C) 2000 -2004 by llornkcor
10 email : ljp@llornkcor.com
11*****************************************************************************/
12
13#include "helpwindow.h"
14#include <qstatusbar.h>
15
16#include <qmenubar.h>
17#include <qtoolbar.h>
18#include <qtoolbutton.h>
19#include <qcombobox.h>
20
21#include <ctype.h>
22
23HelpWindow::HelpWindow( const QString& home_, const QString& _path, QWidget* parent, const char *name )
24 : QMainWindow( parent, name, WDestructiveClose ), pathCombo( 0 ), selectedURL()
25{
26 QString local_library = (QDir::homeDirPath ()) +"/Applications/gutenbrowser/";
27// readHistory();
28// readBookmarks();
29
30 browser = new QTextBrowser( this );
31 QStringList Strlist;
32 Strlist.append( home_);
33 browser->mimeSourceFactory()->setFilePath( Strlist );
34
35 browser->setFrameStyle( QFrame::Panel | QFrame::Sunken );
36
37 connect(browser,SIGNAL(textChanged()),this,SLOT(textChanged()));
38
39 setCentralWidget( browser );
40
41 if ( !home_.isEmpty() )
42
43////////////////////////////////
44 browser->setSource( home_ );
45
46////////////////////////////////
47 connect( browser, SIGNAL( highlighted( const QString&) ),
48 statusBar(), SLOT( message( const QString&)) );
49
50// resize( 640,600 );
51#ifdef Q_WS_QWS
52 setGeometry( 0,0,236,280);
53#else
54 setGeometry( 10,30,520,420 );
55// resize(520,420);
56#endif
57
58 QPopupMenu* file = new QPopupMenu( this );
59// file->insertItem( tr("&New Window"), this, SLOT( newWindow() ), ALT | Key_N );
60 file->insertItem( tr("&Open File"), this, SLOT( openFile() ), ALT | Key_O );
61// file->insertItem( tr("&Print"), this, SLOT( print() ), ALT | Key_P );
62 file->insertSeparator();
63 file->insertItem( tr("&Close"), this, SLOT( close() ), ALT | Key_Q );
64// file->insertItem( tr("E&xit"), qApp, SLOT( closeAllWindows() ), ALT | Key_X );
65
66 // The same three icons are used twice each.
67////F FIXME
68 QString pixs=(QDir::homeDirPath ()) +"/Applications/gutenbrowser/pix/";
69 QIconSet icon_back( QPixmap(pixs+"back.png") );
70 QIconSet icon_forward( QPixmap(pixs+"forward.png") );
71 QIconSet icon_home( QPixmap(pixs+"home.png") );
72
73 QPopupMenu* go = new QPopupMenu( this );
74 backwardId = go->insertItem( icon_back, tr("&Backward"), browser, SLOT( backward() ), ALT | Key_Left );
75 forwardId = go->insertItem( icon_forward, tr("&Forward"), browser, SLOT( forward() ), ALT | Key_Right );
76 go->insertItem( icon_home, tr("&Home"), browser, SLOT( home() ) );
77
78// QPopupMenu* help = new QPopupMenu( this );
79// help->insertItem( tr("&About ..."), this, SLOT( about() ) );
80// help->insertItem( tr("About &Qt ..."), this, SLOT( aboutQt() ) );
81
82
83 hist = new QPopupMenu( this );
84 QStringList::Iterator it = history.begin();
85 for ( ; it != history.end(); ++it )
86 mHistory[ hist->insertItem( *it ) ] = *it;
87 connect( hist, SIGNAL( activated( int ) ), this, SLOT( histChosen( int ) ) );
88
89 bookm = new QPopupMenu( this );
90 bookm->insertItem( tr( "Add Bookmark" ), this, SLOT( addBookmark() ) );
91 bookm->insertSeparator();
92
93 QStringList::Iterator it2 = bookmarks.begin();
94 for ( ; it2 != bookmarks.end(); ++it2 )
95 mBookmarks[ bookm->insertItem( *it2 ) ] = *it2;
96 connect( bookm, SIGNAL( activated( int ) ),
97 this, SLOT( bookmChosen( int ) ) );
98
99 menuBar()->insertItem( tr("&File"), file );
100 menuBar()->insertItem( tr("&Go"), go );
101 menuBar()->insertItem( tr( "History" ), hist );
102 menuBar()->insertItem( tr( "Bookmarks" ), bookm );
103// menuBar()->insertSeparator();
104// menuBar()->insertItem( tr("&Help"), help );
105
106 menuBar()->setItemEnabled( forwardId, FALSE);
107 menuBar()->setItemEnabled( backwardId, FALSE);
108 connect( browser, SIGNAL( backwardAvailable( bool ) ), this, SLOT( setBackwardAvailable( bool ) ) );
109 connect( browser, SIGNAL( forwardAvailable( bool ) ), this, SLOT( setForwardAvailable( bool ) ) );
110
111
112 QToolBar* toolbar = new QToolBar( this );
113 addToolBar( toolbar, "Toolbar");
114 QToolButton* button;
115
116 button = new QToolButton( icon_back, tr("Backward"), "", browser, SLOT(backward()), toolbar );
117 connect( browser, SIGNAL( backwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
118 button->setEnabled( FALSE );
119 button = new QToolButton( icon_forward, tr("Forward"), "", browser, SLOT(forward()), toolbar );
120 connect( browser, SIGNAL( forwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
121 button->setEnabled( FALSE );
122 button = new QToolButton( icon_home, tr("Home"), "", browser, SLOT(home()), toolbar );
123
124 toolbar->addSeparator();
125
126 pathCombo = new QComboBox( TRUE, toolbar );
127 connect( pathCombo, SIGNAL( activated( const QString & ) ), this, SLOT( pathSelected( const QString & ) ) );
128 toolbar->setStretchableWidget( pathCombo );
129
130// pathCombo->setMaximumWidth(190);
131// setRightJustification( TRUE );
132// setDockEnabled( Left, FALSE );
133// setDockEnabled( Right, FALSE );
134
135 pathCombo->insertItem( home_ );
136
137 browser->setFocus();
138
139
140}
141
142
143void HelpWindow::setBackwardAvailable( bool b)
144{
145 menuBar()->setItemEnabled( backwardId, b);
146}
147
148void HelpWindow::setForwardAvailable( bool b)
149{
150 menuBar()->setItemEnabled( forwardId, b);
151}
152
153
154void HelpWindow::textChanged()
155{
156 if ( browser->documentTitle().isNull() ) {
157 setCaption( "Gutenbrowser - Helpviewer - " + browser->context() );
158 selectedURL = browser->context();
159 }
160 else {
161 setCaption( "Gutenbrowser - Helpviewer - " + browser->documentTitle() ) ;
162 selectedURL = browser->documentTitle();
163 }
164
165 if ( !selectedURL.isEmpty() && pathCombo ) {
166 bool exists = FALSE;
167 int i;
168 for ( i = 0; i < pathCombo->count(); ++i ) {
169 if ( pathCombo->text( i ) == selectedURL ) {
170 exists = TRUE;
171 break;
172 }
173 }
174 if ( !exists ) {
175 pathCombo->insertItem( selectedURL, 0 );
176 pathCombo->setCurrentItem( 0 );
177 mHistory[ hist->insertItem( selectedURL ) ] = selectedURL;
178 } else
179 pathCombo->setCurrentItem( i );
180 selectedURL = QString::null;
181 }
182}
183
184HelpWindow::~HelpWindow()
185{
186 history.clear();
187 QMap<int, QString>::Iterator it = mHistory.begin();
188 for ( ; it != mHistory.end(); ++it )
189 history.append( *it );
190
191 QFile f( QDir::currentDirPath() + "/.history" );
192 f.open( IO_WriteOnly );
193 QDataStream s( &f );
194 s << history;
195 f.close();
196
197 bookmarks.clear();
198 QMap<int, QString>::Iterator it2 = mBookmarks.begin();
199 for ( ; it2 != mBookmarks.end(); ++it2 )
200 bookmarks.append( *it2 );
201
202 QFile f2( QDir::currentDirPath() + "/.bookmarks" );
203 f2.open( IO_WriteOnly );
204 QDataStream s2( &f2 );
205 s2 << bookmarks;
206 f2.close();
207}
208
209// void HelpWindow::about()
210// {
211// QMessageBox::about( this, "Gutenbrowser", "<p>Thanks to Trolltech for this</p>" );
212// }
213
214// void HelpWindow::aboutQt()
215// {
216// QMessageBox::aboutQt( this, "QBrowser" );
217// }
218
219void HelpWindow::openFile()
220{
221#ifndef QT_NO_FILEDIALOG
222 QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, this );
223 if ( !fn.isEmpty() )
224 browser->setSource( fn );
225#endif
226}
227
228void HelpWindow::newWindow()
229{
230 ( new HelpWindow(browser->source(), "qbrowser") )->show();
231}
232
233void HelpWindow::print()
234{
235#ifndef QT_NO_PRINTER
236 QPrinter printer;
237 printer.setFullPage(TRUE);
238 if ( printer.setup() ) {
239 QPainter p( &printer );
240 QPaintDeviceMetrics metrics(p.device());
241 int dpix = metrics.logicalDpiX();
242 int dpiy = metrics.logicalDpiY();
243 const int margin = 72; // pt
244 QRect body(margin*dpix/72, margin*dpiy/72,
245 metrics.width()-margin*dpix/72*2,
246 metrics.height()-margin*dpiy/72*2 );
247 QFont font("times", 10);
248 QSimpleRichText richText( browser->text(), font, browser->context(), browser->styleSheet(),
249 browser->mimeSourceFactory(), body.height() );
250 richText.setWidth( &p, body.width() );
251 QRect view( body );
252 int page = 1;
253 do {
254 p.setClipRect( body );
255 richText.draw( &p, body.left(), body.top(), view, colorGroup() );
256 p.setClipping( FALSE );
257 view.moveBy( 0, body.height() );
258 p.translate( 0 , -body.height() );
259 p.setFont( font );
260 p.drawText( view.right() - p.fontMetrics().width( QString::number(page) ),
261 view.bottom() + p.fontMetrics().ascent() + 5, QString::number(page) );
262 if ( view.top() >= richText.height() )
263 break;
264 printer.newPage();
265 page++;
266 } while (TRUE);
267 }
268#endif
269}
270
271void HelpWindow::pathSelected( const QString &_path )
272{
273 browser->setSource( _path );
274 QMap<int, QString>::Iterator it = mHistory.begin();
275 bool exists = FALSE;
276 for ( ; it != mHistory.end(); ++it ) {
277 if ( *it == _path ) {
278 exists = TRUE;
279 break;
280 }
281 }
282 if ( !exists )
283 mHistory[ hist->insertItem( _path ) ] = _path;
284}
285
286void HelpWindow::readHistory()
287{
288 if ( QFile::exists( QDir::currentDirPath() + "/.history" ) ) {
289 QFile f( QDir::currentDirPath() + "/.history" );
290 f.open( IO_ReadOnly );
291 QDataStream s( &f );
292 s >> history;
293 f.close();
294 while ( history.count() > 20 )
295 history.remove( history.begin() );
296 }
297}
298
299void HelpWindow::readBookmarks()
300{
301 if ( QFile::exists( QDir::currentDirPath() + "/.bookmarks" ) ) {
302 QFile f( QDir::currentDirPath() + "/.bookmarks" );
303 f.open( IO_ReadOnly );
304 QDataStream s( &f );
305 s >> bookmarks;
306 f.close();
307 }
308}
309
310void HelpWindow::histChosen( int i )
311{
312 if ( mHistory.contains( i ) )
313 browser->setSource( mHistory[ i ] );
314}
315
316void HelpWindow::bookmChosen( int i )
317{
318 if ( mBookmarks.contains( i ) )
319 browser->setSource( mBookmarks[ i ] );
320}
321
322void HelpWindow::addBookmark()
323{
324 mBookmarks[ bookm->insertItem( caption() ) ] = caption();
325}
326