/demos/textedit/textedit.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 734 lines · 595 code · 95 blank · 44 comment · 60 complexity · 34eef4afb40c1975328359ec35318378 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the demonstration applications of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "textedit.h"
  42. #include <QAction>
  43. #include <QApplication>
  44. #include <QClipboard>
  45. #include <QColorDialog>
  46. #include <QComboBox>
  47. #include <QFontComboBox>
  48. #include <QFile>
  49. #include <QFileDialog>
  50. #include <QFileInfo>
  51. #include <QFontDatabase>
  52. #include <QMenu>
  53. #include <QMenuBar>
  54. #include <QPrintDialog>
  55. #include <QPrinter>
  56. #include <QTextCodec>
  57. #include <QTextEdit>
  58. #include <QToolBar>
  59. #include <QTextCursor>
  60. #include <QTextDocumentWriter>
  61. #include <QTextList>
  62. #include <QtDebug>
  63. #include <QCloseEvent>
  64. #include <QMessageBox>
  65. #include <QPrintPreviewDialog>
  66. #ifdef Q_WS_MAC
  67. const QString rsrcPath = ":/images/mac";
  68. #else
  69. const QString rsrcPath = ":/images/win";
  70. #endif
  71. TextEdit::TextEdit(QWidget *parent)
  72. : QMainWindow(parent)
  73. {
  74. setToolButtonStyle(Qt::ToolButtonFollowStyle);
  75. setupFileActions();
  76. setupEditActions();
  77. setupTextActions();
  78. {
  79. QMenu *helpMenu = new QMenu(tr("Help"), this);
  80. menuBar()->addMenu(helpMenu);
  81. helpMenu->addAction(tr("About"), this, SLOT(about()));
  82. helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt()));
  83. }
  84. textEdit = new QTextEdit(this);
  85. connect(textEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
  86. this, SLOT(currentCharFormatChanged(QTextCharFormat)));
  87. connect(textEdit, SIGNAL(cursorPositionChanged()),
  88. this, SLOT(cursorPositionChanged()));
  89. setCentralWidget(textEdit);
  90. textEdit->setFocus();
  91. setCurrentFileName(QString());
  92. fontChanged(textEdit->font());
  93. colorChanged(textEdit->textColor());
  94. alignmentChanged(textEdit->alignment());
  95. connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
  96. actionSave, SLOT(setEnabled(bool)));
  97. connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
  98. this, SLOT(setWindowModified(bool)));
  99. connect(textEdit->document(), SIGNAL(undoAvailable(bool)),
  100. actionUndo, SLOT(setEnabled(bool)));
  101. connect(textEdit->document(), SIGNAL(redoAvailable(bool)),
  102. actionRedo, SLOT(setEnabled(bool)));
  103. setWindowModified(textEdit->document()->isModified());
  104. actionSave->setEnabled(textEdit->document()->isModified());
  105. actionUndo->setEnabled(textEdit->document()->isUndoAvailable());
  106. actionRedo->setEnabled(textEdit->document()->isRedoAvailable());
  107. connect(actionUndo, SIGNAL(triggered()), textEdit, SLOT(undo()));
  108. connect(actionRedo, SIGNAL(triggered()), textEdit, SLOT(redo()));
  109. actionCut->setEnabled(false);
  110. actionCopy->setEnabled(false);
  111. connect(actionCut, SIGNAL(triggered()), textEdit, SLOT(cut()));
  112. connect(actionCopy, SIGNAL(triggered()), textEdit, SLOT(copy()));
  113. connect(actionPaste, SIGNAL(triggered()), textEdit, SLOT(paste()));
  114. connect(textEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  115. connect(textEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  116. #ifndef QT_NO_CLIPBOARD
  117. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  118. #endif
  119. QString initialFile = ":/example.html";
  120. const QStringList args = QCoreApplication::arguments();
  121. if (args.count() == 2)
  122. initialFile = args.at(1);
  123. if (!load(initialFile))
  124. fileNew();
  125. }
  126. void TextEdit::closeEvent(QCloseEvent *e)
  127. {
  128. if (maybeSave())
  129. e->accept();
  130. else
  131. e->ignore();
  132. }
  133. void TextEdit::setupFileActions()
  134. {
  135. QToolBar *tb = new QToolBar(this);
  136. tb->setWindowTitle(tr("File Actions"));
  137. addToolBar(tb);
  138. QMenu *menu = new QMenu(tr("&File"), this);
  139. menuBar()->addMenu(menu);
  140. QAction *a;
  141. QIcon newIcon = QIcon::fromTheme("document-new", QIcon(rsrcPath + "/filenew.png"));
  142. a = new QAction( newIcon, tr("&New"), this);
  143. a->setPriority(QAction::LowPriority);
  144. a->setShortcut(QKeySequence::New);
  145. connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
  146. tb->addAction(a);
  147. menu->addAction(a);
  148. a = new QAction(QIcon::fromTheme("document-open", QIcon(rsrcPath + "/fileopen.png")),
  149. tr("&Open..."), this);
  150. a->setShortcut(QKeySequence::Open);
  151. connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
  152. tb->addAction(a);
  153. menu->addAction(a);
  154. menu->addSeparator();
  155. actionSave = a = new QAction(QIcon::fromTheme("document-save", QIcon(rsrcPath + "/filesave.png")),
  156. tr("&Save"), this);
  157. a->setShortcut(QKeySequence::Save);
  158. connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
  159. a->setEnabled(false);
  160. tb->addAction(a);
  161. menu->addAction(a);
  162. a = new QAction(tr("Save &As..."), this);
  163. a->setPriority(QAction::LowPriority);
  164. connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  165. menu->addAction(a);
  166. menu->addSeparator();
  167. #ifndef QT_NO_PRINTER
  168. a = new QAction(QIcon::fromTheme("document-print", QIcon(rsrcPath + "/fileprint.png")),
  169. tr("&Print..."), this);
  170. a->setPriority(QAction::LowPriority);
  171. a->setShortcut(QKeySequence::Print);
  172. connect(a, SIGNAL(triggered()), this, SLOT(filePrint()));
  173. tb->addAction(a);
  174. menu->addAction(a);
  175. a = new QAction(QIcon::fromTheme("fileprint", QIcon(rsrcPath + "/fileprint.png")),
  176. tr("Print Preview..."), this);
  177. connect(a, SIGNAL(triggered()), this, SLOT(filePrintPreview()));
  178. menu->addAction(a);
  179. a = new QAction(QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png")),
  180. tr("&Export PDF..."), this);
  181. a->setPriority(QAction::LowPriority);
  182. a->setShortcut(Qt::CTRL + Qt::Key_D);
  183. connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf()));
  184. tb->addAction(a);
  185. menu->addAction(a);
  186. menu->addSeparator();
  187. #endif
  188. a = new QAction(tr("&Quit"), this);
  189. a->setShortcut(Qt::CTRL + Qt::Key_Q);
  190. connect(a, SIGNAL(triggered()), this, SLOT(close()));
  191. menu->addAction(a);
  192. }
  193. void TextEdit::setupEditActions()
  194. {
  195. QToolBar *tb = new QToolBar(this);
  196. tb->setWindowTitle(tr("Edit Actions"));
  197. addToolBar(tb);
  198. QMenu *menu = new QMenu(tr("&Edit"), this);
  199. menuBar()->addMenu(menu);
  200. QAction *a;
  201. a = actionUndo = new QAction(QIcon::fromTheme("edit-undo", QIcon(rsrcPath + "/editundo.png")),
  202. tr("&Undo"), this);
  203. a->setShortcut(QKeySequence::Undo);
  204. tb->addAction(a);
  205. menu->addAction(a);
  206. a = actionRedo = new QAction(QIcon::fromTheme("edit-redo", QIcon(rsrcPath + "/editredo.png")),
  207. tr("&Redo"), this);
  208. a->setPriority(QAction::LowPriority);
  209. a->setShortcut(QKeySequence::Redo);
  210. tb->addAction(a);
  211. menu->addAction(a);
  212. menu->addSeparator();
  213. a = actionCut = new QAction(QIcon::fromTheme("edit-cut", QIcon(rsrcPath + "/editcut.png")),
  214. tr("Cu&t"), this);
  215. a->setPriority(QAction::LowPriority);
  216. a->setShortcut(QKeySequence::Cut);
  217. tb->addAction(a);
  218. menu->addAction(a);
  219. a = actionCopy = new QAction(QIcon::fromTheme("edit-copy", QIcon(rsrcPath + "/editcopy.png")),
  220. tr("&Copy"), this);
  221. a->setPriority(QAction::LowPriority);
  222. a->setShortcut(QKeySequence::Copy);
  223. tb->addAction(a);
  224. menu->addAction(a);
  225. a = actionPaste = new QAction(QIcon::fromTheme("edit-paste", QIcon(rsrcPath + "/editpaste.png")),
  226. tr("&Paste"), this);
  227. a->setPriority(QAction::LowPriority);
  228. a->setShortcut(QKeySequence::Paste);
  229. tb->addAction(a);
  230. menu->addAction(a);
  231. #ifndef QT_NO_CLIPBOARD
  232. if (const QMimeData *md = QApplication::clipboard()->mimeData())
  233. actionPaste->setEnabled(md->hasText());
  234. #endif
  235. }
  236. void TextEdit::setupTextActions()
  237. {
  238. QToolBar *tb = new QToolBar(this);
  239. tb->setWindowTitle(tr("Format Actions"));
  240. addToolBar(tb);
  241. QMenu *menu = new QMenu(tr("F&ormat"), this);
  242. menuBar()->addMenu(menu);
  243. actionTextBold = new QAction(QIcon::fromTheme("format-text-bold", QIcon(rsrcPath + "/textbold.png")),
  244. tr("&Bold"), this);
  245. actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
  246. actionTextBold->setPriority(QAction::LowPriority);
  247. QFont bold;
  248. bold.setBold(true);
  249. actionTextBold->setFont(bold);
  250. connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
  251. tb->addAction(actionTextBold);
  252. menu->addAction(actionTextBold);
  253. actionTextBold->setCheckable(true);
  254. actionTextItalic = new QAction(QIcon::fromTheme("format-text-italic", QIcon(rsrcPath + "/textitalic.png")),
  255. tr("&Italic"), this);
  256. actionTextItalic->setPriority(QAction::LowPriority);
  257. actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
  258. QFont italic;
  259. italic.setItalic(true);
  260. actionTextItalic->setFont(italic);
  261. connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
  262. tb->addAction(actionTextItalic);
  263. menu->addAction(actionTextItalic);
  264. actionTextItalic->setCheckable(true);
  265. actionTextUnderline = new QAction(QIcon::fromTheme("format-text-underline", QIcon(rsrcPath + "/textunder.png")),
  266. tr("&Underline"), this);
  267. actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
  268. actionTextUnderline->setPriority(QAction::LowPriority);
  269. QFont underline;
  270. underline.setUnderline(true);
  271. actionTextUnderline->setFont(underline);
  272. connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
  273. tb->addAction(actionTextUnderline);
  274. menu->addAction(actionTextUnderline);
  275. actionTextUnderline->setCheckable(true);
  276. menu->addSeparator();
  277. QActionGroup *grp = new QActionGroup(this);
  278. connect(grp, SIGNAL(triggered(QAction*)), this, SLOT(textAlign(QAction*)));
  279. // Make sure the alignLeft is always left of the alignRight
  280. if (QApplication::isLeftToRight()) {
  281. actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")),
  282. tr("&Left"), grp);
  283. actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp);
  284. actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp);
  285. } else {
  286. actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp);
  287. actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp);
  288. actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), tr("&Left"), grp);
  289. }
  290. actionAlignJustify = new QAction(QIcon::fromTheme("format-justify-fill", QIcon(rsrcPath + "/textjustify.png")), tr("&Justify"), grp);
  291. actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
  292. actionAlignLeft->setCheckable(true);
  293. actionAlignLeft->setPriority(QAction::LowPriority);
  294. actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
  295. actionAlignCenter->setCheckable(true);
  296. actionAlignCenter->setPriority(QAction::LowPriority);
  297. actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
  298. actionAlignRight->setCheckable(true);
  299. actionAlignRight->setPriority(QAction::LowPriority);
  300. actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
  301. actionAlignJustify->setCheckable(true);
  302. actionAlignJustify->setPriority(QAction::LowPriority);
  303. tb->addActions(grp->actions());
  304. menu->addActions(grp->actions());
  305. menu->addSeparator();
  306. QPixmap pix(16, 16);
  307. pix.fill(Qt::black);
  308. actionTextColor = new QAction(pix, tr("&Color..."), this);
  309. connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
  310. tb->addAction(actionTextColor);
  311. menu->addAction(actionTextColor);
  312. tb = new QToolBar(this);
  313. tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
  314. tb->setWindowTitle(tr("Format Actions"));
  315. addToolBarBreak(Qt::TopToolBarArea);
  316. addToolBar(tb);
  317. comboStyle = new QComboBox(tb);
  318. tb->addWidget(comboStyle);
  319. comboStyle->addItem("Standard");
  320. comboStyle->addItem("Bullet List (Disc)");
  321. comboStyle->addItem("Bullet List (Circle)");
  322. comboStyle->addItem("Bullet List (Square)");
  323. comboStyle->addItem("Ordered List (Decimal)");
  324. comboStyle->addItem("Ordered List (Alpha lower)");
  325. comboStyle->addItem("Ordered List (Alpha upper)");
  326. comboStyle->addItem("Ordered List (Roman lower)");
  327. comboStyle->addItem("Ordered List (Roman upper)");
  328. connect(comboStyle, SIGNAL(activated(int)),
  329. this, SLOT(textStyle(int)));
  330. comboFont = new QFontComboBox(tb);
  331. tb->addWidget(comboFont);
  332. connect(comboFont, SIGNAL(activated(QString)),
  333. this, SLOT(textFamily(QString)));
  334. comboSize = new QComboBox(tb);
  335. comboSize->setObjectName("comboSize");
  336. tb->addWidget(comboSize);
  337. comboSize->setEditable(true);
  338. QFontDatabase db;
  339. foreach(int size, db.standardSizes())
  340. comboSize->addItem(QString::number(size));
  341. connect(comboSize, SIGNAL(activated(QString)),
  342. this, SLOT(textSize(QString)));
  343. comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
  344. .pointSize())));
  345. }
  346. bool TextEdit::load(const QString &f)
  347. {
  348. if (!QFile::exists(f))
  349. return false;
  350. QFile file(f);
  351. if (!file.open(QFile::ReadOnly))
  352. return false;
  353. QByteArray data = file.readAll();
  354. QTextCodec *codec = Qt::codecForHtml(data);
  355. QString str = codec->toUnicode(data);
  356. if (Qt::mightBeRichText(str)) {
  357. textEdit->setHtml(str);
  358. } else {
  359. str = QString::fromLocal8Bit(data);
  360. textEdit->setPlainText(str);
  361. }
  362. setCurrentFileName(f);
  363. return true;
  364. }
  365. bool TextEdit::maybeSave()
  366. {
  367. if (!textEdit->document()->isModified())
  368. return true;
  369. if (fileName.startsWith(QLatin1String(":/")))
  370. return true;
  371. QMessageBox::StandardButton ret;
  372. ret = QMessageBox::warning(this, tr("Application"),
  373. tr("The document has been modified.\n"
  374. "Do you want to save your changes?"),
  375. QMessageBox::Save | QMessageBox::Discard
  376. | QMessageBox::Cancel);
  377. if (ret == QMessageBox::Save)
  378. return fileSave();
  379. else if (ret == QMessageBox::Cancel)
  380. return false;
  381. return true;
  382. }
  383. void TextEdit::setCurrentFileName(const QString &fileName)
  384. {
  385. this->fileName = fileName;
  386. textEdit->document()->setModified(false);
  387. QString shownName;
  388. if (fileName.isEmpty())
  389. shownName = "untitled.txt";
  390. else
  391. shownName = QFileInfo(fileName).fileName();
  392. setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));
  393. setWindowModified(false);
  394. }
  395. void TextEdit::fileNew()
  396. {
  397. if (maybeSave()) {
  398. textEdit->clear();
  399. setCurrentFileName(QString());
  400. }
  401. }
  402. void TextEdit::fileOpen()
  403. {
  404. QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
  405. QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
  406. if (!fn.isEmpty())
  407. load(fn);
  408. }
  409. bool TextEdit::fileSave()
  410. {
  411. if (fileName.isEmpty())
  412. return fileSaveAs();
  413. QTextDocumentWriter writer(fileName);
  414. bool success = writer.write(textEdit->document());
  415. if (success)
  416. textEdit->document()->setModified(false);
  417. return success;
  418. }
  419. bool TextEdit::fileSaveAs()
  420. {
  421. QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
  422. QString(), tr("ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)"));
  423. if (fn.isEmpty())
  424. return false;
  425. if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
  426. fn += ".odt"; // default
  427. setCurrentFileName(fn);
  428. return fileSave();
  429. }
  430. void TextEdit::filePrint()
  431. {
  432. #ifndef QT_NO_PRINTER
  433. QPrinter printer(QPrinter::HighResolution);
  434. QPrintDialog *dlg = new QPrintDialog(&printer, this);
  435. if (textEdit->textCursor().hasSelection())
  436. dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
  437. dlg->setWindowTitle(tr("Print Document"));
  438. if (dlg->exec() == QDialog::Accepted) {
  439. textEdit->print(&printer);
  440. }
  441. delete dlg;
  442. #endif
  443. }
  444. void TextEdit::filePrintPreview()
  445. {
  446. #ifndef QT_NO_PRINTER
  447. QPrinter printer(QPrinter::HighResolution);
  448. QPrintPreviewDialog preview(&printer, this);
  449. connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPreview(QPrinter*)));
  450. preview.exec();
  451. #endif
  452. }
  453. void TextEdit::printPreview(QPrinter *printer)
  454. {
  455. #ifdef QT_NO_PRINTER
  456. Q_UNUSED(printer);
  457. #else
  458. textEdit->print(printer);
  459. #endif
  460. }
  461. void TextEdit::filePrintPdf()
  462. {
  463. #ifndef QT_NO_PRINTER
  464. //! [0]
  465. QString fileName = QFileDialog::getSaveFileName(this, "Export PDF",
  466. QString(), "*.pdf");
  467. if (!fileName.isEmpty()) {
  468. if (QFileInfo(fileName).suffix().isEmpty())
  469. fileName.append(".pdf");
  470. QPrinter printer(QPrinter::HighResolution);
  471. printer.setOutputFormat(QPrinter::PdfFormat);
  472. printer.setOutputFileName(fileName);
  473. textEdit->document()->print(&printer);
  474. }
  475. //! [0]
  476. #endif
  477. }
  478. void TextEdit::textBold()
  479. {
  480. QTextCharFormat fmt;
  481. fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
  482. mergeFormatOnWordOrSelection(fmt);
  483. }
  484. void TextEdit::textUnderline()
  485. {
  486. QTextCharFormat fmt;
  487. fmt.setFontUnderline(actionTextUnderline->isChecked());
  488. mergeFormatOnWordOrSelection(fmt);
  489. }
  490. void TextEdit::textItalic()
  491. {
  492. QTextCharFormat fmt;
  493. fmt.setFontItalic(actionTextItalic->isChecked());
  494. mergeFormatOnWordOrSelection(fmt);
  495. }
  496. void TextEdit::textFamily(const QString &f)
  497. {
  498. QTextCharFormat fmt;
  499. fmt.setFontFamily(f);
  500. mergeFormatOnWordOrSelection(fmt);
  501. }
  502. void TextEdit::textSize(const QString &p)
  503. {
  504. qreal pointSize = p.toFloat();
  505. if (p.toFloat() > 0) {
  506. QTextCharFormat fmt;
  507. fmt.setFontPointSize(pointSize);
  508. mergeFormatOnWordOrSelection(fmt);
  509. }
  510. }
  511. void TextEdit::textStyle(int styleIndex)
  512. {
  513. QTextCursor cursor = textEdit->textCursor();
  514. if (styleIndex != 0) {
  515. QTextListFormat::Style style = QTextListFormat::ListDisc;
  516. switch (styleIndex) {
  517. default:
  518. case 1:
  519. style = QTextListFormat::ListDisc;
  520. break;
  521. case 2:
  522. style = QTextListFormat::ListCircle;
  523. break;
  524. case 3:
  525. style = QTextListFormat::ListSquare;
  526. break;
  527. case 4:
  528. style = QTextListFormat::ListDecimal;
  529. break;
  530. case 5:
  531. style = QTextListFormat::ListLowerAlpha;
  532. break;
  533. case 6:
  534. style = QTextListFormat::ListUpperAlpha;
  535. break;
  536. case 7:
  537. style = QTextListFormat::ListLowerRoman;
  538. break;
  539. case 8:
  540. style = QTextListFormat::ListUpperRoman;
  541. break;
  542. }
  543. cursor.beginEditBlock();
  544. QTextBlockFormat blockFmt = cursor.blockFormat();
  545. QTextListFormat listFmt;
  546. if (cursor.currentList()) {
  547. listFmt = cursor.currentList()->format();
  548. } else {
  549. listFmt.setIndent(blockFmt.indent() + 1);
  550. blockFmt.setIndent(0);
  551. cursor.setBlockFormat(blockFmt);
  552. }
  553. listFmt.setStyle(style);
  554. cursor.createList(listFmt);
  555. cursor.endEditBlock();
  556. } else {
  557. // ####
  558. QTextBlockFormat bfmt;
  559. bfmt.setObjectIndex(-1);
  560. cursor.mergeBlockFormat(bfmt);
  561. }
  562. }
  563. void TextEdit::textColor()
  564. {
  565. QColor col = QColorDialog::getColor(textEdit->textColor(), this);
  566. if (!col.isValid())
  567. return;
  568. QTextCharFormat fmt;
  569. fmt.setForeground(col);
  570. mergeFormatOnWordOrSelection(fmt);
  571. colorChanged(col);
  572. }
  573. void TextEdit::textAlign(QAction *a)
  574. {
  575. if (a == actionAlignLeft)
  576. textEdit->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
  577. else if (a == actionAlignCenter)
  578. textEdit->setAlignment(Qt::AlignHCenter);
  579. else if (a == actionAlignRight)
  580. textEdit->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
  581. else if (a == actionAlignJustify)
  582. textEdit->setAlignment(Qt::AlignJustify);
  583. }
  584. void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
  585. {
  586. fontChanged(format.font());
  587. colorChanged(format.foreground().color());
  588. }
  589. void TextEdit::cursorPositionChanged()
  590. {
  591. alignmentChanged(textEdit->alignment());
  592. }
  593. void TextEdit::clipboardDataChanged()
  594. {
  595. #ifndef QT_NO_CLIPBOARD
  596. if (const QMimeData *md = QApplication::clipboard()->mimeData())
  597. actionPaste->setEnabled(md->hasText());
  598. #endif
  599. }
  600. void TextEdit::about()
  601. {
  602. QMessageBox::about(this, tr("About"), tr("This example demonstrates Qt's "
  603. "rich text editing facilities in action, providing an example "
  604. "document for you to experiment with."));
  605. }
  606. void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
  607. {
  608. QTextCursor cursor = textEdit->textCursor();
  609. if (!cursor.hasSelection())
  610. cursor.select(QTextCursor::WordUnderCursor);
  611. cursor.mergeCharFormat(format);
  612. textEdit->mergeCurrentCharFormat(format);
  613. }
  614. void TextEdit::fontChanged(const QFont &f)
  615. {
  616. comboFont->setCurrentIndex(comboFont->findText(QFontInfo(f).family()));
  617. comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
  618. actionTextBold->setChecked(f.bold());
  619. actionTextItalic->setChecked(f.italic());
  620. actionTextUnderline->setChecked(f.underline());
  621. }
  622. void TextEdit::colorChanged(const QColor &c)
  623. {
  624. QPixmap pix(16, 16);
  625. pix.fill(c);
  626. actionTextColor->setIcon(pix);
  627. }
  628. void TextEdit::alignmentChanged(Qt::Alignment a)
  629. {
  630. if (a & Qt::AlignLeft) {
  631. actionAlignLeft->setChecked(true);
  632. } else if (a & Qt::AlignHCenter) {
  633. actionAlignCenter->setChecked(true);
  634. } else if (a & Qt::AlignRight) {
  635. actionAlignRight->setChecked(true);
  636. } else if (a & Qt::AlignJustify) {
  637. actionAlignJustify->setChecked(true);
  638. }
  639. }