3
* This file is part of BibleTime's source code, http://www.bibletime.info/.
5
* Copyright 1999-2006 by the BibleTime developers.
6
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
12
#include "cdragdropmgr.h"
14
#include "backend/cswordmoduleinfo.h"
15
#include "backend/cswordbackend.h"
16
#include "backend/cswordversekey.h"
17
#include "util/cpointers.h"
26
CDragDropMgr::BTDrag::BTDrag( const QString& xml, QWidget* dragSource, const char* name)
27
: QTextDrag(xml, dragSource, name) {}
30
//static function to see whether we can decode tje given mime type
31
bool CDragDropMgr::BTDrag::canDecode( const QMimeSource * mime ) {
32
if ( mime->provides("BibleTime/DND") ) { //we can decode this type!
35
return false; //not yet implemented
38
bool CDragDropMgr::BTDrag::provides( const char* type ) const {
39
return (type == "BibleTime/DND"); //return only true if the type is BibleTime/DND
42
const char* CDragDropMgr::BTDrag::format( int i ) const {
43
if ( i == 0) { //we support only one format!
44
return "BibleTime/DND";
49
bool CDragDropMgr::BTDrag::decode(const QMimeSource* e, QString& str) {
51
str = QString( e->encodedData( "BibleTime/DND" ) );
57
bool CDragDropMgr::BTDrag::decode(const QMimeSource* e, QString& str, QCString& /*subtype*/) {
58
return decode(e, str);
61
QByteArray CDragDropMgr::BTDrag::encodedData( const char* /*type*/ ) const {
62
return QTextDrag::encodedData("text/plain"); //hack because QTextDrag only accepts text/plainand not our BibleTime/DND type
65
///////////////////////////// new class //////////////////////
67
CDragDropMgr::Item::Item( const QString& text )
69
m_bookmarkModuleName(QString::null),
70
m_bookmarkKey(QString::null),
71
m_bookmarkDescription(QString::null),
74
CDragDropMgr::Item::Item( const QString& moduleName, const QString& key, const QString& description )
76
m_bookmarkModuleName(moduleName),
78
m_bookmarkDescription(description),
79
m_text(QString::null) {
80
//we have to make sure the key is saved in it's english representation, so we convert it
81
if (CSwordModuleInfo* mod = CPointers::backend()->findModuleByName( moduleName )) {
82
if (mod->type() == CSwordModuleInfo::Bible || mod->type() == CSwordModuleInfo::Commentary) {
87
m_bookmarkKey = vk.key();
88
// qWarning("english key of %s is %s", key.latin1(), m_bookmarkKey.latin1());
93
CDragDropMgr::Item::~Item() {}
95
const CDragDropMgr::Item::Type& CDragDropMgr::Item::type() const {
96
//returns the type of drag & drop action this item represents
100
/** Returns the text which is used by this DragDrop Item, only valid if type() == Text */
101
const QString& CDragDropMgr::Item::text() const {
102
// Q_ASSERT(!m_text.isEmpty());
106
/** Returns the key, ony valid if type() == Bookmark */
107
const QString& CDragDropMgr::Item::bookmarkKey() const {
108
// Q_ASSERT(!m_bookmarkKey.isEmpty());
109
return m_bookmarkKey;
112
/** Returns the bookmark module, ony valid if type() == Bookmark */
113
const QString& CDragDropMgr::Item::bookmarkModule() const {
114
// Q_ASSERT(!m_bookmarkModuleName.isEmpty());
115
return m_bookmarkModuleName;
118
/** Returns the bookmark description, ony valid if type() == Bookmark */
119
const QString& CDragDropMgr::Item::bookmarkDescription() const {
120
// Q_ASSERT(!m_bookmarkDescription.isEmpty());
121
return m_bookmarkDescription;
124
////////////////////////////////// NEW CLASS //////////////////////////
126
CDragDropMgr::CDragDropMgr() {}
128
CDragDropMgr::~CDragDropMgr() {}
130
const bool CDragDropMgr::canDecode( const QMimeSource* const mime ) {
131
if (CDragDropMgr::BTDrag::canDecode(mime)) {
134
else if( QTextDrag::canDecode(mime) ) {
135
qWarning("QTextDrag can decode this mime!");
141
QDragObject* const CDragDropMgr::dragObject( CDragDropMgr::ItemList& items, QWidget* dragSource ) {
142
if ( items.count() ) {
143
//process the items and set the data to the dragobject we return later
144
QDomDocument doc("DOC");
145
doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
146
QDomElement content = doc.createElement("BibleTimeDND");
147
content.setAttribute("syntaxVersion", "1.0");
148
doc.appendChild(content);
150
CDragDropMgr::ItemList::iterator it;
151
for ( it = items.begin(); it != items.end(); ++it ) {
153
if (item.type() == Item::Bookmark) { //a bookmark was dragged
154
//append the XML stuff for a bookmark
155
QDomElement bookmark = doc.createElement("BOOKMARK");
156
bookmark.setAttribute("key", item.bookmarkKey());
157
bookmark.setAttribute("description", item.bookmarkDescription());
158
bookmark.setAttribute("moduleName", item.bookmarkModule());
160
content.appendChild(bookmark);
162
else if (item.type() == Item::Text) { //plain text was dragged
163
//append the XML stuff for plain text
164
QDomElement plainText = doc.createElement("TEXT");
165
plainText.setAttribute("text", item.text());
167
content.appendChild(plainText);
171
BTDrag* dragObject = new BTDrag( doc.toString(), dragSource );
172
// qWarning("DND data created: %s", (const char*)doc.toString().utf8());
178
CDragDropMgr::ItemList CDragDropMgr::decode( const QMimeSource* const src) {
179
//if the drag was started by another widget which doesn't use CDragDropMgr (a drag created by QTextDrag)
180
if (canDecode(src) && QTextDrag::canDecode(src)) { //if we can decode but it's a QTextDrag and not a BTDrag object
182
QTextDrag::decode(src, text);
183
// qWarning(text.latin1());
185
CDragDropMgr::ItemList dndItems;
186
dndItems.append( Item(text) );
189
else if (!canDecode(src)) { //if we can't decode it
190
return CDragDropMgr::ItemList();
194
BTDrag::decode(src, xmlData);
196
if (xmlData.isEmpty()) { //something went wrong!
197
// qWarning("CDragDropMgr::decode: empty xml data!");
198
return CDragDropMgr::ItemList();
201
// qWarning("Drag&Drop data is: %s", xmlData.latin1());
204
//we can handle the dropEvent and have xml data to work on!
208
doc.setContent( xmlData );
210
QDomElement document = doc.documentElement();
211
if( document.tagName() != "BibleTimeDND" ) { //BibleTime was used in syntax version 1.0
212
qWarning("DragDropMgr::decode: Missing BibleTimeDND doc");
213
return CDragDropMgr::ItemList();
215
// see if there's a section with the name MAINWINDOW
216
QDomElement elem = document.firstChild().toElement();
217
while (!elem.isNull()) {
218
if (elem.tagName() == "BOOKMARK") { //we found a bookmark!
219
// qWarning("found a bookmark!");
220
const QString key = elem.hasAttribute("key") ? elem.attribute("key") : QString::null;
221
const QString moduleName = elem.hasAttribute("moduleName") ? elem.attribute("moduleName") : QString::null;
222
const QString description = elem.hasAttribute("description") ? elem.attribute("description") : QString::null;
224
dndItems.append( CDragDropMgr::Item(moduleName, key, description) );
226
else if (elem.tagName() == "TEXT") { //we found a plain text passage!
227
const QString text = elem.hasAttribute("text") ? elem.attribute("text") : QString::null;
228
dndItems.append( CDragDropMgr::Item(text) );
230
elem = elem.nextSibling().toElement();
236
/** Returns which type the given drop event has, if it's a mixed one (both bookmarks and plain text), which shouldn't happen, it return Item::Unknown. */
237
CDragDropMgr::Item::Type CDragDropMgr::dndType( const QMimeSource* e ) {
238
ItemList dndItems = decode(e);
239
if (dndItems.isEmpty()) {//wrong dropEvent or something strange
240
return Item::Unknown;
243
//check whether all items have the ssame type, if they do return the type
244
//as soon as two items have different types return Item::Unknown
245
ItemList::Iterator it;
246
Item::Type type = Item::Unknown;
247
for( it = dndItems.begin(); it != dndItems.end(); ++it ) {
248
if( type == Item::Unknown) { //if Unknown is set this is the first loop, don't return Unknown
251
else if (type != (*it).type() ) {//items have different type, return Item::Unknown
252
return Item::Unknown;