Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Diff of /textzone.cpp [000000] .. [d3b1aa] Maximize Restore

  Switch to side-by-side view

--- a
+++ b/textzone.cpp
@@ -0,0 +1,367 @@
+#include <QtGui>
+
+#include "textzone.h"
+
+TextZone::TextZone(QTextDocument *doc, QWidget *parent) :
+    QTextEdit(parent)
+{
+    textDocument = doc;
+    createActions();
+    setContextMenuPolicy(Qt::DefaultContextMenu);
+    connect(this, SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(charFormat(QTextCharFormat)));
+
+
+}
+
+
+
+
+//--------- Context Menu :------------------------------------------------------------------
+
+
+void TextZone::createActions()
+{
+    undoAct = new QAction(tr("&Undo"), this);
+    undoAct->setShortcuts(QKeySequence::Undo);
+    undoAct->setStatusTip(tr("Undo the last operation"));
+    connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
+
+    redoAct = new QAction(tr("&Redo"), this);
+    redoAct->setShortcuts(QKeySequence::Redo);
+    redoAct->setStatusTip(tr("Redo the last operation"));
+    connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
+
+    cutAct = new QAction(tr("Cu&t"), this);
+    cutAct->setShortcuts(QKeySequence::Cut);
+    cutAct->setStatusTip(tr("Cut the current selection's contents to the "
+                            "clipboard"));
+    connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
+
+    copyAct = new QAction(tr("&Copy"), this);
+    copyAct->setShortcuts(QKeySequence::Copy);
+    copyAct->setStatusTip(tr("Copy the current selection's contents to the "
+                             "clipboard"));
+    connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
+
+    pasteAct = new QAction(tr("&Paste"), this);
+    pasteAct->setShortcuts(QKeySequence::Paste);
+    pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
+                              "selection"));
+    connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
+
+    boldAct = new QAction(tr("&Bold"), this);
+    boldAct->setCheckable(true);
+    boldAct->setShortcuts(QKeySequence::Bold);
+    boldAct->setStatusTip(tr("Make the text bold"));
+    connect(boldAct, SIGNAL(triggered(bool)), this, SLOT(bold(bool)));
+
+    QFont boldFont = boldAct->font();
+    boldFont.setBold(true);
+    boldAct->setFont(boldFont);
+
+    italicAct = new QAction(tr("&Italic"), this);
+    italicAct->setCheckable(true);
+    italicAct->setShortcuts(QKeySequence::Italic);
+    italicAct->setStatusTip(tr("Make the text italic"));
+    connect(italicAct, SIGNAL(triggered(bool)), this, SLOT(italic(bool)));
+
+    QFont italicFont = italicAct->font();
+    italicFont.setItalic(true);
+    italicAct->setFont(italicFont);
+
+    //    setLineSpacingAct = new QAction(tr("Set &Line Spacing..."), this);
+    //    setLineSpacingAct->setStatusTip(tr("Change the gap between the lines of a "
+    //                                       "paragraph"));
+    //    connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing()));
+
+    //    setParagraphSpacingAct = new QAction(tr("Set &Paragraph Spacing..."), this);
+    //    setParagraphSpacingAct->setStatusTip(tr("Change the gap between paragraphs"));
+    //    connect(setParagraphSpacingAct, SIGNAL(triggered()),
+    //            this, SLOT(setParagraphSpacing()));
+
+    leftAlignAct = new QAction(tr("&Left Align"), this);
+    leftAlignAct->setCheckable(true);
+    leftAlignAct->setShortcut(tr("Ctrl+L"));
+    leftAlignAct->setStatusTip(tr("Left align the selected text"));
+    connect(leftAlignAct, SIGNAL(triggered(bool)), this, SLOT(leftAlign(bool)));
+
+    rightAlignAct = new QAction(tr("&Right Align"), this);
+    rightAlignAct->setCheckable(true);
+    rightAlignAct->setShortcut(tr("Ctrl+R"));
+    rightAlignAct->setStatusTip(tr("Right align the selected text"));
+    connect(rightAlignAct, SIGNAL(triggered(bool)), this, SLOT(rightAlign(bool)));
+
+    justifyAct = new QAction(tr("&Justify"), this);
+    justifyAct->setCheckable(true);
+    justifyAct->setShortcut(tr("Ctrl+J"));
+    justifyAct->setStatusTip(tr("Justify the selected text"));
+    connect(justifyAct, SIGNAL(triggered(bool)), this, SLOT(justify(bool)));
+
+    centerAct = new QAction(tr("&Center"), this);
+    centerAct->setCheckable(true);
+    centerAct->setShortcut(tr("Ctrl+E"));
+    centerAct->setStatusTip(tr("Center the selected text"));
+    connect(centerAct, SIGNAL(triggered(bool)), this, SLOT(center(bool)));
+
+    alignmentGroup = new QMenu(tr("&Alignment"),this);
+    alignmentGroup->addAction(leftAlignAct);
+    alignmentGroup->addAction(rightAlignAct);
+    alignmentGroup->addAction(justifyAct);
+    alignmentGroup->addAction(centerAct);
+    leftAlignAct->setChecked(true);
+
+
+
+
+}
+
+//------------------------------------------------------------------
+
+void TextZone::undo()
+{
+    textDocument->undo();
+    //QTextEdit::undo();
+}
+
+void TextZone::redo()
+{
+    textDocument->redo();
+}
+
+void TextZone::cut()
+{
+    QTextEdit::cut();
+}
+
+void TextZone::copy()
+{
+    QTextEdit::copy();
+}
+
+void TextZone::paste()
+{
+    QTextEdit::paste();
+}
+
+
+void TextZone::bold(bool boldBool)
+{
+    if(boldBool){
+        setFontWeight(75);
+    }
+    else{
+        setFontWeight(50);
+    }
+}
+
+void TextZone::italic(bool italBool)
+{
+    if(italBool){
+        setFontItalic(true);
+    }
+    else{
+        setFontItalic(false);
+    }
+}
+
+void TextZone::leftAlign(bool leftBool)
+{
+    if(leftBool){
+        centerAct->setChecked(false);
+        rightAlignAct->setChecked(false);
+        justifyAct->setChecked(false);
+        setAlignment(Qt::AlignLeft);
+    }
+    else{
+        leftAlignAct->setChecked(true);
+        leftAlign(true);
+    }
+}
+
+void TextZone::rightAlign(bool rightBool)
+{
+    if(rightBool){
+        centerAct->setChecked(false);
+        leftAlignAct->setChecked(false);
+        justifyAct->setChecked(false);
+        setAlignment(Qt::AlignRight);
+    }
+    else{
+        leftAlignAct->setChecked(true);
+        leftAlign(true);
+    }
+
+}
+
+void TextZone::justify(bool justBool)
+{
+    if(justBool){
+        centerAct->setChecked(false);
+        rightAlignAct->setChecked(false);
+        leftAlignAct->setChecked(false);
+        setAlignment(Qt::AlignJustify);
+    }
+    else{
+        leftAlignAct->setChecked(true);
+        leftAlign(true);
+    }
+
+}
+
+void TextZone::center(bool centBool)
+{
+    if(centBool){
+        rightAlignAct->setChecked(false);
+        leftAlignAct->setChecked(false);
+        justifyAct->setChecked(false);
+        setAlignment(Qt::AlignHCenter);
+    }
+    else{
+        leftAlignAct->setChecked(true);
+        leftAlign(true);
+    }
+}
+
+
+
+
+//----------------------------------------------------------------------
+
+void TextZone::contextMenuEvent(QContextMenuEvent *event)
+{
+    QMenu menu(this);
+    menu.addSeparator();
+    menu.addAction(boldAct);
+    menu.addAction(italicAct);
+    menu.addSeparator();
+    menu.addMenu(alignmentGroup);
+    menu.addSeparator();
+    menu.addAction(cutAct);
+    menu.addAction(copyAct);
+    menu.addAction(pasteAct);
+    menu.addSeparator();
+    menu.addAction(undoAct);
+    menu.addAction(redoAct);
+
+    menu.exec(event->globalPos());
+}
+
+//--------------------------------------------------------------------------------
+
+void TextZone::charFormat(QTextCharFormat cFormat)
+{
+    QString family = cFormat.fontFamily();
+    int weight = cFormat.fontWeight();
+    bool italic = cFormat.fontItalic();
+    Qt::Alignment align = alignment();
+
+    if(weight > 50)
+        boldAct->setChecked(true);
+    else
+        boldAct->setChecked(false);
+
+
+
+    if(italic)
+        italicAct->setChecked(true);
+    else
+        italicAct->setChecked(false);
+
+
+
+    if (align & Qt::AlignLeft){
+        centerAct->setChecked(false);
+        rightAlignAct->setChecked(false);
+        justifyAct->setChecked(false);
+        leftAlignAct->setChecked(true);
+    }
+    else if (align & Qt::AlignHCenter){
+        rightAlignAct->setChecked(false);
+        justifyAct->setChecked(false);
+        leftAlignAct->setChecked(false);
+        centerAct->setChecked(true);
+    }
+    else if (align & Qt::AlignRight){
+        centerAct->setChecked(false);
+        justifyAct->setChecked(false);
+        leftAlignAct->setChecked(false);
+        rightAlignAct->setChecked(true);
+    }
+    else if (align & Qt::AlignJustify){
+        centerAct->setChecked(false);
+        rightAlignAct->setChecked(false);
+        leftAlignAct->setChecked(false);
+        justifyAct->setChecked(true);
+    }
+
+
+
+}
+
+//--------------------------------------------------------------------------------
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+void TextZone::insertFromMimeData (const QMimeData *source )
+{
+    if(source->hasHtml()){
+
+//        QString htmlText = ;
+
+//htmlText
+        QTextDocument document;
+        document.setHtml(qvariant_cast<QString>(source->html()));
+
+        QTextCursor cursor = this->textCursor();
+        cursor.insertHtml(document.toHtml("utf-8"));
+        qDebug() << "insertFromMimeData Html";
+
+    }
+    else if(source->hasText()){
+        QString plainText = qvariant_cast<QString>(source->text());
+        QTextCursor cursor = this->textCursor();
+        cursor.insertText(plainText);
+        qDebug() << "insertFromMimeData plainText";
+
+    }
+
+}
+
+//--------------------------------------------------------------------------------
+
+
+bool TextZone::canInsertFromMimeData (const QMimeData *source) const
+{
+
+    if (source->hasHtml() || source->hasText())
+        return true;
+
+    else
+        return QTextEdit::canInsertFromMimeData(source);
+
+
+    qDebug() << "canInsertFromMimeData";
+}
+