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