From d4d31d2daadb7f6a72b26973bd403f96a4fcfad7 Mon Sep 17 00:00:00 2001 From: nowrep Date: Tue, 11 Feb 2014 15:33:02 +0100 Subject: [PATCH] [Bookmarks] Improved drawing of separator Instead of QFrame::Line, use actual ToolbarSeparator. Also add separator into toolbar for default bookmarks.json --- src/lib/bookmarks/bookmarksitemdelegate.cpp | 9 +- src/lib/bookmarks/bookmarkstoolbar.cpp | 26 +---- src/lib/bookmarks/bookmarkstoolbarbutton.cpp | 60 ++++++---- src/lib/bookmarks/bookmarkstoolbarbutton.h | 1 - src/lib/data/data/bookmarks.json | 113 +++++++++---------- 5 files changed, 102 insertions(+), 107 deletions(-) diff --git a/src/lib/bookmarks/bookmarksitemdelegate.cpp b/src/lib/bookmarks/bookmarksitemdelegate.cpp index 7a2ef294e..6f3c1ae30 100644 --- a/src/lib/bookmarks/bookmarksitemdelegate.cpp +++ b/src/lib/bookmarks/bookmarksitemdelegate.cpp @@ -20,7 +20,7 @@ #include "bookmarksmodel.h" #include "bookmarkitem.h" -#include +#include #include #include @@ -35,9 +35,8 @@ void BookmarksItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem QStyledItemDelegate::paint(painter, option, index); if (index.data(BookmarksModel::TypeRole).toInt() == BookmarkItem::Separator) { - QStyleOptionFrameV3 opt; - opt.frameShape = QFrame::HLine; - opt.rect = option.rect; + QStyleOption opt = option; + opt.state &= ~QStyle::State_Horizontal; // We need to fake continuous line over 2 columns if (m_tree->viewType() == BookmarksTreeView::BookmarksManagerViewType) { @@ -50,6 +49,6 @@ void BookmarksItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem } } - QApplication::style()->drawControl(QStyle::CE_ShapedFrame, &opt, painter); + QApplication::style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter); } } diff --git a/src/lib/bookmarks/bookmarkstoolbar.cpp b/src/lib/bookmarks/bookmarkstoolbar.cpp index 99ca9df07..32254909a 100644 --- a/src/lib/bookmarks/bookmarkstoolbar.cpp +++ b/src/lib/bookmarks/bookmarkstoolbar.cpp @@ -77,7 +77,7 @@ void BookmarksToolbar::contextMenuRequested(const QPoint &pos) connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow())); connect(actDelete, SIGNAL(triggered()), this, SLOT(deleteBookmark())); - actDelete->setEnabled(m_clickedBookmark && (m_clickedBookmark->isUrl() || m_clickedBookmark->isFolder())); + actDelete->setEnabled(m_clickedBookmark && m_bookmarks->canBeModified(m_clickedBookmark)); actNewTab->setEnabled(m_clickedBookmark && m_clickedBookmark->isUrl()); actNewWindow->setEnabled(m_clickedBookmark && m_clickedBookmark->isUrl()); @@ -158,26 +158,10 @@ void BookmarksToolbar::addItem(BookmarkItem* item) { Q_ASSERT(item); - switch (item->type()) { - case BookmarkItem::Folder: - case BookmarkItem::Url: { - BookmarksToolbarButton* button = new BookmarksToolbarButton(item, this); - button->setMainWindow(m_window); - button->setShowOnlyIcon(m_bookmarks->showOnlyIconsInToolbar()); - m_layout->addWidget(button); - break; - } - - case BookmarkItem::Separator: { - QFrame* separator = new QFrame(this); - separator->setFrameShape(QFrame::VLine); - m_layout->addWidget(separator); - break; - } - - default: - break; - } + BookmarksToolbarButton* button = new BookmarksToolbarButton(item, this); + button->setMainWindow(m_window); + button->setShowOnlyIcon(m_bookmarks->showOnlyIconsInToolbar()); + m_layout->addWidget(button); } BookmarksToolbarButton* BookmarksToolbar::buttonAt(const QPoint &pos) diff --git a/src/lib/bookmarks/bookmarkstoolbarbutton.cpp b/src/lib/bookmarks/bookmarkstoolbarbutton.cpp index 378e188d8..900ac84cd 100644 --- a/src/lib/bookmarks/bookmarkstoolbarbutton.cpp +++ b/src/lib/bookmarks/bookmarkstoolbarbutton.cpp @@ -27,6 +27,10 @@ #include #include +#define MAX_WIDTH 150 +#define SEPARATOR_WIDTH 8 +#define PADDING 5 + BookmarksToolbarButton::BookmarksToolbarButton(BookmarkItem* bookmark, QWidget* parent) : QPushButton(parent) , m_bookmark(bookmark) @@ -34,7 +38,6 @@ BookmarksToolbarButton::BookmarksToolbarButton(BookmarkItem* bookmark, QWidget* , m_buttons(Qt::NoButton) , m_modifiers(Qt::NoModifier) , m_showOnlyIcon(false) - , m_padding(5) { init(); } @@ -62,29 +65,33 @@ void BookmarksToolbarButton::setShowOnlyIcon(bool show) QSize BookmarksToolbarButton::sizeHint() const { - const int maxWidth = 150; + int width = PADDING * 2 + 16; - int width = m_padding * 2 + 16; - - if (!m_showOnlyIcon) { - width += m_padding * 2 + fontMetrics().width(m_bookmark->title()); + if (m_bookmark->isSeparator()) { + width = SEPARATOR_WIDTH; + } + else if (!m_showOnlyIcon) { + width += PADDING * 2 + fontMetrics().width(m_bookmark->title()); if (menu()) { - width += m_padding + 8; + width += PADDING + 8; } } QSize s = QPushButton::sizeHint(); - s.setWidth(qMin(width, maxWidth)); + s.setWidth(qMin(width, MAX_WIDTH)); return s; } QSize BookmarksToolbarButton::minimumSizeHint() const { - int width = m_padding * 2 + 16; + int width = PADDING * 2 + 16; - if (!m_showOnlyIcon && menu()) { - width += m_padding + 8; + if (m_bookmark->isSeparator()) { + width = SEPARATOR_WIDTH; + } + else if (!m_showOnlyIcon && menu()) { + width += PADDING + 8; } QSize s = QPushButton::minimumSizeHint(); @@ -184,7 +191,6 @@ void BookmarksToolbarButton::openBookmarkInNewWindow(BookmarkItem* item) void BookmarksToolbarButton::init() { Q_ASSERT(m_bookmark); - Q_ASSERT(m_bookmark->type() == BookmarkItem::Url || m_bookmark->type() == BookmarkItem::Folder); setFlat(true); setFocusPolicy(Qt::NoFocus); @@ -262,30 +268,38 @@ void BookmarksToolbarButton::paintEvent(QPaintEvent* event) Q_UNUSED(event) QPainter p(this); - QStyleOptionButton opt; - initStyleOption(&opt); + QStyleOptionButton option; + initStyleOption(&option); + + // Just draw separator + if (m_bookmark->isSeparator()) { + QStyleOption opt = option; + opt.state |= QStyle::State_Horizontal; + style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, &p); + return; + } // This ensures correct rendering of Down state with Oxygen if (isDown()) { - opt.state &= ~QStyle::State_MouseOver; + option.state &= ~QStyle::State_MouseOver; } // Draw button base - style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &p, this); + style()->drawControl(QStyle::CE_PushButtonBevel, &option, &p, this); - const int height = opt.rect.height(); - const int center = height / 2 + opt.rect.top(); + const int height = option.rect.height(); + const int center = height / 2 + option.rect.top(); const int iconSize = 16; const int iconYPos = center - iconSize / 2; - int leftPosition = m_padding; - int rightPosition = opt.rect.right() - m_padding; + int leftPosition = PADDING; + int rightPosition = option.rect.right() - PADDING; // Draw icon QRect iconRect(leftPosition, iconYPos, iconSize, iconSize); p.drawPixmap(iconRect, m_bookmark->icon().pixmap(iconSize)); - leftPosition = iconRect.right() + m_padding; + leftPosition = iconRect.right() + PADDING; // Draw menu arrow if (!m_showOnlyIcon && menu()) { @@ -295,7 +309,7 @@ void BookmarksToolbarButton::paintEvent(QPaintEvent* event) opt.rect = QRect(rightPosition - 8, center - arrowSize / 2, arrowSize, arrowSize); opt.state &= QStyle::State_MouseOver; style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &opt, &p, this); - rightPosition = opt.rect.left() - m_padding; + rightPosition = opt.rect.left() - PADDING; } // Draw text @@ -304,6 +318,6 @@ void BookmarksToolbarButton::paintEvent(QPaintEvent* event) const int textYPos = center - fontMetrics().height() / 2; const QString txt = fontMetrics().elidedText(m_bookmark->title(), Qt::ElideRight, textWidth); QRect textRect(leftPosition, textYPos, textWidth, fontMetrics().height()); - style()->drawItemText(&p, textRect, Qt::TextSingleLine | Qt::AlignCenter, opt.palette, true, txt); + style()->drawItemText(&p, textRect, Qt::TextSingleLine | Qt::AlignCenter, option.palette, true, txt); } } diff --git a/src/lib/bookmarks/bookmarkstoolbarbutton.h b/src/lib/bookmarks/bookmarkstoolbarbutton.h index 2ceaad8b7..b9468cf73 100644 --- a/src/lib/bookmarks/bookmarkstoolbarbutton.h +++ b/src/lib/bookmarks/bookmarkstoolbarbutton.h @@ -69,7 +69,6 @@ private: Qt::MouseButtons m_buttons; Qt::KeyboardModifiers m_modifiers; bool m_showOnlyIcon; - int m_padding; }; diff --git a/src/lib/data/data/bookmarks.json b/src/lib/data/data/bookmarks.json index efc5c78f9..a90338df9 100644 --- a/src/lib/data/data/bookmarks.json +++ b/src/lib/data/data/bookmarks.json @@ -1,74 +1,73 @@ { - "roots" : { - "bookmark_bar" : { - "children" : [ + "roots": { + "bookmark_bar": { + "children": [ { - "description" : "Source code of QupZilla", - "keyword" : "qz-git", - "name" : "QupZilla Git", - "type" : "url", - "url" : "https://github.com/QupZilla/qupzilla", - "visit_count" : 0 + "description": "Source code of QupZilla", + "keyword": "qz-git", + "name": "QupZilla Git", + "type": "url", + "url": "https://github.com/QupZilla/qupzilla", + "visit_count": 0 }, { - "description" : "GitHub Issue Tracker for QupZilla", - "keyword" : "qz-issues", - "name" : "QupZilla Issues", - "type" : "url", - "url" : "https://github.com/QupZilla/qupzilla/issues", - "visit_count" : 0 + "description": "", + "keyword": "qz-wiki", + "name": "QupZilla Wiki", + "type": "url", + "url": "https://github.com/QupZilla/qupzilla/wiki", + "visit_count": 0 }, { - "description" : "", - "keyword" : "qz-wiki", - "name" : "QupZilla Wiki", - "type" : "url", - "url" : "https://github.com/QupZilla/qupzilla/wiki", - "visit_count" : 0 + "description": "GitHub Issue Tracker for QupZilla", + "keyword": "qz-issues", + "name": "QupZilla Issues", + "type": "url", + "url": "https://github.com/QupZilla/qupzilla/issues", + "visit_count": 0 }, { - "description" : "Blog with development updates", - "keyword" : "qz-blog", - "name" : "QupZilla Blog", - "type" : "url", - "url" : "http://blog.qupzilla.com/", - "visit_count" : 0 + "type": "separator" }, { - "description" : "QupZilla Homepage", - "keyword" : "qz-home", - "name" : "QupZilla Home", - "type" : "url", - "url" : "http://www.qupzilla.com/", - "visit_count" : 0 + "description": "Blog with development updates", + "keyword": "qz-blog", + "name": "QupZilla Blog", + "type": "url", + "url": "http://blog.qupzilla.com/", + "visit_count": 0 + }, + { + "description": "QupZilla Homepage", + "keyword": "qz-home", + "name": "QupZilla Home", + "type": "url", + "url": "http://www.qupzilla.com/", + "visit_count": 0 } ], - "description" : "Bookmarks located in Bookmarks Toolbar", - "expanded" : true, - "expanded_sidebar" : true, - "name" : "Bookmarks Toolbar", - "type" : "folder" + "description": "Bookmarks located in Bookmarks Toolbar", + "expanded": true, + "expanded_sidebar": true, + "name": "Bookmarks Toolbar", + "type": "folder" }, - "bookmark_menu" : { - "children" : [ - - ], - "description" : "Bookmarks located in Bookmarks Menu", - "expanded" : true, - "expanded_sidebar" : true, - "name" : "Bookmarks Menu", - "type" : "folder" + "bookmark_menu": { + "children": [], + "description": "Bookmarks located in Bookmarks Menu", + "expanded": true, + "expanded_sidebar": true, + "name": "Bookmarks Menu", + "type": "folder" }, - "other" : { - "children" : [ - - ], - "description" : "All other bookmarks", - "expanded" : true, - "expanded_sidebar" : true, - "name" : "Unsorted Bookmarks", - "type" : "folder" + "other": { + "children": [], + "description": "All other bookmarks", + "expanded": true, + "expanded_sidebar": true, + "name": "Unsorted Bookmarks", + "type": "folder" } }, - "version" : 1 + "version": 1 } \ No newline at end of file