Guest User

TextEditSource

a guest
Aug 9th, 2016
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mtextedit.h"
  2. #include <QTextDocument>
  3. #include <QTextCursor>
  4. #include <QImage>
  5. #include <QByteArray>
  6. #include <QBuffer>
  7. #include <QApplication>
  8. #include <QClipboard>
  9. #include <QMimeData>
  10. #include <QFontDatabase>
  11. #include <QTextList>
  12. #include <QFileDialog>
  13. #include <QImageReader>
  14. #include <QUrl>
  15. #include <QRegularExpression>
  16. #include <QStandardItemModel>
  17. #include <QAction>
  18. #include <QInputDialog>
  19. #include <QSettings>
  20. #include "Globals.h"
  21. #include <QDesktopServices>
  22. #include <QMessageBox>
  23.  
  24. MTextEdit::MTextEdit(QWidget *parent)
  25.     : QTextBrowser(parent)
  26.     , m_lastBlockList(nullptr)
  27.     , m_fontSize(9)
  28.     , m_imageCounter(1U)
  29. {
  30.     setOpenLinks(false);
  31.     setOpenExternalLinks(false);
  32.     setReadOnly(false);
  33.     setTabStopWidth(40);
  34.     connect(this, &MTextEdit::anchorClicked, this, &MTextEdit::urlActivated);
  35.     connect(this, &MTextEdit::currentCharFormatChanged, this, &MTextEdit::slotCurrentCharFormatChanged);
  36.     connect(this, &MTextEdit::cursorPositionChanged, this, &MTextEdit::slotCursorPositionChanged);
  37.  
  38. #ifndef QT_NO_CLIPBOARD
  39.     connect(QApplication::clipboard(), &QClipboard::dataChanged, this, &MTextEdit::slotClipboardDataChanged);
  40. #endif
  41.  
  42.  
  43.     m_fontsModel = new QStandardItemModel(this);
  44.     m_fontsModel->insertColumn(0);
  45.     const QList<int> fontSizes = QFontDatabase::standardSizes();
  46.     m_fontsModel->insertRows(0, fontSizes.size());
  47.     for (int i = 0; i < fontSizes.size(); ++i)
  48.         m_fontsModel->setData(m_fontsModel->index(i, 0), fontSizes.at(i));
  49.     m_fontsModel->sort(0);
  50.     connect(this, &MTextEdit::fontSizeChanged, this, &MTextEdit::textSize);
  51.  
  52.  
  53.     connect(this, &MTextEdit::fgColorChanged, this, &MTextEdit::textFgColor);
  54.     connect(this, &MTextEdit::bgColorChanged, this, &MTextEdit::textBgColor);
  55.  
  56.     createActions();
  57.  
  58.     fontChanged(font());
  59.     bgColorChanged(textColor());
  60.     setFontSize(QApplication::font().pointSize());
  61.     setFgColor(QApplication::palette().foreground().color());
  62.     setBgColor(QApplication::palette().background().color());
  63. }
  64.  
  65.  
  66. bool MTextEdit::canInsertFromMimeData(const QMimeData *source) const {
  67.     return
  68.         source->hasImage()
  69.         || source->hasUrls()
  70.         || QTextBrowser::canInsertFromMimeData(source);
  71. }
  72.  
  73.  
  74. void MTextEdit::insertFromMimeData(const QMimeData *source) {
  75.     if (source->hasImage()) {
  76.         dropImage(source->imageData().value<QImage>());
  77.     }
  78.     else if (source->hasUrls()) {
  79.         const auto allUrls = source->urls();
  80.         for (const QUrl& singleUrl : allUrls)
  81.             dropDocument(singleUrl);
  82.     }
  83.     else
  84.         QTextBrowser::insertFromMimeData(source);
  85. }
  86.  
  87.  
  88. QMimeData *MTextEdit::createMimeDataFromSelection() const {
  89.     return QTextBrowser::createMimeDataFromSelection();
  90. }
  91.  
  92.  
  93.  
  94. void MTextEdit::focusInEvent(QFocusEvent *e)
  95. {
  96.     QTextBrowser::focusInEvent(e);
  97.     setFocus(Qt::TabFocusReason);
  98. }
  99.  
  100.  
  101.  
  102. void MTextEdit::createActions()
  103. {
  104.     m_undoAction = new QAction(QIcon::fromTheme("edit-undo"), tr("Undo"), this);
  105.     m_undoAction->setShortcut(QKeySequence::Undo);
  106.     connect(this->document(), &QTextDocument::undoAvailable, m_undoAction, &QAction::setEnabled);
  107.     connect(m_undoAction, &QAction::triggered, this, &MTextEdit::undo);
  108.     m_undoAction->setEnabled(this->document()->isUndoAvailable());
  109.     m_redoAction = new QAction(QIcon::fromTheme("edit-redo"), tr("Redo"), this);
  110.     m_redoAction->setShortcut(QKeySequence::Redo);
  111.     m_redoAction->setEnabled(this->document()->isRedoAvailable());
  112.     connect(this->document(), &QTextDocument::redoAvailable, m_redoAction, &QAction::setEnabled);
  113.     connect(m_redoAction, &QAction::triggered, this, &MTextEdit::redo);
  114.     m_cutAction = new QAction(QIcon::fromTheme("edit-cut"), tr("Cut"), this);
  115.     m_cutAction->setEnabled(false);
  116.     m_cutAction->setShortcut(QKeySequence::Cut);
  117.     connect(m_cutAction, &QAction::triggered, this, &MTextEdit::cut);
  118.     connect(this, &MTextEdit::copyAvailable, m_cutAction, &QAction::setEnabled);
  119.     m_copyAction = new QAction(QIcon::fromTheme("edit-copy"), tr("Copy"), this);
  120.     m_copyAction->setEnabled(false);
  121.     m_copyAction->setShortcut(QKeySequence::Copy);
  122.     connect(m_copyAction, &QAction::triggered, this, &MTextEdit::copy);
  123.     connect(this, &MTextEdit::copyAvailable, m_copyAction, &QAction::setEnabled);
  124.     m_pasteAction = new QAction(QIcon::fromTheme("edit-paste"), tr("Paste"), this);
  125.     m_pasteAction->setShortcut(QKeySequence::Paste);
  126.     connect(m_copyAction, &QAction::triggered, this, &MTextEdit::paste);
  127.     m_boldAction = new QAction(QIcon::fromTheme("format-text-bold"), tr("Bold"), this);
  128.     m_boldAction->setShortcut(Qt::CTRL + Qt::Key_B);
  129.     m_boldAction->setCheckable(true);
  130.     connect(m_boldAction, &QAction::triggered, this, &MTextEdit::textBold);
  131.     m_italicAction = new QAction(QIcon::fromTheme("format-text-italic"), tr("Italic"), this);
  132.     m_italicAction->setShortcut(Qt::CTRL + Qt::Key_I);
  133.     m_italicAction->setCheckable(true);
  134.     connect(m_italicAction, &QAction::triggered, this, &MTextEdit::textItalic);
  135.     m_underlineAction = new QAction(QIcon::fromTheme("format-text-underline"), tr("Underline"), this);
  136.     m_underlineAction->setShortcut(Qt::CTRL + Qt::Key_U);
  137.     m_underlineAction->setCheckable(true);
  138.     connect(m_underlineAction, &QAction::triggered, this, &MTextEdit::textUnderline);
  139.     m_strikeoutAction = new QAction(QIcon::fromTheme("format-text-strikethrough"), tr("Strike out"), this);
  140.     m_strikeoutAction->setCheckable(true);
  141.     connect(m_strikeoutAction, &QAction::triggered, this, &MTextEdit::textStrikeout);
  142.     m_orderedListAction = new QAction(QIcon::fromTheme("format-list-ordered"), tr("Ordered list"), this);
  143.     m_orderedListAction->setCheckable(true);
  144.     m_orderedListAction->setShortcut(Qt::CTRL + Qt::Key_Equal);
  145.     connect(m_orderedListAction, &QAction::triggered, this, &MTextEdit::listOrdered);
  146.     m_unorderedListAction = new QAction(QIcon::fromTheme("format-list-unordered"), tr("Unordered list"), this);
  147.     m_unorderedListAction->setCheckable(true);
  148.     m_unorderedListAction->setShortcut(Qt::CTRL + Qt::Key_Minus);
  149.     connect(m_unorderedListAction, &QAction::triggered, this, &MTextEdit::listBullet);
  150.     m_increaseIndentAction = new QAction(QIcon::fromTheme("format-indent-more"), tr("Increase indentation"), this);
  151.     m_increaseIndentAction->setShortcut(Qt::CTRL + Qt::Key_Period);
  152.     connect(m_increaseIndentAction, &QAction::triggered, this, &MTextEdit::increaseIndentation);
  153.     m_decreaseIndentAction = new QAction(QIcon::fromTheme("format-indent-less"), tr("Decrease indentation"), this);
  154.     m_decreaseIndentAction->setShortcut(Qt::CTRL + Qt::Key_Comma);
  155.     connect(m_decreaseIndentAction, &QAction::triggered, this, &MTextEdit::decreaseIndentation);
  156.     m_imageAction = new QAction(QIcon::fromTheme("insert-image"), tr("Image"), this);
  157.     connect(m_imageAction, &QAction::triggered, this, &MTextEdit::insertImage);
  158.  
  159.     addAction(m_undoAction);
  160.     addAction(m_redoAction);
  161.     addAction(m_cutAction);
  162.     addAction(m_copyAction);
  163.     addAction(m_pasteAction);
  164.     addAction(m_pasteAction);
  165.     addAction(m_boldAction);
  166.     addAction(m_italicAction);
  167.     addAction(m_underlineAction);
  168.     addAction(m_strikeoutAction);
  169.     addAction(m_orderedListAction);
  170.     addAction(m_unorderedListAction);
  171.     addAction(m_increaseIndentAction);
  172.     addAction(m_decreaseIndentAction);
  173.     addAction(m_imageAction);
  174. }
  175.  
  176.  
  177.  
  178. void MTextEdit::textBold()
  179. {
  180.     QTextCharFormat fmt;
  181.     fmt.setFontWeight(m_boldAction->isChecked() ? QFont::Bold : QFont::Normal);
  182.     mergeFormatOnWordOrSelection(fmt);
  183. }
  184.  
  185.  
  186. void MTextEdit::textUnderline()
  187. {
  188.     QTextCharFormat fmt;
  189.     fmt.setFontUnderline(m_underlineAction->isChecked());
  190.     mergeFormatOnWordOrSelection(fmt);
  191. }
  192.  
  193. void MTextEdit::textItalic()
  194. {
  195.     QTextCharFormat fmt;
  196.     fmt.setFontItalic(m_italicAction->isChecked());
  197.     mergeFormatOnWordOrSelection(fmt);
  198. }
  199.  
  200. void MTextEdit::textStrikeout()
  201. {
  202.     QTextCharFormat fmt;
  203.     fmt.setFontStrikeOut(m_strikeoutAction->isChecked());
  204.     mergeFormatOnWordOrSelection(fmt);
  205. }
  206.  
  207. void MTextEdit::textSize()
  208. {
  209.     qreal pointSize = fontSize();
  210.     if (pointSize > 0) {
  211.         QTextCharFormat fmt;
  212.         fmt.setFontPointSize(pointSize);
  213.         mergeFormatOnWordOrSelection(fmt);
  214.     }
  215. }
  216.  
  217.  
  218.  
  219.  
  220. void MTextEdit::textFgColor(const QColor& col)
  221. {
  222.     QTextCursor cursor = textCursor();
  223.     if (!cursor.hasSelection()) {
  224.         cursor.select(QTextCursor::WordUnderCursor);
  225.     }
  226.     QTextCharFormat fmt = cursor.charFormat();
  227.     if (col.isValid()) {
  228.         fmt.setForeground(col);
  229.     }
  230.     else {
  231.         fmt.clearForeground();
  232.     }
  233.     cursor.setCharFormat(fmt);
  234.     setCurrentCharFormat(fmt);
  235. }
  236.  
  237. void MTextEdit::textBgColor(const QColor& col)
  238. {
  239.     QTextCursor cursor = textCursor();
  240.     if (!cursor.hasSelection()) {
  241.         cursor.select(QTextCursor::WordUnderCursor);
  242.     }
  243.     QTextCharFormat fmt = cursor.charFormat();
  244.     if (col.isValid()) {
  245.         fmt.setBackground(col);
  246.     }
  247.     else {
  248.         fmt.clearBackground();
  249.     }
  250.     cursor.setCharFormat(fmt);
  251.     setCurrentCharFormat(fmt);
  252. }
  253.  
  254. void MTextEdit::listBullet(bool checked)
  255. {
  256.     if (checked) {
  257.         m_orderedListAction->setChecked(false);
  258.     }
  259.     list(checked, QTextListFormat::ListDisc);
  260. }
  261.  
  262. void MTextEdit::listOrdered(bool checked)
  263. {
  264.     if (checked) {
  265.         m_unorderedListAction->setChecked(false);
  266.     }
  267.     list(checked, QTextListFormat::ListDecimal);
  268. }
  269.  
  270. void MTextEdit::list(bool checked, QTextListFormat::Style style)
  271. {
  272.     QTextCursor cursor = textCursor();
  273.     cursor.beginEditBlock();
  274.     if (!checked) {
  275.         QTextBlockFormat obfmt = cursor.blockFormat();
  276.         QTextBlockFormat bfmt;
  277.         bfmt.setIndent(obfmt.indent());
  278.         cursor.setBlockFormat(bfmt);
  279.     }
  280.     else {
  281.         QTextListFormat listFmt;
  282.         if (cursor.currentList()) {
  283.             listFmt = cursor.currentList()->format();
  284.         }
  285.         listFmt.setStyle(style);
  286.         cursor.createList(listFmt);
  287.     }
  288.     cursor.endEditBlock();
  289. }
  290.  
  291.  
  292.  
  293. void MTextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
  294. {
  295.     QTextCursor cursor = textCursor();
  296.     if (!cursor.hasSelection()) {
  297.         cursor.select(QTextCursor::WordUnderCursor);
  298.     }
  299.     cursor.mergeCharFormat(format);
  300.     mergeCurrentCharFormat(format);
  301.     setFocus(Qt::TabFocusReason);
  302. }
  303.  
  304. void MTextEdit::slotCursorPositionChanged()
  305. {
  306.     QTextList *l = textCursor().currentList();
  307.     if (m_lastBlockList && (l == m_lastBlockList || (l != 0 && m_lastBlockList != 0
  308.         && l->format().style() == m_lastBlockList->format().style()))) {
  309.         return;
  310.     }
  311.     m_lastBlockList = l;
  312.     if (l) {
  313.         QTextListFormat lfmt = l->format();
  314.         if (lfmt.style() == QTextListFormat::ListDisc) {
  315.             m_unorderedListAction->setChecked(true);
  316.             m_orderedListAction->setChecked(false);
  317.         }
  318.         else if (lfmt.style() == QTextListFormat::ListDecimal) {
  319.             m_unorderedListAction->setChecked(false);
  320.             m_orderedListAction->setChecked(true);
  321.         }
  322.         else {
  323.             m_unorderedListAction->setChecked(false);
  324.             m_orderedListAction->setChecked(false);
  325.         }
  326.     }
  327.     else {
  328.         m_unorderedListAction->setChecked(false);
  329.         m_orderedListAction->setChecked(false);
  330.     }
  331. }
  332.  
  333. void MTextEdit::fontChanged(const QFont &f)
  334. {
  335.     setFontSize(f.pointSize());
  336.     m_boldAction->setChecked(f.bold());
  337.     m_italicAction->setChecked(f.italic());
  338.     m_underlineAction->setChecked(f.underline());
  339.     m_strikeoutAction->setChecked(f.strikeOut());
  340.  
  341.     if (textCursor().currentList()) {
  342.         QTextListFormat lfmt = textCursor().currentList()->format();
  343.         if (lfmt.style() == QTextListFormat::ListDisc) {
  344.             m_unorderedListAction->setChecked(true);
  345.             m_orderedListAction->setChecked(false);
  346.         }
  347.         else if (lfmt.style() == QTextListFormat::ListDecimal) {
  348.             m_unorderedListAction->setChecked(false);
  349.             m_orderedListAction->setChecked(true);
  350.         }
  351.         else {
  352.             m_unorderedListAction->setChecked(false);
  353.             m_orderedListAction->setChecked(false);
  354.         }
  355.     }
  356.     else {
  357.         m_unorderedListAction->setChecked(false);
  358.         m_orderedListAction->setChecked(false);
  359.     }
  360. }
  361.  
  362. void MTextEdit::slotCurrentCharFormatChanged(const QTextCharFormat &format)
  363. {
  364.     fontChanged(format.font());
  365.     setBgColor((format.background().isOpaque()) ? format.background().color() : QColor());
  366.     setFgColor((format.foreground().isOpaque()) ? format.foreground().color() : QColor());
  367. }
  368.  
  369. void MTextEdit::slotClipboardDataChanged()
  370. {
  371. #ifndef QT_NO_CLIPBOARD
  372.     if (const QMimeData *md = QApplication::clipboard()->mimeData())
  373.         m_pasteAction->setEnabled(md->hasText());
  374. #endif
  375. }
  376.  
  377. void MTextEdit::urlActivated(const QUrl &link)
  378. {
  379.     if (link.isLocalFile()) {
  380.         if (!QDesktopServices::openUrl(link)) {
  381.             if (QMessageBox::critical(this, tr("Error"), tr("File not found.\nThe file could be temporarily unavailable or has been deleted"), QMessageBox::Retry, QMessageBox::Cancel) == QMessageBox::Retry)
  382.                 return urlActivated(link);
  383.         }
  384.     }
  385. }
  386.  
  387. QString MTextEdit::toHtml() const
  388. {
  389.     QString s = QTextBrowser::toHtml();
  390.     // convert emails to links
  391.     s = s.replace(QRegularExpression("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)([a-zA-Z\\d]+@[a-zA-Z\\d]+\\.[a-zA-Z]+)"), "\\1<a href=\"mailto:\\2\">\\2</a>");
  392.     // convert links
  393.     s = s.replace(QRegularExpression("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)((?:https?|ftp|file)://[^\\s'\"<>]+)"), "\\1<a href=\"\\2\">\\2</a>");
  394.     // see also: Utils::linkify()
  395.     return s;
  396. }
  397.  
  398. QAction* MTextEdit::undoAction()
  399. {
  400.     return m_undoAction;
  401. }
  402.  
  403. QAction* MTextEdit::redoAction()
  404. {
  405.     return m_redoAction;
  406. }
  407.  
  408. QAction* MTextEdit::cutAction()
  409. {
  410.     return m_cutAction;
  411. }
  412.  
  413. QAction* MTextEdit::copyAction()
  414. {
  415.     return m_copyAction;
  416. }
  417.  
  418. QAction* MTextEdit::pasteAction()
  419. {
  420.     return m_pasteAction;
  421. }
  422.  
  423.  
  424.  
  425. QAction* MTextEdit::boldAction()
  426. {
  427.     return m_boldAction;
  428. }
  429.  
  430. QAction* MTextEdit::italicAction()
  431. {
  432.     return m_italicAction;
  433. }
  434.  
  435. QAction* MTextEdit::underlineAction()
  436. {
  437.     return m_underlineAction;
  438. }
  439.  
  440. QAction* MTextEdit::strikeoutAction()
  441. {
  442.     return m_strikeoutAction;
  443. }
  444.  
  445. QAction* MTextEdit::orderedListAction()
  446. {
  447.     return m_orderedListAction;
  448. }
  449.  
  450. QAction* MTextEdit::unorderedListAction()
  451. {
  452.     return m_unorderedListAction;
  453. }
  454.  
  455. QAction* MTextEdit::increaseIndentAction()
  456. {
  457.     return m_increaseIndentAction;
  458. }
  459.  
  460. QAction* MTextEdit::decreaseIndentAction()
  461. {
  462.     return m_decreaseIndentAction;
  463. }
  464.  
  465. QAction* MTextEdit::imageAction()
  466. {
  467.     return m_imageAction;
  468. }
  469.  
  470. void MTextEdit::increaseIndentation()
  471. {
  472.     indent(+1);
  473. }
  474.  
  475. void MTextEdit::decreaseIndentation()
  476. {
  477.     indent(-1);
  478. }
  479.  
  480. void MTextEdit::indent(int delta)
  481. {
  482.     QTextCursor cursor = textCursor();
  483.     cursor.beginEditBlock();
  484.     QTextBlockFormat bfmt = cursor.blockFormat();
  485.     int ind = bfmt.indent();
  486.     if (ind + delta >= 0) {
  487.         bfmt.setIndent(ind + delta);
  488.     }
  489.     cursor.setBlockFormat(bfmt);
  490.     cursor.endEditBlock();
  491. }
  492.  
  493. void MTextEdit::setText(const QString& text)
  494. {
  495.     if (text.isEmpty()) {
  496.         setPlainText(text);
  497.         return;
  498.     }
  499.     if (text[0] == '<') {
  500.         setHtml(text);
  501.     }
  502.     else {
  503.         setPlainText(text);
  504.     }
  505. }
  506.  
  507.  
  508. void MTextEdit::insertImage()
  509. {
  510.     QSettings s;
  511.     QString attdir = s.value("general/filedialog-path").toString();
  512.     QString file = QFileDialog::getOpenFileName(this,
  513.         tr("Select an image"),
  514.         attdir,
  515.         tr("Images  (*.jpg *.gif *.png *.bmp)"));
  516.     QImage image = QImageReader(file).read();
  517.  
  518.     dropImage(image);
  519.  
  520. }
  521.  
  522.  
  523. const unsigned int& MTextEdit::fontSize() const
  524. {
  525.     return m_fontSize;
  526. }
  527.  
  528. void MTextEdit::setFontSize(const unsigned int& val)
  529. {
  530.     if (val != m_fontSize && val > 0) {
  531.         m_fontSize = val;
  532.         emit fontSizeChanged(val);
  533.     }
  534. }
  535.  
  536. const QColor& MTextEdit::fgColor() const
  537. {
  538.     return m_fgColor;
  539. }
  540.  
  541. const QColor& MTextEdit::bgColor() const
  542. {
  543.     return m_bgColor;
  544. }
  545.  
  546. void MTextEdit::setFgColor(const QColor& val)
  547. {
  548.     if (val != m_fgColor) {
  549.         m_fgColor = val;
  550.         emit fgColorChanged(m_fgColor);
  551.     }
  552. }
  553.  
  554. void MTextEdit::setBgColor(const QColor& val)
  555. {
  556.     if (val != m_bgColor) {
  557.         m_bgColor = val;
  558.         emit bgColorChanged(m_bgColor);
  559.     }
  560. }
  561.  
  562. QByteArray MTextEdit::save() const
  563. {
  564.     QHash<QString, QImage> imageHash;
  565.     const QString htmlString = toHtml();
  566.     for (auto i = m_imageList.constBegin();i!=m_imageList.constEnd();++i){
  567.         if (htmlString.contains(*i))
  568.             imageHash.insert(*i, document()->resource(QTextDocument::ImageResource, QUrl(*i)).value<QImage>());
  569.     }
  570.     QByteArray result;
  571.     QDataStream writer(&result, QIODevice::WriteOnly);
  572.     writer << htmlString << imageHash;
  573.     return result;
  574. }
  575.  
  576. void MTextEdit::load(const QByteArray& val)
  577. {
  578.     m_imageCounter = 1U;
  579.     m_imageList.clear();
  580.     if (val.isEmpty())
  581.         return setPlainText(QString());
  582.     QDataStream reader(val);
  583.     QString htmlString;
  584.     QHash<QString, QImage> imageHash;
  585.     reader >> htmlString >> imageHash;
  586.     for (auto i = imageHash.constBegin(); i != imageHash.constEnd(); ++i) {
  587.         m_imageList.insert(i.key());
  588.         document()->addResource(QTextDocument::ImageResource, QUrl(i.key()), i.value());
  589.     }
  590.  
  591.     setHtml(htmlString);
  592. }
  593.  
  594. void MTextEdit::dropImage(const QImage& image)
  595. {
  596.     QString resourceName;
  597.     do{
  598.         resourceName = QStringLiteral("data://image%1").arg(m_imageCounter++);
  599.     } while (m_imageList.contains(resourceName));
  600.     m_imageList.insert(resourceName);
  601.     document()->addResource(QTextDocument::ImageResource, QUrl(resourceName), image);
  602.     QTextCursor cursor = textCursor();
  603.     cursor.insertImage(image, resourceName);
  604. }
  605. void MTextEdit::dropDocument(const QUrl& origFilePath)
  606. {
  607.     const QString repositoryPath = PBglobals::docsRepository();
  608.     if (!QDir().mkpath(repositoryPath))
  609.         return;
  610.     const QFileInfo origFile(origFilePath.toLocalFile());
  611.     QString destinationPath = origFile.fileName();
  612.     for (int i = 1; QFile::exists(repositoryPath + '/' + destinationPath); ++i)
  613.         destinationPath = origFile.baseName() + QString::number(i) + '.' + origFile.completeSuffix();
  614.     QFile::copy(origFile.absoluteFilePath(), repositoryPath + '/' + destinationPath);
  615.  
  616.     QTextCharFormat fmt;
  617.     fmt.setAnchor(true);
  618.     fmt.setAnchorHref(QUrl::fromLocalFile(repositoryPath + '/' + destinationPath).toString(QUrl::FullyEncoded));
  619.     fmt.setForeground(QApplication::palette().color(QPalette::Link));
  620.     fmt.setFontUnderline(true);
  621.  
  622.     debugConsole(toHtml());
  623.     QTextCursor cursor = textCursor();
  624.     const QTextCharFormat previousFormat = cursor.charFormat();
  625.     cursor.insertText(origFile.fileName(), fmt);
  626.     cursor.insertText(QStringLiteral(" "), previousFormat);
  627.     debugConsole(toHtml());
  628. }
RAW Paste Data