#include <config.h>
#include "GuiSelection.h"
+
#include "qt_helpers.h"
-#include "debug.h"
+#include "support/debug.h"
+#include "support/lstrings.h"
#include <QApplication>
#include <QClipboard>
#include <QString>
-#include "support/lstrings.h"
-using lyx::support::internalLineEnding;
-using lyx::support::externalLineEnding;
-
-using std::endl;
namespace lyx {
namespace frontend {
+
+GuiSelection::GuiSelection()
+ : schedule_check_(true),
+ selection_supported_(qApp->clipboard()->supportsSelection())
+{
+ connect(qApp->clipboard(), SIGNAL(selectionChanged()),
+ this, SLOT(on_dataChanged()));
+}
+
+
void GuiSelection::haveSelection(bool own)
{
- if (!qApp->clipboard()->supportsSelection())
+ if (!selection_supported_)
return;
// Tell qt that we have a selection by setting a dummy selection.
// an application actually requests it.
// This way calling Selection::have() is cheap and we can do it as
// often as we want.
+ //LYXERR(Debug::SELECTION, "GuiSelection: setting dummy selection");
if (own)
qApp->clipboard()->setText(QString(), QClipboard::Selection);
// We don't need to do anything if own = false, as this case is
docstring const GuiSelection::get() const
{
QString const str = qApp->clipboard()->text(QClipboard::Selection)
- .normalized(QString::NormalizationForm_KC);
- LYXERR(Debug::ACTION) << "GuiSelection::get: " << fromqstr(str)
- << endl;
+ .normalized(QString::NormalizationForm_C);
+ LYXERR(Debug::SELECTION, "GuiSelection::get: " << str);
if (str.isNull())
return docstring();
- return internalLineEnding(qstring_to_ucs4(str));
+ return internalLineEnding(str);
}
void GuiSelection::put(docstring const & str)
{
- LYXERR(Debug::ACTION) << "GuiSelection::put: " << to_utf8(str) << endl;
+ LYXERR(Debug::SELECTION, "GuiSelection::put: " << to_utf8(str));
- qApp->clipboard()->setText(toqstr(externalLineEnding(str)),
+ qApp->clipboard()->setText(externalLineEnding(str),
QClipboard::Selection);
}
+void GuiSelection::on_dataChanged()
+{
+ schedule_check_ = true;
+ LYXERR(Debug::SELECTION, "GuiSelection::on_dataChanged");
+}
+
+
bool GuiSelection::empty() const
{
- if (!qApp->clipboard()->supportsSelection())
+ if (!selection_supported_)
return true;
- return qApp->clipboard()->text(QClipboard::Selection).isEmpty();
+ // Cache which is to speed up selection-status read
+ // (4 calls when open Edit menu).
+ static bool text_selection_empty;
+ if (schedule_check_) {
+ text_selection_empty = qApp->clipboard()->
+ text(QClipboard::Selection).isEmpty();
+ schedule_check_ = false;
+ }
+
+ LYXERR(Debug::SELECTION, "GuiSelection::filled: " << !text_selection_empty);
+ return text_selection_empty;
}
} // namespace frontend
} // namespace lyx
+
+#include "moc_GuiSelection.cpp"