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