]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
Ported to KBookmarkManager::closestBookmark - which even solves another TODO about...
[dolphin.git] / src / bookmarkselector.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "bookmarkselector.h"
21
22 #include "urlnavigator.h"
23
24 #include <assert.h>
25
26 #include <kiconloader.h>
27 #include <kglobalsettings.h>
28 #include <kbookmarkmanager.h>
29 #include <kmenu.h>
30 #include <kdebug.h>
31
32 #include <QPainter>
33 #include <QPixmap>
34
35 BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager) :
36 UrlButton(parent),
37 m_selectedAddress(),
38 m_urlNavigator(parent),
39 m_bookmarkManager(bookmarkManager)
40 {
41 setFocusPolicy(Qt::NoFocus);
42
43 m_bookmarksMenu = new KMenu(this);
44
45 KBookmarkGroup root = m_bookmarkManager->root();
46 KBookmark bookmark = root.first();
47 int i = 0;
48 while (!bookmark.isNull()) {
49 QAction* action = new QAction(MainBarIcon(bookmark.icon()),
50 bookmark.text(),
51 this);
52 m_bookmarksMenu->addAction(action);
53 QString address = QChar('/');
54 address += QString::number(i);
55 action->setData(address);
56 if (address == m_selectedAddress) {
57 QPixmap pixmap = SmallIcon(bookmark.icon());
58 setIcon(QIcon(pixmap));
59 setIconSize(pixmap.size());
60 setMinimumWidth(pixmap.width() + 2);
61 }
62 bookmark = root.next(bookmark);
63 ++i;
64 }
65
66 connect(m_bookmarksMenu, SIGNAL(triggered(QAction*)),
67 this, SLOT(activateBookmark(QAction*)));
68
69 setMenu(m_bookmarksMenu);
70 }
71
72 BookmarkSelector::~BookmarkSelector()
73 {
74 }
75
76 void BookmarkSelector::updateSelection(const KUrl& url)
77 {
78 KBookmark bookmark = m_bookmarkManager->root().closestBookmark(url);
79 if (!bookmark.isNull()) {
80 m_selectedAddress = bookmark.address();
81 setIcon(SmallIcon(bookmark.icon()));
82 }
83 else {
84 m_selectedAddress = QString();
85 // No bookmark has been found which matches to the given Url. Show
86 // a generic folder icon as pixmap for indication:
87 setIcon(SmallIcon("folder"));
88 }
89 }
90
91 KBookmark BookmarkSelector::selectedBookmark() const
92 {
93 return m_bookmarkManager->findByAddress(m_selectedAddress);
94 }
95
96 QSize BookmarkSelector::sizeHint() const
97 {
98 const int height = UrlButton::sizeHint().height();
99 return QSize(height, height);
100 }
101
102 void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
103 {
104 QPainter painter(this);
105
106 const int buttonWidth = width();
107 const int buttonHeight = height();
108
109 QColor backgroundColor;
110 QColor foregroundColor;
111 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
112 isDisplayHintEnabled(DraggedHint);
113 if (isHighlighted) {
114 backgroundColor = KGlobalSettings::highlightColor();
115 foregroundColor = KGlobalSettings::highlightedTextColor();
116 }
117 else {
118 backgroundColor = palette().brush(QPalette::Background).color();
119 foregroundColor = KGlobalSettings::buttonTextColor();
120 }
121
122 // dimm the colors if the parent view does not have the focus
123 const bool isActive = m_urlNavigator->isActive();
124 if (!isActive) {
125 QColor dimmColor(palette().brush(QPalette::Background).color());
126 foregroundColor = mixColors(foregroundColor, dimmColor);
127 if (isHighlighted) {
128 backgroundColor = mixColors(backgroundColor, dimmColor);
129 }
130 }
131
132 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
133 // dimm the foreground color by mixing it with the background
134 foregroundColor = mixColors(foregroundColor, backgroundColor);
135 painter.setPen(foregroundColor);
136 }
137
138 // draw button backround
139 painter.setPen(Qt::NoPen);
140 painter.setBrush(backgroundColor);
141 painter.drawRect(0, 0, buttonWidth, buttonHeight);
142
143 // draw icon
144 const QPixmap pixmap = icon().pixmap();
145 const int x = (buttonWidth - pixmap.width()) / 2;
146 const int y = (buttonHeight - pixmap.height()) / 2;
147 painter.drawPixmap(x, y, pixmap);
148 }
149
150 void BookmarkSelector::activateBookmark(QAction* action)
151 {
152 assert(action != 0);
153 m_selectedAddress = action->data().toString();
154
155 const KBookmark bookmark = selectedBookmark();
156 setPixmap(SmallIcon(bookmark.icon()));
157 emit bookmarkActivated(bookmark.url());
158 }
159
160 #include "bookmarkselector.moc"
161