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"
|
|
|
|
|
2023-03-05 22:41:42 +00:00
|
|
|
#include <QAction>
|
2012-12-24 11:30:03 +00:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QModelIndex>
|
2013-01-27 22:12:00 +00:00
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QStyle>
|
2023-06-05 13:22:20 +00:00
|
|
|
#include <QStringListModel>
|
2023-06-10 02:36:59 +00:00
|
|
|
#include <QTimer>
|
2012-12-24 11:30:03 +00:00
|
|
|
|
2023-03-05 22:41:42 +00:00
|
|
|
TranslateBox::TranslateBox( QWidget * parent ):
|
2023-06-05 13:22:20 +00:00
|
|
|
QWidget( parent ),
|
|
|
|
translate_line( new QLineEdit( this ) ),
|
|
|
|
m_popupEnabled( false )
|
2012-12-24 11:30:03 +00:00
|
|
|
{
|
2023-06-05 13:22:20 +00:00
|
|
|
completer = new QCompleter( words, this );
|
|
|
|
resize( 200, 90 );
|
|
|
|
QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Preferred );
|
|
|
|
sizePolicy.setHorizontalStretch( 0 );
|
|
|
|
sizePolicy.setVerticalStretch( 0 );
|
|
|
|
setSizePolicy( sizePolicy );
|
|
|
|
|
|
|
|
setFocusProxy( translate_line );
|
|
|
|
translate_line->setObjectName( "translateLine" );
|
2012-12-24 11:30:03 +00:00
|
|
|
translate_line->setPlaceholderText( tr( "Type a word or phrase to search dictionaries" ) );
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
auto layout = new QHBoxLayout( this );
|
|
|
|
setLayout( layout );
|
|
|
|
layout->setContentsMargins( 0, 0, 0, 0 );
|
|
|
|
layout->addWidget( translate_line );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
auto dropdown = new QAction( QIcon( ":/icons/1downarrow.svg" ), tr( "Drop-down" ), this );
|
|
|
|
connect( dropdown, &QAction::triggered, this, &TranslateBox::rightButtonClicked );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
translate_line->addAction( dropdown, QLineEdit::TrailingPosition );
|
|
|
|
translate_line->addAction( new QAction( QIcon( ":/icons/system-search.svg" ), "", this ),
|
|
|
|
QLineEdit::LeadingPosition );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
translate_line->setFocusPolicy( Qt::ClickFocus );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
|
|
|
translate_line->installEventFilter( this );
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
translate_line->setCompleter( completer );
|
|
|
|
completer->setCompletionMode( QCompleter::UnfilteredPopupCompletion );
|
|
|
|
completer->setMaxVisibleItems( 16 );
|
2023-09-30 03:23:29 +00:00
|
|
|
completer->popup()->setMinimumHeight( 256 );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
2023-09-20 23:44:58 +00:00
|
|
|
connect( translate_line, &QLineEdit::returnPressed, [ this ]() {
|
|
|
|
emit returnPressed();
|
|
|
|
} );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
void TranslateBox::setText( const QString & text, bool showPopup )
|
2013-01-27 22:12:00 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-07 18:15:34 +00:00
|
|
|
void TranslateBox::setSizePolicy( QSizePolicy policy )
|
|
|
|
{
|
|
|
|
QWidget::setSizePolicy( policy );
|
|
|
|
if ( translate_line )
|
|
|
|
translate_line->setSizePolicy( policy );
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
void TranslateBox::setModel( QStringList & _words )
|
2012-12-24 11:30:03 +00:00
|
|
|
{
|
2023-09-20 23:44:58 +00:00
|
|
|
disconnect( completer, QOverload< const QString & >::of( &QCompleter::activated ), translate_line, nullptr );
|
2023-09-17 03:13:08 +00:00
|
|
|
const auto model = static_cast< QStringListModel * >( completer->model() );
|
2012-12-24 11:30:03 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
model->setStringList( _words );
|
2023-08-12 04:39:23 +00:00
|
|
|
|
2023-09-14 00:06:42 +00:00
|
|
|
completer->popup()->scrollToTop();
|
|
|
|
|
2023-08-12 04:39:23 +00:00
|
|
|
connect( completer,
|
|
|
|
QOverload< const QString & >::of( &QCompleter::activated ),
|
|
|
|
translate_line,
|
2023-09-10 14:06:41 +00:00
|
|
|
[ & ]( const QString & text ) {
|
|
|
|
translate_line->setText( text );
|
2023-09-10 09:52:18 +00:00
|
|
|
emit returnPressed();
|
2023-09-10 14:06:41 +00:00
|
|
|
} );
|
2023-06-05 13:22:20 +00:00
|
|
|
}
|
2013-02-07 15:24:24 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
void TranslateBox::showPopup()
|
|
|
|
{
|
|
|
|
if ( m_popupEnabled ) {
|
|
|
|
completer->popup()->show();
|
|
|
|
completer->complete();
|
2013-02-01 10:15:20 +00:00
|
|
|
}
|
2023-06-05 13:22:20 +00:00
|
|
|
else {
|
|
|
|
completer->popup()->hide();
|
2013-02-01 10:15:20 +00:00
|
|
|
}
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QLineEdit * TranslateBox::translateLine()
|
|
|
|
{
|
|
|
|
return translate_line;
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
QWidget * TranslateBox::completerWidget()
|
2012-12-24 11:30:03 +00:00
|
|
|
{
|
2023-06-05 13:22:20 +00:00
|
|
|
return completer->widget();
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TranslateBox::rightButtonClicked()
|
|
|
|
{
|
2013-01-27 22:12:00 +00:00
|
|
|
setPopupEnabled( !m_popupEnabled );
|
|
|
|
}
|
2023-06-05 13:22:20 +00:00
|
|
|
void TranslateBox::setSizePolicy( QSizePolicy::Policy hor, QSizePolicy::Policy ver )
|
2013-01-27 22:12:00 +00:00
|
|
|
{
|
2023-06-05 13:22:20 +00:00
|
|
|
setSizePolicy( QSizePolicy( hor, ver ) );
|
2012-12-24 11:30:03 +00:00
|
|
|
}
|