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
|
|
|
|
2009-04-03 17:00:09 +00:00
|
|
|
#include "GuiApplication.h"
|
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"
|
2010-02-09 16:11:13 +00:00
|
|
|
#include "LyX.h"
|
2007-10-07 18:40:01 +00:00
|
|
|
#include "LyXAction.h"
|
|
|
|
#include "FuncRequest.h"
|
2009-01-18 21:50:23 +00:00
|
|
|
#include "Session.h"
|
2007-10-07 18:40:01 +00:00
|
|
|
|
|
|
|
#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:
|
2009-01-18 21:50:23 +00:00
|
|
|
bool event(QEvent * ev) {
|
|
|
|
if (ev->type() == QEvent::MouseButtonPress) {
|
|
|
|
QMouseEvent * me = static_cast<QMouseEvent *>(ev);
|
|
|
|
if (me->x() < 0 || me->y() < 0
|
|
|
|
|| me->x() > width() || me->y() > height())
|
|
|
|
hide();
|
|
|
|
return true;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2009-01-18 21:50:23 +00:00
|
|
|
return QListWidget::event(ev);
|
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
|
2009-01-18 21:50:23 +00:00
|
|
|
itemClicked(currentItem());
|
2007-06-21 06:38:50 +00:00
|
|
|
} 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)
|
2009-01-18 21:50:23 +00:00
|
|
|
: view_(view)
|
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());
|
|
|
|
|
2015-03-12 23:47:21 +00:00
|
|
|
QPixmap qpup = getPixmap("images/", "up", "svgz,png");
|
|
|
|
QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
QVBoxLayout * top = new QVBoxLayout(this);
|
|
|
|
QHBoxLayout * layout = new QHBoxLayout(0);
|
|
|
|
|
2016-06-26 14:58:18 +00:00
|
|
|
edit_ = new GuiCommandEdit(this);
|
|
|
|
edit_->setMinimumSize(edit_->sizeHint());
|
|
|
|
edit_->setFocusPolicy(Qt::ClickFocus);
|
|
|
|
|
|
|
|
int height = max(24, 2 * (edit_->sizeHint().height() / 2));
|
|
|
|
QSize button_size = QSize(height, height);
|
|
|
|
QSize icon_size = button_size - QSize(4, 4);
|
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
upPB = new QPushButton(qpup, "", this);
|
|
|
|
upPB->setToolTip(qt_("List of previous commands"));
|
2016-06-26 14:58:18 +00:00
|
|
|
upPB->setMaximumSize(button_size);
|
|
|
|
upPB->setIconSize(icon_size);
|
2009-01-18 21:50:23 +00:00
|
|
|
downPB = new QPushButton(qpdown, "", this);
|
|
|
|
downPB->setToolTip(qt_("Next command"));
|
2016-06-26 14:58:18 +00:00
|
|
|
downPB->setMaximumSize(button_size);
|
|
|
|
downPB->setIconSize(icon_size);
|
2009-01-18 21:50:23 +00:00
|
|
|
downPB->setEnabled(false);
|
|
|
|
connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
|
|
|
|
connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
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()));
|
2015-04-18 17:10:33 +00:00
|
|
|
connect(edit_, SIGNAL(escapePressed()), this, SLOT(hideParent()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
layout->addWidget(upPB, 0);
|
|
|
|
layout->addWidget(downPB, 0);
|
2006-03-05 17:24:44 +00:00
|
|
|
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_);
|
2009-01-18 21:50:23 +00:00
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
LastCommandsSection::LastCommands last_commands
|
2009-01-18 21:50:23 +00:00
|
|
|
= theSession().lastCommands().getcommands();
|
2017-07-03 17:53:14 +00:00
|
|
|
LastCommandsSection::LastCommands::const_iterator it
|
2009-01-18 21:50:23 +00:00
|
|
|
= last_commands.begin();
|
2017-07-03 17:53:14 +00:00
|
|
|
LastCommandsSection::LastCommands::const_iterator end
|
2009-01-18 21:50:23 +00:00
|
|
|
= last_commands.end();
|
|
|
|
|
|
|
|
upPB->setEnabled(it != end);
|
|
|
|
|
|
|
|
for(; it != end; ++it)
|
|
|
|
history_.push_back(*it);
|
|
|
|
history_pos_ = history_.end();
|
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
|
|
|
{
|
2015-04-18 17:10:33 +00:00
|
|
|
std::string const cmd = fromqstr(edit_->text());
|
|
|
|
if (!cmd.empty())
|
|
|
|
theSession().lastCommands().add(cmd);
|
|
|
|
DispatchResult const & dr = dispatch(cmd);
|
|
|
|
if (!dr.error()) {
|
|
|
|
view_->setFocus();
|
|
|
|
edit_->setText(QString());
|
|
|
|
edit_->clearFocus();
|
|
|
|
// If the toolbar was "auto", it is not needed anymore
|
|
|
|
view_->resetCommandExecute();
|
|
|
|
}
|
2009-01-18 21:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCommandBuffer::listHistoryUp()
|
|
|
|
{
|
|
|
|
if (history_.size()==1) {
|
|
|
|
edit_->setText(toqstr(history_.back()));
|
|
|
|
upPB->setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
|
|
|
|
showList(history_, pos, true);
|
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;
|
2009-03-15 13:20:15 +00:00
|
|
|
vector<string> const & comp = completions(input, new_input);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (comp.empty()) {
|
2009-01-18 21:50:23 +00:00
|
|
|
if (new_input != input)
|
|
|
|
edit_->setText(toqstr(new_input));
|
2006-03-05 17:24:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
edit_->setText(toqstr(new_input));
|
2009-01-18 21:50:23 +00:00
|
|
|
QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
|
|
|
|
showList(comp, pos);
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
void GuiCommandBuffer::showList(vector<string> const & list,
|
|
|
|
QPoint const & pos, bool reversed) const
|
|
|
|
{
|
|
|
|
QTempListBox * listBox = new QTempListBox;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
// For some reason the scrollview's contents are larger
|
|
|
|
// than the number of actual items...
|
2009-01-18 21:50:23 +00:00
|
|
|
vector<string>::const_iterator cit = list.begin();
|
|
|
|
vector<string>::const_iterator end = list.end();
|
2009-03-15 13:20:15 +00:00
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
if (reversed)
|
|
|
|
listBox->insertItem(0, toqstr(*cit));
|
|
|
|
else
|
|
|
|
listBox->addItem(toqstr(*cit));
|
2009-01-18 21:50:23 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-03-15 13:20:15 +00:00
|
|
|
listBox->resize(listBox->sizeHint());
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
int const y = max(0, pos.y() - listBox->height());
|
|
|
|
listBox->move(pos.x(), y);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
|
2009-05-23 09:40:44 +00:00
|
|
|
this, SLOT(itemSelected(QListWidgetItem *)));
|
2009-01-18 21:50:23 +00:00
|
|
|
connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
|
2009-05-23 09:40:44 +00:00
|
|
|
this, SLOT(itemSelected(QListWidgetItem *)));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
listBox->show();
|
|
|
|
listBox->setFocus();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-23 09:40:44 +00:00
|
|
|
void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
|
|
|
QWidget const * widget = static_cast<QWidget const *>(sender());
|
|
|
|
const_cast<QWidget *>(widget)->hide();
|
2009-01-18 21:50:23 +00:00
|
|
|
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
|
|
|
{
|
2007-10-07 18:40:01 +00:00
|
|
|
string const h = historyUp();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
if (!h.empty())
|
2006-03-05 17:24:44 +00:00
|
|
|
edit_->setText(toqstr(h));
|
2009-01-18 21:50:23 +00:00
|
|
|
|
|
|
|
upPB->setEnabled(history_pos_ != history_.begin());
|
|
|
|
downPB->setEnabled(history_pos_ != history_.end());
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::down()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-10-07 18:40:01 +00:00
|
|
|
string const h = historyDown();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
if (!h.empty())
|
2006-03-05 17:24:44 +00:00
|
|
|
edit_->setText(toqstr(h));
|
|
|
|
|
2009-06-15 09:08:18 +00:00
|
|
|
downPB->setEnabled(!history_.empty()
|
|
|
|
&& history_pos_ != history_.end() - 1);
|
2009-01-18 21:50:23 +00:00
|
|
|
upPB->setEnabled(history_pos_ != history_.begin());
|
|
|
|
}
|
2017-07-03 17:53:14 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
void GuiCommandBuffer::hideParent()
|
2007-04-29 08:58:09 +00:00
|
|
|
{
|
|
|
|
view_->setFocus();
|
2015-04-18 17:10:33 +00:00
|
|
|
view_->resetCommandExecute();
|
2007-04-29 08:58:09 +00:00
|
|
|
edit_->setText(QString());
|
|
|
|
edit_->clearFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2009-09-05 14:29:11 +00:00
|
|
|
return view_->currentBufferView()->cursor().currentState();
|
2007-10-07 18:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<string> const
|
|
|
|
GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
|
|
|
|
{
|
|
|
|
vector<string> comp;
|
|
|
|
|
2010-04-22 11:45:34 +00:00
|
|
|
lyx::copy_if(commands_.begin(), commands_.end(),
|
2007-10-07 18:40:01 +00:00
|
|
|
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;
|
2010-04-22 11:45:34 +00:00
|
|
|
lyx::copy_if(comp.begin(), comp.end(),
|
2007-10-07 18:40:01 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-18 17:10:33 +00:00
|
|
|
DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
|
2007-10-07 18:40:01 +00:00
|
|
|
{
|
2015-04-18 17:10:33 +00:00
|
|
|
if (str.empty()) {
|
|
|
|
static DispatchResult empty_dr;
|
|
|
|
return empty_dr;
|
|
|
|
}
|
2007-10-07 18:40:01 +00:00
|
|
|
|
2009-01-18 21:50:23 +00:00
|
|
|
history_.push_back(trim(str));
|
2007-10-07 18:40:01 +00:00
|
|
|
history_pos_ = history_.end();
|
2009-01-18 21:50:23 +00:00
|
|
|
upPB->setEnabled(history_pos_ != history_.begin());
|
|
|
|
downPB->setEnabled(history_pos_ != history_.end());
|
2007-10-07 18:40:01 +00:00
|
|
|
FuncRequest func = lyxaction.lookupFunc(str);
|
2010-04-09 19:00:42 +00:00
|
|
|
func.setOrigin(FuncRequest::COMMANDBUFFER);
|
2015-04-18 17:10:33 +00:00
|
|
|
return 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"
|