2002-07-19 23:05:24 +00:00
|
|
|
/**
|
|
|
|
* \file QCommandBuffer.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-07-19 23:05:24 +00:00
|
|
|
*
|
2002-09-24 13:57:09 +00:00
|
|
|
* \author John Levon
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-07-19 23:05:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "controllers/ControlCommandBuffer.h"
|
2002-12-17 20:37:13 +00:00
|
|
|
#include "qt_helpers.h"
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
#include "QtView.h"
|
|
|
|
#include "QCommandBuffer.h"
|
|
|
|
#include "QCommandEdit.h"
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
#include <qlistbox.h>
|
2003-04-15 01:06:13 +00:00
|
|
|
#include <qlayout.h>
|
|
|
|
#include <qtooltip.h>
|
|
|
|
#include <qpushbutton.h>
|
2002-07-24 07:35:59 +00:00
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
using lyx::support::LibFileSearch;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2002-07-24 07:35:59 +00:00
|
|
|
using std::vector;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2002-07-24 07:35:59 +00:00
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
namespace {
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
class QTempListBox : public QListBox {
|
2002-07-19 23:05:24 +00:00
|
|
|
public:
|
2002-08-29 04:39:43 +00:00
|
|
|
QTempListBox()
|
|
|
|
: QListBox(0, 0,
|
2002-10-20 01:48:28 +00:00
|
|
|
WType_Modal | WType_Popup | WDestructiveClose) {
|
2002-08-29 04:39:43 +00:00
|
|
|
setHScrollBarMode(AlwaysOff);
|
|
|
|
}
|
|
|
|
protected:
|
2002-09-12 01:41:16 +00:00
|
|
|
void mouseReleaseEvent(QMouseEvent * e) {
|
|
|
|
if (e->x() < 0 || e->y() < 0
|
2002-10-20 01:48:28 +00:00
|
|
|
|| e->x() > width() || e->y() > height()) {
|
2002-09-12 01:41:16 +00:00
|
|
|
hide();
|
|
|
|
} else {
|
|
|
|
emit selected(currentText());
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
void keyPressEvent(QKeyEvent * e) {
|
|
|
|
if (e->key() == Key_Escape) {
|
|
|
|
hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QListBox::keyPressEvent(e);
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
} // end of anon
|
|
|
|
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2003-04-15 01:06:13 +00:00
|
|
|
QCommandBuffer::QCommandBuffer(QtView * view, QWidget * parent, ControlCommandBuffer & control)
|
|
|
|
: QWidget(parent), view_(view), controller_(control)
|
2002-07-19 23:05:24 +00:00
|
|
|
{
|
2002-12-17 20:37:13 +00:00
|
|
|
QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
|
|
|
|
QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
|
2002-07-19 23:05:24 +00:00
|
|
|
|
2003-04-15 01:06:13 +00:00
|
|
|
QVBoxLayout * top = new QVBoxLayout(this);
|
|
|
|
QHBoxLayout * layout = new QHBoxLayout(0);
|
|
|
|
|
|
|
|
QPushButton * up = new QPushButton(qpup, "", this);
|
|
|
|
QToolTip::add(up, qt_("Previous command"));
|
|
|
|
connect(up, SIGNAL(clicked()), this, SLOT(up()));
|
|
|
|
QPushButton * down = new QPushButton(qpdown, "", this);
|
|
|
|
QToolTip::add(down, qt_("Next command"));
|
|
|
|
connect(down, SIGNAL(clicked()), this, SLOT(down()));
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
edit_ = new QCommandEdit(this);
|
|
|
|
edit_->setMinimumSize(edit_->sizeHint());
|
2003-04-05 21:10:34 +00:00
|
|
|
edit_->setFocusPolicy(ClickFocus);
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
|
|
|
|
connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
|
2003-04-05 21:10:34 +00:00
|
|
|
connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
|
2002-07-19 23:05:24 +00:00
|
|
|
connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
|
|
|
|
connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
|
2003-04-15 01:06:13 +00:00
|
|
|
|
|
|
|
layout->addWidget(up, 0);
|
|
|
|
layout->addWidget(down, 0);
|
|
|
|
layout->addWidget(edit_, 10);
|
|
|
|
top->addLayout(layout);
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::focus_command()
|
|
|
|
{
|
|
|
|
edit_->setFocus();
|
|
|
|
}
|
|
|
|
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
void QCommandBuffer::cancel()
|
|
|
|
{
|
|
|
|
view_->centralWidget()->setFocus();
|
|
|
|
edit_->setText("");
|
|
|
|
}
|
|
|
|
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
void QCommandBuffer::dispatch()
|
|
|
|
{
|
2002-12-17 20:37:13 +00:00
|
|
|
controller_.dispatch(fromqstr(edit_->text()));
|
2002-07-19 23:05:24 +00:00
|
|
|
view_->centralWidget()->setFocus();
|
|
|
|
edit_->setText("");
|
2003-04-05 21:10:34 +00:00
|
|
|
edit_->clearFocus();
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::complete()
|
|
|
|
{
|
2002-12-17 20:37:13 +00:00
|
|
|
string const input = fromqstr(edit_->text());
|
2002-07-19 23:05:24 +00:00
|
|
|
string new_input;
|
|
|
|
vector<string> comp = controller_.completions(input, new_input);
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
if (comp.empty() && new_input == input) {
|
2002-12-17 20:37:13 +00:00
|
|
|
// show_info_suffix(qt_("[no match]"), input);
|
2002-07-19 23:05:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp.empty()) {
|
2002-12-17 20:37:13 +00:00
|
|
|
edit_->setText(toqstr(new_input));
|
2002-11-27 10:30:28 +00:00
|
|
|
// show_info_suffix(("[only completion]"), new_input + ' ');
|
2002-07-19 23:05:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-12-17 20:37:13 +00:00
|
|
|
edit_->setText(toqstr(new_input));
|
2002-07-19 23:05:24 +00:00
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
QTempListBox * list = new QTempListBox;
|
2002-09-12 01:41:16 +00:00
|
|
|
|
|
|
|
// For some reason the scrollview's contents are larger
|
2002-09-24 13:57:09 +00:00
|
|
|
// than the number of actual items...
|
2002-07-19 23:05:24 +00:00
|
|
|
vector<string>::const_iterator cit = comp.begin();
|
|
|
|
vector<string>::const_iterator end = comp.end();
|
|
|
|
for (; cit != end; ++cit) {
|
2002-12-17 20:37:13 +00:00
|
|
|
list->insertItem(toqstr(*cit));
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
// width() is not big enough by a few pixels. Qt Sucks.
|
|
|
|
list->setMinimumWidth(list->sizeHint().width() + 10);
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
list->resize(list->sizeHint());
|
2002-09-24 13:57:09 +00:00
|
|
|
QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
int y = std::max(0, pos.y() - list->height());
|
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
list->move(pos.x(), y);
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
connect(list, SIGNAL(selected(const QString &)),
|
2002-09-24 13:57:09 +00:00
|
|
|
this, SLOT(complete_selected(const QString &)));
|
|
|
|
|
2002-08-29 04:39:43 +00:00
|
|
|
list->show();
|
|
|
|
list->setFocus();
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
void QCommandBuffer::complete_selected(QString const & str)
|
2002-07-19 23:05:24 +00:00
|
|
|
{
|
|
|
|
QWidget const * widget = static_cast<QWidget const *>(sender());
|
|
|
|
const_cast<QWidget *>(widget)->hide();
|
2003-04-05 21:10:34 +00:00
|
|
|
edit_->setText(str + ' ');
|
|
|
|
edit_->setFocus();
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
void QCommandBuffer::up()
|
|
|
|
{
|
2002-12-17 20:37:13 +00:00
|
|
|
string const input(fromqstr(edit_->text()));
|
2002-07-19 23:05:24 +00:00
|
|
|
string const h(controller_.historyUp());
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
if (h.empty()) {
|
2002-12-17 20:37:13 +00:00
|
|
|
// show_info_suffix(qt_("[Beginning of history]"), input);
|
2002-07-19 23:05:24 +00:00
|
|
|
} else {
|
2002-12-17 20:37:13 +00:00
|
|
|
edit_->setText(toqstr(h));
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCommandBuffer::down()
|
|
|
|
{
|
2002-12-17 20:37:13 +00:00
|
|
|
string const input(fromqstr(edit_->text()));
|
2002-07-19 23:05:24 +00:00
|
|
|
string const h(controller_.historyDown());
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
if (h.empty()) {
|
2002-12-17 20:37:13 +00:00
|
|
|
// show_info_suffix(qt_("[End of history]"), input);
|
2002-07-19 23:05:24 +00:00
|
|
|
} else {
|
2002-12-17 20:37:13 +00:00
|
|
|
edit_->setText(toqstr(h));
|
2002-07-19 23:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 13:57:09 +00:00
|
|
|
#if 0
|
2002-07-19 23:05:24 +00:00
|
|
|
void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
|
|
|
|
{
|
|
|
|
stored_input_ = input;
|
|
|
|
info_suffix_shown_ = true;
|
2002-11-27 10:30:28 +00:00
|
|
|
set_input(input + ' ' + suffix);
|
2002-07-19 23:05:24 +00:00
|
|
|
suffix_timer_->start();
|
|
|
|
}
|
2002-09-24 13:57:09 +00:00
|
|
|
|
2002-07-19 23:05:24 +00:00
|
|
|
|
|
|
|
void XMiniBuffer::suffix_timeout()
|
|
|
|
{
|
|
|
|
info_suffix_shown_ = false;
|
|
|
|
set_input(stored_input_);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|