]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiClipboard.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / GuiClipboard.cpp
1 // -*- C++ -*-
2 /**
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.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiClipboard.h"
16 #include "qt_helpers.h"
17
18 #include "debug.h"
19
20 #include <QApplication>
21 #include <QClipboard>
22 #include <QMimeData>
23 #include <QString>
24
25 #include "support/lstrings.h"
26 using lyx::support::internalLineEnding;
27 using lyx::support::externalLineEnding;
28
29 using std::endl;
30 using std::string;
31
32 static char const * const mime_type = "application/x-lyx";
33
34
35 namespace lyx {
36 namespace frontend {
37
38 string const GuiClipboard::getAsLyX() const
39 {
40         LYXERR(Debug::ACTION) << "GuiClipboard::getAsLyX(): `";
41         // We don't convert encodings here since the encoding of the
42         // clipboard contents is specified in the data itself
43         QMimeData const * source =
44                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
45         if (!source) {
46                 LYXERR(Debug::ACTION) << "' (no QMimeData)" << endl;
47                 return string();
48         }
49         if (source->hasFormat(mime_type)) {
50                 // data from ourself or some other LyX instance
51                 QByteArray const ar = source->data(mime_type);
52                 string const s(ar.data(), ar.count());
53                 LYXERR(Debug::ACTION) << s << "'" << endl;
54                 return s;
55         }
56         LYXERR(Debug::ACTION) << "'" << endl;
57         return string();
58 }
59
60
61 docstring const GuiClipboard::getAsText() const
62 {
63         // text data from other applications
64         QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
65                                 .normalized(QString::NormalizationForm_KC);
66         LYXERR(Debug::ACTION) << "GuiClipboard::getAsText(): `"
67                               << fromqstr(str) << "'" << endl;
68         if (str.isNull())
69                 return docstring();
70
71         return internalLineEnding(qstring_to_ucs4(str));
72 }
73
74
75 void GuiClipboard::put(string const & lyx, docstring const & text)
76 {
77         LYXERR(Debug::ACTION) << "GuiClipboard::put(`" << lyx << "' `"
78                               << to_utf8(text) << "')" << endl;
79         // We don't convert the encoding of lyx since the encoding of the
80         // clipboard contents is specified in the data itself
81         QMimeData * data = new QMimeData;
82         if (!lyx.empty()) {
83                 QByteArray const qlyx(lyx.c_str(), lyx.size());
84                 data->setData(mime_type, qlyx);
85         }
86         // Don't test for text.empty() since we want to be able to clear the
87         // clipboard.
88         QString const qtext = toqstr(text);
89         data->setText(qtext);
90         qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
91 }
92
93
94 bool GuiClipboard::hasLyXContents() const
95 {
96         QMimeData const * const source =
97                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
98         return source && source->hasFormat(mime_type);
99 }
100
101
102 bool GuiClipboard::isInternal() const
103 {
104         // ownsClipboard() is also true for stuff coming from dialogs, e.g.
105         // the preamble dialog
106         // FIXME: This does only work on X11, since ownsClipboard() is
107         // hardwired to return false on Windows and OS X.
108         return qApp->clipboard()->ownsClipboard() && hasLyXContents();
109 }
110
111
112 bool GuiClipboard::empty() const
113 {
114         // We need to check both the plaintext and the LyX version of the
115         // clipboard. The plaintext version is empty if the LyX version
116         // contains only one inset, and the LyX version is empry if the
117         // clipboard does not come from LyX.
118         if (!qApp->clipboard()->text(QClipboard::Clipboard).isEmpty())
119                 return false;
120         return !hasLyXContents();
121 }
122
123 } // namespace frontend
124 } // namespace lyx