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