3 * \file qt4/GuiSelection.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiSelection.h"
17 #include "qt_helpers.h"
19 #include "support/debug.h"
20 #include "support/lstrings.h"
22 #include <QApplication>
30 using support::internalLineEnding;
31 using support::externalLineEnding;
34 GuiSelection::GuiSelection()
35 : selection_supported_(qApp->clipboard()->supportsSelection()),
38 connect(qApp->clipboard(), SIGNAL(selectionChanged()),
39 this, SLOT(on_dataChanged()));
43 void GuiSelection::haveSelection(bool own)
45 if (!qApp->clipboard()->supportsSelection())
48 // Tell qt that we have a selection by setting a dummy selection.
49 // We don't use the interface provided by Qt for setting the
50 // selection for performance reasons (see documentation of
51 // Selection::put()). Instead we only tell here that we have a
52 // selection by setting the selection to the empty string.
53 // The real selection is set in GuiApplication::x11EventFilter when
54 // an application actually requests it.
55 // This way calling Selection::have() is cheap and we can do it as
57 //LYXERR(Debug::SELECTION, "GuiSelection: setting dummy selection");
59 qApp->clipboard()->setText(QString(), QClipboard::Selection);
60 // We don't need to do anything if own = false, as this case is
62 // FIXME (gb): This is wrong. What is missing here is rather a call of
64 // qApp->clipboard()->clear(QClipboard::Selection);
65 // Since we do not issue this call we rather implement
66 // "persistent selections" as far as X is concerned.
70 docstring const GuiSelection::get() const
72 QString const str = qApp->clipboard()->text(QClipboard::Selection)
73 .normalized(QString::NormalizationForm_C);
74 LYXERR(Debug::SELECTION, "GuiSelection::get: " << str);
78 return internalLineEnding(qstring_to_ucs4(str));
82 void GuiSelection::put(docstring const & str)
84 LYXERR(Debug::SELECTION, "GuiSelection::put: " << to_utf8(str));
86 qApp->clipboard()->setText(toqstr(externalLineEnding(str)),
87 QClipboard::Selection);
91 void GuiSelection::on_dataChanged()
93 schedule_check_ = true;
94 LYXERR(Debug::SELECTION, "GuiSelection::on_dataChanged");
98 bool GuiSelection::empty() const
100 if (!selection_supported_)
103 // Cache which is to speed up selection-status read
104 // (4 calls when openi Edit menu).
105 static bool text_selection_empty;
106 if (schedule_check_) {
107 text_selection_empty = qApp->clipboard()->
108 text(QClipboard::Selection).isEmpty();
109 schedule_check_ = false;
112 LYXERR(Debug::SELECTION, "GuiSelection::filled: " << !text_selection_empty);
113 return text_selection_empty;
116 } // namespace frontend
119 #include "GuiSelection_moc.cpp"