summaryrefslogtreecommitdiff
path: root/src/frontend/display/cplainwritedisplay.cpp
blob: 07cecf30a1fe1d86f3d63a7f1e2c8acd0d777c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2011 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/display/cplainwritedisplay.h"

#include <QSharedPointer>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QMenu>

#include "backend/keys/cswordkey.h"
#include "frontend/cdragdrop.h"
#include "frontend/displaywindow/btactioncollection.h"
#include "frontend/displaywindow/cdisplaywindow.h"
#include "frontend/displaywindow/cwritewindow.h"


CPlainWriteDisplay::CPlainWriteDisplay(CWriteWindow* parentWindow, QWidget* parent) : QTextEdit(parentWindow ? parentWindow : parent), CWriteDisplay(parentWindow) {
    setAcceptRichText(false);
    setAcceptDrops(true);
    viewport()->setAcceptDrops(true);

    connect(this, SIGNAL(textChanged()),
            connectionsProxy(), SLOT(emitTextChanged()));
}

/** Reimplementation. */
void CPlainWriteDisplay::selectAll() {
    QTextEdit::selectAll();
}

void CPlainWriteDisplay::setText( const QString& newText ) {
    //make sure the text has been converted to show \n instead of <br/>
    QString text = newText;
//     text.replace("\n<br /><!-- BT newline -->\n", "\n");
    text.replace("<br />", "\n"); //inserted by BT or the Qt textedit widget

    QTextEdit::setText(text);
}

bool CPlainWriteDisplay::hasSelection() {
    /// \todo test this
    return textCursor().hasSelection();
}

QWidget *CPlainWriteDisplay::view() {
    return this;
}

const QString CPlainWriteDisplay::text( const CDisplay::TextType /*format*/, const CDisplay::TextPart /*part*/) {
    return QString::null;
}

/** Sets the current status of the edit widget. */
void CPlainWriteDisplay::setModified( const bool modified ) {
    document()->setModified(modified);
}

/** Reimplementation. */
bool CPlainWriteDisplay::isModified() const {
    return document()->isModified();
}


/** Returns the text of this edit widget. */
const QString CPlainWriteDisplay::plainText() {
    QString ret = QTextEdit::toPlainText();

    //in plain text mode the text just contains newlines, convert them into <br/> before we return the text for display in a HTML widget
    ret.replace("\n", "<br />");

    return ret;
}

/** Creates the necessary action objects and puts them on the toolbar. */
void CPlainWriteDisplay::setupToolbar(QToolBar*, BtActionCollection*) {}

/** Reimplementation to insert the text of a dragged reference into the edit view. */
void CPlainWriteDisplay::dragEnterEvent( QDragEnterEvent* e ) {
    //if (CDragDropMgr::canDecode(e)) {
    if (e->mimeData()->hasFormat("BibleTime/Bookmark") || e->mimeData()->hasFormat("text/plain")) {
        e->acceptProposedAction();
    }
    else {
        //e->accept(false);
        e->ignore();
    }
}

/** Reimplementation to insert the text of a dragged reference into the edit view. */
void CPlainWriteDisplay::dragMoveEvent( QDragMoveEvent* e ) {
    if (e->mimeData()->hasFormat("BibleTime/Bookmark") || e->mimeData()->hasFormat("text/plain")) {
        //placeCursor(e->pos());
        setTextCursor(cursorForPosition(e->pos()));
        ensureCursorVisible();
        e->acceptProposedAction();
    }
    else {
        //e->accept(false);
        e->ignore();
    }
}

/** Reimplementation to manage drops of our drag and drop objects. */
void CPlainWriteDisplay::dropEvent( QDropEvent* e ) {
    const BTMimeData* mimedata = qobject_cast<const BTMimeData*>(e->mimeData());

    if ( mimedata && mimedata->hasFormat("BibleTime/Bookmark") ) {
        e->acceptProposedAction();

        BTMimeData::ItemList items = mimedata->bookmarks();
        BTMimeData::ItemList::iterator it;
        for (it = items.begin(); it != items.end(); ++it) {

            CSwordModuleInfo *module = CSwordBackend::instance()->findModuleByName((*it).module());
            QSharedPointer<CSwordKey> key( CSwordKey::createInstance(module) );
            key->setKey((*it).key());
            QString moduleText = key->strippedText();

            const QString text = QString::fromLatin1("%1\n(%2, %3)\n").arg(moduleText).arg((*it).key()).arg((*it).module());

            setTextCursor(cursorForPosition(e->pos()));
            textCursor().insertText( text );
        }
    }
    else if ( e->mimeData()->hasFormat("text/plain")) {
        e->acceptProposedAction();
        setTextCursor(cursorForPosition(e->pos()));
        textCursor().insertText( e->mimeData()->text() );
    }
}