2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-08-31 05:53:55 +00:00
|
|
|
* \file GuiCommandBuffer.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2007-10-07 18:40:01 +00:00
|
|
|
* \author Lars
|
|
|
|
* \author Asger and Jürgen
|
2006-03-05 17:24:44 +00:00
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
// Qt defines a macro 'signals' that clashes with a boost namespace.
|
|
|
|
// All is well if the namespace is visible first.
|
2006-06-20 09:33:01 +00:00
|
|
|
#include "GuiView.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiCommandBuffer.h"
|
|
|
|
#include "GuiCommandEdit.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2007-10-07 18:40:01 +00:00
|
|
|
#include "BufferView.h"
|
|
|
|
#include "Cursor.h"
|
|
|
|
#include "LyXFunc.h"
|
|
|
|
#include "LyXAction.h"
|
|
|
|
#include "FuncRequest.h"
|
|
|
|
|
|
|
|
#include "frontends/LyXView.h"
|
|
|
|
|
|
|
|
#include "support/lyxalgo.h"
|
|
|
|
#include "support/lstrings.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
|
2006-10-22 14:37:32 +00:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QKeyEvent>
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QLayout>
|
2006-10-22 14:37:32 +00:00
|
|
|
#include <QListWidget>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPixmap>
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QPushButton>
|
2006-10-22 14:37:32 +00:00
|
|
|
#include <QToolTip>
|
|
|
|
#include <QVBoxLayout>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-10-07 18:40:01 +00:00
|
|
|
using std::back_inserter;
|
|
|
|
using std::transform;
|
2006-03-05 17:24:44 +00:00
|
|
|
using std::string;
|
2007-10-07 18:40:01 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2007-10-07 18:40:01 +00:00
|
|
|
using support::prefixIs;
|
|
|
|
using support::libFileSearch;
|
|
|
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class QTempListBox : public QListWidget {
|
|
|
|
public:
|
|
|
|
QTempListBox() {
|
|
|
|
//setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setWindowModality(Qt::WindowModal);
|
|
|
|
setWindowFlags(Qt::Popup);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
}
|
|
|
|
protected:
|
2006-10-22 14:37:32 +00:00
|
|
|
void mouseReleaseEvent(QMouseEvent * ev) {
|
|
|
|
if (ev->x() < 0 || ev->y() < 0
|
|
|
|
|| ev->x() > width() || ev->y() > height()) {
|
2006-03-05 17:24:44 +00:00
|
|
|
hide();
|
|
|
|
} else {
|
2006-10-22 14:37:32 +00:00
|
|
|
// emit signal
|
2006-12-30 10:30:02 +00:00
|
|
|
itemPressed(currentItem());
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 14:37:32 +00:00
|
|
|
void keyPressEvent(QKeyEvent * ev) {
|
|
|
|
if (ev->key() == Qt::Key_Escape) {
|
2006-03-05 17:24:44 +00:00
|
|
|
hide();
|
|
|
|
return;
|
2007-06-21 06:38:50 +00:00
|
|
|
} else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
|
|
|
|
// emit signal
|
|
|
|
itemPressed(currentItem());
|
|
|
|
} else
|
|
|
|
QListWidget::keyPressEvent(ev);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of anon
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
GuiCommandBuffer::GuiCommandBuffer(GuiViewBase * view)
|
2007-10-07 18:40:01 +00:00
|
|
|
: view_(view), lv_(*view), history_pos_(history_.end())
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-10-07 18:40:01 +00:00
|
|
|
transform(lyxaction.func_begin(), lyxaction.func_end(),
|
|
|
|
back_inserter(commands_), firster());
|
|
|
|
|
2007-08-30 20:09:12 +00:00
|
|
|
QPixmap qpup(toqstr(libFileSearch("images", "up", "png").absFilename()));
|
|
|
|
QPixmap qpdown(toqstr(libFileSearch("images", "down", "png").absFilename()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
QVBoxLayout * top = new QVBoxLayout(this);
|
|
|
|
QHBoxLayout * layout = new QHBoxLayout(0);
|
|
|
|
|
|
|
|
QPushButton * up = new QPushButton(qpup, "", this);
|
2007-03-29 22:12:34 +00:00
|
|
|
up->setMaximumSize(24, 24);
|
2006-03-05 17:24:44 +00:00
|
|
|
QPushButton * down = new QPushButton(qpdown, "", this);
|
2006-06-04 20:50:41 +00:00
|
|
|
down->setToolTip(qt_("Next command"));
|
2007-03-29 22:12:34 +00:00
|
|
|
down->setMaximumSize(24, 24);
|
2006-03-05 17:24:44 +00:00
|
|
|
connect(down, SIGNAL(clicked()), this, SLOT(down()));
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
edit_ = new GuiCommandEdit(this);
|
2006-03-05 17:24:44 +00:00
|
|
|
edit_->setMinimumSize(edit_->sizeHint());
|
|
|
|
edit_->setFocusPolicy(Qt::ClickFocus);
|
|
|
|
|
|
|
|
connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
|
|
|
|
connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
|
|
|
|
connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
|
|
|
|
connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
|
|
|
|
connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
|
2007-04-29 08:58:09 +00:00
|
|
|
connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
layout->addWidget(up, 0);
|
|
|
|
layout->addWidget(down, 0);
|
|
|
|
layout->addWidget(edit_, 10);
|
2006-10-22 14:37:32 +00:00
|
|
|
layout->setMargin(0);
|
2006-03-05 17:24:44 +00:00
|
|
|
top->addLayout(layout);
|
2006-10-22 14:37:32 +00:00
|
|
|
top->setMargin(0);
|
2007-08-24 07:13:07 +00:00
|
|
|
setFocusProxy(edit_);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::cancel()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-12-24 18:16:25 +00:00
|
|
|
view_->setFocus();
|
2006-10-22 14:37:32 +00:00
|
|
|
edit_->setText(QString());
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::dispatch()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-10-07 18:40:01 +00:00
|
|
|
dispatch(fromqstr(edit_->text()));
|
2006-12-23 17:09:50 +00:00
|
|
|
view_->setFocus();
|
2006-10-22 14:37:32 +00:00
|
|
|
edit_->setText(QString());
|
2006-03-05 17:24:44 +00:00
|
|
|
edit_->clearFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::complete()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
|
|
|
string const input = fromqstr(edit_->text());
|
|
|
|
string new_input;
|
2007-10-07 18:40:01 +00:00
|
|
|
vector<string> comp = completions(input, new_input);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (comp.empty() && new_input == input) {
|
2006-10-22 14:37:32 +00:00
|
|
|
// show_info_suffix(qt_("[no match]"), input);
|
2006-03-05 17:24:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp.empty()) {
|
|
|
|
edit_->setText(toqstr(new_input));
|
|
|
|
// show_info_suffix(("[only completion]"), new_input + ' ');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
edit_->setText(toqstr(new_input));
|
|
|
|
|
|
|
|
QTempListBox * list = new QTempListBox;
|
|
|
|
|
|
|
|
// For some reason the scrollview's contents are larger
|
|
|
|
// than the number of actual items...
|
|
|
|
vector<string>::const_iterator cit = comp.begin();
|
|
|
|
vector<string>::const_iterator end = comp.end();
|
2006-10-22 14:37:32 +00:00
|
|
|
for (; cit != end; ++cit)
|
2006-03-05 17:24:44 +00:00
|
|
|
list->addItem(toqstr(*cit));
|
|
|
|
|
|
|
|
list->resize(list->sizeHint());
|
2006-10-22 14:37:32 +00:00
|
|
|
QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-22 14:37:32 +00:00
|
|
|
int const y = std::max(0, pos.y() - list->height());
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
list->move(pos.x(), y);
|
|
|
|
|
|
|
|
connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
|
|
|
|
this, SLOT(complete_selected(QListWidgetItem *)));
|
2007-04-19 16:43:09 +00:00
|
|
|
connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
|
|
|
|
this, SLOT(complete_selected(QListWidgetItem *)));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
list->show();
|
|
|
|
list->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
|
|
|
QWidget const * widget = static_cast<QWidget const *>(sender());
|
|
|
|
const_cast<QWidget *>(widget)->hide();
|
|
|
|
edit_->setText(item->text() + ' ');
|
2007-06-21 06:38:50 +00:00
|
|
|
edit_->activateWindow();
|
2006-03-05 17:24:44 +00:00
|
|
|
edit_->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::up()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-22 14:37:32 +00:00
|
|
|
string const input = fromqstr(edit_->text());
|
2007-10-07 18:40:01 +00:00
|
|
|
string const h = historyUp();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (h.empty()) {
|
|
|
|
// show_info_suffix(qt_("[Beginning of history]"), input);
|
|
|
|
} else {
|
|
|
|
edit_->setText(toqstr(h));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::down()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-22 14:37:32 +00:00
|
|
|
string const input = fromqstr(edit_->text());
|
2007-10-07 18:40:01 +00:00
|
|
|
string const h = historyDown();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (h.empty()) {
|
|
|
|
// show_info_suffix(qt_("[End of history]"), input);
|
|
|
|
} else {
|
|
|
|
edit_->setText(toqstr(h));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::hideParent()
|
2007-04-29 08:58:09 +00:00
|
|
|
{
|
|
|
|
view_->setFocus();
|
|
|
|
edit_->setText(QString());
|
|
|
|
edit_->clearFocus();
|
2007-10-07 18:40:01 +00:00
|
|
|
hide();
|
2007-04-29 08:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 18:40:01 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class prefix_p {
|
|
|
|
public:
|
|
|
|
string p;
|
|
|
|
prefix_p(string const & s) : p(s) {}
|
|
|
|
bool operator()(string const & s) const { return prefixIs(s, p); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of anon namespace
|
|
|
|
|
|
|
|
|
|
|
|
string const GuiCommandBuffer::historyUp()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-10-07 18:40:01 +00:00
|
|
|
if (history_pos_ == history_.begin())
|
|
|
|
return string();
|
|
|
|
|
|
|
|
return *(--history_pos_);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 18:40:01 +00:00
|
|
|
string const GuiCommandBuffer::historyDown()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-10-07 18:40:01 +00:00
|
|
|
if (history_pos_ == history_.end())
|
|
|
|
return string();
|
|
|
|
if (history_pos_ + 1 == history_.end())
|
|
|
|
return string();
|
|
|
|
|
|
|
|
return *(++history_pos_);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2007-10-07 18:40:01 +00:00
|
|
|
|
|
|
|
docstring const GuiCommandBuffer::getCurrentState() const
|
|
|
|
{
|
|
|
|
return lv_.view()->cursor().currentState();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCommandBuffer::hide() const
|
|
|
|
{
|
|
|
|
lv_.showMiniBuffer(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<string> const
|
|
|
|
GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
|
|
|
|
{
|
|
|
|
vector<string> comp;
|
|
|
|
|
|
|
|
copy_if(commands_.begin(), commands_.end(),
|
|
|
|
back_inserter(comp), prefix_p(prefix));
|
|
|
|
|
|
|
|
if (comp.empty()) {
|
|
|
|
new_prefix = prefix;
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp.size() == 1) {
|
|
|
|
new_prefix = comp[0];
|
|
|
|
return vector<string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// find maximal available prefix
|
|
|
|
string const tmp = comp[0];
|
|
|
|
string test = prefix;
|
|
|
|
if (tmp.length() > test.length())
|
|
|
|
test += tmp[test.length()];
|
|
|
|
while (test.length() < tmp.length()) {
|
|
|
|
vector<string> vtmp;
|
|
|
|
copy_if(comp.begin(), comp.end(),
|
|
|
|
back_inserter(vtmp), prefix_p(test));
|
|
|
|
if (vtmp.size() != comp.size()) {
|
|
|
|
test.erase(test.length() - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
test += tmp[test.length()];
|
|
|
|
}
|
|
|
|
|
|
|
|
new_prefix = test;
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCommandBuffer::dispatch(string const & str)
|
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
history_.push_back(str);
|
|
|
|
history_pos_ = history_.end();
|
|
|
|
FuncRequest func = lyxaction.lookupFunc(str);
|
|
|
|
func.origin = FuncRequest::COMMANDBUFFER;
|
|
|
|
lv_.dispatch(func);
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiCommandBuffer_moc.cpp"
|