fb2main.cpp 20 KB

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