1 #include "texteditor.h"
4 #include <QActionGroup>
5 #include <QApplication>
6 #include <QColorDialog>
11 #include <QMessageBox>
12 #include <QPrintDialog>
20 #include "mainwindow.h"
22 #include "shortcuts.h"
24 extern Main *mainWindow;
25 extern int statusbarTime;
26 extern Settings settings;
28 extern QAction *actionViewToggleNoteEditor;
30 extern QString vymName;
32 extern Switchboard switchboard;
34 extern QPrinter *printer;
37 ///////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////
40 TextEditor::TextEditor()
42 statusBar()->hide(); // Hide sizeGrip on default, which comes with statusBar
44 e = new QTextEdit(this);
46 e->setTabStopDistance(20); // unit is pixel, default would be 80
47 e->setAutoFillBackground(true);
48 e->installEventFilter(this);
49 connect(e, SIGNAL(textChanged()), this, SLOT(editorChanged()));
51 statusBar()->showMessage(tr("Ready", "Statusbar message"), statusbarTime);
53 connect(e, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)), this,
54 SLOT(formatChanged(const QTextCharFormat &)));
56 // Don't show menubar per default
63 setupSettingsActions();
66 blockChangedSignal = false;
67 blockTextUpdate = false;
70 editorName = "Text editor";
74 TextEditor::~TextEditor()
77 QString n = QString("/satellite/%1/").arg(shortcutScope);
78 settings.setValue(n + "geometry/size", size());
79 settings.setValue(n + "geometry/pos", pos());
80 settings.setValue(n + "state", saveState(0));
83 if (actionSettingsFonthintDefault->isChecked())
87 settings.setValue(n + "fonts/fonthintDefault", s);
88 settings.setValue(n + "fonts/varFont", varFont.toString());
89 settings.setValue(n + "fonts/fixedFont", fixedFont.toString());
91 settings.setValue(n + "colors/richTextDefaultBackground", colorRichTextDefaultBackground.name());
92 settings.setValue(n + "colors/richTextDefaultForeground", colorRichTextDefaultForeground.name());
95 void TextEditor::init(const QString &scope)
97 shortcutScope = scope;
98 QString n = QString("/satellite/%1/").arg(shortcutScope);
99 restoreState(settings.value(n + "state", 0).toByteArray());
101 fixedFont.fromString(
102 settings.value(n + "fonts/fixedFont", "Courier,12,-1,5,48,0,0,0,1,0")
106 .value(n + "fonts/varFont", "DejaVu Sans Mono,12,-1,0,50,0,0,0,0,0")
109 settings.value(n + "fonts/fonthintDefault", "variable").toString();
111 actionSettingsFonthintDefault->setChecked(true);
112 e->setCurrentFont(fixedFont);
115 actionSettingsFonthintDefault->setChecked(false);
116 e->setCurrentFont(varFont);
119 // Default colors for RichText //FIXME-2 here? Though we use plainText as default?
121 colorRichTextDefaultBackground.setNamedColor(
122 settings.value(n + "colors/richTextDefaultBackground", "#ffffff").toString());
123 pix.fill(colorRichTextDefaultBackground);
124 actionFilledEditorColor->setIcon(pix);
127 colorRichTextDefaultForeground.setNamedColor(
128 settings.value(n + "colors/richTextDefaultForeground", "#000000").toString());
129 pix.fill(colorRichTextDefaultForeground);
130 actionFontColor->setIcon(pix);
132 // Default is PlainText
133 actionFormatRichText->setChecked(false);
137 bool TextEditor::isEmpty()
139 if (e->toPlainText().length() > 0)
145 void TextEditor::setEditorTitle(const QString &s)
147 editorTitle = (s.isEmpty()) ? editorName : editorName + ": " + s;
149 // Set title of parent dockWidget
151 parentWidget()->setWindowTitle(editorTitle);
153 setWindowTitle(editorTitle);
156 QString TextEditor::getEditorTitle() { return editorTitle; }
158 void TextEditor::setEditorName(const QString &s) { editorName = s; }
160 void TextEditor::setFont(const QFont &font)
162 blockChangedSignal = true;
164 QTextCursor tc = e->textCursor();
165 QTextCharFormat format = tc.charFormat();
167 tc.select(QTextCursor::Document);
168 format.setFont(font);
169 tc.setCharFormat(format);
171 fontChanged(fixedFont);
173 blockChangedSignal = false;
176 void TextEditor::setFontHint(const QString &fh)
179 actionFormatUseFixedFont->setChecked(true);
180 e->setCurrentFont(fixedFont);
184 actionFormatUseFixedFont->setChecked(false);
185 e->setCurrentFont(varFont);
190 QString TextEditor::getFontHint()
192 if (actionFormatUseFixedFont->isChecked())
198 QString TextEditor::getFontHintDefault()
200 if (actionSettingsFonthintDefault->isChecked())
206 void TextEditor::setFilename(const QString &fn)
208 if (state == filledEditor) {
211 statusBar()->showMessage(
212 tr("No filename available for this note.", "Statusbar message"),
217 statusBar()->showMessage(
218 tr(QString("Current filename is %1").arg(filename).toUtf8(),
219 "Statusbar message"),
225 QString TextEditor::getFilename() { return filename; }
227 void TextEditor::setFilenameHint(const QString &fnh) { filenameHint = fnh; }
229 QString TextEditor::getFilenameHint() { return filenameHint; }
231 QString TextEditor::getText()
233 if (e->toPlainText().isEmpty())
236 if (actionFormatRichText->isChecked())
239 return e->toPlainText();
242 VymText TextEditor::getVymText()
246 if (actionFormatRichText->isChecked())
247 vt.setRichText(e->toHtml());
249 vt.setPlainText(e->toPlainText());
251 if (actionFormatUseFixedFont->isChecked())
252 vt.setFontHint(getFontHint());
257 bool TextEditor::findText(const QString &t,
258 const QTextDocument::FindFlags &flags)
260 if (e->find(t, flags))
266 bool TextEditor::findText(const QString &t,
267 const QTextDocument::FindFlags &flags, int i)
269 // Position at beginning
270 QTextCursor c = e->textCursor();
271 c.setPosition(0, QTextCursor::MoveAnchor);
277 if (!e->find(t, flags))
284 void TextEditor::setTextCursor(const QTextCursor &cursor)
286 e->setTextCursor(cursor);
289 QTextCursor TextEditor::getTextCursor() { return e->textCursor(); }
291 void TextEditor::setFocus() { e->setFocus(); }
293 void TextEditor::setupFileActions()
295 QToolBar *tb = addToolBar(tr("Note Actions"));
296 tb->setObjectName("noteEditorFileActions");
297 QMenu *fileMenu = menuBar()->addMenu(tr("&Note", "Menubar"));
299 QString tag = tr("Texteditor", "Shortcuts");
301 a = new QAction(QPixmap(":/fileopen.png"), tr("&Import..."), this);
302 a->setShortcut(Qt::CTRL + Qt::Key_O);
303 a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
304 switchboard.addSwitch("textLoad", shortcutScope, a, tag);
305 connect(a, SIGNAL(triggered()), this, SLOT(textLoad()));
307 fileMenu->addAction(a);
310 fileMenu->addSeparator();
311 a = new QAction(QPixmap(":/filesave.png"), tr("&Export..."), this);
312 a->setShortcut(Qt::CTRL + Qt::Key_S);
313 a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
314 switchboard.addSwitch("textSave", shortcutScope, a, tag);
315 connect(a, SIGNAL(triggered()), this, SLOT(textSave()));
317 fileMenu->addAction(a);
321 a = new QAction(tr("Export &As... (HTML)"), this);
322 connect(a, SIGNAL(triggered()), this, SLOT(textSaveAs()));
323 fileMenu->addAction(a);
324 actionFileSaveAs = a;
326 a = new QAction(tr("Export &As...(ASCII)"), this);
327 switchboard.addSwitch("textExportAsASCII", shortcutScope, a, tag);
328 connect(a, SIGNAL(triggered()), this, SLOT(textExportAsASCII()));
329 fileMenu->addAction(a);
331 actionFileSaveAs = a;
333 fileMenu->addSeparator();
334 a = new QAction(QPixmap(":/fileprint.png"), tr("&Print..."), this);
335 a->setShortcut(Qt::CTRL + Qt::Key_P);
336 switchboard.addSwitch("textPrint", shortcutScope, a, tag);
337 connect(a, SIGNAL(triggered()), this, SLOT(textPrint()));
339 fileMenu->addAction(a);
342 a = new QAction(QPixmap(":/edittrash.png"), tr("&Delete All"), this);
343 connect(a, SIGNAL(triggered()), this, SLOT(deleteAll()));
344 fileMenu->addAction(a);
346 actionFileDeleteAll = a;
349 void TextEditor::setupEditActions()
351 QString tag = tr("Texteditor", "Shortcuts");
352 QToolBar *editToolBar = addToolBar(tr("Edit Actions"));
353 editToolBar->setObjectName("noteEditorEditActions");
355 QMenu *editMenu = menuBar()->addMenu(tr("Edi&t"));
358 a = new QAction(QPixmap(":/undo.png"), tr("&Undo"), this);
359 a->setShortcut(Qt::CTRL + Qt::Key_Z);
360 a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
361 switchboard.addSwitch("textUndo", shortcutScope, a, tag);
362 connect(a, SIGNAL(triggered()), e, SLOT(undo()));
363 editMenu->addAction(a);
364 editToolBar->addAction(a);
367 a = new QAction(QPixmap(":/redo.png"), tr("&Redo"), this);
368 a->setShortcut(Qt::CTRL + Qt::Key_Y);
369 a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
370 switchboard.addSwitch("textRedo", shortcutScope, a, tag);
371 connect(a, SIGNAL(triggered()), e, SLOT(redo()));
372 editMenu->addAction(a);
373 editToolBar->addAction(a);
376 editMenu->addSeparator();
377 a = new QAction(QPixmap(), tr("Select and copy &all"), this);
378 a->setShortcutContext(Qt::WidgetShortcut);
379 a->setShortcut(Qt::CTRL + Qt::Key_A);
380 switchboard.addSwitch("textCopyAll", shortcutScope, a, tag);
381 connect(a, SIGNAL(triggered()), this, SLOT(editCopyAll()));
382 editMenu->addAction(a);
384 editMenu->addSeparator();
385 a = new QAction(QPixmap(":/editcopy.png"), tr("&Copy"), this);
386 a->setShortcut(Qt::CTRL + Qt::Key_C);
387 a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
388 switchboard.addSwitch("textCopy", shortcutScope, a, tag);
389 connect(a, SIGNAL(triggered()), e, SLOT(copy()));
390 editMenu->addAction(a);
391 editToolBar->addAction(a);
394 a = new QAction(QPixmap(":/editcut.png"), tr("Cu&t"), this);
395 a->setShortcut(Qt::CTRL + Qt::Key_X);
396 a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
397 switchboard.addSwitch("textCut", shortcutScope, a, tag);
398 connect(a, SIGNAL(triggered()), e, SLOT(cut()));
399 editMenu->addAction(a);
400 editToolBar->addAction(a);
403 a = new QAction(QPixmap(":/editpaste.png"), tr("&Paste"), this);
404 a->setShortcut(Qt::CTRL + Qt::Key_V);
405 a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
406 switchboard.addSwitch("textPaste", shortcutScope, a, tag);
407 connect(a, SIGNAL(triggered()), e, SLOT(paste()));
408 editMenu->addAction(a);
409 editToolBar->addAction(a);
413 void TextEditor::setupFormatActions()
415 QString tag = tr("Texteditor", "Shortcuts");
417 addToolBar(tr("Font hints", "toolbar in texteditor"));
418 fontHintsToolBar->setObjectName("noteEditorFontToolBar");
419 QMenu *formatMenu = menuBar()->addMenu(tr("F&ormat"));
423 a = new QAction(QPixmap(":/formatfixedfont.png"), tr("&Font hint"), this);
424 a->setShortcut(Qt::CTRL + Qt::Key_H);
425 a->setCheckable(true);
427 settings.value("/noteeditor/fonts/useFixedByDefault", false).toBool());
428 switchboard.addSwitch("textToggleFonthint", shortcutScope, a, tag);
429 connect(a, SIGNAL(triggered()), this, SLOT(toggleFonthint()));
430 formatMenu->addAction(a);
431 fontHintsToolBar->addAction(a);
432 actionFormatUseFixedFont = a;
434 // Original icon: ./share/icons/oxygen/22x22/actions/format-text-color.png
435 a = new QAction(QPixmap(":/formatrichtext.png"), tr("&Richtext"), this);
436 a->setShortcut(Qt::CTRL + Qt::Key_R);
437 // a->setShortcutContext (Qt::WidgetShortcut);
438 a->setCheckable(true);
439 switchboard.addSwitch("textToggleRichText", shortcutScope, a, tag);
440 connect(a, SIGNAL(triggered()), this, SLOT(toggleRichText()));
441 formatMenu->addAction(a);
442 fontHintsToolBar->addAction(a);
443 actionFormatRichText = a;
445 fontToolBar = addToolBar(tr("Fonts", "toolbar in texteditor"));
446 fontToolBar->setObjectName("noteEditorFontToolBar");
448 comboFont = new QComboBox;
449 fontToolBar->addWidget(comboFont);
450 QFontDatabase fontDB;
451 comboFont->insertItems(0, fontDB.families());
452 connect(comboFont, SIGNAL(activated(const QString &)), this,
453 SLOT(textFamily(const QString &)));
455 comboSize = new QComboBox;
456 fontToolBar->addWidget(comboSize);
457 QList<int> sizes = fontDB.standardSizes();
458 QList<int>::iterator it = sizes.begin();
460 while (it != sizes.end()) {
462 ++it; // increment i before using it
463 comboSize->insertItem(i, QString::number(*it));
465 connect(comboSize, SIGNAL(activated(const QString &)), this,
466 SLOT(textSize(const QString &)));
468 formatMenu->addSeparator();
470 formatToolBar = addToolBar(tr("Format", "toolbar in texteditor"));
471 formatToolBar->setObjectName("noteEditorFormatToolBar");
474 pix.fill(e->textColor());
475 a = new QAction(pix, tr("&Color..."), this);
476 formatMenu->addAction(a);
477 formatToolBar->addAction(a);
478 connect(a, SIGNAL(triggered()), this, SLOT(textColor()));
481 a = new QAction(QPixmap(":/text_bold.png"), tr("&Bold"), this);
482 a->setShortcut(Qt::CTRL + Qt::Key_B);
483 // a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
484 switchboard.addSwitch("textToggleBold", shortcutScope, a, tag);
485 connect(a, SIGNAL(triggered()), this, SLOT(textBold()));
486 formatToolBar->addAction(a);
487 formatMenu->addAction(a);
488 a->setCheckable(true);
491 a = new QAction(QPixmap(":/text_italic.png"), tr("&Italic"), this);
492 a->setShortcut(Qt::CTRL + Qt::Key_I);
493 // a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
494 switchboard.addSwitch("textToggleItalic", shortcutScope, a, tag);
495 connect(a, SIGNAL(triggered()), this, SLOT(textItalic()));
496 formatToolBar->addAction(a);
497 formatMenu->addAction(a);
498 a->setCheckable(true);
499 actionTextItalic = a;
501 a = new QAction(QPixmap(":/text_under.png"), tr("&Underline"), this);
502 a->setShortcut(Qt::CTRL + Qt::Key_U);
503 // a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
504 switchboard.addSwitch("textToggleUnderline", shortcutScope, a, tag);
505 connect(a, SIGNAL(triggered()), this, SLOT(textUnderline()));
506 formatToolBar->addAction(a);
507 formatMenu->addAction(a);
508 a->setCheckable(true);
509 // richTextWidgets.append((QWidget*)a);
510 actionTextUnderline = a;
511 formatMenu->addSeparator();
513 QActionGroup *actGrp2 = new QActionGroup(this);
514 actGrp2->setExclusive(true);
515 a = new QAction(QPixmap(":/text_sub.png"), tr("Subs&cript"), actGrp2);
516 a->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_B);
517 // a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
518 a->setCheckable(true);
519 formatToolBar->addAction(a);
520 formatMenu->addAction(a);
521 switchboard.addSwitch("textToggleSub", shortcutScope, a, tag);
522 connect(a, SIGNAL(triggered()), this, SLOT(textVAlign()));
523 actionAlignSubScript = a;
525 a = new QAction(QPixmap(":/text_super.png"), tr("Su&perscript"), actGrp2);
526 a->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_P);
527 // a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
528 a->setCheckable(true);
529 formatToolBar->addAction(a);
530 formatMenu->addAction(a);
531 switchboard.addSwitch("textToggleSuper", shortcutScope, a, tag);
532 connect(a, SIGNAL(triggered()), this, SLOT(textVAlign()));
533 actionAlignSuperScript = a;
534 QActionGroup *grp = new QActionGroup(this);
535 connect(grp, SIGNAL(triggered(QAction *)), this,
536 SLOT(textAlign(QAction *)));
538 formatMenu->addSeparator();
540 a = new QAction(QPixmap(":/text_left.png"), tr("&Left"), grp);
541 // a->setShortcut( Qt::CTRL+Qt::Key_L );
542 a->setCheckable(true);
543 formatToolBar->addAction(a);
544 formatMenu->addAction(a);
546 a = new QAction(QPixmap(":/text_center.png"), tr("C&enter"), grp);
547 // a->setShortcut( Qt::CTRL + Qt::Key_E);
548 a->setCheckable(true);
549 formatToolBar->addAction(a);
550 formatMenu->addAction(a);
551 actionAlignCenter = a;
552 a = new QAction(QPixmap(":/text_right.png"), tr("&Right"), grp);
553 // a->setShortcut(Qt::CTRL + Qt::Key_R );
554 a->setCheckable(true);
555 formatToolBar->addAction(a);
556 formatMenu->addAction(a);
557 actionAlignRight = a;
558 a = new QAction(QPixmap(":/text_block.png"), tr("&Justify"), grp);
559 // a->setShortcut(Qt::CTRL + Qt::Key_J );
560 a->setCheckable(true);
561 formatToolBar->addAction(a);
562 formatMenu->addAction(a);
563 actionAlignJustify = a;
566 void TextEditor::setupSettingsActions()
568 QMenu *settingsMenu = menuBar()->addMenu(tr("&Settings"));
571 a = new QAction(tr("Set &fixed font"), this);
572 connect(a, SIGNAL(triggered()), this, SLOT(setFixedFont()));
573 settingsMenu->addAction(a);
574 actionSettingsFixedFont = a;
576 a = new QAction(tr("Set &variable font"), this);
577 connect(a, SIGNAL(triggered()), this, SLOT(setVarFont()));
578 settingsMenu->addAction(a);
579 actionSettingsVarFont = a;
581 a = new QAction(tr("&fixed font is default"), this);
582 a->setCheckable(true);
583 // set state later in constructor...
584 settingsMenu->addAction(a);
585 actionSettingsFonthintDefault = a;
587 settingsMenu->addSeparator();
590 tr("Set RichText default background color", "TextEditor") + "...", this);
591 settingsMenu->addAction(a);
592 connect(a, SIGNAL(triggered()), this, SLOT(selectColorRichTextDefaultBackground()));
593 actionFilledEditorColor = a;
595 a = new QAction(tr("Set RichText default font color", "TextEditor") + "...", this);
596 settingsMenu->addAction(a);
597 connect(a, SIGNAL(triggered()), this, SLOT(selectColorRichTextDefaultForeground()));
601 void TextEditor::textLoad()
603 if (state != inactiveEditor) {
605 QMessageBox mb(vymName + " - " + tr("Note Editor"),
606 "Loading will overwrite the existing note",
607 QMessageBox::Warning,
608 QMessageBox::Yes | QMessageBox::Default,
609 QMessageBox::Cancel, 0);
610 mb.setButtonText(QMessageBox::Yes, "Load note");
612 case QMessageBox::Cancel:
618 QFileDialog *fd = new QFileDialog(this);
620 types << "Text (*.txt *.html)"
621 << "VYM notes and HTML (*.html)"
622 << "ASCII texts (*.txt)"
624 fd->setNameFilters(types);
625 fd->setDirectory(QDir().current());
628 if (fd->exec() == QDialog::Accepted && !fd->selectedFiles().isEmpty())
629 fn = fd->selectedFiles().first();
633 if (!f.open(QIODevice::ReadOnly))
637 setTextAuto(ts.readAll());
643 void TextEditor::closeEvent(QCloseEvent *ce)
645 ce->accept(); // TextEditor can be reopened with show()
647 emit(windowClosed());
651 bool TextEditor::eventFilter(QObject *obj, QEvent *ev)
654 if (ev->type() == QEvent::KeyPress) {
655 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
656 if (keyEvent == QKeySequence::Paste) {
657 // switch editor mode to match clipboard content before pasting
658 const QClipboard *clipboard = QApplication::clipboard();
659 const QMimeData *mimeData = clipboard->mimeData();
661 if (mimeData->hasHtml() && !actionFormatRichText->isChecked())
662 setRichTextMode(true);
666 // pass the event on to the parent class
667 return QMainWindow::eventFilter(obj, ev);
670 void TextEditor::editorChanged()
672 //qDebug() << "TE::editorChanged" << editorName << "blockChanged: " << blockChangedSignal;
673 EditorState oldState = state;
677 state = filledEditor;
679 if (!blockChangedSignal) {
680 blockTextUpdate = true;
681 emit(textHasChanged(getVymText()));
682 blockTextUpdate = false;
685 if (state == oldState)
691 void TextEditor::setRichText(const QString &t)
693 blockChangedSignal = true;
694 e->setReadOnly(false);
696 actionFormatRichText->setChecked(true);
698 // Update state including colors
702 blockChangedSignal = false;
705 void TextEditor::setPlainText(const QString &t)
707 blockChangedSignal = true;
708 e->setReadOnly(false);
711 actionFormatRichText->setChecked(false);
713 // Reset also text format
714 QTextCharFormat textformat;
715 textformat.setForeground(qApp->palette().color(QPalette::WindowText));
716 textformat.setFont(varFont);
717 e->setCurrentCharFormat(textformat);
719 // Update state including colors
723 blockChangedSignal = false;
726 void TextEditor::setTextAuto(const QString &t)
728 if (Qt::mightBeRichText(t))
734 void TextEditor::setVymText(const VymText &vt)
736 // While a note is being edited, we are sending textHasChanged
737 // Then we don't want to update the text additionally from outside,
738 // as this would position cursor at beginning of text
739 if (blockTextUpdate) return;
741 if (vt.getText() == getText()) return;
744 setRichText(vt.getText());
746 setPlainText(vt.getText());
749 void TextEditor::setInactive()
751 setState(inactiveEditor);
754 void TextEditor::editCopyAll()
760 void TextEditor::clear()
762 //qDebug() << "TE::clear" << editorName;
763 bool blockChangedOrg = blockChangedSignal;
765 blockChangedSignal = true;
767 setState(emptyEditor);
769 blockChangedSignal = blockChangedOrg;
772 void TextEditor::deleteAll()
777 void TextEditor::textSaveAs()
779 QString caption = tr("Export Note to single file");
780 QString fn = QFileDialog::getSaveFileName(
781 this, caption, QString(), "VYM Note (HTML) (*.html);;All files (*)",
782 0, QFileDialog::DontConfirmOverwrite);
789 tr("The file %1\nexists already.\nDo you want to overwrite it?",
790 "dialog 'save note as'")
792 QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default,
793 QMessageBox::Cancel | QMessageBox::Escape, Qt::NoButton);
794 mb.setButtonText(QMessageBox::Yes, tr("Overwrite"));
795 mb.setButtonText(QMessageBox::No, tr("Cancel"));
797 case QMessageBox::Yes:
802 case QMessageBox::Cancel:
813 statusBar()->showMessage(
814 tr("Couldn't export note ", "dialog 'save note as'") + fn,
818 void TextEditor::textSave()
820 if (filename.isEmpty()) {
825 QString text = e->toHtml(); // FIXME-4 or plaintext? check...
827 if (!f.open(QIODevice::WriteOnly)) {
828 statusBar()->showMessage(QString("Could not write to %1").arg(filename),
838 e->document()->setModified(false);
840 statusBar()->showMessage(QString("Note exported as %1").arg(filename),
844 void TextEditor::textExportAsASCII()
847 if (!filenameHint.isEmpty()) {
848 if (!filenameHint.contains(".txt"))
849 s = filenameHint + ".txt";
855 QString caption = tr("Export Note to single file (ASCII)");
856 fn = QFileDialog::getSaveFileName(
857 this, caption, s, "VYM Note (ASCII) (*.txt);;All files (*)");
865 tr("The file %1\nexists already.\nDo you want to overwrite it?",
866 "dialog 'save note as'")
868 QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default,
869 QMessageBox::Cancel | QMessageBox::Escape, Qt::NoButton);
870 mb.setButtonText(QMessageBox::Yes, tr("Overwrite"));
871 mb.setButtonText(QMessageBox::No, tr("Cancel"));
874 if (ret == QMessageBox::Cancel)
878 if (!file.open(QIODevice::WriteOnly))
879 statusBar()->showMessage(
880 QString("Could not write to %1").arg(filename), statusbarTime);
882 QTextStream t(&file);
883 t << getVymText().getTextASCII();
886 statusBar()->showMessage(QString("Note exported as %1").arg(fn),
892 void TextEditor::textPrint()
894 QTextDocument *document = e->document();
897 mainWindow->setupPrinter();
899 QPrintDialog dialog(printer, this);
900 dialog.setWindowTitle(tr("Print", "TextEditor"));
901 if (dialog.exec() != QDialog::Accepted)
904 document->print(printer);
907 void TextEditor::textEditUndo() {}
909 void TextEditor::toggleFonthint()
911 if (!actionFormatUseFixedFont->isChecked()) {
912 e->setCurrentFont(varFont);
916 e->setCurrentFont(fixedFont);
919 emit(textHasChanged(getVymText()));
922 void TextEditor::setRichTextMode(bool b)
924 //qDebug() << "TE::setRichTextMode b=" << b;
925 actionFormatUseFixedFont->setEnabled(false);
927 setRichText(e->toHtml());
929 // Use default foreground color for all text when switching to RichText
930 QTextCursor cursor = e->textCursor();
932 e->setTextColor(colorRichTextDefaultForeground);
933 e->setTextCursor(cursor);
936 setPlainText(e->toPlainText());
938 emit(textHasChanged(getVymText()));
941 void TextEditor::toggleRichText()
943 if (actionFormatRichText->isChecked())
944 setRichTextMode(true);
946 setRichTextMode(false);
949 void TextEditor::setFixedFont()
952 QFont font = QFontDialog::getFont(&ok, fixedFont, this);
957 void TextEditor::setVarFont()
960 QFont font = QFontDialog::getFont(&ok, varFont, this);
965 void TextEditor::textBold()
967 if (actionTextBold->isChecked())
968 e->setFontWeight(QFont::Bold);
970 e->setFontWeight(QFont::Normal);
973 void TextEditor::textUnderline()
975 e->setFontUnderline(actionTextUnderline->isChecked());
978 void TextEditor::textItalic()
980 e->setFontItalic(actionTextItalic->isChecked());
983 void TextEditor::textFamily(const QString &f) { e->setFontFamily(f); }
985 void TextEditor::textSize(const QString &p) { e->setFontPointSize(p.toInt()); }
987 void TextEditor::textColor()
989 QColor col = QColorDialog::getColor(e->textColor(), this);
992 e->setTextColor(col);
994 QPixmap pix( 16, 16 );
996 actionTextColor->setIcon( pix );
1000 void TextEditor::textAlign(QAction *a)
1002 QTextCursor c = e->textCursor();
1004 if (a == actionAlignLeft)
1005 e->setAlignment(Qt::AlignLeft);
1006 else if (a == actionAlignCenter)
1007 e->setAlignment(Qt::AlignHCenter);
1008 else if (a == actionAlignRight)
1009 e->setAlignment(Qt::AlignRight);
1010 else if (a == actionAlignJustify)
1011 e->setAlignment(Qt::AlignJustify);
1014 void TextEditor::textVAlign()
1016 QTextCharFormat format;
1018 if (sender() == actionAlignSuperScript &&
1019 actionAlignSuperScript->isChecked()) {
1020 format.setVerticalAlignment(QTextCharFormat::AlignSuperScript);
1022 else if (sender() == actionAlignSubScript &&
1023 actionAlignSubScript->isChecked()) {
1024 format.setVerticalAlignment(QTextCharFormat::AlignSubScript);
1027 format.setVerticalAlignment(QTextCharFormat::AlignNormal);
1029 e->mergeCurrentCharFormat(format);
1032 void TextEditor::fontChanged(const QFont &f)
1034 int i = comboFont->findText(f.family());
1036 comboFont->setCurrentIndex(i);
1037 i = comboSize->findText(QString::number(f.pointSize()));
1039 comboSize->setCurrentIndex(i);
1040 actionTextBold->setChecked(f.bold());
1041 actionTextItalic->setChecked(f.italic());
1042 actionTextUnderline->setChecked(f.underline());
1045 void TextEditor::colorChanged(const QColor &c)
1047 QPixmap pix(16, 16);
1049 actionTextColor->setIcon(pix);
1052 void TextEditor::formatChanged(const QTextCharFormat &f)
1054 if (!actionFormatRichText->isChecked())
1056 fontChanged(f.font());
1057 colorChanged(f.foreground().color());
1058 alignmentChanged(e->alignment());
1059 verticalAlignmentChanged(f.verticalAlignment());
1062 void TextEditor::alignmentChanged(int a)
1064 if ((a == Qt::AlignLeft) || (a & Qt::AlignLeft))
1065 actionAlignLeft->setChecked(true);
1066 else if ((a & Qt::AlignHCenter))
1067 actionAlignCenter->setChecked(true);
1068 else if ((a & Qt::AlignRight))
1069 actionAlignRight->setChecked(true);
1070 else if ((a & Qt::AlignJustify))
1071 actionAlignJustify->setChecked(true);
1074 void TextEditor::verticalAlignmentChanged(QTextCharFormat::VerticalAlignment a)
1076 actionAlignSubScript->setChecked(false);
1077 actionAlignSuperScript->setChecked(false);
1079 case QTextCharFormat::AlignSuperScript:
1080 actionAlignSuperScript->setChecked(true);
1082 case QTextCharFormat::AlignSubScript:
1083 actionAlignSubScript->setChecked(true);
1089 void TextEditor::updateActions()
1092 b = (state == inactiveEditor) ? false : true;
1094 actionFileLoad->setEnabled(b);
1095 actionFileSave->setEnabled(b);
1096 actionFileSaveAs->setEnabled(b);
1097 actionFilePrint->setEnabled(b);
1098 actionFileDeleteAll->setEnabled(b);
1099 actionEditUndo->setEnabled(b);
1100 actionEditRedo->setEnabled(b);
1101 actionEditCopy->setEnabled(b);
1102 actionEditCut->setEnabled(b);
1103 actionEditPaste->setEnabled(b);
1104 actionFormatUseFixedFont->setEnabled(b);
1105 actionFormatRichText->setEnabled(b);
1107 if (!actionFormatRichText->isChecked() || !b) {
1108 comboFont->setEnabled(false);
1109 comboSize->setEnabled(false);
1110 fontToolBar->hide();
1111 formatToolBar->hide();
1112 actionTextColor->setEnabled(false);
1113 actionTextBold->setEnabled(false);
1114 actionTextUnderline->setEnabled(false);
1115 actionTextItalic->setEnabled(false);
1116 actionTextColor->setEnabled(false);
1117 actionAlignSubScript->setEnabled(false);
1118 actionAlignSuperScript->setEnabled(false);
1119 actionAlignLeft->setEnabled(false);
1120 actionAlignCenter->setEnabled(false);
1121 actionAlignRight->setEnabled(false);
1122 actionAlignJustify->setEnabled(false);
1125 comboFont->setEnabled(true);
1126 comboSize->setEnabled(true);
1127 fontToolBar->show();
1128 formatToolBar->show();
1129 actionTextColor->setEnabled(true);
1130 actionTextBold->setEnabled(true);
1131 actionTextUnderline->setEnabled(true);
1132 actionTextItalic->setEnabled(true);
1133 actionTextColor->setEnabled(true);
1134 actionAlignSubScript->setEnabled(true);
1135 actionAlignSuperScript->setEnabled(true);
1136 actionAlignLeft->setEnabled(true);
1137 actionAlignCenter->setEnabled(true);
1138 actionAlignRight->setEnabled(true);
1139 actionAlignJustify->setEnabled(true);
1140 actionFormatUseFixedFont->setEnabled(false);
1144 void TextEditor::setState(EditorState s) // FIXME-2 called 12x when reselecting once in ME
1145 // 5 alone for HeadingEditor
1147 //qDebug() << "TE::setState" << s << editorName;
1148 QPalette p = qApp->palette();
1153 if (actionFormatRichText->isChecked())
1154 e->setTextColor(colorRichTextDefaultForeground);
1156 e->setTextColor(p.color(QPalette::Text));
1159 if (actionFormatRichText->isChecked()) {
1160 if (useColorMapBackground)
1161 baseColor = colorMapBackground;
1163 baseColor = colorRichTextDefaultBackground;
1165 baseColor = p.color(QPalette::Base);
1167 e->setReadOnly(false);
1169 case inactiveEditor:
1170 baseColor = Qt::black;
1171 e->setReadOnly(true);
1173 p.setColor(QPalette::Base, baseColor);
1179 void TextEditor::updateState()
1181 //qDebug() << "TE::updateState" << editorName;
1183 setState(emptyEditor);
1185 setState(filledEditor);
1188 void TextEditor::selectColorRichTextDefaultBackground()
1190 QColor col = QColorDialog::getColor(colorRichTextDefaultBackground, nullptr);
1193 colorRichTextDefaultBackground = col;
1194 QPixmap pix(16, 16);
1195 pix.fill(colorRichTextDefaultBackground);
1196 actionFilledEditorColor->setIcon(pix);
1199 void TextEditor::selectColorRichTextDefaultForeground()
1201 QColor col = QColorDialog::getColor(colorRichTextDefaultForeground, nullptr);
1204 setColorRichTextDefaultForeground(col);
1207 void TextEditor::setColorRichTextDefaultForeground(const QColor &col)
1209 if (!col.isValid()) return;
1211 colorRichTextDefaultForeground = col;
1212 QPixmap pix(16, 16);
1213 pix.fill(colorRichTextDefaultForeground);
1214 actionFontColor->setIcon(pix);
1217 void TextEditor::setColorMapBackground(const QColor &col)
1219 colorMapBackground = col;
1222 void TextEditor::setUseColorMapBackground(bool b)
1224 useColorMapBackground = b;