2006-06-26 17:18:28 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file qt4/GuiClipboard.cpp
|
2006-06-26 17:18:28 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
* \author Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2006-07-08 13:27:43 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2006-06-26 17:18:28 +00:00
|
|
|
#include "GuiClipboard.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2006-06-26 17:18:28 +00:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
2007-01-13 18:29:50 +00:00
|
|
|
#include <QMimeData>
|
2006-06-26 17:18:28 +00:00
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
2007-09-11 21:27:57 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-01-13 18:29:50 +00:00
|
|
|
|
2007-04-25 16:39:21 +00:00
|
|
|
static char const * const mime_type = "application/x-lyx";
|
2007-01-13 18:29:50 +00:00
|
|
|
|
2006-06-26 17:18:28 +00:00
|
|
|
namespace lyx {
|
2007-12-12 10:16:00 +00:00
|
|
|
|
|
|
|
using support::internalLineEnding;
|
|
|
|
using support::externalLineEnding;
|
|
|
|
|
2006-06-26 17:18:28 +00:00
|
|
|
namespace frontend {
|
|
|
|
|
2007-09-30 20:28:15 +00:00
|
|
|
GuiClipboard::GuiClipboard()
|
|
|
|
{
|
|
|
|
connect(qApp->clipboard(), SIGNAL(dataChanged()),
|
|
|
|
this, SLOT(on_dataChanged()));
|
|
|
|
// initialize clipboard status.
|
2007-10-01 00:11:21 +00:00
|
|
|
on_dataChanged();
|
2007-09-30 20:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 18:29:50 +00:00
|
|
|
string const GuiClipboard::getAsLyX() const
|
2006-06-26 17:18:28 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
|
2007-01-13 18:29:50 +00:00
|
|
|
// We don't convert encodings here since the encoding of the
|
|
|
|
// clipboard contents is specified in the data itself
|
|
|
|
QMimeData const * source =
|
|
|
|
qApp->clipboard()->mimeData(QClipboard::Clipboard);
|
|
|
|
if (!source) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::ACTION, "' (no QMimeData)");
|
2007-01-13 18:29:50 +00:00
|
|
|
return string();
|
|
|
|
}
|
|
|
|
if (source->hasFormat(mime_type)) {
|
|
|
|
// data from ourself or some other LyX instance
|
|
|
|
QByteArray const ar = source->data(mime_type);
|
|
|
|
string const s(ar.data(), ar.count());
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::ACTION, s << "'");
|
2007-01-13 18:29:50 +00:00
|
|
|
return s;
|
|
|
|
}
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::ACTION, "'");
|
2007-01-13 18:29:50 +00:00
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const GuiClipboard::getAsText() const
|
|
|
|
{
|
|
|
|
// text data from other applications
|
2007-04-03 08:15:39 +00:00
|
|
|
QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
|
2007-07-05 14:45:00 +00:00
|
|
|
.normalized(QString::NormalizationForm_C);
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
|
2006-06-26 17:18:28 +00:00
|
|
|
if (str.isNull())
|
2006-09-03 07:02:38 +00:00
|
|
|
return docstring();
|
2006-06-26 17:18:28 +00:00
|
|
|
|
2006-09-03 07:02:38 +00:00
|
|
|
return internalLineEnding(qstring_to_ucs4(str));
|
2006-06-26 17:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 18:29:50 +00:00
|
|
|
void GuiClipboard::put(string const & lyx, docstring const & text)
|
2006-06-26 17:18:28 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
|
|
|
|
<< to_utf8(text) << "')");
|
2007-01-13 18:29:50 +00:00
|
|
|
// We don't convert the encoding of lyx since the encoding of the
|
|
|
|
// clipboard contents is specified in the data itself
|
|
|
|
QMimeData * data = new QMimeData;
|
|
|
|
if (!lyx.empty()) {
|
|
|
|
QByteArray const qlyx(lyx.c_str(), lyx.size());
|
|
|
|
data->setData(mime_type, qlyx);
|
|
|
|
}
|
|
|
|
// Don't test for text.empty() since we want to be able to clear the
|
|
|
|
// clipboard.
|
|
|
|
QString const qtext = toqstr(text);
|
|
|
|
data->setText(qtext);
|
|
|
|
qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
|
|
|
|
}
|
|
|
|
|
2006-06-26 17:18:28 +00:00
|
|
|
|
2007-01-13 18:29:50 +00:00
|
|
|
bool GuiClipboard::hasLyXContents() const
|
|
|
|
{
|
|
|
|
QMimeData const * const source =
|
|
|
|
qApp->clipboard()->mimeData(QClipboard::Clipboard);
|
|
|
|
return source && source->hasFormat(mime_type);
|
2006-06-26 17:18:28 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 08:53:54 +00:00
|
|
|
|
|
|
|
bool GuiClipboard::isInternal() const
|
|
|
|
{
|
2007-01-27 16:55:25 +00:00
|
|
|
// ownsClipboard() is also true for stuff coming from dialogs, e.g.
|
|
|
|
// the preamble dialog
|
2007-04-03 08:27:23 +00:00
|
|
|
// FIXME: This does only work on X11, since ownsClipboard() is
|
|
|
|
// hardwired to return false on Windows and OS X.
|
2007-01-27 16:55:25 +00:00
|
|
|
return qApp->clipboard()->ownsClipboard() && hasLyXContents();
|
2007-01-03 08:53:54 +00:00
|
|
|
}
|
|
|
|
|
2007-01-07 16:43:38 +00:00
|
|
|
|
2007-09-30 20:28:15 +00:00
|
|
|
void GuiClipboard::on_dataChanged()
|
|
|
|
{
|
|
|
|
text_clipboard_empty_ = qApp->clipboard()->
|
|
|
|
text(QClipboard::Clipboard).isEmpty();
|
|
|
|
|
|
|
|
has_lyx_contents_ = hasLyXContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-07 16:43:38 +00:00
|
|
|
bool GuiClipboard::empty() const
|
|
|
|
{
|
2007-02-21 21:47:44 +00:00
|
|
|
// We need to check both the plaintext and the LyX version of the
|
|
|
|
// clipboard. The plaintext version is empty if the LyX version
|
2007-09-30 20:28:15 +00:00
|
|
|
// contains only one inset, and the LyX version is empty if the
|
2007-02-21 21:47:44 +00:00
|
|
|
// clipboard does not come from LyX.
|
2007-09-30 20:28:15 +00:00
|
|
|
if (!text_clipboard_empty_)
|
2007-02-21 21:47:44 +00:00
|
|
|
return false;
|
2007-09-30 20:28:15 +00:00
|
|
|
return !has_lyx_contents_;
|
2007-01-07 16:43:38 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 17:18:28 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2007-09-30 20:28:15 +00:00
|
|
|
|
|
|
|
#include "GuiClipboard_moc.cpp"
|