initial import
[vym] / texteditor.cpp
1 #include "texteditor.h"
2
3 #include <iostream>
4 #include <cstdlib>
5 #include <typeinfo>
6
7 #include "noteobj.h"
8 #include "settings.h"
9
10 extern int statusbarTime;
11 extern Settings settings;
12
13 extern QAction *actionViewToggleNoteEditor;
14
15 extern QString iconPath;
16 extern QString vymName;
17
18 using namespace std;
19
20
21 ///////////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////////
23
24
25 TextEditor::TextEditor()
26 {
27    // printer = new QPrinter( QPrinter::HighResolution );
28         //printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
29
30     e = new QTextEdit( this);
31     e->setFocus();
32         e->setTextFormat(Qt::RichText);         // default
33         e->setTabStopWidth (20);                // unit is pixel
34         e->setColor (Qt::black);
35         e->setAutoFillBackground (true);
36         connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
37     setCentralWidget( e );
38     statusBar()->message( tr("Ready","Statusbar message"), statusbarTime);
39         setCaption(vymName +" - " +tr ("Note Editor","Window caption"));
40
41
42         connect(e, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
43             this, SLOT(formatChanged(const QTextCharFormat &)));
44
45
46
47         // Toolbars
48         setupFileActions();
49         setupEditActions();
50         setupFormatActions();
51         setupSettingsActions();
52         
53         // Various states
54         blockChangedSignal=false;
55         setInactive();
56
57         // Load Settings
58         resize (settings.value ( "/satellite/noteeditor/geometry/size", QSize(450,600)).toSize());
59         move   (settings.value ( "/satellite/noteeditor/geometry/pos", QPoint (250,50)).toPoint());
60         
61         setShowWithMain (settings.value ( "/satellite/noteeditor/showWithMain",true).toBool());
62
63         varFont.fromString( settings.value
64                 ("/satellite/noteeditor/fonts/varFont",
65                 "Nimbus Sans l,14,-1,5,48,0,0,0,0,0").toString() 
66         );
67         fixedFont.fromString (settings.value(
68                 "/satellite/noteeditor/fonts/fixedFont",
69                 "Courier,14,-1,5,48,0,0,0,1,0").toString() 
70         );
71         QString s=settings.value ("/satellite/noteeditor/fonts/fonthintDefault","variable").toString();
72         if (s == "fixed")
73         {       
74                 actionSettingsFonthintDefault->setOn (true);
75                 e->setCurrentFont (fixedFont);
76         } else  
77         {
78                 actionSettingsFonthintDefault->setOn (false);
79                 e->setCurrentFont (varFont);
80         }       
81         filenameHint="";
82
83         // Restore position of toolbars
84         restoreState (settings.value("/satellite/noteeditor/state",0).toByteArray());
85
86         // Save settings in vymrc
87         //settings.setValue("/mainwindow/printerName",printer->printerName());
88 }
89
90
91 TextEditor::~TextEditor()
92 {
93     //if (printer) delete printer;
94         // Save Settings
95         settings.setValue( "/satellite/noteeditor/geometry/size", size() );
96         settings.setValue( "/satellite/noteeditor/geometry/pos", pos() );
97         settings.setValue ("/satellite/noteeditor/state",saveState(0));
98         
99         settings.setValue( "/satellite/noteeditor/showWithMain",showwithmain);
100
101         QString s;
102         if (actionSettingsFonthintDefault->isOn() )
103                 s="fixed";
104         else    
105                 s="variable";
106         settings.setValue( "/satellite/noteeditor/fonts/fonthintDefault",s );
107         settings.setValue("/satellite/noteeditor/fonts/varFont", varFont.toString() );
108         settings.setValue("/satellite/noteeditor/fonts/fixedFont", fixedFont.toString() );
109
110
111 }
112
113 bool TextEditor::isEmpty()
114 {
115         if (e->toPlainText().length()>0)
116                 return false;
117         else
118                 return true;
119 }
120
121 void TextEditor::setShowWithMain(bool v)
122 {
123         showwithmain=v;
124 }
125
126 bool TextEditor::showWithMain()
127 {
128         return showwithmain;
129 }
130
131
132 void TextEditor::setFontHint (const QString &fh)
133 {
134         if (fh=="fixed")
135                 actionFormatUseFixedFont->setOn (true);
136         else
137                 actionFormatUseFixedFont->setOn (false);
138 }
139
140
141 QString TextEditor::getFontHint()
142 {
143         if (actionFormatUseFixedFont->isOn())
144                 return "fixed";
145         else    
146                 return "var";
147 }
148
149 QString TextEditor::getFontHintDefault()
150 {
151         if (actionSettingsFonthintDefault->isOn())
152                 return "fixed";
153         else    
154                 return "var";
155 }
156
157 void TextEditor::setFilename(const QString &fn)
158 {
159         if (state==filledEditor)
160                 if (fn.isEmpty() )
161                 {
162                         filename="";
163                         statusBar()->message( tr("No filename available for this note.","Statusbar message"), statusbarTime );
164                 }       
165                 else
166                 {
167                         filename=fn;
168                         statusBar()->message( tr(QString( "Current filename is %1" ).arg( filename ),"Statusbar message"), statusbarTime );
169                 }       
170 }
171
172 QString TextEditor::getFilename()
173 {
174         return filename;
175 }
176
177 void TextEditor::setFilenameHint(const QString &fnh)
178 {
179         filenameHint=fnh;
180 }
181
182 QString TextEditor::getFilenameHint()
183 {
184         return filenameHint;
185 }
186
187 bool TextEditor::findText(const QString &t, const QTextDocument::FindFlags &flags)
188 {
189         if (e->find (t,flags))
190                 return true;
191         else    
192                 return false;
193 }
194
195 void TextEditor::setupFileActions()
196 {
197     QToolBar *tb = addToolBar ( tr("Note Actions") );
198         tb->setObjectName ("noteEditorFileActions");
199     QMenu *fileMenu = menuBar()->addMenu( tr( "&Note","Menubar" ));
200
201     QAction *a;
202     a = new QAction( QPixmap( iconPath+"fileopen.png"), tr( "&Import..." ),this);
203         a->setStatusTip (tr( "Import","Status tip for Note menu" ) );
204         a->setShortcut( Qt::CTRL + Qt::Key_O );
205     connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) );
206         tb->addAction (a);
207         fileMenu->addAction (a);
208         actionFileLoad=a;
209
210     fileMenu->addSeparator();
211     a = new QAction( QPixmap(iconPath+"filesave.png" ), tr( "&Export..." ),this);
212         a->setStatusTip (tr( "Export Note (HTML)","Status tip for Note menu" ) );
213         a->setShortcut( Qt::CTRL + Qt::Key_S );
214     connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) );
215         tb->addAction (a);
216         fileMenu->addAction (a);
217         actionFileSave=a;
218         
219     a = new QAction(  QPixmap(), tr( "Export &As... (HTML)" ), this);
220         a->setStatusTip (tr( "Export Note As (HTML) ","Status tip for Note Menu"  ));
221     connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) );
222         fileMenu->addAction (a);
223         actionFileSaveAs=a;
224
225     a = new QAction(QPixmap(), tr( "Export &As...(ASCII)" ), this);
226         a->setStatusTip ( tr( "Export Note As (ASCII) ","Status tip for note menu" ) );
227         a->setShortcut(Qt::ALT + Qt::Key_X );
228     connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) );
229         fileMenu->addAction (a);
230         actionFileSaveAs=a;
231 /*
232     fileMenu->addSeparator();
233     
234     a = new QAction( QPixmap(iconPath+"fileprint.png" ), tr( "&Print..." ),this);
235         a->setStatusTip (tr( "Print Note","Status tip for note menu" ) );
236         a->setShortcut( Qt::CTRL + Qt::Key_P );
237     connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) );
238         tb->addAction (a);
239         fileMenu->addAction (a);
240         actionFilePrint=a;
241 */
242 }
243
244 void TextEditor::setupEditActions()
245 {
246     QToolBar *tb = addToolBar ( tr( "Edit Actions" ));
247         tb->setObjectName ("noteEditorEditActions");
248     QMenu *editMenu = menuBar()->addMenu ( tr( "&Edit" ));
249
250     QAction *a;
251     a = new QAction(QPixmap(iconPath+"undo.png"), tr( "&Undo" ), this );
252         a->setStatusTip ( tr( "Undo","Status tip for note menu" ) );
253         a->setShortcut(Qt::CTRL + Qt::Key_Z );
254     connect( a, SIGNAL( activated() ), e, SLOT( undo() ) );
255         editMenu->addAction (a);
256         tb->addAction (a);
257         actionEditUndo=a;
258         
259     a = new QAction(QPixmap(iconPath+"redo.png" ), tr( "&Redo" ),this); 
260         a->setStatusTip ( tr( "Redo","Status tip for note menu" ) );
261         a->setShortcut( Qt::CTRL + Qt::Key_Y );
262     connect( a, SIGNAL( activated() ), e, SLOT( redo() ) );
263         editMenu->addAction (a);
264         tb->addAction (a);
265         actionEditRedo=a;
266
267     editMenu->addSeparator();
268     a = new QAction(QPixmap(), tr( "Select and copy &all" ),this); 
269         a->setStatusTip ( tr( "Select and copy all","Status tip for note menu" ) );
270         a->setShortcut( Qt::CTRL + Qt::Key_A );
271     connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) );
272         editMenu->addAction (a);
273
274     editMenu->addSeparator();
275     a = new QAction(QPixmap(iconPath+"editcopy.png" ), tr( "&Copy" ),this);
276         a->setStatusTip ( tr( "Copy","Status tip for note menu" ) );
277         a->setShortcut( Qt::CTRL + Qt::Key_C );
278     connect( a, SIGNAL( activated() ), e, SLOT( copy() ) );
279         editMenu->addAction (a);
280         tb->addAction (a);
281         actionEditCopy=a;
282         
283     a = new QAction(QPixmap(iconPath+"editcut.png" ), tr( "Cu&t" ),this);
284         a->setStatusTip ( tr( "Cut","Status tip for note menu" ) );
285         a->setShortcut( Qt::CTRL + Qt::Key_X );
286     connect( a, SIGNAL( activated() ), e, SLOT( cut() ) );
287         editMenu->addAction (a);
288         tb->addAction (a);
289         actionEditCut=a;
290
291     a = new QAction(QPixmap(iconPath+"editpaste.png" ), tr( "&Paste" ),this);
292         a->setStatusTip ( tr( "Paste","Status tip for note menu" ) );
293         a->setShortcut( Qt::CTRL + Qt::Key_V );
294     connect( a, SIGNAL( activated() ), e, SLOT( paste() ) );
295         editMenu->addAction (a);
296         tb->addAction (a);
297         actionEditPaste=a;
298         
299     a = new QAction( QPixmap( iconPath+"edittrash.png"), tr( "&Delete All" ), this);
300         a->setStatusTip (tr( "Delete all","Status tip for note menu" ) );
301     connect( a, SIGNAL( activated() ), e, SLOT( clear() ) );
302         editMenu->addAction (a);
303         tb->addAction (a);
304         actionEditDeleteAll=a;
305
306 }
307
308 void TextEditor::setupFormatActions()
309 {
310     QToolBar *tb = addToolBar ( tr("Format Actions" ));
311         tb->setObjectName ("noteEditorFormatActions");
312     QMenu *formatMenu = menuBar()->addMenu ( tr( "F&ormat" ));
313
314     QAction *a;
315
316     a = new QAction( QPixmap(iconPath+"formatfixedfont.png"), tr( "&Font hint" ), Qt::ALT + Qt::Key_I, this, "fontHint" );
317         a->setStatusTip (tr( "Toggle font hint for the whole text","Status tip for note menu" ) );
318         a->setToggleAction (true);
319         a->setOn (settings.value("/noteeditor/fonts/useFixedByDefault",false).toBool() );
320     connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) );
321         formatMenu->addAction (a);
322         tb->addAction (a);
323         actionFormatUseFixedFont=a;
324
325     comboFont = new QComboBox;
326         tb->addWidget (comboFont);
327     QFontDatabase fontDB;
328     comboFont->insertStringList( fontDB.families() );
329     connect( comboFont, SIGNAL( activated( const QString & ) ),
330              this, SLOT( textFamily( const QString & ) ) );
331     comboFont->addItem( QApplication::font().family() );
332     comboSize = new QComboBox;
333         tb->addWidget (comboSize);
334         QList<int> sizes=fontDB.standardSizes();
335         QList<int>::iterator i = sizes.begin();
336         while (i != sizes.end()) 
337         {
338                 ++i; // increment i before using it
339                 comboSize->insertItem ( QString::number(*i));
340         }       
341     connect( comboSize, SIGNAL( activated( const QString & ) ),
342              this, SLOT( textSize( const QString & ) ) );
343     comboSize->addItem ( QString::number( QApplication::font().pointSize() ) );
344
345     formatMenu->addSeparator();
346
347     QPixmap pix( 16, 16 );
348     pix.fill( e->color());
349     a = new QAction( pix, tr( "&Color..." ), this);
350         formatMenu->addAction (a);
351         tb->addAction (a);
352     connect( a, SIGNAL( activated() ), this, SLOT( textColor() ) );
353     actionTextColor=a;
354
355     a = new QAction( QPixmap (iconPath+"text_bold.png"), tr( "&Bold" ), this);
356         a->setShortcut(Qt::CTRL + Qt::Key_B );
357     connect( a, SIGNAL( activated() ), this, SLOT( textBold() ) );
358         tb->addAction (a);
359         formatMenu->addAction (a);
360     a->setToggleAction( true );
361     actionTextBold=a;
362         
363     a = new QAction( QPixmap(iconPath+"text_italic.png"), tr( "&Italic" ),  this);
364         a->setShortcut(Qt::CTRL + Qt::Key_I);
365     connect( a, SIGNAL( activated() ), this, SLOT( textItalic() ) );
366         tb->addAction (a);
367         formatMenu->addAction (a);
368     a->setToggleAction( true );
369     actionTextItalic=a;
370         
371     a = new QAction( QPixmap (iconPath+"text_under.png"), tr( "&Underline" ), this);
372         a->setShortcut(Qt::CTRL + Qt::Key_U );
373     connect( a, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
374         tb->addAction (a);
375         formatMenu->addAction (a);
376     a->setToggleAction( true );
377     actionTextUnderline=a;
378     formatMenu->addSeparator();
379
380     QActionGroup *grp2 = new QActionGroup( this );
381     grp2->setExclusive(true);
382     a = new QAction( QPixmap (iconPath+"text_sub.png"), tr( "Subs&cript" ),grp2 );
383         a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B );
384     a->setToggleAction( true );
385         tb->addAction (a);
386         formatMenu->addAction (a);
387     connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
388     actionAlignSubScript=a;
389
390     a = new QAction( QPixmap (iconPath+"text_super.png"), tr( "Su&perscript" ),grp2  );
391         a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_P );
392     a->setToggleAction( true );
393         tb->addAction (a);
394         formatMenu->addAction (a);
395     connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
396     actionAlignSuperScript=a;
397     QActionGroup *grp = new QActionGroup( this );
398     connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
399
400     formatMenu->addSeparator();
401
402     a = new QAction( QPixmap (iconPath+"text_left.png"), tr( "&Left" ),grp );
403         a->setShortcut( Qt::CTRL+Qt::Key_L );
404     a->setToggleAction( true );
405         tb->addAction (a);
406         formatMenu->addAction (a);
407     actionAlignLeft=a;
408     a = new QAction( QPixmap (iconPath+"text_center.png"), tr( "C&enter" ),grp);
409     a->setShortcut(  Qt::CTRL + Qt::Key_E);
410     a->setToggleAction( true );
411         tb->addAction (a);
412         formatMenu->addAction (a);
413     actionAlignCenter=a;
414     a = new QAction( QPixmap (iconPath+"text_right.png" ), tr( "&Right" ), grp);
415         a->setShortcut(Qt::CTRL + Qt::Key_R );
416     a->setToggleAction( true );
417         tb->addAction (a);
418         formatMenu->addAction (a);
419     actionAlignRight=a;
420     a = new QAction( QPixmap ( iconPath+"text_block.png"), tr( "&Justify" ), grp );
421         a->setShortcut(Qt::CTRL + Qt::Key_J );
422     a->setToggleAction( true );
423         tb->addAction (a);
424         formatMenu->addAction (a);
425     actionAlignJustify=a;
426 }
427
428 void TextEditor::setupSettingsActions()
429 {
430     QMenu *settingsMenu = menuBar()->addMenu ( tr( "&Settings" ));
431
432     QAction *a;
433     a = new QAction(tr( "Set &fixed font" ), this);
434         a->setStatusTip ( tr( "Set fixed font","Status tip for note menu" ));
435     connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) );
436         settingsMenu->addAction (a);
437         actionSettingsFixedFont=a;
438
439     a = new QAction(tr( "Set &variable font" ), this);
440         a->setStatusTip ( tr( "Set variable font","Status tip for note menu" ) );
441     connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) );
442         settingsMenu->addAction (a);
443         actionSettingsVarFont=a;
444
445     a = new QAction(tr( "&fixed font is default" ),  this);
446         a->setStatusTip (tr( "Used fixed font by default","Status tip for note menu" ) );
447         a->setToggleAction (true);
448         // set state later in constructor...
449         settingsMenu->addAction (a);
450         actionSettingsFonthintDefault=a;
451 }
452
453 void TextEditor::textLoad()
454 {
455         if (state!=inactiveEditor)
456         {
457                 if (!isEmpty()) 
458                 {
459                         QMessageBox mb( vymName + " - " +tr("Note Editor"),
460                                 "Loading will overwrite the existing note",
461                                 QMessageBox::Warning,
462                                 QMessageBox::Yes | QMessageBox::Default,
463                                 QMessageBox::Cancel,
464                                 0 );
465                         mb.setButtonText( QMessageBox::Yes, "Load note" );
466                         switch( mb.exec() ) {
467                                 case QMessageBox::Cancel:
468                                         return;
469                                         break;
470                         }
471                 } 
472                 // Load note
473                 QFileDialog *fd=new QFileDialog( this);
474                 QStringList types;
475                 types<< "VYM notes (*.html)" <<
476                         "ASCII texts (*.txt)" <<
477                         "All filed (*)";
478                 fd->setFilters (types);
479                 fd->setDirectory (QDir().current());
480                 fd->show();
481                 QString fn;
482                 if ( fd->exec() == QDialog::Accepted )
483                         fn = fd->selectedFile();
484
485                 if ( !fn.isEmpty() )
486                 {
487                         QFile f( fn );
488                         if ( !f.open( QIODevice::ReadOnly ) )
489                         return;
490
491                         QTextStream ts( &f );
492                         setText( ts.read() );
493                         editorChanged();
494                 }
495         }
496 }
497
498 void TextEditor::closeEvent( QCloseEvent* ce )
499 {
500     ce->accept();       // TextEditor can be reopened with show()
501         showwithmain=false;
502         hide();
503         emit (windowClosed() );
504     return;
505 }
506
507 QString TextEditor::getText()
508 {
509         if (e->toPlainText().isEmpty())
510                 return "";
511         else    
512                 return e->text();
513 }
514
515 void TextEditor::editorChanged()
516 {
517         if (isEmpty())
518                 state=emptyEditor;
519         else
520                 state=filledEditor;
521
522                 if (state==emptyEditor)
523                         setState (emptyEditor);
524                 else
525                         setState (filledEditor);
526         // SLOT is LinkableMapObj, which will update systemFlag
527         if (!blockChangedSignal) emit (textHasChanged() );
528 }
529
530
531 void TextEditor::setText(QString t)
532 {
533         blockChangedSignal=true;
534         e->setReadOnly(false);
535         e->setText(t);
536         enableActions();
537         blockChangedSignal=false;
538 }
539
540 void TextEditor::setInactive()
541 {
542         state=inactiveEditor;
543         setText("");
544         setState (inactiveEditor);
545         e->setReadOnly (true);
546
547         disableActions();
548 }
549
550 void TextEditor::editCopyAll()
551 {
552         e->selectAll();
553         e->copy();
554 }
555
556 void TextEditor::textSaveAs()
557 {
558     QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
559                                                this,"export note dialog",tr("Export Note to single file") );
560
561     if ( !fn.isEmpty() ) 
562         {
563                 QFile file (fn);
564                 if (file.exists())
565                 {
566                         QMessageBox mb( vymName,
567                                 tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn),
568                         QMessageBox::Warning,
569                         QMessageBox::Yes | QMessageBox::Default,
570                         QMessageBox::Cancel | QMessageBox::Escape,
571                         Qt::NoButton );
572                         mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
573                         mb.setButtonText( QMessageBox::No, tr("Cancel"));
574                         switch( mb.exec() ) {
575                                 case QMessageBox::Yes:
576                                         // save 
577                                         filename = fn;
578                                         textSave();
579                                         return;
580                                 case QMessageBox::Cancel:
581                                         // do nothing
582                                         break;
583                         }
584                 } else
585                 {
586                         filename = fn;
587                         textSave();
588                         return;
589                 }                       
590     }
591         statusBar()->message(tr( "Couldn't export note ","dialog 'save note as'") + fn, statusbarTime );
592 }
593
594
595 void TextEditor::textSave()
596 {
597     if ( filename.isEmpty() ) 
598         {
599                 textSaveAs();
600                 return;
601     }
602
603     QString text = e->text();
604     QFile f( filename );
605     if ( !f.open( QIODevice::WriteOnly ) ) 
606         {
607                 statusBar()->message( QString("Could not write to %1").arg(filename),
608                                           statusbarTime );
609                 return;
610     }
611
612     QTextStream t( &f );
613     t << text;
614     f.close();
615
616     e->setModified( FALSE );
617
618     statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
619 }
620
621 void TextEditor::textExportAsASCII()
622 {
623         QString text = NoteObj (e->text()).getNoteASCII();
624         QString fn,s;
625         if (!filenameHint.isEmpty())
626         {
627                 if (!filenameHint.contains (".txt"))
628                         s=filenameHint+".txt";
629                 else    
630                         s=filenameHint;
631         } else  
632                 s=QString::null;
633         fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
634         int ret=-1;
635
636     if ( !fn.isEmpty() ) 
637         {
638                 QFile file (fn);
639                 if (file.exists())
640                 {
641                         QMessageBox mb( vymName,
642                                 tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn),
643                         QMessageBox::Warning,
644                         QMessageBox::Yes | QMessageBox::Default,
645                         QMessageBox::Cancel | QMessageBox::Escape,
646                         Qt::NoButton );
647                         mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
648                         mb.setButtonText( QMessageBox::No, tr("Cancel"));
649                         ret=mb.exec();
650                 }       
651                 if (ret==QMessageBox::Cancel)
652                         return;
653                         
654                 // save 
655                 if ( !file.open( QIODevice::WriteOnly ) ) 
656                         statusBar()->message( QString("Could not write to %1").arg(filename),
657                                                   statusbarTime );
658                 else
659                 {
660                         QTextStream t( &file );
661                         t << text;
662                         file.close();
663
664                         statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
665                 }
666     }
667 }
668
669
670 void TextEditor::textPrint()
671 {
672
673 }
674
675 void TextEditor::textEditUndo()
676 {
677 }
678
679 void TextEditor::toggleFonthint()
680 {
681         setUpdatesEnabled (false);
682         e->selectAll ();
683         if (!actionFormatUseFixedFont->isOn() ) 
684                 e->setCurrentFont (varFont);
685         else    
686                 e->setCurrentFont (fixedFont);
687         e->selectAll ();
688         setUpdatesEnabled (true);
689         repaint();
690 }
691
692 void TextEditor::setFixedFont()
693 {
694         bool ok;
695         QFont font =QFontDialog::getFont(
696                     &ok, fixedFont, this );
697     if ( ok ) 
698         // font is set to the font the user selected
699                 fixedFont=font;
700 }
701
702 void TextEditor::setVarFont()
703 {
704         bool ok;
705         QFont font =QFontDialog::getFont(
706                     &ok, varFont, this );
707     if ( ok ) 
708         // font is set to the font the user selected
709                 varFont=font;
710 }
711
712 void TextEditor::textBold()
713 {
714     e->setBold( actionTextBold->isOn() );
715 }
716
717 void TextEditor::textUnderline()
718 {
719     e->setUnderline( actionTextUnderline->isOn() );
720 }
721
722 void TextEditor::textItalic()
723 {
724     e->setItalic( actionTextItalic->isOn() );
725 }
726
727 void TextEditor::textFamily( const QString &f )
728 {
729     e->setFamily( f );
730 }
731
732 void TextEditor::textSize( const QString &p )
733 {
734     e->setPointSize( p.toInt() );
735 }
736
737
738 void TextEditor::textColor()
739 {
740     QColor col = QColorDialog::getColor( e->color(), this );
741     if ( !col.isValid() ) return;
742     e->setColor( col );
743     QPixmap pix( 16, 16 );
744     pix.fill( Qt::black );
745     actionTextColor->setIconSet( pix );
746 }
747
748 void TextEditor::textAlign( QAction *a )
749 {
750     if ( a == actionAlignLeft )
751                 e->setAlignment( Qt::AlignLeft );
752     else if ( a == actionAlignCenter )
753                 e->setAlignment( Qt::AlignHCenter );
754     else if ( a == actionAlignRight )
755                 e->setAlignment( Qt::AlignRight );
756     else if ( a == actionAlignJustify )
757                 e->setAlignment( Qt::AlignJustify );
758 }
759
760 void TextEditor::textVAlign()
761 {
762         QTextCharFormat format;
763
764     if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) {
765                 format.setVerticalAlignment(QTextCharFormat::AlignSuperScript);
766     } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) {
767                 format.setVerticalAlignment(QTextCharFormat::AlignSubScript);
768     } else {
769                 format.setVerticalAlignment(QTextCharFormat::AlignNormal);
770     }
771         e->mergeCurrentCharFormat(format);
772 }
773
774
775 void TextEditor::fontChanged( const QFont &f )
776 {
777         int i=comboFont->findText(f.family());
778     if (i>=0) comboFont->setCurrentIndex (i);
779         i=comboSize->findText(QString::number(f.pointSize()));
780         if (i>=0) comboSize->setCurrentIndex(i);
781     actionTextBold->setOn( f.bold() );
782     actionTextItalic->setOn( f.italic() );
783     actionTextUnderline->setOn( f.underline() );
784 }
785
786 void TextEditor::colorChanged( const QColor &c )
787 {
788     QPixmap pix( 16, 16 );
789     pix.fill( c );
790     actionTextColor->setIconSet( pix );
791 }
792
793 void TextEditor::formatChanged( const QTextCharFormat &f )
794 {
795         fontChanged(f.font());
796     colorChanged(f.foreground().color());
797     alignmentChanged(e->alignment());
798         verticalAlignmentChanged (f.verticalAlignment());
799 }
800
801 void TextEditor::alignmentChanged( int a )
802 {
803     if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft ))
804                 actionAlignLeft->setOn( true );
805     else if ( ( a & Qt::AlignHCenter ) )
806                 actionAlignCenter->setOn( true );
807     else if ( ( a & Qt::AlignRight ) )
808                 actionAlignRight->setOn( true );
809     else if ( ( a & Qt::AlignJustify ) )
810                 actionAlignJustify->setOn( true );
811 }
812
813 void TextEditor::verticalAlignmentChanged(QTextCharFormat::VerticalAlignment a)
814 {
815         actionAlignSubScript->setOn (false);
816         actionAlignSuperScript->setOn (false);
817         switch (a)
818         {
819                 case QTextCharFormat::AlignSuperScript: 
820                         actionAlignSuperScript->setOn (true);
821                         break;
822                 case QTextCharFormat::AlignSubScript:
823                         actionAlignSubScript->setOn (true);
824                         break;
825                 default: ;      
826         }
827 }
828
829
830
831 void TextEditor::enableActions()
832 {
833         actionFileLoad->setEnabled(true);
834         actionFileSave->setEnabled(true);
835         actionFileSaveAs->setEnabled(true);
836         //actionFilePrint->setEnabled(true);
837         actionEditUndo->setEnabled(true);
838         actionEditRedo->setEnabled(true);
839         actionEditCopy->setEnabled(true);
840         actionEditCut->setEnabled(true);
841         actionEditPaste->setEnabled(true);
842         actionEditDeleteAll->setEnabled(true);
843         actionFormatUseFixedFont->setEnabled(true);
844 }
845
846 void TextEditor::disableActions()
847 {
848         actionFileLoad->setEnabled(false);
849         actionFileSave->setEnabled(false);
850         actionFileSaveAs->setEnabled(false);
851         //actionFilePrint->setEnabled(false);
852         actionEditUndo->setEnabled(false);
853         actionEditRedo->setEnabled(false);
854         actionEditCopy->setEnabled(false);
855         actionEditCut->setEnabled(false);
856         actionEditPaste->setEnabled(false);
857         actionEditDeleteAll->setEnabled(false);
858         actionFormatUseFixedFont->setEnabled(false);
859 }
860
861 void TextEditor::setState (EditorState s)
862 {
863         
864         QPalette p=palette();
865         QColor c;
866         switch (s)
867         {
868                 case emptyEditor:    c=QColor (150,150,150); break;
869                 case filledEditor:   c=QColor (255,255,255); break;
870                 case inactiveEditor: c=QColor (0,0,0);
871         }
872     p.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(9), c);
873     p.setColor(QPalette::Inactive, static_cast<QPalette::ColorRole>(9), c);
874     e->setPalette(p);
875 }
876
877