fb2main.cpp 19 KB

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