insilmaril@0: #include "texteditor.h" insilmaril@0: insilmaril@2: #include insilmaril@0: #include insilmaril@0: #include insilmaril@0: #include insilmaril@2: #include insilmaril@2: #include insilmaril@2: #include insilmaril@0: #include insilmaril@2: #include insilmaril@0: #include insilmaril@2: #include insilmaril@0: #include insilmaril@0: #include insilmaril@0: #include insilmaril@0: #include insilmaril@0: #include insilmaril@2: #include insilmaril@2: //Added by qt3to4: insilmaril@2: #include insilmaril@2: #include insilmaril@2: #include insilmaril@0: insilmaril@0: #include insilmaril@2: #include insilmaril@0: #include insilmaril@0: insilmaril@2: #include "noteobj.h" insilmaril@2: #include "version.h" insilmaril@0: insilmaril@2: extern Q3Canvas* actCanvas; insilmaril@0: extern int statusbarTime; insilmaril@0: extern QSettings settings; insilmaril@0: insilmaril@2: extern QAction *actionViewToggleNoteEditor; insilmaril@2: insilmaril@2: extern QString iconPath; insilmaril@2: insilmaril@0: using namespace std; insilmaril@0: insilmaril@0: insilmaril@0: /////////////////////////////////////////////////////////////////////// insilmaril@0: /////////////////////////////////////////////////////////////////////// insilmaril@0: insilmaril@2: insilmaril@0: TextEditor::TextEditor() insilmaril@0: { insilmaril@0: printer = new QPrinter( QPrinter::HighResolution ); insilmaril@2: printer->setPrinterName (settings.readEntry("/vym/mainwindow/printerName",printer->printerName())); insilmaril@0: insilmaril@2: // Editor (use MyTextEdit to wrap verticalAlignmentChanged to insilmaril@2: // int argument. see header file) insilmaril@2: e = new MyTextEdit( this, "editor" ); insilmaril@0: e->setFocus(); insilmaril@2: e->setTextFormat(Qt::RichText); // default insilmaril@0: e->setTabStopWidth (20); // unit is pixel insilmaril@2: e->setColor (Qt::black); insilmaril@0: connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) ); insilmaril@0: setCentralWidget( e ); insilmaril@0: statusBar()->message( "Ready", statusbarTime); insilmaril@2: setCaption(__VYM " - " +tr ("Note Editor")); insilmaril@0: insilmaril@0: // Toolbars insilmaril@0: setupFileActions(); insilmaril@0: setupEditActions(); insilmaril@0: setupFormatActions(); insilmaril@0: setupSettingsActions(); insilmaril@0: insilmaril@0: // Various states insilmaril@2: emptyPaper = QBrush(Qt::gray); insilmaril@2: filledPaper = QBrush(Qt::white); insilmaril@2: inactivePaper= QBrush(Qt::black); insilmaril@0: setInactive(); insilmaril@0: insilmaril@0: // Load Settings insilmaril@0: resize (settings.readNumEntry( "/vym/noteeditor/geometry/width", 450), insilmaril@0: settings.readNumEntry( "/vym/noteeditor/geometry/height",600)); insilmaril@0: move (settings.readNumEntry( "/vym/noteeditor/geometry/posX", 150), insilmaril@0: settings.readNumEntry( "/vym/noteeditor/geometry/posY", 50)); insilmaril@0: insilmaril@0: if (settings.readEntry( "/vym/noteeditor/showWithMain","yes") =="yes") insilmaril@0: setShowWithMain(true); insilmaril@0: else insilmaril@0: setShowWithMain(false); insilmaril@0: insilmaril@0: varFont.fromString( settings.readEntry insilmaril@0: ("/vym/noteeditor/fonts/varFont", insilmaril@0: "Nimbus Sans l,14,-1,5,48,0,0,0,0,0") insilmaril@0: ); insilmaril@0: fixedFont.fromString (settings.readEntry ( insilmaril@0: "/vym/noteeditor/fonts/fixedFont", insilmaril@0: "Courier,14,-1,5,48,0,0,0,1,0") insilmaril@0: ); insilmaril@0: QString s=settings.readEntry ("/vym/noteeditor/fonts/fonthintDefault","variable"); 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@2: filenameHint=""; insilmaril@2: insilmaril@2: // Save settings in vymrc insilmaril@2: settings.writeEntry("/vym/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@0: settings.writeEntry( "/vym/noteeditor/geometry/width", width() ); insilmaril@0: settings.writeEntry( "/vym/noteeditor/geometry/height", height() ); insilmaril@0: settings.writeEntry( "/vym/noteeditor/geometry/posX", pos().x() ); insilmaril@0: settings.writeEntry( "/vym/noteeditor/geometry/posY", pos().y() ); insilmaril@0: insilmaril@0: if (showWithMain()) insilmaril@0: settings.writeEntry( "/vym/noteeditor/showWithMain","yes"); insilmaril@0: else insilmaril@0: settings.writeEntry( "/vym/noteeditor/showWithMain","no"); insilmaril@0: insilmaril@0: QString s; insilmaril@0: if (actionSettingsFonthintDefault->isOn() ) insilmaril@0: s="fixed"; insilmaril@0: else insilmaril@0: s="variable"; insilmaril@0: settings.writeEntry( "/vym/noteeditor/fonts/fonthintDefault",s ); insilmaril@0: settings.writeEntry ("/vym/noteeditor/fonts/varFont", insilmaril@0: varFont.toString() ); insilmaril@0: settings.writeEntry ("/vym/noteeditor/fonts/fixedFont", insilmaril@0: fixedFont.toString() ); insilmaril@0: } insilmaril@0: insilmaril@0: bool TextEditor::isEmpty() insilmaril@0: { insilmaril@0: if (e->text().length()) 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@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@0: if (fn.isEmpty() ) insilmaril@0: { insilmaril@0: filename=""; insilmaril@0: statusBar()->message( "No filename available for this note.", statusbarTime ); insilmaril@0: } insilmaril@0: else insilmaril@0: { insilmaril@0: filename=fn; insilmaril@0: statusBar()->message( QString( "Current filename is %1" ).arg( filename ), statusbarTime ); insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: QString TextEditor::getFilename() insilmaril@0: { insilmaril@0: return filename; insilmaril@0: } insilmaril@0: insilmaril@2: void TextEditor::setFilenameHint(const QString &fnh) insilmaril@2: { insilmaril@2: filenameHint=fnh; insilmaril@2: } insilmaril@2: insilmaril@2: QString TextEditor::getFilenameHint() insilmaril@2: { insilmaril@2: return filenameHint; insilmaril@2: } insilmaril@2: insilmaril@0: bool TextEditor::findText(const QString &t, const bool &cs) insilmaril@0: { insilmaril@0: bool wo=false; // word matches insilmaril@0: if (e->find (t, cs, wo, true, 0, 0 )) insilmaril@0: return true; insilmaril@0: else insilmaril@0: return false; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupFileActions() insilmaril@0: { insilmaril@5: QToolBar *tb = addToolBar ( tr("File Actions") ); insilmaril@5: QMenu *fileMenu = menuBar()->addMenu( tr( "&File" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@2: a = new QAction( QPixmap( iconPath+"fileopen.png"), tr( "&Import..." ),this); insilmaril@2: a->setStatusTip (tr( "Import" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_O ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) ); insilmaril@5: tb->addAction (a); insilmaril@5: fileMenu->addAction (a); insilmaril@0: actionFileLoad=a; insilmaril@0: insilmaril@5: fileMenu->addSeparator(); insilmaril@2: a = new QAction( QPixmap(iconPath+"filesave.png" ), tr( "&Export..." ),this); insilmaril@2: a->setStatusTip (tr( "Export Note (HTML)" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_S ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) ); insilmaril@5: tb->addAction (a); insilmaril@5: fileMenu->addAction (a); insilmaril@0: actionFileSave=a; insilmaril@0: insilmaril@2: a = new QAction( QPixmap(), tr( "Export &As... (HTML)" ), this); insilmaril@2: a->setStatusTip (tr( "Export Note As (HTML) " )); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) ); insilmaril@5: fileMenu->addAction (a); insilmaril@0: actionFileSaveAs=a; insilmaril@0: insilmaril@2: a = new QAction(QPixmap(), tr( "Export &As...(ASCII)" ), this); insilmaril@2: a->setStatusTip ( tr( "Export Note As (ASCII) " ) ); insilmaril@2: a->setShortcut(Qt::ALT + Qt::Key_X ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) ); insilmaril@5: fileMenu->addAction (a); insilmaril@0: actionFileSaveAs=a; insilmaril@0: insilmaril@5: fileMenu->addSeparator(); insilmaril@2: a = new QAction( QPixmap(iconPath+"fileprint.png" ), tr( "&Print..." ),this); insilmaril@2: a->setStatusTip (tr( "Print Note" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_P ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) ); insilmaril@5: tb->addAction (a); insilmaril@5: fileMenu->addAction (a); insilmaril@0: actionFilePrint=a; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupEditActions() insilmaril@0: { insilmaril@5: QToolBar *tb = addToolBar ( tr( "Edit Actions" )); insilmaril@5: QMenu *editMenu = menuBar()->addMenu ( tr( "&Edit" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@2: a = new QAction(QPixmap(iconPath+"undo.png"), tr( "&Undo" ), this ); insilmaril@2: a->setStatusTip ( tr( "Undo" ) ); insilmaril@2: a->setShortcut(Qt::CTRL + Qt::Key_Z ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( undo() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@0: actionEditUndo=a; insilmaril@0: insilmaril@2: a = new QAction(QPixmap(iconPath+"redo.png" ), tr( "&Redo" ),this); insilmaril@2: a->setStatusTip ( tr( "Redo" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_Y ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( redo() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@0: actionEditRedo=a; insilmaril@0: insilmaril@5: editMenu->addSeparator(); insilmaril@2: a = new QAction(QPixmap(), tr( "Select and copy &all" ),this); insilmaril@2: a->setStatusTip ( tr( "Select and copy all" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_A ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@0: insilmaril@5: editMenu->addSeparator(); insilmaril@2: a = new QAction(QPixmap(iconPath+"editcopy.png" ), tr( "&Copy" ),this); insilmaril@2: a->setStatusTip ( tr( "Copy" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_C ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( copy() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@0: actionEditCopy=a; insilmaril@0: insilmaril@2: a = new QAction(QPixmap(iconPath+"editcut.png" ), tr( "Cu&t" ),this); insilmaril@2: a->setStatusTip ( tr( "Cut" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_X ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( cut() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@0: actionEditCut=a; insilmaril@0: insilmaril@2: a = new QAction(QPixmap(iconPath+"editpaste.png" ), tr( "&Paste" ),this); insilmaril@2: a->setStatusTip ( tr( "Paste" ) ); insilmaril@2: a->setShortcut( Qt::CTRL + Qt::Key_V ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( paste() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@0: actionEditPaste=a; insilmaril@0: insilmaril@2: a = new QAction( QPixmap( iconPath+"edittrash.png"), tr( "&Delete All" ), this); insilmaril@2: a->setStatusTip (tr( "Delete all" ) ); insilmaril@0: connect( a, SIGNAL( activated() ), e, SLOT( clear() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@0: actionEditDeleteAll=a; insilmaril@0: insilmaril@2: a = new QAction(QPixmap(), tr( "&Convert Paragraphs" ),this); insilmaril@2: a->setStatusTip(tr( "Convert paragraphs to linebreaks" )); insilmaril@2: a->setShortcut( Qt::ALT + Qt::Key_P ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textConvertPar() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@0: actionEditConvertPar=a; insilmaril@0: insilmaril@2: a = new QAction( QPixmap(), tr( "&Join lines" ), this); insilmaril@2: a->setStatusTip(tr( "Join all lines of a paragraph" ) ); insilmaril@2: a->setShortcut(Qt::ALT + Qt::Key_J ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( textJoinLines() ) ); insilmaril@5: editMenu->addAction (a); insilmaril@0: actionEditJoinLines=a; insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupFormatActions() insilmaril@0: { insilmaril@5: QToolBar *tb = addToolBar ( tr("Format Actions" )); insilmaril@5: QMenu *formatMenu = menuBar()->addMenu ( tr( "F&ormat" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@0: insilmaril@2: a = new QAction( QPixmap(iconPath+"formatfixedfont.png"), tr( "&Font hint" ), Qt::ALT + Qt::Key_I, this, "fontHint" ); insilmaril@2: a->setStatusTip (tr( "Toggle font hint for the whole text" ) ); insilmaril@0: a->setToggleAction (true); insilmaril@0: a->setOn (settings.readBoolEntry ("/vym/noteeditor/fonts/useFixedByDefault",false) ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) ); insilmaril@5: formatMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@0: actionFormatUseFixedFont=a; insilmaril@0: insilmaril@5: // comboFont = new QComboBox( true, tb ); insilmaril@5: comboFont = new QComboBox; insilmaril@5: tb->addWidget (comboFont); insilmaril@0: QFontDatabase db; insilmaril@0: comboFont->insertStringList( db.families() ); insilmaril@0: connect( comboFont, SIGNAL( activated( const QString & ) ), insilmaril@0: this, SLOT( textFamily( const QString & ) ) ); insilmaril@5: comboFont->addItem( QApplication::font().family() ); insilmaril@5: // comboSize = new QComboBox( true, tb ); insilmaril@5: comboSize = new QComboBox; insilmaril@5: tb->addWidget (comboSize); insilmaril@2: Q3ValueList sizes = db.standardSizes(); insilmaril@2: Q3ValueList::Iterator it = sizes.begin(); insilmaril@0: for ( ; it != sizes.end(); ++it ) insilmaril@0: comboSize->insertItem( QString::number( *it ) ); insilmaril@0: connect( comboSize, SIGNAL( activated( const QString & ) ), insilmaril@0: this, SLOT( textSize( const QString & ) ) ); insilmaril@5: comboSize->addItem ( QString::number( QApplication::font().pointSize() ) ); insilmaril@0: insilmaril@5: formatMenu->addSeparator(); insilmaril@0: insilmaril@0: QPixmap pix( 16, 16 ); insilmaril@0: pix.fill( e->color()); insilmaril@5: a = new QAction( pix, tr( "&Color..." ), this); insilmaril@5: formatMenu->addAction (a); insilmaril@5: tb->addAction (a); insilmaril@5: connect( a, SIGNAL( activated() ), this, SLOT( textColor() ) ); insilmaril@5: actionTextColor=a; insilmaril@0: insilmaril@5: a = new QAction( QPixmap (iconPath+"text_bold.png"), tr( "&Bold" ), this); insilmaril@5: a->setShortcut(Qt::CTRL + Qt::Key_B ); insilmaril@5: connect( a, SIGNAL( activated() ), this, SLOT( textBold() ) ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: a->setToggleAction( true ); insilmaril@5: actionTextBold=a; insilmaril@5: insilmaril@5: a = new QAction( QPixmap(iconPath+"text_italic.png"), tr( "&Italic" ), this); insilmaril@5: a->setShortcut(Qt::CTRL + Qt::Key_I); insilmaril@5: connect( a, SIGNAL( activated() ), this, SLOT( textItalic() ) ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: a->setToggleAction( true ); insilmaril@5: actionTextItalic=a; insilmaril@5: insilmaril@5: a = new QAction( QPixmap (iconPath+"text_under.png"), tr( "&Underline" ), this); insilmaril@5: a->setShortcut(Qt::CTRL + Qt::Key_U ); insilmaril@5: connect( a, SIGNAL( activated() ), this, SLOT( textUnderline() ) ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: a->setToggleAction( true ); insilmaril@5: actionTextUnderline=a; insilmaril@5: formatMenu->addSeparator(); insilmaril@0: insilmaril@3: QActionGroup *grp = new QActionGroup( this ); insilmaril@0: connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) ); insilmaril@0: insilmaril@5: a = new QAction( QPixmap (iconPath+"text_left.png"), tr( "&Left" ),grp ); insilmaril@5: a->setShortcut( Qt::CTRL+Qt::Key_L ); insilmaril@5: a->setToggleAction( true ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: actionAlignLeft=a; insilmaril@5: a = new QAction( QPixmap (iconPath+"text_center.png"), tr( "C&enter" ),grp); insilmaril@5: a->setShortcut( Qt::CTRL + Qt::Key_E); insilmaril@5: a->setToggleAction( true ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: actionAlignCenter=a; insilmaril@5: a = new QAction( QPixmap (iconPath+"text_right.png" ), tr( "&Right" ), grp); insilmaril@5: a->setShortcut(Qt::CTRL + Qt::Key_R ); insilmaril@5: a->setToggleAction( true ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: actionAlignRight=a; insilmaril@5: a = new QAction( QPixmap ( iconPath+"text_block.png"), tr( "&Justify" ), grp ); insilmaril@5: a->setShortcut(Qt::CTRL + Qt::Key_J ); insilmaril@5: a->setToggleAction( true ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: actionAlignJustify=a; insilmaril@0: insilmaril@3: QActionGroup *grp2 = new QActionGroup( this ); insilmaril@5: grp2->setExclusive(true); insilmaril@5: a = new QAction( QPixmap (iconPath+"text_sub.png"), tr( "Subs&cript" ),grp2 ); insilmaril@5: a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B ); insilmaril@5: a->setToggleAction( true ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: connect(a, SIGNAL(activated()), this, SLOT(textVAlign())); insilmaril@5: actionAlignSubScript=a; insilmaril@2: insilmaril@5: a = new QAction( QPixmap (iconPath+"text_super.png"), tr( "Su&perscript" ),grp2 ); insilmaril@5: a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_P ); insilmaril@5: a->setToggleAction( true ); insilmaril@5: tb->addAction (a); insilmaril@5: formatMenu->addAction (a); insilmaril@5: connect(a, SIGNAL(activated()), this, SLOT(textVAlign())); insilmaril@5: actionAlignSuperScript=a; insilmaril@2: insilmaril@5: formatMenu->addSeparator(); insilmaril@2: insilmaril@0: connect( e, SIGNAL( currentFontChanged( const QFont & ) ), insilmaril@0: this, SLOT( fontChanged( const QFont & ) ) ); insilmaril@0: connect( e, SIGNAL( currentColorChanged( const QColor & ) ), insilmaril@0: this, SLOT( colorChanged( const QColor & ) ) ); insilmaril@0: connect( e, SIGNAL( currentAlignmentChanged( int ) ), insilmaril@0: this, SLOT( alignmentChanged( int ) ) ); insilmaril@2: connect( e, SIGNAL( currentVerticalAlignmentChanged(int)), insilmaril@2: this, SLOT( verticalAlignmentChanged(int))); insilmaril@0: insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setupSettingsActions() insilmaril@0: { insilmaril@5: QMenu *settingsMenu = menuBar()->addMenu ( tr( "&Settings" )); insilmaril@0: insilmaril@0: QAction *a; insilmaril@2: a = new QAction(tr( "Set &fixed font" ), this); insilmaril@2: a->setStatusTip ( tr( "Set fixed font" )); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) ); insilmaril@5: settingsMenu->addAction (a); insilmaril@0: actionSettingsFixedFont=a; insilmaril@0: insilmaril@2: a = new QAction(tr( "Set &variable font" ), this); insilmaril@2: a->setStatusTip ( tr( "Set variable font" ) ); insilmaril@0: connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) ); insilmaril@5: settingsMenu->addAction (a); insilmaril@0: actionSettingsVarFont=a; insilmaril@0: insilmaril@2: a = new QAction(tr( "&fixed font is default" ), this); insilmaril@2: a->setStatusTip (tr( "Used fixed font by default" ) ); insilmaril@0: a->setToggleAction (true); insilmaril@0: // set state later in constructor... insilmaril@5: 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@0: if (e->length()) insilmaril@0: { insilmaril@2: QMessageBox mb( __VYM " - " +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@2: Q3FileDialog *fd=new Q3FileDialog( this); insilmaril@0: fd->addFilter ("ASCII texts (*.txt)"); insilmaril@0: fd->addFilter ("VYM notes (*.html)"); 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@2: 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@2: ce->accept(); // TextEditor can be reopened with show() insilmaril@2: actionViewToggleNoteEditor->setOn (false); insilmaril@2: showwithmain=false; insilmaril@2: return; insilmaril@0: } insilmaril@0: insilmaril@0: QString TextEditor::getText() insilmaril@0: { insilmaril@0: 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@0: e->setPaper (emptyPaper); insilmaril@0: else insilmaril@0: e->setPaper (filledPaper); insilmaril@0: // SLOT is LinkableMapObj, which will update systemFlag insilmaril@0: emit (textHasChanged() ); insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: void TextEditor::setText(QString t) insilmaril@0: { insilmaril@2: if ( !Q3StyleSheet::mightBeRichText( t ) ) insilmaril@2: t = Q3StyleSheet::convertFromPlainText( t, Q3StyleSheetItem::WhiteSpaceNormal ); insilmaril@0: e->setReadOnly(false); insilmaril@0: e->setText(t); insilmaril@2: // editorChanged(); //not called automagically FIXME or is it? insilmaril@0: insilmaril@0: enableActions(); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::setInactive() insilmaril@0: { insilmaril@2: state=inactiveEditor; insilmaril@0: setText(""); insilmaril@0: e->setPaper (inactivePaper); 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@2: QString fn = Q3FileDialog::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@2: QMessageBox mb( __VYM, insilmaril@0: tr("The file ") + fn + insilmaril@0: tr(" exists already. " insilmaril@0: "Do you want to overwrite it?"), insilmaril@0: QMessageBox::Warning, insilmaril@0: QMessageBox::Yes | QMessageBox::Default, insilmaril@0: QMessageBox::Cancel | QMessageBox::Escape, insilmaril@2: 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@0: statusBar()->message(tr( "Couldn't export note ") + 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@2: 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::textConvertPar() insilmaril@0: { insilmaril@0: // In X11 a copy&paste generates paragraphs, insilmaril@0: // which is not always wanted insilmaril@0: // This function replaces paragraphs by linebreaks. insilmaril@0: int parFrom, parTo, indFrom, indTo; insilmaril@0: e->getSelection (&parFrom,&indFrom,&parTo,&indTo); insilmaril@0: QString t; insilmaril@0: if (parFrom>-1) insilmaril@0: t=e->selectedText(); insilmaril@0: else insilmaril@0: t=e->text(); insilmaril@0: insilmaril@0: QRegExp re(""); insilmaril@0: re.setMinimal(true); insilmaril@0: t.replace (re,""); insilmaril@0: t.replace ("

","
"); insilmaril@0: if (parFrom>-1) insilmaril@0: { insilmaril@0: e->setCursorPosition (parFrom,indFrom); insilmaril@0: e->cut(); insilmaril@0: // Tried to simply insert the changed text with insilmaril@0: // e->insert (t,(uint)(QTextEdit::RemoveSelected)); insilmaril@0: // but then the html would be quoted. So I use the ugly insilmaril@0: // way: insert a marker, replace it in whole text of QTextEdit insilmaril@0: QString marker="R3PlAcEMeL4teR!"; insilmaril@0: e->insert (marker); insilmaril@0: e->setText (e->text().replace(marker,t)); insilmaril@0: } else insilmaril@0: e->setText(t); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textJoinLines() insilmaril@0: { insilmaril@0: int parFrom, parTo, indFrom, indTo; insilmaril@0: e->getSelection (&parFrom,&indFrom,&parTo,&indTo); insilmaril@0: QString t; insilmaril@0: if (parFrom>-1) insilmaril@0: t=e->selectedText(); insilmaril@0: else insilmaril@0: t=e->text(); insilmaril@0: // In addition to textConvertPar it is sometimes insilmaril@0: // useful to join all lines of a paragraph insilmaril@0: QRegExp re("

\n+

(?!

)"); insilmaril@0: re.setMinimal(true); insilmaril@0: t.replace (re," "); insilmaril@0: insilmaril@0: // Above we may have introduced new " " at beginning of a insilmaril@0: // paragraph - remove it. insilmaril@0: re.setPattern("

"); insilmaril@0: t.replace (re,"

"); insilmaril@0: if (parFrom>-1) insilmaril@0: { insilmaril@0: e->setCursorPosition (parFrom,indFrom); insilmaril@0: e->cut(); insilmaril@0: // Tried to simply insert the changed text with insilmaril@0: // e->insert (t,(uint)(QTextEdit::RemoveSelected)); insilmaril@0: // but then the html would be quoted. So I use the ugly insilmaril@0: // way: insert a marker, replace it in whole text of QTextEdit insilmaril@0: QString marker="R3PlAcEMeL4teR!"; insilmaril@0: e->insert (marker); insilmaril@0: e->setText (e->text().replace(marker,t)); insilmaril@0: } else insilmaril@0: e->setText(t); insilmaril@0: } insilmaril@0: insilmaril@0: void TextEditor::textExportAsASCII() insilmaril@0: { insilmaril@2: QString text = NoteObj (e->text()).getNoteASCII(); insilmaril@2: QString fn,s; insilmaril@2: if (!filenameHint.isEmpty()) insilmaril@2: { insilmaril@2: if (!filenameHint.contains (".txt")) insilmaril@2: s=filenameHint+".txt"; insilmaril@2: else insilmaril@2: s=filenameHint; insilmaril@2: } else insilmaril@2: s=QString::null; insilmaril@2: fn = Q3FileDialog::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@0: QMessageBox mb( "VYM", insilmaril@0: tr("The file ") + fn + insilmaril@0: tr(" exists already. " insilmaril@0: "Do you want to overwrite it?"), insilmaril@0: QMessageBox::Warning, insilmaril@0: QMessageBox::Yes | QMessageBox::Default, insilmaril@0: QMessageBox::Cancel | QMessageBox::Escape, insilmaril@2: 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@2: 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: printer->setFullPage(TRUE); insilmaril@0: if ( printer->setup( this ) ) insilmaril@0: { insilmaril@0: QPainter p( printer ); insilmaril@0: // Check that there is a valid device to print to. insilmaril@0: if ( !p.device() ) return; insilmaril@2: Q3PaintDeviceMetrics metrics( p.device() ); insilmaril@0: int dpiy = metrics.logicalDpiY(); insilmaril@0: int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins insilmaril@0: QRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin ); insilmaril@0: QFont font( e->currentFont() ); insilmaril@0: font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing insilmaril@0: insilmaril@2: Q3SimpleRichText richText( e->text(), font, insilmaril@0: e->context(), insilmaril@0: e->styleSheet(), insilmaril@0: e->mimeSourceFactory(), insilmaril@0: body.height() ); insilmaril@0: richText.setWidth( &p, body.width() ); insilmaril@0: QRect view( body ); insilmaril@0: int page = 1; insilmaril@0: do insilmaril@0: { insilmaril@0: richText.draw( &p, body.left(), body.top(), view, colorGroup() ); insilmaril@0: view.moveBy( 0, body.height() ); insilmaril@0: p.translate( 0 , -body.height() ); insilmaril@0: p.setFont( font ); insilmaril@0: p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ), insilmaril@0: view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) ); insilmaril@0: if ( view.top() >= richText.height() ) insilmaril@0: break; insilmaril@0: printer->newPage(); insilmaril@0: page++; insilmaril@0: } while (TRUE); insilmaril@0: } 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@0: e->selectAll (true); insilmaril@0: if (!actionFormatUseFixedFont->isOn() ) insilmaril@0: e->setCurrentFont (varFont); insilmaril@0: else insilmaril@0: e->setCurrentFont (fixedFont); insilmaril@0: e->selectAll (false); 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@0: if ( !col.isValid() ) insilmaril@0: return; insilmaril@0: e->setColor( col ); insilmaril@0: QPixmap pix( 16, 16 ); insilmaril@2: 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@2: e->setAlignment( Qt::AlignLeft ); insilmaril@0: else if ( a == actionAlignCenter ) insilmaril@2: e->setAlignment( Qt::AlignHCenter ); insilmaril@0: else if ( a == actionAlignRight ) insilmaril@2: e->setAlignment( Qt::AlignRight ); insilmaril@0: else if ( a == actionAlignJustify ) insilmaril@2: e->setAlignment( Qt::AlignJustify ); insilmaril@0: } insilmaril@0: insilmaril@2: void TextEditor::textVAlign() insilmaril@2: { insilmaril@2: if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) { insilmaril@2: e->setVerticalAlignment( Q3TextEdit::AlignSuperScript); insilmaril@2: } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) { insilmaril@2: e->setVerticalAlignment( Q3TextEdit::AlignSubScript); insilmaril@2: } else { insilmaril@2: e->setVerticalAlignment( Q3TextEdit::AlignNormal); insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@2: insilmaril@0: void TextEditor::fontChanged( const QFont &f ) insilmaril@0: { insilmaril@5: /* insilmaril@0: comboFont->lineEdit()->setText( f.family() ); insilmaril@0: comboSize->lineEdit()->setText( QString::number( f.pointSize() ) ); insilmaril@5: */ 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@0: void TextEditor::alignmentChanged( int a ) insilmaril@0: { insilmaril@2: if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft )) insilmaril@0: actionAlignLeft->setOn( true ); insilmaril@2: else if ( ( a & Qt::AlignHCenter ) ) insilmaril@0: actionAlignCenter->setOn( true ); insilmaril@2: else if ( ( a & Qt::AlignRight ) ) insilmaril@0: actionAlignRight->setOn( true ); insilmaril@2: else if ( ( a & Qt::AlignJustify ) ) insilmaril@0: actionAlignJustify->setOn( true ); insilmaril@0: } insilmaril@0: insilmaril@2: void TextEditor::verticalAlignmentChanged(int a) insilmaril@2: { insilmaril@2: if (a == Q3TextEdit::AlignSuperScript ) { insilmaril@2: actionAlignSuperScript->setOn(true); insilmaril@2: actionAlignSubScript->setOn(false); insilmaril@2: } else if (a == Q3TextEdit::AlignSubScript ) { insilmaril@2: actionAlignSuperScript->setOn(false); insilmaril@2: actionAlignSubScript->setOn(true); insilmaril@2: } else { insilmaril@2: actionAlignSuperScript->setOn(false); insilmaril@2: actionAlignSubScript->setOn(false); insilmaril@2: } insilmaril@2: } insilmaril@2: 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: actionEditConvertPar->setEnabled(true); insilmaril@0: actionEditJoinLines->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: actionEditConvertPar->setEnabled(false); insilmaril@0: actionEditJoinLines->setEnabled(false); insilmaril@0: actionFormatUseFixedFont->setEnabled(false); insilmaril@0: } insilmaril@0: insilmaril@0: