1
0

fb2main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. menuBar()->addSeparator();
  328. menu = menuBar()->addMenu(tr("&Help"));
  329. act = new QAction(icon("help-about"), tr("&About"), this);
  330. act->setStatusTip(tr("Show the application's About box"));
  331. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  332. menu->addAction(act);
  333. act = new QAction(tr("About &Qt"), this);
  334. act->setStatusTip(tr("Show the Qt library's About box"));
  335. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  336. menu->addAction(act);
  337. }
  338. void MainWindow::connectTextDocument(QTextDocument * document)
  339. {
  340. connect(document, SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  341. connect(document, SIGNAL(undoAvailable(bool)), actionUndo, SLOT(setEnabled(bool)));
  342. connect(document, SIGNAL(redoAvailable(bool)), actionRedo, SLOT(setEnabled(bool)));
  343. }
  344. void MainWindow::createText()
  345. {
  346. textEdit = new QWebView(this);
  347. textEdit->page()->setContentEditable(true);
  348. setCentralWidget(textEdit);
  349. textEdit->setFocus();
  350. }
  351. void MainWindow::createQsci()
  352. {
  353. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  354. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  355. qsciEdit = new QsciScintilla;
  356. qsciEdit->setUtf8(true);
  357. qsciEdit->setCaretLineVisible(true);
  358. qsciEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  359. qsciEdit->setWrapMode(QsciScintilla::WrapWord);
  360. qsciEdit->setEolMode(QsciScintilla::EolWindows);
  361. qsciEdit->setAutoIndent(true);
  362. qsciEdit->setIndentationGuides(true);
  363. qsciEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  364. qsciEdit->setAutoCompletionCaseSensitivity(true);
  365. qsciEdit->setAutoCompletionReplaceWord(true);
  366. qsciEdit->setAutoCompletionShowSingle(true);
  367. qsciEdit->setAutoCompletionThreshold(2);
  368. qsciEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  369. qsciEdit->setMarginWidth(0, 0);
  370. qsciEdit->setMarginLineNumbers(1, true);
  371. qsciEdit->setMarginWidth(1, QString("10000"));
  372. qsciEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
  373. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  374. qsciEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  375. qsciEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  376. QFont font("Courier", 10);
  377. font.setStyleHint(QFont::TypeWriter);
  378. QsciLexerXML * lexer = new QsciLexerXML;
  379. lexer->setFont(font, -1);
  380. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  381. qsciEdit->setLexer(lexer);
  382. setCentralWidget(qsciEdit);
  383. qsciEdit->setFocus();
  384. // connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  385. // connect(qsciEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(cursorMoved(int, int)));
  386. actionUndo->setEnabled(false);
  387. actionRedo->setEnabled(false);
  388. }
  389. void MainWindow::createStatusBar()
  390. {
  391. statusBar()->showMessage(tr("Ready"));
  392. }
  393. void MainWindow::readSettings()
  394. {
  395. QSettings settings;
  396. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  397. QSize size = settings.value("size", QSize(400, 400)).toSize();
  398. move(pos);
  399. resize(size);
  400. }
  401. void MainWindow::writeSettings()
  402. {
  403. QSettings settings;
  404. settings.setValue("pos", pos());
  405. settings.setValue("size", size());
  406. }
  407. bool MainWindow::maybeSave()
  408. {
  409. if (textEdit && textEdit->isModified()) {
  410. QMessageBox::StandardButton ret;
  411. ret = QMessageBox::warning(this, tr("SDI"),
  412. tr("The document has been modified.\n"
  413. "Do you want to save your changes?"),
  414. QMessageBox::Save | QMessageBox::Discard
  415. | QMessageBox::Cancel);
  416. if (ret == QMessageBox::Save)
  417. return fileSave();
  418. else if (ret == QMessageBox::Cancel)
  419. return false;
  420. }
  421. return true;
  422. }
  423. bool MainWindow::saveFile(const QString &fileName)
  424. {
  425. return false;
  426. QFile file(fileName);
  427. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  428. QMessageBox::warning(this, tr("SDI"),
  429. tr("Cannot write file %1:\n%2.")
  430. .arg(fileName)
  431. .arg(file.errorString()));
  432. return false;
  433. }
  434. QTextStream out(&file);
  435. QApplication::setOverrideCursor(Qt::WaitCursor);
  436. // out << textEdit->toPlainText();
  437. QApplication::restoreOverrideCursor();
  438. setCurrentFile(fileName);
  439. statusBar()->showMessage(tr("File saved"), 2000);
  440. return true;
  441. }
  442. void MainWindow::setCurrentFile(const QString &filename, const QString &html)
  443. {
  444. static int sequenceNumber = 1;
  445. QString title;
  446. isUntitled = filename.isEmpty();
  447. if (isUntitled) {
  448. curFile = tr("book%1.fb2").arg(sequenceNumber++);
  449. title = curFile;
  450. } else {
  451. QFileInfo info = filename;
  452. curFile = info.canonicalFilePath();
  453. title = info.fileName();
  454. }
  455. title += QString(" - ") += qApp->applicationName();
  456. textEdit->setHtml(html);
  457. textEdit->page()->setContentEditable(true);
  458. setWindowModified(false);
  459. setWindowFilePath(curFile);
  460. setWindowTitle(title);
  461. }
  462. MainWindow *MainWindow::findMainWindow(const QString &fileName)
  463. {
  464. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  465. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  466. MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
  467. if (mainWin && mainWin->curFile == canonicalFilePath)
  468. return mainWin;
  469. }
  470. return 0;
  471. }
  472. void MainWindow::viewQsci()
  473. {
  474. if (centralWidget() == qsciEdit) return;
  475. QString html;
  476. if (textEdit) {
  477. html = textEdit->page()->mainFrame()->toHtml();
  478. delete textEdit;
  479. textEdit = NULL;
  480. }
  481. createQsci();
  482. qsciEdit->setText(html);
  483. }
  484. void MainWindow::viewText()
  485. {
  486. if (centralWidget() == textEdit) return;
  487. if (qsciEdit) { delete qsciEdit; qsciEdit = NULL; }
  488. createText();
  489. }
  490. void MainWindow::currentCharFormatChanged(const QTextCharFormat &format)
  491. {
  492. QFont font = format.font();
  493. actionTextBold -> setChecked(font.bold());
  494. actionTextItalic -> setChecked(font.italic());
  495. actionTextStrike -> setChecked(font.strikeOut());
  496. actionTextSub -> setChecked(format.verticalAlignment() == QTextCharFormat::AlignSubScript);
  497. actionTextSup -> setChecked(format.verticalAlignment() == QTextCharFormat::AlignSuperScript);
  498. }
  499. void MainWindow::cursorPositionChanged()
  500. {
  501. // alignmentChanged(textEdit->alignment());
  502. }
  503. void MainWindow::clipboardDataChanged()
  504. {
  505. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  506. actionPaste->setEnabled(md->hasText());
  507. }
  508. }
  509. void MainWindow::textBold()
  510. {
  511. QTextCharFormat fmt;
  512. fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
  513. mergeFormatOnWordOrSelection(fmt);
  514. }
  515. void MainWindow::textItalic()
  516. {
  517. QTextCharFormat fmt;
  518. fmt.setFontItalic(actionTextItalic->isChecked());
  519. mergeFormatOnWordOrSelection(fmt);
  520. }
  521. void MainWindow::textStrike()
  522. {
  523. QTextCharFormat fmt;
  524. fmt.setFontStrikeOut(actionTextStrike->isChecked());
  525. mergeFormatOnWordOrSelection(fmt);
  526. }
  527. void MainWindow::textSub()
  528. {
  529. QTextCharFormat fmt;
  530. fmt.setVerticalAlignment(actionTextSub->isChecked() ? QTextCharFormat::AlignSubScript : QTextCharFormat::AlignNormal);
  531. mergeFormatOnWordOrSelection(fmt);
  532. }
  533. void MainWindow::textSup()
  534. {
  535. QTextCharFormat fmt;
  536. fmt.setVerticalAlignment(actionTextSup->isChecked() ? QTextCharFormat::AlignSuperScript : QTextCharFormat::AlignNormal);
  537. mergeFormatOnWordOrSelection(fmt);
  538. }
  539. void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
  540. {
  541. /*
  542. if (!textEdit) return;
  543. QTextCursor cursor = textEdit->textCursor();
  544. cursor.beginEditBlock();
  545. if (!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor);
  546. cursor.mergeCharFormat(format);
  547. textEdit->mergeCurrentCharFormat(format);
  548. cursor.endEditBlock();
  549. */
  550. }