]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
cleanups
[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 = baseBookmark(m_bookmarkManager, 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 KBookmark BookmarkSelector::baseBookmark(KBookmarkManager* bookmarkManager, const KUrl& url)
103 {
104 const KBookmarkGroup root = bookmarkManager->root();
105 KBookmark bookmark = root.first();
106 KBookmark foundBookmark;
107
108 int maxLength = 0;
109
110 // Search the bookmark which is equal to the Url or at least is a parent Url.
111 // If there are more than one possible parent Url candidates, choose the bookmark
112 // which covers the bigger range of the Url.
113 int i = 0;
114 while (!bookmark.isNull()) {
115 const KUrl bookmarkUrl = bookmark.url();
116 if (bookmarkUrl.isParentOf(url)) {
117 const int length = bookmarkUrl.prettyUrl().length();
118 if (length > maxLength) {
119 foundBookmark = bookmark;
120 maxLength = length;
121 }
122 }
123 bookmark = root.next(bookmark);
124 ++i;
125 }
126
127 return foundBookmark;
128 }
129
130 void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
131 {
132 QPainter painter(this);
133
134 const int buttonWidth = width();
135 const int buttonHeight = height();
136
137 QColor backgroundColor;
138 QColor foregroundColor;
139 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
140 isDisplayHintEnabled(DraggedHint);
141 if (isHighlighted) {
142 backgroundColor = KGlobalSettings::highlightColor();
143 foregroundColor = KGlobalSettings::highlightedTextColor();
144 }
145 else {
146 backgroundColor = palette().brush(QPalette::Background).color();
147 foregroundColor = KGlobalSettings::buttonTextColor();
148 }
149
150 // dimm the colors if the parent view does not have the focus
151 const bool isActive = m_urlNavigator->isActive();
152 if (!isActive) {
153 QColor dimmColor(palette().brush(QPalette::Background).color());
154 foregroundColor = mixColors(foregroundColor, dimmColor);
155 if (isHighlighted) {
156 backgroundColor = mixColors(backgroundColor, dimmColor);
157 }
158 }
159
160 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
161 // dimm the foreground color by mixing it with the background
162 foregroundColor = mixColors(foregroundColor, backgroundColor);
163 painter.setPen(foregroundColor);
164 }
165
166 // draw button backround
167 painter.setPen(Qt::NoPen);
168 painter.setBrush(backgroundColor);
169 painter.drawRect(0, 0, buttonWidth, buttonHeight);
170
171 // draw icon
172 const QPixmap pixmap = icon().pixmap();
173 const int x = (buttonWidth - pixmap.width()) / 2;
174 const int y = (buttonHeight - pixmap.height()) / 2;
175 painter.drawPixmap(x, y, pixmap);
176 }
177
178 void BookmarkSelector::activateBookmark(QAction* action)
179 {
180 assert(action != 0);
181 m_selectedAddress = action->data().toString();
182
183 const KBookmark bookmark = selectedBookmark();
184 setPixmap(SmallIcon(bookmark.icon()));
185 emit bookmarkActivated(bookmark.url());
186 }
187
188 #include "bookmarkselector.moc"
189