3 * \file qt4/GuiClipboard.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiClipboard.h"
16 #include "qt_helpers.h"
20 #include <QApplication>
25 #include "support/lstrings.h"
27 using lyx::support::internalLineEnding;
28 using lyx::support::externalLineEnding;
33 static char const * const mime_type = "application/x-lyx";
39 GuiClipboard::GuiClipboard()
41 connect(qApp->clipboard(), SIGNAL(dataChanged()),
42 this, SLOT(on_dataChanged()));
43 // initialize clipboard status.
48 string const GuiClipboard::getAsLyX() const
50 LYXERR(Debug::ACTION) << "GuiClipboard::getAsLyX(): `";
51 // We don't convert encodings here since the encoding of the
52 // clipboard contents is specified in the data itself
53 QMimeData const * source =
54 qApp->clipboard()->mimeData(QClipboard::Clipboard);
56 LYXERR(Debug::ACTION) << "' (no QMimeData)" << endl;
59 if (source->hasFormat(mime_type)) {
60 // data from ourself or some other LyX instance
61 QByteArray const ar = source->data(mime_type);
62 string const s(ar.data(), ar.count());
63 LYXERR(Debug::ACTION) << s << "'" << endl;
66 LYXERR(Debug::ACTION) << "'" << endl;
71 docstring const GuiClipboard::getAsText() const
73 // text data from other applications
74 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
75 .normalized(QString::NormalizationForm_C);
76 LYXERR(Debug::ACTION) << "GuiClipboard::getAsText(): `"
77 << fromqstr(str) << "'" << endl;
81 return internalLineEnding(qstring_to_ucs4(str));
85 void GuiClipboard::put(string const & lyx, docstring const & text)
87 LYXERR(Debug::ACTION) << "GuiClipboard::put(`" << lyx << "' `"
88 << to_utf8(text) << "')" << endl;
89 // We don't convert the encoding of lyx since the encoding of the
90 // clipboard contents is specified in the data itself
91 QMimeData * data = new QMimeData;
93 QByteArray const qlyx(lyx.c_str(), lyx.size());
94 data->setData(mime_type, qlyx);
96 // Don't test for text.empty() since we want to be able to clear the
98 QString const qtext = toqstr(text);
100 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
104 bool GuiClipboard::hasLyXContents() const
106 QMimeData const * const source =
107 qApp->clipboard()->mimeData(QClipboard::Clipboard);
108 return source && source->hasFormat(mime_type);
112 bool GuiClipboard::isInternal() const
114 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
115 // the preamble dialog
116 // FIXME: This does only work on X11, since ownsClipboard() is
117 // hardwired to return false on Windows and OS X.
118 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
122 void GuiClipboard::on_dataChanged()
124 text_clipboard_empty_ = qApp->clipboard()->
125 text(QClipboard::Clipboard).isEmpty();
127 has_lyx_contents_ = hasLyXContents();
131 bool GuiClipboard::empty() const
133 // We need to check both the plaintext and the LyX version of the
134 // clipboard. The plaintext version is empty if the LyX version
135 // contains only one inset, and the LyX version is empty if the
136 // clipboard does not come from LyX.
137 if (!text_clipboard_empty_)
139 return !has_lyx_contents_;
142 } // namespace frontend
145 #include "GuiClipboard_moc.cpp"