From d26cb5ea53a3ee77dcbc5a2501e0b8158f76da4e Mon Sep 17 00:00:00 2001 From: "craig.p.drummond" Date: Tue, 22 Apr 2014 18:39:06 +0000 Subject: [PATCH] Combine back button and label into 1 button --- ChangeLog | 1 + widgets/itemview.cpp | 76 +++++++++++++++++++++++--------------------- widgets/itemview.h | 1 - widgets/itemview.ui | 16 +++------- 4 files changed, 45 insertions(+), 49 deletions(-) diff --git a/ChangeLog b/ChangeLog index b4723d896..80d288371 100644 --- a/ChangeLog +++ b/ChangeLog @@ -97,6 +97,7 @@ 60. Fix crash when chaging view mode whilst search is active. 61. Add a 'touch friendly' setting - icons are made larger, and views are 'flickable' +62. Use a toolbutton for listview header and back action. 1.3.4 ----- diff --git a/widgets/itemview.cpp b/widgets/itemview.cpp index 8c9cf1420..1f6133175 100644 --- a/widgets/itemview.cpp +++ b/widgets/itemview.cpp @@ -44,6 +44,7 @@ #include #include #include +#include static int listDecorationSize=22; static int treeDecorationSize=16; @@ -559,6 +560,33 @@ QString ItemView::modeStr(Mode m) static const char *constPageProp="page"; static Action *backAction=0; +// ALL this just becasue styles do not elide the text of toolbuttons!!!!! :-( +class BackButtonProxyStyle : public QProxyStyle +{ +public: + BackButtonProxyStyle() + : QProxyStyle() + { + setBaseStyle(qApp->style()); + } + + void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const + { + if (CC_ToolButton==control) { + if (const QStyleOptionToolButton *toolbutton = qstyleoption_cast(option)) { + QStyleOptionToolButton copy=*toolbutton; + int iconWidth = copy.iconSize.isValid() ? copy.iconSize.width() : baseStyle()->pixelMetric(PM_ToolBarIconSize, option, widget); + int frameWidth=baseStyle()->pixelMetric(PM_DefaultFrameWidth, option, widget); + QFontMetrics fm(painter->fontMetrics()); + copy.text=fm.elidedText(toolbutton->text, Qt::ElideRight, toolbutton->rect.width()-(iconWidth+8+(2*frameWidth)), QPalette::WindowText); + baseStyle()->drawComplexControl(control, ©, painter, widget); + return; + } + } + baseStyle()->drawComplexControl(control, option, painter, widget); + } +}; + ItemView::ItemView(QWidget *p) : QWidget(p) , searchTimer(0) @@ -578,6 +606,15 @@ ItemView::ItemView(QWidget *p) backAction->setShortcut(Qt::AltModifier+(Qt::LeftToRight==layoutDirection() ? Qt::Key_Left : Qt::Key_Right)); } backButton->setDefaultAction(backAction); + backButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + Icon::init(backButton); + backButton->setAutoRaise(true); + #ifdef Q_OS_MAC + backButton->setStyleSheet("QToolButton {border: 0}"); + #endif + backButton->setFocusPolicy(Qt::NoFocus); + backButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + backButton->setStyle(new BackButtonProxyStyle()); Action::updateToolTip(backAction); listView->addAction(backAction); listView->addDefaultAction(backAction); @@ -585,8 +622,6 @@ ItemView::ItemView(QWidget *p) sep->setSeparator(true); listView->addAction(sep); Icon::init(backButton); - title->installEventFilter(this); - title->setAttribute(Qt::WA_Hover, true); treeView->setPageDefaults(); // Some styles, eg Cleanlooks/Plastique require that we explicitly set mouse tracking on the treeview. treeView->setAttribute(Qt::WA_MouseTracking); @@ -819,13 +854,12 @@ void ItemView::setLevel(int l, bool haveChildren) } backButton->setVisible(currentLevel>0); - title->setVisible(currentLevel>0); if (currentLevel>0) { if (prev>currentLevel) { - title->setText(prevText[currentLevel]); + backButton->setText(prevText[currentLevel]); } else { - prevText.insert(prev, title->text()); + prevText.insert(prev, backButton->text()); } } } @@ -1132,34 +1166,6 @@ void ItemView::showEvent(QShowEvent *ev) backAction->setEnabled(0!=currentLevel); } -bool ItemView::eventFilter(QObject *o, QEvent *e) -{ - if (o==title) { - static const char * constPressesProperty="pressed"; - switch (e->type()) { - case QEvent::MouseButtonPress: - if (Qt::NoModifier==static_cast(e)->modifiers() && Qt::LeftButton==static_cast(e)->button()) { - title->setProperty(constPressesProperty, true); - } - break; - case QEvent::MouseButtonRelease: - if (title->property(constPressesProperty).toBool()) { - backActivated(); - } - title->setProperty(constPressesProperty, false); - break; - case QEvent::HoverEnter: - title->setStyleSheet(QLatin1String("QLabel{color:palette(highlight);}")); - break; - case QEvent::HoverLeave: - title->setStyleSheet(QString()); - default: - break; - } - } - return QWidget::eventFilter(o, e); -} - void ItemView::showSpinner(bool v) { if (v) { @@ -1267,9 +1273,7 @@ void ItemView::activateItem(const QModelIndex &index, bool emitRootSet) if (text.isEmpty()) { text=index.data(Qt::DisplayRole).toString(); } - // Add padding so that text is not right on border of label widget. - // This spacing makes the mouse-over background change look nicer. - title->setText(QLatin1String(" ")+text); + backButton->setText(text); listView->setRootIndex(index); itemModel->setRootIndex(index); if (emitRootSet) { diff --git a/widgets/itemview.h b/widgets/itemview.h index 01d26bf2a..7eec3eb1e 100644 --- a/widgets/itemview.h +++ b/widgets/itemview.h @@ -189,7 +189,6 @@ private: void setLevel(int level, bool haveChildren=true); bool usingTreeView() const { return mode<=Mode_DetailedTree; } bool usingListView() const { return mode>=Mode_List; } - bool eventFilter(QObject *o, QEvent *e); public Q_SLOTS: void focusSearch(); diff --git a/widgets/itemview.ui b/widgets/itemview.ui index 90cb869db..907d56903 100644 --- a/widgets/itemview.ui +++ b/widgets/itemview.ui @@ -36,20 +36,17 @@ - + 0 0 - - + + - - - - + @@ -64,11 +61,6 @@ QLabel
squeezedtextlabel.h
- - ToolButton - QToolButton -
toolbutton.h
-
SearchWidget QLineEdit