diff --git a/src/frontends/qt2/QCommandBuffer.C b/src/frontends/qt2/QCommandBuffer.C new file mode 100644 index 0000000000..173ddbb2bb --- /dev/null +++ b/src/frontends/qt2/QCommandBuffer.C @@ -0,0 +1,181 @@ +/** + * \file QCommandBuffer.C + * Copyright 2002 the LyX Team + * Read the file COPYING + * + * \author John Levon + */ + +#include + +#include "support/filetools.h" +#include "controllers/ControlCommandBuffer.h" +#include "gettext.h" + +#include "QtView.h" +#include "QCommandBuffer.h" +#include "QCommandEdit.h" + +#include +#include +#include + +namespace { + +class QTempComboBox : public QComboBox { +public: + QTempComboBox(QWidget * parent) : QComboBox(parent) { + setWFlags(WDestructiveClose); + } + + void popup() { QComboBox::popup(); } +}; + +}; + +QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control) + : QToolBar(view), view_(view), controller_(control) +{ + setHorizontalStretchable(true); + + QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str()); + + QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this); + upb->setMinimumSize(upb->sizeHint()); + upb->show(); + + QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this); + downb->setMinimumSize(downb->sizeHint()); + downb->show(); + + edit_ = new QCommandEdit(this); + edit_->setMinimumSize(edit_->sizeHint()); + edit_->show(); + setStretchableWidget(edit_); + + setMinimumSize(sizeHint()); + show(); + + connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel())); + connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch())); + connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete())); + connect(edit_, SIGNAL(upPressed()), this, SLOT(up())); + connect(edit_, SIGNAL(downPressed()), this, SLOT(down())); +} + + + +void QCommandBuffer::focus_command() +{ + edit_->setFocus(); +} + + +void QCommandBuffer::cancel() +{ + view_->centralWidget()->setFocus(); + edit_->setText(""); +} + + +void QCommandBuffer::dispatch() +{ + controller_.dispatch(edit_->text().latin1()); + view_->centralWidget()->setFocus(); + edit_->setText(""); +} + + +void QCommandBuffer::complete() +{ + string const input = edit_->text().latin1(); + string new_input; + vector comp = controller_.completions(input, new_input); + + if (comp.empty() && new_input == input) { + // show_info_suffix(_("[no match]"), input); + return; + } + + if (comp.empty()) { + edit_->setText(new_input.c_str()); + // show_info_suffix(("[only completion]"), new_input + " "); + return; + } + + edit_->setText(new_input.c_str()); + + QTempComboBox * combo = new QTempComboBox(view_); + combo->move(edit_->x() + x(), edit_->y() + y()); + + vector::const_iterator cit = comp.begin(); + vector::const_iterator end = comp.end(); + for (; cit != end; ++cit) { + combo->insertItem(cit->c_str()); + } + + combo->setMinimumWidth(combo->sizeHint().width()); + combo->resize(combo->width(), edit_->height()); + + connect(combo, SIGNAL(activated(const QString &)), + this, SLOT(complete_selected(const QString &))); + + combo->show(); + combo->setFocus(); + combo->popup(); +} + + +void QCommandBuffer::complete_selected(const QString & str) +{ + edit_->setText(str + " "); + // FIXME + QWidget const * widget = static_cast(sender()); + edit_->setFocus(); + const_cast(widget)->hide(); +} + + +void QCommandBuffer::up() +{ + string const input = edit_->text().latin1(); + string const h(controller_.historyUp()); + + if (h.empty()) { + // show_info_suffix(_("[Beginning of history]"), input); + } else { + edit_->setText(h.c_str()); + } +} + + +void QCommandBuffer::down() +{ + string const input = edit_->text().latin1(); + string const h(controller_.historyDown()); + + if (h.empty()) { + // show_info_suffix(_("[End of history]"), input); + } else { + edit_->setText(h.c_str()); + } +} + + +#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 diff --git a/src/frontends/qt2/QCommandBuffer.h b/src/frontends/qt2/QCommandBuffer.h new file mode 100644 index 0000000000..b647c5b107 --- /dev/null +++ b/src/frontends/qt2/QCommandBuffer.h @@ -0,0 +1,57 @@ +/** + * \file QCommandBuffer.h + * Copyright 2002 the LyX Team + * Read the file COPYING + * + * \author John Levon + */ + +#ifndef QCOMMANDBUFFER_H +#define QCOMMANDBUFFER_H + +#include +#include "LString.h" + +#include +#include + +class QtView; +class QCommandEdit; +class ControlCommandBuffer; + +class QCommandBuffer : public QToolBar { + Q_OBJECT +public: + + QCommandBuffer(QtView * view, ControlCommandBuffer & control); + + /// focus the edit widget + void focus_command(); + +public slots: + /// cancel command compose + void cancel(); + /// dispatch a command + void dispatch(); + /// tab-complete + void complete(); + /// select-complete + void complete_selected(const QString & str); + /// up + void up(); + /// down + void down(); + +private: + /// owning view + QtView * view_; + + /// controller + ControlCommandBuffer & controller_; + + /// command widget + QCommandEdit * edit_; + +}; + +#endif // QCOMMANDBUFFER_H diff --git a/src/frontends/qt2/QCommandEdit.C b/src/frontends/qt2/QCommandEdit.C new file mode 100644 index 0000000000..d3920c3a47 --- /dev/null +++ b/src/frontends/qt2/QCommandEdit.C @@ -0,0 +1,43 @@ +/** + * \file QCommandEdit.C + * Copyright 2002 the LyX Team + * Read the file COPYING + * + * \author John Levon + */ + +#include "QCommandEdit.h" + +QCommandEdit::QCommandEdit(QWidget * parent) + : QLineEdit(parent) +{ +} + + +void QCommandEdit::keyPressEvent(QKeyEvent * e) +{ + switch (e->key()) { + case Key_Escape: + emit escapePressed(); + break; + + case Key_Up: + emit upPressed(); + break; + + case Key_Down: + emit downPressed(); + break; + + case Key_Right: + if (e->state() & ControlButton) + emit rightPressed(); + else + QLineEdit::keyPressEvent(e); + break; + + default: + QLineEdit::keyPressEvent(e); + break; + } +} diff --git a/src/frontends/qt2/QCommandEdit.h b/src/frontends/qt2/QCommandEdit.h new file mode 100644 index 0000000000..6ec68ed3ff --- /dev/null +++ b/src/frontends/qt2/QCommandEdit.h @@ -0,0 +1,37 @@ +/** + * \file QCommandEdit.h + * Copyright 2002 the LyX Team + * Read the file COPYING + * + * \author John Levon + */ + +#ifndef QCOMMANDEDIT_H +#define QCOMMANDEDIT_H + +#include + +class QCommandEdit : public QLineEdit { + Q_OBJECT +public: + + QCommandEdit(QWidget * parent); + +signals: + /// cancel + void escapePressed(); + + /// up history + void upPressed(); + + /// down history + void downPressed(); + + /// complete + void rightPressed(); + +protected: + virtual void keyPressEvent(QKeyEvent * e); +}; + +#endif // QCOMMANDEDIT_H