summaryrefslogtreecommitdiff
path: root/src/frontend/display/cplainwritedisplay.cpp
blob: 720aa721cacdc395b506572616d5a4ca1b419b9d (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 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 <boost/scoped_ptr.hpp>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QMenu>
#include <QDebug>

#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()));
}

CPlainWriteDisplay::~CPlainWriteDisplay() {}

/** 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() {
    qDebug() << "CPlainWriteDisplay::view()";
    return this;
}

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

void CPlainWriteDisplay::print( const CDisplay::TextPart, CSwordBackend::DisplayOptions, CSwordBackend::FilterOptions) {
}

/** 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;
}

/** Reimplementation from QTextEdit. Provides an popup menu for the given position. */
QMenu* CPlainWriteDisplay::createPopupMenu( const QPoint& /*pos*/ ) {
    return installedPopup();
}
//
///** Reimplementation from QTextEdit. Provides an popup menu for the given position. */
//QMenu* CPlainWriteDisplay::createPopupMenu( ) {
//	return installedPopup();
//}

/** 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 ) {
    //qDebug() << "CPlainWriteDisplay::dropEvent";
    const BTMimeData* mimedata = qobject_cast<const BTMimeData*>(e->mimeData());

    if ( mimedata && mimedata->hasFormat("BibleTime/Bookmark") ) {
        //qDebug() << "format was bookmark";
        e->acceptProposedAction();

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

            CSwordModuleInfo* module = backend()->findModuleByName((*it).module());
            boost::scoped_ptr<CSwordKey> key( CSwordKey::createInstance(module) );
            key->key( (*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")) {
        //qDebug() << "format was plain text";
        e->acceptProposedAction();
        setTextCursor(cursorForPosition(e->pos()));
        textCursor().insertText( e->mimeData()->text() );
    }
}