1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

[Bookmarks] Cache bookmark's icon for 20 seconds

BookmarksToolbarButton: Draw button according to BookmarkItem, not
text and icon properties
This commit is contained in:
nowrep 2014-02-10 16:03:29 +01:00
parent 338a6f991e
commit ad615316c8
10 changed files with 41 additions and 55 deletions

View File

@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "bookmarkitem.h"
#include "iconprovider.h"
BookmarkItem::BookmarkItem(BookmarkItem::Type type, BookmarkItem* parent)
: m_type(type)
@ -69,6 +70,23 @@ QList<BookmarkItem*> BookmarkItem::children() const
return m_children;
}
QIcon BookmarkItem::icon()
{
switch (m_type) {
case Url:
// Cache icon for 20 seconds
if (m_iconTime < QTime::currentTime().addSecs(-20)) {
m_icon = _iconForUrl(m_url);
m_iconTime = QTime::currentTime();
}
return m_icon;
case Folder:
return qIconProvider->standardIcon(QStyle::SP_DirIcon);
default:
return QIcon();
}
}
QString BookmarkItem::urlString() const
{
return QString::fromUtf8(m_url.toEncoded());

View File

@ -20,6 +20,8 @@
#include <QString>
#include <QList>
#include <QIcon>
#include <QTime>
#include <QUrl>
#include "qz_namespace.h"
@ -48,7 +50,9 @@ public:
BookmarkItem* parent() const;
QList<BookmarkItem*> children() const;
QIcon icon();
QString urlString() const;
QUrl url() const;
void setUrl(const QUrl &url);
@ -87,6 +91,8 @@ private:
QString m_title;
QString m_description;
QString m_keyword;
QIcon m_icon;
QTime m_iconTime;
int m_visitCount;
bool m_expanded;
bool m_sidebarExpanded;

View File

@ -158,8 +158,6 @@ void BookmarksMenu::init()
connect(this, SIGNAL(menuMiddleClicked(Menu*)), this, SLOT(menuMiddleClicked(Menu*)));
}
#define FOLDER_ICON style()->standardIcon(QStyle::SP_DirIcon)
void BookmarksMenu::refresh()
{
while (actions().count() != 4) {

View File

@ -18,7 +18,6 @@
#include "bookmarksmodel.h"
#include "bookmarkitem.h"
#include "bookmarks.h"
#include "iconprovider.h"
#include <QApplication>
#include <QMimeData>
@ -127,25 +126,11 @@ QVariant BookmarksModel::data(const QModelIndex &index, int role) const
default:
return QVariant();
}
switch (index.column()) {
case 0:
case 1:
return itm->url().toEncoded();
default:
return QVariant();
}
case Qt::DecorationRole:
if (index.column() != 0) {
return QVariant();
if (index.column() == 0) {
return itm->icon();
}
switch (itm->type()) {
case BookmarkItem::Folder:
return QApplication::style()->standardIcon(QStyle::SP_DirIcon);
case BookmarkItem::Url:
return _iconForUrl(itm->url());
default:
return QVariant();
}
default:
return QVariant();
}

View File

@ -149,7 +149,6 @@ void BookmarksToolbar::showOnlyIconsChanged(bool state)
{
for (int i = 0; i < m_layout->count(); ++i) {
BookmarksToolbarButton* b = qobject_cast<BookmarksToolbarButton*>(m_layout->itemAt(i)->widget());
if (b) {
b->setShowOnlyIcon(state);
}

View File

@ -20,7 +20,6 @@
#include "bookmarkitem.h"
#include "bookmarks.h"
#include "mainapplication.h"
#include "iconprovider.h"
#include "enhancedmenu.h"
#include <QStyle>
@ -58,8 +57,7 @@ bool BookmarksToolbarButton::showOnlyIcon() const
void BookmarksToolbarButton::setShowOnlyIcon(bool show)
{
m_showOnlyIcon = show;
setText(show ? QString() : m_bookmark->title());
updateGeometry();
}
QSize BookmarksToolbarButton::sizeHint() const
@ -69,7 +67,7 @@ QSize BookmarksToolbarButton::sizeHint() const
int width = m_padding * 2 + 16;
if (!m_showOnlyIcon) {
width += m_padding * 2 + fontMetrics().width(text());
width += m_padding * 2 + fontMetrics().width(m_bookmark->title());
if (menu()) {
width += m_padding + 8;
@ -193,16 +191,9 @@ void BookmarksToolbarButton::init()
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
setIconSize(QSize(16, 16));
setMaximumHeight(24);
setText(createText());
setToolTip(createTooltip());
if (m_bookmark->isUrl()) {
setIcon(_iconForUrl(m_bookmark->url()));
}
else if (m_bookmark->isFolder()) {
setIcon(style()->standardIcon(QStyle::SP_DirIcon));
if (m_bookmark->isFolder()) {
Menu* m = new Menu(this);
connect(m, SIGNAL(aboutToShow()), this, SLOT(createMenu()));
connect(m, SIGNAL(menuMiddleClicked(Menu*)), this, SLOT(menuMiddleClicked(Menu*)));
@ -223,11 +214,6 @@ QString BookmarksToolbarButton::createTooltip() const
return m_bookmark->title();
}
QString BookmarksToolbarButton::createText() const
{
return m_bookmark->title();
}
void BookmarksToolbarButton::mousePressEvent(QMouseEvent* event)
{
m_buttons = event->buttons();
@ -295,7 +281,7 @@ void BookmarksToolbarButton::paintEvent(QPaintEvent* event)
// Draw icon
QRect iconRect(leftPosition, iconYPos, iconSize, iconSize);
p.drawPixmap(iconRect, icon().pixmap(iconSize));
p.drawPixmap(iconRect, m_bookmark->icon().pixmap(iconSize));
leftPosition = iconRect.right() + m_padding;
// Draw menu arrow
@ -313,7 +299,7 @@ void BookmarksToolbarButton::paintEvent(QPaintEvent* event)
if (!m_showOnlyIcon) {
const int textWidth = rightPosition - leftPosition;
const int textYPos = center - fontMetrics().height() / 2;
const QString txt = fontMetrics().elidedText(text(), Qt::ElideRight, textWidth);
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);
}

View File

@ -59,7 +59,6 @@ private slots:
private:
void init();
QString createTooltip() const;
QString createText() const;
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);

View File

@ -53,13 +53,11 @@ void BookmarksFoldersMenu::folderChoosed()
}
}
#define FOLDER_ICON QApplication::style()->standardIcon(QStyle::SP_DirIcon)
void BookmarksFoldersMenu::init()
{
#define ADD_MENU(name) \
BookmarkItem* f_##name = mApp->bookmarks()->name(); \
QMenu* m_##name = addMenu(FOLDER_ICON, f_##name->title()); \
QMenu* m_##name = addMenu(f_##name->icon(), f_##name->title()); \
createMenu(m_##name, f_##name);
ADD_MENU(toolbarFolder)
@ -78,7 +76,7 @@ void BookmarksFoldersMenu::createMenu(QMenu* menu, BookmarkItem* parent)
foreach (BookmarkItem* child, parent->children()) {
if (child->isFolder()) {
QMenu* m = menu->addMenu(FOLDER_ICON, child->title());
QMenu* m = menu->addMenu(child->icon(), child->title());
createMenu(m, child);
}
}
@ -108,6 +106,7 @@ void BookmarksFoldersButton::setSelectedFolder(BookmarkItem* folder)
m_selectedFolder = folder;
setText(folder->title());
setIcon(folder->icon());
if (sender()) {
emit selectedFolderChanged(folder);
@ -116,9 +115,7 @@ void BookmarksFoldersButton::setSelectedFolder(BookmarkItem* folder)
void BookmarksFoldersButton::init()
{
setIcon(FOLDER_ICON);
setMenu(m_menu);
setSelectedFolder(m_selectedFolder);
}
@ -275,8 +272,6 @@ void BookmarksTools::openFolderInTabs(QupZilla* window, BookmarkItem* folder)
}
}
#define FOLDER_ICON QApplication::style()->standardIcon(QStyle::SP_DirIcon)
void BookmarksTools::addActionToMenu(QObject* receiver, Menu* menu, BookmarkItem* item)
{
Q_ASSERT(menu);
@ -304,7 +299,7 @@ void BookmarksTools::addFolderToMenu(QObject* receiver, Menu* menu, BookmarkItem
Q_ASSERT(folder->isFolder());
Menu* m = new Menu(folder->title());
m->setIcon(FOLDER_ICON);
m->setIcon(folder->icon());
QObject::connect(m, SIGNAL(menuMiddleClicked(Menu*)), receiver, SLOT(menuMiddleClicked(Menu*)));
QAction* act = menu->addMenu(m);
@ -325,7 +320,7 @@ void BookmarksTools::addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* b
Q_ASSERT(bookmark);
Q_ASSERT(bookmark->isUrl());
Action* act = new Action(_iconForUrl(bookmark->url()), bookmark->title());
Action* act = new Action(bookmark->icon(), bookmark->title());
act->setData(QVariant::fromValue<void*>(static_cast<void*>(bookmark)));
QObject::connect(act, SIGNAL(triggered()), receiver, SLOT(bookmarkActivated()));

View File

@ -170,7 +170,7 @@ void BookmarksImportDialog::showExportedBookmarks()
QTreeWidgetItem* root = new QTreeWidgetItem(ui->treeWidget);
root->setText(0, m_exportedFolder->title());
root->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon));
root->setIcon(0, m_exportedFolder->icon());
ui->treeWidget->addTopLevelItem(root);
foreach (BookmarkItem* b, m_exportedFolder->children()) {
@ -178,7 +178,7 @@ void BookmarksImportDialog::showExportedBookmarks()
if (b->isUrl()) {
QTreeWidgetItem* item = new QTreeWidgetItem(root);
item->setText(0, b->title());
item->setIcon(0, _iconForUrl(b->url()));
item->setIcon(0, b->icon());
item->setText(1, b->urlString());
}
}

View File

@ -68,7 +68,7 @@ void LocationCompleterModel::refreshCompletions(const QString &string)
QStandardItem* item = new QStandardItem();
item->setIcon(_iconForUrl(bookmark->url()));
item->setIcon(bookmark->icon());
item->setText(bookmark->url().toEncoded());
item->setData(-1, IdRole);
item->setData(bookmark->title(), TitleRole);