2012-12-24 11:30:03 +00:00
|
|
|
/* This file is (c) 2012 Tvangeste <i.4m.l33t@yandex.ru>
|
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "translatebox.hh"
|
|
|
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QModelIndex>
|
2013-01-27 22:12:00 +00:00
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QStyle>
|
2012-12-24 11:30:03 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
#define MAX_POPUP_ROWS 17
|
|
|
|
}
|
|
|
|
|
2013-01-28 12:18:13 +00:00
|
|
|
CompletionList::CompletionList(TranslateBox * parent) : WordList(parent),
|
|
|
|
translateBox(parent)
|
2012-12-24 11:30:03 +00:00
|
|
|
{
|
2013-01-28 13:41:30 +00:00
|
|
|
#ifdef Q_WS_WIN
|
2012-12-24 11:30:03 +00:00
|
|
|
setWindowFlags(Qt::ToolTip);
|
2013-01-28 13:41:30 +00:00
|
|
|
#else
|
|
|
|
setParent( parent->window() );
|
|
|
|
setAutoFillBackground( true );
|
|
|
|
#endif
|
|
|
|
|
2012-12-24 11:30:03 +00:00
|
|
|
setMaximumWidth(1000);
|
|
|
|
|
|
|
|
connect(this, SIGNAL( activated( QModelIndex ) ),
|
|
|
|
this, SLOT( acceptCurrentEntry() ) );
|
|
|
|
|
|
|
|
connect(this, SIGNAL( itemClicked( QListWidgetItem * ) ),
|
|
|
|
this, SLOT( acceptCurrentEntry() ) );
|
2013-01-25 15:42:44 +00:00
|
|
|
|
2013-01-28 12:18:13 +00:00
|
|
|
translateBox->window()->installEventFilter(this);
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CompletionList::eventFilter( QObject * obj, QEvent * ev )
|
|
|
|
{
|
|
|
|
// when the main window is moved or resized, hide the word list suggestions
|
|
|
|
if ( ev->type() == QEvent::Move || ev->type() == QEvent::Resize )
|
|
|
|
{
|
2013-01-28 12:18:13 +00:00
|
|
|
translateBox->setPopupEnabled( false );
|
2013-01-25 15:42:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QWidget::eventFilter( obj, ev );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CompletionList::preferredHeight() const
|
|
|
|
{
|
|
|
|
const QSize itemSizeHint = itemDelegate()->sizeHint(viewOptions(), model()->index( 0, 0 ) );
|
2013-01-27 22:12:00 +00:00
|
|
|
int rows = qMin( count(), MAX_POPUP_ROWS );
|
|
|
|
|
|
|
|
int scrollBarHeight = 0;
|
|
|
|
|
|
|
|
bool hBarIsVisible = horizontalScrollBar()->maximum() > 0;
|
|
|
|
|
|
|
|
if ( hBarIsVisible )
|
|
|
|
scrollBarHeight += QApplication::style()->pixelMetric( QStyle::PM_ScrollBarExtent );
|
|
|
|
|
|
|
|
return rows == 0 ? 0 : itemSizeHint.height() * rows + frameWidth() * 2 + scrollBarHeight;
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CompletionList::acceptCurrentEntry()
|
|
|
|
{
|
|
|
|
if (!isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QModelIndex index = currentIndex();
|
|
|
|
if ( !index.isValid() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit doubleClicked(index);
|
2013-01-28 12:18:13 +00:00
|
|
|
translateBox->setPopupEnabled( false );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TranslateBox::TranslateBox(QWidget *parent) : QWidget(parent),
|
2013-01-27 22:12:00 +00:00
|
|
|
word_list(new CompletionList(this)), m_popupEnabled(false)
|
2012-12-24 11:30:03 +00:00
|
|
|
{
|
|
|
|
// initially hidden
|
|
|
|
word_list->hide();
|
|
|
|
|
|
|
|
resize(200, 90);
|
|
|
|
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
|
|
|
|
sizePolicy.setHorizontalStretch(0);
|
|
|
|
sizePolicy.setVerticalStretch(0);
|
|
|
|
setSizePolicy(sizePolicy);
|
|
|
|
// setMinimumSize(QSize(800, 0));
|
|
|
|
|
|
|
|
translate_line = new ExtLineEdit(this);
|
|
|
|
setFocusProxy(translate_line);
|
|
|
|
translate_line->setObjectName("translateLine");
|
|
|
|
#if QT_VERSION >= 0x040700
|
|
|
|
translate_line->setPlaceholderText( tr( "Type a word or phrase to search dictionaries" ) );
|
|
|
|
#endif
|
|
|
|
|
2013-01-25 15:42:44 +00:00
|
|
|
word_list->setTranslateLine(translate_line);
|
|
|
|
|
2012-12-24 11:30:03 +00:00
|
|
|
// completer = new QCompleter(m_completionList->model(), this);
|
|
|
|
// completer->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
// completer->setCompletionMode(QCompleter::InlineCompletion);
|
|
|
|
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout(this);
|
|
|
|
setLayout(layout);
|
|
|
|
layout->setMargin(0);
|
|
|
|
layout->addWidget(translate_line);
|
|
|
|
|
|
|
|
QPixmap image(":/icons/system-search.png");
|
|
|
|
translate_line->setButtonPixmap(ExtLineEdit::Left, image.scaled(18, 18, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
2012-12-25 20:44:26 +00:00
|
|
|
// translate_line->setButtonToolTip(ExtLineEdit::Left, tr("Options"));
|
2012-12-24 11:30:03 +00:00
|
|
|
translate_line->setButtonVisible(ExtLineEdit::Left, true);
|
|
|
|
translate_line->setButtonFocusPolicy(ExtLineEdit::Left, Qt::ClickFocus);
|
|
|
|
|
|
|
|
QPixmap right(":/icons/1downarrow.png");
|
|
|
|
translate_line->setButtonPixmap(ExtLineEdit::Right, right);
|
|
|
|
translate_line->setButtonToolTip(ExtLineEdit::Right, tr("Drop-down"));
|
|
|
|
translate_line->setButtonVisible(ExtLineEdit::Right, true);
|
|
|
|
translate_line->setButtonFocusPolicy(ExtLineEdit::Right, Qt::NoFocus);
|
|
|
|
|
|
|
|
translate_line->setFocusPolicy(Qt::ClickFocus);
|
|
|
|
|
|
|
|
translate_line->installEventFilter( this );
|
|
|
|
this->installEventFilter( this );
|
|
|
|
|
|
|
|
connect(translate_line, SIGNAL( textChanged( QString const & ) ),
|
2013-01-27 22:12:00 +00:00
|
|
|
this, SLOT( onTextEdit() ) );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
|
|
|
connect(translate_line, SIGNAL( rightButtonClicked() ),
|
|
|
|
this, SLOT( rightButtonClicked() ) );
|
2013-01-27 22:12:00 +00:00
|
|
|
|
|
|
|
connect(word_list, SIGNAL( contentChanged() ),
|
|
|
|
this, SLOT( showPopup() ) );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TranslateBox::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
{
|
2012-12-25 12:46:38 +00:00
|
|
|
// hide the suggestions list when the window is not active
|
|
|
|
if ( event->type() == QEvent::WindowDeactivate )
|
|
|
|
{
|
2012-12-25 19:46:26 +00:00
|
|
|
if (!word_list->isActiveWindow())
|
|
|
|
{
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( false );
|
2012-12-25 19:46:26 +00:00
|
|
|
}
|
2012-12-25 12:46:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-24 11:30:03 +00:00
|
|
|
if (obj == translate_line && event->type() == QEvent::KeyPress) {
|
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
|
|
|
|
switch (keyEvent->key()) {
|
|
|
|
case Qt::Key_Up:
|
|
|
|
case Qt::Key_Down:
|
|
|
|
case Qt::Key_PageUp:
|
|
|
|
case Qt::Key_PageDown:
|
|
|
|
if ( !word_list->isVisible() )
|
|
|
|
{
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( true );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QApplication::sendEvent(word_list, event);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
|
|
|
return word_list->acceptCurrentEntry();
|
|
|
|
case Qt::Key_Escape:
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( false );
|
2012-12-24 11:30:03 +00:00
|
|
|
return true;
|
|
|
|
case Qt::Key_Tab:
|
|
|
|
if ( !word_list->isVisible() )
|
|
|
|
{
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( true );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QKeyEvent event( QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier );
|
|
|
|
QApplication::sendEvent( word_list, &event );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case Qt::Key_Backtab:
|
|
|
|
if ( !word_list->isVisible() )
|
|
|
|
{
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( true );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QKeyEvent event( QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier );
|
|
|
|
QApplication::sendEvent( word_list, &event );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (obj == translate_line && event->type() == QEvent::FocusOut) {
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
QFocusEvent *fev = static_cast<QFocusEvent*>(event);
|
|
|
|
if (fev->reason() != Qt::ActiveWindowFocusReason ||
|
|
|
|
(fev->reason() == Qt::ActiveWindowFocusReason && !word_list->isActiveWindow()))
|
|
|
|
#endif
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( false );
|
2012-12-24 11:30:03 +00:00
|
|
|
} else if (obj == translate_line && event->type() == QEvent::FocusIn) {
|
|
|
|
// By default, focusing the traslate line does not show
|
|
|
|
// the popup window.
|
|
|
|
} else if (obj == this && event->type() == QEvent::ShortcutOverride) {
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
|
2013-01-02 09:55:23 +00:00
|
|
|
if (ke->key() == Qt::Key_Escape && !ke->modifiers() && word_list->isVisible() ) {
|
2012-12-24 11:30:03 +00:00
|
|
|
event->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
|
2013-01-27 22:12:00 +00:00
|
|
|
void TranslateBox::setText( QString text, bool showPopup )
|
|
|
|
{
|
|
|
|
setPopupEnabled( showPopup );
|
|
|
|
translate_line->setText( text );
|
|
|
|
}
|
|
|
|
|
2013-01-17 09:08:53 +00:00
|
|
|
void TranslateBox::setPopupEnabled( bool enable )
|
|
|
|
{
|
|
|
|
m_popupEnabled = enable;
|
2013-01-27 22:12:00 +00:00
|
|
|
showPopup();
|
2013-01-17 09:08:53 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 11:30:03 +00:00
|
|
|
void TranslateBox::showPopup()
|
|
|
|
{
|
|
|
|
// completer->setCompletionPrefix( m_fileLineEdit->text() );
|
|
|
|
// qDebug() << "COMPLETION:" << completer->currentCompletion();
|
|
|
|
|
2013-01-27 22:12:00 +00:00
|
|
|
if (translate_line->text().trimmed().isEmpty() || word_list->count() == 0)
|
2012-12-24 11:30:03 +00:00
|
|
|
{
|
|
|
|
// nothing to show
|
|
|
|
if (word_list->isVisible())
|
2013-01-11 11:47:22 +00:00
|
|
|
{
|
2013-01-25 15:42:44 +00:00
|
|
|
word_list->hide();
|
2013-01-11 11:47:22 +00:00
|
|
|
translate_line->setFocus();
|
|
|
|
}
|
2012-12-24 11:30:03 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 09:08:53 +00:00
|
|
|
if ( !m_popupEnabled )
|
|
|
|
{
|
|
|
|
word_list->hide();
|
|
|
|
return;
|
|
|
|
}
|
2012-12-25 19:46:26 +00:00
|
|
|
|
2013-02-01 10:15:20 +00:00
|
|
|
int preferredHeight = word_list->preferredHeight();
|
|
|
|
|
2013-01-28 13:41:30 +00:00
|
|
|
QPoint origin( 0, translate_line->y() + translate_line->height() );
|
|
|
|
if ( word_list->isWindow() )
|
2013-02-01 10:15:20 +00:00
|
|
|
{
|
2013-01-28 13:41:30 +00:00
|
|
|
origin = mapToGlobal( origin );
|
2013-02-01 10:15:20 +00:00
|
|
|
}
|
2013-01-28 13:41:30 +00:00
|
|
|
else
|
2013-02-01 10:15:20 +00:00
|
|
|
{
|
2013-01-28 13:41:30 +00:00
|
|
|
origin = mapToParent( origin );
|
2013-02-01 10:15:20 +00:00
|
|
|
preferredHeight = qMin( translate_line->window()->height() - origin.y(), preferredHeight );
|
|
|
|
}
|
2012-12-24 11:30:03 +00:00
|
|
|
|
2013-02-01 10:15:20 +00:00
|
|
|
const QSize size(width(), preferredHeight);
|
2013-01-28 13:41:30 +00:00
|
|
|
const QRect rect( origin, size );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
|
|
|
word_list->setGeometry(rect);
|
|
|
|
word_list->show();
|
2012-12-25 19:46:26 +00:00
|
|
|
translate_line->setFocus();
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QLineEdit * TranslateBox::translateLine()
|
|
|
|
{
|
|
|
|
return translate_line;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:42:44 +00:00
|
|
|
WordList * TranslateBox::wordList()
|
2012-12-24 11:30:03 +00:00
|
|
|
{
|
|
|
|
return word_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TranslateBox::rightButtonClicked()
|
|
|
|
{
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( !m_popupEnabled );
|
|
|
|
}
|
|
|
|
|
|
|
|
void TranslateBox::onTextEdit()
|
|
|
|
{
|
|
|
|
if ( translate_line->hasFocus() )
|
|
|
|
setPopupEnabled( true );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|