goldendict-ng/src/ui/translatebox.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
3.3 KiB
C++
Raw Normal View History

/* 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 <QAction>
#include <QHBoxLayout>
#include <QEvent>
#include <QKeyEvent>
#include <QApplication>
#include <QModelIndex>
#include <QScrollBar>
#include <QStyle>
#include <QStringListModel>
#include <QTimer>
TranslateBox::TranslateBox( QWidget * parent ):
QWidget( parent ),
translate_line( new QLineEdit( this ) ),
m_popupEnabled( false )
{
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" );
translate_line->setPlaceholderText( tr( "Type a word or phrase to search dictionaries" ) );
auto layout = new QHBoxLayout( this );
setLayout( layout );
layout->setContentsMargins( 0, 0, 0, 0 );
layout->addWidget( translate_line );
auto dropdown = new QAction( QIcon( ":/icons/1downarrow.svg" ), tr( "Drop-down" ), this );
connect( dropdown, &QAction::triggered, this, &TranslateBox::rightButtonClicked );
translate_line->addAction( dropdown, QLineEdit::TrailingPosition );
translate_line->addAction( new QAction( QIcon( ":/icons/system-search.svg" ), "", this ),
QLineEdit::LeadingPosition );
translate_line->setFocusPolicy( Qt::ClickFocus );
translate_line->installEventFilter( this );
translate_line->setCompleter( completer );
completer->setCompletionMode( QCompleter::UnfilteredPopupCompletion );
completer->setMaxVisibleItems( 16 );
connect( completer,
QOverload< const QString & >::of( &QCompleter::activated ),
translate_line,
[ & ]( const QString & text ) {
translate_line->setText( text );
emit returnPressed();
} );
}
void TranslateBox::setText( const QString & text, bool showPopup )
{
setPopupEnabled( showPopup );
translate_line->setText( text );
}
void TranslateBox::setPopupEnabled( bool enable )
{
m_popupEnabled = enable;
showPopup();
}
2013-02-07 18:15:34 +00:00
void TranslateBox::setSizePolicy( QSizePolicy policy )
{
QWidget::setSizePolicy( policy );
if ( translate_line )
translate_line->setSizePolicy( policy );
}
void TranslateBox::setModel( QStringList & _words )
{
2023-08-12 04:41:08 +00:00
disconnect( completer, 0, translate_line, 0 );
auto model = (QStringListModel *)( completer->model() );
model->setStringList( _words );
connect( completer,
QOverload< const QString & >::of( &QCompleter::activated ),
translate_line,
2023-08-12 05:49:27 +00:00
[ & ]( const QString & text) {
translate_line->setText(text);
emit returnPressed();
} );
}
void TranslateBox::showPopup()
{
if ( m_popupEnabled ) {
completer->popup()->show();
completer->complete();
}
else {
completer->popup()->hide();
}
}
QLineEdit * TranslateBox::translateLine()
{
return translate_line;
}
QWidget * TranslateBox::completerWidget()
{
return completer->widget();
}
void TranslateBox::rightButtonClicked()
{
setPopupEnabled( !m_popupEnabled );
}
void TranslateBox::setSizePolicy( QSizePolicy::Policy hor, QSizePolicy::Policy ver )
{
setSizePolicy( QSizePolicy( hor, ver ) );
}