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>
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiCommandBuffer.h"
|
2007-11-13 14:04:32 +00:00
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiCommandEdit.h"
|
2007-11-13 14:04:32 +00:00
|
|
|
#include "GuiView.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 "support/lyxalgo.h"
|
|
|
|
#include "support/lstrings.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-22 14:37:32 +00:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#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-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
|
|
|
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-11-05 13:52:37 +00:00
|
|
|
GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
|
2007-11-13 14:04:32 +00:00
|
|
|
: view_(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-10-17 18:28:45 +00:00
|
|
|
QPixmap qpup(":/images/up.png");
|
|
|
|
QPixmap qpdown(":/images/down.png");
|
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
|
|
|
{
|
2008-02-21 19:42:34 +00:00
|
|
|
QString cmd = 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();
|
2008-02-21 19:42:34 +00:00
|
|
|
dispatch(fromqstr(cmd));
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
int const y = 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
|
|
|
|
{
|
2007-11-13 14:04:32 +00:00
|
|
|
return view_->view()->cursor().currentState();
|
2007-10-07 18:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCommandBuffer::hide() const
|
|
|
|
{
|
2007-11-14 11:14:06 +00:00
|
|
|
FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
|
2007-11-26 22:45:17 +00:00
|
|
|
theLyXFunc().setLyXView(view_);
|
|
|
|
lyx::dispatch(cmd);
|
2007-10-07 18:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2007-11-26 22:45:17 +00:00
|
|
|
theLyXFunc().setLyXView(view_);
|
|
|
|
lyx::dispatch(func);
|
2007-10-07 18:40:01 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2008-11-14 14:28:50 +00:00
|
|
|
#include "moc_GuiCommandBuffer.cpp"
|