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"
27 using lyx::support::internalLineEnding;
28 using lyx::support::externalLineEnding;
32 static char const * const mime_type = "application/x-lyx";
38 GuiClipboard::GuiClipboard()
40 connect(qApp->clipboard(), SIGNAL(dataChanged()),
41 this, SLOT(on_dataChanged()));
42 // initialize clipboard status.
47 string const GuiClipboard::getAsLyX() const
49 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
50 // We don't convert encodings here since the encoding of the
51 // clipboard contents is specified in the data itself
52 QMimeData const * source =
53 qApp->clipboard()->mimeData(QClipboard::Clipboard);
55 LYXERR(Debug::ACTION, "' (no QMimeData)");
58 if (source->hasFormat(mime_type)) {
59 // data from ourself or some other LyX instance
60 QByteArray const ar = source->data(mime_type);
61 string const s(ar.data(), ar.count());
62 LYXERR(Debug::ACTION, s << "'");
65 LYXERR(Debug::ACTION, "'");
70 docstring const GuiClipboard::getAsText() const
72 // text data from other applications
73 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
74 .normalized(QString::NormalizationForm_C);
75 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
79 return internalLineEnding(qstring_to_ucs4(str));
83 void GuiClipboard::put(string const & lyx, docstring const & text)
85 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
86 << to_utf8(text) << "')");
87 // We don't convert the encoding of lyx since the encoding of the
88 // clipboard contents is specified in the data itself
89 QMimeData * data = new QMimeData;
91 QByteArray const qlyx(lyx.c_str(), lyx.size());
92 data->setData(mime_type, qlyx);
94 // Don't test for text.empty() since we want to be able to clear the
96 QString const qtext = toqstr(text);
98 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
102 bool GuiClipboard::hasLyXContents() const
104 QMimeData const * const source =
105 qApp->clipboard()->mimeData(QClipboard::Clipboard);
106 return source && source->hasFormat(mime_type);
110 bool GuiClipboard::isInternal() const
112 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
113 // the preamble dialog
114 // FIXME: This does only work on X11, since ownsClipboard() is
115 // hardwired to return false on Windows and OS X.
116 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
120 void GuiClipboard::on_dataChanged()
122 text_clipboard_empty_ = qApp->clipboard()->
123 text(QClipboard::Clipboard).isEmpty();
125 has_lyx_contents_ = hasLyXContents();
129 bool GuiClipboard::empty() const
131 // We need to check both the plaintext and the LyX version of the
132 // clipboard. The plaintext version is empty if the LyX version
133 // contains only one inset, and the LyX version is empty if the
134 // clipboard does not come from LyX.
135 if (!text_clipboard_empty_)
137 return !has_lyx_contents_;
140 } // namespace frontend
143 #include "GuiClipboard_moc.cpp"