insilmaril@0: #include "texteditor.h" insilmaril@0: insilmaril@0: #include insilmaril@53: #include insilmaril@0: #include insilmaril@0: insilmaril@53: #include "noteobj.h" insilmaril@53: #include "settings.h" insilmaril@0: insilmaril@0: extern int statusbarTime; insilmaril@53: extern Settings settings; insilmaril@53: insilmaril@53: extern QAction *actionViewToggleNoteEditor; insilmaril@53: insilmaril@53: extern QString iconPath; insilmaril@53: extern QString vymName; insilmaril@0: insilmaril@0: using namespace std; insilmaril@0: insilmaril@0: insilmaril@0: /////////////////////////////////////////////////////////////////////// insilmaril@0: /////////////////////////////////////////////////////////////////////// insilmaril@0: insilmaril@53: insilmaril@0: TextEditor::TextEditor() insilmaril@0: { insilmaril@0: printer = new QPrinter( QPrinter::HighResolution ); insilmaril@53: printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString()); insilmaril@0: insilmaril@53: e = new QTextEdit( this); insilmaril@0: e->setFocus(); insilmaril@53: e->setTextFormat(Qt::RichText); // default insilmaril@0: e->setTabStopWidth (20); // unit is pixel insilmaril@53: e->setColor (Qt::black); insilmaril@53: e->setAutoFillBackground (true); insilmaril@0: connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) ); insilmaril@0: setCentralWidget( e ); insilmaril@53: statusBar()->message( tr("Ready","Statusbar message"), statusbarTime); insilmaril@53: setCaption(vymName +" - " +tr ("Note Editor","Window caption")); insilmaril@53: insilmaril@53: insilmaril@53: connect(e, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)), insilmaril@53: this, SLOT(formatChanged(const QTextCharFormat &))); insilmaril@53: insilmaril@53: insilmaril@0: insilmaril@0: // Toolbars insilmaril@0: setupFileActions(); insilmaril@0: setupEditActions(); insilmaril@0: setupFormatActions(); insilmaril@0: setupSettingsActions(); insilmaril@0: insilmaril@0: // Various states insilmaril@53: blockChangedSignal=false; insilmaril@0: setInactive(); insilmaril@0: insilmaril@0: // Load Settings insilmaril@53: resize (settings.value ( "/satellite/noteeditor/geometry/size", QSize(450,600)).toSize()); insilmaril@53: move (settings.value ( "/satellite/noteeditor/geometry/pos", QPoint (250,50)).toPoint()); insilmaril@0: insilmaril@53: setShowWithMain (settings.value ( "/satellite/noteeditor/showWithMain",true).toBool()); insilmaril@0: insilmaril@53: varFont.fromString( settings.value insilmaril@53: ("/satellite/noteeditor/fonts/varFont", insilmaril@53: "Nimbus Sans l,14,-1,5,48,0,0,0,0,0").toString() insilmaril@0: ); insilmaril@53: fixedFont.fromString (settings.value( insilmaril@53: "/satellite/noteeditor/fonts/fixedFont", insilmaril@53: "Courier,14,-1,5,48,0,0,0,1,0").toString() insilmaril@0: ); insilmaril@53: QString s=settings.value ("/satellite/noteeditor/fonts/fonthintDefault","variable").toString(); insilmaril@0: if (s == "fixed") insilmaril@0: { insilmaril@0: actionSettingsFonthintDefault->setOn (true); insilmaril@0: e->setCurrentFont (fixedFont); insilmaril@0: } else insilmaril@0: { insilmaril@0: actionSettingsFonthintDefault->setOn (false); insilmaril@0: e->setCurrentFont (varFont); insilmaril@0: } insilmaril@53: filenameHint=""; insilmaril@53: insilmaril@53: // Restore position of toolbars insilmaril@53: restoreState (settings.value("/satellite/noteeditor/state",0).toByteArray()); insilmaril@53: insilmaril@53: // Save settings in vymrc insilmaril@53: settings.setValue("/mainwindow/printerName",printer->printerName()); insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: TextEditor::~TextEditor() insilmaril@0: { insilmaril@0: if (printer) delete printer; insilmaril@0: // Save Settings insilmaril@53: settings.setValue( "/satellite/noteeditor/geometry/size", size() ); insilmaril@53: settings.setValue( "/satellite/noteeditor/geometry/pos", pos() ); insilmaril@53: settings.setValue ("/satellite/noteeditor/state",saveState(0)); insilmaril@0: insilmaril@53: settings.setValue( "/satellite/noteeditor/showWithMain",showwithmain); insilmaril@0: insilmaril@0: QString s; insilmaril@0: if (actionSettingsFonthintDefault->isOn() ) insilmaril@0: s="fixed"; insilmaril@0: else insilmaril@0: s="variable"; insilmaril@53: settings.setValue( "/satellite/noteeditor/fonts/fonthintDefault",s ); insilmaril@53: settings.setValue("/satellite/noteeditor/fonts/varFont", varFont.toString() ); insilmaril@53: settings.setValue("/satellite/noteeditor/fonts/fixedFont", fixedFont.toString() ); insilmaril@53: insilmaril@53: insilmaril@0: } insilmaril@0: insilmaril@0: bool TextEditor::isEmpty() insilmaril@0: { insilmaril@53: if (e->toPlainText().length()>0) insilmaril@0: return false; insilmaril@0: else insilmaril@0: return true; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setShowWithMain(bool v) insilmaril@0: { insilmaril@0: showwithmain=v; insilmaril@0: } insilmaril@0: insilmaril@0: bool TextEditor::showWithMain() insilmaril@0: { insilmaril@0: return showwithmain; insilmaril@0: } insilmaril@0: insilmaril@53: insilmaril@0: void TextEditor::setFontHint (const QString &fh) insilmaril@0: { insilmaril@0: if (fh=="fixed") insilmaril@0: actionFormatUseFixedFont->setOn (true); insilmaril@0: else insilmaril@0: actionFormatUseFixedFont->setOn (false); insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: QString TextEditor::getFontHint() insilmaril@0: { insilmaril@0: if (actionFormatUseFixedFont->isOn()) insilmaril@0: return "fixed"; insilmaril@0: else insilmaril@0: return "var"; insilmaril@0: } insilmaril@0: insilmaril@0: QString TextEditor::getFontHintDefault() insilmaril@0: { insilmaril@0: if (actionSettingsFonthintDefault->isOn()) insilmaril@0: return "fixed"; insilmaril@0: else insilmaril@0: return "var"; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setFilename(const QString &fn) insilmaril@0: { insilmaril@0: if (state==filledEditor) insilmaril@53: { insilmaril@0: if (fn.isEmpty() ) insilmaril@0: { insilmaril@0: filename=""; insilmaril@53: statusBar()->message( tr("No filename available for this note.","Statusbar message"), statusbarTime ); insilmaril@0: } insilmaril@0: else insilmaril@0: { insilmaril@0: filename=fn; insilmaril@53: statusBar()->message( tr(QString( "Current filename is %1" ).arg( filename ),"Statusbar message"), statusbarTime ); insilmaril@0: } insilmaril@53: } insilmaril@0: } insilmaril@0: insilmaril@0: QString TextEditor::getFilename() insilmaril@0: { insilmaril@0: return filename; insilmaril@0: } insilmaril@0: insilmaril@53: void TextEditor::setFilenameHint(const QString &fnh) insilmaril@0: { insilmaril@53: filenameHint=fnh; insilmaril@53: } insilmaril@53: insilmaril@53: QString TextEditor::getFilenameHint() insilmaril@53: { insilmaril@53: return filenameHint; insilmaril@53: } insilmaril@53: insilmaril@53: bool TextEditor::findText(const QString &t, const QTextDocument::FindFlags &flags) insilmaril@53: { insilmaril@53: if (e->find (t,flags)) insilmaril@0: return true; insilmaril@0: else insilmaril@0: return false; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupFileActions() insilmaril@0: { insilmaril@53: QToolBar *tb = addToolBar ( tr("Note Actions") ); insilmaril@53: tb->setObjectName ("noteEditorFileActions"); insilmaril@53: QMenu *fileMenu = menuBar()->addMenu( tr( "&Note","Menubar" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@53: a = new QAction( QPixmap( iconPath+"fileopen.png"), tr( "&Import..." ),this); insilmaril@53: a->setStatusTip (tr( "Import","Status tip for Note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_O ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) ); insilmaril@53: tb->addAction (a); insilmaril@53: fileMenu->addAction (a); insilmaril@0: actionFileLoad=a; insilmaril@0: insilmaril@53: fileMenu->addSeparator(); insilmaril@53: a = new QAction( QPixmap(iconPath+"filesave.png" ), tr( "&Export..." ),this); insilmaril@53: a->setStatusTip (tr( "Export Note (HTML)","Status tip for Note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_S ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) ); insilmaril@53: tb->addAction (a); insilmaril@53: fileMenu->addAction (a); insilmaril@0: actionFileSave=a; insilmaril@0: insilmaril@53: a = new QAction( QPixmap(), tr( "Export &As... (HTML)" ), this); insilmaril@53: a->setStatusTip (tr( "Export Note As (HTML) ","Status tip for Note Menu" )); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) ); insilmaril@53: fileMenu->addAction (a); insilmaril@0: actionFileSaveAs=a; insilmaril@0: insilmaril@53: a = new QAction(QPixmap(), tr( "Export &As...(ASCII)" ), this); insilmaril@53: a->setStatusTip ( tr( "Export Note As (ASCII) ","Status tip for note menu" ) ); insilmaril@53: a->setShortcut(Qt::ALT + Qt::Key_X ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) ); insilmaril@53: fileMenu->addAction (a); insilmaril@0: actionFileSaveAs=a; insilmaril@0: insilmaril@53: fileMenu->addSeparator(); insilmaril@53: a = new QAction( QPixmap(iconPath+"fileprint.png" ), tr( "&Print..." ),this); insilmaril@53: a->setStatusTip (tr( "Print Note","Status tip for note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_P ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) ); insilmaril@53: tb->addAction (a); insilmaril@53: fileMenu->addAction (a); insilmaril@0: actionFilePrint=a; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupEditActions() insilmaril@0: { insilmaril@53: QToolBar *tb = addToolBar ( tr( "Edit Actions" )); insilmaril@53: tb->setObjectName ("noteEditorEditActions"); insilmaril@53: QMenu *editMenu = menuBar()->addMenu ( tr( "&Edit" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@53: a = new QAction(QPixmap(iconPath+"undo.png"), tr( "&Undo" ), this ); insilmaril@53: a->setStatusTip ( tr( "Undo","Status tip for note menu" ) ); insilmaril@53: a->setShortcut(Qt::CTRL + Qt::Key_Z ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( undo() ) ); insilmaril@53: editMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@0: actionEditUndo=a; insilmaril@0: insilmaril@53: a = new QAction(QPixmap(iconPath+"redo.png" ), tr( "&Redo" ),this); insilmaril@53: a->setStatusTip ( tr( "Redo","Status tip for note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_Y ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( redo() ) ); insilmaril@53: editMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@0: actionEditRedo=a; insilmaril@0: insilmaril@53: editMenu->addSeparator(); insilmaril@53: a = new QAction(QPixmap(), tr( "Select and copy &all" ),this); insilmaril@53: a->setStatusTip ( tr( "Select and copy all","Status tip for note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_A ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) ); insilmaril@53: editMenu->addAction (a); insilmaril@0: insilmaril@53: editMenu->addSeparator(); insilmaril@53: a = new QAction(QPixmap(iconPath+"editcopy.png" ), tr( "&Copy" ),this); insilmaril@53: a->setStatusTip ( tr( "Copy","Status tip for note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_C ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( copy() ) ); insilmaril@53: editMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@0: actionEditCopy=a; insilmaril@0: insilmaril@53: a = new QAction(QPixmap(iconPath+"editcut.png" ), tr( "Cu&t" ),this); insilmaril@53: a->setStatusTip ( tr( "Cut","Status tip for note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_X ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( cut() ) ); insilmaril@53: editMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@0: actionEditCut=a; insilmaril@0: insilmaril@53: a = new QAction(QPixmap(iconPath+"editpaste.png" ), tr( "&Paste" ),this); insilmaril@53: a->setStatusTip ( tr( "Paste","Status tip for note menu" ) ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_V ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( paste() ) ); insilmaril@53: editMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@0: actionEditPaste=a; insilmaril@0: insilmaril@53: a = new QAction( QPixmap( iconPath+"edittrash.png"), tr( "&Delete All" ), this); insilmaril@53: a->setStatusTip (tr( "Delete all","Status tip for note menu" ) ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( clear() ) ); insilmaril@53: editMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@0: actionEditDeleteAll=a; insilmaril@0: insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupFormatActions() insilmaril@0: { insilmaril@53: QToolBar *tb = addToolBar ( tr("Format Actions" )); insilmaril@53: tb->setObjectName ("noteEditorFormatActions"); insilmaril@53: QMenu *formatMenu = menuBar()->addMenu ( tr( "F&ormat" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@0: insilmaril@53: a = new QAction( QPixmap(iconPath+"formatfixedfont.png"), tr( "&Font hint" ), Qt::ALT + Qt::Key_I, this, "fontHint" ); insilmaril@53: a->setStatusTip (tr( "Toggle font hint for the whole text","Status tip for note menu" ) ); insilmaril@0: a->setToggleAction (true); insilmaril@53: a->setOn (settings.value("/noteeditor/fonts/useFixedByDefault",false).toBool() ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) ); insilmaril@53: formatMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@0: actionFormatUseFixedFont=a; insilmaril@0: insilmaril@53: comboFont = new QComboBox; insilmaril@53: tb->addWidget (comboFont); insilmaril@53: QFontDatabase fontDB; insilmaril@53: comboFont->insertStringList( fontDB.families() ); insilmaril@0: connect( comboFont, SIGNAL( activated( const QString & ) ), insilmaril@0: this, SLOT( textFamily( const QString & ) ) ); insilmaril@53: comboFont->addItem( QApplication::font().family() ); insilmaril@53: comboSize = new QComboBox; insilmaril@53: tb->addWidget (comboSize); insilmaril@53: QList sizes=fontDB.standardSizes(); insilmaril@53: QList::iterator i = sizes.begin(); insilmaril@53: while (i != sizes.end()) insilmaril@53: { insilmaril@53: ++i; // increment i before using it insilmaril@53: comboSize->insertItem ( QString::number(*i)); insilmaril@53: } insilmaril@0: connect( comboSize, SIGNAL( activated( const QString & ) ), insilmaril@0: this, SLOT( textSize( const QString & ) ) ); insilmaril@53: comboSize->addItem ( QString::number( QApplication::font().pointSize() ) ); insilmaril@0: insilmaril@53: formatMenu->addSeparator(); insilmaril@0: insilmaril@0: QPixmap pix( 16, 16 ); insilmaril@0: pix.fill( e->color()); insilmaril@53: a = new QAction( pix, tr( "&Color..." ), this); insilmaril@53: formatMenu->addAction (a); insilmaril@53: tb->addAction (a); insilmaril@53: connect( a, SIGNAL( activated() ), this, SLOT( textColor() ) ); insilmaril@53: actionTextColor=a; insilmaril@0: insilmaril@53: a = new QAction( QPixmap (iconPath+"text_bold.png"), tr( "&Bold" ), this); insilmaril@53: a->setShortcut(Qt::CTRL + Qt::Key_B ); insilmaril@53: connect( a, SIGNAL( activated() ), this, SLOT( textBold() ) ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: a->setToggleAction( true ); insilmaril@53: actionTextBold=a; insilmaril@53: insilmaril@53: a = new QAction( QPixmap(iconPath+"text_italic.png"), tr( "&Italic" ), this); insilmaril@53: a->setShortcut(Qt::CTRL + Qt::Key_I); insilmaril@53: connect( a, SIGNAL( activated() ), this, SLOT( textItalic() ) ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: a->setToggleAction( true ); insilmaril@53: actionTextItalic=a; insilmaril@53: insilmaril@53: a = new QAction( QPixmap (iconPath+"text_under.png"), tr( "&Underline" ), this); insilmaril@53: a->setShortcut(Qt::CTRL + Qt::Key_U ); insilmaril@53: connect( a, SIGNAL( activated() ), this, SLOT( textUnderline() ) ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: a->setToggleAction( true ); insilmaril@53: actionTextUnderline=a; insilmaril@53: formatMenu->addSeparator(); insilmaril@0: insilmaril@53: QActionGroup *grp2 = new QActionGroup( this ); insilmaril@53: grp2->setExclusive(true); insilmaril@53: a = new QAction( QPixmap (iconPath+"text_sub.png"), tr( "Subs&cript" ),grp2 ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B ); insilmaril@53: a->setToggleAction( true ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: connect(a, SIGNAL(activated()), this, SLOT(textVAlign())); insilmaril@53: actionAlignSubScript=a; insilmaril@53: insilmaril@53: a = new QAction( QPixmap (iconPath+"text_super.png"), tr( "Su&perscript" ),grp2 ); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_P ); insilmaril@53: a->setToggleAction( true ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: connect(a, SIGNAL(activated()), this, SLOT(textVAlign())); insilmaril@53: actionAlignSuperScript=a; insilmaril@0: QActionGroup *grp = new QActionGroup( this ); insilmaril@0: connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) ); insilmaril@0: insilmaril@53: formatMenu->addSeparator(); insilmaril@0: insilmaril@53: a = new QAction( QPixmap (iconPath+"text_left.png"), tr( "&Left" ),grp ); insilmaril@53: a->setShortcut( Qt::CTRL+Qt::Key_L ); insilmaril@53: a->setToggleAction( true ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: actionAlignLeft=a; insilmaril@53: a = new QAction( QPixmap (iconPath+"text_center.png"), tr( "C&enter" ),grp); insilmaril@53: a->setShortcut( Qt::CTRL + Qt::Key_E); insilmaril@53: a->setToggleAction( true ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: actionAlignCenter=a; insilmaril@53: a = new QAction( QPixmap (iconPath+"text_right.png" ), tr( "&Right" ), grp); insilmaril@53: a->setShortcut(Qt::CTRL + Qt::Key_R ); insilmaril@53: a->setToggleAction( true ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: actionAlignRight=a; insilmaril@53: a = new QAction( QPixmap ( iconPath+"text_block.png"), tr( "&Justify" ), grp ); insilmaril@53: a->setShortcut(Qt::CTRL + Qt::Key_J ); insilmaril@53: a->setToggleAction( true ); insilmaril@53: tb->addAction (a); insilmaril@53: formatMenu->addAction (a); insilmaril@53: actionAlignJustify=a; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupSettingsActions() insilmaril@0: { insilmaril@53: QMenu *settingsMenu = menuBar()->addMenu ( tr( "&Settings" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@53: a = new QAction(tr( "Set &fixed font" ), this); insilmaril@53: a->setStatusTip ( tr( "Set fixed font","Status tip for note menu" )); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) ); insilmaril@53: settingsMenu->addAction (a); insilmaril@0: actionSettingsFixedFont=a; insilmaril@0: insilmaril@53: a = new QAction(tr( "Set &variable font" ), this); insilmaril@53: a->setStatusTip ( tr( "Set variable font","Status tip for note menu" ) ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) ); insilmaril@53: settingsMenu->addAction (a); insilmaril@0: actionSettingsVarFont=a; insilmaril@0: insilmaril@53: a = new QAction(tr( "&fixed font is default" ), this); insilmaril@53: a->setStatusTip (tr( "Used fixed font by default","Status tip for note menu" ) ); insilmaril@0: a->setToggleAction (true); insilmaril@0: // set state later in constructor... insilmaril@53: settingsMenu->addAction (a); insilmaril@0: actionSettingsFonthintDefault=a; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textLoad() insilmaril@0: { insilmaril@0: if (state!=inactiveEditor) insilmaril@0: { insilmaril@53: if (!isEmpty()) insilmaril@0: { insilmaril@53: QMessageBox mb( vymName + " - " +tr("Note Editor"), insilmaril@0: "Loading will overwrite the existing note", insilmaril@0: QMessageBox::Warning, insilmaril@0: QMessageBox::Yes | QMessageBox::Default, insilmaril@0: QMessageBox::Cancel, insilmaril@0: 0 ); insilmaril@0: mb.setButtonText( QMessageBox::Yes, "Load note" ); insilmaril@0: switch( mb.exec() ) { insilmaril@0: case QMessageBox::Cancel: insilmaril@0: return; insilmaril@0: break; insilmaril@0: } insilmaril@0: } insilmaril@0: // Load note insilmaril@0: QFileDialog *fd=new QFileDialog( this); insilmaril@53: QStringList types; insilmaril@53: types<< "VYM notes (*.html)" << insilmaril@53: "ASCII texts (*.txt)" << insilmaril@53: "All filed (*)"; insilmaril@53: fd->setFilters (types); insilmaril@53: fd->setDirectory (QDir().current()); insilmaril@0: fd->show(); insilmaril@0: QString fn; insilmaril@0: if ( fd->exec() == QDialog::Accepted ) insilmaril@0: fn = fd->selectedFile(); insilmaril@0: insilmaril@0: if ( !fn.isEmpty() ) insilmaril@0: { insilmaril@0: QFile f( fn ); insilmaril@53: if ( !f.open( QIODevice::ReadOnly ) ) insilmaril@0: return; insilmaril@0: insilmaril@0: QTextStream ts( &f ); insilmaril@0: setText( ts.read() ); insilmaril@0: editorChanged(); insilmaril@0: } insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::closeEvent( QCloseEvent* ce ) insilmaril@0: { insilmaril@53: ce->accept(); // TextEditor can be reopened with show() insilmaril@53: showwithmain=false; insilmaril@53: hide(); insilmaril@53: emit (windowClosed() ); insilmaril@53: return; insilmaril@0: } insilmaril@0: insilmaril@0: QString TextEditor::getText() insilmaril@0: { insilmaril@53: if (e->toPlainText().isEmpty()) insilmaril@53: return ""; insilmaril@53: else insilmaril@53: return e->text(); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::editorChanged() insilmaril@0: { insilmaril@0: if (isEmpty()) insilmaril@0: state=emptyEditor; insilmaril@0: else insilmaril@0: state=filledEditor; insilmaril@0: insilmaril@0: if (state==emptyEditor) insilmaril@53: setState (emptyEditor); insilmaril@0: else insilmaril@53: setState (filledEditor); insilmaril@0: // SLOT is LinkableMapObj, which will update systemFlag insilmaril@53: if (!blockChangedSignal) emit (textHasChanged() ); insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: void TextEditor::setText(QString t) insilmaril@0: { insilmaril@53: blockChangedSignal=true; insilmaril@0: e->setReadOnly(false); insilmaril@0: e->setText(t); insilmaril@0: enableActions(); insilmaril@53: blockChangedSignal=false; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setInactive() insilmaril@0: { insilmaril@53: state=inactiveEditor; insilmaril@0: setText(""); insilmaril@53: setState (inactiveEditor); insilmaril@0: e->setReadOnly (true); insilmaril@0: insilmaril@0: disableActions(); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::editCopyAll() insilmaril@0: { insilmaril@0: e->selectAll(); insilmaril@0: e->copy(); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textSaveAs() insilmaril@0: { insilmaril@0: QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)", insilmaril@0: this,"export note dialog",tr("Export Note to single file") ); insilmaril@0: insilmaril@0: if ( !fn.isEmpty() ) insilmaril@0: { insilmaril@0: QFile file (fn); insilmaril@0: if (file.exists()) insilmaril@0: { insilmaril@53: QMessageBox mb( vymName, insilmaril@53: tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn), insilmaril@0: QMessageBox::Warning, insilmaril@0: QMessageBox::Yes | QMessageBox::Default, insilmaril@0: QMessageBox::Cancel | QMessageBox::Escape, insilmaril@53: Qt::NoButton ); insilmaril@0: mb.setButtonText( QMessageBox::Yes, tr("Overwrite") ); insilmaril@0: mb.setButtonText( QMessageBox::No, tr("Cancel")); insilmaril@0: switch( mb.exec() ) { insilmaril@0: case QMessageBox::Yes: insilmaril@0: // save insilmaril@0: filename = fn; insilmaril@0: textSave(); insilmaril@0: return; insilmaril@0: case QMessageBox::Cancel: insilmaril@0: // do nothing insilmaril@0: break; insilmaril@0: } insilmaril@0: } else insilmaril@0: { insilmaril@0: filename = fn; insilmaril@0: textSave(); insilmaril@0: return; insilmaril@0: } insilmaril@0: } insilmaril@53: statusBar()->message(tr( "Couldn't export note ","dialog 'save note as'") + fn, statusbarTime ); insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: void TextEditor::textSave() insilmaril@0: { insilmaril@0: if ( filename.isEmpty() ) insilmaril@0: { insilmaril@0: textSaveAs(); insilmaril@0: return; insilmaril@0: } insilmaril@0: insilmaril@0: QString text = e->text(); insilmaril@0: QFile f( filename ); insilmaril@53: if ( !f.open( QIODevice::WriteOnly ) ) insilmaril@0: { insilmaril@0: statusBar()->message( QString("Could not write to %1").arg(filename), insilmaril@0: statusbarTime ); insilmaril@0: return; insilmaril@0: } insilmaril@0: insilmaril@0: QTextStream t( &f ); insilmaril@0: t << text; insilmaril@0: f.close(); insilmaril@0: insilmaril@0: e->setModified( FALSE ); insilmaril@0: insilmaril@0: statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime ); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textExportAsASCII() insilmaril@0: { insilmaril@53: QString text = NoteObj (e->text()).getNoteASCII(); insilmaril@53: QString fn,s; insilmaril@53: if (!filenameHint.isEmpty()) insilmaril@53: { insilmaril@53: if (!filenameHint.contains (".txt")) insilmaril@53: s=filenameHint+".txt"; insilmaril@53: else insilmaril@53: s=filenameHint; insilmaril@53: } else insilmaril@53: s=QString::null; insilmaril@53: fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") ); insilmaril@0: int ret=-1; insilmaril@0: insilmaril@0: if ( !fn.isEmpty() ) insilmaril@0: { insilmaril@0: QFile file (fn); insilmaril@0: if (file.exists()) insilmaril@0: { insilmaril@53: QMessageBox mb( vymName, insilmaril@53: tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn), insilmaril@0: QMessageBox::Warning, insilmaril@0: QMessageBox::Yes | QMessageBox::Default, insilmaril@0: QMessageBox::Cancel | QMessageBox::Escape, insilmaril@53: Qt::NoButton ); insilmaril@0: mb.setButtonText( QMessageBox::Yes, tr("Overwrite") ); insilmaril@0: mb.setButtonText( QMessageBox::No, tr("Cancel")); insilmaril@0: ret=mb.exec(); insilmaril@0: } insilmaril@0: if (ret==QMessageBox::Cancel) insilmaril@0: return; insilmaril@0: insilmaril@0: // save insilmaril@53: if ( !file.open( QIODevice::WriteOnly ) ) insilmaril@0: statusBar()->message( QString("Could not write to %1").arg(filename), insilmaril@0: statusbarTime ); insilmaril@0: else insilmaril@0: { insilmaril@0: QTextStream t( &file ); insilmaril@0: t << text; insilmaril@0: file.close(); insilmaril@0: insilmaril@0: statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime ); insilmaril@0: } insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: void TextEditor::textPrint() insilmaril@0: { insilmaril@0: insilmaril@53: QTextDocument *document = e->document(); insilmaril@53: QPrinter printer; insilmaril@53: insilmaril@53: QPrintDialog *dialog = new QPrintDialog(&printer, this); insilmaril@53: dialog->setWindowTitle(tr("Print Note")); insilmaril@53: if (dialog->exec() != QDialog::Accepted) insilmaril@53: return; insilmaril@53: insilmaril@53: document->print(&printer); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textEditUndo() insilmaril@0: { insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::toggleFonthint() insilmaril@0: { insilmaril@0: setUpdatesEnabled (false); insilmaril@53: e->selectAll (); insilmaril@0: if (!actionFormatUseFixedFont->isOn() ) insilmaril@0: e->setCurrentFont (varFont); insilmaril@0: else insilmaril@0: e->setCurrentFont (fixedFont); insilmaril@53: e->selectAll (); insilmaril@0: setUpdatesEnabled (true); insilmaril@0: repaint(); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setFixedFont() insilmaril@0: { insilmaril@0: bool ok; insilmaril@0: QFont font =QFontDialog::getFont( insilmaril@0: &ok, fixedFont, this ); insilmaril@0: if ( ok ) insilmaril@0: // font is set to the font the user selected insilmaril@0: fixedFont=font; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setVarFont() insilmaril@0: { insilmaril@0: bool ok; insilmaril@0: QFont font =QFontDialog::getFont( insilmaril@0: &ok, varFont, this ); insilmaril@0: if ( ok ) insilmaril@0: // font is set to the font the user selected insilmaril@0: varFont=font; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textBold() insilmaril@0: { insilmaril@0: e->setBold( actionTextBold->isOn() ); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textUnderline() insilmaril@0: { insilmaril@0: e->setUnderline( actionTextUnderline->isOn() ); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textItalic() insilmaril@0: { insilmaril@0: e->setItalic( actionTextItalic->isOn() ); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textFamily( const QString &f ) insilmaril@0: { insilmaril@0: e->setFamily( f ); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textSize( const QString &p ) insilmaril@0: { insilmaril@0: e->setPointSize( p.toInt() ); insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: void TextEditor::textColor() insilmaril@0: { insilmaril@0: QColor col = QColorDialog::getColor( e->color(), this ); insilmaril@53: if ( !col.isValid() ) return; insilmaril@0: e->setColor( col ); insilmaril@0: QPixmap pix( 16, 16 ); insilmaril@53: pix.fill( Qt::black ); insilmaril@0: actionTextColor->setIconSet( pix ); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textAlign( QAction *a ) insilmaril@0: { insilmaril@0: if ( a == actionAlignLeft ) insilmaril@53: e->setAlignment( Qt::AlignLeft ); insilmaril@0: else if ( a == actionAlignCenter ) insilmaril@53: e->setAlignment( Qt::AlignHCenter ); insilmaril@0: else if ( a == actionAlignRight ) insilmaril@53: e->setAlignment( Qt::AlignRight ); insilmaril@0: else if ( a == actionAlignJustify ) insilmaril@53: e->setAlignment( Qt::AlignJustify ); insilmaril@0: } insilmaril@0: insilmaril@53: void TextEditor::textVAlign() insilmaril@53: { insilmaril@53: QTextCharFormat format; insilmaril@53: insilmaril@53: if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) { insilmaril@53: format.setVerticalAlignment(QTextCharFormat::AlignSuperScript); insilmaril@53: } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) { insilmaril@53: format.setVerticalAlignment(QTextCharFormat::AlignSubScript); insilmaril@53: } else { insilmaril@53: format.setVerticalAlignment(QTextCharFormat::AlignNormal); insilmaril@53: } insilmaril@53: e->mergeCurrentCharFormat(format); insilmaril@53: } insilmaril@53: insilmaril@53: insilmaril@0: void TextEditor::fontChanged( const QFont &f ) insilmaril@0: { insilmaril@53: int i=comboFont->findText(f.family()); insilmaril@53: if (i>=0) comboFont->setCurrentIndex (i); insilmaril@53: i=comboSize->findText(QString::number(f.pointSize())); insilmaril@53: if (i>=0) comboSize->setCurrentIndex(i); insilmaril@0: actionTextBold->setOn( f.bold() ); insilmaril@0: actionTextItalic->setOn( f.italic() ); insilmaril@0: actionTextUnderline->setOn( f.underline() ); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::colorChanged( const QColor &c ) insilmaril@0: { insilmaril@0: QPixmap pix( 16, 16 ); insilmaril@0: pix.fill( c ); insilmaril@0: actionTextColor->setIconSet( pix ); insilmaril@0: } insilmaril@0: insilmaril@53: void TextEditor::formatChanged( const QTextCharFormat &f ) insilmaril@53: { insilmaril@53: fontChanged(f.font()); insilmaril@53: colorChanged(f.foreground().color()); insilmaril@53: alignmentChanged(e->alignment()); insilmaril@53: verticalAlignmentChanged (f.verticalAlignment()); insilmaril@53: } insilmaril@53: insilmaril@0: void TextEditor::alignmentChanged( int a ) insilmaril@0: { insilmaril@53: if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft )) insilmaril@53: actionAlignLeft->setOn( true ); insilmaril@53: else if ( ( a & Qt::AlignHCenter ) ) insilmaril@53: actionAlignCenter->setOn( true ); insilmaril@53: else if ( ( a & Qt::AlignRight ) ) insilmaril@53: actionAlignRight->setOn( true ); insilmaril@53: else if ( ( a & Qt::AlignJustify ) ) insilmaril@53: actionAlignJustify->setOn( true ); insilmaril@53: } insilmaril@53: insilmaril@53: void TextEditor::verticalAlignmentChanged(QTextCharFormat::VerticalAlignment a) insilmaril@53: { insilmaril@53: actionAlignSubScript->setOn (false); insilmaril@53: actionAlignSuperScript->setOn (false); insilmaril@53: switch (a) insilmaril@53: { insilmaril@53: case QTextCharFormat::AlignSuperScript: insilmaril@53: actionAlignSuperScript->setOn (true); insilmaril@53: break; insilmaril@53: case QTextCharFormat::AlignSubScript: insilmaril@53: actionAlignSubScript->setOn (true); insilmaril@53: break; insilmaril@53: default: ; insilmaril@53: } insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: insilmaril@0: void TextEditor::enableActions() insilmaril@0: { insilmaril@0: actionFileLoad->setEnabled(true); insilmaril@0: actionFileSave->setEnabled(true); insilmaril@0: actionFileSaveAs->setEnabled(true); insilmaril@0: actionFilePrint->setEnabled(true); insilmaril@0: actionEditUndo->setEnabled(true); insilmaril@0: actionEditRedo->setEnabled(true); insilmaril@0: actionEditCopy->setEnabled(true); insilmaril@0: actionEditCut->setEnabled(true); insilmaril@0: actionEditPaste->setEnabled(true); insilmaril@0: actionEditDeleteAll->setEnabled(true); insilmaril@0: actionFormatUseFixedFont->setEnabled(true); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::disableActions() insilmaril@0: { insilmaril@0: actionFileLoad->setEnabled(false); insilmaril@0: actionFileSave->setEnabled(false); insilmaril@0: actionFileSaveAs->setEnabled(false); insilmaril@0: actionFilePrint->setEnabled(false); insilmaril@0: actionEditUndo->setEnabled(false); insilmaril@0: actionEditRedo->setEnabled(false); insilmaril@0: actionEditCopy->setEnabled(false); insilmaril@0: actionEditCut->setEnabled(false); insilmaril@0: actionEditPaste->setEnabled(false); insilmaril@0: actionEditDeleteAll->setEnabled(false); insilmaril@0: actionFormatUseFixedFont->setEnabled(false); insilmaril@0: } insilmaril@0: insilmaril@53: void TextEditor::setState (EditorState s) insilmaril@53: { insilmaril@53: insilmaril@53: QPalette p=palette(); insilmaril@53: QColor c; insilmaril@53: switch (s) insilmaril@53: { insilmaril@53: case emptyEditor: c=QColor (150,150,150); break; insilmaril@53: case filledEditor: c=QColor (255,255,255); break; insilmaril@53: case inactiveEditor: c=QColor (0,0,0); insilmaril@53: } insilmaril@53: p.setColor(QPalette::Active, static_cast(9), c); insilmaril@53: p.setColor(QPalette::Inactive, static_cast(9), c); insilmaril@53: e->setPalette(p); insilmaril@53: } insilmaril@0: insilmaril@53: