summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-write/mainwindow.cpp
authorleseb <leseb>2002-07-14 21:21:35 (UTC)
committer leseb <leseb>2002-07-14 21:21:35 (UTC)
commit4feeec8b5b41cfd3d13274411f515524f687da09 (patch) (unidiff)
tree002bbfb9997713e5d5975855d3cfbba7a71b9104 /noncore/apps/opie-write/mainwindow.cpp
parentbdef9cf23ced569a9bc80c1d4f25d85861273b4a (diff)
downloadopie-4feeec8b5b41cfd3d13274411f515524f687da09.zip
opie-4feeec8b5b41cfd3d13274411f515524f687da09.tar.gz
opie-4feeec8b5b41cfd3d13274411f515524f687da09.tar.bz2
opie-write first draft
Diffstat (limited to 'noncore/apps/opie-write/mainwindow.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-write/mainwindow.cpp570
1 files changed, 570 insertions, 0 deletions
diff --git a/noncore/apps/opie-write/mainwindow.cpp b/noncore/apps/opie-write/mainwindow.cpp
new file mode 100644
index 0000000..ed95e83
--- a/dev/null
+++ b/noncore/apps/opie-write/mainwindow.cpp
@@ -0,0 +1,570 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the Qtopia Environment.
5**
6** Licensees holding valid Qtopia Developer license may use this
7** file in accordance with the Qtopia Developer License Agreement
8** provided with the Software.
9**
10** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
11** THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12** PURPOSE.
13**
14** email sales@trolltech.com for information about Qtopia License
15** Agreements.
16**
17** Contact info@trolltech.com if any conditions of this licensing are
18** not clear to you.
19**
20**********************************************************************/
21
22#include "mainwindow.h"
23#include <qpe/fileselector.h>
24#include <qpe/applnk.h>
25#include <qpe/resource.h>
26//#include "qspellchecker.h"
27#include "qtextedit.h"
28#include <qaction.h>
29#include <qtoolbar.h>
30#include <qtoolbutton.h>
31#include <qtabwidget.h>
32#include <qapplication.h>
33#include <qfontdatabase.h>
34#include <qcombobox.h>
35#include <qlineedit.h>
36#include <qfileinfo.h>
37#include <qfile.h>
38#include <qfiledialog.h>
39#include <qprinter.h>
40#include <qpaintdevicemetrics.h>
41#include <qmenubar.h>
42#include <qpopupmenu.h>
43#include <qcolordialog.h>
44#include <qpainter.h>
45#include <qstyle.h>
46
47class ButtonMenu : public QToolButton
48{
49 Q_OBJECT
50public:
51 ButtonMenu( QWidget *parent, const char *name=0 )
52 : QToolButton( parent, name ), current(0)
53 {
54 setPopup( new QPopupMenu( this ) );
55 setPopupDelay( 1 );
56 connect( popup(), SIGNAL(activated(int)), this, SLOT(selected(int)) );
57 }
58
59 int insertItem(const QIconSet &icon, const QString &text, int id ) {
60 if ( !popup()->count() ) {
61 setIconSet( icon );
62 current = id;
63 }
64 return popup()->insertItem( icon, text, id );
65 }
66
67 void setCurrentItem( int id ) {
68 if ( id != current ) {
69 current = id;
70 setIconSet( *popup()->iconSet( id ) );
71 }
72 }
73
74 virtual QSize sizeHint() const {
75 return QToolButton::sizeHint() + QSize( 4, 0 );
76 }
77
78signals:
79 void activated( int id );
80
81protected slots:
82 void selected( int id ) {
83 current = id;
84 setIconSet( *popup()->iconSet( id ) );
85 emit activated( id );
86 }
87
88protected:
89 virtual void drawButtonLabel( QPainter *p ) {
90 p->translate( -4, 0 );
91 QToolButton::drawButtonLabel( p );
92 p->translate( 4, 0 );
93 }
94
95private:
96 int current;
97};
98
99//===========================================================================
100
101MainWindow::MainWindow( QWidget *parent, const char *name )
102 : QMainWindow( parent, name ),
103 doc( 0 )
104{
105 setRightJustification(TRUE);
106
107 editorStack = new QWidgetStack( this );
108
109 fileSelector = new FileSelector( "text/html",
110 editorStack, "fileselector" );
111
112
113 fileSelector->setCloseVisible( FALSE );
114 editorStack->addWidget( fileSelector, 0 );
115
116 editor = new Qt3::QTextEdit( editorStack );
117 editor->setTextFormat( Qt::RichText );
118 editorStack->addWidget( editor, 1 );
119
120 setupActions();
121
122 QObject::connect( fileSelector, SIGNAL(closeMe()),
123 this, SLOT(showEditTools()) );
124 QObject::connect( fileSelector, SIGNAL(fileSelected(const DocLnk &)),
125 this, SLOT(openFile(const DocLnk &)) );
126 QObject::connect( fileSelector, SIGNAL(newSelected(const DocLnk&)),
127 this, SLOT(newFile(const DocLnk&)) );
128
129 if ( fileSelector->fileCount() < 1 )
130 fileNew();
131 else {
132 fileOpen();
133 }
134 doConnections( editor );
135
136 setCentralWidget( editorStack );
137}
138
139MainWindow::~MainWindow()
140{
141 save();
142}
143
144void MainWindow::setupActions()
145{
146 setToolBarsMovable(false);
147
148 tbMenu = new QToolBar( this );
149 tbMenu->setHorizontalStretchable( TRUE );
150
151 QMenuBar *menu = new QMenuBar( tbMenu );
152
153 tbEdit = new QToolBar( this );
154
155 QPopupMenu *file = new QPopupMenu( this );
156 menu->insertItem( tr("File"), file );
157
158 QPopupMenu *edit = new QPopupMenu( this );
159 menu->insertItem( tr("Edit"), edit );
160
161 // ### perhaps these shortcut keys should have some
162 // IPaq keys defined???
163 QAction *a;
164
165 a = new QAction( tr( "New" ), Resource::loadPixmap("new"), QString::null, 0, this, 0 );
166 connect( a, SIGNAL(activated()), this, SLOT(fileNew()) );
167 a->addTo( file );
168
169 a = new QAction( tr( "Open" ), Resource::loadPixmap( "fileopen" ), QString::null, 0, this, 0 );
170 connect( a, SIGNAL(activated()), this, SLOT(fileOpen()) );
171 a->addTo( file );
172
173 a = new QAction( tr( "Undo" ), Resource::loadIconSet("opie-write/undo"),
174 QString::null, 0, this, "editUndo" );
175 connect( a, SIGNAL( activated() ), this, SLOT( editUndo() ) );
176 connect( editor, SIGNAL(undoAvailable(bool)), a, SLOT(setEnabled(bool)) );
177 a->addTo( tbEdit );
178 a->addTo( edit );
179 a = new QAction( tr( "Redo" ), Resource::loadIconSet("opie-write/redo"),
180 QString::null, 0, this, "editRedo" );
181 connect( a, SIGNAL( activated() ), this, SLOT( editRedo() ) );
182 connect( editor, SIGNAL(redoAvailable(bool)), a, SLOT(setEnabled(bool)) );
183 a->addTo( tbEdit );
184 a->addTo( edit );
185
186 edit->insertSeparator();
187
188 a = new QAction( tr( "Copy" ), Resource::loadIconSet("copy"),
189 QString::null, 0, this, "editCopy" );
190 connect( a, SIGNAL( activated() ), this, SLOT( editCopy() ) );
191 connect( editor, SIGNAL(copyAvailable(bool)), a, SLOT(setEnabled(bool)) );
192 a->addTo( tbEdit );
193 a->addTo( edit );
194 a = new QAction( tr( "Cut" ), Resource::loadIconSet("cut"),
195 QString::null, 0, this, "editCut" );
196 connect( a, SIGNAL( activated() ), this, SLOT( editCut() ) );
197 connect( editor, SIGNAL(copyAvailable(bool)), a, SLOT(setEnabled(bool)) );
198 a->addTo( tbEdit );
199 a->addTo( edit );
200 a = new QAction( tr( "Paste" ), Resource::loadPixmap("paste"),
201 QString::null, 0, this, "editPaste" );
202 connect( a, SIGNAL( activated() ), this, SLOT( editPaste() ) );
203 a->addTo( tbEdit );
204 a->addTo( edit );
205
206 tbFont = new QToolBar( this );
207 tbFont->setLabel( "Font Actions" );
208 tbFont->setHorizontalStretchable(TRUE);
209
210 comboFont = new QComboBox( FALSE, tbFont );
211 QFontDatabase db;
212 comboFont->insertStringList( db.families() );
213 connect( comboFont, SIGNAL( activated( const QString & ) ),
214 this, SLOT( textFamily( const QString & ) ) );
215 comboFont->setCurrentItem( comboFont->listBox()->index( comboFont->listBox()->findItem( QApplication::font().family() ) ) );
216
217 comboSize = new QComboBox( TRUE, tbFont );
218 QValueList<int> sizes = db.standardSizes();
219 QValueList<int>::Iterator it = sizes.begin();
220 for ( ; it != sizes.end(); ++it )
221 comboSize->insertItem( QString::number( *it ) );
222 connect( comboSize, SIGNAL( activated( const QString & ) ),
223 this, SLOT( textSize( const QString & ) ) );
224 comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
225 comboSize->setFixedWidth( 38 );
226
227 tbStyle = new QToolBar( this );
228 tbStyle->setLabel( "Style Actions" );
229
230 actionTextBold = new QAction( tr( "Bold" ),
231 Resource::loadPixmap("bold"),
232 QString::null, CTRL + Key_B,
233 this, "textBold" );
234 connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
235 actionTextBold->addTo( tbStyle );
236 actionTextBold->setToggleAction( TRUE );
237 actionTextItalic = new QAction( tr( "Italic" ),
238 Resource::loadPixmap("italic"),
239 tr( "&Italic" ), CTRL + Key_I,
240 this, "textItalic" );
241 connect( actionTextItalic, SIGNAL( activated() ), this,
242 SLOT( textItalic() ) );
243 actionTextItalic->addTo( tbStyle );
244 actionTextItalic->setToggleAction( TRUE );
245 actionTextUnderline = new QAction( tr( "Underline" ),
246 Resource::loadPixmap("underline"),
247 tr( "&Underline" ), CTRL + Key_U,
248 this, "textUnderline" );
249 connect( actionTextUnderline, SIGNAL( activated() ),
250 this, SLOT( textUnderline() ) );
251 actionTextUnderline->addTo( tbStyle );
252 actionTextUnderline->setToggleAction( TRUE );
253
254 alignMenu = new ButtonMenu( tbStyle );
255 alignMenu->insertItem( Resource::loadPixmap("left"), tr("Left"), AlignLeft );
256 alignMenu->insertItem( Resource::loadPixmap("center"), tr("Center"), AlignCenter );
257 alignMenu->insertItem( Resource::loadPixmap("right"), tr("Right"), AlignRight );
258 alignMenu->insertItem( Resource::loadPixmap("opie-write/justify"), tr("Full"), Qt3::AlignJustify );
259 connect( alignMenu, SIGNAL(activated(int)), this, SLOT(textAlign(int)) );
260}
261
262Qt3::QTextEdit *MainWindow::currentEditor() const
263{
264 return editor;
265}
266
267void MainWindow::doConnections( Qt3::QTextEdit *e )
268{
269 connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
270 this, SLOT( fontChanged( const QFont & ) ) );
271 connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
272 this, SLOT( colorChanged( const QColor & ) ) );
273 connect( e, SIGNAL( currentAlignmentChanged( int ) ),
274 this, SLOT( alignmentChanged( int ) ) );
275}
276
277void MainWindow::updateFontSizeCombo( const QFont &f )
278{
279 comboSize->clear();
280 QFontDatabase fdb;
281 QValueList<int> sizes = fdb.pointSizes( f.family() );
282 QValueList<int>::Iterator it = sizes.begin();
283 for ( ; it != sizes.end(); ++it )
284 comboSize->insertItem( QString::number( *it ) );
285}
286
287void MainWindow::editUndo()
288{
289 if ( !currentEditor() )
290 return;
291 currentEditor()->undo();
292}
293
294void MainWindow::editRedo()
295{
296 if ( !currentEditor() )
297 return;
298 currentEditor()->redo();
299}
300
301void MainWindow::editCut()
302{
303 if ( !currentEditor() )
304 return;
305 currentEditor()->cut();
306}
307
308void MainWindow::editCopy()
309{
310 if ( !currentEditor() )
311 return;
312 currentEditor()->copy();
313}
314
315void MainWindow::editPaste()
316{
317 if ( !currentEditor() )
318 return;
319 currentEditor()->paste();
320}
321
322void MainWindow::textBold()
323{
324 if ( !currentEditor() )
325 return;
326 currentEditor()->setBold( actionTextBold->isOn() );
327}
328
329void MainWindow::textUnderline()
330{
331 if ( !currentEditor() )
332 return;
333 currentEditor()->setUnderline( actionTextUnderline->isOn() );
334}
335
336void MainWindow::textItalic()
337{
338 if ( !currentEditor() )
339 return;
340 currentEditor()->setItalic( actionTextItalic->isOn() );
341}
342
343void MainWindow::textFamily( const QString &f )
344{
345 if ( !currentEditor() )
346 return;
347 currentEditor()->setFamily( f );
348 currentEditor()->viewport()->setFocus();
349}
350
351void MainWindow::textSize( const QString &p )
352{
353 if ( !currentEditor() )
354 return;
355 currentEditor()->setPointSize( p.toInt() );
356 currentEditor()->viewport()->setFocus();
357}
358
359void MainWindow::textStyle( int i )
360{
361 if ( !currentEditor() )
362 return;
363 if ( i == 0 )
364 currentEditor()->setParagType( Qt3::QStyleSheetItem::DisplayBlock,
365 Qt3::QStyleSheetItem::ListDisc );
366 else if ( i == 1 )
367 currentEditor()->setParagType( Qt3::QStyleSheetItem::DisplayListItem,
368 Qt3::QStyleSheetItem::ListDisc );
369 else if ( i == 2 )
370 currentEditor()->setParagType( Qt3::QStyleSheetItem::DisplayListItem,
371 Qt3::QStyleSheetItem::ListCircle );
372 else if ( i == 3 )
373 currentEditor()->setParagType( Qt3::QStyleSheetItem::DisplayListItem,
374 Qt3::QStyleSheetItem::ListSquare );
375 else if ( i == 4 )
376 currentEditor()->setParagType( Qt3::QStyleSheetItem::DisplayListItem,
377 Qt3::QStyleSheetItem::ListDecimal );
378 else if ( i == 5 )
379 currentEditor()->setParagType( Qt3::QStyleSheetItem::DisplayListItem,
380 Qt3::QStyleSheetItem::ListLowerAlpha );
381 else if ( i == 6 )
382 currentEditor()->setParagType( Qt3::QStyleSheetItem::DisplayListItem,
383 Qt3::QStyleSheetItem::ListUpperAlpha );
384 currentEditor()->viewport()->setFocus();
385}
386
387void MainWindow::textAlign( int a )
388{
389 if ( !currentEditor() )
390 return;
391 editor->setAlignment( a );
392}
393
394void MainWindow::fontChanged( const QFont &f )
395{
396 comboFont->setCurrentItem( comboFont->listBox()->index( comboFont->listBox()->findItem( f.family() ) ) );
397 updateFontSizeCombo( f );
398 comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
399 actionTextBold->setOn( f.bold() );
400 actionTextItalic->setOn( f.italic() );
401 actionTextUnderline->setOn( f.underline() );
402}
403
404void MainWindow::colorChanged( const QColor & )
405{
406}
407
408void MainWindow::alignmentChanged( int a )
409{
410 if ( ( a == Qt3::AlignAuto ) || ( a & AlignLeft )) {
411 alignMenu->setCurrentItem(AlignLeft);
412 } else if ( ( a & AlignCenter ) ) {
413 alignMenu->setCurrentItem(AlignCenter);
414 } else if ( ( a & AlignRight ) ) {
415 alignMenu->setCurrentItem(AlignRight);
416 } else if ( ( a & Qt3::AlignJustify ) ) {
417 alignMenu->setCurrentItem(Qt3::AlignJustify);
418 }
419}
420
421void MainWindow::editorChanged( QWidget * )
422{
423 if ( !currentEditor() )
424 return;
425 fontChanged( currentEditor()->font() );
426 colorChanged( currentEditor()->color() );
427 alignmentChanged( currentEditor()->alignment() );
428}
429
430void MainWindow::fileOpen()
431{
432 save();
433 editorStack->raiseWidget( fileSelector );
434 fileSelector->reread();
435 hideEditTools();
436 fileSelector->setNewVisible( TRUE );
437 clear();
438 updateCaption();
439}
440
441void MainWindow::fileRevert()
442{
443 qDebug( "QMainWindow::fileRevert needs to be done" );
444}
445
446void MainWindow::fileNew()
447{
448 editor->setTextFormat( Qt::RichText );
449 save();
450 newFile(DocLnk());
451}
452
453void MainWindow::insertTable()
454{
455 qDebug( "MainWindow::insertTable() needs to be done" );
456}
457
458void MainWindow::newFile( const DocLnk &dl )
459{
460 DocLnk nf = dl;
461 nf.setType( "text/html" );
462 clear();
463 editorStack->raiseWidget( editor );
464 editor->viewport()->setFocus();
465 doc = new DocLnk( nf );
466 updateCaption();
467}
468
469void MainWindow::openFile( const DocLnk &dl )
470{
471 FileManager fm;
472 QString txt;
473 if ( !fm.loadFile( dl, txt ) )
474 qDebug( "couldn't open file" );
475 clear();
476 editorStack->raiseWidget( editor );
477 editor->viewport()->setFocus();
478 doc = new DocLnk( dl );
479 editor->setText( txt );
480 editor->setModified( FALSE );
481 updateCaption();
482}
483
484void MainWindow::showEditTools( void )
485{
486 tbMenu->show();
487 tbEdit->show();
488 tbFont->show();
489 tbStyle->show();
490}
491
492void MainWindow::hideEditTools( void )
493{
494 // let's reset the buttons...
495 actionTextBold->setOn( FALSE );
496 actionTextItalic->setOn( FALSE );
497 actionTextUnderline->setOn( FALSE );
498 //comboFont->setCurrentText( QApplication::font().family() );
499 comboSize->lineEdit()->setText( QString::number(QApplication::font().pointSize() ) );
500 tbMenu->hide();
501 tbEdit->hide();
502 tbFont->hide();
503 tbStyle->hide();
504}
505
506
507void MainWindow::save()
508{
509 if ( !doc )
510 return;
511 if ( !editor->isModified() )
512 return;
513
514 QString rt = editor->text();
515
516 // quick hack to get around formatting...
517 editor->setTextFormat( Qt::PlainText );
518 QString pt = editor->text();
519 editor->setTextFormat( Qt::RichText );
520
521 if ( doc->name().isEmpty() ) {
522 unsigned ispace = pt.find( ' ' );
523 unsigned ienter = pt.find( '\n' );
524 int i = (ispace < ienter) ? ispace : ienter;
525 QString docname;
526 if ( i == -1 ) {
527 if ( pt.isEmpty() )
528 docname = "Empty Text";
529 else
530 docname = pt;
531 } else {
532 docname = pt.left( i );
533 }
534 doc->setName(docname);
535 }
536 FileManager fm;
537 fm.saveFile( *doc, rt );
538}
539
540void MainWindow::clear()
541{
542 delete doc;
543 doc = 0;
544 editor->clear();
545}
546
547void MainWindow::updateCaption()
548{
549 if ( !doc )
550 setCaption( tr("Rich Text Editor") );
551 else {
552 QString s = doc->name();
553 if ( s.isEmpty() )
554 s = tr( "Unnamed" );
555 setCaption( s + " - " + tr("Rich Text Editor") );
556 }
557}
558
559void MainWindow::closeEvent( QCloseEvent *e )
560{
561 if ( editorStack->visibleWidget() == editor ) {
562 // call fileOpen instead, don't close it
563 fileOpen();
564 e->ignore();
565 } else {
566 e->accept();
567 }
568}
569
570#include "mainwindow.moc"