fb2main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include <QTreeView>
  4. #include <QWebView>
  5. #include <QWebFrame>
  6. #include "fb2main.h"
  7. #include "fb2doc.h"
  8. #include "fb2read.h"
  9. #include "fb2text.h"
  10. #include "fb2tree.h"
  11. #include "fb2view.h"
  12. #include "fb2main.h"
  13. #include <Qsci/qsciscintilla.h>
  14. #include <Qsci/qscilexerxml.h>
  15. MainWindow::MainWindow()
  16. {
  17. init();
  18. createText();
  19. setCurrentFile("");
  20. }
  21. MainWindow::MainWindow(const QString &filename)
  22. {
  23. init();
  24. createQsci();
  25. loadXML(filename);
  26. setCurrentFile(filename);
  27. }
  28. MainWindow::MainWindow(const QString &filename, Fb2MainDocument * document)
  29. {
  30. init();
  31. createText();
  32. thread = new Fb2ReadThread(this, filename);
  33. connect(thread, SIGNAL(sendDocument()), SLOT(sendDocument()));
  34. thread->start();
  35. }
  36. void MainWindow::init()
  37. {
  38. thread = NULL;
  39. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  40. setAttribute(Qt::WA_DeleteOnClose);
  41. isUntitled = true;
  42. createActions();
  43. createStatusBar();
  44. textEdit = NULL;
  45. noteEdit = NULL;
  46. qsciEdit = NULL;
  47. treeView = NULL;
  48. messageEdit = NULL;
  49. readSettings();
  50. setUnifiedTitleAndToolBarOnMac(true);
  51. }
  52. void MainWindow::logMessage(const QString &message)
  53. {
  54. if (!messageEdit) {
  55. messageEdit = new QTextEdit(this);
  56. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  57. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  58. dock->setAttribute(Qt::WA_DeleteOnClose);
  59. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  60. dock->setWidget(messageEdit);
  61. addDockWidget(Qt::BottomDockWidgetArea, dock);
  62. messageEdit->setMaximumHeight(80);
  63. }
  64. messageEdit->append(message);
  65. }
  66. void MainWindow::logShowed()
  67. {
  68. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  69. }
  70. void MainWindow::logDestroyed()
  71. {
  72. messageEdit = NULL;
  73. }
  74. static QTextFrame * findFrame(QTextFrame *root, const QModelIndex &index)
  75. {
  76. if (!index.isValid()) return root;
  77. if (QTextFrame *frame = findFrame(root, index.parent())) {
  78. QTextFrame *child = static_cast<QTextFrame*>(index.internalPointer());
  79. int i = frame->childFrames().indexOf(child);
  80. if (i >= 0) return child;
  81. }
  82. return NULL;
  83. }
  84. void MainWindow::treeActivated(const QModelIndex &index)
  85. {
  86. }
  87. void MainWindow::treeDestroyed()
  88. {
  89. treeView = NULL;
  90. }
  91. bool MainWindow::loadXML(const QString &filename)
  92. {
  93. if (!filename.isEmpty()) {
  94. QFile file(filename);
  95. if (file.open(QFile::ReadOnly | QFile::Text)) {
  96. qsciEdit->clear();
  97. return qsciEdit->read(&file);
  98. }
  99. }
  100. return false;
  101. }
  102. void MainWindow::closeEvent(QCloseEvent *event)
  103. {
  104. if (maybeSave()) {
  105. writeSettings();
  106. event->accept();
  107. } else {
  108. event->ignore();
  109. }
  110. }
  111. void MainWindow::fileNew()
  112. {
  113. MainWindow *other = new MainWindow;
  114. other->move(x() + 40, y() + 40);
  115. other->show();
  116. }
  117. void MainWindow::fileOpen()
  118. {
  119. QString filename = QFileDialog::getOpenFileName(this);
  120. if (filename.isEmpty()) return;
  121. MainWindow * existing = findMainWindow(filename);
  122. if (existing) {
  123. existing->show();
  124. existing->raise();
  125. existing->activateWindow();
  126. return;
  127. }
  128. if (textEdit) {
  129. if (isUntitled && !isWindowModified()) {
  130. thread = new Fb2ReadThread(this, filename);
  131. connect(thread, SIGNAL(sendDocument()), SLOT(sendDocument()));
  132. thread->start();
  133. } else {
  134. MainWindow * other = new MainWindow(filename, NULL);
  135. other->move(x() + 40, y() + 40);
  136. other->show();
  137. }
  138. } else if (qsciEdit) {
  139. if (isUntitled && !isWindowModified()) {
  140. loadXML(filename);
  141. setCurrentFile(filename);
  142. } else {
  143. MainWindow * other = new MainWindow(filename);
  144. other->move(x() + 40, y() + 40);
  145. other->show();
  146. }
  147. }
  148. }
  149. void MainWindow::sendDocument()
  150. {
  151. setCurrentFile(thread->file(), thread->html());
  152. return;
  153. treeView = new QTreeView(this);
  154. // treeView->setModel(new Fb2TreeModel(*textEdit));
  155. treeView->setHeaderHidden(true);
  156. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  157. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  158. QDockWidget * dock = new QDockWidget(tr("Contents"), this);
  159. dock->setAttribute(Qt::WA_DeleteOnClose);
  160. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  161. dock->setWidget(treeView);
  162. addDockWidget(Qt::LeftDockWidgetArea, dock);
  163. }
  164. bool MainWindow::fileSave()
  165. {
  166. if (isUntitled) {
  167. return fileSaveAs();
  168. } else {
  169. return saveFile(curFile);
  170. }
  171. }
  172. bool MainWindow::fileSaveAs()
  173. {
  174. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
  175. if (fileName.isEmpty()) return false;
  176. return saveFile(fileName);
  177. }
  178. void MainWindow::about()
  179. {
  180. QMessageBox::about(this, tr("About SDI"),
  181. tr("The <b>SDI</b> example demonstrates how to write single "
  182. "document interface applications using Qt."));
  183. }
  184. void MainWindow::documentWasModified()
  185. {
  186. QFileInfo info = windowFilePath();
  187. QString title = info.fileName();
  188. title += QString("[*]") += QString(" - ") += qApp->applicationName();
  189. setWindowTitle(title);
  190. setWindowModified(true);
  191. }
  192. QIcon MainWindow::icon(const QString &name)
  193. {
  194. QIcon icon;
  195. icon.addFile(QString(":/images/24x24/%1.png").arg(name), QSize(24,24));
  196. icon.addFile(QString(":/images/16x16/%1.png").arg(name), QSize(16,16));
  197. return QIcon::fromTheme(name, icon);
  198. }
  199. void MainWindow::createActions()
  200. {
  201. QAction * act;
  202. QMenu * menu;
  203. QToolBar * tool;
  204. QList<QAction*> actions;
  205. menu = menuBar()->addMenu(tr("&File"));
  206. tool = addToolBar(tr("File"));
  207. act = new QAction(icon("document-new"), tr("&New"), this);
  208. act->setPriority(QAction::LowPriority);
  209. act->setShortcuts(QKeySequence::New);
  210. act->setStatusTip(tr("Create a new file"));
  211. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  212. menu->addAction(act);
  213. tool->addAction(act);
  214. act = new QAction(icon("document-open"), tr("&Open..."), this);
  215. act->setShortcuts(QKeySequence::Open);
  216. act->setStatusTip(tr("Open an existing file"));
  217. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  218. menu->addAction(act);
  219. tool->addAction(act);
  220. act = new QAction(icon("document-save"), tr("&Save"), this);
  221. act->setShortcuts(QKeySequence::Save);
  222. act->setStatusTip(tr("Save the document to disk"));
  223. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  224. menu->addAction(act);
  225. tool->addAction(act);
  226. act = new QAction(icon("document-save-as"), tr("Save &As..."), this);
  227. act->setShortcuts(QKeySequence::SaveAs);
  228. act->setStatusTip(tr("Save the document under a new name"));
  229. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  230. menu->addAction(act);
  231. menu->addSeparator();
  232. act = new QAction(icon("window-close"), tr("&Close"), this);
  233. act->setShortcuts(QKeySequence::Close);
  234. act->setStatusTip(tr("Close this window"));
  235. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  236. menu->addAction(act);
  237. act = new QAction(icon("application-exit"), tr("E&xit"), this);
  238. act->setShortcuts(QKeySequence::Quit);
  239. act->setStatusTip(tr("Exit the application"));
  240. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  241. menu->addAction(act);
  242. menu = menuBar()->addMenu(tr("&Edit"));
  243. tool = addToolBar(tr("Edit"));
  244. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  245. actionUndo = act = new QAction(icon("edit-undo"), tr("&Undo"), this);
  246. act->setPriority(QAction::LowPriority);
  247. act->setShortcut(QKeySequence::Undo);
  248. menu->addAction(act);
  249. tool->addAction(act);
  250. actionRedo = act = new QAction(icon("edit-redo"), tr("&Redo"), this);
  251. act->setPriority(QAction::LowPriority);
  252. act->setShortcut(QKeySequence::Redo);
  253. menu->addAction(act);
  254. tool->addAction(act);
  255. menu->addSeparator();
  256. tool->addSeparator();
  257. actionCut = act = new QAction(icon("edit-cut"), tr("Cu&t"), this);
  258. act->setPriority(QAction::LowPriority);
  259. act->setShortcuts(QKeySequence::Cut);
  260. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  261. act->setEnabled(false);
  262. menu->addAction(act);
  263. tool->addAction(act);
  264. actionCopy = act = new QAction(icon("edit-copy"), tr("&Copy"), this);
  265. act->setPriority(QAction::LowPriority);
  266. act->setShortcuts(QKeySequence::Copy);
  267. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  268. act->setEnabled(false);
  269. menu->addAction(act);
  270. tool->addAction(act);
  271. actionPaste = act = new QAction(icon("edit-paste"), tr("&Paste"), this);
  272. act->setPriority(QAction::LowPriority);
  273. act->setShortcuts(QKeySequence::Paste);
  274. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  275. menu->addAction(act);
  276. tool->addAction(act);
  277. clipboardDataChanged();
  278. menu = menuBar()->addMenu(tr("Format"));
  279. tool = addToolBar(tr("Format"));
  280. actionTextBold = act = new QAction(icon("format-text-bold"), tr("Bold"), this);
  281. act->setShortcuts(QKeySequence::Bold);
  282. act->setCheckable(true);
  283. connect(act, SIGNAL(triggered()), SLOT(textBold()));
  284. menu->addAction(act);
  285. tool->addAction(act);
  286. actionTextItalic = act = new QAction(icon("format-text-italic"), tr("Italic"), this);
  287. act->setShortcuts(QKeySequence::Italic);
  288. act->setCheckable(true);
  289. connect(act, SIGNAL(triggered()), SLOT(textItalic()));
  290. menu->addAction(act);
  291. tool->addAction(act);
  292. actionTextStrike = act = new QAction(icon("format-text-strikethrough"), tr("Strikethrough"), this);
  293. act->setCheckable(true);
  294. connect(act, SIGNAL(triggered()), SLOT(textStrike()));
  295. menu->addAction(act);
  296. tool->addAction(act);
  297. actionTextSup = act = new QAction(icon("format-text-superscript"), tr("Superscript"), this);
  298. act->setCheckable(true);
  299. connect(act, SIGNAL(triggered()), SLOT(textSup()));
  300. menu->addAction(act);
  301. tool->addAction(act);
  302. actionTextSub = act = new QAction(icon("format-text-subscript"), tr("Subscript"), this);
  303. act->setCheckable(true);
  304. connect(act, SIGNAL(triggered()), SLOT(textSub()));
  305. menu->addAction(act);
  306. tool->addAction(act);
  307. menu = menuBar()->addMenu(tr("&View"));
  308. QAction * actText = act = new QAction(tr("&Text"), this);
  309. act->setCheckable(true);
  310. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  311. QAction * actQsci = act = new QAction(tr("&XML"), this);
  312. act->setCheckable(true);
  313. connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
  314. QActionGroup * viewGroup = new QActionGroup(this);
  315. viewGroup->addAction(actText);
  316. viewGroup->addAction(actQsci);
  317. actText->setChecked(true);
  318. menu->addAction(actText);
  319. menu->addAction(actQsci);
  320. menu->addSeparator();
  321. tool = addToolBar(tr("Zoom"));
  322. actionZoomIn = act = new QAction(icon("zoom-in"), tr("Zoom in"), this);
  323. act->setShortcuts(QKeySequence::ZoomIn);
  324. menu->addAction(act);
  325. tool->addAction(act);
  326. actionZoomOut = act = new QAction(icon("zoom-out"), tr("Zoom out"), this);
  327. act->setShortcuts(QKeySequence::ZoomOut);
  328. menu->addAction(act);
  329. tool->addAction(act);
  330. actionZoomOrig = act = new QAction(icon("zoom-original"), tr("Zoom original"), this);
  331. menu->addAction(act);
  332. tool->addAction(act);
  333. menuBar()->addSeparator();
  334. menu = menuBar()->addMenu(tr("&Help"));
  335. act = new QAction(icon("help-about"), tr("&About"), this);
  336. act->setStatusTip(tr("Show the application's About box"));
  337. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  338. menu->addAction(act);
  339. act = new QAction(tr("About &Qt"), this);
  340. act->setStatusTip(tr("Show the Qt library's About box"));
  341. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  342. menu->addAction(act);
  343. }
  344. void MainWindow::connectTextDocument(QTextDocument * document)
  345. {
  346. connect(document, SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  347. connect(document, SIGNAL(undoAvailable(bool)), actionUndo, SLOT(setEnabled(bool)));
  348. connect(document, SIGNAL(redoAvailable(bool)), actionRedo, SLOT(setEnabled(bool)));
  349. }
  350. void MainWindow::createText()
  351. {
  352. textEdit = new Fb2WebView(this);
  353. setCentralWidget(textEdit);
  354. textEdit->setFocus();
  355. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  356. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  357. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  358. }
  359. void MainWindow::createQsci()
  360. {
  361. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  362. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  363. qsciEdit = new QsciScintilla;
  364. qsciEdit->setUtf8(true);
  365. qsciEdit->setCaretLineVisible(true);
  366. qsciEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  367. qsciEdit->setWrapMode(QsciScintilla::WrapWord);
  368. qsciEdit->setEolMode(QsciScintilla::EolWindows);
  369. qsciEdit->setAutoIndent(true);
  370. qsciEdit->setIndentationGuides(true);
  371. qsciEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  372. qsciEdit->setAutoCompletionCaseSensitivity(true);
  373. qsciEdit->setAutoCompletionReplaceWord(true);
  374. qsciEdit->setAutoCompletionShowSingle(true);
  375. qsciEdit->setAutoCompletionThreshold(2);
  376. qsciEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  377. qsciEdit->setMarginWidth(0, 0);
  378. qsciEdit->setMarginLineNumbers(1, true);
  379. qsciEdit->setMarginWidth(1, QString("10000"));
  380. qsciEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
  381. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  382. qsciEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  383. qsciEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  384. QFont font("Courier", 10);
  385. font.setStyleHint(QFont::TypeWriter);
  386. QsciLexerXML * lexer = new QsciLexerXML;
  387. lexer->setFont(font, -1);
  388. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  389. qsciEdit->setLexer(lexer);
  390. setCentralWidget(qsciEdit);
  391. qsciEdit->setFocus();
  392. // connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  393. // connect(qsciEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(cursorMoved(int, int)));
  394. actionUndo->setEnabled(false);
  395. actionRedo->setEnabled(false);
  396. }
  397. void MainWindow::createStatusBar()
  398. {
  399. statusBar()->showMessage(tr("Ready"));
  400. }
  401. void MainWindow::readSettings()
  402. {
  403. QSettings settings;
  404. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  405. QSize size = settings.value("size", QSize(400, 400)).toSize();
  406. move(pos);
  407. resize(size);
  408. }
  409. void MainWindow::writeSettings()
  410. {
  411. QSettings settings;
  412. settings.setValue("pos", pos());
  413. settings.setValue("size", size());
  414. }
  415. bool MainWindow::maybeSave()
  416. {
  417. if (textEdit && textEdit->isModified()) {
  418. QMessageBox::StandardButton ret;
  419. ret = QMessageBox::warning(this, tr("SDI"),
  420. tr("The document has been modified.\n"
  421. "Do you want to save your changes?"),
  422. QMessageBox::Save | QMessageBox::Discard
  423. | QMessageBox::Cancel);
  424. if (ret == QMessageBox::Save)
  425. return fileSave();
  426. else if (ret == QMessageBox::Cancel)
  427. return false;
  428. }
  429. return true;
  430. }
  431. bool MainWindow::saveFile(const QString &fileName)
  432. {
  433. return false;
  434. QFile file(fileName);
  435. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  436. QMessageBox::warning(this, tr("SDI"),
  437. tr("Cannot write file %1:\n%2.")
  438. .arg(fileName)
  439. .arg(file.errorString()));
  440. return false;
  441. }
  442. QTextStream out(&file);
  443. QApplication::setOverrideCursor(Qt::WaitCursor);
  444. // out << textEdit->toPlainText();
  445. QApplication::restoreOverrideCursor();
  446. setCurrentFile(fileName);
  447. statusBar()->showMessage(tr("File saved"), 2000);
  448. return true;
  449. }
  450. void MainWindow::setCurrentFile(const QString &filename, const QString &html)
  451. {
  452. static int sequenceNumber = 1;
  453. QString title;
  454. isUntitled = filename.isEmpty();
  455. if (isUntitled) {
  456. curFile = tr("book%1.fb2").arg(sequenceNumber++);
  457. title = curFile;
  458. } else {
  459. QFileInfo info = filename;
  460. curFile = info.canonicalFilePath();
  461. title = info.fileName();
  462. }
  463. title += QString(" - ") += qApp->applicationName();
  464. textEdit->setHtml(html, QUrl("fb2://s/"));
  465. setWindowModified(false);
  466. setWindowFilePath(curFile);
  467. setWindowTitle(title);
  468. }
  469. MainWindow *MainWindow::findMainWindow(const QString &fileName)
  470. {
  471. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  472. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  473. MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
  474. if (mainWin && mainWin->curFile == canonicalFilePath)
  475. return mainWin;
  476. }
  477. return 0;
  478. }
  479. void MainWindow::viewQsci()
  480. {
  481. if (centralWidget() == qsciEdit) return;
  482. QString html;
  483. if (textEdit) {
  484. html = textEdit->page()->mainFrame()->toHtml();
  485. delete textEdit;
  486. textEdit = NULL;
  487. }
  488. createQsci();
  489. qsciEdit->setText(html);
  490. }
  491. void MainWindow::viewText()
  492. {
  493. if (centralWidget() == textEdit) return;
  494. if (qsciEdit) { delete qsciEdit; qsciEdit = NULL; }
  495. createText();
  496. }
  497. void MainWindow::currentCharFormatChanged(const QTextCharFormat &format)
  498. {
  499. QFont font = format.font();
  500. actionTextBold -> setChecked(font.bold());
  501. actionTextItalic -> setChecked(font.italic());
  502. actionTextStrike -> setChecked(font.strikeOut());
  503. actionTextSub -> setChecked(format.verticalAlignment() == QTextCharFormat::AlignSubScript);
  504. actionTextSup -> setChecked(format.verticalAlignment() == QTextCharFormat::AlignSuperScript);
  505. }
  506. void MainWindow::cursorPositionChanged()
  507. {
  508. // alignmentChanged(textEdit->alignment());
  509. }
  510. void MainWindow::clipboardDataChanged()
  511. {
  512. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  513. actionPaste->setEnabled(md->hasText());
  514. }
  515. }
  516. void MainWindow::textBold()
  517. {
  518. QTextCharFormat fmt;
  519. fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
  520. mergeFormatOnWordOrSelection(fmt);
  521. }
  522. void MainWindow::textItalic()
  523. {
  524. QTextCharFormat fmt;
  525. fmt.setFontItalic(actionTextItalic->isChecked());
  526. mergeFormatOnWordOrSelection(fmt);
  527. }
  528. void MainWindow::textStrike()
  529. {
  530. QTextCharFormat fmt;
  531. fmt.setFontStrikeOut(actionTextStrike->isChecked());
  532. mergeFormatOnWordOrSelection(fmt);
  533. }
  534. void MainWindow::textSub()
  535. {
  536. QTextCharFormat fmt;
  537. fmt.setVerticalAlignment(actionTextSub->isChecked() ? QTextCharFormat::AlignSubScript : QTextCharFormat::AlignNormal);
  538. mergeFormatOnWordOrSelection(fmt);
  539. }
  540. void MainWindow::textSup()
  541. {
  542. QTextCharFormat fmt;
  543. fmt.setVerticalAlignment(actionTextSup->isChecked() ? QTextCharFormat::AlignSuperScript : QTextCharFormat::AlignNormal);
  544. mergeFormatOnWordOrSelection(fmt);
  545. }
  546. void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
  547. {
  548. /*
  549. if (!textEdit) return;
  550. QTextCursor cursor = textEdit->textCursor();
  551. cursor.beginEditBlock();
  552. if (!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor);
  553. cursor.mergeCharFormat(format);
  554. textEdit->mergeCurrentCharFormat(format);
  555. cursor.endEditBlock();
  556. */
  557. }