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>
|
|
|
|
|
2008-02-07 00:05:18 +00:00
|
|
|
#include "FileDialog.h"
|
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "GuiClipboard.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2008-02-03 10:43:03 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "Cursor.h"
|
2006-06-26 17:18:28 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2008-02-07 00:05:18 +00:00
|
|
|
#include "support/convert.h"
|
|
|
|
#include "support/debug.h"
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lstrings.h"
|
2010-10-23 00:21:58 +00:00
|
|
|
#include "support/lyxtime.h"
|
2008-05-23 07:52:39 +00:00
|
|
|
|
2008-04-10 21:49:34 +00:00
|
|
|
#ifdef Q_WS_MACX
|
|
|
|
#include "support/linkback/LinkBackProxy.h"
|
|
|
|
#endif // Q_WS_MACX
|
|
|
|
|
|
|
|
#include "frontends/alert.h"
|
2006-06-26 17:18:28 +00:00
|
|
|
|
|
|
|
#include <QApplication>
|
2008-02-03 10:43:03 +00:00
|
|
|
#include <QBuffer>
|
2006-06-26 17:18:28 +00:00
|
|
|
#include <QClipboard>
|
2008-02-03 10:43:03 +00:00
|
|
|
#include <QDataStream>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QImage>
|
2007-01-13 18:29:50 +00:00
|
|
|
#include <QMimeData>
|
2006-06-26 17:18:28 +00:00
|
|
|
#include <QString>
|
2008-02-03 10:43:03 +00:00
|
|
|
#include <QStringList>
|
2006-06-26 17:18:28 +00:00
|
|
|
|
2010-12-11 23:04:24 +00:00
|
|
|
#include <boost/crc.hpp>
|
|
|
|
|
2008-04-19 10:37:08 +00:00
|
|
|
#include <memory>
|
2008-02-03 10:43:03 +00:00
|
|
|
#include <map>
|
2010-10-23 10:49:45 +00:00
|
|
|
#include <iostream>
|
2008-02-03 10:43:03 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
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
|
|
|
|
2006-06-26 17:18:28 +00:00
|
|
|
namespace frontend {
|
|
|
|
|
2010-10-23 00:21:58 +00:00
|
|
|
static QMimeData const * read_clipboard()
|
|
|
|
{
|
|
|
|
LYXERR(Debug::ACTION, "Getting Clipboard");
|
|
|
|
QMimeData const * source =
|
|
|
|
qApp->clipboard()->mimeData(QClipboard::Clipboard);
|
|
|
|
if (!source) {
|
|
|
|
LYXERR0("0 bytes (no QMimeData)");
|
2012-10-26 00:42:27 +00:00
|
|
|
return new QMimeData;
|
2010-10-23 00:21:58 +00:00
|
|
|
}
|
|
|
|
// It appears that doing IO between getting a mimeData object
|
|
|
|
// and using it can cause a crash (maybe Qt used IO
|
|
|
|
// as an excuse to free() it? Anyway let's not introduce
|
|
|
|
// any new IO here, so e.g. leave the following line commented.
|
|
|
|
// lyxerr << "Got Clipboard (" << (long) source << ")\n" ;
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CacheMimeData::update()
|
|
|
|
{
|
|
|
|
time_t const start_time = current_time();
|
|
|
|
LYXERR(Debug::ACTION, "Creating CacheMimeData object");
|
|
|
|
cached_formats_ = read_clipboard()->formats();
|
|
|
|
|
|
|
|
// Qt times out after 5 seconds if it does not recieve a response.
|
|
|
|
if (current_time() - start_time > 3) {
|
2010-10-23 08:29:57 +00:00
|
|
|
LYXERR0("No timely response from clipboard, perhaps process "
|
|
|
|
<< "holding clipboard is frozen?");
|
2010-10-23 00:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QByteArray CacheMimeData::data(QString const & mimeType) const
|
|
|
|
{
|
|
|
|
return read_clipboard()->data(mimeType);
|
|
|
|
}
|
|
|
|
|
2008-05-25 07:49:16 +00:00
|
|
|
|
|
|
|
QString const lyxMimeType(){ return "application/x-lyx"; }
|
|
|
|
QString const pdfMimeType(){ return "application/pdf"; }
|
|
|
|
QString const emfMimeType(){ return "image/x-emf"; }
|
|
|
|
QString const wmfMimeType(){ return "image/x-wmf"; }
|
2008-05-25 07:22:45 +00:00
|
|
|
|
2008-02-03 10:43:03 +00:00
|
|
|
|
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
|
2010-10-23 00:21:58 +00:00
|
|
|
if (cache_.hasFormat(lyxMimeType())) {
|
2007-01-13 18:29:50 +00:00
|
|
|
// data from ourself or some other LyX instance
|
2010-10-23 00:21:58 +00:00
|
|
|
QByteArray const ar = cache_.data(lyxMimeType());
|
2007-01-13 18:29:50 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-03 10:43:03 +00:00
|
|
|
FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
|
|
|
|
Clipboard::GraphicsType & type) const
|
|
|
|
{
|
|
|
|
// create file dialog filter according to the existing types in the clipboard
|
|
|
|
vector<Clipboard::GraphicsType> types;
|
2008-04-07 07:28:03 +00:00
|
|
|
if (hasGraphicsContents(Clipboard::EmfGraphicsType))
|
|
|
|
types.push_back(Clipboard::EmfGraphicsType);
|
|
|
|
if (hasGraphicsContents(Clipboard::WmfGraphicsType))
|
|
|
|
types.push_back(Clipboard::WmfGraphicsType);
|
2008-02-03 10:43:03 +00:00
|
|
|
if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
|
|
|
|
types.push_back(Clipboard::LinkBackGraphicsType);
|
|
|
|
if (hasGraphicsContents(Clipboard::PdfGraphicsType))
|
|
|
|
types.push_back(Clipboard::PdfGraphicsType);
|
|
|
|
if (hasGraphicsContents(Clipboard::PngGraphicsType))
|
|
|
|
types.push_back(Clipboard::PngGraphicsType);
|
|
|
|
if (hasGraphicsContents(Clipboard::JpegGraphicsType))
|
|
|
|
types.push_back(Clipboard::JpegGraphicsType);
|
|
|
|
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(!types.empty(), /**/);
|
2008-02-03 10:43:03 +00:00
|
|
|
|
|
|
|
// select prefered type if AnyGraphicsType was passed
|
|
|
|
if (type == Clipboard::AnyGraphicsType)
|
|
|
|
type = types.front();
|
|
|
|
|
|
|
|
// which extension?
|
|
|
|
map<Clipboard::GraphicsType, string> extensions;
|
|
|
|
map<Clipboard::GraphicsType, docstring> typeNames;
|
|
|
|
|
2008-04-07 07:28:03 +00:00
|
|
|
extensions[Clipboard::EmfGraphicsType] = "emf";
|
|
|
|
extensions[Clipboard::WmfGraphicsType] = "wmf";
|
2008-02-03 10:43:03 +00:00
|
|
|
extensions[Clipboard::LinkBackGraphicsType] = "linkback";
|
|
|
|
extensions[Clipboard::PdfGraphicsType] = "pdf";
|
|
|
|
extensions[Clipboard::PngGraphicsType] = "png";
|
|
|
|
extensions[Clipboard::JpegGraphicsType] = "jpeg";
|
|
|
|
|
2008-04-07 07:28:03 +00:00
|
|
|
typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile");
|
|
|
|
typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile");
|
2008-02-03 10:43:03 +00:00
|
|
|
typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
|
|
|
|
typeNames[Clipboard::PdfGraphicsType] = _("PDF");
|
|
|
|
typeNames[Clipboard::PngGraphicsType] = _("PNG");
|
|
|
|
typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
|
|
|
|
|
|
|
|
// find unused filename with primary extension
|
2010-04-21 01:19:09 +00:00
|
|
|
string document_path = cur.buffer()->fileName().onlyPath().absFileName();
|
2008-02-03 10:43:03 +00:00
|
|
|
unsigned newfile_number = 0;
|
|
|
|
FileName filename;
|
|
|
|
do {
|
|
|
|
++newfile_number;
|
2008-02-27 22:23:12 +00:00
|
|
|
filename = FileName(addName(document_path,
|
2008-02-03 10:43:03 +00:00
|
|
|
to_utf8(_("pasted"))
|
|
|
|
+ convert<string>(newfile_number) + "."
|
|
|
|
+ extensions[type]));
|
|
|
|
} while (filename.isReadableFile());
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// create file type filter, putting the prefered on to the front
|
2008-04-20 19:56:01 +00:00
|
|
|
QStringList filter;
|
2008-02-27 22:23:12 +00:00
|
|
|
for (size_t i = 0; i != types.size(); ++i) {
|
2008-02-03 10:43:03 +00:00
|
|
|
docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
|
2008-02-27 22:23:12 +00:00
|
|
|
+ " (*." + from_ascii(extensions[types[i]]) + ")";
|
2008-02-03 10:43:03 +00:00
|
|
|
if (types[i] == type)
|
2008-04-20 19:56:01 +00:00
|
|
|
filter.prepend(toqstr(s));
|
2008-02-03 10:43:03 +00:00
|
|
|
else
|
2008-04-20 19:56:01 +00:00
|
|
|
filter.append(toqstr(s));
|
2008-02-03 10:43:03 +00:00
|
|
|
}
|
2008-04-20 19:56:01 +00:00
|
|
|
filter = fileFilters(filter.join(";;"));
|
2008-02-03 10:43:03 +00:00
|
|
|
|
|
|
|
// show save dialog for the graphic
|
2008-03-05 23:10:53 +00:00
|
|
|
FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
|
2008-02-03 10:43:03 +00:00
|
|
|
FileDialog::Result result =
|
2010-04-21 01:19:09 +00:00
|
|
|
dlg.save(toqstr(filename.onlyPath().absFileName()), filter,
|
2008-03-05 23:10:53 +00:00
|
|
|
toqstr(filename.onlyFileName()));
|
2008-02-03 10:43:03 +00:00
|
|
|
|
|
|
|
if (result.first == FileDialog::Later)
|
|
|
|
return FileName();
|
|
|
|
|
2008-03-05 23:10:53 +00:00
|
|
|
string newFilename = fromqstr(result.second);
|
2008-02-03 10:43:03 +00:00
|
|
|
if (newFilename.empty()) {
|
|
|
|
cur.bv().message(_("Canceled."));
|
|
|
|
return FileName();
|
|
|
|
}
|
|
|
|
filename.set(newFilename);
|
|
|
|
|
|
|
|
// check the extension (the user could have changed it)
|
2010-04-21 01:19:09 +00:00
|
|
|
if (!suffixIs(ascii_lowercase(filename.absFileName()),
|
2008-02-03 10:43:03 +00:00
|
|
|
"." + extensions[type])) {
|
|
|
|
// the user changed the extension. Check if the type is available
|
2008-02-27 22:23:12 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 1; i != types.size(); ++i) {
|
2010-04-21 01:19:09 +00:00
|
|
|
if (suffixIs(ascii_lowercase(filename.absFileName()),
|
2008-02-03 10:43:03 +00:00
|
|
|
"." + extensions[types[i]])) {
|
|
|
|
type = types[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalid extension found, or none at all. In the latter
|
|
|
|
// case set the default extensions.
|
|
|
|
if (i == types.size()
|
|
|
|
&& filename.onlyFileName().find('.') == string::npos) {
|
|
|
|
filename.changeExtension("." + extensions[type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether the file exists and warn the user
|
|
|
|
if (!filename.exists())
|
|
|
|
break;
|
|
|
|
int ret = frontend::Alert::prompt(
|
|
|
|
_("Overwrite external file?"),
|
2008-03-17 02:49:47 +00:00
|
|
|
bformat(_("File %1$s already exists, do you want to overwrite it?"),
|
2010-04-21 01:19:09 +00:00
|
|
|
from_utf8(filename.absFileName())), 1, 1, _("&Overwrite"), _("&Cancel"));
|
2008-02-03 10:43:03 +00:00
|
|
|
if (ret == 0)
|
|
|
|
// overwrite, hence break the dialog loop
|
|
|
|
break;
|
|
|
|
|
|
|
|
// not overwrite, hence show the dialog again (i.e. loop)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
|
|
|
|
{
|
|
|
|
// get the filename from the user
|
|
|
|
FileName filename = getPastedGraphicsFileName(cur, type);
|
|
|
|
if (filename.empty())
|
|
|
|
return FileName();
|
|
|
|
|
|
|
|
// handle image cases first
|
|
|
|
if (type == PngGraphicsType || type == JpegGraphicsType) {
|
|
|
|
// get image from QImage from clipboard
|
|
|
|
QImage image = qApp->clipboard()->image();
|
|
|
|
if (image.isNull()) {
|
|
|
|
LYXERR(Debug::ACTION, "No image in clipboard");
|
|
|
|
return FileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert into graphics format
|
|
|
|
QByteArray ar;
|
|
|
|
QBuffer buffer(&ar);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
if (type == PngGraphicsType)
|
2010-04-21 01:19:09 +00:00
|
|
|
image.save(toqstr(filename.absFileName()), "PNG");
|
2008-02-03 10:43:03 +00:00
|
|
|
else if (type == JpegGraphicsType)
|
2010-04-21 01:19:09 +00:00
|
|
|
image.save(toqstr(filename.absFileName()), "JPEG");
|
2008-02-03 10:43:03 +00:00
|
|
|
else
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(false, /**/);
|
2008-02-03 10:43:03 +00:00
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get mime for type
|
|
|
|
QString mime;
|
|
|
|
switch (type) {
|
2008-05-25 07:49:16 +00:00
|
|
|
case PdfGraphicsType: mime = pdfMimeType(); break;
|
|
|
|
case LinkBackGraphicsType: mime = pdfMimeType(); break;
|
|
|
|
case EmfGraphicsType: mime = emfMimeType(); break;
|
|
|
|
case WmfGraphicsType: mime = wmfMimeType(); break;
|
2008-04-10 21:49:34 +00:00
|
|
|
default: LASSERT(false, /**/);
|
2008-02-03 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get data
|
2010-10-23 00:21:58 +00:00
|
|
|
if (!cache_.hasFormat(mime))
|
2008-02-03 10:43:03 +00:00
|
|
|
return FileName();
|
|
|
|
// data from ourself or some other LyX instance
|
2010-10-23 00:21:58 +00:00
|
|
|
QByteArray const ar = cache_.data(mime);
|
2013-04-12 20:12:47 +00:00
|
|
|
LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.constData()
|
2008-02-03 10:43:03 +00:00
|
|
|
<< "length = " << ar.count());
|
2013-04-12 20:12:47 +00:00
|
|
|
|
2010-04-21 01:19:09 +00:00
|
|
|
QFile f(toqstr(filename.absFileName()));
|
2008-02-03 10:43:03 +00:00
|
|
|
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
|
|
LYXERR(Debug::ACTION, "Error opening file "
|
2010-04-21 01:19:09 +00:00
|
|
|
<< filename.absFileName() << " for writing");
|
2008-02-03 10:43:03 +00:00
|
|
|
return FileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the (LinkBack) PDF data
|
|
|
|
f.write(ar);
|
|
|
|
if (type == LinkBackGraphicsType) {
|
|
|
|
#ifdef Q_WS_MACX
|
|
|
|
void const * linkBackData;
|
|
|
|
unsigned linkBackLen;
|
|
|
|
getLinkBackData(&linkBackData, &linkBackLen);
|
|
|
|
f.write((char *)linkBackData, linkBackLen);
|
|
|
|
quint32 pdfLen = ar.size();
|
|
|
|
QDataStream ds(&f);
|
|
|
|
ds << pdfLen; // big endian by default
|
|
|
|
#else
|
|
|
|
// only non-Mac this should never happen
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(false, /**/);
|
2008-02-03 10:43:03 +00:00
|
|
|
#endif // Q_WS_MACX
|
|
|
|
}
|
|
|
|
|
|
|
|
f.close();
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 18:29:50 +00:00
|
|
|
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);
|
2008-04-20 21:05:35 +00:00
|
|
|
LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << 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
|
|
|
|
2008-10-09 07:21:48 +00:00
|
|
|
return internalLineEnding(str);
|
2006-06-26 17:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-12 20:12:47 +00:00
|
|
|
void GuiClipboard::put(string const & lyx, docstring const & html, 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 << "' `"
|
2013-04-12 20:12:47 +00:00
|
|
|
<< to_utf8(html) << "' `" << 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());
|
2008-05-25 07:49:16 +00:00
|
|
|
data->setData(lyxMimeType(), qlyx);
|
2010-12-11 23:04:24 +00:00
|
|
|
// If the OS has not the concept of clipboard ownership,
|
|
|
|
// we recognize internal data through its checksum.
|
|
|
|
if (!hasInternal()) {
|
|
|
|
boost::crc_32_type crc32;
|
|
|
|
crc32.process_bytes(lyx.c_str(), lyx.size());
|
|
|
|
checksum = crc32.checksum();
|
|
|
|
}
|
2007-01-13 18:29:50 +00:00
|
|
|
}
|
|
|
|
// Don't test for text.empty() since we want to be able to clear the
|
|
|
|
// clipboard.
|
|
|
|
QString const qtext = toqstr(text);
|
|
|
|
data->setText(qtext);
|
2013-04-12 20:12:47 +00:00
|
|
|
QString const qhtml = toqstr(html);
|
|
|
|
data->setHtml(qhtml);
|
2007-01-13 18:29:50 +00:00
|
|
|
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
|
|
|
|
{
|
2010-10-23 00:21:58 +00:00
|
|
|
return cache_.hasFormat(lyxMimeType());
|
2008-02-03 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-04 23:31:32 +00:00
|
|
|
bool GuiClipboard::hasTextContents() const
|
|
|
|
{
|
2010-10-23 00:21:58 +00:00
|
|
|
return cache_.hasText();
|
2009-01-04 23:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-03 10:43:03 +00:00
|
|
|
bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
|
|
|
|
{
|
|
|
|
if (type == AnyGraphicsType) {
|
|
|
|
return hasGraphicsContents(PdfGraphicsType)
|
|
|
|
|| hasGraphicsContents(PngGraphicsType)
|
|
|
|
|| hasGraphicsContents(JpegGraphicsType)
|
2008-04-07 07:28:03 +00:00
|
|
|
|| hasGraphicsContents(EmfGraphicsType)
|
|
|
|
|| hasGraphicsContents(WmfGraphicsType)
|
2008-02-03 10:43:03 +00:00
|
|
|
|| hasGraphicsContents(LinkBackGraphicsType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle image cases first
|
|
|
|
if (type == PngGraphicsType || type == JpegGraphicsType)
|
2010-10-23 00:21:58 +00:00
|
|
|
return cache_.hasImage();
|
2008-02-03 10:43:03 +00:00
|
|
|
|
|
|
|
// handle LinkBack for Mac
|
|
|
|
if (type == LinkBackGraphicsType)
|
2008-05-23 07:52:39 +00:00
|
|
|
#ifdef Q_WS_MACX
|
2008-02-03 10:43:03 +00:00
|
|
|
return isLinkBackDataInPasteboard();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif // Q_WS_MACX
|
|
|
|
|
|
|
|
// get mime data
|
2010-10-23 00:21:58 +00:00
|
|
|
QStringList const & formats = cache_.formats();
|
2008-02-03 10:43:03 +00:00
|
|
|
LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
|
2008-05-23 07:52:39 +00:00
|
|
|
for (int i = 0; i < formats.size(); ++i)
|
2008-04-20 21:05:35 +00:00
|
|
|
LYXERR(Debug::ACTION, "Found format " << formats[i]);
|
2008-02-03 10:43:03 +00:00
|
|
|
|
|
|
|
// compute mime for type
|
|
|
|
QString mime;
|
|
|
|
switch (type) {
|
2008-05-25 07:49:16 +00:00
|
|
|
case EmfGraphicsType: mime = emfMimeType(); break;
|
|
|
|
case WmfGraphicsType: mime = wmfMimeType(); break;
|
|
|
|
case PdfGraphicsType: mime = pdfMimeType(); break;
|
2008-04-10 21:49:34 +00:00
|
|
|
default: LASSERT(false, /**/);
|
2008-02-03 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
2010-10-23 00:21:58 +00:00
|
|
|
return cache_.hasFormat(mime);
|
2006-06-26 17:18:28 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 08:53:54 +00:00
|
|
|
|
|
|
|
bool GuiClipboard::isInternal() const
|
|
|
|
{
|
2010-12-11 23:04:24 +00:00
|
|
|
if (!hasLyXContents())
|
|
|
|
return false;
|
|
|
|
|
2007-01-27 16:55:25 +00:00
|
|
|
// ownsClipboard() is also true for stuff coming from dialogs, e.g.
|
2010-12-11 23:04:24 +00:00
|
|
|
// the preamble dialog. This does only work on X11 and Windows, since
|
|
|
|
// ownsClipboard() is hardwired to return false on OS X.
|
|
|
|
if (hasInternal())
|
|
|
|
return qApp->clipboard()->ownsClipboard();
|
|
|
|
|
|
|
|
// We are running on OS X: Check whether clipboard data is from
|
|
|
|
// ourself by comparing its checksum with the stored one.
|
|
|
|
QByteArray const ar = cache_.data(lyxMimeType());
|
|
|
|
string const data(ar.data(), ar.count());
|
|
|
|
boost::crc_32_type crc32;
|
|
|
|
crc32.process_bytes(data.c_str(), data.size());
|
|
|
|
return checksum == crc32.checksum();
|
2007-01-03 08:53:54 +00:00
|
|
|
}
|
|
|
|
|
2007-01-07 16:43:38 +00:00
|
|
|
|
2007-12-28 15:56:05 +00:00
|
|
|
bool GuiClipboard::hasInternal() const
|
|
|
|
{
|
|
|
|
// Windows and Mac OS X does not have the concept of ownership;
|
|
|
|
// the clipboard is a fully global resource so all applications
|
2010-12-11 23:04:24 +00:00
|
|
|
// are notified of changes. However, on Windows ownership is
|
|
|
|
// emulated by Qt through the OleIsCurrentClipboard() API, while
|
|
|
|
// on Mac OS X we deal with this issue by ourself.
|
|
|
|
#if (defined(Q_WS_X11) || defined(Q_WS_WIN))
|
2007-12-28 15:56:05 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-30 20:28:15 +00:00
|
|
|
void GuiClipboard::on_dataChanged()
|
|
|
|
{
|
2010-10-23 00:21:58 +00:00
|
|
|
//Note: we do not really need to run cache_.update() unless the
|
|
|
|
//data has been changed *and* the GuiClipboard has been queried.
|
|
|
|
//However if run cache_.update() the moment a process grabs the
|
|
|
|
//clipboard, the process holding the clipboard presumably won't
|
|
|
|
//yet be frozen, and so we won't need to wait 5 seconds for Qt
|
|
|
|
//to time-out waiting for the clipboard.
|
|
|
|
cache_.update();
|
|
|
|
QStringList l = cache_.formats();
|
2008-02-03 10:43:03 +00:00
|
|
|
LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
|
2008-04-20 21:05:35 +00:00
|
|
|
for (int i = 0; i < l.count(); i++)
|
|
|
|
LYXERR(Debug::ACTION, l.value(i));
|
2013-04-12 20:12:47 +00:00
|
|
|
|
2007-09-30 20:28:15 +00:00
|
|
|
text_clipboard_empty_ = qApp->clipboard()->
|
|
|
|
text(QClipboard::Clipboard).isEmpty();
|
|
|
|
|
|
|
|
has_lyx_contents_ = hasLyXContents();
|
2008-02-03 10:43:03 +00:00
|
|
|
has_graphics_contents_ = hasGraphicsContents();
|
2007-09-30 20:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2008-02-03 10:43:03 +00:00
|
|
|
return !has_lyx_contents_ && !has_graphics_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
|
|
|
|
2008-11-14 14:28:50 +00:00
|
|
|
#include "moc_GuiClipboard.cpp"
|