From 89755f8c09276f827a327381aae06cbf311852d9 Mon Sep 17 00:00:00 2001 From: Abs62 Date: Sat, 6 Jul 2013 19:31:31 +0400 Subject: [PATCH] Refactor work with RTL languages in word search list and history list --- delegate.cc | 19 +++++++++++++++++++ delegate.hh | 18 ++++++++++++++++++ goldendict.pro | 6 ++++-- historypanewidget.cc | 8 ++++++++ historypanewidget.hh | 4 ++++ wordlist.cc | 9 ++++----- wordlist.hh | 2 ++ 7 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 delegate.cc create mode 100644 delegate.hh diff --git a/delegate.cc b/delegate.cc new file mode 100644 index 00000000..c69dcf7e --- /dev/null +++ b/delegate.cc @@ -0,0 +1,19 @@ +#include + +#include "delegate.hh" + +WordListItemDelegate::WordListItemDelegate( QAbstractItemDelegate * delegate ) : +QStyledItemDelegate() +{ + mainDelegate = static_cast< QStyledItemDelegate * >( delegate ); +} + +void WordListItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + QStyleOptionViewItemV4 opt4 = option; + QStyleOptionViewItem opt = option; + initStyleOption( &opt4, index ); + if( opt4.text.isRightToLeft() ) + opt.direction = Qt::RightToLeft; + mainDelegate->paint( painter, opt, index ); +} diff --git a/delegate.hh b/delegate.hh new file mode 100644 index 00000000..28331096 --- /dev/null +++ b/delegate.hh @@ -0,0 +1,18 @@ +#ifndef __DELEGATE_HH_INCLUDED__ +#define __DELEGATE_HH_INCLUDED__ + +#include +#include + +class WordListItemDelegate : public QStyledItemDelegate +{ +public: + WordListItemDelegate( QAbstractItemDelegate * delegate ); + virtual void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const; + +private: + QStyledItemDelegate * mainDelegate; +}; + +#endif + diff --git a/goldendict.pro b/goldendict.pro index cc4094de..d9d42def 100644 --- a/goldendict.pro +++ b/goldendict.pro @@ -242,7 +242,8 @@ HEADERS += folding.hh \ mdx.hh \ voiceengines.hh \ ffmpegaudio.hh \ - articleinspector.hh + articleinspector.hh \ + delegate.hh FORMS += groups.ui \ dictgroupwidget.ui \ @@ -350,7 +351,8 @@ SOURCES += folding.cc \ mdx.cc \ voiceengines.cc \ ffmpegaudio.cc \ - articleinspector.cc + articleinspector.cc \ + delegate.cc win32 { FORMS += texttospeechsource.ui diff --git a/historypanewidget.cc b/historypanewidget.cc index 68687362..dfabc341 100644 --- a/historypanewidget.cc +++ b/historypanewidget.cc @@ -87,6 +87,14 @@ void HistoryPaneWidget::setUp( Config::Class * cfg, History * history, QMenu * connect( m_historyList, SIGNAL( customContextMenuRequested( QPoint const & ) ), this, SLOT( showCustomMenu( QPoint const & ) ) ); + listItemDelegate = new WordListItemDelegate( m_historyList->itemDelegate() ); + m_historyList->setItemDelegate( listItemDelegate ); +} + +HistoryPaneWidget::~HistoryPaneWidget() +{ + if( listItemDelegate ) + delete listItemDelegate; } void HistoryPaneWidget::copySelectedItems() diff --git a/historypanewidget.hh b/historypanewidget.hh index a07d7c82..a8487f63 100644 --- a/historypanewidget.hh +++ b/historypanewidget.hh @@ -14,6 +14,7 @@ #include #include "history.hh" +#include "delegate.hh" /// A widget holding the contents of the 'History' docklet. class HistoryPaneWidget : public QWidget @@ -22,7 +23,9 @@ class HistoryPaneWidget : public QWidget public: explicit HistoryPaneWidget( QWidget * parent = 0 ): QWidget( parent ), itemSelectionChanged( false ) + , listItemDelegate( 0 ) {} + virtual ~HistoryPaneWidget(); virtual QSize sizeHint() const { return QSize( 204, 204 ); } @@ -63,6 +66,7 @@ private: /// when selecting history items via mouse and keyboard bool itemSelectionChanged; + WordListItemDelegate * listItemDelegate; }; class HistoryModel : public QAbstractListModel diff --git a/wordlist.cc b/wordlist.cc index b1db750f..ecc01175 100644 --- a/wordlist.cc +++ b/wordlist.cc @@ -6,9 +6,11 @@ #include "wordlist.hh" WordList::WordList( QWidget * parent ) : QListWidget( parent ) +, listItemDelegate( itemDelegate() ) { wordFinder = 0; translateLine = 0; + setItemDelegate( &listItemDelegate ); } void WordList::attachFinder( WordFinder * finder ) @@ -78,10 +80,8 @@ void WordList::updateMatchResults( bool finished ) i->setFont( f ); } } - if (i->text().at(0).direction() == QChar::DirR) - i->setTextAlignment(Qt::AlignRight); - if (i->text().at(0).direction() == QChar::DirL) - i->setTextAlignment(Qt::AlignLeft); + + i->setTextAlignment(Qt::AlignLeft); } while ( count() > (int) results.size() ) @@ -132,4 +132,3 @@ void WordList::refreshTranslateLine() } } - diff --git a/wordlist.hh b/wordlist.hh index b6ffa94a..c603105a 100644 --- a/wordlist.hh +++ b/wordlist.hh @@ -8,6 +8,7 @@ #include #include "wordfinder.hh" +#include "delegate.hh" class WordList : public QListWidget { @@ -34,6 +35,7 @@ private: WordFinder * wordFinder; QLineEdit * translateLine; + WordListItemDelegate listItemDelegate; }; #endif // WORDLIST_HH