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"
18 #include "support/debug.h"
20 #include <QApplication>
25 #include "support/lstrings.h"
28 using namespace lyx::support;
30 static char const * const mime_type = "application/x-lyx";
36 GuiClipboard::GuiClipboard()
38 connect(qApp->clipboard(), SIGNAL(dataChanged()),
39 this, SLOT(on_dataChanged()));
40 // initialize clipboard status.
45 string const GuiClipboard::getAsLyX() const
47 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
48 // We don't convert encodings here since the encoding of the
49 // clipboard contents is specified in the data itself
50 QMimeData const * source =
51 qApp->clipboard()->mimeData(QClipboard::Clipboard);
53 LYXERR(Debug::ACTION, "' (no QMimeData)");
56 if (source->hasFormat(mime_type)) {
57 // data from ourself or some other LyX instance
58 QByteArray const ar = source->data(mime_type);
59 string const s(ar.data(), ar.count());
60 LYXERR(Debug::ACTION, s << "'");
63 LYXERR(Debug::ACTION, "'");
68 docstring const GuiClipboard::getAsText() const
70 // text data from other applications
71 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
72 .normalized(QString::NormalizationForm_C);
73 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
77 return internalLineEnding(qstring_to_ucs4(str));
81 void GuiClipboard::put(string const & lyx, docstring const & text)
83 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
84 << to_utf8(text) << "')");
85 // We don't convert the encoding of lyx since the encoding of the
86 // clipboard contents is specified in the data itself
87 QMimeData * data = new QMimeData;
89 QByteArray const qlyx(lyx.c_str(), lyx.size());
90 data->setData(mime_type, qlyx);
92 // Don't test for text.empty() since we want to be able to clear the
94 QString const qtext = toqstr(text);
96 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
100 bool GuiClipboard::hasLyXContents() const
102 QMimeData const * const source =
103 qApp->clipboard()->mimeData(QClipboard::Clipboard);
104 return source && source->hasFormat(mime_type);
108 bool GuiClipboard::isInternal() const
110 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
111 // the preamble dialog
112 // FIXME: This does only work on X11, since ownsClipboard() is
113 // hardwired to return false on Windows and OS X.
114 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
118 bool GuiClipboard::hasInternal() const
120 // Windows and Mac OS X does not have the concept of ownership;
121 // the clipboard is a fully global resource so all applications
122 // are notified of changes.
123 #if (defined(Q_WS_X11))
131 void GuiClipboard::on_dataChanged()
133 text_clipboard_empty_ = qApp->clipboard()->
134 text(QClipboard::Clipboard).isEmpty();
136 has_lyx_contents_ = hasLyXContents();
140 bool GuiClipboard::empty() const
142 // We need to check both the plaintext and the LyX version of the
143 // clipboard. The plaintext version is empty if the LyX version
144 // contains only one inset, and the LyX version is empty if the
145 // clipboard does not come from LyX.
146 if (!text_clipboard_empty_)
148 return !has_lyx_contents_;
151 } // namespace frontend
154 #include "GuiClipboard_moc.cpp"