2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file QCommandBuffer.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.
|
|
|
|
*
|
|
|
|
* \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
|
|
|
|
|
|
|
#include "QCommandBuffer.h"
|
|
|
|
#include "QCommandEdit.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
2006-04-09 08:42:58 +00:00
|
|
|
using lyx::support::libFileSearch;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
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-08-23 13:20:01 +00:00
|
|
|
QCommandBuffer::QCommandBuffer(GuiView * view)
|
|
|
|
: view_(view), controller_(*view)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm").absFilename()));
|
|
|
|
QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm").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-06-04 20:50:41 +00:00
|
|
|
up->setToolTip(qt_("Previous command"));
|
2006-03-05 17:24:44 +00:00
|
|
|
connect(up, SIGNAL(clicked()), this, SLOT(up()));
|
|
|
|
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()));
|
|
|
|
|
|
|
|
edit_ = new QCommandEdit(this);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::cancel()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::dispatch()
|
|
|
|
{
|
|
|
|
controller_.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::complete()
|
|
|
|
{
|
|
|
|
string const input = fromqstr(edit_->text());
|
|
|
|
string new_input;
|
|
|
|
vector<string> comp = controller_.completions(input, new_input);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::complete_selected(QListWidgetItem * item)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::up()
|
|
|
|
{
|
2006-10-22 14:37:32 +00:00
|
|
|
string const input = fromqstr(edit_->text());
|
|
|
|
string const h = controller_.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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::down()
|
|
|
|
{
|
2006-10-22 14:37:32 +00:00
|
|
|
string const input = fromqstr(edit_->text());
|
|
|
|
string const h = controller_.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-04-29 08:58:09 +00:00
|
|
|
void QCommandBuffer::hideParent()
|
|
|
|
{
|
|
|
|
view_->setFocus();
|
|
|
|
edit_->setText(QString());
|
|
|
|
edit_->clearFocus();
|
|
|
|
controller_.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#if 0
|
|
|
|
void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
|
|
|
|
{
|
|
|
|
stored_input_ = input;
|
|
|
|
info_suffix_shown_ = true;
|
|
|
|
set_input(input + ' ' + suffix);
|
|
|
|
suffix_timer_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XMiniBuffer::suffix_timeout()
|
|
|
|
{
|
|
|
|
info_suffix_shown_ = false;
|
|
|
|
set_input(stored_input_);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
|
|
|
#include "QCommandBuffer_moc.cpp"
|